From 39b8085a3b46f10c6dbe31752b0d56360cd9f8b9 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Sat, 19 Jul 2025 22:05:03 -0300 Subject: [PATCH] remove all warning --- .../examples/mocked_components/client/mod.rs | 4 +- .../client/transfer_deshielded.rs | 2 +- .../client/transfer_private.rs | 2 +- .../client/transfer_public.rs | 2 +- .../client/transfer_shielded.rs | 6 ++- .../mocked_components/sequencer/mod.rs | 5 --- .../sequencer/process_privacy_execution.rs | 14 +++--- .../sequencer/process_public_execution.rs | 11 ++--- .../examples/public_execution.rs | 2 +- .../program_methods/guest/src/bin/outer.rs | 5 +-- .../sparse_merkle_tree/src/lib.rs | 43 ++++++++----------- risc0-selective-privacy-poc/src/error.rs | 15 +++++++ risc0-selective-privacy-poc/src/lib.rs | 26 +++++------ 13 files changed, 71 insertions(+), 66 deletions(-) create mode 100644 risc0-selective-privacy-poc/src/error.rs diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs index 5c2762b..194cab8 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs @@ -32,10 +32,10 @@ impl MockedClient { visibilities: &[AccountVisibility], commitment_tree_root: [u32; 8], sequencer: &mut MockedSequencer, - ) -> Result, ()> { + ) -> Result, nssa::Error> { // Execute and generate proof of the outer program let (receipt, private_outputs) = - nssa::execute_offchain::

(input_accounts, instruction_data, visibilities, commitment_tree_root).unwrap(); + nssa::execute_offchain::

(input_accounts, instruction_data, visibilities, commitment_tree_root)?; // Send proof to the sequencer sequencer.process_privacy_execution(receipt)?; 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 index 89d581f..e22f239 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs @@ -15,7 +15,7 @@ impl MockedClient { to_address: &Address, balance_to_move: u128, sequencer: &mut MockedSequencer, - ) -> Result { + ) -> Result { // Fetch commitment tree root from the sequencer let commitment_tree_root = sequencer.get_commitment_tree_root(); // Compute authenticaton path for the input private 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 index c6f2c6c..d96c29f 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs @@ -15,7 +15,7 @@ impl MockedClient { to_address: &Address, balance_to_move: u128, sequencer: &mut MockedSequencer, - ) -> Result<[Account; 2], ()> { + ) -> Result<[Account; 2], nssa::Error> { // Fetch commitment tree root from the sequencer let commitment_tree_root = sequencer.get_commitment_tree_root(); // Compute authenticaton path for the input private 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 index d2bf365..697ec1c 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_public.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_public.rs @@ -10,7 +10,7 @@ impl MockedClient { to_address: &Address, amount_to_transfer: u128, sequencer: &mut MockedSequencer, - ) -> Result<(), ()> { + ) -> Result<(), nssa::Error> { // Submit a public (on-chain) execution of the Transfer program to the sequencer sequencer.process_public_execution::(&[self.user_address(), *to_address], amount_to_transfer) } 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 index 7a4bec0..ad869a5 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs @@ -14,12 +14,14 @@ impl MockedClient { to_address: &Address, balance_to_move: u128, sequencer: &mut MockedSequencer, - ) -> Result { + ) -> Result { // Fetch commitment tree root from the sequencer let commitment_tree_root = sequencer.get_commitment_tree_root(); // Fetch sender account from the sequencer - let from_account = sequencer.get_account(&self.user_address()).ok_or(())?; + let from_account = sequencer + .get_account(&self.user_address()) + .ok_or(nssa::Error::Generic)?; // Create a new default private account for the receiver let to_account = Self::fresh_account_for_mint(*to_address); diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs index 09501f9..9931d7e 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs @@ -64,11 +64,6 @@ impl MockedSequencer { .try_into() .unwrap() } - - /// Returns the list of all registered addresses - pub fn addresses(&self) -> Vec

{ - self.accounts.keys().cloned().collect() - } } /// Pretty prints the chain's state diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs index d5931be..c4dcdff 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs @@ -7,26 +7,26 @@ use super::MockedSequencer; impl MockedSequencer { /// Processes a privacy execution request. /// Verifies the proof of the privacy execution and updates the state of the chain. - pub fn process_privacy_execution(&mut self, receipt: Receipt) -> Result<(), ()> { + pub fn process_privacy_execution(&mut self, receipt: Receipt) -> Result<(), nssa::Error> { // Parse the output of the "outer" program let output: PrivacyExecutionOutput = receipt.journal.decode().unwrap(); // Reject in case the root used in the privacy execution is not the current root. if output.commitment_tree_root != self.get_commitment_tree_root() { - return Err(()); + return Err(nssa::Error::Generic); } // Reject in case the number of accounts pre states is different from the post states if output.public_accounts_pre.len() != output.public_accounts_post.len() { - return Err(()); + return Err(nssa::Error::Generic); } // Reject if the states of the public input accounts used in the inner execution do not // coincide with the on-chain state. for account in output.public_accounts_pre.iter() { - let current_account = self.get_account(&account.address).ok_or(())?; + let current_account = self.get_account(&account.address).ok_or(nssa::Error::Generic)?; if ¤t_account != account { - return Err(()); + return Err(nssa::Error::Generic); } } @@ -36,7 +36,7 @@ impl MockedSequencer { .iter() .any(|nullifier| self.nullifier_set.contains(nullifier)) { - return Err(()); + return Err(nssa::Error::Generic); } // Reject if the commitments have already been seen. @@ -45,7 +45,7 @@ impl MockedSequencer { .iter() .any(|commitment| self.commitment_tree.values().contains(commitment)) { - return Err(()); + return Err(nssa::Error::Generic); } // Verify the proof of the privacy execution. diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_public_execution.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_public_execution.rs index dea7fc3..54e615e 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_public_execution.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_public_execution.rs @@ -1,7 +1,4 @@ -use core::{ - account::Account, - types::{Address, ProgramOutput}, -}; +use core::{account::Account, types::Address}; use super::MockedSequencer; @@ -11,11 +8,11 @@ impl MockedSequencer { &mut self, input_account_addresses: &[Address], instruction_data: P::InstructionData, - ) -> Result<(), ()> { + ) -> Result<(), nssa::Error> { // Fetch the current state of the input accounts. let input_accounts: Vec = input_account_addresses .iter() - .map(|address| self.get_account(address).ok_or(())) + .map(|address| self.get_account(address).ok_or(nssa::Error::Generic)) .collect::>()?; // Execute the program @@ -23,7 +20,7 @@ impl MockedSequencer { // Perform consistency checks if !self.program_output_is_valid(&input_accounts, &program_output.accounts_post) { - return Err(()); + return Err(nssa::Error::Generic); } // Update the accounts states diff --git a/risc0-selective-privacy-poc/examples/public_execution.rs b/risc0-selective-privacy-poc/examples/public_execution.rs index c9325ae..f1f59e6 100644 --- a/risc0-selective-privacy-poc/examples/public_execution.rs +++ b/risc0-selective-privacy-poc/examples/public_execution.rs @@ -17,7 +17,7 @@ pub fn main() { let balance_to_move = vec![10, 20]; - let inputs_outputs = + let _inputs_outputs = nssa::execute_onchain::(&[sender, receiver_1, receiver_2], balance_to_move).unwrap(); println!("OK!"); diff --git a/risc0-selective-privacy-poc/program_methods/guest/src/bin/outer.rs b/risc0-selective-privacy-poc/program_methods/guest/src/bin/outer.rs index d9c5e87..b415f29 100644 --- a/risc0-selective-privacy-poc/program_methods/guest/src/bin/outer.rs +++ b/risc0-selective-privacy-poc/program_methods/guest/src/bin/outer.rs @@ -1,5 +1,4 @@ use core::{ - account::Account, compute_nullifier, hash, is_in_tree, types::{Nonce, PrivacyExecutionOutput, ProgramId, ProgramOutput}, visibility::AccountVisibility, @@ -29,8 +28,8 @@ use risc0_zkvm::{guest::env, serde::to_vec}; /// - The commitments for the ouput private accounts. /// - The commitment tree root used for the authentication path verifications. fn main() { - // Read inputs and outputs - let mut inner_program_output: ProgramOutput = env::read(); + // Read inner program output + let inner_program_output: ProgramOutput = env::read(); let num_inputs = inner_program_output.accounts_pre.len(); assert_eq!(inner_program_output.accounts_post.len(), num_inputs); 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 3822116..a4f5528 100644 --- a/risc0-selective-privacy-poc/sparse_merkle_tree/src/lib.rs +++ b/risc0-selective-privacy-poc/sparse_merkle_tree/src/lib.rs @@ -5,13 +5,9 @@ use sha2::{Digest, Sha256}; use std::collections::{HashMap, HashSet}; 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, - 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29, -]; const ONE_HASH: [u8; 32] = [ - 75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227, 209, 96, 10, 214, 49, - 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154, + 75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227, 209, 96, 10, 214, 49, 195, 133, 165, 215, + 204, 226, 60, 119, 133, 69, 154, ]; /// Compute parent as the hash of two child nodes @@ -65,12 +61,8 @@ impl SparseMerkleTree { let left_index = parent_index << 1; let right_index = left_index | 1; - let left = nodes - .get(&(depth + 1, left_index)) - .unwrap_or(&DEFAULT_HASHES[depth]); - let right = nodes - .get(&(depth + 1, right_index)) - .unwrap_or(&DEFAULT_HASHES[depth]); + let left = nodes.get(&(depth + 1, left_index)).unwrap_or(&DEFAULT_HASHES[depth]); + let right = nodes.get(&(depth + 1, right_index)).unwrap_or(&DEFAULT_HASHES[depth]); if left != &DEFAULT_HASHES[depth] || right != &DEFAULT_HASHES[depth] { let h = hash_node(left, right); @@ -84,10 +76,7 @@ impl SparseMerkleTree { } pub fn root(&self) -> [u8; 32] { - self.node_map - .get(&(0, 0)) - .cloned() - .unwrap_or(DEFAULT_HASHES[0]) + self.node_map.get(&(0, 0)).cloned().unwrap_or(DEFAULT_HASHES[0]) } pub fn get_authentication_path_for_value(&self, value: u32) -> [[u8; 32]; 32] { @@ -133,14 +122,20 @@ impl SparseMerkleTree { #[cfg(test)] mod tests { use super::*; + + 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, 179, 55, 56, 118, + 133, 17, 163, 6, 23, 175, 160, 29, + ]; + #[test] fn test_default_hashes() { assert_eq!(DEFAULT_HASHES[TREE_DEPTH - 1], ZERO_HASH); assert_eq!( DEFAULT_HASHES[0], [ - 157, 148, 193, 146, 141, 23, 128, 25, 196, 90, 21, 193, 179, 235, 209, 157, 146, - 64, 171, 100, 192, 44, 121, 46, 78, 53, 190, 198, 191, 82, 85, 16 + 157, 148, 193, 146, 141, 23, 128, 25, 196, 90, 21, 193, 179, 235, 209, 157, 146, 64, 171, 100, 192, 44, + 121, 46, 78, 53, 190, 198, 191, 82, 85, 16 ] ); } @@ -158,8 +153,8 @@ mod tests { assert_eq!( tree.root(), [ - 109, 94, 224, 93, 195, 77, 137, 36, 108, 105, 177, 22, 212, 17, 160, 255, 224, 61, - 191, 17, 129, 10, 26, 76, 197, 42, 230, 160, 80, 44, 101, 184 + 109, 94, 224, 93, 195, 77, 137, 36, 108, 105, 177, 22, 212, 17, 160, 255, 224, 61, 191, 17, 129, 10, + 26, 76, 197, 42, 230, 160, 80, 44, 101, 184 ] ); } @@ -174,8 +169,8 @@ mod tests { assert_eq!( tree.root(), [ - 36, 178, 159, 245, 165, 76, 242, 85, 25, 218, 149, 135, 194, 127, 130, 201, 219, - 187, 167, 216, 1, 222, 234, 197, 152, 156, 243, 174, 68, 27, 114, 8 + 36, 178, 159, 245, 165, 76, 242, 85, 25, 218, 149, 135, 194, 127, 130, 201, 219, 187, 167, 216, 1, 222, + 234, 197, 152, 156, 243, 174, 68, 27, 114, 8 ] ); } @@ -188,8 +183,8 @@ mod tests { assert_eq!( tree.root(), [ - 148, 76, 190, 191, 248, 243, 89, 40, 197, 157, 206, 23, 58, 197, 86, 169, 225, 217, - 110, 166, 54, 10, 245, 175, 168, 4, 145, 220, 30, 210, 67, 113 + 148, 76, 190, 191, 248, 243, 89, 40, 197, 157, 206, 23, 58, 197, 86, 169, 225, 217, 110, 166, 54, 10, + 245, 175, 168, 4, 145, 220, 30, 210, 67, 113 ] ); } diff --git a/risc0-selective-privacy-poc/src/error.rs b/risc0-selective-privacy-poc/src/error.rs new file mode 100644 index 0000000..f1d7fa5 --- /dev/null +++ b/risc0-selective-privacy-poc/src/error.rs @@ -0,0 +1,15 @@ +#[derive(Debug)] +pub enum Error { + /// For simplicity, this POC uses a generic error + Generic, +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Generic => write!(f, "An unexpected error occurred"), + } + } +} + +impl std::error::Error for Error {} diff --git a/risc0-selective-privacy-poc/src/lib.rs b/risc0-selective-privacy-poc/src/lib.rs index 03d8869..f635798 100644 --- a/risc0-selective-privacy-poc/src/lib.rs +++ b/risc0-selective-privacy-poc/src/lib.rs @@ -7,7 +7,9 @@ use program_methods::{OUTER_ELF, OUTER_ID}; use rand::{rngs::OsRng, Rng}; use risc0_zkvm::{default_executor, default_prover, ExecutorEnv, ExecutorEnvBuilder, Receipt}; +pub mod error; pub mod program; +pub use error::Error; pub use program::Program; @@ -21,10 +23,10 @@ fn write_inputs( input_accounts: &[Account], instruction_data: P::InstructionData, env_builder: &mut ExecutorEnvBuilder, -) -> Result<(), ()> { +) -> Result<(), Error> { let input_accounts = input_accounts.to_vec(); - env_builder.write(&input_accounts).map_err(|_| ())?; - env_builder.write(&instruction_data).map_err(|_| ())?; + env_builder.write(&input_accounts).map_err(|_| Error::Generic)?; + env_builder.write(&instruction_data).map_err(|_| Error::Generic)?; Ok(()) } @@ -33,7 +35,7 @@ fn write_inputs( fn execute_and_prove_inner( input_accounts: &[Account], instruction_data: P::InstructionData, -) -> Result { +) -> Result { // Write inputs to the program let mut env_builder = ExecutorEnv::builder(); write_inputs::

