miden_objects/account/
header.rs

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