-
Notifications
You must be signed in to change notification settings - Fork 115
feat: double-word Array abstraction #2299
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
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6f03672
feat: working array
mmagician e136d3c
chore: add changelog entry for Array component (#2204)
Copilot c1c788d
fix: set_map_item no longer returns old root
mmagician 05b5234
chore: change Try to TryFrom for AccountComponent
mmagician aa50de3
chore: correct the docs
mmagician d04e396
chore: masm doc corrections
mmagician 17d96bf
chore: use BeWord instead of Word
mmagician 2d34da0
chore: adjust masm comment about max len
mmagician cff5982
Update crates/miden-standards/asm/account_components/array.masm
mmagician 6bd6069
chore: turn array into utility
mmagician 3ff3c49
chore: lint, simplify test & comments
mmagician 0c37ba3
chore: remove unnecessary comments
mmagician ab2ca4d
chore: remove duplicated changelog entry
mmagician fe5f3db
feat: working double word array
mmagician 0656c5f
chore: simplify double word masm logic
mmagician 456bd60
chore: lint, add setter assertions
mmagician 69c9eac
chore: update changelog entries
mmagician d14d9fd
Apply suggestions from code review
mmagician 68a1427
Merge branch 'next' into mmagician-double-word-array
mmagician ed0c433
feat: make procedures exec; fix stack accordingly
mmagician 3f50929
fix: proc only use 3 locals
mmagician e6ea585
chore: use constants for locals
mmagician c5357ac
[WIP] Update changelog entry for double-word Array abstraction (#2385)
Copilot ce14a45
Merge branch 'next' into mmagician-double-word-array
mmagician 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
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
134 changes: 134 additions & 0 deletions
134
crates/miden-standards/asm/standards/data_structures/double_word_array.masm
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,134 @@ | ||
| # The MASM code for the Double-Word Array abstraction. | ||
| # | ||
| # It provides an abstraction layer over a storage map, treating it as an array | ||
| # of double-words, with "set" and "get" for storing and retrieving values by | ||
| # (slot_id, index). | ||
| # The array can store up to 2^64 - 2^32 + 1 elements (indices 0 to 2^64 - 2^32). | ||
| # | ||
| # Using this Double-Word Array utility requires that the underlying storage map is already created and | ||
| # initialized as part of an account component, under the given slot ID. | ||
|
|
||
|
|
||
| use miden::protocol::active_account | ||
| use miden::protocol::native_account | ||
|
|
||
| # Local Memory Addresses | ||
| const SLOT_ID_PREFIX_LOC=0 | ||
| const SLOT_ID_SUFFIX_LOC=1 | ||
| const INDEX_LOC=2 | ||
|
|
||
| type BeWord = struct @bigendian { a: felt, b: felt, c: felt, d: felt } | ||
| type BeDoubleWord = struct @bigendian { | ||
| a: felt, | ||
| b: felt, | ||
| c: felt, | ||
| d: felt, | ||
| e: felt, | ||
| f: felt, | ||
| g: felt, | ||
| h: felt | ||
| } | ||
|
|
||
| # PROCEDURES | ||
| # ================================================================================================= | ||
|
|
||
| #! Sets a double-word in the array at the specified index. | ||
| #! | ||
| #! Inputs: [slot_id_prefix, slot_id_suffix, index, VALUE_0, VALUE_1] | ||
| #! Outputs: [OLD_VALUE_0, OLD_VALUE_1] | ||
| #! | ||
| #! Where: | ||
| #! - slot_id_{prefix, suffix} are the prefix and suffix felts of the slot identifier. | ||
| #! - index is the index at which to store the value (0 to 2^64 - 2^32). | ||
| #! - VALUE_0 is the first word to store at the specified index. | ||
| #! - VALUE_1 is the second word to store at the specified index. | ||
| #! | ||
| #! Internally, the words are stored under keys [index, 0, 0, 0] and [index, 1, 0, 0], | ||
| #! for the first and second word, respectively. | ||
| #! | ||
| #! Invocation: exec | ||
| @locals(3) | ||
| pub proc set( | ||
| slot_id_prefix: felt, | ||
| slot_id_suffix: felt, | ||
| index: felt, | ||
| value: BeDoubleWord | ||
| ) -> BeDoubleWord | ||
| # save inputs to locals for reuse | ||
| loc_store.SLOT_ID_PREFIX_LOC | ||
| loc_store.SLOT_ID_SUFFIX_LOC | ||
| loc_store.INDEX_LOC | ||
| # => [VALUE_0, VALUE_1] | ||
|
|
||
| # Set the first word under key [index, 0, 0, 0]. | ||
| push.0.0.0 | ||
| loc_load.INDEX_LOC | ||
| # => [index, 0, 0, 0, VALUE_0, VALUE_1] | ||
|
|
||
| loc_load.SLOT_ID_SUFFIX_LOC | ||
| loc_load.SLOT_ID_PREFIX_LOC | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_0, VALUE_0, VALUE_1] | ||
|
|
||
| exec.native_account::set_map_item | ||
| # => [OLD_VALUE_0, VALUE_1] | ||
| swapw | ||
|
|
||
| # Set the second word under key [index, 1, 0, 0]. | ||
| push.0.0.1 | ||
| loc_load.INDEX_LOC | ||
| # => [index, 1, 0, 0, VALUE_1, OLD_VALUE_0] | ||
|
|
||
| loc_load.SLOT_ID_SUFFIX_LOC | ||
| loc_load.SLOT_ID_PREFIX_LOC | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_1, VALUE_1, OLD_VALUE_0] | ||
|
|
||
| exec.native_account::set_map_item | ||
| # => [OLD_VALUE_1, OLD_VALUE_0] | ||
| swapw | ||
| end | ||
|
|
||
| #! Gets a double-word from the array at the specified index. | ||
| #! | ||
| #! Inputs: [slot_id_prefix, slot_id_suffix, index] | ||
| #! Outputs: [VALUE_0, VALUE_1] | ||
| #! | ||
| #! Where: | ||
| #! - slot_id_{prefix, suffix} are the prefix and suffix felts of the slot identifier. | ||
| #! - index is the index of the element to retrieve (0 to 2^64 - 2^32). | ||
| #! - VALUE_0 is the first word stored at the specified index (zero if not set). | ||
| #! - VALUE_1 is the second word stored at the specified index (zero if not set). | ||
| #! | ||
| #! Invocation: exec | ||
| @locals(3) | ||
| pub proc get(slot_id_prefix: felt, slot_id_suffix: felt, index: felt) -> BeDoubleWord | ||
| # Save inputs to locals for reuse. | ||
| loc_store.SLOT_ID_PREFIX_LOC | ||
| loc_store.SLOT_ID_SUFFIX_LOC | ||
| loc_store.INDEX_LOC | ||
| # => [] | ||
|
|
||
| # Get the first word from key [index, 0, 0, 0]. | ||
| push.0.0.0 | ||
| loc_load.INDEX_LOC | ||
| # => [index, 0, 0, 0] | ||
|
|
||
| loc_load.SLOT_ID_SUFFIX_LOC | ||
| loc_load.SLOT_ID_PREFIX_LOC | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_0] | ||
|
|
||
| exec.active_account::get_map_item | ||
| # => [VALUE_0] | ||
|
|
||
| # Get the second word from key [index, 1, 0, 0]. | ||
| push.0.0.1 | ||
| loc_load.INDEX_LOC | ||
| # => [index, 1, 0, 0, VALUE_0] | ||
|
|
||
| loc_load.SLOT_ID_SUFFIX_LOC | ||
| loc_load.SLOT_ID_PREFIX_LOC | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_1, VALUE_0] | ||
|
|
||
| exec.active_account::get_map_item | ||
| swapw | ||
| # => [VALUE_0, VALUE_1] | ||
| end |
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
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.