add pre states to program output

This commit is contained in:
Sergio Chouhy 2025-08-14 14:09:04 -03:00
parent 035f950229
commit c5a4e83e3e
11 changed files with 59 additions and 44 deletions

View File

@ -1,17 +1,33 @@
use crate::account::{Account, AccountWithMetadata}; use crate::account::{Account, AccountWithMetadata};
use risc0_zkvm::serde::Deserializer; use risc0_zkvm::serde::Deserializer;
use risc0_zkvm::{DeserializeOwned, guest::env}; use risc0_zkvm::{DeserializeOwned, guest::env};
use serde::{Deserialize, Serialize};
pub type ProgramId = [u32; 8]; pub type ProgramId = [u32; 8];
pub type InstructionData = Vec<u32>; pub type InstructionData = Vec<u32>;
pub const DEFAULT_PROGRAM_ID: ProgramId = [0; 8]; pub const DEFAULT_PROGRAM_ID: ProgramId = [0; 8];
#[derive(Serialize, Deserialize)]
pub struct ProgramOutput {
pub pre_states: Vec<AccountWithMetadata>,
pub post_states: Vec<Account>,
}
pub fn read_nssa_inputs<T: DeserializeOwned>() -> (Vec<AccountWithMetadata>, T) { pub fn read_nssa_inputs<T: DeserializeOwned>() -> (Vec<AccountWithMetadata>, T) {
let pre_states: Vec<AccountWithMetadata> = env::read(); let pre_states: Vec<AccountWithMetadata> = env::read();
let words: InstructionData = env::read(); let words: InstructionData = env::read();
let instruction_data = T::deserialize(&mut Deserializer::new(words.as_ref())).unwrap(); let instruction_data = T::deserialize(&mut Deserializer::new(words.as_ref())).unwrap();
(pre_states, instruction_data) (pre_states, instruction_data)
} }
pub fn write_nssa_outputs(pre_states: Vec<AccountWithMetadata>, post_states: Vec<Account>) {
let output = ProgramOutput {
pre_states,
post_states,
};
env::commit(&output);
}
/// Validates well-behaved program execution /// Validates well-behaved program execution
/// ///
/// # Parameters /// # Parameters

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = u128; type Instruction = u128;
@ -32,5 +31,5 @@ fn main() {
sender_post.balance -= balance_to_move; sender_post.balance -= balance_to_move;
receiver_post.balance += balance_to_move; receiver_post.balance += balance_to_move;
env::commit(&vec![sender_post, receiver_post]); write_nssa_outputs(vec![sender, receiver], vec![sender_post, receiver_post]);
} }

View File

