improve docstrings

This commit is contained in:
Sergio Chouhy 2025-07-20 22:30:31 -03:00
parent 734206bf73
commit 29b56e3a32
3 changed files with 21 additions and 14 deletions

View File

@ -53,10 +53,19 @@ pub fn bytes_to_words(bytes: &[u8; 32]) -> [u32; 8] {
words words
} }
/// Verifies that a program public execution didn't break the chain's rules. /// Ensures that account transitions follow the rules of a well-behaved program.
/// `input_accounts` are the accounts provided as inputs to the program. ///
/// `output_accounts` are the accounts post states after execution of the program /// A well-behaved program is one that:
pub fn post_execution_consistency_checks( /// - does not change account addresses
/// - does not change account nonces
/// - does not change the `program_owner` field
/// - only reduces the balance of accounts it owns
/// - preserves the total token supply across all accounts
///
/// This function does **not** check that the output accounts are the result of correctly
/// executing the program. That must be checked separately, either by re-executing
/// the program with the inputs or by verifying a proof of correct execution.
pub fn check_well_behaved_account_transition(
input_accounts: &[Account], input_accounts: &[Account],
output_accounts: &[Account], output_accounts: &[Account],
program_id: ProgramId, program_id: ProgramId,
@ -71,6 +80,7 @@ pub fn post_execution_consistency_checks(
if account_pre.address != account_post.address { if account_pre.address != account_post.address {
return false; return false;
} }
// Fail if the program modified the nonces of the input accounts // Fail if the program modified the nonces of the input accounts
if account_pre.nonce != account_post.nonce { if account_pre.nonce != account_post.nonce {
return false; return false;

View File

@ -1,4 +1,4 @@
use core::{account::Account, post_execution_consistency_checks, types::Address}; use core::{account::Account, check_well_behaved_account_transition, types::Address};
use crate::mocked_components::sequencer::error::Error; use crate::mocked_components::sequencer::error::Error;
@ -22,7 +22,7 @@ impl MockedSequencer {
nssa::execute_onchain::<P>(&input_accounts, instruction_data).map_err(|_| Error::BadInput)?; nssa::execute_onchain::<P>(&input_accounts, instruction_data).map_err(|_| Error::BadInput)?;
// Assert accounts pre- and post-states preserve chains invariants // Assert accounts pre- and post-states preserve chains invariants
if !post_execution_consistency_checks(&input_accounts, &program_output.accounts_post, P::PROGRAM_ID) { if !check_well_behaved_account_transition(&input_accounts, &program_output.accounts_post, P::PROGRAM_ID) {
return Err(Error::BadInput); return Err(Error::BadInput);
} }

View File

@ -1,5 +1,5 @@
use core::{ use core::{
compute_nullifier, hash, is_in_tree, post_execution_consistency_checks, check_well_behaved_account_transition, compute_nullifier, hash, is_in_tree,
types::{Nonce, PrivacyExecutionOutput, ProgramId, ProgramOutput}, types::{Nonce, PrivacyExecutionOutput, ProgramId, ProgramOutput},
visibility::AccountVisibility, visibility::AccountVisibility,
}; };
@ -7,18 +7,15 @@ use risc0_zkvm::{guest::env, serde::to_vec};
/// Privacy execution logic. /// Privacy execution logic.
/// This is the circuit for proving correct off-chain executions of programs. /// This is the circuit for proving correct off-chain executions of programs.
/// It also verifies that the chain's invariants are not violated. /// It also verifies that the chain's invariants are not violated and the program is well-behaved.
/// ///
/// Inputs: /// Inputs:
/// - Vec<Account>: The output of the inner program. This is assumed to include the accounts pre and /// - ProgramOuptut: The output of the inner program. This is includes the accounts pre and
/// post-states of the execution of the inner program. /// post-states of the execution of the inner program.
///
/// - Vec<AccountVisibility>: A vector indicating which accounts are private and which are public. /// - Vec<AccountVisibility>: A vector indicating which accounts are private and which are public.
///
/// - Vec<Nonce>: The vector of nonces to be used for the output accounts. This is assumed to be /// - Vec<Nonce>: The vector of nonces to be used for the output accounts. This is assumed to be
/// sampled at random by the host program. /// sampled at random by the host program.
/// /// - [u32; 8]: The root of the commitment tree. Commitments of input private accounts will be
/// - [u32; 8]: The root of the commitment tree. Commitments of used private accounts will be
/// checked against this to prove that they belong to the tree. /// checked against this to prove that they belong to the tree.
/// - ProgamId: The ID of the inner program. /// - ProgamId: The ID of the inner program.
/// ///
@ -52,7 +49,7 @@ fn main() {
env::verify(program_id, &to_vec(&inner_program_output).unwrap()).unwrap(); env::verify(program_id, &to_vec(&inner_program_output).unwrap()).unwrap();
// Assert accounts pre- and post-states preserve chains invariants // Assert accounts pre- and post-states preserve chains invariants
assert!(post_execution_consistency_checks( assert!(check_well_behaved_account_transition(
&inner_program_output.accounts_pre, &inner_program_output.accounts_pre,
&inner_program_output.accounts_post, &inner_program_output.accounts_post,
program_id program_id