plonky2/evm/src/cpu/kernel/asm/moddiv.asm

40 lines
867 B
NASM
Raw Normal View History

2022-07-05 15:01:40 +02:00
/// 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
%inverse
// stack: x^-1, y
%mulmodn
%endmacro
%macro mulmodn
// stack: x, y
2022-07-22 18:26:15 +02:00
%bn_base
2022-07-05 15:01:40 +02:00
// stack: N, x, y
SWAP2
// stack: y, x, N
MULMOD
%endmacro
%macro squaremodn
// stack: x
DUP1
// stack: x, x
%mulmodn
%endmacro
2022-07-23 12:36:03 +02:00
// Non-deterministically provide the inverse modulo N.
2022-07-05 15:01:40 +02:00
%macro inverse
2022-07-22 18:26:15 +02:00
// stack: x
2022-07-22 19:25:06 +02:00
PROVER_INPUT(ff::bn254_base::inverse)
2022-07-22 18:26:15 +02:00
// stack: x^-1, x
2022-07-28 10:35:53 +02:00
%stack (inv, x) -> (inv, x, @BN_BASE, inv)
// stack: x^-1, x, N, x^-1
2022-07-22 18:26:15 +02:00
MULMOD
2022-07-28 10:35:53 +02:00
// stack: x^-1 * x, x^-1
%assert_eq_const(1)
2022-07-05 15:43:41 +02:00
// stack: x^-1
2022-07-05 15:01:40 +02:00
%endmacro