lssa/nssa/core/src/program.rs

265 lines
8.2 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-12-08 22:05:51 -05:00
//#[cfg(feature = "host")]
use crate::account::AccountId;
2025-12-04 21:34:47 -03:00
use crate::account::{Account, AccountWithMetadata};
2025-11-26 00:27:20 +03:00
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,
}
2025-11-28 17:09:38 -03:00
/// A 32-byte seed used to compute a *Program-Derived AccountId* (PDA).
///
2025-12-02 10:48:21 -03:00
/// Each program can derive up to `2^256` unique account IDs by choosing different
2025-11-28 17:09:38 -03:00
/// seeds. PDAs allow programs to control namespaced account identifiers without
/// collisions between programs.
2025-12-07 20:34:26 -05:00
#[derive(Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(any(feature = "host", test), derive(Debug, Eq))]
2025-11-27 12:08:27 -03:00
pub struct PdaSeed([u8; 32]);
impl PdaSeed {
pub fn new(value: [u8; 32]) -> Self {
Self(value)
}
}
2025-12-08 22:05:51 -05:00
//#[cfg(feature = "host")]
2025-11-27 13:10:38 -03:00
impl From<(&ProgramId, &PdaSeed)> for AccountId {
fn from(value: (&ProgramId, &PdaSeed)) -> Self {
use risc0_zkvm::sha::{Impl, Sha256};
const PROGRAM_DERIVED_ACCOUNT_ID_PREFIX: &[u8; 32] =
b"/NSSA/v0.2/AccountId/PDA/\x00\x00\x00\x00\x00\x00\x00";
let mut bytes = [0; 96];
bytes[0..32].copy_from_slice(PROGRAM_DERIVED_ACCOUNT_ID_PREFIX);
let program_id_bytes: &[u8] =
bytemuck::try_cast_slice(value.0).expect("ProgramId should be castable to &[u8]");
bytes[32..64].copy_from_slice(program_id_bytes);
bytes[64..].copy_from_slice(&value.1.0);
AccountId::new(
Impl::hash_bytes(&bytes)
.as_bytes()
.try_into()
.expect("Hash output must be exactly 32 bytes long"),
)
}
}
2025-12-07 20:34:26 -05:00
#[derive(Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(any(feature = "host", test), derive(Debug, 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>,
2025-11-27 13:10:38 -03:00
pub pda_seeds: Vec<PdaSeed>,
}
2025-12-04 10:10:01 -03:00
/// Represents the final state of an `Account` after a program execution.
/// A post state may optionally request that the executing program
/// becomes the owner of the account (a “claim”). This is used to signal
/// that the program intends to take ownership of the account.
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
pub struct AccountPostState {
2025-12-04 16:26:40 -03:00
account: Account,
2025-12-03 16:39:33 -03:00
claim: bool,
}
2025-12-03 16:39:33 -03:00
impl AccountPostState {
2025-12-04 10:10:01 -03:00
/// Creates a post state without a claim request.
/// The executing program is not requesting ownership of the account.
2025-12-03 16:39:33 -03:00
pub fn new(account: Account) -> Self {
Self {
account,
claim: false,
}
}
2025-12-03 17:06:09 -03:00
2025-12-04 10:10:01 -03:00
/// Creates a post state that requests ownership of the account.
/// This indicates that the executing program intends to claim the
/// account as its own and is allowed to mutate it.
2025-12-03 16:39:33 -03:00
pub fn new_claimed(account: Account) -> Self {
Self {
account,
claim: true,
}
}
2025-12-04 10:10:01 -03:00
/// Returns `true` if this post state requests that the account
/// be claimed (owned) by the executing program.
2025-12-03 16:39:33 -03:00
pub fn requires_claim(&self) -> bool {
self.claim
}
2025-12-04 16:26:40 -03:00
/// Returns the underlying account
pub fn account(&self) -> &Account {
&self.account
}
/// Returns the underlying account
pub fn account_mut(&mut self) -> &mut Account {
&mut self.account
}
}
#[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-04 10:02:29 -03:00
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,
};
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]
2025-12-04 10:02:29 -03:00
fn test_post_state_new_without_claim_constructor() {
2025-12-03 16:39:33 -03:00
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());
}
2025-12-04 16:26:40 -03:00
#[test]
fn test_post_state_account_getter() {
let mut account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 1337,
data: vec![0xde, 0xad, 0xbe, 0xef],
nonce: 10,
};
let mut account_post_state = AccountPostState::new(account.clone());
assert_eq!(account_post_state.account(), &account);
assert_eq!(account_post_state.account_mut(), &mut account);
}
}