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
24 changes: 0 additions & 24 deletions crates/miden-agglayer/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use alloc::vec::Vec;

use miden_core::FieldElement;
use miden_protocol::Felt;
use primitive_types::U256;

// UTILITY FUNCTIONS
// ================================================================================================
Expand All @@ -18,25 +16,3 @@ pub fn felts_to_bytes(limbs: &[Felt]) -> Vec<u8> {
}
bytes
}

/// Convert a U256 value to an array of 8 Felt values (u32 limbs in little-endian order).
///
/// The U256 is stored as 4 u64 words in little-endian order. We split each u64 into two u32 limbs.
pub fn u256_to_felts(value: U256) -> [Felt; 8] {
let mut limbs = [Felt::ZERO; 8];
for i in 0..4 {
let word = value.0[i];
limbs[i * 2] = Felt::new(word as u32 as u64); // Low 32 bits
limbs[i * 2 + 1] = Felt::new((word >> 32) as u32 as u64); // High 32 bits
}
limbs
}

/// Convert an array of 8 Felt values (u32 limbs in little-endian order) to a U256 value.
pub fn felts_to_u256(felts: Vec<Felt>) -> U256 {
assert_eq!(felts.len(), 8, "expected exactly 8 felts");
let array: [Felt; 8] =
[felts[0], felts[1], felts[2], felts[3], felts[4], felts[5], felts[6], felts[7]];
let bytes = felts_to_bytes(&array);
U256::from_little_endian(&bytes)
}
27 changes: 19 additions & 8 deletions crates/miden-testing/tests/agglayer/asset_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use primitive_types::U256;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

use super::test_utils::{assert_execution_fails_with, execute_masm_script, stack_to_u256};
use super::test_utils::{assert_execution_fails_with, execute_masm_script};

// ================================================================================================
// SCALE UP TESTS (Felt -> U256)
Expand All @@ -25,7 +25,7 @@ use super::test_utils::{assert_execution_fails_with, execute_masm_script, stack_
async fn test_scale_up_helper(
miden_amount: Felt,
scale_exponent: Felt,
expected_result_u256: U256,
expected_result: EthAmount,
) -> anyhow::Result<()> {
let script_code = format!(
"
Expand All @@ -42,23 +42,34 @@ async fn test_scale_up_helper(
);

let exec_output = execute_masm_script(&script_code).await?;
let actual_result_u256 = stack_to_u256(&exec_output);
let actual_felts: Vec<Felt> = exec_output.stack[0..8].to_vec();

assert_eq!(actual_result_u256, expected_result_u256);
// to_elements() returns big-endian limb order with each limb byte-swapped (LE-interpreted
// from BE source bytes). The scale-up output is native u32 limbs in LE limb order, so we
// reverse the limbs and swap bytes within each u32 to match.
let expected_felts: Vec<Felt> = expected_result
.to_elements()
.into_iter()
.rev()
.map(|f| Felt::new((f.as_int() as u32).swap_bytes() as u64))
.collect();

assert_eq!(actual_felts, expected_felts);

Ok(())
}

#[tokio::test]
async fn test_scale_up_basic_examples() -> anyhow::Result<()> {
// Test case 1: amount=1, no scaling (scale_exponent=0)
test_scale_up_helper(Felt::new(1), Felt::new(0), U256::from(1u64)).await?;
test_scale_up_helper(Felt::new(1), Felt::new(0), EthAmount::from_uint_str("1").unwrap())
.await?;

// Test case 2: amount=1, scale to 1e18 (scale_exponent=18)
test_scale_up_helper(
Felt::new(1),
Felt::new(18),
U256::from_dec_str("1000000000000000000").unwrap(),
EthAmount::from_uint_str("1000000000000000000").unwrap(),
)
.await?;

Expand All @@ -71,15 +82,15 @@ async fn test_scale_up_realistic_amounts() -> anyhow::Result<()> {
test_scale_up_helper(
Felt::new(100_000_000),
Felt::new(12),
U256::from_dec_str("100000000000000000000").unwrap(),
EthAmount::from_uint_str("100000000000000000000").unwrap(),
)
.await?;

// Large amount: 1e18 units scaled by 8
test_scale_up_helper(
Felt::new(1000000000000000000),
Felt::new(8),
U256::from_dec_str("100000000000000000000000000").unwrap(),
EthAmount::from_uint_str("100000000000000000000000000").unwrap(),
)
.await?;

Expand Down
9 changes: 0 additions & 9 deletions crates/miden-testing/tests/agglayer/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ use miden_agglayer::{
LeafData,
MetadataHash,
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_protocol::Felt;
use miden_protocol::transaction::TransactionKernel;
use miden_protocol::utils::sync::LazyLock;
use miden_tx::utils::hex_to_bytes;
use primitive_types::U256;
use serde::Deserialize;

// SERDE HELPERS
Expand Down Expand Up @@ -239,9 +236,3 @@ pub async fn assert_execution_fails_with(script_code: &str, expected_error: &str
error_msg
);
}

/// Convert the top 8 u32 values from the execution stack to a U256
pub fn stack_to_u256(exec_output: &ExecutionOutput) -> U256 {
let felts: Vec<Felt> = exec_output.stack[0..8].to_vec();
utils::felts_to_u256(felts)
}