From b44c6c82124655a847d916c3f1cfcf80ecfa9fbe Mon Sep 17 00:00:00 2001 From: Paul-Henry Kajfasz Date: Fri, 26 Jul 2024 14:05:01 +0100 Subject: [PATCH] Updated validate_account_procedures to use pipe_double_words_to_memory --- miden-lib/asm/miden/kernels/tx/prologue.masm | 37 +++++++++----------- miden-lib/asm/miden/note.masm | 26 +++++++------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/miden-lib/asm/miden/kernels/tx/prologue.masm b/miden-lib/asm/miden/kernels/tx/prologue.masm index fda283715..bef9ad382 100644 --- a/miden-lib/asm/miden/kernels/tx/prologue.masm +++ b/miden-lib/asm/miden/kernels/tx/prologue.masm @@ -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 @@ -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 diff --git a/miden-lib/asm/miden/note.masm b/miden-lib/asm/miden/note.masm index e19f449fe..17456575f 100644 --- a/miden-lib/asm/miden/note.masm +++ b/miden-lib/asm/miden/note.masm @@ -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 #! @@ -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] @@ -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 @@ -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 @@ -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] @@ -239,7 +239,7 @@ 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] @@ -247,7 +247,7 @@ export.compute_inputs_hash 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] @@ -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