From c322f937757a226bc024c1905b6ae7dde60b17bc Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 16 Jul 2025 15:37:49 -0300 Subject: [PATCH] refactor --- .../core/src/account.rs | 66 ++----------------- risc0-selective-privacy-poc/core/src/input.rs | 2 +- risc0-selective-privacy-poc/core/src/lib.rs | 48 ++++++++++++++ .../outer_methods/guest/src/bin/outer.rs | 3 +- .../sparse_merkle_tree/src/lib.rs | 6 +- .../src/private_execution.rs | 4 +- 6 files changed, 61 insertions(+), 68 deletions(-) diff --git a/risc0-selective-privacy-poc/core/src/account.rs b/risc0-selective-privacy-poc/core/src/account.rs index 1083ca6..85f6251 100644 --- a/risc0-selective-privacy-poc/core/src/account.rs +++ b/risc0-selective-privacy-poc/core/src/account.rs @@ -1,16 +1,10 @@ -use risc0_zkvm::{ - serde::to_vec, - sha::{Impl, Sha256}, +use crate::{ + hash, + types::{Address, Commitment, Nonce}, }; +use risc0_zkvm::{serde::to_vec, sha::Impl}; use serde::{Deserialize, Serialize}; -pub type Commitment = u32; -pub type Nullifier = [u32; 8]; -pub type Address = [u32; 8]; -pub type Nonce = [u32; 8]; -pub type Key = [u32; 8]; -pub type AuthenticationPath = [[u32; 8]; 32]; - #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Account { pub address: Address, @@ -22,11 +16,7 @@ impl Account { /// Creates a new account with address = hash(private_key) and balance = 0 pub fn new_from_private_key(private_key: Address, nonce: Nonce) -> Self { let address = hash(&private_key); - Self { - address, - balance: 0, - nonce, - } + Self::new(address, nonce) } pub fn new(address: Address, nonce: Nonce) -> Self { @@ -42,49 +32,3 @@ impl Account { hash(&to_vec(&self).unwrap())[0] } } - -pub fn hash(bytes: &[u32]) -> [u32; 8] { - Impl::hash_words(bytes).as_words().try_into().unwrap() -} - -pub fn is_in_tree(commitment: Commitment, path: &AuthenticationPath, root: [u32; 8]) -> bool { - const HASH_ONE: [u32; 8] = [ - 789771595, 3310634292, 3140410939, 3820475020, 3591004369, 2777006897, 1021496535, - 2588247415, - ]; - - let mut hash = HASH_ONE; - let mut current_index = commitment; - for path_value in path.iter() { - if current_index & 1 == 0 { - hash = hash_two(&hash, path_value); - } else { - hash = hash_two(path_value, &hash); - } - current_index >>= 1; - } - root == hash -} - -/// Returns Hash(Commitment || private_key) -pub fn compute_nullifier(commitment: &Commitment, private_key: &Key) -> Nullifier { - let mut bytes_to_hash = [0; 9]; // <- 1 word for the commitment, 8 words for the private key - bytes_to_hash[..1].copy_from_slice(&[*commitment]); - bytes_to_hash[1..].copy_from_slice(private_key); - hash(&bytes_to_hash) -} - -fn hash_two(left: &[u32; 8], right: &[u32; 8]) -> [u32; 8] { - let mut bytes_to_hash = [0; 16]; - bytes_to_hash[..8].copy_from_slice(left); - bytes_to_hash[8..].copy_from_slice(right); - hash(&bytes_to_hash) -} - -pub fn bytes_to_words(bytes: &[u8; 32]) -> [u32; 8] { - let mut words = [0; 8]; - for (i, chunk) in bytes.chunks(4).enumerate() { - words[i] = u32::from_le_bytes(chunk.try_into().unwrap()); - } - words -} diff --git a/risc0-selective-privacy-poc/core/src/input.rs b/risc0-selective-privacy-poc/core/src/input.rs index fc7930f..944164a 100644 --- a/risc0-selective-privacy-poc/core/src/input.rs +++ b/risc0-selective-privacy-poc/core/src/input.rs @@ -1,5 +1,5 @@ -use crate::account::{AuthenticationPath, Key}; use serde::{Deserialize, Serialize}; +use crate::types::{AuthenticationPath, Key}; #[derive(Serialize, Deserialize)] pub enum InputVisibiility { diff --git a/risc0-selective-privacy-poc/core/src/lib.rs b/risc0-selective-privacy-poc/core/src/lib.rs index 2b84523..87d255f 100644 --- a/risc0-selective-privacy-poc/core/src/lib.rs +++ b/risc0-selective-privacy-poc/core/src/lib.rs @@ -1,5 +1,53 @@ pub mod account; pub mod input; +pub mod types; +use crate::types::{AuthenticationPath, Commitment, Key, Nullifier}; +use risc0_zkvm::sha::{Impl, Sha256}; +use serde::{Deserialize, Serialize}; +pub fn hash(bytes: &[u32]) -> [u32; 8] { + Impl::hash_words(bytes).as_words().try_into().unwrap() +} +pub fn is_in_tree(commitment: Commitment, path: &AuthenticationPath, root: [u32; 8]) -> bool { + const HASH_ONE: [u32; 8] = [ + 789771595, 3310634292, 3140410939, 3820475020, 3591004369, 2777006897, 1021496535, + 2588247415, + ]; + + let mut hash = HASH_ONE; + let mut current_index = commitment; + for path_value in path.iter() { + if current_index & 1 == 0 { + hash = hash_two(&hash, path_value); + } else { + hash = hash_two(path_value, &hash); + } + current_index >>= 1; + } + root == hash +} + +/// Returns Hash(Commitment || private_key) +pub fn compute_nullifier(commitment: &Commitment, private_key: &Key) -> Nullifier { + let mut bytes_to_hash = [0; 9]; // <- 1 word for the commitment, 8 words for the private key + bytes_to_hash[..1].copy_from_slice(&[*commitment]); + bytes_to_hash[1..].copy_from_slice(private_key); + hash(&bytes_to_hash) +} + +fn hash_two(left: &[u32; 8], right: &[u32; 8]) -> [u32; 8] { + let mut bytes_to_hash = [0; 16]; + bytes_to_hash[..8].copy_from_slice(left); + bytes_to_hash[8..].copy_from_slice(right); + hash(&bytes_to_hash) +} + +pub fn bytes_to_words(bytes: &[u8; 32]) -> [u32; 8] { + let mut words = [0; 8]; + for (i, chunk) in bytes.chunks(4).enumerate() { + words[i] = u32::from_le_bytes(chunk.try_into().unwrap()); + } + words +} diff --git a/risc0-selective-privacy-poc/outer_methods/guest/src/bin/outer.rs b/risc0-selective-privacy-poc/outer_methods/guest/src/bin/outer.rs index bcfa30e..186b357 100644 --- a/risc0-selective-privacy-poc/outer_methods/guest/src/bin/outer.rs +++ b/risc0-selective-privacy-poc/outer_methods/guest/src/bin/outer.rs @@ -1,7 +1,6 @@ use risc0_zkvm::{guest::env, serde::to_vec}; use toy_example_core::{ - account::{compute_nullifier, hash, is_in_tree, Account, Nonce}, - input::InputVisibiility, + account::Account, compute_nullifier, hash, input::InputVisibiility, is_in_tree, types::Nonce, }; /// Private execution logic. diff --git a/risc0-selective-privacy-poc/sparse_merkle_tree/src/lib.rs b/risc0-selective-privacy-poc/sparse_merkle_tree/src/lib.rs index fd53197..8d09d9c 100644 --- a/risc0-selective-privacy-poc/sparse_merkle_tree/src/lib.rs +++ b/risc0-selective-privacy-poc/sparse_merkle_tree/src/lib.rs @@ -1,9 +1,9 @@ +mod default_hashes; + +use default_hashes::DEFAULT_HASHES; use sha2::{Digest, Sha256}; use std::collections::{HashMap, HashSet}; -mod default_hashes; -use default_hashes::DEFAULT_HASHES; - const TREE_DEPTH: usize = 32; const ZERO_HASH: [u8; 32] = [ 110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, diff --git a/risc0-selective-privacy-poc/src/private_execution.rs b/risc0-selective-privacy-poc/src/private_execution.rs index 60a52c1..1390d7b 100644 --- a/risc0-selective-privacy-poc/src/private_execution.rs +++ b/risc0-selective-privacy-poc/src/private_execution.rs @@ -3,8 +3,10 @@ use rand::{rngs::OsRng, Rng}; use risc0_zkvm::{default_prover, ExecutorEnv, Receipt}; use sparse_merkle_tree::SparseMerkleTree; use toy_example_core::{ - account::{bytes_to_words, Account, Address, AuthenticationPath, Commitment, Nonce, Nullifier}, + account::Account, + bytes_to_words, input::InputVisibiility, + types::{Address, AuthenticationPath, Commitment, Nonce, Nullifier}, }; use transfer_methods::{TRANSFER_ELF, TRANSFER_ID};