From 2175fcd4971d35bab48b401e825062b1fb48d6b6 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Fri, 11 Jul 2025 19:44:43 -0300 Subject: [PATCH] minor changes --- .../outer_methods/guest/src/bin/outer.rs | 8 +++---- .../src/private_execution.rs | 11 ++++++---- .../src/public_execution.rs | 21 +++++++++++++------ .../guest/src/bin/transfer.rs | 2 +- 4 files changed, 27 insertions(+), 15 deletions(-) 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 85a4865..4b0f448 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 @@ -5,11 +5,12 @@ use toy_example_core::{Account, hash, compute_nullifier, is_in_commitment_tree}; /// Circuit for proving correct execution of some program with program id /// equal to `program_id` (last input). /// -/// Currently only supports private execution of a program with two inputs, one -/// of which must be a fresh new account (for example a private transfer function) +/// Currently only supports private execution of a program with two input accounts, one +/// of which must be a fresh new account (`account_2`) (for example a private transfer function). /// /// This circuit checks: /// - That accounts pre states and post states are consistent with the execution of the given `program_id`. +/// - That `account_2` is fresh (meaning, for this toy example, that it has 0 balance). /// - That `program_id` execution didn't change addresses of the accounts. /// /// Outputs: @@ -20,13 +21,12 @@ fn main() { let account_1_private_key: [u32; 8] = env::read(); let account_1: Account = env::read(); let account_2: Account = env::read(); - let balance_to_move: u128 = env::read(); let account_1_post: Account = env::read(); let account_2_post: Account = env::read(); let commitment_tree_root: [u32; 8] = env::read(); let program_id: [u32; 8] = env::read(); - // Assert account_2 account is fresh + // Assert account_2 is a fresh account assert_eq!(account_2.balance, 0); // Prove ownership of account_1 account by proving diff --git a/risc0-selective-privacy-poc/src/private_execution.rs b/risc0-selective-privacy-poc/src/private_execution.rs index fdea687..fc06248 100644 --- a/risc0-selective-privacy-poc/src/private_execution.rs +++ b/risc0-selective-privacy-poc/src/private_execution.rs @@ -9,14 +9,18 @@ use toy_example_core::Account; /// A private execution of the transfer function. /// This actually "burns" a sender private account and "mints" two new private accounts: -/// one for the recepient with the transfered balance, and another owned by the sender with the remaining balance. +/// 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() { let commitment_tree_root = [0xdd, 0xee, 0xaa, 0xdd, 0xbb, 0xee, 0xee, 0xff]; // 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 = [0; 8]; - let mut sender = Account::new_from_private_key(sender_private_key, [1; 8]); - sender.balance = 150; + let sender = { + // Creating it now but it's supposed to be already created by other previous transactions. + let mut account = Account::new_from_private_key(sender_private_key, [1; 8]); + account.balance = 150; + account + }; let balance_to_move: u128 = 3; // This is the new private account (UTXO) being minted by this private execution. @@ -35,7 +39,6 @@ fn run_private_execution_of_transfer_program() { env_builder.write(&sender_private_key).unwrap(); env_builder.write(&sender).unwrap(); env_builder.write(&receiver) .unwrap(); - env_builder.write(&balance_to_move).unwrap(); env_builder.write(&sender_post).unwrap(); env_builder.write(&receiver_post).unwrap(); env_builder.write(&commitment_tree_root).unwrap(); diff --git a/risc0-selective-privacy-poc/src/public_execution.rs b/risc0-selective-privacy-poc/src/public_execution.rs index 0b8096c..366eb25 100644 --- a/risc0-selective-privacy-poc/src/public_execution.rs +++ b/risc0-selective-privacy-poc/src/public_execution.rs @@ -2,14 +2,23 @@ use risc0_zkvm::{default_executor, ExecutorEnv}; use toy_example_core::Account; use transfer_methods::TRANSFER_ELF; +/// 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() { - let sender_private_key = [0; 8]; - let mut sender = Account::new_from_private_key(sender_private_key, [1; 8]); - sender.balance = 150; + // Account fetched from the chain state with 150 in its balance. + let sender = { + let mut account = Account::new([5; 8], [98; 8]); + account.balance = 150; + account + }; - let receiver_private_key = [99; 8]; - let mut receiver = Account::new_from_private_key(receiver_private_key, [1; 8]); - receiver.balance = 900; + // Account fetched from the chain state with 900 in its balance. + let receiver = { + let mut account = Account::new([6; 8], [99; 8]); + account.balance = 900; + account + }; let balance_to_move: u128 = 3; 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 a37ede5..ddd7d0d 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 @@ -3,7 +3,7 @@ use toy_example_core::Account; /// A transfer of balance program. -/// To be used both in public and private contexts +/// To be used both in public and private contexts. fn main() { let sender: Account = env::read(); let receiver: Account = env::read();