Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
malik672 committed Nov 27, 2024
1 parent ace7d84 commit 8b0c4ca
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions codegen/src/wasm/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,63 +101,66 @@ mod tests {
let addmod_func = HostFunc::try_from(("zinkc", "u256_addmod"));
assert!(addmod_func.is_ok());
let addmod_host_func = addmod_func.unwrap();

// Verify it maps to the correct EVM opcode
match addmod_host_func {
HostFunc::Evm(OpCode::ADDMOD) => {},
HostFunc::Evm(OpCode::ADDMOD) => {}
_ => panic!("Expected ADDMOD opcode"),
}

// Verify stack input/output for ADDMOD
assert_eq!(addmod_host_func.stack_in(), 3);
assert_eq!(addmod_host_func.stack_out(), 1);
assert_eq!(addmod_host_func.stack_in(), 3);
assert_eq!(addmod_host_func.stack_out(), 1);

// Test MULMOD host function conversion
let mulmod_func = HostFunc::try_from(("zinkc", "u256_mulmod"));
assert!(mulmod_func.is_ok());
let mulmod_host_func = mulmod_func.unwrap();

// Verify it maps to the correct EVM opcode
match mulmod_host_func {
HostFunc::Evm(OpCode::MULMOD) => {},
HostFunc::Evm(OpCode::MULMOD) => {}
_ => panic!("Expected MULMOD opcode"),
}

// Verify stack input/output for MULMOD
assert_eq!(mulmod_host_func.stack_in(), 3);
assert_eq!(mulmod_host_func.stack_out(), 1);
assert_eq!(mulmod_host_func.stack_in(), 3);
assert_eq!(mulmod_host_func.stack_out(), 1);
}

#[test]
fn test_addmod_mulmod_example_scenarios() {
fn modular_add(a: u64, b: u64, n: u64) -> u64 {
((a % n) + (b % n)) % n
}

let test_cases = [
(10u64, 20, 7, 2, 4),
(100, 200, 3, 0, 2),
(5, 6, 7, 4, 2),
(10, 11, 12, 9, 2)
(10u64, 20, 7, 2, 4),
(100, 200, 3, 0, 2),
(5, 6, 7, 4, 2),
(10, 11, 12, 9, 2),
];

for (a, b, n, expected_add, expected_mul) in test_cases.iter() {
let actual_add = modular_add(*a, *b, *n);
let actual_mul = (*a * *b) % *n;

println!(
"Debug: a={}, b={}, N={}, actual_add={}, expected_add={}",
a, b, n, actual_add, expected_add
);

assert_eq!(actual_add, *expected_add,

assert_eq!(
actual_add, *expected_add,
"ADDMOD failed for inputs {}, {}, {} (got {}, expected {})",
a, b, n, actual_add, expected_add);

assert_eq!(actual_mul, *expected_mul,
a, b, n, actual_add, expected_add
);

assert_eq!(
actual_mul, *expected_mul,
"MULMOD failed for inputs {}, {}, {} (got {}, expected {})",
a, b, n, actual_mul, expected_mul);
a, b, n, actual_mul, expected_mul
);
}
}

}
}

0 comments on commit 8b0c4ca

Please sign in to comment.