Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
46143c8
feat: add Solidity<>Miden address type conversion functions
partylikeits1983 Jan 9, 2026
3c84da6
fix: formatting
partylikeits1983 Jan 9, 2026
c71d9df
refactor: rm unnecessary indirection
partylikeits1983 Jan 9, 2026
779ab24
refactor: use crypto util functions
partylikeits1983 Jan 9, 2026
a8238b6
refactor: implement suggestions & refactor
partylikeits1983 Jan 9, 2026
5dd9c85
refactor: update logic & comments to little endian
partylikeits1983 Jan 9, 2026
99bcee7
Update crates/miden-agglayer/src/utils.rs
partylikeits1983 Jan 9, 2026
2d0a89a
refactor: improve EthAddress representation clarity and MASM alignment
partylikeits1983 Jan 9, 2026
3d45e7f
refactor: simplify ethereum_address_to_account_id proc
partylikeits1983 Jan 9, 2026
43cbcf3
fix: clippy
partylikeits1983 Jan 9, 2026
049e8be
fix: lint doc check
partylikeits1983 Jan 9, 2026
99161e3
refactor: use u32assert2
partylikeits1983 Jan 9, 2026
3dc29f6
refactor: simplify from_account_id() & u32 check
partylikeits1983 Jan 9, 2026
4c6289d
revert: undo drop addr4 in ethereum_address_to_account_id
partylikeits1983 Jan 9, 2026
a5f3309
Merge branch 'ajl-solidity-type-conversions' into ajl-agglayer-get-le…
partylikeits1983 Jan 12, 2026
576f907
feat: init getLeafValue() test
partylikeits1983 Jan 12, 2026
a8e35d3
feat: implement AdviceMap key based getLeafValue procedure
partylikeits1983 Jan 12, 2026
a1a1c3d
Update crates/miden-agglayer/src/eth_address.rs
partylikeits1983 Jan 13, 2026
359b3ef
refactor: update test name
partylikeits1983 Jan 13, 2026
1264d24
refactor: rename to EthAddressFormat
partylikeits1983 Jan 13, 2026
2288c0d
refactor: rearrange EthAddressFormat
partylikeits1983 Jan 13, 2026
d9c309a
refactor: rename file to eth_address_format
partylikeits1983 Jan 13, 2026
393ee03
Merge branch 'agglayer' into ajl-solidity-type-conversions
partylikeits1983 Jan 14, 2026
3c3c29e
fix: update script roots
partylikeits1983 Jan 14, 2026
a926316
Merge branch 'ajl-solidity-type-conversions' into ajl-agglayer-get-le…
mmagician Jan 14, 2026
af29827
chore: pipe words to memory
mmagician Jan 14, 2026
d6b9954
refactor: add stack comments
partylikeits1983 Jan 14, 2026
f9f2d57
chore: pipe words to memory instead of manual `adv_loadw`
partylikeits1983 Jan 14, 2026
d61f836
refactor: deduplicate execute_program_with_default_host
partylikeits1983 Jan 14, 2026
d51bed1
feat: add hardcoded expected hash to test
partylikeits1983 Jan 14, 2026
1388770
fix: verify hash matches commitment
mmagician Jan 15, 2026
f200752
fix: put data under correct key in advice map
mmagician Jan 15, 2026
8ebdc7b
chore: merge agglayer
partylikeits1983 Jan 19, 2026
1567d89
fix: rm redundant file
partylikeits1983 Jan 20, 2026
c1aec4d
Merge branch 'agglayer' into ajl-agglayer-get-leaf-value
partylikeits1983 Jan 21, 2026
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
2 changes: 1 addition & 1 deletion crates/miden-agglayer/asm/bridge/bridge_in.masm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
#! Invocation: call
pub proc check_claim_proof
exec.get_rollup_exit_root
# => [GER_ROOT[8], CLAIM_NOTE_RPO_COMMITMENT]
# => [GER_ROOT[8], PROOF_DATA_KEY, LEAF_DATA_KEY]

# Check CLAIM note proof data against current GER
exec.crypto_utils::verify_claim_proof
Expand Down
1 change: 0 additions & 1 deletion crates/miden-agglayer/asm/bridge/bridge_out.masm
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,3 @@ pub proc bridge_out
exec.create_burn_note
# => []
end

