remove all warning

This commit is contained in:
Sergio Chouhy 2025-07-19 22:05:03 -03:00
parent 452154576f
commit 39b8085a3b
13 changed files with 71 additions and 66 deletions

View File

@ -32,10 +32,10 @@ impl MockedClient {
visibilities: &[AccountVisibility],
commitment_tree_root: [u32; 8],
sequencer: &mut MockedSequencer,
) -> Result<Vec<Account>, ()> {
) -> Result<Vec<Account>, nssa::Error> {
// Execute and generate proof of the outer program
let (receipt, private_outputs) =
nssa::execute_offchain::<P>(input_accounts, instruction_data, visibilities, commitment_tree_root).unwrap();
nssa::execute_offchain::<P>(input_accounts, instruction_data, visibilities, commitment_tree_root)?;
// Send proof to the sequencer
sequencer.process_privacy_execution(receipt)?;

View File

@ -15,7 +15,7 @@ impl MockedClient {
to_address: &Address,
balance_to_move: u128,
sequencer: &mut MockedSequencer,
) -> Result<Account, ()> {
) -> Result<Account, 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

View File

@ -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

View File

@ -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::<TransferProgram>(&[self.user_address(), *to_address], amount_to_transfer)
}

View File

@ -14,12 +14,14 @@ impl MockedClient {
to_address: &Address,
balance_to_move: u128,
sequencer: &mut MockedSequencer,
) -> Result<Account, ()> {
) -> Result<Account, nssa::Error> {
// 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);

View File

@ -64,11 +64,6 @@ impl MockedSequencer {
.try_into()
.unwrap()
}
/// Returns the list of all registered addresses
pub fn addresses(&self) -> Vec<Address> {
self.accounts.keys().cloned().collect()
}
}
/// Pretty prints the chain's state

View File

@ -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 &current_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.

View File

@ -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<Account> = input_account_addresses
.iter()
.map(|address| self.get_account(address).ok_or(()))
.map(|address| self.get_account(address).ok_or(nssa::Error::Generic))
.collect::<Result<_, _>>()?;
// 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

View File

@ -17,7 +17,7 @@ pub fn main() {
let balance_to_move = vec![10, 20];
let inputs_outputs =
let _inputs_outputs =
nssa::execute_onchain::<TransferMultipleProgram>(&[sender, receiver_1, receiver_2], balance_to_move).unwrap();
println!("OK!");

View File

@ -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);

View File

@ -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
]
);
}

View File

@ -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 {}

View File

@ -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<P: Program>(
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<P: Program>(
fn execute_and_prove_inner<P: Program>(
input_accounts: &[Account],
instruction_data: P::InstructionData,
) -> Result<Receipt, ()> {
) -> Result<Receipt, Error> {
// Write inputs to the program
let mut env_builder = ExecutorEnv::builder();
write_inputs::<P>(input_accounts, instruction_data, &mut env_builder)?;
@ -41,7 +43,7 @@ fn execute_and_prove_inner<P: Program>(
// 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<P: Program>(
input_accounts: &[Account],
instruction_data: P::InstructionData,
) -> Result<ProgramOutput, ()> {
) -> Result<ProgramOutput, Error> {
// Write inputs to the program
let mut env_builder = ExecutorEnv::builder();
write_inputs::<P>(input_accounts, instruction_data, &mut env_builder)?;
@ -79,10 +81,10 @@ pub fn execute_onchain<P: Program>(
// 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<P: Program>(
instruction_data: P::InstructionData,
visibilities: &[AccountVisibility],
commitment_tree_root: [u32; 8],
) -> Result<(Receipt, Vec<Account>), ()> {
) -> Result<(Receipt, Vec<Account>), Error> {
// Prove inner program and get post state of the accounts
let inner_receipt = execute_and_prove_inner::<P>(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<P: Program>(
}
/// 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)
}