From 78f01dbe9a14fb8ec241f1ebca93c5ba3cbd20de Mon Sep 17 00:00:00 2001 From: aniampio Date: Tue, 21 Nov 2023 11:23:45 +0000 Subject: [PATCH] Add function to sign an expiration date --- .../src/constants.rs | 3 + common/nym_offline_compact_ecash/src/lib.rs | 1 + .../src/scheme/expiration_date_signatures.rs | 60 +++++++++++++++++++ .../src/scheme/keygen.rs | 2 +- .../src/scheme/mod.rs | 1 + .../src/scheme/setup.rs | 6 +- 6 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 common/nym_offline_compact_ecash/src/constants.rs create mode 100644 common/nym_offline_compact_ecash/src/scheme/expiration_date_signatures.rs diff --git a/common/nym_offline_compact_ecash/src/constants.rs b/common/nym_offline_compact_ecash/src/constants.rs new file mode 100644 index 0000000000..c1b2552da0 --- /dev/null +++ b/common/nym_offline_compact_ecash/src/constants.rs @@ -0,0 +1,3 @@ +pub const ATTRIBUTES_LEN: usize = 3; // number of attributes encoded in a single zk-nym credential +pub const VALIDITY_PERIOD: u64 = 30; +pub const TYPE_EXP: [u8; 32] = *b"ZKNYMEXPIRATIONDATE4llCBMEypAxr3"; \ No newline at end of file diff --git a/common/nym_offline_compact_ecash/src/lib.rs b/common/nym_offline_compact_ecash/src/lib.rs index b5790f345d..0a68c69189 100644 --- a/common/nym_offline_compact_ecash/src/lib.rs +++ b/common/nym_offline_compact_ecash/src/lib.rs @@ -26,6 +26,7 @@ mod scheme; mod tests; mod traits; mod utils; +mod constants; pub type Attribute = Scalar; 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 new file mode 100644 index 0000000000..22f44b4b08 --- /dev/null +++ b/common/nym_offline_compact_ecash/src/scheme/expiration_date_signatures.rs @@ -0,0 +1,60 @@ +use crate::scheme::setup::Parameters; +use crate::scheme::keygen::{SecretKeyAuth, VerificationKeyAuth}; +use crate::constants; +use crate::utils::hash_g1; +use bls12_381::{Scalar, G1Projective}; + +#[derive(Debug, PartialEq, Clone)] +pub struct ExpirationDateSignature{ + pub(crate) h : G1Projective, + pub(crate) s : G1Projective, +} + +pub type PartialExpirationDateSignature = ExpirationDateSignature; + +// This function is execute by a single issuing authority +pub fn sign_expiration_date(params: &Parameters, sk_auth: SecretKeyAuth, expiration_date: u64) -> Vec{ + + // Initialize a vector to collect exp_sign values + let mut exp_signs: Vec = Vec::new(); + + for l in 0..constants::VALIDITY_PERIOD { + let m0: u64 = expiration_date; + let m1: u64 = expiration_date - constants::VALIDITY_PERIOD + l; + let m2 = constants::TYPE_EXP; + + // Convert u64 values to bytes + let m0_bytes: [u8; 8] = m0.to_le_bytes(); + let m1_bytes: [u8; 8] = m1.to_le_bytes(); + let mut concatenated_attributes = Vec::new(); + + concatenated_attributes.extend_from_slice(&m0_bytes); + concatenated_attributes.extend_from_slice(&m1_bytes); + concatenated_attributes.extend_from_slice(&m2); + + // Compute the hash + let h = hash_g1(concatenated_attributes); + + // Compute s = x + y[0]*m0 + y[1]*m1 + y[2]*m2 + let mut s_exponent = sk_auth.x; + + // Perform scalar-point multiplications and accumulate the result + s_exponent += &sk_auth.ys[0] * Scalar::from(m0); + s_exponent += &sk_auth.ys[1] * Scalar::from(m1); + s_exponent += &sk_auth.ys[2] * Scalar::from_bytes(&m2).unwrap(); + + // Create the signature on the expiration date + let exp_sign = PartialExpirationDateSignature { + h, + s: h * s_exponent, + }; + + // Collect the exp_sign value into the vector + exp_signs.push(exp_sign); + } + + exp_signs + +} + +// pub fn aggregate_expiration_signatures(params: &Parameters, vk_auth: &VerificationKeyAuth, expiration_date: u64, indices: Option<&[u64]>, ) \ No newline at end of file diff --git a/common/nym_offline_compact_ecash/src/scheme/keygen.rs b/common/nym_offline_compact_ecash/src/scheme/keygen.rs index 13d670fb20..03ec1c344f 100644 --- a/common/nym_offline_compact_ecash/src/scheme/keygen.rs +++ b/common/nym_offline_compact_ecash/src/scheme/keygen.rs @@ -405,7 +405,7 @@ pub fn ttp_keygen( // generate polynomials let v = Polynomial::new_random(params, threshold - 1); - let ws = (0..attributes) + let ws = (0..attributes+1) .map(|_| Polynomial::new_random(params, threshold - 1)) .collect::>(); diff --git a/common/nym_offline_compact_ecash/src/scheme/mod.rs b/common/nym_offline_compact_ecash/src/scheme/mod.rs index 14bfe73088..f3c99343d5 100644 --- a/common/nym_offline_compact_ecash/src/scheme/mod.rs +++ b/common/nym_offline_compact_ecash/src/scheme/mod.rs @@ -20,6 +20,7 @@ pub mod identify; pub mod keygen; pub mod setup; pub mod withdrawal; +pub mod expiration_date_signatures; #[derive(Debug, Clone, PartialEq)] pub struct PartialWallet { diff --git a/common/nym_offline_compact_ecash/src/scheme/setup.rs b/common/nym_offline_compact_ecash/src/scheme/setup.rs index f88f28c479..cbeebb10bf 100644 --- a/common/nym_offline_compact_ecash/src/scheme/setup.rs +++ b/common/nym_offline_compact_ecash/src/scheme/setup.rs @@ -6,8 +6,8 @@ use rand::thread_rng; use crate::error::{CompactEcashError, Result}; use crate::utils::{hash_g1, Signature}; +use crate::constants::ATTRIBUTES_LEN; -const ATTRIBUTES_LEN: usize = 3; pub struct GroupParameters { /// Generator of the G1 group @@ -51,10 +51,6 @@ impl GroupParameters { &self.gammas } - // pub(crate) fn gamma1(&self) -> &G1Projective { - // &self.gammas[0] - // } - pub(crate) fn gamma_idx(&self, i: usize) -> Option<&G1Projective>{ self.gammas.get(i) }