Skip to content

Commit d3409b7

Browse files
committed
docs and update tests
Signed-off-by: lovesh <lovesh.bond@gmail.com>
1 parent 21df38c commit d3409b7

File tree

10 files changed

+150
-84
lines changed

10 files changed

+150
-84
lines changed

equality_across_groups/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ rand = "0.8"
2525
blake2.workspace = true
2626
ark-bls12-381.workspace = true
2727
rand_core = { version = "0.6", default-features = false }
28+
test_utils = { default-features = false, path = "../test_utils" }
2829

2930
[features]
3031
default = ["parallel"]
31-
std = ["ark-ff/std", "ark-ec/std", "ark-std/std", "ark-serialize/std", "rand_core/std", "crypto-bigint/rand", "dock_crypto_utils/std", "bulletproofs_plus_plus/std", "kvac/std", "schnorr_pok/std"]
32-
parallel = ["std", "ark-ff/parallel", "ark-ec/parallel", "ark-std/parallel", "rayon", "dock_crypto_utils/parallel", "bulletproofs_plus_plus/parallel", "kvac/parallel", "schnorr_pok/parallel"]
32+
std = ["ark-ff/std", "ark-ec/std", "ark-std/std", "ark-serialize/std", "rand_core/std", "crypto-bigint/rand", "dock_crypto_utils/std", "bulletproofs_plus_plus/std", "kvac/std", "schnorr_pok/std", "test_utils/std"]
33+
parallel = ["std", "ark-ff/parallel", "ark-ec/parallel", "ark-std/parallel", "rayon", "dock_crypto_utils/parallel", "bulletproofs_plus_plus/parallel", "kvac/parallel", "schnorr_pok/parallel", "test_utils/parallel"]

