diff --git a/nssa/core/src/account/commitment.rs b/nssa/core/src/account/commitment.rs index a288c0a..b9d9abf 100644 --- a/nssa/core/src/account/commitment.rs +++ b/nssa/core/src/account/commitment.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::account::{Account, NullifierPublicKey}; #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] -pub struct Commitment([u8; 32]); +pub struct Commitment(pub(super) [u8; 32]); impl Commitment { pub fn new(Npk: &NullifierPublicKey, account: &Account) -> Self { diff --git a/nssa/core/src/account/encoding.rs b/nssa/core/src/account/encoding.rs index c560a9d..982cdee 100644 --- a/nssa/core/src/account/encoding.rs +++ b/nssa/core/src/account/encoding.rs @@ -3,7 +3,7 @@ use risc0_zkvm::{ sha::{Impl, Sha256}, }; -use crate::account::Account; +use crate::account::{Account, Commitment, NullifierPublicKey}; impl Account { pub fn to_bytes(&self) -> Vec { @@ -19,6 +19,19 @@ impl Account { } } +impl Commitment { + pub(crate) fn to_bytes(&self) -> [u8; 32] { + self.0 + } +} + + +impl NullifierPublicKey { + pub(crate) fn to_bytes(&self) -> [u8; 32] { + self.0 + } +} + #[cfg(test)] mod tests { use crate::account::Account; diff --git a/nssa/core/src/account/nullifier.rs b/nssa/core/src/account/nullifier.rs index 155afe2..8df2a3e 100644 --- a/nssa/core/src/account/nullifier.rs +++ b/nssa/core/src/account/nullifier.rs @@ -1,15 +1,10 @@ +use risc0_zkvm::sha::{Impl, Sha256}; use serde::{Deserialize, Serialize}; use crate::account::Commitment; #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] -pub struct NullifierPublicKey([u8; 32]); - -impl NullifierPublicKey { - pub(crate) fn to_bytes(&self) -> [u8; 32] { - self.0 - } -} +pub struct NullifierPublicKey(pub(super) [u8; 32]); impl From<&NullifierSecretKey> for NullifierPublicKey { fn from(_value: &NullifierSecretKey) -> Self { @@ -24,6 +19,26 @@ pub struct Nullifier([u8; 32]); impl Nullifier { pub fn new(commitment: &Commitment, nsk: &NullifierSecretKey) -> Self { - todo!() + let mut bytes = Vec::new(); + bytes.extend_from_slice(&commitment.to_bytes()); + bytes.extend_from_slice(nsk); + Self(Impl::hash_bytes(&bytes).as_bytes().try_into().unwrap()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_constructor() { + let commitment = Commitment((0..32u8).collect::>().try_into().unwrap()); + let nsk = [0x42; 32]; + let expected_nullifier = Nullifier([ + 97, 87, 111, 191, 0, 44, 125, 145, 237, 104, 31, 230, 203, 254, 68, 176, 126, 17, 240, + 205, 249, 143, 11, 43, 15, 198, 189, 219, 191, 49, 36, 61, + ]); + let nullifier = Nullifier::new(&commitment, &nsk); + assert_eq!(nullifier, expected_nullifier); } }