Skip to content

Commit

Permalink
Added AccountProcedure and TryFrom [Felt; 8]
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive committed Jul 23, 2024
1 parent cc7303d commit 9cc6661
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
6 changes: 3 additions & 3 deletions miden-lib/src/transaction/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;

use miden_objects::{
accounts::Account,
accounts::{Account, AccountProcedure},
transaction::{
ChainMmr, ExecutedTransaction, InputNote, InputNotes, PreparedTransaction, TransactionArgs,
TransactionInputs, TransactionScript, TransactionWitness,
Expand Down Expand Up @@ -244,8 +244,8 @@ fn add_account_to_advice_inputs(
// --- account code -------------------------------------------------------
let code = account.code();

// extend the advice_map with the account code data and procedure length
let num_procs = code.as_elements().len() / 8;
// extend the advice_map with the account code data and number of procedures
let num_procs = code.as_elements().len() / AccountProcedure::NUM_ELEMENTS_PER_PROC;
let mut procs = code.as_elements();
procs.insert(0, Felt::from(num_procs as u32));
inputs.extend_map([(code.commitment().clone(), procs)]);
Expand Down
28 changes: 24 additions & 4 deletions miden-tx/src/host/account_procs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::string::ToString;
use miden_lib::transaction::TransactionKernelError;
use miden_lib::{transaction::TransactionKernelError, utils::collections::KvMap};
use miden_objects::accounts::AccountProcedure;

use crate::error::TransactionHostError;

Expand Down Expand Up @@ -27,10 +28,29 @@ impl AccountProcedureIndexMap {

let mut result = BTreeMap::new();

// we skip procs[0] because it's the `len` of procs
// we skip procs[0] because it's the number of procedures
for (proc_idx, proc_info) in procs[1..].chunks_exact(8).enumerate() {
let root: [Felt; 4] = proc_info[0..4].try_into().expect("Slice with incorrect len.");
result.insert(Digest::from(root), proc_idx.try_into().unwrap());
let proc_info_array: [Felt; 8] = proc_info.try_into().map_err(|_| {
TransactionHostError::AccountProcedureIndexMapError(
"Invalid procedure info length".to_string(),
)
})?;

let procedure = AccountProcedure::try_from(proc_info_array).map_err(|e| {
TransactionHostError::AccountProcedureIndexMapError(format!(
"Failed to create AccountProcedure: {:?}",
e
))
})?;

let proc_idx = u8::try_from(proc_idx).map_err(|_| {
TransactionHostError::AccountProcedureIndexMapError(format!(
"Invalid procedure index: {}",
proc_idx
))
})?;

result.insert(*procedure.mast_root(), proc_idx);
}

Ok(Self(result))
Expand Down
3 changes: 3 additions & 0 deletions objects/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub use auth::AuthSecretKey;
pub mod code;
pub use code::AccountCode;

mod procedure;
pub use procedure::AccountProcedure;

pub mod delta;
pub use delta::{AccountDelta, AccountStorageDelta, AccountVaultDelta, StorageMapDelta};

Expand Down
38 changes: 38 additions & 0 deletions objects/src/accounts/procedure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::AccountError;

use super::{Digest, Felt};

pub struct AccountProcedure {
mast_root: Digest,
storage_offset: u16,
}

impl AccountProcedure {
// CONSTANTS
// --------------------------------------------------------------------------------------------

/// The number of elements needed to represent an [AccountProcedure]
pub const NUM_ELEMENTS_PER_PROC: usize = 8;

/// Returns a reference to the procedure's mast_root
pub fn mast_root(&self) -> &Digest {
&self.mast_root
}

/// Returns a reference to the procedure's storage_offset
pub fn storage_offset(&self) -> u16 {
self.storage_offset
}
}

impl TryFrom<[Felt; 8]> for AccountProcedure {
type Error = AccountError;

fn try_from(value: [Felt; 8]) -> Result<Self, Self::Error> {
let mast_root = Digest::from(<[Felt; 4]>::try_from(&value[0..4]).unwrap());
let storage_offset = u16::try_from(value[4].inner())
.map_err(|_| AccountError::AccountCodeProcedureInvalidStorageOffset)?;

Ok(Self { mast_root, storage_offset })
}
}
1 change: 1 addition & 0 deletions objects/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum AccountError {
AccountCodeAssemblerError(AssemblyError),
AccountCodeNoProcedures,
AccountCodeTooManyProcedures { max: usize, actual: usize },
AccountCodeProcedureInvalidStorageOffset,
AccountIdInvalidFieldElement(String),
AccountIdTooFewOnes(u32, u32),
AssetVaultUpdateError(AssetVaultError),
Expand Down

0 comments on commit 9cc6661

Please sign in to comment.