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

46 lines
992 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-22 18:26:15 +02:00
// Computes the inverse modulo N by providing it non-deterministically.
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-22 19:25:06 +02:00
%stack (inv, x) -> (inv, x, @BN_BASE, inv, x)
2022-07-22 18:26:15 +02:00
// stack: x^-1, x, N, x^-1, x
MULMOD
// stack: x^-1 * x, x^-1, x
PUSH 1
// stack: 1, x^-1 * x, x^-1, x
%assert_eq
// stack: x^-1, x
2022-07-05 15:43:41 +02:00
SWAP1
// stack: x, x^-1
POP
// stack: x^-1
2022-07-05 15:01:40 +02:00
%endmacro