-
Notifications
You must be signed in to change notification settings - Fork 22
/
perfect_range.rs
404 lines (368 loc) · 14.3 KB
/
perfect_range.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Range proof protocol as described in Fig.3 of the paper [Efficient Protocols for Set Membership and Range Proofs](https://link.springer.com/chapter/10.1007/978-3-540-89255-7_15).
//! Considers a perfect-range, i.e. range of the form `[0, u^l)` where `u` is the base and the upper bound is a power of the base.
//! The calculations are changed a bit to be consistent with other instances of Schnorr protocol in this project.
use crate::{
ccs_set_membership::setup::SetMembershipCheckParamsWithPairing, common::MemberCommitmentKey,
error::SmcRangeProofError,
};
use ark_ec::{
pairing::{Pairing, PairingOutput},
AffineRepr, CurveGroup,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{cfg_into_iter, io::Write, ops::Mul, rand::RngCore, vec::Vec, UniformRand};
use dock_crypto_utils::{
expect_equality, misc::n_rand, msm::multiply_field_elems_with_same_group_elem,
};
use crate::common::padded_base_n_digits_as_field_elements;
use dock_crypto_utils::randomized_pairing_check::RandomizedPairingChecker;
use crate::ccs_range_proof::util::{check_commitment_for_prefect_range, find_l};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CCSPerfectRangeProofProtocol<E: Pairing> {
pub base: u16,
pub digits: Vec<E::ScalarField>,
pub r: E::ScalarField,
pub v: Vec<E::ScalarField>,
pub V: Vec<E::G1Affine>,
pub a: Vec<PairingOutput<E>>,
pub D: E::G1Affine,
pub m: E::ScalarField,
pub s: Vec<E::ScalarField>,
pub t: Vec<E::ScalarField>,
}
#[derive(Clone, PartialEq, Eq, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct CCSPerfectRangeProof<E: Pairing> {
pub base: u16,
pub V: Vec<E::G1Affine>,
pub a: Vec<PairingOutput<E>>,
pub D: E::G1Affine,
pub z_v: Vec<E::ScalarField>,
pub z_sigma: Vec<E::ScalarField>,
pub z_r: E::ScalarField,
}
impl<E: Pairing> CCSPerfectRangeProofProtocol<E> {
pub fn init<R: RngCore>(
rng: &mut R,
value: u64,
randomness: E::ScalarField,
max: u64,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
) -> Result<Self, SmcRangeProofError> {
let params = params.into();
Self::init_given_base(
rng,
value,
randomness,
max,
params.get_supported_base_for_range_proof(),
comm_key,
params,
)
}
pub fn init_given_base<R: RngCore>(
rng: &mut R,
value: u64,
randomness: E::ScalarField,
max: u64,
base: u16,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
) -> Result<Self, SmcRangeProofError> {
let params = params.into();
params.validate_base(base)?;
let l = find_l(max, base) as usize;
// Note: This is different from the paper as only a single `m` needs to be created.
let m = E::ScalarField::rand(rng);
let s = n_rand(rng, l).collect::<Vec<E::ScalarField>>();
let D = comm_key.commit_decomposed(base, &s, &m);
let digits = padded_base_n_digits_as_field_elements(value, base, l);
let t = n_rand(rng, l).collect::<Vec<E::ScalarField>>();
let v = n_rand(rng, l).collect::<Vec<_>>();
let V = randomize_sigs!(&digits, &v, ¶ms);
// Following is different from the paper, the paper has `-s` and `t` but here its opposite
let a = cfg_into_iter!(0..l)
.map(|i| {
E::pairing(
E::G1Prepared::from(V[i] * s[i]),
params.bb_sig_params.g2_prepared.clone(),
) + params.bb_sig_params.g1g2.mul(-t[i])
})
.collect::<Vec<_>>();
Ok(Self {
base,
digits,
r: randomness,
v,
V: E::G1::normalize_batch(&V),
a,
D,
m,
s,
t,
})
}
pub fn challenge_contribution<W: Write>(
&self,
commitment: &E::G1Affine,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
writer: W,
) -> Result<(), SmcRangeProofError> {
Self::compute_challenge_contribution(
&self.V, &self.a, &self.D, commitment, comm_key, params, writer,
)
}
pub fn gen_proof(self, challenge: &E::ScalarField) -> CCSPerfectRangeProof<E> {
gen_proof_perfect_range!(self, challenge, CCSPerfectRangeProof)
}
pub fn compute_challenge_contribution<W: Write>(
V: &[E::G1Affine],
a: &[PairingOutput<E>],
D: &E::G1Affine,
commitment: &E::G1Affine,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
mut writer: W,
) -> Result<(), SmcRangeProofError> {
let params = params.into();
params.serialize_for_schnorr_protocol(&mut writer)?;
comm_key.serialize_compressed(&mut writer)?;
commitment.serialize_compressed(&mut writer)?;
for V_i in V {
V_i.serialize_compressed(&mut writer)?;
}
for a_i in a {
a_i.serialize_compressed(&mut writer)?;
}
D.serialize_compressed(&mut writer)?;
Ok(())
}
}
impl<E: Pairing> CCSPerfectRangeProof<E> {
pub fn verify(
&self,
commitment: &E::G1Affine,
challenge: &E::ScalarField,
max: u64,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
) -> Result<(), SmcRangeProofError> {
let params = params.into();
self.verify_except_pairings(commitment, challenge, max, comm_key, ¶ms)?;
let (yc_sigma, lhs) = self.compute_for_pairing_check(challenge, ¶ms);
if cfg_into_iter!(0..self.V.len())
.map(|i| {
let rhs = E::pairing(
E::G1Prepared::from(self.V[i]),
E::G2Prepared::from(yc_sigma[i]),
);
lhs[i] == rhs
})
.any(|r| r == false)
{
return Err(SmcRangeProofError::InvalidRangeProof);
}
Ok(())
}
pub fn verify_given_randomized_pairing_checker(
&self,
commitment: &E::G1Affine,
challenge: &E::ScalarField,
max: u64,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
pairing_checker: &mut RandomizedPairingChecker<E>,
) -> Result<(), SmcRangeProofError> {
let params = params.into();
self.verify_except_pairings(commitment, challenge, max, comm_key, ¶ms)?;
let (yc_sigma, lhs) = self.compute_for_pairing_check(challenge, ¶ms);
for i in 0..self.V.len() {
pairing_checker.add_multiple_sources_and_target(&[self.V[i]], &[yc_sigma[i]], &lhs[i]);
}
Ok(())
}
pub fn verify_except_pairings(
&self,
commitment: &E::G1Affine,
challenge: &E::ScalarField,
max: u64,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: &SetMembershipCheckParamsWithPairing<E>,
) -> Result<(), SmcRangeProofError> {
params.validate_base(self.base)?;
let l = find_l(max, self.base) as usize;
expect_equality!(
self.V.len(),
l,
SmcRangeProofError::ProofShorterThanExpected
);
expect_equality!(
self.a.len(),
l,
SmcRangeProofError::ProofShorterThanExpected
);
expect_equality!(
self.z_v.len(),
l,
SmcRangeProofError::ProofShorterThanExpected
);
expect_equality!(
self.z_sigma.len(),
l,
SmcRangeProofError::ProofShorterThanExpected
);
check_commitment_for_prefect_range::<E>(
self.base,
&self.z_sigma,
&self.z_r,
&self.D,
commitment,
challenge,
comm_key,
)
}
pub fn challenge_contribution<W: Write>(
&self,
commitment: &E::G1Affine,
comm_key: &MemberCommitmentKey<E::G1Affine>,
params: impl Into<SetMembershipCheckParamsWithPairing<E>>,
writer: W,
) -> Result<(), SmcRangeProofError> {
CCSPerfectRangeProofProtocol::compute_challenge_contribution(
&self.V, &self.a, &self.D, commitment, comm_key, params, writer,
)
}
fn compute_for_pairing_check(
&self,
challenge: &E::ScalarField,
params: &SetMembershipCheckParamsWithPairing<E>,
) -> (Vec<E::G2>, Vec<PairingOutput<E>>) {
// y * c
let yc = params.bb_pk.0 * challenge;
// g2_z_sigma_i = g2 * z_sigma_i
let g2_z_sigma = multiply_field_elems_with_same_group_elem(
params.bb_sig_params.g2.into_group(),
&self.z_sigma,
);
// lhs_i = a_i + e(g1, g2) * z_v_i
let lhs = cfg_into_iter!(0..self.V.len())
.map(|i| self.a[i] + (params.bb_sig_params.g1g2 * self.z_v[i]))
.collect::<Vec<_>>();
// yc_sigma_i = yc + g2_z_sigma_i
let yc_sigma = cfg_into_iter!(0..g2_z_sigma.len())
.map(|i| yc + g2_z_sigma[i])
.collect::<Vec<_>>();
(yc_sigma, lhs)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ccs_set_membership::setup::SetMembershipCheckParams;
use ark_bls12_381::{Bls12_381, Fr, G1Affine};
use ark_std::{
rand::{rngs::StdRng, SeedableRng},
UniformRand,
};
use blake2::Blake2b512;
use schnorr_pok::compute_random_oracle_challenge;
use std::time::{Duration, Instant};
#[test]
fn range_proof_for_perfect_range() {
let mut rng = StdRng::seed_from_u64(0u64);
for base in [2, 4, 8, 16] {
let (params, _) = SetMembershipCheckParams::<Bls12_381>::new_for_range_proof::<
_,
Blake2b512,
>(&mut rng, b"test", base);
params.verify().unwrap();
let params_with_pairing = SetMembershipCheckParamsWithPairing::from(params.clone());
params_with_pairing.verify().unwrap();
let comm_key = MemberCommitmentKey::<G1Affine>::generate_using_rng(&mut rng);
for l in [10, 15] {
let mut proving_time = Duration::default();
let mut verifying_time = Duration::default();
let mut verifying_with_rpc_time = Duration::default();
let mut proof_size = 0;
for _ in 0..5 {
// TODO: Combine base and l in outer for loop
let max = (base as u64).pow(l);
let value = u64::rand(&mut rng) % max;
assert!(value < max);
let randomness = Fr::rand(&mut rng);
let commitment = comm_key.commit(&Fr::from(value), &randomness);
let start = Instant::now();
let protocol = CCSPerfectRangeProofProtocol::init_given_base(
&mut rng,
value,
randomness,
max,
base,
&comm_key,
params_with_pairing.clone(),
)
.unwrap();
let mut chal_bytes_prover = vec![];
protocol
.challenge_contribution(
&commitment,
&comm_key,
params_with_pairing.clone(),
&mut chal_bytes_prover,
)
.unwrap();
let challenge_prover =
compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_bytes_prover);
let proof = protocol.gen_proof(&challenge_prover);
proving_time += start.elapsed();
let start = Instant::now();
let mut chal_bytes_verifier = vec![];
proof
.challenge_contribution(
&commitment,
&comm_key,
params_with_pairing.clone(),
&mut chal_bytes_verifier,
)
.unwrap();
let challenge_verifier =
compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_bytes_verifier);
assert_eq!(challenge_prover, challenge_verifier);
assert_eq!(proof.V.len(), l as usize);
proof
.verify(
&commitment,
&challenge_verifier,
max,
&comm_key,
params_with_pairing.clone(),
)
.unwrap();
verifying_time += start.elapsed();
let mut pairing_checker =
RandomizedPairingChecker::new_using_rng(&mut rng, true);
let start = Instant::now();
proof
.verify_given_randomized_pairing_checker(
&commitment,
&challenge_verifier,
max,
&comm_key,
params_with_pairing.clone(),
&mut pairing_checker,
)
.unwrap();
assert!(pairing_checker.verify());
verifying_with_rpc_time += start.elapsed();
let mut bytes = vec![];
proof.serialize_compressed(&mut bytes).unwrap();
proof_size = bytes.len();
}
println!("For base {} and max {}, proof size = {}, average proving time = {:?}, average verifying time = {:?} and average verifying time using randomized pairing checker={:?}", base, (base as u64).pow(l), proof_size, proving_time / 5, verifying_time / 5, verifying_with_rpc_time/5);
}
}
}
}