diff --git a/miden-tx/src/tests/kernel_tests/test_prologue.rs b/miden-tx/src/tests/kernel_tests/test_prologue.rs index fcb5b784a..7146d86fd 100644 --- a/miden-tx/src/tests/kernel_tests/test_prologue.rs +++ b/miden-tx/src/tests/kernel_tests/test_prologue.rs @@ -29,7 +29,7 @@ use miden_lib::{ use miden_objects::{ accounts::{ AccountBuilder, AccountComponent, AccountProcedureInfo, AccountStorage, AccountType, - NUM_ELEMENTS_PER_STORAGE_SLOT, + StorageSlot, }, testing::{ account_component::BASIC_WALLET_CODE, @@ -277,7 +277,7 @@ fn account_data_memory_assertions(process: &Process, inputs: &Transact .account() .storage() .as_elements() - .chunks(NUM_ELEMENTS_PER_STORAGE_SLOT / 2) + .chunks(StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT / 2) .enumerate() { assert_eq!( diff --git a/miden-tx/src/tests/kernel_tests/test_tx.rs b/miden-tx/src/tests/kernel_tests/test_tx.rs index f227957a4..1c7d180fe 100644 --- a/miden-tx/src/tests/kernel_tests/test_tx.rs +++ b/miden-tx/src/tests/kernel_tests/test_tx.rs @@ -23,7 +23,7 @@ use miden_objects::{ ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2, }, Account, AccountBuilder, AccountComponent, AccountProcedureInfo, AccountStorage, - StorageSlot, NUM_ELEMENTS_PER_STORAGE_SLOT, + StorageSlot, }, assets::NonFungibleAsset, crypto::merkle::{LeafIndex, MerklePath}, @@ -1119,7 +1119,7 @@ fn foreign_account_data_memory_assertions(foreign_account: &Account, process: &P for (i, elements) in foreign_account .storage() .as_elements() - .chunks(NUM_ELEMENTS_PER_STORAGE_SLOT / 2) + .chunks(StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT / 2) .enumerate() { assert_eq!( diff --git a/objects/src/accounts/mod.rs b/objects/src/accounts/mod.rs index b3ffa4d96..2244e1bb8 100644 --- a/objects/src/accounts/mod.rs +++ b/objects/src/accounts/mod.rs @@ -33,10 +33,7 @@ mod seed; pub use seed::{get_account_seed, get_account_seed_single}; mod storage; -pub use storage::{ - AccountStorage, AccountStorageHeader, StorageMap, StorageSlot, StorageSlotType, - NUM_ELEMENTS_PER_STORAGE_SLOT, -}; +pub use storage::{AccountStorage, AccountStorageHeader, StorageMap, StorageSlot, StorageSlotType}; mod header; pub use header::AccountHeader; diff --git a/objects/src/accounts/storage/header.rs b/objects/src/accounts/storage/header.rs index 703ff7a3e..162eda135 100644 --- a/objects/src/accounts/storage/header.rs +++ b/objects/src/accounts/storage/header.rs @@ -6,36 +6,46 @@ use vm_core::{ }; use vm_processor::DeserializationError; -use super::{ - AccountStorage, Felt, StorageSlot, StorageSlotType, Word, NUM_ELEMENTS_PER_STORAGE_SLOT, -}; +use super::{AccountStorage, Felt, StorageSlot, StorageSlotType, Word}; use crate::AccountError; // ACCOUNT STORAGE HEADER // ================================================================================================ -#[derive(Debug, Clone, PartialEq, Eq)] +/// Storage slot header is a lighter version of the [StorageSlot] storing only the type and the +/// top-level value for the slot, and being, in fact, just a thin wrapper around a tuple. +/// +/// That is, for complex storage slot (e.g., storage map), the header contains only the commitment +/// to the underlying data. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct StorageSlotHeader(StorageSlotType, Word); -impl From<&StorageSlot> for StorageSlotHeader { - fn from(value: &StorageSlot) -> Self { - Self(value.slot_type(), value.value()) - } -} - impl StorageSlotHeader { + /// Returns a new instance of storage slot header from the provided storage slot type and value. pub fn new(value: &(StorageSlotType, Word)) -> Self { - Self(value.0.clone(), value.1) + Self(value.0, value.1) } - pub fn as_elements(&self) -> [Felt; NUM_ELEMENTS_PER_STORAGE_SLOT] { - let mut elements = [ZERO; NUM_ELEMENTS_PER_STORAGE_SLOT]; + /// Returns this storage slot header as field elements. + /// + /// This is done by converting this storage slot into 8 field elements as follows: + /// ```text + /// [SLOT_VALUE, slot_type, 0, 0, 0] + /// ``` + pub fn as_elements(&self) -> [Felt; StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT] { + let mut elements = [ZERO; StorageSlot::NUM_ELEMENTS_PER_STORAGE_SLOT]; elements[0..4].copy_from_slice(&self.1); elements[4..8].copy_from_slice(&self.0.as_word()); elements } } +impl From<&StorageSlot> for StorageSlotHeader { + fn from(value: &StorageSlot) -> Self { + Self(value.slot_type(), value.value()) + } +} + /// Account storage header is a lighter version of the [AccountStorage] storing only the type and /// the top-level value for each storage slot. /// diff --git a/objects/src/accounts/storage/mod.rs b/objects/src/accounts/storage/mod.rs index df0cce0b0..685f4dd5f 100644 --- a/objects/src/accounts/storage/mod.rs +++ b/objects/src/accounts/storage/mod.rs @@ -15,12 +15,6 @@ pub use map::StorageMap; mod header; pub use header::{AccountStorageHeader, StorageSlotHeader}; -// CONSTANTS -// ================================================================================================ - -/// The number of field elements needed to represent a [StorageSlot] in kernel memory. -pub const NUM_ELEMENTS_PER_STORAGE_SLOT: usize = 8; - // ACCOUNT STORAGE // ================================================================================================ diff --git a/objects/src/accounts/storage/slot/mod.rs b/objects/src/accounts/storage/slot/mod.rs index de4b03dbf..ff806e340 100644 --- a/objects/src/accounts/storage/slot/mod.rs +++ b/objects/src/accounts/storage/slot/mod.rs @@ -20,6 +20,9 @@ pub enum StorageSlot { } impl StorageSlot { + /// The number of field elements needed to represent a [StorageSlot] in kernel memory. + pub const NUM_ELEMENTS_PER_STORAGE_SLOT: usize = 8; + /// Returns true if this storage slot has a value equal the default of it's type pub fn is_default(&self) -> bool { match self { diff --git a/objects/src/accounts/storage/slot/type.rs b/objects/src/accounts/storage/slot/type.rs index 5be17b364..4d6ed2d5b 100644 --- a/objects/src/accounts/storage/slot/type.rs +++ b/objects/src/accounts/storage/slot/type.rs @@ -10,7 +10,7 @@ use vm_processor::DeserializationError; // ================================================================================================ /// An object that represents the type of a storage slot. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum StorageSlotType { /// Represents a slot that contains a value. Value,