-
Notifications
You must be signed in to change notification settings - Fork 106
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
Open
mmagician
wants to merge
5
commits into
mmagician-array-via-map
Choose a base branch
from
mmagician-double-word-array
base: mmagician-array-via-map
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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
130 changes: 130 additions & 0 deletions
130
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,130 @@ | ||
| # 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 | ||
|
|
||
| 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, pad(5)] | ||
| #! Outputs: [OLD_VALUE_0, OLD_VALUE_1, pad(8)] | ||
| #! | ||
| #! 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: call | ||
| @locals(16) | ||
| pub proc set( | ||
| slot_id_prefix: felt, | ||
| slot_id_suffix: felt, | ||
| index: felt, | ||
| value: BeDoubleWord | ||
| ) -> BeDoubleWord | ||
| # save inputs to locals for reuse | ||
| loc_store.0 | ||
| loc_store.1 | ||
| loc_store.2 | ||
| # => [VALUE_0, VALUE_1, pad(8)] auto-padding | ||
|
|
||
| # Set the first word under key [index, 0, 0, 0]. | ||
| push.0.0.0 | ||
| loc_load.2 | ||
| # => [index, 0, 0, 0, VALUE_0, VALUE_1, pad(8)] | ||
|
|
||
| loc_load.1 | ||
| loc_load.0 | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_0, VALUE_0, VALUE_1, pad(8)] | ||
|
|
||
| exec.native_account::set_map_item | ||
| # => [OLD_VALUE_0, VALUE_1, pad(8)] | ||
| swapw | ||
|
|
||
| # Set the second word under key [index, 1, 0, 0]. | ||
| push.0.0.1 | ||
| loc_load.2 | ||
| # => [index, 1, 0, 0, VALUE_1, OLD_VALUE_0, pad(8)] | ||
|
|
||
| loc_load.1 | ||
| loc_load.0 | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_1, VALUE_1, OLD_VALUE_0, pad(8)] | ||
|
|
||
| exec.native_account::set_map_item | ||
| # => [OLD_VALUE_1, OLD_VALUE_0, pad(8)] | ||
| swapw | ||
| end | ||
|
|
||
| #! Gets a double-word from the array at the specified index. | ||
| #! | ||
| #! Inputs: [slot_id_prefix, slot_id_suffix, index, pad(13)] | ||
| #! Outputs: [VALUE_0, VALUE_1, pad(8)] | ||
| #! | ||
| #! 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: call | ||
| @locals(12) | ||
| pub proc get(slot_id_prefix: felt, slot_id_suffix: felt, index: felt) -> BeDoubleWord | ||
| # Save inputs to locals for reuse. | ||
| loc_store.0 | ||
| loc_store.1 | ||
| loc_store.2 | ||
| # => [pad(16)] auto-padding | ||
|
|
||
| # Get the first word from key [index, 0, 0, 0]. | ||
| push.0.0.0 | ||
| loc_load.2 | ||
| # => [index, 0, 0, 0, pad(16)] | ||
|
|
||
| loc_load.1 | ||
| loc_load.0 | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_0, pad(16)] | ||
|
|
||
| exec.active_account::get_map_item | ||
| # => [VALUE_0, pad(16)] | ||
|
|
||
| # Get the second word from key [index, 1, 0, 0]. | ||
| push.0.0.1 | ||
| loc_load.2 | ||
| # => [index, 1, 0, 0, pad(16)] | ||
|
|
||
| loc_load.1 | ||
| loc_load.0 | ||
| # => [slot_id_prefix, slot_id_suffix, KEY_1, pad(16)] | ||
|
|
||
| exec.active_account::get_map_item | ||
| swapw | ||
| # => [VALUE_0, VALUE_1, pad(16)] | ||
| swapdw dropw dropw | ||
| 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.