add nullifier constructor

This commit is contained in:
Sergio Chouhy 2025-08-18 09:50:11 -03:00
parent acbde736f0
commit 2813d536bb
3 changed files with 38 additions and 10 deletions

View File

@ -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 {

View File

@ -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<u8> {
@ -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;

View File

@ -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::<Vec<_>>().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);
}
}