From fe6764d56e8573ed281bdc0b549ecce85161efb7 Mon Sep 17 00:00:00 2001 From: David Rusu Date: Wed, 12 Jun 2024 00:02:08 -0400 Subject: [PATCH] cl: split main.rs into crypto.rs and note.rs --- cl/src/crypto.rs | 13 ++++++++++ cl/src/main.rs | 61 ++--------------------------------------------- cl/src/note.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 cl/src/crypto.rs create mode 100644 cl/src/note.rs diff --git a/cl/src/crypto.rs b/cl/src/crypto.rs new file mode 100644 index 0000000..a67965a --- /dev/null +++ b/cl/src/crypto.rs @@ -0,0 +1,13 @@ +use blake2::{Blake2s256, Digest}; +use group::Group; +use jubjub::ExtendedPoint; +use rand_chacha::ChaCha20Rng; +use rand_core::SeedableRng; + +pub fn hash_to_curve(bytes: &[u8]) -> ExtendedPoint { + let mut hasher = Blake2s256::new(); + hasher.update(b"NOMOS_HASH_TO_CURVE"); + hasher.update(bytes); + let seed: [u8; 32] = hasher.finalize().into(); + ExtendedPoint::random(ChaCha20Rng::from_seed(seed)) +} diff --git a/cl/src/main.rs b/cl/src/main.rs index 4a80a5a..1bd1f87 100644 --- a/cl/src/main.rs +++ b/cl/src/main.rs @@ -1,63 +1,6 @@ -use blake2::{Blake2s256, Digest}; -use group::Group; -use jubjub::{AffinePoint, ExtendedPoint, Scalar}; -use lazy_static::lazy_static; -use rand_chacha::ChaCha20Rng; -use rand_core::SeedableRng; - -lazy_static! { - static ref PEDERSON_COMMITMENT_BLINDING_POINT: ExtendedPoint = - hash_to_curve(b"NOMOS_CL_PEDERSON_COMMITMENT_BLINDING"); -} - -fn hash_to_curve(bytes: &[u8]) -> ExtendedPoint { - let mut hasher = Blake2s256::new(); - hasher.update(b"NOMOS_HASH_TO_CURVE"); - hasher.update(bytes); - let seed: [u8; 32] = hasher.finalize().into(); - ExtendedPoint::random(ChaCha20Rng::from_seed(seed)) -} - -struct Note { - value: u64, - unit: String, -} - -impl Note { - fn new(value: u64, unit: impl Into) -> Self { - Self { - value, - unit: unit.into(), - } - } - - fn balance(&self, blinding: Scalar) -> ExtendedPoint { - let value_scalar = Scalar::from(self.value); - let unit_point = hash_to_curve(self.unit.as_bytes()); - - unit_point * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding - } -} +mod crypto; +mod note; fn main() { println!("Hello, world!"); } - -#[test] -fn test_note_balance() { - let r = Scalar::from(32); - - let a = Note::new(10, "NMO"); - let b = Note::new(10, "NMO"); - assert_eq!(a.balance(r), b.balance(r)); - - // balances are be homomorphic - assert_eq!( - AffinePoint::from(Note::new(10, "NMO").balance(r) - Note::new(8, "NMO").balance(r)), - AffinePoint::from(Note::new(2, "NMO").balance(r - r)) - ); - - let d = Note::new(10, "ETH"); - - assert_ne!(a.balance(r), d.balance(r)) -} diff --git a/cl/src/note.rs b/cl/src/note.rs new file mode 100644 index 0000000..978341d --- /dev/null +++ b/cl/src/note.rs @@ -0,0 +1,62 @@ +use jubjub::{ExtendedPoint, Scalar}; +use lazy_static::lazy_static; + +use crate::crypto; + +lazy_static! { + static ref PEDERSON_COMMITMENT_BLINDING_POINT: ExtendedPoint = + crypto::hash_to_curve(b"NOMOS_CL_PEDERSON_COMMITMENT_BLINDING"); +} + +pub struct Note { + pub value: u64, + pub unit: String, +} + +impl Note { + pub fn new(value: u64, unit: impl Into) -> Self { + Self { + value, + unit: unit.into(), + } + } + + pub fn balance(&self, blinding: Scalar) -> ExtendedPoint { + let value_scalar = Scalar::from(self.value); + let unit_point = crypto::hash_to_curve(self.unit.as_bytes()); + + unit_point * value_scalar + *PEDERSON_COMMITMENT_BLINDING_POINT * blinding + } +} + +#[test] +fn test_note_balance() { + // balances are blinded + let a = Note::new(10, "NMO"); + assert_ne!(a.balance(12.into()), a.balance(8.into())); + + // balances are deterministic + assert_eq!(a.balance(12.into()), a.balance(12.into())); + + // balances are be homomorphic + let r = Scalar::from(32); + let ten = Note::new(10, "NMO"); + let eight = Note::new(8, "NMO"); + let two = Note::new(2, "NMO"); + assert_eq!(ten.balance(r) - eight.balance(r), two.balance(r - r)); + + assert_eq!( + ten.balance(54.into()) - ten.balance(48.into()), + Note::new(0, "NMO").balance(6.into()) + ); + + // Unit's differentiate between values. + let d = Note::new(10, "ETH"); + assert_ne!(a.balance(r), d.balance(r)); + + // Zero is the same across all units + assert_eq!( + Note::new(0, "NMO").balance(r), + Note::new(0, "ETH").balance(r) + ); +}