From 48149f93d153fa8840c72c71763e9c8af86d604a Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 5 Dec 2022 22:50:45 -0800 Subject: [PATCH] inverse --- evm/src/cpu/kernel/aggregator.rs | 2 +- .../curve/bn254/curve_arithmetic/miller_loop.asm | 2 +- .../field_arithmetic/{moddiv.asm => inverse.asm} | 13 +++++++++++++ evm/src/cpu/kernel/interpreter.rs | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) rename evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/{moddiv.asm => inverse.asm} (62%) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index bb2dce92..c0f8de77 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -29,7 +29,7 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/curve/bn254/curve_arithmetic/curve_mul.asm"), include_str!("asm/curve/bn254/curve_arithmetic/miller_loop.asm"), include_str!("asm/curve/bn254/curve_arithmetic/tate_pairing.asm"), - include_str!("asm/curve/bn254/field_arithmetic/moddiv.asm"), + include_str!("asm/curve/bn254/field_arithmetic/inverse.asm"), include_str!("asm/curve/bn254/field_arithmetic/field_macros.asm"), include_str!("asm/curve/bn254/field_arithmetic/fp6_mul.asm"), include_str!("asm/curve/bn254/field_arithmetic/fp12_mul.asm"), diff --git a/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/miller_loop.asm b/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/miller_loop.asm index 3f625d16..2b630186 100644 --- a/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/miller_loop.asm +++ b/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/miller_loop.asm @@ -70,7 +70,7 @@ miller_end: miller_one: // stack: 0xnm, times, O, P, Q, out, retdest - PUSH 0x10 DUP2 LT + DUP1 %gt_const(0x10) // stack: skip?, 0xnm, times, O, P, Q, out, retdest %jumpi(miller_zero) // stack: 0xnm, times, O, P, Q, out, retdest diff --git a/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm b/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/inverse.asm similarity index 62% rename from evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm rename to evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/inverse.asm index 578eddae..6d5169e8 100644 --- a/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/moddiv.asm +++ b/evm/src/cpu/kernel/asm/curve/bn254/field_arithmetic/inverse.asm @@ -20,3 +20,16 @@ %assert_eq_const(1) // stack: x^-1 %endmacro + +// Non-deterministically provide the inverse modulo N. +%macro inverse + // stack: x + PROVER_INPUT(ff::bn254_base::inverse) + // 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 +%endmacro diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index b63911ed..f075b11d 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -388,7 +388,8 @@ impl<'a> Interpreter<'a> { // should be changed to the proper implementation prime fn bn_base_order_() -> U256 { - U256::from_str("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47").unwrap() + U256::from_str("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") + .unwrap() } fn run_addfp254(&mut self) {