lssa/nssa/core/src/program.rs

91 lines
3.0 KiB
Rust
Raw Normal View History

2025-09-08 19:29:56 -03:00
use crate::account::{Account, AccountWithMetadata, FingerPrint};
2025-08-10 18:51:55 -03:00
use risc0_zkvm::serde::Deserializer;
use risc0_zkvm::{DeserializeOwned, guest::env};
2025-08-14 14:09:04 -03:00
use serde::{Deserialize, Serialize};
2025-08-06 20:05:04 -03:00
pub type ProgramId = [u32; 8];
2025-08-10 18:51:55 -03:00
pub type InstructionData = Vec<u32>;
pub const DEFAULT_PROGRAM_ID: ProgramId = [0; 8];
2025-08-06 20:05:04 -03:00
2025-08-14 14:30:04 -03:00
pub struct ProgramInput<T> {
pub pre_states: Vec<AccountWithMetadata>,
pub instruction: T,
}
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
2025-08-14 14:09:04 -03:00
pub struct ProgramOutput {
pub pre_states: Vec<AccountWithMetadata>,
pub post_states: Vec<Account>,
}
2025-08-14 14:30:04 -03:00
pub fn read_nssa_inputs<T: DeserializeOwned>() -> ProgramInput<T> {
2025-08-10 18:51:55 -03:00
let pre_states: Vec<AccountWithMetadata> = env::read();
2025-09-08 19:29:56 -03:00
let instruction_words: InstructionData = env::read();
let authorized_fingerprints: Vec<FingerPrint> = env::read();
let instruction = T::deserialize(&mut Deserializer::new(instruction_words.as_ref())).unwrap();
2025-08-14 14:30:04 -03:00
ProgramInput {
pre_states,
instruction,
}
2025-08-10 18:51:55 -03:00
}
2025-08-14 14:09:04 -03:00
pub fn write_nssa_outputs(pre_states: Vec<AccountWithMetadata>, post_states: Vec<Account>) {
let output = ProgramOutput {
pre_states,
post_states,
};
env::commit(&output);
}
2025-08-06 20:05:04 -03:00
/// Validates well-behaved program execution
///
/// # Parameters
/// - `pre_states`: The list of input accounts, each annotated with authorization metadata.
/// - `post_states`: The list of resulting accounts after executing the program logic.
/// - `executing_program_id`: The identifier of the program that was executed.
2025-08-10 09:57:10 -03:00
pub fn validate_execution(
2025-08-06 20:05:04 -03:00
pre_states: &[AccountWithMetadata],
post_states: &[Account],
executing_program_id: ProgramId,
2025-08-09 19:49:07 -03:00
) -> bool {
2025-08-06 20:05:04 -03:00
// 1. Lengths must match
if pre_states.len() != post_states.len() {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
for (pre, post) in pre_states.iter().zip(post_states) {
// 2. Nonce must remain unchanged
if pre.account.nonce != post.nonce {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
// 3. Ownership change only allowed from default accounts
if pre.account.program_owner != post.program_owner && pre.account != Account::default() {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
// 4. Decreasing balance only allowed if owned by executing program
if post.balance < pre.account.balance && pre.account.program_owner != executing_program_id {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
// 5. Data changes only allowed if owned by executing program
if pre.account.data != post.data
&& (executing_program_id != pre.account.program_owner
|| executing_program_id != post.program_owner)
{
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
}
// 6. Total balance is preserved
let total_balance_pre_states: u128 = pre_states.iter().map(|pre| pre.account.balance).sum();
let total_balance_post_states: u128 = post_states.iter().map(|post| post.balance).sum();
if total_balance_pre_states != total_balance_post_states {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
2025-08-09 19:49:07 -03:00
true
2025-08-06 20:05:04 -03:00
}