diff --git a/common/nym_offline_compact_ecash/src/scheme/setup.rs b/common/nym_offline_compact_ecash/src/scheme/setup.rs index daa596c97e..0deca83be8 100644 --- a/common/nym_offline_compact_ecash/src/scheme/setup.rs +++ b/common/nym_offline_compact_ecash/src/scheme/setup.rs @@ -153,32 +153,55 @@ pub fn sign_coin_indices( ) -> Vec { let m1: Scalar = Scalar::from_bytes(&constants::TYPE_IDX).unwrap(); let m2: Scalar = Scalar::from_bytes(&constants::TYPE_IDX).unwrap(); - // Initialize a vector to collect the (partial) coin signatures let mut partial_coins_signatures = Vec::with_capacity(params.L() as usize); + for l in 0..params.L(){ + let m0: Scalar = Scalar::from(l as u64); + // Compute the hash h + let mut concatenated_bytes = + Vec::with_capacity(vk.to_bytes().len() + l.to_le_bytes().len()); + concatenated_bytes.extend_from_slice(&vk.to_bytes()); + concatenated_bytes.extend_from_slice(&l.to_le_bytes()); + let h = hash_g1(concatenated_bytes); - partial_coins_signatures.par_iter_mut() - .enumerate() - .for_each(|(l, coin_idx_sign)| { - let m0: Scalar = Scalar::from(l as u64); - // Compute the hash h - let mut concatenated_bytes = - Vec::with_capacity(vk.to_bytes().len() + l.to_le_bytes().len()); - concatenated_bytes.extend_from_slice(&vk.to_bytes()); - concatenated_bytes.extend_from_slice(&l.to_le_bytes()); - let h = hash_g1(concatenated_bytes); - - // Sign the attributes by performing scalar-point multiplications and accumulating the result - let mut s_exponent = sk_auth.x; - s_exponent += &sk_auth.ys[0] * m0; - s_exponent += &sk_auth.ys[1] * m1; - s_exponent += &sk_auth.ys[2] * m2; - // Create the signature struct of on the coin index - *coin_idx_sign = CoinIndexSignature { - h, - s: h * s_exponent, - }; - }); + // Sign the attributes by performing scalar-point multiplications and accumulating the result + let mut s_exponent = sk_auth.x; + s_exponent += &sk_auth.ys[0] * m0; + s_exponent += &sk_auth.ys[1] * m1; + s_exponent += &sk_auth.ys[2] * m2; + // Create the signature struct of on the coin index + let coin_idx_sign = CoinIndexSignature { + h, + s: h * s_exponent, + }; + partial_coins_signatures.push(coin_idx_sign); + } partial_coins_signatures + // // Initialize a vector to collect the (partial) coin signatures + // let mut partial_coins_signatures = Vec::with_capacity(params.L() as usize); + // + // partial_coins_signatures.par_iter_mut() + // .enumerate() + // .for_each(|(l, coin_idx_sign)| { + // let m0: Scalar = Scalar::from(l as u64); + // // Compute the hash h + // let mut concatenated_bytes = + // Vec::with_capacity(vk.to_bytes().len() + l.to_le_bytes().len()); + // concatenated_bytes.extend_from_slice(&vk.to_bytes()); + // concatenated_bytes.extend_from_slice(&l.to_le_bytes()); + // let h = hash_g1(concatenated_bytes); + // + // // Sign the attributes by performing scalar-point multiplications and accumulating the result + // let mut s_exponent = sk_auth.x; + // s_exponent += &sk_auth.ys[0] * m0; + // s_exponent += &sk_auth.ys[1] * m1; + // s_exponent += &sk_auth.ys[2] * m2; + // // Create the signature struct of on the coin index + // *coin_idx_sign = CoinIndexSignature { + // h, + // s: h * s_exponent, + // }; + // }); + // partial_coins_signatures } pub fn verify_coin_indices_signatures( @@ -190,38 +213,70 @@ pub fn verify_coin_indices_signatures( let m1: Scalar = Scalar::from_bytes(&constants::TYPE_IDX).unwrap(); let m2: Scalar = Scalar::from_bytes(&constants::TYPE_IDX).unwrap(); - signatures.par_iter().enumerate().try_for_each(|(l, sig)| { - let m0: Scalar = Scalar::from(l as u64); - // Compute the hash h - let mut concatenated_bytes = - Vec::with_capacity(vk.to_bytes().len() + l.to_le_bytes().len()); - concatenated_bytes.extend_from_slice(&vk.to_bytes()); - concatenated_bytes.extend_from_slice(&l.to_le_bytes()); - let h = hash_g1(concatenated_bytes); - // Check if the hash is matching - if sig.h != h { - return Err(CompactEcashError::CoinIndices( - "Failed to verify the commitment hash".to_string(), - )); - } - let partially_signed_attributes = [m0, m1, m2] - .iter() - .zip(vk_auth.beta_g2.iter()) - .map(|(m, beta_i)| beta_i * Scalar::from(*m)) - .sum::(); + for (l, sig) in signatures.iter().enumerate() { + let m0: Scalar = Scalar::from(l as u64); + // Compute the hash h + let mut concatenated_bytes = + Vec::with_capacity(vk.to_bytes().len() + l.to_le_bytes().len()); + concatenated_bytes.extend_from_slice(&vk.to_bytes()); + concatenated_bytes.extend_from_slice(&l.to_le_bytes()); + let h = hash_g1(concatenated_bytes); + // Check if the hash is matching + if sig.h != h { + return Err(CompactEcashError::CoinIndices( + "Failed to verify the commitment hash".to_string(), + )); + } + let partially_signed_attributes = [m0, m1, m2] + .iter() + .zip(vk_auth.beta_g2.iter()) + .map(|(m, beta_i)| beta_i * Scalar::from(*m)) + .sum::(); - if !check_bilinear_pairing( - &sig.h.to_affine(), - &G2Prepared::from((vk_auth.alpha + partially_signed_attributes).to_affine()), - &sig.s.to_affine(), - params.grp().prepared_miller_g2(), - ) { - return Err(CompactEcashError::CoinIndices( - "Verification of the coin signature failed".to_string(), - )); - } - Ok(()) - }) + if !check_bilinear_pairing( + &sig.h.to_affine(), + &G2Prepared::from((vk_auth.alpha + partially_signed_attributes).to_affine()), + &sig.s.to_affine(), + params.grp().prepared_miller_g2(), + ) { + return Err(CompactEcashError::CoinIndices( + "Verification of the coin signature failed".to_string(), + )); + } + } + Ok(()) + // signatures.par_iter().enumerate().try_for_each(|(l, sig)| { + // let m0: Scalar = Scalar::from(l as u64); + // // Compute the hash h + // let mut concatenated_bytes = + // Vec::with_capacity(vk.to_bytes().len() + l.to_le_bytes().len()); + // concatenated_bytes.extend_from_slice(&vk.to_bytes()); + // concatenated_bytes.extend_from_slice(&l.to_le_bytes()); + // let h = hash_g1(concatenated_bytes); + // // Check if the hash is matching + // if sig.h != h { + // return Err(CompactEcashError::CoinIndices( + // "Failed to verify the commitment hash".to_string(), + // )); + // } + // let partially_signed_attributes = [m0, m1, m2] + // .iter() + // .zip(vk_auth.beta_g2.iter()) + // .map(|(m, beta_i)| beta_i * Scalar::from(*m)) + // .sum::(); + // + // if !check_bilinear_pairing( + // &sig.h.to_affine(), + // &G2Prepared::from((vk_auth.alpha + partially_signed_attributes).to_affine()), + // &sig.s.to_affine(), + // params.grp().prepared_miller_g2(), + // ) { + // return Err(CompactEcashError::CoinIndices( + // "Verification of the coin signature failed".to_string(), + // )); + // } + // Ok(()) + // }) } pub fn aggregate_indices_signatures( @@ -389,7 +444,7 @@ mod tests { .map(|(i, (vk, sigs))| (i.clone(), vk.clone(), sigs.clone())) .collect(); - // assert!(aggregate_indices_signatures(¶ms, &verification_key, &combined_data).is_ok()); + assert!(aggregate_indices_signatures(¶ms, &verification_key, &combined_data).is_ok()); } }