diff --git a/risc0-selective-privacy-poc/examples/happy_path.rs b/risc0-selective-privacy-poc/examples/happy_path.rs index 194ac8b..2ea921d 100644 --- a/risc0-selective-privacy-poc/examples/happy_path.rs +++ b/risc0-selective-privacy-poc/examples/happy_path.rs @@ -24,19 +24,19 @@ fn main() { let sender_addr = addresses[1]; let receiver_addr = addresses[2]; sequencer - .invoke_public::(&[sender_addr, receiver_addr], 10) + .invoke_public_execution::(&[sender_addr, receiver_addr], 10) .unwrap(); println!("2️⃣ 🚀 Balances after transfer"); sequencer.print(); // A shielded execution of the Transfer Program let private_account_2 = - MockedClient::send_shielded(&addresses[1], &addresses[2], 15, &mut sequencer); + MockedClient::transfer_shielded(&addresses[1], &addresses[2], 15, &mut sequencer); println!("Balances after shielded execution"); sequencer.print(); // A private execution of the Transfer Program - let private_account_1 = MockedClient::send_private( + let private_account_1 = MockedClient::transfer_private( &private_account_2, &ACCOUNTS_PRIVATE_KEYS[1], // <-- this is shifted 🫠 &addresses[3], @@ -47,7 +47,7 @@ fn main() { sequencer.print(); // A deshielded execution of the Transfer Program - MockedClient::send_deshielded( + MockedClient::transfer_deshielded( &private_account_1, &ACCOUNTS_PRIVATE_KEYS[0], &addresses[0], @@ -60,7 +60,7 @@ fn main() { // A public execution of the Pinata program let preimage = bytes_to_words(b"NSSA Selective privacy is great!").to_vec(); sequencer - .invoke_public::(&[addresses[0], addresses[3]], preimage) + .invoke_public_execution::(&[addresses[0], addresses[3]], preimage) .unwrap(); println!("5️⃣ 🚀 Balances after public piñata execution"); sequencer.print(); diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client.rs b/risc0-selective-privacy-poc/examples/mocked_components/client.rs deleted file mode 100644 index 6389479..0000000 --- a/risc0-selective-privacy-poc/examples/mocked_components/client.rs +++ /dev/null @@ -1,125 +0,0 @@ -use core::{ - account::Account, - input::InputVisibiility, - types::{Address, Commitment, Key, Nullifier}, -}; - -use nssa::program::TransferProgram; - -use crate::mocked_components::sequencer::MockedSequencer; - -pub struct MockedClient; - -impl MockedClient { - pub fn fresh_account_for_mint(address: Address) -> Account { - let nonce = [0; 8]; - Account::new(address, nonce) - } - - /// A shielded execution of the Transfer program - pub fn send_shielded( - from_address: &Address, - to_address: &Address, - balance_to_move: u128, - sequencer: &mut MockedSequencer, - ) -> Account { - // All of this is executed locally by the sender - let sender_account = sequencer.get_account(&from_address).unwrap(); - let commitment_tree_root = sequencer.get_commitment_tree_root(); - let receiver_addr = to_address; - let mut receiver_account = Self::fresh_account_for_mint(*receiver_addr); - let visibilities = vec![InputVisibiility::Public, InputVisibiility::Private(None)]; - let (receipt, nonces) = nssa::invoke_privacy_execution::( - &[sender_account, receiver_account.clone()], - balance_to_move, - &visibilities, - commitment_tree_root, - ) - .unwrap(); - - // Assemble the private account - receiver_account.nonce = nonces[1]; - receiver_account.balance = balance_to_move; - let output: (Vec, Vec, Vec, [u32; 8]) = - receipt.journal.decode().unwrap(); - - // Send to te sequencer - sequencer - .invoke_privacy_execution(receipt, &output.0, &output.1, &output.2) - .unwrap(); - receiver_account - } - - /// A private execution of the Transfer program - pub fn send_private( - from_account: &Account, - from_account_pk: &Key, - to_address: &Address, - balance_to_move: u128, - sequencer: &mut MockedSequencer, - ) -> Account { - // All of this is executed locally by the sender - let commitment_tree_root = sequencer.get_commitment_tree_root(); - let receiver_addr = to_address; - let sender_commitment_auth_path = - sequencer.get_authentication_path_for(&from_account.commitment()); - let mut receiver_account = Self::fresh_account_for_mint(*receiver_addr); - let visibilities = vec![ - InputVisibiility::Private(Some((from_account_pk.clone(), sender_commitment_auth_path))), - InputVisibiility::Private(None), - ]; - let (receipt, nonces) = nssa::invoke_privacy_execution::( - &[from_account.clone(), receiver_account.clone()], - balance_to_move, - &visibilities, - commitment_tree_root, - ) - .unwrap(); - - // Assemble the private account - receiver_account.nonce = nonces[1]; - receiver_account.balance = balance_to_move; - let output: (Vec, Vec, Vec, [u32; 8]) = - receipt.journal.decode().unwrap(); - - // Send to te sequencer - sequencer - .invoke_privacy_execution(receipt, &output.0, &output.1, &output.2) - .unwrap(); - receiver_account - } - - pub fn send_deshielded( - from_account: &Account, - from_account_pk: &Key, - to_address: &Address, - balance_to_move: u128, - sequencer: &mut MockedSequencer, - ) { - // All of this is executed locally by the sender - let commitment_tree_root = sequencer.get_commitment_tree_root(); - let receiver_addr = to_address; - let sender_commitment_auth_path = - sequencer.get_authentication_path_for(&from_account.commitment()); - let to_account = sequencer.get_account(&to_address).unwrap(); - let visibilities = vec![ - InputVisibiility::Private(Some((from_account_pk.clone(), sender_commitment_auth_path))), - InputVisibiility::Public, - ]; - let (receipt, nonces) = nssa::invoke_privacy_execution::( - &[from_account.clone(), to_account], - balance_to_move, - &visibilities, - commitment_tree_root, - ) - .unwrap(); - - let output: (Vec, Vec, Vec, [u32; 8]) = - receipt.journal.decode().unwrap(); - - // Send to te sequencer - sequencer - .invoke_privacy_execution(receipt, &output.0, &output.1, &output.2) - .unwrap(); - } -} diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs new file mode 100644 index 0000000..04bea16 --- /dev/null +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs @@ -0,0 +1,23 @@ +use core::{ + account::Account, + input::InputVisibiility, + types::{Address, Commitment, Key, Nullifier}, +}; + +use nssa::program::TransferProgram; + +use crate::mocked_components::sequencer::MockedSequencer; + +pub mod transfer_deshielded; +pub mod transfer_private; +pub mod transfer_public; +pub mod transfer_shielded; + +pub struct MockedClient; + +impl MockedClient { + pub fn fresh_account_for_mint(address: Address) -> Account { + let nonce = [0; 8]; + Account::new(address, nonce) + } +} diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs new file mode 100644 index 0000000..c89d4ad --- /dev/null +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs @@ -0,0 +1,43 @@ +use core::account::Account; +use core::input::InputVisibiility; +use core::types::{Address, Commitment, Key, Nullifier}; + +use nssa::program::TransferProgram; + +use super::{MockedSequencer, MockedClient}; + +impl MockedClient { + /// A shielded execution of the Transfer program + pub fn transfer_shielded( + from_address: &Address, + to_address: &Address, + balance_to_move: u128, + sequencer: &mut MockedSequencer, + ) -> Account { + // All of this is executed locally by the sender + let sender_account = sequencer.get_account(&from_address).unwrap(); + let commitment_tree_root = sequencer.get_commitment_tree_root(); + let receiver_addr = to_address; + let mut receiver_account = Self::fresh_account_for_mint(*receiver_addr); + let visibilities = vec![InputVisibiility::Public, InputVisibiility::Private(None)]; + let (receipt, nonces) = nssa::invoke_privacy_execution::( + &[sender_account, receiver_account.clone()], + balance_to_move, + &visibilities, + commitment_tree_root, + ) + .unwrap(); + + // Assemble the private account + receiver_account.nonce = nonces[1]; + receiver_account.balance = balance_to_move; + let output: (Vec, Vec, Vec, [u32; 8]) = + receipt.journal.decode().unwrap(); + + // Send to te sequencer + sequencer + .invoke_privacy_execution(receipt, &output.0, &output.1, &output.2) + .unwrap(); + receiver_account + } +} diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs new file mode 100644 index 0000000..6bf6a6f --- /dev/null +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs @@ -0,0 +1,48 @@ +use core::account::Account; +use core::input::InputVisibiility; +use core::types::{Address, Commitment, Key, Nullifier}; + +use nssa::program::TransferProgram; + +use super::{MockedClient, MockedSequencer}; + +impl MockedClient { + /// A private execution of the Transfer program + pub fn transfer_private( + from_account: &Account, + from_account_pk: &Key, + to_address: &Address, + balance_to_move: u128, + sequencer: &mut MockedSequencer, + ) -> Account { + // All of this is executed locally by the sender + let commitment_tree_root = sequencer.get_commitment_tree_root(); + let receiver_addr = to_address; + let sender_commitment_auth_path = + sequencer.get_authentication_path_for(&from_account.commitment()); + let mut receiver_account = Self::fresh_account_for_mint(*receiver_addr); + let visibilities = vec![ + InputVisibiility::Private(Some((from_account_pk.clone(), sender_commitment_auth_path))), + InputVisibiility::Private(None), + ]; + let (receipt, nonces) = nssa::invoke_privacy_execution::( + &[from_account.clone(), receiver_account.clone()], + balance_to_move, + &visibilities, + commitment_tree_root, + ) + .unwrap(); + let output: (Vec, Vec, Vec, [u32; 8]) = + receipt.journal.decode().unwrap(); + + // Send to te sequencer + sequencer + .invoke_privacy_execution(receipt, &output.0, &output.1, &output.2) + .unwrap(); + + // Assemble the private account + receiver_account.nonce = nonces[1]; + receiver_account.balance = balance_to_move; + receiver_account + } +} diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_public.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_public.rs new file mode 100644 index 0000000..e69de29 diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs new file mode 100644 index 0000000..03b61ce --- /dev/null +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs @@ -0,0 +1,43 @@ +use core::account::Account; +use core::input::InputVisibiility; +use core::types::{Address, Commitment, Key, Nullifier}; + +use nssa::program::TransferProgram; + +use super::{MockedClient, MockedSequencer}; + +impl MockedClient { + pub fn transfer_deshielded( + from_account: &Account, + from_account_pk: &Key, + to_address: &Address, + balance_to_move: u128, + sequencer: &mut MockedSequencer, + ) { + // All of this is executed locally by the sender + let commitment_tree_root = sequencer.get_commitment_tree_root(); + let receiver_addr = to_address; + let sender_commitment_auth_path = + sequencer.get_authentication_path_for(&from_account.commitment()); + let to_account = sequencer.get_account(&to_address).unwrap(); + let visibilities = vec![ + InputVisibiility::Private(Some((from_account_pk.clone(), sender_commitment_auth_path))), + InputVisibiility::Public, + ]; + let (receipt, nonces) = nssa::invoke_privacy_execution::( + &[from_account.clone(), to_account], + balance_to_move, + &visibilities, + commitment_tree_root, + ) + .unwrap(); + + let output: (Vec, Vec, Vec, [u32; 8]) = + receipt.journal.decode().unwrap(); + + // Send to te sequencer + sequencer + .invoke_privacy_execution(receipt, &output.0, &output.1, &output.2) + .unwrap(); + } +} diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/invoke_public_execution.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/invoke_public_execution.rs index c06adba..0e6a46f 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/invoke_public_execution.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/invoke_public_execution.rs @@ -3,7 +3,7 @@ use core::{account::Account, types::Address}; use super::MockedSequencer; impl MockedSequencer { - pub fn invoke_public( + pub fn invoke_public_execution( &mut self, input_account_addresses: &[Address], instruction_data: P::InstructionData,