From 9e834ebaefd9b43b7cbd48c40dc7423c610de2c0 Mon Sep 17 00:00:00 2001 From: aniampio Date: Tue, 19 Apr 2022 16:56:27 +0300 Subject: [PATCH] Run cargo fmt --- common/nym-compact-ecash/src/proofs/mod.rs | 14 +++--- .../src/proofs/proof_spend.rs | 45 +++++++++--------- .../src/proofs/proof_withdrawal.rs | 42 +++++++++-------- .../src/scheme/aggregation.rs | 14 +++--- .../nym-compact-ecash/src/scheme/identify.rs | 5 +- common/nym-compact-ecash/src/scheme/keygen.rs | 11 ++--- common/nym-compact-ecash/src/scheme/mod.rs | 12 ++--- common/nym-compact-ecash/src/scheme/setup.rs | 46 ++++++++++++------- .../src/scheme/withdrawal.rs | 12 ++--- common/nym-compact-ecash/src/tests/e2e.rs | 15 +++--- common/nym-compact-ecash/src/utils.rs | 12 ++--- common/nym-divisible-ecash/src/lib.rs | 6 +-- common/nym-divisible-ecash/src/proofs/mod.rs | 1 + .../src/scheme/aggregation.rs | 1 + .../src/scheme/identify.rs | 1 + .../nym-divisible-ecash/src/scheme/keygen.rs | 1 + common/nym-divisible-ecash/src/scheme/mod.rs | 2 +- .../nym-divisible-ecash/src/scheme/setup.rs | 19 +++++--- .../scheme/structure_preserving_signature.rs | 20 +++++--- .../src/scheme/withdrawal.rs | 1 + common/nym-divisible-ecash/src/tests/e2e.rs | 1 + common/nym-divisible-ecash/src/traits.rs | 8 ++-- common/nym-divisible-ecash/src/utils.rs | 20 ++++---- common/nymcoconut/src/lib.rs | 14 +++--- common/nymcoconut/src/scheme/verification.rs | 24 +++++----- 25 files changed, 192 insertions(+), 155 deletions(-) diff --git a/common/nym-compact-ecash/src/proofs/mod.rs b/common/nym-compact-ecash/src/proofs/mod.rs index 00cdf32437..7208560002 100644 --- a/common/nym-compact-ecash/src/proofs/mod.rs +++ b/common/nym-compact-ecash/src/proofs/mod.rs @@ -3,8 +3,8 @@ use std::convert::TryFrom; use std::convert::TryInto; use bls12_381::{G1Affine, G1Projective, Scalar}; -use digest::Digest; use digest::generic_array::typenum::Unsigned; +use digest::Digest; use group::GroupEncoding; use sha2::Sha256; @@ -20,10 +20,10 @@ type ChallengeDigest = Sha256; /// Generates a Scalar [or Fp] challenge by hashing a number of elliptic curve points. fn compute_challenge(iter: I) -> Scalar - where - D: Digest, - I: Iterator, - B: AsRef<[u8]>, +where + D: Digest, + I: Iterator, + B: AsRef<[u8]>, { let mut h = D::new(); for point_representation in iter { @@ -51,8 +51,8 @@ fn produce_response(witness_replacement: &Scalar, challenge: &Scalar, secret: &S // note: it's caller's responsibility to ensure witnesses.len() = secrets.len() fn produce_responses(witnesses: &[Scalar], challenge: &Scalar, secrets: &[S]) -> Vec - where - S: Borrow, +where + S: Borrow, { debug_assert_eq!(witnesses.len(), secrets.len()); diff --git a/common/nym-compact-ecash/src/proofs/proof_spend.rs b/common/nym-compact-ecash/src/proofs/proof_spend.rs index 18801e884f..a6201d32ab 100644 --- a/common/nym-compact-ecash/src/proofs/proof_spend.rs +++ b/common/nym-compact-ecash/src/proofs/proof_spend.rs @@ -5,7 +5,7 @@ use bls12_381::{G1Projective, G2Projective, Scalar}; use group::{Curve, Group, GroupEncoding}; use crate::error::{CompactEcashError, Result}; -use crate::proofs::{ChallengeDigest, compute_challenge, produce_response, produce_responses}; +use crate::proofs::{compute_challenge, produce_response, produce_responses, ChallengeDigest}; use crate::scheme::keygen::{SecretKeyUser, VerificationKeyAuth}; use crate::scheme::setup::{GroupParameters, Parameters}; use crate::utils::{try_deserialize_g1_projective, try_deserialize_g2_projective}; @@ -167,10 +167,10 @@ impl SpendProof { let zkcm_kappa = grparams.gen2() * r_r + verification_key.alpha + r_attributes - .iter() - .zip(verification_key.beta_g2.iter()) - .map(|(attr, beta_i)| beta_i * attr) - .sum::(); + .iter() + .zip(verification_key.beta_g2.iter()) + .map(|(attr, beta_i)| beta_i * attr) + .sum::(); let zkcm_A = g1 * r_o_a + gamma1 * r_l; let zkcm_C = g1 * r_o_c + gamma1 * r_v; @@ -179,9 +179,7 @@ impl SpendProof { let zkcm_gamma11 = (instance.A + instance.C + gamma1) * r_mu + g1 * r_o_mu; let zkcm_T = g1 * r_sk + (g1 * R) * r_lambda; let zkcm_gamma12 = (instance.A + instance.D + gamma1) * r_lambda + g1 * r_o_lambda; - let zkcm_kappa_l = grparams.gen2() * r_r_l - + params.pkRP().alpha - + params.pkRP().beta * r_l; + let zkcm_kappa_l = grparams.gen2() * r_r_l + params.pkRP().alpha + params.pkRP().beta * r_l; // compute the challenge let challenge = compute_challenge::( @@ -259,11 +257,11 @@ impl SpendProof { + grparams.gen2() * self.response_r + verification_key.alpha * (Scalar::one() - self.challenge) + self - .response_attributes - .iter() - .zip(verification_key.beta_g2.iter()) - .map(|(attr, beta_i)| beta_i * attr) - .sum::(); + .response_attributes + .iter() + .zip(verification_key.beta_g2.iter()) + .map(|(attr, beta_i)| beta_i * attr) + .sum::(); let zkcm_A = g1 * self.response_o_a + gamma1 * self.response_l + instance.A * self.challenge; @@ -319,14 +317,14 @@ impl SpendProof { mod tests { use bls12_381::{G1Projective, G2Projective, Scalar}; use group::Curve; - use rand::{Rng, thread_rng}; + use rand::{thread_rng, Rng}; use crate::proofs::proof_spend::{SpendInstance, SpendProof, SpendWitness}; - use crate::scheme::{pseudorandom_fgt, pseudorandom_fgv}; use crate::scheme::aggregation::aggregate_verification_keys; - use crate::scheme::keygen::{PublicKeyUser, ttp_keygen, VerificationKeyAuth}; + use crate::scheme::keygen::{ttp_keygen, PublicKeyUser, VerificationKeyAuth}; + use crate::scheme::setup::{setup, GroupParameters}; use crate::scheme::PayInfo; - use crate::scheme::setup::{GroupParameters, setup}; + use crate::scheme::{pseudorandom_fgt, pseudorandom_fgv}; use crate::utils::hash_to_scalar; #[test] @@ -359,10 +357,10 @@ mod tests { let kappa = grparams.gen2() * r + verification_key.alpha + attributes - .iter() - .zip(verification_key.beta_g2.iter()) - .map(|(priv_attr, beta_i)| beta_i * priv_attr) - .sum::(); + .iter() + .zip(verification_key.beta_g2.iter()) + .map(|(priv_attr, beta_i)| beta_i * priv_attr) + .sum::(); let o_a = grparams.random_scalar(); let o_c = grparams.random_scalar(); @@ -392,9 +390,8 @@ mod tests { // randomise the signature associated with value l let (sign_l_prime, r_l) = sign_l.randomise(grparams); // compute kappa_l - let kappa_l = grparams.gen2() * r_l - + params.pkRP().alpha - + params.pkRP().beta * Scalar::from(l); + let kappa_l = + grparams.gen2() * r_l + params.pkRP().alpha + params.pkRP().beta * Scalar::from(l); let instance = SpendInstance { kappa, diff --git a/common/nym-compact-ecash/src/proofs/proof_withdrawal.rs b/common/nym-compact-ecash/src/proofs/proof_withdrawal.rs index c5b5c3ea34..d44db84c42 100644 --- a/common/nym-compact-ecash/src/proofs/proof_withdrawal.rs +++ b/common/nym-compact-ecash/src/proofs/proof_withdrawal.rs @@ -5,7 +5,7 @@ use group::GroupEncoding; use itertools::izip; use crate::error::{CompactEcashError, Result}; -use crate::proofs::{ChallengeDigest, compute_challenge, produce_response, produce_responses}; +use crate::proofs::{compute_challenge, produce_response, produce_responses, ChallengeDigest}; use crate::scheme::keygen::PublicKeyUser; use crate::scheme::setup::GroupParameters; use crate::utils::try_deserialize_g1_projective; @@ -136,10 +136,10 @@ impl WithdrawalReqProof { // compute zkp commitments for each instance let zkcm_com = params.gen1() * r_com_opening + r_attributes - .iter() - .zip(params.gammas().iter()) - .map(|(rm_i, gamma_i)| gamma_i * rm_i) - .sum::(); + .iter() + .zip(params.gammas().iter()) + .map(|(rm_i, gamma_i)| gamma_i * rm_i) + .sum::(); let zkcm_pedcom = r_pedcom_openings .iter() @@ -192,26 +192,30 @@ impl WithdrawalReqProof { } } - pub(crate) fn verify(&self, params: &GroupParameters, instance: &WithdrawalReqInstance) -> bool { + pub(crate) fn verify( + &self, + params: &GroupParameters, + instance: &WithdrawalReqInstance, + ) -> bool { // recompute zk commitments for each instance let zkcm_com = instance.com * self.challenge + params.gen1() * self.response_opening + self - .response_attributes - .iter() - .zip(params.gammas().iter()) - .map(|(m_i, gamma_i)| gamma_i * m_i) - .sum::(); + .response_attributes + .iter() + .zip(params.gammas().iter()) + .map(|(m_i, gamma_i)| gamma_i * m_i) + .sum::(); let zkcm_pedcom = izip!( instance.pc_coms.iter(), self.response_openings.iter(), self.response_attributes.iter() ) - .map(|(cm_j, resp_o_j, resp_m_j)| { - cm_j * self.challenge + params.gen1() * resp_o_j + instance.h * resp_m_j - }) - .collect::>(); + .map(|(cm_j, resp_o_j, resp_m_j)| { + cm_j * self.challenge + params.gen1() * resp_o_j + instance.h * resp_m_j + }) + .collect::>(); let zk_commitment_user_sk = instance.pk_user.pk * self.challenge + params.gen1() * self.response_attributes[0]; @@ -288,10 +292,10 @@ mod tests { let com_opening = params.random_scalar(); let com = params.gen1() * com_opening + attr - .iter() - .zip(params.gammas()) - .map(|(&m, gamma)| gamma * m) - .sum::(); + .iter() + .zip(params.gammas()) + .map(|(&m, gamma)| gamma * m) + .sum::(); let h = hash_g1(com.to_bytes()); let pc_openings = params.n_random_scalars(attr.len()); diff --git a/common/nym-compact-ecash/src/scheme/aggregation.rs b/common/nym-compact-ecash/src/scheme/aggregation.rs index 1a0660f184..11d2a8d1ca 100644 --- a/common/nym-compact-ecash/src/scheme/aggregation.rs +++ b/common/nym-compact-ecash/src/scheme/aggregation.rs @@ -6,16 +6,16 @@ use bls12_381::{G2Prepared, G2Projective, Scalar}; use group::Curve; use itertools::Itertools; -use crate::Attribute; use crate::error::{CompactEcashError, Result}; -use crate::scheme::{PartialWallet, Wallet}; use crate::scheme::keygen::{SecretKeyUser, VerificationKeyAuth}; use crate::scheme::setup::GroupParameters; use crate::scheme::withdrawal::RequestInfo; +use crate::scheme::{PartialWallet, Wallet}; use crate::utils::{ - check_bilinear_pairing, PartialSignature, perform_lagrangian_interpolation_at_origin, + check_bilinear_pairing, perform_lagrangian_interpolation_at_origin, PartialSignature, Signature, SignatureShare, SignerIndex, }; +use crate::Attribute; pub(crate) trait Aggregatable: Sized { fn aggregate(aggregatable: &[Self], indices: Option<&[SignerIndex]>) -> Result; @@ -27,10 +27,10 @@ pub(crate) trait Aggregatable: Sized { } impl Aggregatable for T - where - T: Sum, - for<'a> T: Sum<&'a T>, - for<'a> &'a T: Mul, +where + T: Sum, + for<'a> T: Sum<&'a T>, + for<'a> &'a T: Mul, { fn aggregate(aggregatable: &[T], indices: Option<&[u64]>) -> Result { if aggregatable.is_empty() { diff --git a/common/nym-compact-ecash/src/scheme/identify.rs b/common/nym-compact-ecash/src/scheme/identify.rs index 79257573e0..800188dc7d 100644 --- a/common/nym-compact-ecash/src/scheme/identify.rs +++ b/common/nym-compact-ecash/src/scheme/identify.rs @@ -2,10 +2,7 @@ use crate::error::Result; use crate::scheme::keygen::PublicKeyUser; use crate::scheme::Payment; -pub fn identify( - pay1: Payment, - pay2: Payment, -) -> Result { +pub fn identify(pay1: Payment, pay2: Payment) -> Result { // TODO: We should include here the check for S and payInfo let pkUser = (pay2.T * pay1.R - pay1.T * pay2.R) * ((pay1.R - pay2.R).invert().unwrap()); Ok(PublicKeyUser { pk: pkUser }) diff --git a/common/nym-compact-ecash/src/scheme/keygen.rs b/common/nym-compact-ecash/src/scheme/keygen.rs index 3d84ddc69f..d0c62db429 100644 --- a/common/nym-compact-ecash/src/scheme/keygen.rs +++ b/common/nym-compact-ecash/src/scheme/keygen.rs @@ -11,11 +11,11 @@ use crate::error::{CompactEcashError, Result}; use crate::scheme::aggregation::aggregate_verification_keys; use crate::scheme::setup::GroupParameters; use crate::scheme::SignerIndex; +use crate::utils::Polynomial; use crate::utils::{ try_deserialize_g1_projective, try_deserialize_g2_projective, try_deserialize_scalar, try_deserialize_scalar_vec, }; -use crate::utils::Polynomial; #[derive(Debug, PartialEq, Clone)] pub struct SecretKeyAuth { @@ -242,13 +242,13 @@ impl<'a> Mul for &'a VerificationKeyAuth { } impl Sum for VerificationKeyAuth - where - T: Borrow, +where + T: Borrow, { #[inline] fn sum(iter: I) -> Self - where - I: Iterator, + where + I: Iterator, { let mut peekable = iter.peekable(); let head_attributes = match peekable.peek() { @@ -440,4 +440,3 @@ pub fn ttp_keygen( Ok(keypairs) } - diff --git a/common/nym-compact-ecash/src/scheme/mod.rs b/common/nym-compact-ecash/src/scheme/mod.rs index 55838744f3..e5bd15f232 100644 --- a/common/nym-compact-ecash/src/scheme/mod.rs +++ b/common/nym-compact-ecash/src/scheme/mod.rs @@ -5,14 +5,14 @@ use std::convert::TryInto; use bls12_381::{G1Projective, G2Prepared, G2Projective, Scalar}; use group::{Curve, Group}; -use crate::Attribute; use crate::error::{CompactEcashError, Result}; use crate::proofs::proof_spend::{SpendInstance, SpendProof, SpendWitness}; use crate::scheme::keygen::{SecretKeyUser, VerificationKeyAuth}; use crate::scheme::setup::{GroupParameters, Parameters}; use crate::utils::{ - check_bilinear_pairing, hash_to_scalar, Signature, SignerIndex, try_deserialize_g1_projective, + check_bilinear_pairing, hash_to_scalar, try_deserialize_g1_projective, Signature, SignerIndex, }; +use crate::Attribute; pub mod aggregation; pub mod identify; @@ -195,10 +195,10 @@ pub fn compute_kappa( params.gen2() * blinding_factor + verification_key.alpha + attributes - .iter() - .zip(verification_key.beta_g2.iter()) - .map(|(priv_attr, beta_i)| beta_i * priv_attr) - .sum::() + .iter() + .zip(verification_key.beta_g2.iter()) + .map(|(priv_attr, beta_i)| beta_i * priv_attr) + .sum::() } pub struct PayInfo { diff --git a/common/nym-compact-ecash/src/scheme/setup.rs b/common/nym-compact-ecash/src/scheme/setup.rs index a5b78c21f3..6914136e3b 100644 --- a/common/nym-compact-ecash/src/scheme/setup.rs +++ b/common/nym-compact-ecash/src/scheme/setup.rs @@ -28,13 +28,11 @@ impl GroupParameters { .map(|i| hash_g1(format!("gamma{}", i))) .collect(); - Ok(GroupParameters { g1: G1Affine::generator(), g2: G2Affine::generator(), gammas, _g2_prepared_miller: G2Prepared::from(G2Affine::generator()), - }) } @@ -77,7 +75,6 @@ impl GroupParameters { } } - #[derive(Debug, PartialEq, Clone)] pub struct SecretKeyRP { pub(crate) x: Scalar, @@ -86,7 +83,10 @@ pub struct SecretKeyRP { impl SecretKeyRP { pub fn public_key(&self, params: &GroupParameters) -> PublicKeyRP { - PublicKeyRP { alpha: params.gen2() * self.x, beta: params.gen2() * self.y } + PublicKeyRP { + alpha: params.gen2() * self.x, + beta: params.gen2() * self.y, + } } } @@ -108,18 +108,28 @@ pub struct Parameters { } impl Parameters { - pub fn grp(&self) -> &GroupParameters { &self.grp } - pub fn pkRP(&self) -> &PublicKeyRP { &self.pkRP } - pub fn L(&self) -> u64 { self.L } - pub fn signs(&self) -> &HashMap { &self.signs } + pub fn grp(&self) -> &GroupParameters { + &self.grp + } + pub fn pkRP(&self) -> &PublicKeyRP { + &self.pkRP + } + pub fn L(&self) -> u64 { + self.L + } + pub fn signs(&self) -> &HashMap { + &self.signs + } pub fn get_sign_by_idx(&self, idx: u64) -> Result<&Signature> { match self.signs.get(&idx) { Some(val) => return Ok(val), - None => return - Err(CompactEcashError::RangeProofOutOfBound - ("Cannot find the range proof signature for the given value. \ - Check if the requested value is within the bound 0..L".to_string() - )) + None => { + return Err(CompactEcashError::RangeProofOutOfBound( + "Cannot find the range proof signature for the given value. \ + Check if the requested value is within the bound 0..L" + .to_string(), + )) + } } } } @@ -134,7 +144,13 @@ pub fn setup() -> Parameters { for l in 0..MAX_COIN_VALUE { let r = grp.random_scalar(); let h = grp.gen1() * r; - signs.insert(l, Signature { 0: h, 1: h * (x + y * Scalar::from(l)) }); + signs.insert( + l, + Signature { + 0: h, + 1: h * (x + y * Scalar::from(l)), + }, + ); } Parameters { grp, @@ -143,5 +159,3 @@ pub fn setup() -> Parameters { signs, } } - - diff --git a/common/nym-compact-ecash/src/scheme/withdrawal.rs b/common/nym-compact-ecash/src/scheme/withdrawal.rs index 047a1c46d2..90b9d5d39d 100644 --- a/common/nym-compact-ecash/src/scheme/withdrawal.rs +++ b/common/nym-compact-ecash/src/scheme/withdrawal.rs @@ -5,10 +5,10 @@ use crate::error::{CompactEcashError, Result}; use crate::proofs::proof_withdrawal::{ WithdrawalReqInstance, WithdrawalReqProof, WithdrawalReqWitness, }; -use crate::scheme::keygen::{PublicKeyUser, SecretKeyAuth, SecretKeyUser, VerificationKeyAuth}; use crate::scheme::keygen::ttp_keygen; -use crate::scheme::PartialWallet; +use crate::scheme::keygen::{PublicKeyUser, SecretKeyAuth, SecretKeyUser, VerificationKeyAuth}; use crate::scheme::setup::{GroupParameters, Parameters}; +use crate::scheme::PartialWallet; use crate::utils::{check_bilinear_pairing, hash_g1}; use crate::utils::{BlindedSignature, Signature}; @@ -57,10 +57,10 @@ pub fn withdrawal_request( let com_opening = params.random_scalar(); let com = params.gen1() * com_opening + attributes - .iter() - .zip(gammas) - .map(|(&m, gamma)| gamma * m) - .sum::(); + .iter() + .zip(gammas) + .map(|(&m, gamma)| gamma * m) + .sum::(); // Value h in the paper let com_hash = hash_g1(com.to_bytes()); diff --git a/common/nym-compact-ecash/src/tests/e2e.rs b/common/nym-compact-ecash/src/tests/e2e.rs index 8c135269bc..520b404372 100644 --- a/common/nym-compact-ecash/src/tests/e2e.rs +++ b/common/nym-compact-ecash/src/tests/e2e.rs @@ -1,17 +1,17 @@ use itertools::izip; use crate::error::CompactEcashError; -use crate::scheme::{PartialWallet, Payment, pseudorandom_fgt}; use crate::scheme::aggregation::{ aggregate_signature_shares, aggregate_verification_keys, aggregate_wallets, }; use crate::scheme::identify::identify; use crate::scheme::keygen::{ - generate_keypair_user, PublicKeyUser, SecretKeyUser, ttp_keygen, VerificationKeyAuth, + generate_keypair_user, ttp_keygen, PublicKeyUser, SecretKeyUser, VerificationKeyAuth, }; -use crate::scheme::PayInfo; -use crate::scheme::setup::{GroupParameters, Parameters, setup}; +use crate::scheme::setup::{setup, GroupParameters, Parameters}; use crate::scheme::withdrawal::{issue_verify, issue_wallet, withdrawal_request}; +use crate::scheme::PayInfo; +use crate::scheme::{pseudorandom_fgt, PartialWallet, Payment}; use crate::utils::{hash_to_scalar, SignatureShare}; #[test] @@ -45,8 +45,8 @@ fn main() -> Result<(), CompactEcashError> { wallet_blinded_signatures.iter(), verification_keys_auth.iter() ) - .map(|(w, vk)| issue_verify(&grparams, vk, &user_keypair.secret_key(), w, &req_info).unwrap()) - .collect(); + .map(|(w, vk)| issue_verify(&grparams, vk, &user_keypair.secret_key(), w, &req_info).unwrap()) + .collect(); // Aggregate partial wallets let aggr_wallet = aggregate_wallets( @@ -80,7 +80,8 @@ fn main() -> Result<(), CompactEcashError> { kappa: payment1.kappa.clone(), sig: payment1.sig.clone(), S: payment1.S.clone(), - T: grparams.gen1() * user_keypair.secret_key().sk + pseudorandom_fgt(&grparams, aggr_wallet.t(), l2) * R2, + T: grparams.gen1() * user_keypair.secret_key().sk + + pseudorandom_fgt(&grparams, aggr_wallet.t(), l2) * R2, A: payment1.A.clone(), C: payment1.C.clone(), D: payment1.D.clone(), diff --git a/common/nym-compact-ecash/src/utils.rs b/common/nym-compact-ecash/src/utils.rs index 293edce3f8..42c2303aa7 100644 --- a/common/nym-compact-ecash/src/utils.rs +++ b/common/nym-compact-ecash/src/utils.rs @@ -6,10 +6,10 @@ use core::ops::Mul; use std::convert::{TryFrom, TryInto}; use std::ops::Neg; -use bls12_381::{ - G1Affine, G1Projective, G2Affine, G2Prepared, G2Projective, multi_miller_loop, Scalar, -}; use bls12_381::hash_to_curve::{ExpandMsgXmd, HashToCurve, HashToField}; +use bls12_381::{ + multi_miller_loop, G1Affine, G1Projective, G2Affine, G2Prepared, G2Projective, Scalar, +}; use ff::Field; use group::{Curve, Group}; @@ -85,9 +85,9 @@ pub(crate) fn perform_lagrangian_interpolation_at_origin( points: &[SignerIndex], values: &[T], ) -> Result - where - T: Sum, - for<'a> &'a T: Mul, +where + T: Sum, + for<'a> &'a T: Mul, { if points.is_empty() || values.is_empty() { return Err(CompactEcashError::Interpolation( diff --git a/common/nym-divisible-ecash/src/lib.rs b/common/nym-divisible-ecash/src/lib.rs index e024c444b8..f49b38f2f7 100644 --- a/common/nym-divisible-ecash/src/lib.rs +++ b/common/nym-divisible-ecash/src/lib.rs @@ -1,11 +1,11 @@ use bls12_381::Scalar; +mod error; mod proofs; mod scheme; #[cfg(test)] mod tests; -mod error; -mod utils; mod traits; +mod utils; -pub type Attribute = Scalar; \ No newline at end of file +pub type Attribute = Scalar; diff --git a/common/nym-divisible-ecash/src/proofs/mod.rs b/common/nym-divisible-ecash/src/proofs/mod.rs index e69de29bb2..8b13789179 100644 --- a/common/nym-divisible-ecash/src/proofs/mod.rs +++ b/common/nym-divisible-ecash/src/proofs/mod.rs @@ -0,0 +1 @@ + diff --git a/common/nym-divisible-ecash/src/scheme/aggregation.rs b/common/nym-divisible-ecash/src/scheme/aggregation.rs index e69de29bb2..8b13789179 100644 --- a/common/nym-divisible-ecash/src/scheme/aggregation.rs +++ b/common/nym-divisible-ecash/src/scheme/aggregation.rs @@ -0,0 +1 @@ + diff --git a/common/nym-divisible-ecash/src/scheme/identify.rs b/common/nym-divisible-ecash/src/scheme/identify.rs index e69de29bb2..8b13789179 100644 --- a/common/nym-divisible-ecash/src/scheme/identify.rs +++ b/common/nym-divisible-ecash/src/scheme/identify.rs @@ -0,0 +1 @@ + diff --git a/common/nym-divisible-ecash/src/scheme/keygen.rs b/common/nym-divisible-ecash/src/scheme/keygen.rs index e69de29bb2..8b13789179 100644 --- a/common/nym-divisible-ecash/src/scheme/keygen.rs +++ b/common/nym-divisible-ecash/src/scheme/keygen.rs @@ -0,0 +1 @@ + diff --git a/common/nym-divisible-ecash/src/scheme/mod.rs b/common/nym-divisible-ecash/src/scheme/mod.rs index 3eb6c0d793..8ad1c16839 100644 --- a/common/nym-divisible-ecash/src/scheme/mod.rs +++ b/common/nym-divisible-ecash/src/scheme/mod.rs @@ -2,5 +2,5 @@ pub mod aggregation; pub mod identify; pub mod keygen; pub mod setup; +pub mod structure_preserving_signature; pub mod withdrawal; -pub mod structure_preserving_signature; \ No newline at end of file diff --git a/common/nym-divisible-ecash/src/scheme/setup.rs b/common/nym-divisible-ecash/src/scheme/setup.rs index 0a85070ca4..31d5a2b92b 100644 --- a/common/nym-divisible-ecash/src/scheme/setup.rs +++ b/common/nym-divisible-ecash/src/scheme/setup.rs @@ -41,7 +41,9 @@ impl GroupParameters { &self._g2_prepared_miller } - pub(crate) fn getL(&self) -> u64 { self.L } + pub(crate) fn getL(&self) -> u64 { + self.L + } pub(crate) fn random_scalar(&self) -> Scalar { // lazily-initialized thread-local random number generator, seeded by the system @@ -60,7 +62,6 @@ pub struct Parameters { paramsAuth: ParametersAuthority, } - impl Parameters { pub fn new(grp: GroupParameters) -> Parameters { let g1 = grp.gen1(); @@ -78,10 +79,16 @@ impl Parameters { let sigma = g1 * z; let theta = eta * z; - let sigmasUser: Vec = (1..=grp.getL()).map(|i| sigma * (y * Scalar::from(i))).collect(); - let thetasUser: Vec = (1..=grp.getL()).map(|i| theta * (y * Scalar::from(i))).collect(); + let sigmasUser: Vec = (1..=grp.getL()) + .map(|i| sigma * (y * Scalar::from(i))) + .collect(); + let thetasUser: Vec = (1..=grp.getL()) + .map(|i| theta * (y * Scalar::from(i))) + .collect(); - let deltasAuth: Vec = (0..=grp.getL() - 1).map(|i| g2 * (y * Scalar::from(i))).collect(); + let deltasAuth: Vec = (0..=grp.getL() - 1) + .map(|i| g2 * (y * Scalar::from(i))) + .collect(); let etasUser: Vec = vec_a.iter().map(|x| g1 * x).collect(); let mut etasAuth: Vec = Default::default(); @@ -137,4 +144,4 @@ pub struct ParametersUser { pub struct ParametersAuthority { deltas: Vec, etas: Vec, -} \ No newline at end of file +} diff --git a/common/nym-divisible-ecash/src/scheme/structure_preserving_signature.rs b/common/nym-divisible-ecash/src/scheme/structure_preserving_signature.rs index 4207aa4c3b..5d4b5f0e70 100644 --- a/common/nym-divisible-ecash/src/scheme/structure_preserving_signature.rs +++ b/common/nym-divisible-ecash/src/scheme/structure_preserving_signature.rs @@ -3,8 +3,8 @@ use std::convert::TryFrom; use bls12_381::{G1Projective, G2Projective, Scalar}; use group::Curve; -use crate::Attribute; use crate::scheme::setup::GroupParameters; +use crate::Attribute; #[derive(Debug, Clone)] pub(crate) struct SPSVerificationKey { @@ -24,13 +24,22 @@ pub(crate) struct SPSSecretKey { } impl SPSSecretKey { - pub fn z(&self) -> Scalar { self.z } - pub fn y(&self) -> Scalar { self.y } + pub fn z(&self) -> Scalar { + self.z + } + pub fn y(&self) -> Scalar { + self.y + } pub fn sign(&self, grparams: GroupParameters, attributes: Vec) -> SPSSignature { let r = grparams.random_scalar(); let R = grparams.gen1() * r; - let prod: Vec = attributes.iter().zip(self.ws.iter()).map(|(w_i, m_i)| m_i * w_i.neg()).collect(); - let Z = grparams.gen1() * (self.z() - r * self.y()) + prod.iter().fold(1 | acc, x | acc * x); + let prod: Vec = attributes + .iter() + .zip(self.ws.iter()) + .map(|(w_i, m_i)| m_i * w_i.neg()) + .collect(); + let Z = + grparams.gen1() * (self.z() - r * self.y()) + prod.iter().fold(1 | acc, x | acc * x); // let sum = a.iter().fold(0, |acc, x| acc + x); // let Z: G1Projective = grparams.gen1() * (self.z() - r * self.y()) // + attributes @@ -76,4 +85,3 @@ impl SPSKeyPair { } pub struct SPSSignature {} - diff --git a/common/nym-divisible-ecash/src/scheme/withdrawal.rs b/common/nym-divisible-ecash/src/scheme/withdrawal.rs index e69de29bb2..8b13789179 100644 --- a/common/nym-divisible-ecash/src/scheme/withdrawal.rs +++ b/common/nym-divisible-ecash/src/scheme/withdrawal.rs @@ -0,0 +1 @@ + diff --git a/common/nym-divisible-ecash/src/tests/e2e.rs b/common/nym-divisible-ecash/src/tests/e2e.rs index e69de29bb2..8b13789179 100644 --- a/common/nym-divisible-ecash/src/tests/e2e.rs +++ b/common/nym-divisible-ecash/src/tests/e2e.rs @@ -0,0 +1 @@ + diff --git a/common/nym-divisible-ecash/src/traits.rs b/common/nym-divisible-ecash/src/traits.rs index 425c9dd366..bc0ebb2e8f 100644 --- a/common/nym-divisible-ecash/src/traits.rs +++ b/common/nym-divisible-ecash/src/traits.rs @@ -1,8 +1,8 @@ use crate::error::DivisibleEcashError; pub trait Bytable - where - Self: Sized, +where + Self: Sized, { fn to_byte_vec(&self) -> Vec; @@ -10,8 +10,8 @@ pub trait Bytable } pub trait Base58 - where - Self: Bytable, +where + Self: Bytable, { fn try_from_bs58>(x: S) -> Result { Self::try_from_byte_slice(&bs58::decode(x.as_ref()).into_vec().unwrap()) diff --git a/common/nym-divisible-ecash/src/utils.rs b/common/nym-divisible-ecash/src/utils.rs index 3eeee13db2..95da93d921 100644 --- a/common/nym-divisible-ecash/src/utils.rs +++ b/common/nym-divisible-ecash/src/utils.rs @@ -6,10 +6,10 @@ use core::ops::Mul; use std::convert::{TryFrom, TryInto}; use std::ops::Neg; -use bls12_381::{ - G1Affine, G1Projective, G2Affine, G2Prepared, G2Projective, multi_miller_loop, Scalar, -}; use bls12_381::hash_to_curve::{ExpandMsgXmd, HashToCurve, HashToField}; +use bls12_381::{ + multi_miller_loop, G1Affine, G1Projective, G2Affine, G2Prepared, G2Projective, Scalar, +}; use ff::Field; use group::{Curve, Group}; @@ -85,9 +85,9 @@ pub(crate) fn perform_lagrangian_interpolation_at_origin( points: &[SignerIndex], values: &[T], ) -> Result - where - T: Sum, - for<'a> &'a T: Mul, +where + T: Sum, + for<'a> &'a T: Mul, { if points.is_empty() || values.is_empty() { return Err(DivisibleEcashError::Interpolation( @@ -221,12 +221,16 @@ impl TryFrom<&[u8]> for Signature { let sig1 = try_deserialize_g1_projective( sig1_bytes, - DivisibleEcashError::Deserialization("Failed to deserialize compressed sig1".to_string()), + DivisibleEcashError::Deserialization( + "Failed to deserialize compressed sig1".to_string(), + ), )?; let sig2 = try_deserialize_g1_projective( sig2_bytes, - DivisibleEcashError::Deserialization("Failed to deserialize compressed sig2".to_string()), + DivisibleEcashError::Deserialization( + "Failed to deserialize compressed sig2".to_string(), + ), )?; Ok(Signature(sig1, sig2)) diff --git a/common/nymcoconut/src/lib.rs b/common/nymcoconut/src/lib.rs index c73e8db147..55f5eab534 100644 --- a/common/nymcoconut/src/lib.rs +++ b/common/nymcoconut/src/lib.rs @@ -11,20 +11,20 @@ pub use elgamal::PublicKey; pub use error::CoconutError; pub use scheme::aggregation::aggregate_signature_shares; pub use scheme::aggregation::aggregate_verification_keys; -pub use scheme::BlindedSignature; pub use scheme::issuance::blind_sign; -pub use scheme::issuance::BlindSignRequest; pub use scheme::issuance::prepare_blind_sign; -pub use scheme::keygen::KeyPair; +pub use scheme::issuance::BlindSignRequest; pub use scheme::keygen::ttp_keygen; +pub use scheme::keygen::KeyPair; pub use scheme::keygen::VerificationKey; -pub use scheme::setup::Parameters; pub use scheme::setup::setup; +pub use scheme::setup::Parameters; +pub use scheme::verification::prove_bandwidth_credential; +pub use scheme::verification::verify_credential; +pub use scheme::verification::Theta; +pub use scheme::BlindedSignature; pub use scheme::Signature; pub use scheme::SignatureShare; -pub use scheme::verification::prove_bandwidth_credential; -pub use scheme::verification::Theta; -pub use scheme::verification::verify_credential; pub use traits::Base58; pub use utils::hash_to_scalar; diff --git a/common/nymcoconut/src/scheme/verification.rs b/common/nymcoconut/src/scheme/verification.rs index e312864edb..c2ccd2da12 100644 --- a/common/nymcoconut/src/scheme/verification.rs +++ b/common/nymcoconut/src/scheme/verification.rs @@ -5,10 +5,9 @@ use core::ops::Neg; use std::convert::TryFrom; use std::convert::TryInto; -use bls12_381::{G1Affine, G2Prepared, G2Projective, multi_miller_loop, Scalar}; +use bls12_381::{multi_miller_loop, G1Affine, G2Prepared, G2Projective, Scalar}; use group::{Curve, Group}; -use crate::Attribute; use crate::error::{CoconutError, Result}; use crate::proofs::ProofKappaZeta; use crate::scheme::setup::Parameters; @@ -16,6 +15,7 @@ use crate::scheme::Signature; use crate::scheme::VerificationKey; use crate::traits::{Base58, Bytable}; use crate::utils::try_deserialize_g2_projective; +use crate::Attribute; // TODO NAMING: this whole thing // Theta @@ -123,10 +123,10 @@ pub fn compute_kappa( params.gen2() * blinding_factor + verification_key.alpha + private_attributes - .iter() - .zip(verification_key.beta_g2.iter()) - .map(|(priv_attr, beta_i)| beta_i * priv_attr) - .sum::() + .iter() + .zip(verification_key.beta_g2.iter()) + .map(|(priv_attr, beta_i)| beta_i * priv_attr) + .sum::() } pub fn compute_zeta(params: &Parameters, serial_number: Attribute) -> G2Projective { @@ -250,11 +250,11 @@ pub fn verify( ) -> bool { let kappa = (verification_key.alpha + public_attributes - .iter() - .zip(verification_key.beta_g2.iter()) - .map(|(m_i, b_i)| b_i * m_i) - .sum::()) - .to_affine(); + .iter() + .zip(verification_key.beta_g2.iter()) + .map(|(m_i, b_i)| b_i * m_i) + .sum::()) + .to_affine(); check_bilinear_pairing( &sig.0.to_affine(), @@ -290,7 +290,7 @@ mod tests { serial_number, binding_number, ) - .unwrap(); + .unwrap(); let bytes = theta.to_bytes(); assert_eq!(Theta::try_from(bytes.as_slice()).unwrap(), theta);