diff --git a/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm b/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm index 780473b9..fd54fe2a 100644 --- a/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm +++ b/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm @@ -1,39 +1,22 @@ /// Division modulo 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47, the BN254 base field order -/// To replace with more efficient method using non-determinism later. // Returns y * (x^-1) where the inverse is taken modulo N %macro moddiv - // stack: x, y + // stack: x , y %inverse // stack: x^-1, y - %mulmodn -%endmacro - -%macro mulmodn - // stack: x, y - %bn_base - // stack: N, x, y - SWAP2 - // stack: y, x, N - MULMOD -%endmacro - -%macro squaremodn - // stack: x - DUP1 - // stack: x, x - %mulmodn + MULFP254 %endmacro // Non-deterministically provide the inverse modulo N. %macro inverse // stack: x PROVER_INPUT(ff::bn254_base::inverse) - // stack: x^-1, x - %stack (inv, x) -> (inv, x, @BN_BASE, inv) - // stack: x^-1, x, N, x^-1 - MULMOD + // stack: x^-1 , x + SWAP1 DUP2 + // stack: x^-1 , x, x^-1 + MULFP254 // stack: x^-1 * x, x^-1 %assert_eq_const(1) - // stack: x^-1 + // stack: x^-1 %endmacro diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 3871db84..b63911ed 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -1,6 +1,7 @@ //! An EVM interpreter for testing and debugging purposes. use std::collections::HashMap; +use std::str::FromStr; use anyhow::{anyhow, bail, ensure}; use ethereum_types::{U256, U512}; @@ -386,22 +387,26 @@ impl<'a> Interpreter<'a> { // TODO: 107 is hardcoded as a dummy prime for testing // should be changed to the proper implementation prime + fn bn_base_order_() -> U256 { + U256::from_str("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47").unwrap() + } + fn run_addfp254(&mut self) { let x = self.pop(); let y = self.pop(); - self.push((x + y) % 107); + self.push((x + y) % Self::bn_base_order_()); } fn run_mulfp254(&mut self) { let x = self.pop(); let y = self.pop(); - self.push(U256::try_from(x.full_mul(y) % 107).unwrap()); + self.push(U256::try_from(x.full_mul(y) % Self::bn_base_order_()).unwrap()); } fn run_subfp254(&mut self) { let x = self.pop(); let y = self.pop(); - self.push((U256::from(107) + x - y) % 107); + self.push((Self::bn_base_order_() + x - y) % Self::bn_base_order_()); } fn run_div(&mut self) {