-
Notifications
You must be signed in to change notification settings - Fork 107
feat: add getLeafValue procedure
#2262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 3c84da6
fix: formatting
partylikeits1983 c71d9df
refactor: rm unnecessary indirection
partylikeits1983 779ab24
refactor: use crypto util functions
partylikeits1983 a8238b6
refactor: implement suggestions & refactor
partylikeits1983 5dd9c85
refactor: update logic & comments to little endian
partylikeits1983 99bcee7
Update crates/miden-agglayer/src/utils.rs
partylikeits1983 2d0a89a
refactor: improve EthAddress representation clarity and MASM alignment
partylikeits1983 3d45e7f
refactor: simplify ethereum_address_to_account_id proc
partylikeits1983 43cbcf3
fix: clippy
partylikeits1983 049e8be
fix: lint doc check
partylikeits1983 99161e3
refactor: use u32assert2
partylikeits1983 3dc29f6
refactor: simplify from_account_id() & u32 check
partylikeits1983 4c6289d
revert: undo drop addr4 in ethereum_address_to_account_id
partylikeits1983 a5f3309
Merge branch 'ajl-solidity-type-conversions' into ajl-agglayer-get-le…
partylikeits1983 576f907
feat: init getLeafValue() test
partylikeits1983 a8e35d3
feat: implement AdviceMap key based getLeafValue procedure
partylikeits1983 a1a1c3d
Update crates/miden-agglayer/src/eth_address.rs
partylikeits1983 359b3ef
refactor: update test name
partylikeits1983 1264d24
refactor: rename to EthAddressFormat
partylikeits1983 2288c0d
refactor: rearrange EthAddressFormat
partylikeits1983 d9c309a
refactor: rename file to eth_address_format
partylikeits1983 393ee03
Merge branch 'agglayer' into ajl-solidity-type-conversions
partylikeits1983 3c3c29e
fix: update script roots
partylikeits1983 a926316
Merge branch 'ajl-solidity-type-conversions' into ajl-agglayer-get-le…
mmagician af29827
chore: pipe words to memory
mmagician d6b9954
refactor: add stack comments
partylikeits1983 f9f2d57
chore: pipe words to memory instead of manual `adv_loadw`
partylikeits1983 d61f836
refactor: deduplicate execute_program_with_default_host
partylikeits1983 d51bed1
feat: add hardcoded expected hash to test
partylikeits1983 1388770
fix: verify hash matches commitment
mmagician f200752
fix: put data under correct key in advice map
mmagician 8ebdc7b
chore: merge agglayer
partylikeits1983 1567d89
fix: rm redundant file
partylikeits1983 c1aec4d
Merge branch 'agglayer' into ajl-agglayer-get-leaf-value
partylikeits1983 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,4 +157,3 @@ pub proc bridge_out | |
| exec.create_burn_note | ||
| # => [] | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
partylikeits1983 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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"); | ||
partylikeits1983 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_VALUEis just a sequential hash ofLEAF_DATAusing `Keccak hash function.