lssa/nssa/core/src/program.rs

196 lines
5.5 KiB
Rust
Raw Normal View History

2025-11-26 00:27:20 +03:00
use risc0_zkvm::{DeserializeOwned, guest::env, serde::Deserializer};
2025-08-14 14:09:04 -03:00
use serde::{Deserialize, Serialize};
2025-08-06 20:05:04 -03:00
2025-11-26 00:27:20 +03:00
use crate::account::{Account, AccountWithMetadata};
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))]
pub struct ChainedCall {
pub program_id: ProgramId,
pub instruction_data: InstructionData,
2025-11-17 15:43:01 -03:00
pub pre_states: Vec<AccountWithMetadata>,
}
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
pub struct AccountPostState {
pub account: Account,
2025-12-03 16:39:33 -03:00
claim: bool,
}
2025-12-03 16:39:33 -03:00
impl AccountPostState {
pub fn new(account: Account) -> Self {
Self {
account,
claim: false,
}
}
2025-12-03 17:06:09 -03:00
2025-12-03 16:39:33 -03:00
pub fn new_claimed(account: Account) -> Self {
Self {
account,
claim: true,
}
}
pub fn requires_claim(&self) -> bool {
self.claim
}
}
impl AccountPostState {
pub fn with_claim_request(mut self) -> Self {
self.claim = true;
self
}
}
#[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<AccountPostState>,
2025-11-12 19:18:04 -03:00
pub chained_calls: Vec<ChainedCall>,
2025-08-14 14:09:04 -03:00
}
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 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<AccountPostState>,
) {
2025-08-14 14:09:04 -03:00
let output = ProgramOutput {
pre_states,
post_states,
2025-11-12 19:18:04 -03:00
chained_calls: Vec::new(),
2025-10-29 15:34:11 -03:00
};
env::commit(&output);
}
pub fn write_nssa_outputs_with_chained_call(
pre_states: Vec<AccountWithMetadata>,
post_states: Vec<AccountPostState>,
2025-11-27 09:56:52 -03:00
chained_calls: Vec<ChainedCall>,
2025-10-29 15:34:11 -03:00
) {
let output = ProgramOutput {
pre_states,
post_states,
2025-11-27 09:56:52 -03:00
chained_calls,
2025-08-14 14:09:04 -03:00
};
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: &[AccountPostState],
2025-08-06 20:05:04 -03:00
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.account.nonce {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
2025-09-15 16:22:48 -03:00
// 3. Program ownership changes are not allowed
if pre.account.program_owner != post.account.program_owner {
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
2025-09-15 16:22:48 -03:00
let account_program_owner = pre.account.program_owner;
2025-08-06 20:05:04 -03:00
// 4. Decreasing balance only allowed if owned by executing program
if post.account.balance < pre.account.balance
&& account_program_owner != executing_program_id
{
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
2025-09-15 16:22:48 -03:00
// 5. Data changes only allowed if owned by executing program or if account pre state has
// default values
if pre.account.data != post.account.data
2025-09-15 16:22:48 -03:00
&& pre.account != Account::default()
&& account_program_owner != executing_program_id
2025-08-06 20:05:04 -03:00
{
2025-08-09 19:49:07 -03:00
return false;
2025-08-06 20:05:04 -03:00
}
2025-11-26 00:27:20 +03:00
// 6. If a post state has default program owner, the pre state must have been a default
// account
if post.account.program_owner == DEFAULT_PROGRAM_ID && pre.account != Account::default() {
return false;
}
2025-08-06 20:05:04 -03:00
}
// 7. Total balance is preserved
2025-08-06 20:05:04 -03:00
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.account.balance).sum();
2025-08-06 20:05:04 -03:00
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
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
2025-12-03 16:39:33 -03:00
fn test_post_state_new_without_claim_constructor() {
let account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 1337,
data: vec![0xde, 0xad, 0xbe, 0xef],
nonce: 10,
};
2025-12-03 16:39:33 -03:00
let account_post_state = AccountPostState::new_claimed(account.clone());
assert_eq!(account, account_post_state.account);
2025-12-03 16:39:33 -03:00
assert!(account_post_state.requires_claim());
}
2025-12-03 16:39:33 -03:00
#[test]
fn test_post_state_new_with_claim_constructor() {
let account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 1337,
data: vec![0xde, 0xad, 0xbe, 0xef],
nonce: 10,
};
let account_post_state = AccountPostState::new(account.clone());
assert_eq!(account, account_post_state.account);
assert!(!account_post_state.requires_claim());
}
}