-
Notifications
You must be signed in to change notification settings - Fork 119
Refactor(account) introducing account id key #2495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PhilippGackstatter
merged 16 commits into
0xMiden:next
from
swaploard:refactor(account)--introducing-AccountIdKey
Mar 6, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
15e97a2
Introducing a dedicated AccountIdKey type to unify and centralize all…
swaploard 2975792
Merge branch '0xMiden:next' into refactor(account)--introducing-Accou…
swaploard 2e23665
changelog for introduce AccountIdKey type
swaploard 275beb5
refactor: clean up comments and improve code readability in AccountId…
swaploard 4d60a5a
Merge branch 'next' into refactor(account)--introducing-AccountIdKey
swaploard b9034ff
Merge branch 'next' into refactor(account)--introducing-AccountIdKey
bobbinth 671bb0c
Merge branch 'next' into refactor(account)--introducing-AccountIdKey
swaploard 12bc8be
refactor: update AccountIdKey conversion method and clean up imports
swaploard 89cf428
Merge branch 'next' into refactor(account)--introducing-AccountIdKey
swaploard c50f5dc
Merge branch 'next' into refactor(account)--introducing-AccountIdKey
swaploard ae80bac
refactor: reorganize AccountIdKey indices and clean up related code
swaploard d35cda6
Merge branch 'next' into refactor(account)--introducing-AccountIdKey
swaploard 9bd1b89
Apply suggestions from code review
PhilippGackstatter f6ff249
Update crates/miden-protocol/src/block/account_tree/account_id_key.rs
PhilippGackstatter 7d73320
Update crates/miden-protocol/src/block/account_tree/account_id_key.rs
PhilippGackstatter 16bf071
feat: validate account ID in `AccountTree::new`
PhilippGackstatter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
crates/miden-protocol/src/block/account_tree/account_id_key.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| use miden_crypto::merkle::smt::LeafIndex; | ||
|
|
||
| use super::AccountId; | ||
| use crate::Word; | ||
| use crate::block::account_tree::AccountIdPrefix; | ||
| use crate::crypto::merkle::smt::SMT_DEPTH; | ||
| use crate::errors::AccountIdError; | ||
|
|
||
| /// The account ID encoded as a key for use in AccountTree and advice maps in | ||
| /// `TransactionAdviceInputs`. | ||
| /// | ||
| /// Canonical word layout: | ||
| /// | ||
| /// [0, 0, suffix, prefix] | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub struct AccountIdKey(AccountId); | ||
|
|
||
| impl AccountIdKey { | ||
| // Indices in the word layout where the prefix and suffix are stored. | ||
| const KEY_SUFFIX_IDX: usize = 2; | ||
| const KEY_PREFIX_IDX: usize = 3; | ||
|
|
||
| /// Create from AccountId | ||
| pub fn new(id: AccountId) -> Self { | ||
| Self(id) | ||
| } | ||
|
|
||
| /// Returns the underlying AccountId | ||
| pub fn account_id(&self) -> AccountId { | ||
| self.0 | ||
| } | ||
|
|
||
| // SMT WORD REPRESENTATION | ||
| //--------------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Returns `[0, 0, suffix, prefix]` | ||
| pub fn as_word(&self) -> Word { | ||
| let mut key = Word::empty(); | ||
|
|
||
| key[Self::KEY_SUFFIX_IDX] = self.0.suffix(); | ||
| key[Self::KEY_PREFIX_IDX] = self.0.prefix().as_felt(); | ||
|
|
||
| key | ||
| } | ||
|
|
||
| /// Construct from SMT word representation. | ||
| /// | ||
| /// Validates structure before converting. | ||
| pub fn try_from_word(word: Word) -> Result<AccountId, AccountIdError> { | ||
| AccountId::try_from_elements(word[Self::KEY_SUFFIX_IDX], word[Self::KEY_PREFIX_IDX]) | ||
| } | ||
|
|
||
| // LEAF INDEX | ||
| //--------------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Converts to SMT leaf index used by AccountTree | ||
| pub fn to_leaf_index(&self) -> LeafIndex<SMT_DEPTH> { | ||
| LeafIndex::from(self.as_word()) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| impl From<AccountId> for AccountIdKey { | ||
| fn from(id: AccountId) -> Self { | ||
| Self(id) | ||
| } | ||
| } | ||
|
|
||
| // TESTS | ||
| //--------------------------------------------------------------------------------------------------- | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
Comment on lines
+72
to
+73
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's add |
||
|
|
||
| use miden_core::ZERO; | ||
|
|
||
| use super::{AccountId, *}; | ||
| use crate::account::{AccountIdVersion, AccountStorageMode, AccountType}; | ||
| #[test] | ||
| fn test_as_word_layout() { | ||
| let id = AccountId::dummy( | ||
| [1u8; 15], | ||
| AccountIdVersion::Version0, | ||
| AccountType::RegularAccountImmutableCode, | ||
| AccountStorageMode::Private, | ||
| ); | ||
| let key = AccountIdKey::from(id); | ||
| let word = key.as_word(); | ||
|
|
||
| assert_eq!(word[0], ZERO); | ||
| assert_eq!(word[1], ZERO); | ||
| assert_eq!(word[2], id.suffix()); | ||
| assert_eq!(word[3], id.prefix().as_felt()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_roundtrip_word_conversion() { | ||
| let id = AccountId::dummy( | ||
| [1u8; 15], | ||
| AccountIdVersion::Version0, | ||
| AccountType::RegularAccountImmutableCode, | ||
| AccountStorageMode::Private, | ||
| ); | ||
|
|
||
| let key = AccountIdKey::from(id); | ||
| let recovered = | ||
| AccountIdKey::try_from_word(key.as_word()).expect("valid account id conversion"); | ||
|
|
||
| assert_eq!(id, recovered); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_leaf_index_consistency() { | ||
| let id = AccountId::dummy( | ||
| [1u8; 15], | ||
| AccountIdVersion::Version0, | ||
| AccountType::RegularAccountImmutableCode, | ||
| AccountStorageMode::Private, | ||
| ); | ||
| let key = AccountIdKey::from(id); | ||
|
|
||
| let idx1 = key.to_leaf_index(); | ||
| let idx2 = key.to_leaf_index(); | ||
|
|
||
| assert_eq!(idx1, idx2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_from_conversion() { | ||
| let id = AccountId::dummy( | ||
| [1u8; 15], | ||
| AccountIdVersion::Version0, | ||
| AccountType::RegularAccountImmutableCode, | ||
| AccountStorageMode::Private, | ||
| ); | ||
| let key: AccountIdKey = id.into(); | ||
|
|
||
| assert_eq!(key.account_id(), id); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_multiple_roundtrips() { | ||
| for _ in 0..100 { | ||
| let id = AccountId::dummy( | ||
| [1u8; 15], | ||
| AccountIdVersion::Version0, | ||
| AccountType::RegularAccountImmutableCode, | ||
| AccountStorageMode::Private, | ||
| ); | ||
| let key = AccountIdKey::from(id); | ||
|
|
||
| let recovered = | ||
| AccountIdKey::try_from_word(key.as_word()).expect("valid account id conversion"); | ||
|
|
||
| assert_eq!(id, recovered); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.