Skip to content

Commit

Permalink
Updated validate_account_procedures to use pipe_double_words_to_memory
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive committed Jul 26, 2024
1 parent 4d9eaf0 commit b44c6c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
37 changes: 16 additions & 21 deletions miden-lib/asm/miden/kernels/tx/prologue.masm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use.std::collections::smt
use.std::mem
use.std::collections::mmr
use.std::crypto::hashes::native
use.std::collections::smt

use.miden::kernels::tx::account
use.miden::kernels::tx::asset_vault
Expand Down Expand Up @@ -401,31 +402,25 @@ proc.validate_account_procedures
adv.push_mapval adv_push.1 dup exec.memory::set_num_account_procedures
# => [num_procs, CODE_COMMITMENT]

# get counter and account procedures memory location
push.0 exec.memory::get_account_procedures_section_offset
# => [location, counter, len, CODE_COMMITMENT]
# setup acct_proc_offset and end_ptr for reading from advice stack
mul.2 exec.memory::get_account_procedures_section_offset dup movdn.2 add swap
# => [acct_proc_offset, end_ptr, CODE_COMMITMENT]

# prepare stack for looping
padw padw padw push.1
# => [PAD, PAD, PAD, 1, location, counter, len, CODE_COMMITMENT]

while.true
# pipe elements from advice map to stack and memory and hash them
adv_pipe hperm
# => [HASH, HASH, HASH, location, counter, len, CODE_COMMITMENT]
# pad stack before reading from advice stack
padw padw padw
# => [PAD, PAD, PAD, acct_proc_offset, end_ptr, CODE_COMMITMENT]

# check if all account procedures have been piped
movup.13 add.1 dup movdn.14 dup.15 neq
# => [should_loop, HASH, HASH, HASH, location, counter, len, CODE_COMMITMENT]
end
# read the data from advice stack to memory and hash
exec.mem::pipe_double_words_to_memory
# => [PERM, PERM, PERM, end_ptr, CODE_COMMITMENT]

# keep relevant hash
# extract the digest
exec.native::state_to_digest
# => [HASH, location, counter, len, CODE_COMMITMENT]
# => [DIGEST, end_ptr, CODE_COMMITMENT]

# drop memory location, counter and number of procedures
movup.4 movup.5 movup.6 drop drop drop
# => [HASH, CODE_COMMITMENT]
# drop end_ptr
movup.4 drop
# => [DIGEST, CODE_COMMITMENT]

# verify hashed account procedures match account code commitment
assert_eqw.err=ERR_ACCT_CODE_HASH_MISMATCH
Expand Down
26 changes: 13 additions & 13 deletions miden-lib/asm/miden/note.masm
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ end

#! Computes hash of note inputs starting at the specified memory address.
#!
#! This procedure divides the hashing process into two parts: hashing pairs of words using
#! This procedure divides the hashing process into two parts: hashing pairs of words using
#! `hash_memory_even` procedure and hashing the remaining values using the `hperm` instruction.
#!
#! If the number if inputs is 0, procedure returns the empty word: [0, 0, 0, 0].
#!
#! Inputs: [inputs_ptr, num_inputs]
#! Outputs: [HASH]
#! Cycles:
#! Cycles:
#! - If number of elements divides by 8: 56 cycles + 3 * words
#! - Else: 189 cycles + 3 * words
#!
Expand All @@ -163,7 +163,7 @@ export.compute_inputs_hash
# check that number of inputs is less than 128
dup.1 push.128 u32assert2 u32lte assert

# move number of inputs to the top of the stack
# move number of inputs to the top of the stack
swap
# => [num_inputs, inputs_ptr]

Expand All @@ -178,9 +178,9 @@ export.compute_inputs_hash
# get the padding flag to add it to the capacity part
dup.2 eq.0 not
# => [pad_flag, inputs_ptr, end_addr, num_inputs%8]
# prepare hasher state for RPO permutation
push.0.0.0 padw padw

# prepare hasher state for RPO permutation
push.0.0.0 padw padw
# => [C, B, A, inputs_ptr, end_addr, num_inputs%8]

# hash every pair of words
Expand Down Expand Up @@ -210,7 +210,7 @@ export.compute_inputs_hash
# => [E, D, A', drop_counter]

### 0th value ########################################################

# if current value is the last value to drop ("cycle" number equals to the number of values
# to drop), push 1 instead of 0 to the stack
dup.12 eq.1 swap
Expand All @@ -220,9 +220,9 @@ export.compute_inputs_hash
# => [e_2, e_1, e_0, d_3, d_2, d_1, 0/1, d_0, A', drop_counter]

### 1st value ########################################################

# prepare the second element of the E Word for cdrop instruction
# if current value is the last value to drop ("cycle" number equals to the number of values
# if current value is the last value to drop ("cycle" number equals to the number of values
# to drop), push 1 instead of 0 to the stack
dup.12 eq.2 swap
# => [e_2, 0, e_1, e_0, d_3, d_2, d_1, 0/1, d_0, A', drop_counter]
Expand All @@ -239,15 +239,15 @@ export.compute_inputs_hash
or
# => [latch', e_2, 0, e_1, e_0, d_3, d_2, d_1, 0, d_0, A', drop_counter]

# save the latch value
# save the latch value
dup movdn.14
# => [latch', e_2, 0, e_1, e_0, d_3, d_2, d_1, 0, d_0, A', latch', drop_counter]

# if latch == 1, drop 0; otherwise drop e_1
cdrop
# => [e_2_or_0, e_1, e_0, d_3, d_2, d_1, 0, d_0, A', latch', drop_counter]

# move the calculated value down the stack
# move the calculated value down the stack
movdn.6
# => [e_1, e_0, d_3, d_2, d_1, 0, e_2_or_0, d_0, A', latch', drop_counter]

Expand Down Expand Up @@ -296,8 +296,8 @@ export.compute_inputs_hash
# => [0, e_2_or_0, e_1_or_0, e_0_or_0, d_3_or_0, d_2_or_0, d_1_or_0, d_0, A']
# or in other words
# => [C, B, A', ... ]
# notice that we don't need to check the d_0 value: entering the else branch means that
# we have number of elements not divisible by 8, so we will have at least one element to
# notice that we don't need to check the d_0 value: entering the else branch means that
# we have number of elements not divisible by 8, so we will have at least one element to
# hash here (which turns out to be d_0)

hperm
Expand Down

0 comments on commit b44c6c8

Please sign in to comment.