diff --git a/nssa/core/src/account/commitment.rs b/nssa/core/src/account/commitment.rs index b9d9abf..9a3fc20 100644 --- a/nssa/core/src/account/commitment.rs +++ b/nssa/core/src/account/commitment.rs @@ -12,7 +12,7 @@ pub struct Commitment(pub(super) [u8; 32]); impl Commitment { pub fn new(Npk: &NullifierPublicKey, account: &Account) -> Self { let mut bytes = Vec::new(); - bytes.extend_from_slice(&Npk.to_bytes()); + bytes.extend_from_slice(&Npk.to_byte_array()); bytes.extend_from_slice(&account.to_bytes()); Self(Impl::hash_bytes(&bytes).as_bytes().try_into().unwrap()) } diff --git a/nssa/core/src/account/encoding.rs b/nssa/core/src/account/encoding.rs index 982cdee..91737b6 100644 --- a/nssa/core/src/account/encoding.rs +++ b/nssa/core/src/account/encoding.rs @@ -20,14 +20,14 @@ impl Account { } impl Commitment { - pub(crate) fn to_bytes(&self) -> [u8; 32] { + pub(crate) fn to_byte_array(&self) -> [u8; 32] { self.0 } } impl NullifierPublicKey { - pub(crate) fn to_bytes(&self) -> [u8; 32] { + pub(crate) fn to_byte_array(&self) -> [u8; 32] { self.0 } } diff --git a/nssa/core/src/account/nullifier.rs b/nssa/core/src/account/nullifier.rs index 8df2a3e..13f81db 100644 --- a/nssa/core/src/account/nullifier.rs +++ b/nssa/core/src/account/nullifier.rs @@ -7,8 +7,16 @@ use crate::account::Commitment; pub struct NullifierPublicKey(pub(super) [u8; 32]); impl From<&NullifierSecretKey> for NullifierPublicKey { - fn from(_value: &NullifierSecretKey) -> Self { - todo!() + fn from(value: &NullifierSecretKey) -> Self { + let mut bytes = Vec::new(); + const PREFIX: &[u8; 9] = b"NSSA_keys"; + const SUFFIX_1: &[u8; 1] = &[7]; + const SUFFIX_2: &[u8; 22] = &[0; 22]; + bytes.extend_from_slice(PREFIX); + bytes.extend_from_slice(value); + bytes.extend_from_slice(SUFFIX_1); + bytes.extend_from_slice(SUFFIX_2); + Self(Impl::hash_bytes(&bytes).as_bytes().try_into().unwrap()) } } @@ -20,7 +28,7 @@ pub struct Nullifier([u8; 32]); impl Nullifier { pub fn new(commitment: &Commitment, nsk: &NullifierSecretKey) -> Self { let mut bytes = Vec::new(); - bytes.extend_from_slice(&commitment.to_bytes()); + bytes.extend_from_slice(&commitment.to_byte_array()); bytes.extend_from_slice(nsk); Self(Impl::hash_bytes(&bytes).as_bytes().try_into().unwrap()) } @@ -41,4 +49,18 @@ mod tests { let nullifier = Nullifier::new(&commitment, &nsk); assert_eq!(nullifier, expected_nullifier); } + + #[test] + fn test_from_secret_key() { + let nsk = [ + 50, 139, 109, 225, 82, 86, 80, 108, 140, 248, 232, 229, 96, 80, 148, 250, 15, 9, 155, + 44, 196, 224, 115, 180, 160, 44, 113, 133, 15, 196, 253, 42, + ]; + let expected_Npk = NullifierPublicKey([ + 38, 90, 215, 216, 195, 66, 157, 77, 161, 59, 121, 18, 118, 37, 57, 199, 189, 251, 95, + 130, 12, 9, 171, 169, 140, 221, 87, 242, 46, 243, 111, 85, + ]); + let Npk = NullifierPublicKey::from(&nsk); + assert_eq!(Npk, expected_Npk); + } }