miden_objects/account/header.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
use alloc::vec::Vec;
use vm_core::utils::{Deserializable, Serializable};
use super::{hash_account, Account, AccountId, Digest, Felt, ZERO};
// ACCOUNT HEADER
// ================================================================================================
/// A header of an account which contains information that succinctly describes the state of the
/// components of the account.
///
/// The [AccountHeader] is composed of:
/// - id: the account ID ([`AccountId`]) of the account.
/// - nonce: the nonce of the account.
/// - vault_root: a commitment to the account's vault ([super::AssetVault]).
/// - storage_commitment: a commitment to the account's storage ([super::AccountStorage]).
/// - code_commitment: a commitment to the account's code ([super::AccountCode]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AccountHeader {
id: AccountId,
nonce: Felt,
vault_root: Digest,
storage_commitment: Digest,
code_commitment: Digest,
}
impl AccountHeader {
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------
/// Creates a new [AccountHeader].
pub fn new(
id: AccountId,
nonce: Felt,
vault_root: Digest,
storage_commitment: Digest,
code_commitment: Digest,
) -> Self {
Self {
id,
nonce,
vault_root,
storage_commitment,
code_commitment,
}
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns hash of this account.
///
/// Hash of an account is computed as hash(id, nonce, vault_root, storage_commitment,
/// code_commitment). Computing the account hash requires 2 permutations of the hash
/// function.
pub fn hash(&self) -> Digest {
hash_account(
self.id,
self.nonce,
self.vault_root,
self.storage_commitment,
self.code_commitment,
)
}
/// Returns the id of this account.
pub fn id(&self) -> AccountId {
self.id
}
/// Returns the nonce of this account.
pub fn nonce(&self) -> Felt {
self.nonce
}
/// Returns the vault root of this account.
pub fn vault_root(&self) -> Digest {
self.vault_root
}
/// Returns the storage commitment of this account.
pub fn storage_commitment(&self) -> Digest {
self.storage_commitment
}
/// Returns the code commitment of this account.
pub fn code_commitment(&self) -> Digest {
self.code_commitment
}
/// Converts the account header into a vector of field elements.
///
/// This is done by first converting the account header data into an array of Words as follows:
/// ```text
/// [
/// [account_id_suffix, account_id_prefix, 0, account_nonce]
/// [VAULT_COMMITMENT]
/// [STORAGE_COMMITMENT]
/// [CODE_COMMITMENT]
/// ]
/// ```
/// And then concatenating the resulting elements into a single vector.
pub fn as_elements(&self) -> Vec<Felt> {
[
&[self.id.suffix(), self.id.prefix().as_felt(), ZERO, self.nonce],
self.vault_root.as_elements(),
self.storage_commitment.as_elements(),
self.code_commitment.as_elements(),
]
.concat()
}
}
impl From<Account> for AccountHeader {
fn from(account: Account) -> Self {
(&account).into()
}
}
impl From<&Account> for AccountHeader {
fn from(account: &Account) -> Self {
Self {
id: account.id(),
nonce: account.nonce(),
vault_root: account.vault().commitment(),
storage_commitment: account.storage().commitment(),
code_commitment: account.code().commitment(),
}
}
}
impl Serializable for AccountHeader {
fn write_into<W: vm_core::utils::ByteWriter>(&self, target: &mut W) {
self.id.write_into(target);
self.nonce.write_into(target);
self.vault_root.write_into(target);
self.storage_commitment.write_into(target);
self.code_commitment.write_into(target);
}
}
impl Deserializable for AccountHeader {
fn read_from<R: vm_core::utils::ByteReader>(
source: &mut R,
) -> Result<Self, vm_processor::DeserializationError> {
let id = AccountId::read_from(source)?;
let nonce = Felt::read_from(source)?;
let vault_root = Digest::read_from(source)?;
let storage_commitment = Digest::read_from(source)?;
let code_commitment = Digest::read_from(source)?;
Ok(AccountHeader {
id,
nonce,
vault_root,
storage_commitment,
code_commitment,
})
}
}
// TESTS
// ================================================================================================
#[cfg(test)]
mod tests {
use vm_core::{
utils::{Deserializable, Serializable},
Felt,
};
use super::AccountHeader;
use crate::{
account::{tests::build_account, StorageSlot},
asset::FungibleAsset,
};
#[test]
fn test_serde_account_storage() {
let init_nonce = Felt::new(1);
let asset_0 = FungibleAsset::mock(99);
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let storage_slot = StorageSlot::Value(word);
let account = build_account(vec![asset_0], init_nonce, vec![storage_slot]);
let account_header: AccountHeader = account.into();
let header_bytes = account_header.to_bytes();
let deserialized_header = AccountHeader::read_from_bytes(&header_bytes).unwrap();
assert_eq!(deserialized_header, account_header);
}
}