diff --git a/risc0-selective-privacy-poc/core/src/account.rs b/risc0-selective-privacy-poc/core/src/account.rs new file mode 100644 index 0000000..d6946de --- /dev/null +++ b/risc0-selective-privacy-poc/core/src/account.rs @@ -0,0 +1,47 @@ +#![cfg_attr(not(test), no_std)] + +use serde::{Serialize, Deserialize}; +use risc0_zkvm::{sha::{Impl, Sha256}, serde::to_vec}; + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct Account { + pub address: [u32; 8], + pub balance: u128, + pub nonce: [u32; 8] +} + +impl Account { + /// Creates a new account with address = hash(private_key) and balance = 0 + pub fn new_from_private_key(private_key: [u32; 8], nonce: [u32; 8]) -> Self { + let address = hash(&private_key); + Self { address, balance: 0, nonce } + } + + pub fn new(address: [u32; 8], nonce: [u32; 8]) -> Self { + Self { address, balance: 0, nonce } + } + + /// Returns Hash(Account) + pub fn commitment(&self) -> [u32; 8] { + hash(&to_vec(&self).unwrap()) + } + +} + +pub fn hash(bytes: &[u32]) -> [u32; 8] { + Impl::hash_words(&bytes).as_words().try_into().unwrap() +} + +pub fn is_in_commitment_tree(_commitment: [u32; 8], _tree_root: [u32; 8]) -> bool { + // Dummy implementation + true +} + +/// Returns Hash(Commitment || private_key) +pub fn compute_nullifier(commitment: [u32; 8], private_key: [u32; 8]) -> [u32; 8] { + let mut bytes_to_hash = [0; 16]; + bytes_to_hash[..8].copy_from_slice(&commitment); + bytes_to_hash[8..].copy_from_slice(&private_key); + hash(&bytes_to_hash) +} + diff --git a/risc0-selective-privacy-poc/core/src/lib.rs b/risc0-selective-privacy-poc/core/src/lib.rs index 2e8901c..b0edc6c 100644 --- a/risc0-selective-privacy-poc/core/src/lib.rs +++ b/risc0-selective-privacy-poc/core/src/lib.rs @@ -1,48 +1 @@ -#![cfg_attr(not(test), no_std)] - -use serde::{Serialize, Deserialize}; -use risc0_zkvm::{sha::{Impl, Sha256}, serde::to_vec}; - -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -pub struct Account { - pub address: [u32; 8], - pub balance: u128, - pub nonce: [u32; 8] -} - -impl Account { - /// Creates a new account with address = hash(private_key) and balance = 0 - pub fn new_from_private_key(private_key: [u32; 8], nonce: [u32; 8]) -> Self { - let address = hash(&private_key); - Self { address, balance: 0, nonce } - } - - pub fn new(address: [u32; 8], nonce: [u32; 8]) -> Self { - Self { address, balance: 0, nonce } - } - - /// Returns Hash(Account) - pub fn commitment(&self) -> [u32; 8] { - hash(&to_vec(&self).unwrap()) - } - -} - - -pub fn hash(bytes: &[u32]) -> [u32; 8] { - Impl::hash_words(&bytes).as_words().try_into().unwrap() -} - -pub fn is_in_commitment_tree(_commitment: [u32; 8], _tree_root: [u32; 8]) -> bool { - // Dummy implementation - true -} - -/// Returns Hash(Commitment || private_key) -pub fn compute_nullifier(commitment: [u32; 8], private_key: [u32; 8]) -> [u32; 8] { - let mut bytes_to_hash = [0; 16]; - bytes_to_hash[..8].copy_from_slice(&commitment); - bytes_to_hash[8..].copy_from_slice(&private_key); - hash(&bytes_to_hash) -} - +pub mod account; 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 4b0f448..c76aa71 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,5 +1,5 @@ use risc0_zkvm::{guest::env, sha::{Impl, Sha256}, serde::to_vec}; -use toy_example_core::{Account, hash, compute_nullifier, is_in_commitment_tree}; +use toy_example_core::account::{Account, hash, compute_nullifier, is_in_commitment_tree}; /// Private execution logic. /// Circuit for proving correct execution of some program with program id diff --git a/risc0-selective-privacy-poc/src/private_execution.rs b/risc0-selective-privacy-poc/src/private_execution.rs index fc06248..656f323 100644 --- a/risc0-selective-privacy-poc/src/private_execution.rs +++ b/risc0-selective-privacy-poc/src/private_execution.rs @@ -5,7 +5,7 @@ use outer_methods::{ OUTER_ELF, OUTER_ID }; use risc0_zkvm::{default_prover, ExecutorEnv, Receipt}; -use toy_example_core::Account; +use toy_example_core::account::Account; /// A private execution of the transfer function. /// This actually "burns" a sender private account and "mints" two new private accounts: @@ -97,4 +97,4 @@ mod tests { fn test_private() { run_private_execution_of_transfer_program(); } -} \ No newline at end of file +} diff --git a/risc0-selective-privacy-poc/src/public_execution.rs b/risc0-selective-privacy-poc/src/public_execution.rs index 366eb25..bc0c536 100644 --- a/risc0-selective-privacy-poc/src/public_execution.rs +++ b/risc0-selective-privacy-poc/src/public_execution.rs @@ -1,5 +1,5 @@ use risc0_zkvm::{default_executor, ExecutorEnv}; -use toy_example_core::Account; +use toy_example_core::account::Account; use transfer_methods::TRANSFER_ELF; /// A public execution. @@ -44,4 +44,4 @@ mod tests { fn test_public() { run_public_execution_of_transfer_program(); } -} \ No newline at end of file +} diff --git a/risc0-selective-privacy-poc/transfer_methods/guest/src/bin/transfer.rs b/risc0-selective-privacy-poc/transfer_methods/guest/src/bin/transfer.rs index ddd7d0d..2f33c0f 100644 --- a/risc0-selective-privacy-poc/transfer_methods/guest/src/bin/transfer.rs +++ b/risc0-selective-privacy-poc/transfer_methods/guest/src/bin/transfer.rs @@ -1,5 +1,5 @@ use risc0_zkvm::{guest::env, sha::{Impl, Sha256}, serde::to_vec}; -use toy_example_core::Account; +use toy_example_core::account::Account; /// A transfer of balance program.