From d4a306d67ad225d746600d639f0d3d15936d076d Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 16 Jul 2025 17:25:03 -0300 Subject: [PATCH] moved examples to examples dir --- .../{src => examples}/private_execution.rs | 25 ++++--------------- .../examples/programs/mod.rs | 11 ++++++++ .../{src => examples}/public_execution.rs | 21 +++++----------- risc0-selective-privacy-poc/src/lib.rs | 15 +---------- risc0-selective-privacy-poc/src/program.rs | 14 +++++++---- 5 files changed, 32 insertions(+), 54 deletions(-) rename risc0-selective-privacy-poc/{src => examples}/private_execution.rs (85%) create mode 100644 risc0-selective-privacy-poc/examples/programs/mod.rs rename risc0-selective-privacy-poc/{src => examples}/public_execution.rs (80%) diff --git a/risc0-selective-privacy-poc/src/private_execution.rs b/risc0-selective-privacy-poc/examples/private_execution.rs similarity index 85% rename from risc0-selective-privacy-poc/src/private_execution.rs rename to risc0-selective-privacy-poc/examples/private_execution.rs index 1cb3ceb..c580c78 100644 --- a/risc0-selective-privacy-poc/src/private_execution.rs +++ b/risc0-selective-privacy-poc/examples/private_execution.rs @@ -1,5 +1,7 @@ +mod programs; + use outer_methods::{OUTER_ELF, OUTER_ID}; -use rand::{rngs::OsRng, Rng}; +use programs::TransferProgram; use risc0_zkvm::{default_prover, ExecutorEnv, ProveInfo, Receipt}; use sparse_merkle_tree::SparseMerkleTree; use toy_example_core::{ @@ -9,14 +11,7 @@ use toy_example_core::{ types::{Address, AuthenticationPath, Commitment, Nonce, Nullifier}, }; use transfer_methods::{TRANSFER_ELF, TRANSFER_ID}; - -use crate::program::{execute_and_prove, prove_privacy_execution, Program}; -use crate::TransferProgram; - -pub fn new_random_nonce() -> Nonce { - let mut rng = OsRng; - std::array::from_fn(|_| rng.gen()) -} +use tuki::program::{prove_privacy_execution, Program}; fn mint_fresh_account(address: Address) -> Account { let nonce = [0; 8]; @@ -26,7 +21,7 @@ fn mint_fresh_account(address: Address) -> Account { /// A private execution of the transfer function. /// This actually "burns" a sender private account and "mints" two new private accounts: /// one for the recipient with the transferred balance, and another owned by the sender with the remaining balance. -fn run_private_execution_of_transfer_program() { +fn main() { // This is supposed to be an existing private account (UTXO) with balance equal to 150. // And it is supposed to be a private account of the user running this private execution (hence the access to the private key) let sender_private_key = [1, 2, 3, 4, 4, 3, 2, 1]; @@ -74,13 +69,3 @@ fn run_private_execution_of_transfer_program() { println!("nullifiers: {:?}", output.1); println!("commitments: {:?}", output.2); } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_private() { - run_private_execution_of_transfer_program(); - } -} diff --git a/risc0-selective-privacy-poc/examples/programs/mod.rs b/risc0-selective-privacy-poc/examples/programs/mod.rs new file mode 100644 index 0000000..1bb7219 --- /dev/null +++ b/risc0-selective-privacy-poc/examples/programs/mod.rs @@ -0,0 +1,11 @@ +use transfer_methods::{TRANSFER_ELF, TRANSFER_ID}; +use tuki::program::Program; + +pub struct TransferProgram; + +impl Program for TransferProgram { + const PROGRAM_ID: [u32; 8] = TRANSFER_ID; + const PROGRAM_ELF: &[u8] = TRANSFER_ELF; + // Amount to transfer + type InstructionData = u128; +} diff --git a/risc0-selective-privacy-poc/src/public_execution.rs b/risc0-selective-privacy-poc/examples/public_execution.rs similarity index 80% rename from risc0-selective-privacy-poc/src/public_execution.rs rename to risc0-selective-privacy-poc/examples/public_execution.rs index 44395a9..c7cb566 100644 --- a/risc0-selective-privacy-poc/src/public_execution.rs +++ b/risc0-selective-privacy-poc/examples/public_execution.rs @@ -1,16 +1,17 @@ +mod programs; + use risc0_zkvm::{default_executor, ExecutorEnv}; use toy_example_core::account::Account; use transfer_methods::TRANSFER_ELF; -use crate::{ - program::{execute, Program}, - TransferProgram, -}; +use tuki::program::{execute, Program}; + +use crate::programs::TransferProgram; /// A public execution. /// This would be executed by the runtime after checking that /// the initiating transaction includes the sender's signature. -pub fn run_public_execution_of_transfer_program() { +pub fn main() { // Account fetched from the chain state with 150 in its balance. let sender = { let mut account = Account::new([5; 8], [98; 8]); @@ -38,13 +39,3 @@ pub fn run_public_execution_of_transfer_program() { inputs_outputs[1], inputs_outputs[3], ); } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_public() { - run_public_execution_of_transfer_program(); - } -} diff --git a/risc0-selective-privacy-poc/src/lib.rs b/risc0-selective-privacy-poc/src/lib.rs index 7d9f777..5dc5d07 100644 --- a/risc0-selective-privacy-poc/src/lib.rs +++ b/risc0-selective-privacy-poc/src/lib.rs @@ -1,15 +1,2 @@ -mod private_execution; -mod program; -mod public_execution; +pub mod program; -use program::Program; -use transfer_methods::{TRANSFER_ELF, TRANSFER_ID}; - -struct TransferProgram; - -impl Program for TransferProgram { - const PROGRAM_ID: [u32; 8] = TRANSFER_ID; - const PROGRAM_ELF: &[u8] = TRANSFER_ELF; - // Amount to transfer - type InstructionData = u128; -} diff --git a/risc0-selective-privacy-poc/src/program.rs b/risc0-selective-privacy-poc/src/program.rs index ade94c0..d83813a 100644 --- a/risc0-selective-privacy-poc/src/program.rs +++ b/risc0-selective-privacy-poc/src/program.rs @@ -1,13 +1,17 @@ use outer_methods::OUTER_ELF; +use rand::{rngs::OsRng, Rng}; use risc0_zkvm::{ default_executor, default_prover, ExecutorEnv, ExecutorEnvBuilder, ProveInfo, Receipt, }; use serde::{Deserialize, Serialize}; -use toy_example_core::{account::Account, input::InputVisibiility}; +use toy_example_core::{account::Account, input::InputVisibiility, types::Nonce}; -use crate::private_execution::new_random_nonce; +pub fn new_random_nonce() -> Nonce { + let mut rng = OsRng; + std::array::from_fn(|_| rng.gen()) +} -pub(crate) trait Program { +pub trait Program { const PROGRAM_ID: [u32; 8]; const PROGRAM_ELF: &[u8]; type InstructionData: Serialize + for<'de> Deserialize<'de>; @@ -45,7 +49,7 @@ pub(crate) fn execute_and_prove( Ok((receipt, inputs_outputs)) } -pub(crate) fn execute( +pub fn execute( input_accounts: &[Account], instruction_data: &P::InstructionData, ) -> Result, ()> { @@ -64,7 +68,7 @@ pub(crate) fn execute( Ok(inputs_outputs) } -pub(crate) fn prove_privacy_execution( +pub fn prove_privacy_execution( inputs: &[Account], instruction_data: &P::InstructionData, visibilities: &[InputVisibiility],