equality_across_groups/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Protocols for proving equality of committed values across groups.
66
extension in section 5 of the paper [Proofs of discrete logarithm equality across groups](https://eprint.iacr.org/2022/1593). Check the [module](./src/eq_across_groups.rs) for more docs
77
- Implements the protocol to prove elliptic curve point addition and scalar multiplication from the paper [ZKAttest Ring and Group Signatures for Existing ECDSA Keys](https://eprint.iacr.org/2021/1183). Check the [point addition module](./src/ec/sw_point_addition.rs) and [scalar multiplication module](./src/ec/sw_scalar_mult.rs) for more docs
88
- Use the above protocols to prove knowledge of a committed ECDSA public key on Tom-256 curve. Check the [module](./src/pok_ecdsa_pubkey.rs) for more docs
9-
- Use the above protocols to prove knowledge of a committed ECDSA public key on BLS12-381 curve. Check the tests in [module](./src/pok_ecdsa_pubkey.rs).
9+
- Use the above protocols to prove knowledge of a committed ECDSA public key on BLS12-381 curve. Check the test `pok_ecdsa_pubkey_committed_in_bls12_381_commitment` in [module](./src/pok_ecdsa_pubkey.rs).
1010

1111
**CREDIT**
1212

13-
This idea of using these 2 protocols to prove knowledge of ECDSA public key committed on the BLS12-381 curve came from Patrick Amrein from [Unique AG](https://www.unique.ch)
13+
This idea of using these 2 protocols to prove knowledge of ECDSA public key committed on the BLS12-381 curve came from Patrick Amrein from [Ubique](https://ubique.ch/)
1414
and their work [here](https://github.com/UbiqueInnovation/zkattest-rs) is prior art.
1515

1616
<!-- cargo-rdme end -->

equality_across_groups/src/ec/commitments.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl<C: AffineRepr> PointCommitmentWithOpening<C> {
4949
Self::new_given_randomness(point, r_x, r_y, comm_key)
5050
}
5151

52+
/// `r_x` and `r_y` are randomness in the Pedersen commitments to x and y coordinates respectively
5253
pub fn new_given_randomness<P: AffineRepr>(
5354
point: &P,
5455
r_x: C::ScalarField,

equality_across_groups/src/ec/sw_point_addition.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,19 @@ mod tests {
309309
use blake2::Blake2b512;
310310
use dock_crypto_utils::transcript::{new_merlin_transcript, Transcript};
311311
use rand_core::OsRng;
312-
use std::ops::Neg;
312+
use std::{ops::Neg, time::Instant};
313+
use test_utils::statistics::statistics;
313314

314315
#[test]
315316
fn point_addition() {
316317
let mut rng = OsRng::default();
317318

318319
let comm_key = PedersenCommitmentKey::<tomAff>::new::<Blake2b512>(b"test");
319320

320-
for _ in 0..100 {
321+
let mut prov_time = vec![];
322+
let mut ver_time = vec![];
323+
let num_iters = 100;
324+
for i in 0..num_iters {
321325
let a = secpAff::rand(&mut rng);
322326
let b = secpAff::rand(&mut rng);
323327
let t = (a + b).into_affine();
@@ -329,6 +333,7 @@ mod tests {
329333
PointCommitmentWithOpening::<tomAff>::new::<_, secpAff>(&mut rng, &b, &comm_key)
330334
.unwrap();
331335

336+
let start = Instant::now();
332337
let mut prover_transcript = new_merlin_transcript(b"test");
333338
prover_transcript.append(b"comm_key", &comm_key);
334339
prover_transcript.append(b"comm_a", &comm_a.comm);
@@ -345,7 +350,9 @@ mod tests {
345350
&mut prover_transcript,
346351
)
347352
.unwrap();
353+
prov_time.push(start.elapsed());
348354

355+
let start = Instant::now();
349356
let mut verifier_transcript = new_merlin_transcript(b"test");
350357
verifier_transcript.append(b"comm_key", &comm_key);
351358
verifier_transcript.append(b"comm_a", &comm_a.comm);
@@ -360,6 +367,11 @@ mod tests {
360367
&mut verifier_transcript,
361368
)
362369
.unwrap();
370+
ver_time.push(start.elapsed());
371+
372+
if i == 0 {
373+
println!("Proof size = {} bytes", proof.compressed_size());
374+
}
363375

364376
// Verifying with incorrect sum fails
365377
let mut verifier_transcript = new_merlin_transcript(b"test");
@@ -412,5 +424,9 @@ mod tests {
412424
)
413425
.is_err());
414426
}
427+
428+
println!("For {} iterations", num_iters);
429+
println!("Proving time: {:?}", statistics(prov_time));
430+
println!("Verifying time: {:?}", statistics(ver_time));
415431
}
416432
}

equality_across_groups/src/ec/sw_scalar_mult.rs

+57-33
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ mod tests {
327327
use blake2::Blake2b512;
328328
use dock_crypto_utils::transcript::{new_merlin_transcript, Transcript};
329329
use rand_core::OsRng;
330+
use std::time::Instant;
331+
use test_utils::statistics::statistics;
330332

331333
#[test]
332334
fn scalar_mult() {
@@ -335,44 +337,66 @@ mod tests {
335337
let comm_key_1 = PedersenCommitmentKey::<secpAff>::new::<Blake2b512>(b"test1");
336338
let comm_key_2 = PedersenCommitmentKey::<tomAff>::new::<Blake2b512>(b"test2");
337339

338-
let base = secpAff::rand(&mut rng);
339-
let scalar = secpFr::rand(&mut rng);
340-
let result = (base * scalar).into_affine();
340+
let mut prov_time = vec![];
341+
let mut ver_time = vec![];
342+
// Since the proof size depends on the values of the random challenge bits
343+
let mut proof_sizes = vec![];
344+
let num_iters = 10;
345+
for _ in 0..num_iters {
346+
let base = secpAff::rand(&mut rng);
347+
let scalar = secpFr::rand(&mut rng);
348+
let result = (base * scalar).into_affine();
341349

342-
let comm_scalar = CommitmentWithOpening::new(&mut rng, scalar, &comm_key_1);
343-
let comm_result = PointCommitmentWithOpening::new(&mut rng, &result, &comm_key_2).unwrap();
350+
let comm_scalar = CommitmentWithOpening::new(&mut rng, scalar, &comm_key_1);
351+
let comm_result =
352+
PointCommitmentWithOpening::new(&mut rng, &result, &comm_key_2).unwrap();
344353

345-
let mut prover_transcript = new_merlin_transcript(b"test");
346-
prover_transcript.append(b"comm_key_1", &comm_key_1);
347-
prover_transcript.append(b"comm_key_2", &comm_key_2);
348-
prover_transcript.append(b"comm_scalar", &comm_scalar.comm);
349-
prover_transcript.append(b"comm_result", &comm_result.comm);
350-
let proof = ScalarMultiplicationProof::<secpAff, tomAff>::new(
351-
&mut rng,
352-
comm_scalar.clone(),
353-
comm_result.clone(),
354-
result,
355-
base,
356-
&comm_key_1,
357-
&comm_key_2,
358-
&mut prover_transcript,
359-
)
360-
.unwrap();
361-
362-
let mut verifier_transcript = new_merlin_transcript(b"test");
363-
verifier_transcript.append(b"comm_key_1", &comm_key_1);
364-
verifier_transcript.append(b"comm_key_2", &comm_key_2);
365-
verifier_transcript.append(b"comm_scalar", &comm_scalar.comm);
366-
verifier_transcript.append(b"comm_result", &comm_result.comm);
367-
proof
368-
.verify(
369-
&comm_scalar.comm,
370-
&comm_result.comm,
371-
&base,
354+
let start = Instant::now();
355+
let mut prover_transcript = new_merlin_transcript(b"test");
356+
prover_transcript.append(b"comm_key_1", &comm_key_1);
357+
prover_transcript.append(b"comm_key_2", &comm_key_2);
358+
prover_transcript.append(b"comm_scalar", &comm_scalar.comm);
359+
prover_transcript.append(b"comm_result", &comm_result.comm);
360+
let proof = ScalarMultiplicationProof::<secpAff, tomAff>::new(
361+
&mut rng,
362+
comm_scalar.clone(),
363+
comm_result.clone(),
364+
result,
365+
base,
372366
&comm_key_1,
373367
&comm_key_2,
374-
&mut verifier_transcript,
368+
&mut prover_transcript,
375369
)
376370
.unwrap();
371+
prov_time.push(start.elapsed());
372+
373+
proof_sizes.push(proof.compressed_size());
374+
375+
let start = Instant::now();
376+
let mut verifier_transcript = new_merlin_transcript(b"test");
377+
verifier_transcript.append(b"comm_key_1", &comm_key_1);
378+
verifier_transcript.append(b"comm_key_2", &comm_key_2);
379+
verifier_transcript.append(b"comm_scalar", &comm_scalar.comm);
380+
verifier_transcript.append(b"comm_result", &comm_result.comm);
381+
proof
382+
.verify(
383+
&comm_scalar.comm,
384+
&comm_result.comm,
385+
&base,
386+
&comm_key_1,
387+
&comm_key_2,
388+
&mut verifier_transcript,
389+
)
390+
.unwrap();
391+
ver_time.push(start.elapsed());
392+
}
393+
394+
println!("For {} iterations", num_iters);
395+
println!("Proving time: {:?}", statistics(prov_time));
396+
println!("Verifying time: {:?}", statistics(ver_time));
397+
println!(
398+
"Proof size (bytes): {:?}",
399+
statistics::<usize, usize>(proof_sizes)
400+
);
377401
}
378402
}

equality_across_groups/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
//! extension in section 5 of the paper [Proofs of discrete logarithm equality across groups](https://eprint.iacr.org/2022/1593). Check the [module](./src/eq_across_groups.rs) for more docs
77
//! - Implements the protocol to prove elliptic curve point addition and scalar multiplication from the paper [ZKAttest Ring and Group Signatures for Existing ECDSA Keys](https://eprint.iacr.org/2021/1183). Check the [point addition module](./src/ec/sw_point_addition.rs) and [scalar multiplication module](./src/ec/sw_scalar_mult.rs) for more docs
88
//! - Use the above protocols to prove knowledge of a committed ECDSA public key on Tom-256 curve. Check the [module](./src/pok_ecdsa_pubkey.rs) for more docs
9-
//! - Use the above protocols to prove knowledge of a committed ECDSA public key on BLS12-381 curve. Check the tests in [module](./src/pok_ecdsa_pubkey.rs).
9+
//! - Use the above protocols to prove knowledge of a committed ECDSA public key on BLS12-381 curve. Check the test `pok_ecdsa_pubkey_committed_in_bls12_381_commitment` in [module](./src/pok_ecdsa_pubkey.rs).
1010
//!
1111
//! **CREDIT**
1212
//!
13-
//! This idea of using these 2 protocols to prove knowledge of ECDSA public key committed on the BLS12-381 curve came from Patrick Amrein from [Unique AG](https://www.unique.ch)
13+
//! This idea of using these 2 protocols to prove knowledge of ECDSA public key committed on the BLS12-381 curve came from Patrick Amrein from [Ubique](https://ubique.ch/)
1414
//! and their work [here](https://github.com/UbiqueInnovation/zkattest-rs) is prior art.
1515
1616
// TODO: The protocols do a lot of scalar multiplication checks during verification. These can be optimized using a randomized
1717
// linear combination check similar to `RandomizedPairingChecker`
1818

19+
// TODO: Lot of commitments are made using the same commitment key so it would benefit to have a "prepared" commitment key where a `WindowTable`
20+
// for both `g` and `h` exists
21+
1922
pub mod ec;
2023
pub mod eq_across_groups;
2124
pub mod error;

equality_across_groups/src/pok_ecdsa_pubkey.rs

+31-20
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ mod tests {
202202
use dock_crypto_utils::transcript::new_merlin_transcript;
203203
use rand_core::OsRng;
204204
use std::time::{Duration, Instant};
205+
use test_utils::statistics::statistics;
205206

206207
#[test]
207208
fn transformed_sig_verify() {
@@ -229,8 +230,10 @@ mod tests {
229230

230231
let mut prov_time = vec![];
231232
let mut ver_time = vec![];
233+
// Since the proof size depends on the values of the random challenge bits of the scalar multiplication protocol
234+
let mut proof_sizes = vec![];
232235
let num_iters = 10;
233-
for i in 0..num_iters {
236+
for _ in 0..num_iters {
234237
let message = Fr::rand(&mut rng);
235238
let sig = ecdsa::Signature::new_prehashed(&mut rng, message, sk);
236239
let transformed_sig = TransformedEcdsaSig::new(&sig, message, pk).unwrap();
@@ -255,6 +258,8 @@ mod tests {
255258
.unwrap();
256259
prov_time.push(start.elapsed());
257260

261+
proof_sizes.push(proof.compressed_size());
262+
258263
let start = Instant::now();
259264
let mut verifier_transcript = new_merlin_transcript(b"test");
260265
verifier_transcript.append(b"comm_key_secp", &comm_key_secp);
@@ -269,15 +274,14 @@ mod tests {
269274
)
270275
.unwrap();
271276
ver_time.push(start.elapsed());
272-
273-
if i == 0 {
274-
println!("Proof size = {} bytes", proof.compressed_size());
275-
}
276277
}
277278

278-
println!("For {} iterations", num_iters);
279-
println!("Proving time: {:?}", timing_info(prov_time));
280-
println!("Verifying time: {:?}", timing_info(ver_time));
279+
println!("Proving time: {:?}", statistics(prov_time));
280+
println!("Verifying time: {:?}", statistics(ver_time));
281+
println!(
282+
"Proof size (bytes): {:?}",
283+
statistics::<usize, usize>(proof_sizes)
284+
);
281285
}
282286

283287
#[test]
@@ -329,8 +333,11 @@ mod tests {
329333

330334
let mut prov_time = vec![];
331335
let mut ver_time = vec![];
336+
// Since the proof size depends on the values of the random challenge bits of the scalar multiplication protocol
337+
let mut total_proof_sizes = vec![];
338+
let mut dl_eq_proof_sizes = vec![];
332339
let num_iters = 10;
333-
for i in 0..num_iters {
340+
for _ in 0..num_iters {
334341
let message = Fr::rand(&mut rng);
335342
let sig = ecdsa::Signature::new_prehashed(&mut rng, message, sk);
336343

@@ -441,19 +448,23 @@ mod tests {
441448
.unwrap();
442449
ver_time.push(start.elapsed());
443450

444-
if i == 0 {
445-
let s_pk = pok_pubkey.compressed_size();
446-
let s_pk_x = proof_eq_pk_x.compressed_size();
447-
let s_pk_y = proof_eq_pk_y.compressed_size();
448-
println!(
449-
"Total proof size = {} bytes. Proof size for equality of committed x and y coordinates = {} bytes",
450-
s_pk + s_pk_x + s_pk_y, s_pk_x + s_pk_y
451-
);
452-
}
451+
let s_pk = pok_pubkey.compressed_size();
452+
let s_pk_x = proof_eq_pk_x.compressed_size();
453+
let s_pk_y = proof_eq_pk_y.compressed_size();
454+
total_proof_sizes.push(s_pk + s_pk_x + s_pk_y);
455+
dl_eq_proof_sizes.push(s_pk_x + s_pk_y);
453456
}
454457

455458
println!("For {} iterations", num_iters);
456-
println!("Proving time: {:?}", timing_info(prov_time));
457-
println!("Verifying time: {:?}", timing_info(ver_time));
459+
println!("Proving time: {:?}", statistics(prov_time));
460+
println!("Verifying time: {:?}", statistics(ver_time));
461+
println!(
462+
"Total proof size (bytes): {:?}",
463+
statistics::<usize, usize>(total_proof_sizes)
464+
);
465+
println!(
466+
"Proof size for equality of committed x and y coordinates (bytes): {:?}",
467+
statistics::<usize, usize>(dl_eq_proof_sizes)
468+
);
458469
}
459470
}

equality_across_groups/src/util.rs

-23
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,3 @@ pub fn from_bytes_le<const LIMBS: usize>(bytes: &[u8]) -> BigInt<LIMBS> {
1919
}
2020
res
2121
}
22-
23-
#[cfg(test)]
24-
pub fn timing_info(mut times: Vec<std::time::Duration>) -> String {
25-
// Given timings of an operation repeated several times, prints the total time takes, least time,
26-
// median time and the highest time
27-
times.sort();
28-
let median = {
29-
let mid = times.len() / 2;
30-
if times.len() % 2 == 0 {
31-
(times[mid - 1] + times[mid]) / 2
32-
} else {
33-
times[mid]
34-
}
35-
};
36-
let total = times.iter().sum::<std::time::Duration>();
37-
format!(
38-
"{:.2?} | [{:.2?}, {:.2?}, {:.2?}]",
39-
total,
40-
times[0],
41-
median,
42-
times[times.len() - 1]
43-
)
44-
}

test_utils/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ pub mod bbs;
1111
pub mod serialization;
1212
pub mod kvac;
1313
pub mod ot;
14+
pub mod statistics;

test_utils/src/statistics.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::{
2+
fmt::Debug,
3+
iter::Sum,
4+
ops::{Add, Div},
5+
};
6+
7+
/// Prints the total, least, median, and the highest value of the given list
8+
pub fn statistics<T, U>(mut values: Vec<T>) -> String
9+
where
10+
T: Copy + Ord + Add<Output = T> + Sum<T> + Div<U, Output = T> + Debug,
11+
U: From<u8>,
12+
{
13+
values.sort();
14+
let two = U::from(2);
15+
16+
let median = {
17+
let mid = values.len() / 2;
18+
if values.len() % 2 == 0 {
19+
(values[mid - 1] + values[mid]) / two
20+
} else {
21+
values[mid]
22+
}
23+
};
24+
let total: T = values.iter().copied().sum();
25+
format!(
26+
"{:.2?} | [{:.2?}, {:.2?}, {:.2?}]",
27+
total,
28+
values.first().unwrap(),
29+
median,
30+
values.last().unwrap()
31+
)
32+
}

0 commit comments

Comments
 (0)