diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/account.masm b/crates/miden-protocol/asm/kernels/transaction/lib/account.masm index b2dcfa4bc7..5d16dacc7f 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/account.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/account.masm @@ -1,7 +1,10 @@ use $kernel::account_delta use $kernel::account_id use $kernel::asset_vault -use $kernel::constants +use $kernel::constants::ACCOUNT_PROCEDURE_DATA_LENGTH +use $kernel::constants::EMPTY_SMT_ROOT +use $kernel::constants::STORAGE_SLOT_TYPE_MAP +use $kernel::constants::STORAGE_SLOT_TYPE_VALUE use $kernel::memory use miden::core::collections::smt use miden::core::collections::sorted_array @@ -120,9 +123,6 @@ const ACCOUNT_TREE_DEPTH=64 # The number of field elements it takes to store one account storage slot. const ACCOUNT_STORAGE_SLOT_DATA_LENGTH=8 -# The number of field elements it takes to store one account procedure. -const ACCOUNT_PROCEDURE_DATA_LENGTH=4 - # The offset of the slot type in the storage slot. const ACCOUNT_SLOT_TYPE_OFFSET=1 @@ -538,7 +538,7 @@ pub proc set_item # => [slot_type, slot_ptr, VALUE] # assert slot_type is value - exec.constants::get_storage_slot_type_value eq + push.STORAGE_SLOT_TYPE_VALUE eq assert.err=ERR_ACCOUNT_SETTING_VALUE_ITEM_ON_NON_VALUE_SLOT # => [slot_ptr, VALUE] @@ -632,7 +632,7 @@ pub proc set_map_item # => [slot_type, slot_ptr, KEY, NEW_VALUE] # assert slot_type is map - exec.constants::get_storage_slot_type_map eq + push.STORAGE_SLOT_TYPE_MAP eq assert.err=ERR_ACCOUNT_SETTING_MAP_ITEM_ON_NON_MAP_SLOT # => [slot_ptr, KEY, NEW_VALUE] @@ -1310,7 +1310,7 @@ pub proc insert_new_storage dup exec.get_storage_slot_type # => [slot_type, slot_idx] - exec.constants::get_storage_slot_type_map eq + push.STORAGE_SLOT_TYPE_MAP eq # => [is_map_slot_type, slot_idx] if.true @@ -1354,7 +1354,7 @@ proc insert_and_validate_storage_map # overwrite the map root with the root of an empty SMT, so we can insert the entries of # the map into an empty map and then check whether the resulting root matches MAP_ROOT. - exec.constants::get_empty_smt_root + push.EMPTY_SMT_ROOT dup.8 # => [slot_ptr, EMPTY_SMT_ROOT, MAP_ROOT, slot_ptr] @@ -1674,7 +1674,7 @@ proc get_map_item_raw # => [slot_type, slot_ptr, KEY] # assert slot_type is map - exec.constants::get_storage_slot_type_map eq + push.STORAGE_SLOT_TYPE_MAP eq assert.err=ERR_ACCOUNT_READING_MAP_VALUE_FROM_NON_MAP_SLOT # => [slot_ptr, KEY] diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/account_delta.masm b/crates/miden-protocol/asm/kernels/transaction/lib/account_delta.masm index 220b431df5..79ff5365ff 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/account_delta.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/account_delta.masm @@ -1,7 +1,7 @@ use $kernel::account use $kernel::asset use $kernel::asset_vault -use $kernel::constants +use $kernel::constants::STORAGE_SLOT_TYPE_VALUE use $kernel::link_map use $kernel::memory use miden::core::crypto::hashes::rpo256 @@ -149,7 +149,7 @@ proc update_slot_delta # => [storage_slot_type, slot_idx, RATE, RATE, PERM] # check if slot is of type value - exec.constants::get_storage_slot_type_value eq + push.STORAGE_SLOT_TYPE_VALUE eq # => [is_value_slot_type, slot_idx, RATE, RATE, PERM] if.true diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/constants.masm b/crates/miden-protocol/asm/kernels/transaction/lib/constants.masm index fb0dfb08f9..5c58398d3c 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/constants.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/constants.masm @@ -2,152 +2,36 @@ # ================================================================================================= # The number of elements in a Word -const WORD_SIZE=4 +pub const WORD_SIZE = 4 # The maximum number of input values associated with a single note. -const MAX_INPUTS_PER_NOTE=1024 +pub const MAX_INPUTS_PER_NOTE = 1024 # The maximum number of assets that can be stored in a single note. -const MAX_ASSETS_PER_NOTE=256 +pub const MAX_ASSETS_PER_NOTE = 256 # The maximum number of notes that can be consumed in a single transaction. -const MAX_INPUT_NOTES_PER_TX=1024 +pub const MAX_INPUT_NOTES_PER_TX = 1024 # The size of the memory segment allocated to each note. -const NOTE_MEM_SIZE=2048 +pub const NOTE_MEM_SIZE = 2048 # The depth of the Merkle tree used to commit to notes produced in a block. -const NOTE_TREE_DEPTH=16 +pub const NOTE_TREE_DEPTH = 16 # The maximum number of notes that can be created in a single transaction. -const MAX_OUTPUT_NOTES_PER_TX=1024 +pub const MAX_OUTPUT_NOTES_PER_TX = 1024 -# TYPES -# ================================================================================================= - -# Type of storage slot item in the account storage -const STORAGE_SLOT_TYPE_VALUE=0 -const STORAGE_SLOT_TYPE_MAP=1 -const STORAGE_SLOT_TYPE_ARRAY=2 +# The number of field elements per account procedure data entry (procedure MAST root). +pub const ACCOUNT_PROCEDURE_DATA_LENGTH = 4 -# PROCEDURES +# TYPES # ================================================================================================= -#! Returns the number of elements in a Word. -#! -#! Inputs: [] -#! Outputs: [word_size] -#! -#! Where: -#! - word_size is the number of elements in a Word. -pub proc get_word_size - push.WORD_SIZE -end - -#! Returns the max allowed number of input values per note. -#! -#! Inputs: [] -#! Outputs: [max_inputs_per_note] -#! -#! Where: -#! - max_inputs_per_note is the max inputs per note. -pub use ::$kernel::util::note::get_max_inputs_per_note - -#! Returns the max allowed number of assets per note. -#! -#! Inputs: [] -#! Outputs: [max_assets_per_note] -#! -#! Where: -#! - max_assets_per_note is the max assets per note. -pub proc get_max_assets_per_note - push.MAX_ASSETS_PER_NOTE -end - -#! Returns the maximum number of notes that can be consumed in a single transaction. -#! -#! Inputs: [] -#! Outputs: [max_num_input_notes] -#! -#! Where: -#! - max_num_input_notes is the max number of input notes. -pub proc get_max_num_input_notes - push.MAX_INPUT_NOTES_PER_TX -end - -#! Returns the size of the memory segment allocated to each note. -#! -#! Inputs: [] -#! Outputs: [note_mem_size] -#! -#! Where: -#! - note_mem_size is the size of the memory segment allocated to each note. -pub proc get_note_mem_size - push.NOTE_MEM_SIZE -end +# Root of an empty Sparse Merkle Tree +pub const EMPTY_SMT_ROOT = [15321474589252129342, 17373224439259377994, 15071539326562317628, 3312677166725950353] -#! Returns the depth of the Merkle tree used to commit to notes produced in a block. -#! -#! Inputs: [] -#! Outputs: [note_tree_depth] -#! -#! Where: -#! - note_tree_depth is the depth of the Merkle tree used to commit to notes produced in a block. -pub proc get_note_tree_depth - push.NOTE_TREE_DEPTH -end - -#! Returns the maximum number of notes that can be created in a single transaction. -#! -#! Inputs: [] -#! Outputs: [max_num_output_notes] -#! -#! Where: -#! - max_num_output_notes is the max number of notes that can be created in a single transaction. -pub proc get_max_num_output_notes - push.MAX_OUTPUT_NOTES_PER_TX -end - -#! Returns the root of an empty Sparse Merkle Tree. -#! -#! Inputs: [] -#! Outputs: [EMPTY_SMT_ROOT] -#! -#! Where: -#! - EMPTY_SMT_ROOT is the root of an empty Sparse Merkle Tree. -pub proc get_empty_smt_root - push.15321474589252129342.17373224439259377994.15071539326562317628.3312677166725950353 -end - -#! Returns the type of storage slot value in the account storage. -#! -#! Inputs: [] -#! Outputs: [type_storage_value] -#! -#! Where: -#! - type_storage_value is the type of storage slot item in the account storage. -pub proc get_storage_slot_type_value - push.STORAGE_SLOT_TYPE_VALUE -end - -#! Returns the type of storage slot map in the account storage. -#! -#! Inputs: [] -#! Outputs: [type_storage_map] -#! -#! Where: -#! - type_storage_map is the type of storage slot item in the account storage. -pub proc get_storage_slot_type_map - push.STORAGE_SLOT_TYPE_MAP -end - -#! Returns the type of storage slot array in the account storage. -#! -#! Inputs: [] -#! Outputs: [type_storage_array] -#! -#! Where: -#! - type_storage_array is the type of storage slot item in the account storage. -pub proc get_storage_slot_type_array - push.STORAGE_SLOT_TYPE_ARRAY -end +# Type of storage slot item in the account storage +pub const STORAGE_SLOT_TYPE_VALUE = 0 +pub const STORAGE_SLOT_TYPE_MAP = 1 +pub const STORAGE_SLOT_TYPE_ARRAY = 2 diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/epilogue.masm b/crates/miden-protocol/asm/kernels/transaction/lib/epilogue.masm index 196f4aeaa6..ef969001b5 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/epilogue.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/epilogue.masm @@ -1,7 +1,7 @@ use $kernel::account use $kernel::account_delta use $kernel::asset_vault -use $kernel::constants +use $kernel::constants::NOTE_MEM_SIZE use $kernel::memory use $kernel::note @@ -187,7 +187,7 @@ proc build_output_vault # => [note_data_ptr, output_note_end_ptr] # increment output note pointer and check if we should loop again - exec.constants::get_note_mem_size add dup.1 dup.1 neq + push.NOTE_MEM_SIZE add dup.1 dup.1 neq # => [should_loop, output_note_ptr, output_notes_end_ptr] end diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/memory.masm b/crates/miden-protocol/asm/kernels/transaction/lib/memory.masm index b1d93d415f..7d16cf6cbb 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/memory.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/memory.masm @@ -1,4 +1,6 @@ -use $kernel::constants +use $kernel::constants::ACCOUNT_PROCEDURE_DATA_LENGTH +use $kernel::constants::MAX_ASSETS_PER_NOTE +use $kernel::constants::NOTE_MEM_SIZE use miden::core::mem # ERRORS @@ -166,10 +168,6 @@ const NATIVE_ACCOUNT_DATA_PTR=8192 # The length of the memory interval that the account data occupies. const ACCOUNT_DATA_LENGTH=8192 -# The number of field elements it takes to store one account procedure. -# TODO: Duplicated in account.masm, can we remove that? -const ACCOUNT_PROCEDURE_DATA_LENGTH=4 - # The offsets at which the account data is stored relative to the start of the account data segment. const ACCT_ID_AND_NONCE_OFFSET=0 const ACCT_NONCE_OFFSET=3 @@ -1513,7 +1511,7 @@ end #! - idx is the index of the input note. #! - note_ptr is the memory address of the data segment for the input note with `idx`. pub proc get_input_note_ptr - exec.constants::get_note_mem_size mul add.INPUT_NOTE_DATA_SECTION_OFFSET + push.NOTE_MEM_SIZE mul add.INPUT_NOTE_DATA_SECTION_OFFSET end #! Set the note id of the input note. @@ -1834,7 +1832,7 @@ end #! - i is the index of the output note. #! - ptr is the memory address of the data segment for output note i. pub proc get_output_note_ptr - exec.constants::get_note_mem_size mul add.OUTPUT_NOTE_SECTION_OFFSET + push.NOTE_MEM_SIZE mul add.OUTPUT_NOTE_SECTION_OFFSET end #! Returns the output note recipient. @@ -1922,7 +1920,7 @@ pub proc set_output_note_num_assets # => [note_ptr + offset, num_assets] # check note number of assets limit - dup.1 exec.constants::get_max_assets_per_note lt assert.err=ERR_NOTE_NUM_OF_ASSETS_EXCEED_LIMIT + dup.1 push.MAX_ASSETS_PER_NOTE lt assert.err=ERR_NOTE_NUM_OF_ASSETS_EXCEED_LIMIT mem_store end diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/note.masm b/crates/miden-protocol/asm/kernels/transaction/lib/note.masm index a1917db667..5a3faf962d 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/note.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/note.masm @@ -1,6 +1,6 @@ use miden::core::crypto::hashes::rpo256 -use $kernel::constants +use $kernel::constants::NOTE_MEM_SIZE use $kernel::memory # ERRORS @@ -31,7 +31,7 @@ pub proc increment_active_input_note_ptr # => [orig_input_note_ptr] # increment the pointer - exec.constants::get_note_mem_size add + push.NOTE_MEM_SIZE add # => [active_input_note_ptr] # set the active input note pointer to the incremented value diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/output_note.masm b/crates/miden-protocol/asm/kernels/transaction/lib/output_note.masm index 44a0076621..1da9d7eab2 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/output_note.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/output_note.masm @@ -2,7 +2,7 @@ use $kernel::account use $kernel::memory use $kernel::note use $kernel::asset -use $kernel::constants +use $kernel::constants::MAX_OUTPUT_NOTES_PER_TX use miden::core::word # CONSTANTS @@ -372,7 +372,7 @@ proc increment_num_output_notes # => [note_idx] # assert that there is space for a new note - dup exec.constants::get_max_num_output_notes lt + dup push.MAX_OUTPUT_NOTES_PER_TX lt assert.err=ERR_TX_NUMBER_OF_OUTPUT_NOTES_EXCEEDS_LIMIT # => [note_idx] diff --git a/crates/miden-protocol/asm/kernels/transaction/lib/prologue.masm b/crates/miden-protocol/asm/kernels/transaction/lib/prologue.masm index 9dc1a86926..615240d824 100644 --- a/crates/miden-protocol/asm/kernels/transaction/lib/prologue.masm +++ b/crates/miden-protocol/asm/kernels/transaction/lib/prologue.masm @@ -7,7 +7,13 @@ use $kernel::account use $kernel::account_delta use $kernel::account_id use $kernel::asset_vault -use $kernel::constants +use $kernel::constants::EMPTY_SMT_ROOT +use $kernel::constants::MAX_ASSETS_PER_NOTE +use $kernel::constants::MAX_INPUT_NOTES_PER_TX +use $kernel::constants::MAX_INPUTS_PER_NOTE +use $kernel::constants::NOTE_TREE_DEPTH +use $kernel::constants::STORAGE_SLOT_TYPE_MAP +use $kernel::constants::STORAGE_SLOT_TYPE_VALUE use $kernel::memory # CONSTS @@ -307,7 +313,7 @@ proc validate_new_account # => [ACCT_VAULT_ROOT] # push empty vault root onto stack - exec.constants::get_empty_smt_root + push.EMPTY_SMT_ROOT # => [EMPTY_VAULT_ROOT, ACCT_VAULT_ROOT] assert_eqw.err=ERR_PROLOGUE_NEW_ACCOUNT_VAULT_MUST_BE_EMPTY @@ -336,18 +342,18 @@ proc validate_new_account # => [slot_type] # assert the fungible faucet reserved slot type == value - exec.constants::get_storage_slot_type_value eq + push.STORAGE_SLOT_TYPE_VALUE eq assert.err=ERR_PROLOGUE_NEW_FUNGIBLE_FAUCET_RESERVED_SLOT_INVALID_TYPE # => [] else # assert the non-fungible faucet reserved slot is initialized correctly (root of # empty SMT) - exec.constants::get_empty_smt_root + push.EMPTY_SMT_ROOT assert_eqw.err=ERR_PROLOGUE_NEW_NON_FUNGIBLE_FAUCET_RESERVED_SLOT_MUST_BE_VALID_EMPTY_SMT # => [slot_type] # assert the non-fungible faucet reserved slot type == map - exec.constants::get_storage_slot_type_map eq + push.STORAGE_SLOT_TYPE_MAP eq assert.err=ERR_PROLOGUE_NEW_NON_FUNGIBLE_FAUCET_RESERVED_SLOT_INVALID_TYPE # => [] end @@ -560,7 +566,7 @@ proc authenticate_note # => [NOTE_COMMITMENT, note_index, NOTE_ROOT] # get the depth of the note tree - exec.constants::get_note_tree_depth movdn.4 + push.NOTE_TREE_DEPTH movdn.4 # => [NOTE_COMMITMENT, depth, note_index, NOTE_ROOT] # verify the note commitment @@ -655,7 +661,7 @@ proc process_note_inputs_length # => [inputs_len, note_ptr] # validate the input length - dup exec.::$kernel::util::note::get_max_inputs_per_note lte + dup push.MAX_INPUTS_PER_NOTE lte assert.err=ERR_PROLOGUE_NUMBER_OF_NOTE_INPUTS_EXCEEDED_LIMIT # => [inputs_len, note_ptr] @@ -684,7 +690,7 @@ proc process_note_assets adv_push.1 # => [assets_count, note_ptr] - dup exec.constants::get_max_assets_per_note lte + dup push.MAX_ASSETS_PER_NOTE lte assert.err=ERR_PROLOGUE_NUMBER_OF_NOTE_ASSETS_EXCEEDS_LIMIT # => [assets_count, note_ptr] @@ -978,7 +984,7 @@ proc process_input_notes_data # assert the number of input notes is within limits; since max number of input notes is # expected to be smaller than 2^32, we can use a more efficient u32 comparison dup - exec.constants::get_max_num_input_notes + push.MAX_INPUT_NOTES_PER_TX u32assert2.err=ERR_PROLOGUE_NUMBER_OF_INPUT_NOTES_EXCEEDS_LIMIT u32lte assert.err=ERR_PROLOGUE_NUMBER_OF_INPUT_NOTES_EXCEEDS_LIMIT # => [num_notes] diff --git a/crates/miden-protocol/asm/protocol/note.masm b/crates/miden-protocol/asm/protocol/note.masm index ea7ac724f4..284c68d4ea 100644 --- a/crates/miden-protocol/asm/protocol/note.masm +++ b/crates/miden-protocol/asm/protocol/note.masm @@ -33,8 +33,8 @@ const ERR_PROLOGUE_NOTE_INPUTS_LEN_EXCEEDED_LIMIT="number of note inputs exceede #! #! Invocation: exec pub proc compute_inputs_commitment - # check that number of inputs is less than or equal to 1024 - dup.1 push.1024 u32assert2.err=ERR_PROLOGUE_NOTE_INPUTS_LEN_EXCEEDED_LIMIT + # check that number of inputs is less than or equal to MAX_INPUTS_PER_NOTE + dup.1 push.MAX_INPUTS_PER_NOTE u32assert2.err=ERR_PROLOGUE_NOTE_INPUTS_LEN_EXCEEDED_LIMIT u32lte assert.err=ERR_PROLOGUE_NOTE_INPUTS_LEN_EXCEEDED_LIMIT # => [inputs_ptr, num_inputs] @@ -47,13 +47,8 @@ pub proc compute_inputs_commitment # => [INPUTS_COMMITMENT] end -#! Returns the max allowed number of input values per note. -#! -#! Stack: [] -#! Output: [max_inputs_per_note] -#! -#! - max_inputs_per_note is the max inputs per note. -pub use ::miden::protocol::util::note::get_max_inputs_per_note +# Re-export the max inputs per note constant. +pub use ::miden::protocol::util::note::MAX_INPUTS_PER_NOTE #! Writes the assets data stored in the advice map to the memory specified by the provided #! destination pointer. diff --git a/crates/miden-protocol/asm/shared_utils/util/note.masm b/crates/miden-protocol/asm/shared_utils/util/note.masm index 00a4ea88cf..8c502a8f71 100644 --- a/crates/miden-protocol/asm/shared_utils/util/note.masm +++ b/crates/miden-protocol/asm/shared_utils/util/note.masm @@ -2,18 +2,4 @@ # ================================================================================================= # The maximum number of input values associated with a single note. -const MAX_INPUTS_PER_NOTE=1024 - -# PROCEDURES -# ================================================================================================= - -#! Returns the max allowed number of input values per note. -#! -#! Inputs: [] -#! Outputs: [max_inputs_per_note] -#! -#! Where: -#! - max_inputs_per_note is the max inputs per note. -pub proc get_max_inputs_per_note - push.MAX_INPUTS_PER_NOTE -end +pub const MAX_INPUTS_PER_NOTE = 1024 diff --git a/crates/miden-testing/src/kernel_tests/tx/test_output_note.rs b/crates/miden-testing/src/kernel_tests/tx/test_output_note.rs index a7c48b76b5..9797291131 100644 --- a/crates/miden-testing/src/kernel_tests/tx/test_output_note.rs +++ b/crates/miden-testing/src/kernel_tests/tx/test_output_note.rs @@ -175,12 +175,12 @@ async fn test_create_note_too_many_notes() -> anyhow::Result<()> { let code = format!( " use miden::protocol::output_note - use $kernel::constants + use $kernel::constants::MAX_OUTPUT_NOTES_PER_TX use $kernel::memory use $kernel::prologue begin - exec.constants::get_max_num_output_notes + push.MAX_OUTPUT_NOTES_PER_TX exec.memory::set_num_output_notes exec.prologue::prepare_transaction