-
Notifications
You must be signed in to change notification settings - Fork 12
/
membership_hash.rs
155 lines (144 loc) · 4.82 KB
/
membership_hash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use accumulator::group::Rsa2048;
use accumulator::{group::Group, AccumulatorWithoutHashToPrime};
use algebra::{
bls12_381::{Bls12_381, Fr, G1Projective},
PrimeField,
};
use cpsnarks_set::{
commitments::Commitment,
parameters::Parameters,
protocols::{
hash_to_prime::{
snark_hash::{HashToPrimeHashParameters, Protocol as HPProtocol},
CRSSize,
},
membership::{
transcript::{TranscriptProverChannel, TranscriptVerifierChannel},
Protocol, Statement, Witness,
},
},
};
use criterion::{criterion_group, criterion_main, Criterion};
use merlin::Transcript;
use rand::thread_rng;
use rug::rand::RandState;
use rug::Integer;
use std::cell::RefCell;
const LARGE_PRIMES: [u64; 3] = [
12_702_637_924_034_044_211,
378_373_571_372_703_133,
8_640_171_141_336_142_787,
];
struct TestHashToPrimeParameters {}
impl HashToPrimeHashParameters for TestHashToPrimeParameters {
const MESSAGE_SIZE: u16 = 254;
}
pub fn criterion_benchmark(c: &mut Criterion) {
let params = Parameters::from_curve::<Fr>().unwrap().0;
println!("params: {}", params);
let mut rng1 = RandState::new();
rng1.seed(&Integer::from(13));
let mut rng2 = thread_rng();
let crs = cpsnarks_set::protocols::membership::Protocol::<
Rsa2048,
G1Projective,
HPProtocol<Bls12_381, TestHashToPrimeParameters>,
>::setup(¶ms, &mut rng1, &mut rng2)
.unwrap()
.crs;
println!(
"crs size: {:?}",
crs.crs_hash_to_prime.hash_to_prime_parameters.crs_size()
);
let protocol = Protocol::<
Rsa2048,
G1Projective,
HPProtocol<Bls12_381, TestHashToPrimeParameters>,
>::from_crs(&crs);
drop(crs);
let value = Integer::from(Integer::u_pow_u(
2,
(protocol.crs.parameters.hash_to_prime_bits) as u32,
))
.random_below(&mut rng1);
let (hashed_value, _) = protocol.hash_to_prime(&value).unwrap();
let randomness =
Integer::from(Integer::u_pow_u(2, Fr::size_in_bits() as u32)).random_below(&mut rng1);
let commitment = protocol
.crs
.crs_modeq
.pedersen_commitment_parameters
.commit(&hashed_value, &randomness)
.unwrap();
let accum =
accumulator::Accumulator::<Rsa2048, Integer, AccumulatorWithoutHashToPrime>::empty();
let accum = accum.add(
&LARGE_PRIMES
.iter()
.skip(1)
.map(|p| Integer::from(*p))
.collect::<Vec<_>>(),
);
let accum = accum.add_with_proof(&[hashed_value.clone()]);
let acc = accum.0.value;
let w = accum.1.witness.0.value;
assert_eq!(Rsa2048::exp(&w, &hashed_value), acc);
let proof_transcript = RefCell::new(Transcript::new(b"membership"));
let mut verifier_channel = TranscriptVerifierChannel::new(&protocol.crs, &proof_transcript);
let statement = Statement {
c_e_q: commitment,
c_p: acc.clone(),
};
protocol
.prove(
&mut verifier_channel,
&mut rng1,
&mut rng2,
&statement,
&Witness {
e: value.clone(),
r_q: randomness.clone(),
w: w.clone(),
},
)
.unwrap();
let proof = verifier_channel.proof().unwrap();
let verification_transcript = RefCell::new(Transcript::new(b"membership"));
let mut prover_channel =
TranscriptProverChannel::new(&protocol.crs, &verification_transcript, &proof);
protocol.verify(&mut prover_channel, &statement).unwrap();
c.bench_function("membership_hash protocol proving", |b| {
b.iter(|| {
let proof_transcript = RefCell::new(Transcript::new(b"membership"));
let mut verifier_channel =
TranscriptVerifierChannel::new(&protocol.crs, &proof_transcript);
let statement = Statement {
c_e_q: commitment,
c_p: acc.clone(),
};
protocol
.prove(
&mut verifier_channel,
&mut rng1,
&mut rng2,
&statement,
&Witness {
e: value.clone(),
r_q: randomness.clone(),
w: w.clone(),
},
)
.unwrap();
})
});
c.bench_function("membership_hash protocol verification", |b| {
b.iter(|| {
let verification_transcript = RefCell::new(Transcript::new(b"membership"));
let mut prover_channel =
TranscriptProverChannel::new(&protocol.crs, &verification_transcript, &proof);
protocol.verify(&mut prover_channel, &statement).unwrap();
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);