From dabbe8ba7f3831b7eab2f1a9ea93bc8224ef1318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 17 Oct 2024 15:12:52 +0100 Subject: [PATCH] removed additional sources of copying secrets and introduced extra error variants --- common/nym_offline_compact_ecash/src/error.rs | 9 ++++--- .../src/proofs/proof_withdrawal.rs | 25 ++++++++----------- .../src/scheme/aggregation.rs | 7 +++--- .../src/scheme/withdrawal.rs | 22 ++++++++-------- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/common/nym_offline_compact_ecash/src/error.rs b/common/nym_offline_compact_ecash/src/error.rs index a1afef7b62..badccfc4de 100644 --- a/common/nym_offline_compact_ecash/src/error.rs +++ b/common/nym_offline_compact_ecash/src/error.rs @@ -22,9 +22,15 @@ pub enum CompactEcashError { #[error("aggregation verification error")] AggregationVerification, + #[error("the provided signature is at infinity")] + IdentitySignature, + #[error("different element size for aggregation")] AggregationSizeMismatch, + #[error("the provided commitment hash is at infinity")] + IdentityCommitmentHash, + #[error("withdrawal request failed to verify")] WithdrawalRequestVerification, @@ -40,9 +46,6 @@ pub enum CompactEcashError { #[error("issuance verification failed")] IssuanceVerification, - #[error("verification of partial blind signature failed")] - PartialBlindSignatureVerification, - #[error("trying to spend more than what's available. Spending : {spending}, available : {remaining}")] SpendExceedsAllowance { spending: u64, remaining: u64 }, diff --git a/common/nym_offline_compact_ecash/src/proofs/proof_withdrawal.rs b/common/nym_offline_compact_ecash/src/proofs/proof_withdrawal.rs index cba7161713..b16b66d7ff 100644 --- a/common/nym_offline_compact_ecash/src/proofs/proof_withdrawal.rs +++ b/common/nym_offline_compact_ecash/src/proofs/proof_withdrawal.rs @@ -24,12 +24,12 @@ pub struct WithdrawalReqInstance { } // witness: m1, m2, m3, o, o1, o2, o3, -pub struct WithdrawalReqWitness { - pub private_attributes: Vec, +pub struct WithdrawalReqWitness<'a> { + pub private_attributes: Vec<&'a Scalar>, // Opening for the joined commitment com - pub joined_commitment_opening: Scalar, + pub joined_commitment_opening: &'a Scalar, // Openings for the pedersen commitments of private attributes - pub private_attributes_openings: Vec, + pub private_attributes_openings: &'a Vec, } #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] @@ -93,7 +93,7 @@ impl WithdrawalReqProof { let response_opening = produce_response( &r_com_opening, &challenge, - &witness.joined_commitment_opening, + witness.joined_commitment_opening, ); let response_openings = produce_responses( &r_pedcom_openings, @@ -103,11 +103,8 @@ impl WithdrawalReqProof { .iter() .collect::>(), ); - let response_attributes = produce_responses( - &r_attributes, - &challenge, - &witness.private_attributes.iter().collect::>(), - ); + let response_attributes = + produce_responses(&r_attributes, &challenge, &witness.private_attributes); WithdrawalReqProof { challenge, @@ -212,7 +209,7 @@ mod tests { }; let v = params.random_scalar(); let t = params.random_scalar(); - let private_attributes = vec![sk, v, t]; + let private_attributes = vec![&sk, &v, &t]; let joined_commitment_opening = params.random_scalar(); let joined_commitment = params.gen1() * joined_commitment_opening @@ -227,7 +224,7 @@ mod tests { let private_attributes_commitments = private_attributes_openings .iter() .zip(private_attributes.iter()) - .map(|(o_j, m_j)| params.gen1() * o_j + joined_commitment_hash * m_j) + .map(|(o_j, &m_j)| params.gen1() * o_j + joined_commitment_hash * m_j) .collect::>(); let instance = WithdrawalReqInstance { @@ -239,8 +236,8 @@ mod tests { let witness = WithdrawalReqWitness { private_attributes, - joined_commitment_opening, - private_attributes_openings, + joined_commitment_opening: &joined_commitment_opening, + private_attributes_openings: &private_attributes_openings, }; let zk_proof = WithdrawalReqProof::construct(&instance, &witness); assert!(zk_proof.verify(&instance)) diff --git a/common/nym_offline_compact_ecash/src/scheme/aggregation.rs b/common/nym_offline_compact_ecash/src/scheme/aggregation.rs index 0a8388196d..424c73d5a3 100644 --- a/common/nym_offline_compact_ecash/src/scheme/aggregation.rs +++ b/common/nym_offline_compact_ecash/src/scheme/aggregation.rs @@ -108,6 +108,10 @@ pub fn aggregate_signatures( Err(err) => return Err(err), }; + if bool::from(signature.h.is_identity()) { + return Err(CompactEcashError::IdentitySignature); + } + // Verify the signature let tmp = attributes .iter() @@ -115,9 +119,6 @@ pub fn aggregate_signatures( .map(|(attr, beta_i)| beta_i * attr) .sum::(); - if bool::from(signature.h.is_identity()) { - return Err(CompactEcashError::AggregationVerification); - } if !check_bilinear_pairing( &signature.h.to_affine(), &G2Prepared::from((verification_key.alpha + tmp).to_affine()), diff --git a/common/nym_offline_compact_ecash/src/scheme/withdrawal.rs b/common/nym_offline_compact_ecash/src/scheme/withdrawal.rs index 2e9474566d..62f34f84b0 100644 --- a/common/nym_offline_compact_ecash/src/scheme/withdrawal.rs +++ b/common/nym_offline_compact_ecash/src/scheme/withdrawal.rs @@ -103,11 +103,11 @@ impl RequestInfo { fn compute_private_attribute_commitments( params: &GroupParameters, joined_commitment_hash: &G1Projective, - private_attributes: &[Scalar], + private_attributes: &[&Scalar], ) -> (Vec, Vec) { let (openings, commitments): (Vec, Vec) = private_attributes .iter() - .map(|m_j| { + .map(|&m_j| { let o_j = params.random_scalar(); (o_j, params.gen1() * o_j + joined_commitment_hash * m_j) }) @@ -125,7 +125,7 @@ fn compute_private_attribute_commitments( fn generate_non_identity_h( params: &GroupParameters, sk_user: &SecretKeyUser, - v: Scalar, + v: &Scalar, expiration_date: Scalar, t_type: Scalar, ) -> (G1Projective, G1Projective, Scalar) { @@ -190,10 +190,10 @@ pub fn withdrawal_request( // Generate a non-identity commitment hash let (joined_commitment, joined_commitment_hash, joined_commitment_opening) = - generate_non_identity_h(params, sk_user, v, expiration_date, t_type); + generate_non_identity_h(params, sk_user, &v, expiration_date, t_type); // Compute Pedersen commitments for private attributes (wallet secret and user's secret) - let private_attributes = vec![sk_user.sk, v]; + let private_attributes = vec![&sk_user.sk, &v]; let (private_attributes_openings, private_attributes_commitments) = compute_private_attribute_commitments(params, &joined_commitment_hash, &private_attributes); @@ -209,8 +209,8 @@ pub fn withdrawal_request( let witness = WithdrawalReqWitness { private_attributes, - joined_commitment_opening, - private_attributes_openings: private_attributes_openings.clone(), + joined_commitment_opening: &joined_commitment_opening, + private_attributes_openings: &private_attributes_openings, }; let zk_proof = WithdrawalReqProof::construct(&instance, &witness); @@ -225,7 +225,7 @@ pub fn withdrawal_request( RequestInfo { joined_commitment_hash, joined_commitment_opening, - private_attributes_openings: private_attributes_openings.clone(), + private_attributes_openings, wallet_secret: v, expiration_date, t_type, @@ -260,7 +260,7 @@ pub fn request_verify( let t_type = type_scalar(t_type); if bool::from(req.joined_commitment_hash.is_identity()) { - return Err(CompactEcashError::WithdrawalRequestVerification); + return Err(CompactEcashError::IdentityCommitmentHash); } let expected_commitment_hash = hash_g1( @@ -421,7 +421,7 @@ pub fn issue_verify( return Err(CompactEcashError::IssuanceVerification); } if bool::from(blind_signature.h.is_identity()) { - return Err(CompactEcashError::IssuanceVerification); + return Err(CompactEcashError::IdentitySignature); } // Unblind the blinded signature on the partial signature @@ -583,7 +583,7 @@ mod tests { // Generate the commitment and hash let (_, joined_commitment_hash, _) = - generate_non_identity_h(params, &sk_user, v, expiration_date, t_type); + generate_non_identity_h(params, &sk_user, &v, expiration_date, t_type); // Ensure that the joined_commitment_hash is not the identity element assert!(