From fccd85fa3f98a0082cac82ca523b53c6fb8c1e1c Mon Sep 17 00:00:00 2001 From: aniampio Date: Thu, 23 Nov 2023 17:26:11 +0000 Subject: [PATCH] Replace for loop with par_iter for signing expiration date --- .../benches/benchmarks.rs | 3 ++- .../src/scheme/expiration_date_signatures.rs | 22 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/common/nym_offline_compact_ecash/benches/benchmarks.rs b/common/nym_offline_compact_ecash/benches/benchmarks.rs index d694cb91b4..79c4e1d21b 100644 --- a/common/nym_offline_compact_ecash/benches/benchmarks.rs +++ b/common/nym_offline_compact_ecash/benches/benchmarks.rs @@ -167,6 +167,7 @@ fn bench_compact_ecash(c: &mut Criterion) { }; let params = setup(case.L); + let grp = params.grp(); let user_keypair = generate_keypair_user(&grp); let threshold = (case.threshold_p * case.num_authorities as f32).round() as u64; @@ -449,5 +450,5 @@ fn bench_aggregate_expiration_date_signatures(c: &mut Criterion){ } -criterion_group!(benches, bench_aggregate_expiration_date_signatures); +criterion_group!(benches, bench_partial_sign_expiration_date); criterion_main!(benches); diff --git a/common/nym_offline_compact_ecash/src/scheme/expiration_date_signatures.rs b/common/nym_offline_compact_ecash/src/scheme/expiration_date_signatures.rs index c920a39a9f..1720e9fb26 100644 --- a/common/nym_offline_compact_ecash/src/scheme/expiration_date_signatures.rs +++ b/common/nym_offline_compact_ecash/src/scheme/expiration_date_signatures.rs @@ -42,14 +42,12 @@ pub fn sign_expiration_date( sk_auth: &SecretKeyAuth, expiration_date: u64 ) -> Vec{ - + let m0: Scalar = Scalar::from(expiration_date); + let m2: Scalar = Scalar::from_bytes(&constants::TYPE_EXP).unwrap(); // Initialize a vector to collect exp_sign values let mut exp_signs = Vec::with_capacity(constants::VALIDITY_PERIOD as usize); - - for l in 0..constants::VALIDITY_PERIOD { - let m0: Scalar = Scalar::from(expiration_date); - let m1: Scalar = Scalar::from(expiration_date - constants::VALIDITY_PERIOD + l); - let m2: Scalar = Scalar::from_bytes(&constants::TYPE_EXP).unwrap(); + exp_signs.par_iter_mut().enumerate().for_each(|(l, exp_sign)| { + let m1: Scalar = Scalar::from(expiration_date - constants::VALIDITY_PERIOD + l as u64); // Compute the hash let h = hash_g1([m0.to_bytes(), m1.to_bytes(), m2.to_bytes()].concat()); // Sign the attributes by performing scalar-point multiplications and accumulating the result @@ -58,13 +56,12 @@ pub fn sign_expiration_date( s_exponent += &sk_auth.ys[1] * m1; s_exponent += &sk_auth.ys[2] * m2; // Create the signature struct on the expiration date - let exp_sign = PartialExpirationDateSignature { + *exp_sign = PartialExpirationDateSignature { h, s: h * s_exponent, }; - // Collect the exp_sign value into the vector - exp_signs.push(exp_sign); - } + }); + exp_signs } @@ -92,13 +89,14 @@ pub fn verify_valid_dates_signatures( signatures: &[ExpirationDateSignature], expiration_date: u64, ) -> Result<()>{ + let m0: Scalar = Scalar::from(expiration_date); + let m2: Scalar = Scalar::from_bytes(&constants::TYPE_EXP).unwrap(); + signatures .par_iter() .enumerate() .try_for_each(|(l, sig)| { - let m0: Scalar = Scalar::from(expiration_date); let m1: Scalar = Scalar::from(expiration_date - constants::VALIDITY_PERIOD + l as u64); - let m2: Scalar = Scalar::from_bytes(&constants::TYPE_EXP).unwrap(); // Compute the hash let h = hash_g1([m0.to_bytes(), m1.to_bytes(), m2.to_bytes()].concat()); // Verify the signature correctness