-
Notifications
You must be signed in to change notification settings - Fork 12
/
membership_class.rs
134 lines (124 loc) · 4.26 KB
/
membership_class.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
use accumulator::group::ClassGroup;
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_range::Protocol as HPProtocol,
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,
];
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::<
ClassGroup,
G1Projective,
HPProtocol<Bls12_381>,
>::setup(¶ms, &mut rng1, &mut rng2)
.unwrap()
.crs;
let protocol = Protocol::<ClassGroup, G1Projective, HPProtocol<Bls12_381>>::from_crs(&crs);
let value = Integer::from(Integer::u_pow_u(
2,
(crs.parameters.hash_to_prime_bits) as u32,
)) - &Integer::from(245);
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(&value, &randomness)
.unwrap();
let accum =
accumulator::Accumulator::<ClassGroup, 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(&[value.clone()]);
let acc = accum.0.value;
let w = accum.1.witness.0.value;
assert_eq!(ClassGroup::exp(&w, &value), acc);
let proof_transcript = RefCell::new(Transcript::new(b"membership"));
let mut verifier_channel = TranscriptVerifierChannel::new(&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(&crs, &verification_transcript, &proof);
protocol.verify(&mut prover_channel, &statement).unwrap();
c.bench_function("membership_class protocol proving", |b| {
b.iter(|| {
let proof_transcript = RefCell::new(Transcript::new(b"membership"));
let mut verifier_channel = TranscriptVerifierChannel::new(&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_class protocol verification", |b| {
b.iter(|| {
let verification_transcript = RefCell::new(Transcript::new(b"membership"));
let mut prover_channel =
TranscriptProverChannel::new(&crs, &verification_transcript, &proof);
protocol.verify(&mut prover_channel, &statement).unwrap();
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);