55 changes: 33 additions & 22 deletions crates/miden-agglayer/asm/bridge/crypto_utils.masm
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
use miden::core::crypto::hashes::keccak256
use miden::core::mem

#! Given the leaf data returns the leaf value.
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: might have been good to explain somewhere (briefly) that LFEAF_VALUE is just a sequential hash of LEAF_DATA using `Keccak hash function.

#!
#! Inputs: [leaf_type, origin_network, ORIGIN_ADDRESS, destination_network, DESTINATION_ADDRESS, amount, METADATA_HASH]
#! Inputs:
#! Operand stack: [LEAF_DATA_KEY]
#! Advice map: {
#! LEAF_DATA_KEY => [
#! originNetwork[1], // Origin network identifier (1 felt, uint32)
#! originTokenAddress[5], // Origin token address (5 felts, address as 5 u32 felts)
#! destinationNetwork[1], // Destination network identifier (1 felt, uint32)
#! 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)
#! ],
#! }
#! Outputs: [LEAF_VALUE]
#!
#! Where:
#! - leaf_type is the leaf type: [0] transfer Ether / ERC20 tokens, [1] message.
#! - origin_network is the origin network identifier.
#! - ORIGIN_ADDRESS is the origin token address (5 elements)
#! - destination_network is the destination network identifier.
#! - DESTINATION_ADDRESS is the destination address (5 elements).
#! - amount is the amount: [0] Amount of tokens/ether, [1] Amount of ether.
#! - METADATA_HASH is the hash of the metadata (8 elements).
#! - LEAF_VALUE is the computed leaf value (8 elements).
#!
#! This function computes the keccak256 hash of the abi.encodePacked data.
#!
#! Invocation: exec
pub proc get_leaf_value
# TODO: implement getLeafValue()
# https://github.com/agglayer/agglayer-contracts/blob/e468f9b0967334403069aa650d9f1164b1731ebb/contracts/v2/lib/DepositContractV2.sol#L22
adv.push_mapval
# => [LEAF_DATA_KEY]

# stubbed out:
push.1.1.1.1
push.1.1.1.1

# exec.keccak256::hash_bytes
push.LEAF_DATA_START_PTR push.LEAF_DATA_NUM_WORDS
exec.mem::pipe_preimage_to_memory 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

Expand All @@ -36,7 +47,7 @@ end
#! and that the leaf has not been previously claimed.
#!
#! Inputs:
#! Operand stack: [GER_ROOT[8], CLAIM_PROOF_RPO_COMMITMENT, pad(12)]
#! Operand stack: [GER_ROOT[8], PROOF_DATA_KEY, LEAF_DATA_KEY, pad(12)]
#! Advice map: {
#! PROOF_DATA_KEY => [
#! smtProofLocalExitRoot[256], // SMT proof for local exit root (256 felts, bytes32[_DEPOSIT_CONTRACT_TREE_DEPTH])
Expand Down
34 changes: 6 additions & 28 deletions crates/miden-testing/tests/agglayer/asset_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use alloc::sync::Arc;
use miden_agglayer::{agglayer_library, utils};
use miden_assembly::{Assembler, DefaultSourceManager};
use miden_core_lib::CoreLibrary;
use miden_processor::fast::{ExecutionOutput, FastProcessor};
use miden_processor::{AdviceInputs, DefaultHost, ExecutionError, Program, StackInputs};
use miden_processor::fast::ExecutionOutput;
use miden_protocol::Felt;
use miden_protocol::transaction::TransactionKernel;
use primitive_types::U256;

use super::test_utils::execute_program_with_default_host;

/// Convert a Vec<Felt> to a U256
fn felts_to_u256(felts: Vec<Felt>) -> U256 {
assert_eq!(felts.len(), 8, "expected exactly 8 felts");
Expand All @@ -26,28 +26,6 @@ fn stack_to_u256(exec_output: &ExecutionOutput) -> U256 {
felts_to_u256(felts)
}

/// Execute a program with default host
async fn execute_program_with_default_host(
program: Program,
) -> Result<ExecutionOutput, ExecutionError> {
let mut host = DefaultHost::default();

let test_lib = TransactionKernel::library();
host.load_library(test_lib.mast_forest()).unwrap();

let std_lib = CoreLibrary::default();
host.load_library(std_lib.mast_forest()).unwrap();

let asset_conversion_lib = agglayer_library();
host.load_library(asset_conversion_lib.mast_forest()).unwrap();

let stack_inputs = StackInputs::new(vec![]).unwrap();
let advice_inputs = AdviceInputs::default();

let processor = FastProcessor::new_debug(stack_inputs.as_slice(), advice_inputs);
processor.execute(&program, &mut host).await
}

/// Helper function to test convert_felt_to_u256_scaled with given parameters
async fn test_convert_to_u256_helper(
miden_amount: Felt,
Expand Down Expand Up @@ -79,7 +57,7 @@ async fn test_convert_to_u256_helper(
.assemble_program(&script_code)
.unwrap();

let exec_output = execute_program_with_default_host(program).await?;
let exec_output = execute_program_with_default_host(program, None).await?;

// Extract the first 8 u32 values from the stack (the U256 representation)
let actual_result: [u32; 8] = [
Expand Down Expand Up @@ -156,7 +134,7 @@ async fn test_convert_to_u256_scaled_eth() -> anyhow::Result<()> {
.assemble_program(&script_code)
.unwrap();

let exec_output = execute_program_with_default_host(program).await?;
let exec_output = execute_program_with_default_host(program, None).await?;

let expected_result = U256::from_dec_str("100000000000000000000").unwrap();
let actual_result = stack_to_u256(&exec_output);
Expand Down Expand Up @@ -199,7 +177,7 @@ async fn test_convert_to_u256_scaled_large_amount() -> anyhow::Result<()> {
.assemble_program(&script_code)
.unwrap();

let exec_output = execute_program_with_default_host(program).await?;
let exec_output = execute_program_with_default_host(program, None).await?;

let expected_result = U256::from_dec_str("100000000000000000000000000").unwrap();
let actual_result = stack_to_u256(&exec_output);
Expand Down
146 changes: 146 additions & 0 deletions crates/miden-testing/tests/agglayer/crypto_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
extern crate alloc;

use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;

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::AdviceInputs;
use miden_protocol::{Felt, Hasher, Word};

use super::test_utils::execute_program_with_default_host;

/// Convert bytes to field elements (u32 words packed into felts)
fn bytes_to_felts(data: &[u8]) -> Vec<Felt> {
let mut felts = Vec::new();

// Pad data to multiple of 4 bytes
let mut padded_data = data.to_vec();
while !padded_data.len().is_multiple_of(4) {
padded_data.push(0);
}

// Convert to u32 words in little-endian format
for chunk in padded_data.chunks(4) {
let u32_value = u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
felts.push(Felt::new(u32_value as u64));
}

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

felts
}

fn u32_words_to_solidity_bytes32_hex(words: &[u64]) -> String {
assert_eq!(words.len(), 8, "expected 8 u32 words = 32 bytes");
let mut out = [0u8; 32];

for (i, &w) in words.iter().enumerate() {
let le = (w as u32).to_le_bytes();
out[i * 4..i * 4 + 4].copy_from_slice(&le);
}

let mut s = String::from("0x");
for b in out {
s.push_str(&format!("{:02x}", b));
}
s
}

// Helper: parse 0x-prefixed hex into a fixed-size byte array
fn hex_to_fixed<const N: usize>(s: &str) -> [u8; N] {
let s = s.strip_prefix("0x").unwrap_or(s);
assert_eq!(s.len(), N * 2, "expected {} hex chars", N * 2);
let mut out = [0u8; N];
for i in 0..N {
out[i] = u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap();
}
out
}

#[tokio::test]
async fn test_keccak_hash_get_leaf_value() -> anyhow::Result<()> {
let agglayer_lib = agglayer_library();

// === Values from hardhat test ===
let leaf_type: u8 = 0;
let origin_network: u32 = 0;
let token_address: [u8; 20] = hex_to_fixed("0x1234567890123456789012345678901234567890");
let destination_network: u32 = 1;
let destination_address: [u8; 20] = hex_to_fixed("0x0987654321098765432109876543210987654321");
let amount_u64: u64 = 1; // 1e19
let metadata_hash: [u8; 32] =
hex_to_fixed("0x2cdc14cacf6fec86a549f0e4d01e83027d3b10f29fa527c1535192c1ca1aac81");

// Expected hash value from Solidity implementation
let expected_hash = "0xf6825f6c59be2edf318d7251f4b94c0e03eb631b76a0e7b977fd8ed3ff925a3f";

// abi.encodePacked(
// uint8, uint32, address, uint32, address, uint256, bytes32
// )
let mut amount_u256_be = [0u8; 32];
amount_u256_be[24..32].copy_from_slice(&amount_u64.to_be_bytes());

let mut input_u8 = Vec::with_capacity(113);
input_u8.push(leaf_type);
input_u8.extend_from_slice(&origin_network.to_be_bytes());
input_u8.extend_from_slice(&token_address);
input_u8.extend_from_slice(&destination_network.to_be_bytes());
input_u8.extend_from_slice(&destination_address);
input_u8.extend_from_slice(&amount_u256_be);
input_u8.extend_from_slice(&metadata_hash);

let len_bytes = input_u8.len();
assert_eq!(len_bytes, 113);

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 = Hasher::hash_elements(&input_felts);
let advice_inputs = AdviceInputs::default().with_map(vec![(key, input_felts)]);

let source = format!(
r#"
use miden::core::sys
use miden::core::crypto::hashes::keccak256
use miden::agglayer::crypto_utils

begin
push.{key}

exec.crypto_utils::get_leaf_value
exec.sys::truncate_stack
end
"#
);

let program = Assembler::new(Arc::new(DefaultSourceManager::default()))
.with_dynamic_library(CoreLibrary::default())
.unwrap()
.with_dynamic_library(agglayer_lib.clone())
.unwrap()
.assemble_program(&source)
.unwrap();

let exec_output = execute_program_with_default_host(program, Some(advice_inputs)).await?;

let digest: Vec<u64> = exec_output.stack[0..8].iter().map(|f| f.as_int()).collect();
let hex_digest = u32_words_to_solidity_bytes32_hex(&digest);

let keccak256_digest: Vec<u64> = preimage.digest().as_ref().iter().map(Felt::as_int).collect();
let keccak256_hex_digest = u32_words_to_solidity_bytes32_hex(&keccak256_digest);

assert_eq!(digest, keccak256_digest);
assert_eq!(hex_digest, keccak256_hex_digest);
assert_eq!(hex_digest, expected_hash);
Ok(())
}
2 changes: 2 additions & 0 deletions crates/miden-testing/tests/agglayer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod asset_conversion;
mod bridge_in;
mod bridge_out;
mod crypto_utils;
mod solidity_miden_address_conversion;
pub mod test_utils;
35 changes: 35 additions & 0 deletions crates/miden-testing/tests/agglayer/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extern crate alloc;

use miden_agglayer::agglayer_library;
use miden_core_lib::CoreLibrary;
use miden_processor::fast::{ExecutionOutput, FastProcessor};
use miden_processor::{AdviceInputs, DefaultHost, ExecutionError, Program, StackInputs};
use miden_protocol::transaction::TransactionKernel;

/// Execute a program with default host and optional advice inputs
pub async fn execute_program_with_default_host(
program: Program,
advice_inputs: Option<AdviceInputs>,
) -> Result<ExecutionOutput, ExecutionError> {
let mut host = DefaultHost::default();

let test_lib = TransactionKernel::library();
host.load_library(test_lib.mast_forest()).unwrap();

let std_lib = CoreLibrary::default();
host.load_library(std_lib.mast_forest()).unwrap();

// Register handlers from std_lib
for (event_name, handler) in std_lib.handlers() {
host.register_handler(event_name, handler)?;
}

let agglayer_lib = agglayer_library();
host.load_library(agglayer_lib.mast_forest()).unwrap();

let stack_inputs = StackInputs::new(vec![]).unwrap();
let advice_inputs = advice_inputs.unwrap_or_default();

let processor = FastProcessor::new_debug(stack_inputs.as_slice(), advice_inputs);
processor.execute(&program, &mut host).await
}