@ -1,6 +1,6 @@
use nssa_core::{ use nssa_core::{
account::{Account, AccountWithMetadata}, account::{Account, AccountWithMetadata},
program::{DEFAULT_PROGRAM_ID, InstructionData, ProgramId}, program::{DEFAULT_PROGRAM_ID, InstructionData, ProgramId, ProgramOutput},
}; };
use program_methods::{AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID}; use program_methods::{AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID};
use risc0_zkvm::{ use risc0_zkvm::{
@ -44,7 +44,10 @@ impl Program {
.map_err(|e| NssaError::ProgramExecutionFailed(e.to_string()))?; .map_err(|e| NssaError::ProgramExecutionFailed(e.to_string()))?;
// Get outputs // Get outputs
let mut post_states: Vec<Account> = session_info let ProgramOutput {
post_states: mut post_states,
..
} = session_info
.journal .journal
.decode() .decode()
.map_err(|e| NssaError::ProgramExecutionFailed(e.to_string()))?; .map_err(|e| NssaError::ProgramExecutionFailed(e.to_string()))?;
@ -107,7 +110,10 @@ impl Program {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use nssa_core::account::{Account, AccountWithMetadata}; use nssa_core::{
account::{Account, AccountWithMetadata},
program::ProgramOutput,
};
use crate::program::Program; use crate::program::Program;
@ -256,15 +262,12 @@ mod tests {
balance: balance_to_move, balance: balance_to_move,
..Account::default() ..Account::default()
}; };
let receipt = program let receipt = program
.execute_and_prove(&[sender, recipient], &instruction_data) .execute_and_prove(&[sender, recipient], &instruction_data)
.unwrap(); .unwrap();
let [sender_post, recipient_post] = receipt let ProgramOutput { post_states, .. } = receipt.journal.decode().unwrap();
.journal let [sender_post, recipient_post] = post_states.try_into().unwrap();
.decode::<Vec<Account>>()
.unwrap()
.try_into()
.unwrap();
let output = assert_eq!(sender_post, expected_sender_post); let output = assert_eq!(sender_post, expected_sender_post);

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = u128; type Instruction = u128;
@ -11,9 +10,9 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre = pre.account; let account_pre = &pre.account;
let mut account_post = account_pre.clone(); let mut account_post = account_pre.clone();
account_post.balance -= balance_to_burn; account_post.balance -= balance_to_burn;
env::commit(&vec![account_post]); write_nssa_outputs(vec![pre], vec![account_post]);
} }

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = (); type Instruction = ();
@ -11,10 +10,9 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre = pre.account; let account_pre = &pre.account;
let mut account_post = account_pre.clone(); let mut account_post = account_pre.clone();
account_post.data.push(0); account_post.data.push(0);
env::commit(&vec![account_post]); write_nssa_outputs(vec![pre], vec![account_post]);
} }

View File

@ -1,5 +1,7 @@
use nssa_core::{account::Account, program::read_nssa_inputs}; use nssa_core::{
use risc0_zkvm::guest::env; account::Account,
program::{read_nssa_inputs, write_nssa_outputs},
};
type Instruction = (); type Instruction = ();
@ -11,7 +13,7 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre = pre.account; let account_pre = pre.account.clone();
env::commit(&vec![account_pre, Account::default()]); write_nssa_outputs(vec![pre], vec![account_pre, Account::default()]);
} }

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = (); type Instruction = ();
@ -11,9 +10,9 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre = pre.account; let account_pre = &pre.account;
let mut account_post = account_pre.clone(); let mut account_post = account_pre.clone();
account_post.balance += 1; account_post.balance += 1;
env::commit(&vec![account_post]); write_nssa_outputs(vec![pre], vec![account_post]);
} }

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = (); type Instruction = ();
@ -11,7 +10,7 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre1 = pre1.account; let account_pre1 = pre1.account.clone();
env::commit(&vec![account_pre1]); write_nssa_outputs(vec![pre1], vec![account_pre1]);
} }

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = (); type Instruction = ();
@ -11,9 +10,9 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre = pre.account; let account_pre = &pre.account;
let mut account_post = account_pre.clone(); let mut account_post = account_pre.clone();
account_post.nonce += 1; account_post.nonce += 1;
env::commit(&vec![account_post]); write_nssa_outputs(vec![pre], vec![account_post]);
} }

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = (); type Instruction = ();
@ -11,9 +10,9 @@ fn main() {
Err(_) => return, Err(_) => return,
}; };
let account_pre = pre.account; let account_pre = &pre.account;
let mut account_post = account_pre.clone(); let mut account_post = account_pre.clone();
account_post.program_owner = [0, 1, 2, 3, 4, 5, 6, 7]; account_post.program_owner = [0, 1, 2, 3, 4, 5, 6, 7];
env::commit(&vec![account_post]); write_nssa_outputs(vec![pre], vec![account_post]);
} }

View File

@ -1,5 +1,4 @@
use nssa_core::program::read_nssa_inputs; use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use risc0_zkvm::guest::env;
type Instruction = u128; type Instruction = u128;
@ -16,5 +15,8 @@ fn main() {
sender_post.balance -= balance; sender_post.balance -= balance;
receiver_post.balance += balance; receiver_post.balance += balance;
env::commit(&vec![sender_post, receiver_post]); write_nssa_outputs(
vec![sender_pre, receiver_pre],
vec![sender_post, receiver_post],
);
} }