Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 13 additions & 36 deletions crates/miden-agglayer/asm/bridge/crypto_utils.masm
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use miden::core::crypto::hashes::keccak256
use miden::core::mem

const LEAF_DATA_BYTES = 113
const LEAF_DATA_NUM_WORDS = 8
const LEAF_DATA_START_PTR = 0

#! Given the leaf data key returns the leaf value.
#!
Expand All @@ -14,54 +17,28 @@ const LEAF_DATA_BYTES = 113
#! destinationAddress[5], // Destination address (5 felts, address as 5 u32 felts)
#! amount[8], // Amount of tokens (8 felts, uint256 as 8 u32 felts)
#! metadata[8], // ABI encoded metadata (8 felts, fixed size)
#! EMPTY_WORD // padding
#! ],
#! }
#! Outputs: [LEAF_VALUE]
#!
#! Invocation: exec
pub proc get_leaf_value
adv.push_mapval dropw
# => [LEAF_DATA_KEY]

adv.push_mapval
# => [len, LEAF_DATA_KEY]

dropw
# => []

# @dev what should the starting mem ptr be?
# writing AdviceStack into memory starting at mem address 0
push.0
repeat.7
# => [loc_ptr]

padw
# => [EMPTY_WORD, loc_ptr]

adv_loadw
# => [VALS, loc_ptr]

movup.4 dup movdn.5
# => [loc_ptr, VALS, loc_ptr]

mem_storew_be dropw
# => [loc_ptr]

add.4
# => [loc_ptr+4]
end
# => [loc_ptr]

adv_push.1 swap
# => [loc_ptr, data]

mem_store
push.LEAF_DATA_START_PTR push.LEAF_DATA_NUM_WORDS
exec.mem::pipe_words_to_memory dropw dropw dropw drop
Comment on lines +29 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function looks great! I added some stack comments, I think it should look like this:

pub proc get_leaf_value
    adv.push_mapval dropw
    # => [LEAF_DATA_KEY]

    push.LEAF_DATA_START_PTR push.LEAF_DATA_NUM_WORDS
    exec.mem::pipe_words_to_memory dropw dropw dropw drop
    # => []

    push.LEAF_DATA_BYTES push.LEAF_DATA_START_PTR
    # => [start_ptr, byte_len]

    exec.keccak256::hash_bytes
    # => [LEAF_VALUE[8]]

    # truncate stack
    swapdw dropw dropw
    # => [LEAF_VALUE[8]]
end

# => []

push.LEAF_DATA_BYTES.0
# => [mem_ptr, len_bytes]
push.LEAF_DATA_BYTES push.LEAF_DATA_START_PTR
# => [start_ptr, byte_len]

exec.keccak256::hash_bytes
# => [LEAF_VALUE[8]]

# truncate stack
swapdw dropw dropw
# => [LEAF_VALUE[8]]
end

#! Verify leaf and checks that it has not been claimed.
Expand Down
7 changes: 7 additions & 0 deletions crates/miden-testing/tests/agglayer/crypto_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use miden_agglayer::agglayer_library;
use miden_assembly::{Assembler, DefaultSourceManager};
use miden_core_lib::CoreLibrary;
use miden_core_lib::handlers::keccak256::KeccakPreimage;
use miden_crypto::FieldElement;
use miden_processor::fast::{ExecutionOutput, FastProcessor};
use miden_processor::{AdviceInputs, DefaultHost, ExecutionError, Program, StackInputs};
use miden_protocol::transaction::TransactionKernel;
Expand Down Expand Up @@ -55,6 +56,11 @@ fn bytes_to_felts(data: &[u8]) -> Vec<Felt> {
felts.push(Felt::new(word as u64));
}

// pad to next multiple of 4 felts
while felts.len() % 4 != 0 {
felts.push(Felt::ZERO);
}

felts
}

Expand Down Expand Up @@ -119,6 +125,7 @@ async fn test_keccak_hash_get_leaf_value() -> anyhow::Result<()> {

let preimage = KeccakPreimage::new(input_u8.clone());
let input_felts = bytes_to_felts(&input_u8);
assert_eq!(input_felts.len(), 32);

// Arbitrary key to store input in advice map (in prod this is RPO(input_felts))
let key: Word = [Felt::new(1), Felt::new(1), Felt::new(1), Felt::new(1)].into();
Expand Down
Loading