From 9c22c082c0643125d7429fdb9fecb9a203f5a2d7 Mon Sep 17 00:00:00 2001 From: aniampio Date: Mon, 2 May 2022 17:28:08 +0100 Subject: [PATCH] Draft of the spend function --- .../src/proofs/proof_spend.rs | 4 + .../src/scheme/mod.rs | 107 +++++++++++++++++- .../src/scheme/setup.rs | 10 +- .../src/scheme/spend.rs | 0 4 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 common/nym_offline_divisible_ecash/src/proofs/proof_spend.rs delete mode 100644 common/nym_offline_divisible_ecash/src/scheme/spend.rs diff --git a/common/nym_offline_divisible_ecash/src/proofs/proof_spend.rs b/common/nym_offline_divisible_ecash/src/proofs/proof_spend.rs new file mode 100644 index 0000000000..68d27595d4 --- /dev/null +++ b/common/nym_offline_divisible_ecash/src/proofs/proof_spend.rs @@ -0,0 +1,4 @@ +pub struct SpendInstance {} + +pub struct SpendWitness {} + diff --git a/common/nym_offline_divisible_ecash/src/scheme/mod.rs b/common/nym_offline_divisible_ecash/src/scheme/mod.rs index 7eced2191a..5af94f915f 100644 --- a/common/nym_offline_divisible_ecash/src/scheme/mod.rs +++ b/common/nym_offline_divisible_ecash/src/scheme/mod.rs @@ -1,6 +1,13 @@ -use bls12_381::Scalar; +use std::cell::Cell; -use crate::utils::{Signature, SignerIndex}; +use bls12_381::{G2Projective, Scalar}; + +use crate::Attribute; +use crate::constants::L; +use crate::error::{DivisibleEcashError, Result}; +use crate::scheme::keygen::{SecretKeyUser, VerificationKeyAuth}; +use crate::scheme::setup::{GroupParameters, Parameters}; +use crate::utils::{hash_to_scalar, Signature, SignerIndex}; pub mod aggregation; pub mod identify; @@ -9,8 +16,104 @@ pub mod setup; pub mod structure_preserving_signature; pub mod withdrawal; +pub fn compute_kappa( + params: &GroupParameters, + verification_key: &VerificationKeyAuth, + attributes: &[Attribute], + blinding_factor: Scalar, +) -> G2Projective { + 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::() +} + +pub struct PayInfo { + pub info: [u8; 32], +} + +#[derive(Debug, Clone)] +pub struct Payment {} + pub struct PartialWallet { sig: Signature, v: Scalar, idx: Option, +} + +pub struct Wallet { + sig: Signature, + v: Scalar, + l: Cell, +} + +impl Wallet { + pub fn signature(&self) -> &Signature { + &self.sig + } + + pub fn v(&self) -> Scalar { + self.v + } + + pub fn l(&self) -> u64 { + self.l.get() + } + + fn up(&self) { + self.l.set(self.l.get() + 1); + } + + fn down(&self) { + self.l.set(self.l.get() - 1); + } + + pub(crate) fn spend( + &self, + params: &Parameters, + verification_key: &VerificationKeyAuth, + sk_user: &SecretKeyUser, + pay_info: &PayInfo, + V: u64, + ) -> Result<(Payment, &Self)> { + if self.l() + V > L { + return Err(DivisibleEcashError::Spend( + "The counter l is higher than max L".to_string(), + )); + } + + let grp = params.get_grp(); + let paramsU = params.get_paramsUser(); + // randomize signature in the wallet + let (signature_prime, sign_blinding_factor) = self.signature().randomise(grp); + // construct kappa i.e., blinded attributes for show + let attributes = vec![sk_user.sk, self.v()]; + // compute kappa + let kappa = compute_kappa( + &grp, + &verification_key, + &attributes, + sign_blinding_factor, + ); + + let r1 = grp.random_scalar(); + let r2 = grp.random_scalar(); + let phi1 = grp.gen1() * r1; + let phi2 = paramsU.get_ith_sigma(self.l.get() as usize) * self.v + paramsU.get_ith_eta(V as usize) * r1; + + // compute hash of the payment info + let rr = hash_to_scalar(pay_info.info); + let rho1 = grp.gen1() * r2; + let rho2 = (grp.gen1() * rr) * sk_user.sk + paramsU.get_ith_theta(self.l.get() as usize) * self.v + paramsU.get_ith_eta(V as usize) * r2; + + // compute the zk proof + + // output pay and updated wallet + let pay = Payment {}; + + Ok((pay, self)) + } } \ No newline at end of file diff --git a/common/nym_offline_divisible_ecash/src/scheme/setup.rs b/common/nym_offline_divisible_ecash/src/scheme/setup.rs index a53cae0b86..c7ba8f4e7a 100644 --- a/common/nym_offline_divisible_ecash/src/scheme/setup.rs +++ b/common/nym_offline_divisible_ecash/src/scheme/setup.rs @@ -58,7 +58,7 @@ pub struct Parameters { impl Parameters { pub(crate) fn get_grp(&self) -> &GroupParameters { &self.grp } - + pub(crate) fn get_paramsUser(&self) -> &ParametersUser { &self.paramsUser } pub(crate) fn get_paramsAuth(&self) -> &ParametersAuthority { &self.paramsAuth } @@ -153,12 +153,20 @@ impl ParametersUser { pub(crate) fn get_etas(&self) -> &[G1Projective] { &self.etas } + pub(crate) fn get_ith_eta(&self, idx: usize) -> &G1Projective { self.etas.get(idx).unwrap() } + pub(crate) fn get_sigmas(&self) -> &[G1Projective] { &self.sigmas } + pub(crate) fn get_ith_sigma(&self, idx: usize) -> &G1Projective { self.sigmas.get(idx).unwrap() } + pub(crate) fn get_thetas(&self) -> &[G1Projective] { &self.thetas } + pub(crate) fn get_ith_theta(&self, idx: usize) -> &G1Projective { self.thetas.get(idx).unwrap() } + pub(crate) fn get_sps_signs(&self) -> &[SPSSignature] { &self.sps_signatures } + pub(crate) fn get_ith_sps_sign(&self, idx: usize) -> &SPSSignature { &self.sps_signatures.get(idx).unwrap() } + pub(crate) fn get_sps_pk(&self) -> &SPSVerificationKey { &self.sps_pk } } diff --git a/common/nym_offline_divisible_ecash/src/scheme/spend.rs b/common/nym_offline_divisible_ecash/src/scheme/spend.rs deleted file mode 100644 index e69de29bb2..0000000000