Skip to content

Commit

Permalink
Added validate_storage_sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive committed Sep 18, 2024
1 parent 2e59840 commit 4f9a9d3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 29 deletions.
56 changes: 46 additions & 10 deletions miden-lib/asm/kernels/transaction/lib/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export.validate_storage_offsets
if.true
while.true
# get storage offset from memory
dup exec.get_procedure_storage_offset
dup exec.get_procedure_metadata swap drop
# => [storage_offset, index, num_storage_slots, num_account_procedures]

# assert that storage offset is not 0
Expand Down Expand Up @@ -384,7 +384,7 @@ export.validate_storage_offsets
else
while.true
# get storage offset from memory
dup exec.get_procedure_storage_offset
dup exec.get_procedure_storage_metadata swap drop
# => [storage_offset, index, num_storage_slots, num_account_procedures]

# assert that storage offset is in bounds
Expand All @@ -402,6 +402,41 @@ export.validate_storage_offsets
# => []
end

#! Validates all account procedures storage sizes by
#! checking that all storage offsets are in bounds
#!
#! Stack: []
#! Output: []
export.validate_storage_sizes
# get number of account procedures and number of storage slots
exec.memory::get_num_account_procedures exec.memory::get_num_storage_slots
# => [num_storage_slots, num_account_procedures]

# prepare stack for looping
push.0.1
# => [start_loop, index, num_storage_slots, num_account_procedures]

# we do not check if num_account_procedures == 0 here because a valid
# account has between 1 and 256 procedures with associated offsets
while.true
# get storage size from memory
dup exec.get_procedure_metadata add
# => [storage_limit, index, num_storage_slots, num_account_procedures]

# assert that storage limit is in bounds
dup.2 lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_storage_slots, get_num_account_procedures]

# check if we should continue looping
add.1 dup dup.3 lt
# => [should_loop, index, num_storage_slots, num_account_procedures]
end

# clean stack
drop drop drop
# => []
end

#! Gets an item from the account storage
#!
#! Note:
Expand Down Expand Up @@ -718,21 +753,22 @@ proc.set_item_raw
# => [OLD_VALUE]
end

#! Returns the procedure storage offset
#! Returns the procedure metadata
#!
#! Note:
#! - We assume that index has been validated and is within bounds
#!
#! Stack: [index, ...]
#! Output: [storage_offset, ...]
#! Stack: [index]
#! Output: [storage_offset, storage_size]
#!
#! - storage_offset is the procedure storage offset.
proc.get_procedure_storage_offset
# get procedure storage offset pointer
#! - storage_size is the procedure storage size.
proc.get_procedure_metadata
# get procedure storage metadata pointer
mul.2 exec.memory::get_acct_procedures_section_offset add add.1
# => [storage_offset_ptr]

# load procedure storage offset from memory
mem_load
# => [storage_offset]
# load procedure storage offset from memory and keep relevant data
mem_loadw swap drop swap drop
# => [storage_offset, storage_size]
end
39 changes: 20 additions & 19 deletions miden-lib/asm/kernels/transaction/lib/prologue.masm
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ end
# =================================================================================================


#! Saves the procedure hashes of the chosen kernel to memory. Verifies that kernel root and kernel
#! hash match the sequential hash of all kernels and sequential hash of kernel procedures
#! respectively.
#!
#! Saves the procedure hashes of the chosen kernel to memory. Verifies that kernel root and kernel
#! hash match the sequential hash of all kernels and sequential hash of kernel procedures
#! respectively.
#!
#! Inputs:
#! Operand stack: []
#! Advice stack: [kernel_version]
Expand All @@ -119,15 +119,15 @@ end
#! Outputs:
#! Operand stack: []
#! Advice stack: []
#!
#!
#! Where:
#! - kernel_version, index of the desired kernel in the array of all kernels available for the
#! - kernel_version, index of the desired kernel in the array of all kernels available for the
#! current transaction
#! - KERNEL_ROOT, accumulative hash from all kernel hashes.
#! - [KERNEL_HASHES], array of each kernel hash
#! - [KERNEL_PROCEDURE_HASHES], array of procedure hashes of the current kernel
proc.process_kernel_data
# move the kernel offset to the operand stack
# move the kernel offset to the operand stack
adv_push.1
# OS => [kernel_version]
# AS => []
Expand All @@ -137,20 +137,20 @@ proc.process_kernel_data
# OS => [KERNEL_ROOT, kernel_version]
# AS => []