(input_accounts, instruction_data, &mut env_builder)?; @@ -41,7 +43,7 @@ fn execute_and_prove_inner( // Prove the program let prover = default_prover(); - let prove_info = prover.prove(env, P::PROGRAM_ELF).map_err(|_| ())?; + let prove_info = prover.prove(env, P::PROGRAM_ELF).map_err(|_| Error::Generic)?; Ok(prove_info.receipt) } @@ -71,7 +73,7 @@ fn build_private_outputs_from_inner_results( pub fn execute_onchain( input_accounts: &[Account], instruction_data: P::InstructionData, -) -> Result { +) -> Result { // Write inputs to the program let mut env_builder = ExecutorEnv::builder(); write_inputs::

(input_accounts, instruction_data, &mut env_builder)?; @@ -79,10 +81,10 @@ pub fn execute_onchain( // Execute the program (without proving) let executor = default_executor(); - let session_info = executor.execute(env, P::PROGRAM_ELF).map_err(|_| ())?; + let session_info = executor.execute(env, P::PROGRAM_ELF).map_err(|_| Error::Generic)?; // Get (inputs and) outputs - session_info.journal.decode().map_err(|_| ()) + session_info.journal.decode().map_err(|_| Error::Generic) } /// Executes and proves the inner program `P` and executes and proves the outer program on top of it. @@ -93,10 +95,10 @@ pub fn execute_offchain( instruction_data: P::InstructionData, visibilities: &[AccountVisibility], commitment_tree_root: [u32; 8], -) -> Result<(Receipt, Vec), ()> { +) -> Result<(Receipt, Vec), Error> { // Prove inner program and get post state of the accounts let inner_receipt = execute_and_prove_inner::

(inputs, instruction_data)?; - let inner_program_output: ProgramOutput = inner_receipt.journal.decode().map_err(|_| ())?; + let inner_program_output: ProgramOutput = inner_receipt.journal.decode().map_err(|_| Error::Generic)?; // Sample fresh random nonces for the outputs of this execution let output_nonces: Vec<_> = (0..inputs.len()).map(|_| new_random_nonce()).collect(); @@ -120,6 +122,6 @@ pub fn execute_offchain( } /// Verifies a proof of the outer program for the given parameters. -pub fn verify_privacy_execution(receipt: Receipt) -> Result<(), ()> { - receipt.verify(OUTER_ID).map_err(|_| ()) +pub fn verify_privacy_execution(receipt: Receipt) -> Result<(), Error> { + receipt.verify(OUTER_ID).map_err(|_| Error::Generic) }