diff --git a/cl/src/balance.rs b/cl/src/balance.rs index c79cf24..7f7a1cb 100644 --- a/cl/src/balance.rs +++ b/cl/src/balance.rs @@ -20,10 +20,6 @@ pub struct BalanceWitness { } impl Balance { - pub fn from_witness(w: BalanceWitness) -> Self { - Self(balance(w.value, &w.unit, w.blinding)) - } - pub fn to_bytes(&self) -> [u8; 32] { self.0.to_bytes() } @@ -42,6 +38,10 @@ impl BalanceWitness { Self::new(value, unit, Scalar::random(rng)) } + pub fn commit(&self) -> Balance { + Balance(balance(self.value, &self.unit, self.blinding)) + } + pub fn unit_point(&self) -> SubgroupPoint { unit_point(&self.unit) } @@ -114,8 +114,8 @@ mod test { let rng = seed_rng(0); let r = Scalar::random(rng); assert_eq!( - Balance::from_witness(BalanceWitness::new(0, "NMO", r)), - Balance::from_witness(BalanceWitness::new(0, "ETH", r)), + BalanceWitness::new(0, "NMO", r).commit(), + BalanceWitness::new(0, "ETH", r).commit(), ); } @@ -126,13 +126,10 @@ mod test { let r2 = Scalar::from(8); let a_w = BalanceWitness::new(10, "NMO", r1); let b_w = BalanceWitness::new(10, "NMO", r2); - let a = Balance::from_witness(a_w); - let b = Balance::from_witness(b_w); + let a = a_w.commit(); + let b = b_w.commit(); assert_ne!(a, b); - assert_eq!( - a.0 - b.0, - Balance::from_witness(BalanceWitness::new(0, "NMO", r1 - r2)).0 - ); + assert_eq!(a.0 - b.0, BalanceWitness::new(0, "NMO", r1 - r2).commit().0); } #[test] @@ -141,7 +138,7 @@ mod test { let r = Scalar::from(1337); let nmo = BalanceWitness::new(10, "NMO", r); let eth = BalanceWitness::new(10, "ETH", r); - assert_ne!(Balance::from_witness(nmo), Balance::from_witness(eth)); + assert_ne!(nmo.commit(), eth.commit()); } #[test] @@ -154,16 +151,13 @@ mod test { let two = BalanceWitness::new(2, "NMO", 0.into()); // Values of same unit are homomorphic - assert_eq!( - Balance::from_witness(ten).0 - Balance::from_witness(eight).0, - Balance::from_witness(two).0 - ); + assert_eq!(ten.commit().0 - eight.commit().0, two.commit().0); // Blinding factors are also homomorphic. assert_eq!( - Balance::from_witness(BalanceWitness::new(10, "NMO", r1)).0 - - Balance::from_witness(BalanceWitness::new(10, "NMO", r2)).0, - Balance::from_witness(BalanceWitness::new(0, "NMO", r1 - r2)).0 + BalanceWitness::new(10, "NMO", r1).commit().0 + - BalanceWitness::new(10, "NMO", r2).commit().0, + BalanceWitness::new(0, "NMO", r1 - r2).commit().0 ); } } diff --git a/cl/src/note.rs b/cl/src/note.rs index 443d604..4a6ae6a 100644 --- a/cl/src/note.rs +++ b/cl/src/note.rs @@ -1,6 +1,5 @@ use blake2::{Blake2s256, Digest}; use group::GroupEncoding; -use rand_core::RngCore; use risc0_groth16::VerifyingKeyJson; use serde::{Deserialize, Serialize}; @@ -20,22 +19,19 @@ impl NoteCommitment { // TODO: Rename Note to NoteWitness and NoteCommitment to Note -#[derive(Debug, Clone)] +#[derive(Debug, PartialEq, Eq, Clone)] pub struct NoteWitness { pub balance: BalanceWitness, - pub death_constraint: Vec, // serialized death_constraint + pub death_constraint: Vec, // serialized verification key of death constraint + pub state: [u8; 32], } impl NoteWitness { - pub fn random( - value: u64, - unit: impl Into, - death_constraint: &VerifyingKeyJson, - rng: impl RngCore, - ) -> Self { + pub fn new(balance: BalanceWitness, death_constraint: Vec) -> Self { Self { - balance: BalanceWitness::random(value, unit, rng), - death_constraint: bincode::serialize(death_constraint).unwrap(), + balance, + death_constraint, + state: [0u8; 32], } } @@ -48,6 +44,9 @@ impl NoteWitness { hasher.update(self.balance.unit_point().to_bytes()); // Important! we don't commit to the balance blinding factor as that may make the notes linkable. + // COMMIT TO STATE + hasher.update(self.state); + // COMMIT TO DEATH CONSTRAINT hasher.update(&self.death_constraint); @@ -60,7 +59,7 @@ impl NoteWitness { } pub fn balance(&self) -> Balance { - Balance::from_witness(self.balance.clone()) + self.balance.commit() } } @@ -73,8 +72,8 @@ mod test { #[test] fn test_note_commitments_dont_commit_to_balance_blinding() { let mut rng = seed_rng(0); - let n1 = NoteWitness::random(12, "NMO", &mut rng); - let n2 = NoteWitness::random(12, "NMO", &mut rng); + let n1 = NoteWitness::new(BalanceWitness::random(12, "NMO", &mut rng), vec![]); + let n2 = NoteWitness::new(BalanceWitness::random(12, "NMO", &mut rng), vec![]); let nf_pk = NullifierSecret::random(&mut rng).commit(); let nonce = NullifierNonce::random(&mut rng); diff --git a/cl/src/partial_tx.rs b/cl/src/partial_tx.rs index 8c8fa58..bd23682 100644 --- a/cl/src/partial_tx.rs +++ b/cl/src/partial_tx.rs @@ -1,6 +1,5 @@ use std::collections::BTreeSet; -use blake2::{Blake2s256, Digest}; use jubjub::SubgroupPoint; use rand_core::RngCore; use risc0_groth16::ProofJson;