# push the kernel hashes from the advice map to the advice stack
# push the kernel hashes from the advice map to the advice stack
adv.push_mapvaln
# OS => [KERNEL_ROOT, kernel_version]
# AS => [len_felts, [KERNEL_HASHES]]

# move the number of felt elements in the [KERNEL_HASHES] array to the stack and get the
# move the number of felt elements in the [KERNEL_HASHES] array to the stack and get the
# number of Words from it
adv_push.1 div.4
# OS => [len_words, KERNEL_ROOT, kernel_version]
# AS => [[KERNEL_HASHES]]

# get the pointer to the memory where kernel hashes will be stored
# Note: for now we use the same address for kernel hash and for kernel procedures since there is
# only one kernel and its hash will be overwritten by the procedures anyway.
# only one kernel and its hash will be overwritten by the procedures anyway.
exec.memory::get_kernel_procedures_ptr swap
# OS => [len_words, kernel_mem_ptr, KERNEL_ROOT, kernel_version]
# AS => [[KERNEL_HASHES]]
Expand All @@ -166,20 +166,20 @@ proc.process_kernel_data
# AS => []

# get the hash of the kernel which will be used in the current transaction
exec.memory::get_kernel_procedures_ptr add
exec.memory::get_kernel_procedures_ptr add
# OS => [kernel_ptr]
# AS => []

padw movup.4 mem_loadw
# OS => [KERNEL_HASH]
# AS => []

# push the procedure hashes of the chosen kernel from the advice map to the advice stack
# push the procedure hashes of the chosen kernel from the advice map to the advice stack
adv.push_mapvaln
# OS => [KERNEL_HASH]
# AS => [len_felts, [PROC_HASHES]]

# move the number of felt elements in the [PROC_HASHES] array to the stack and get the
# move the number of felt elements in the [PROC_HASHES] array to the stack and get the
# number of Words from it
adv_push.1 div.4
# OS => [len_words, KERNEL_HASH]
Expand Down Expand Up @@ -612,8 +612,9 @@ proc.process_account_data
# => []
end

# validate account procedure storage offsets
# validate account procedure metadata
exec.account::validate_storage_offsets
exec.account::validate_storage_sizes
end

# INPUT NOTES DATA
Expand Down Expand Up @@ -1161,10 +1162,10 @@ end
#! - Any of the input notes do note exist in the note db.
#!
#! Operand stack: [
#! BLOCK_HASH,
#! account_id,
#! INITIAL_ACCOUNT_HASH,
#! INPUT_NOTES_COMMITMENT,
#! BLOCK_HASH,
#! account_id,
#! INITIAL_ACCOUNT_HASH,
#! INPUT_NOTES_COMMITMENT,
#! ]
#! Advice stack: [
#! PREVIOUS_BLOCK_HASH,
Expand Down Expand Up @@ -1208,7 +1209,7 @@ end
#! - version, the current protocol version.
#! - timestamp, the current timestamp.
#! - NOTE_ROOT, root of the tree with all notes created in the block.
#! - kernel_version, index of the desired kernel in the array of all kernels available for the
#! - kernel_version, index of the desired kernel in the array of all kernels available for the
#! current transaction.
#! - account_nonce, account's nonce.
#! - ACCOUNT_VAULT_ROOT, account's vault root.
Expand Down

0 comments on commit 4f9a9d3

Please sign in to comment.