-
Notifications
You must be signed in to change notification settings - Fork 12
/
nonmembership_hash.rs
159 lines (148 loc) · 5.01 KB
/
nonmembership_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
156
157
158
159
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,
},
nonmembership::{
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::nonmembership::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);
let value = Integer::from(Integer::u_pow_u(
2,
(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 acc_set = LARGE_PRIMES
.iter()
.skip(1)
.map(|p| Integer::from(*p))
.collect::<Vec<_>>();
let accum = accum.add(&acc_set);
let non_mem_proof = accum
.prove_nonmembership(&acc_set, &[hashed_value.clone()])
.unwrap();
let acc = accum.value;
let d = non_mem_proof.d.clone();
let b = non_mem_proof.b;
assert_eq!(
Rsa2048::op(&Rsa2048::exp(&d, &hashed_value), &Rsa2048::exp(&acc, &b)),
protocol.crs.crs_coprime.integer_commitment_parameters.g
);
let proof_transcript = RefCell::new(Transcript::new(b"nonmembership"));
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(),
d: d.clone(),
b: b.clone(),
},
)
.unwrap();
let proof = verifier_channel.proof().unwrap();
let verification_transcript = RefCell::new(Transcript::new(b"nonmembership"));
let mut prover_channel = TranscriptProverChannel::new(&crs, &verification_transcript, &proof);
protocol.verify(&mut prover_channel, &statement).unwrap();
c.bench_function("nonmembership_hash protocol proving", |be| {
be.iter(|| {
let proof_transcript = RefCell::new(Transcript::new(b"nonmembership"));
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(),
d: d.clone(),
b: b.clone(),
},
)
.unwrap();
})
});
c.bench_function("nonmembership_hash protocol verification", |be| {
be.iter(|| {
let verification_transcript = RefCell::new(Transcript::new(b"nonmembership"));
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);