More tests to ensure that inversion of zero is zero
This commit is contained in:
parent
c04721a04e
commit
c8935f0a1d
|
@ -441,21 +441,36 @@ proc mainModularInverse() =
|
||||||
|
|
||||||
check: bool(r == expected)
|
check: bool(r == expected)
|
||||||
|
|
||||||
test "0^-1 (mod 0) = 0 (need for tower of extension fields)":
|
test "0^-1 (mod any) = 0 (need for tower of extension fields)":
|
||||||
let a = BigInt[16].fromUint(0'u16)
|
block:
|
||||||
let M = BigInt[16].fromUint(2017'u16)
|
let a = BigInt[16].fromUint(0'u16)
|
||||||
|
let M = BigInt[16].fromUint(2017'u16)
|
||||||
|
|
||||||
var mp1div2 = M
|
var mp1div2 = M
|
||||||
mp1div2.shiftRight(1)
|
mp1div2.shiftRight(1)
|
||||||
discard mp1div2.add(Word 1)
|
discard mp1div2.add(Word 1)
|
||||||
|
|
||||||
let expected = BigInt[16].fromUint(0'u16)
|
let expected = BigInt[16].fromUint(0'u16)
|
||||||
var r {.noInit.}: BigInt[16]
|
var r {.noInit.}: BigInt[16]
|
||||||
|
|
||||||
r.invmod(a, M, mp1div2)
|
r.invmod(a, M, mp1div2)
|
||||||
|
|
||||||
check: bool(r == expected)
|
check: bool(r == expected)
|
||||||
|
|
||||||
|
block:
|
||||||
|
let a = BigInt[381].fromUint(0'u16)
|
||||||
|
let M = BigInt[381].fromHex("0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab")
|
||||||
|
|
||||||
|
var mp1div2 = M
|
||||||
|
mp1div2.shiftRight(1)
|
||||||
|
discard mp1div2.add(Word 1)
|
||||||
|
|
||||||
|
let expected = BigInt[381].fromUint(0'u16)
|
||||||
|
var r {.noInit.}: BigInt[381]
|
||||||
|
|
||||||
|
r.invmod(a, M, mp1div2)
|
||||||
|
|
||||||
|
check: bool(r == expected)
|
||||||
|
|
||||||
mainArith()
|
mainArith()
|
||||||
mainNeg()
|
mainNeg()
|
||||||
|
|
|
@ -153,18 +153,31 @@ proc main() =
|
||||||
computed == expected
|
computed == expected
|
||||||
|
|
||||||
suite "Modular inversion over prime fields":
|
suite "Modular inversion over prime fields":
|
||||||
test "Specific test on Fp[BLS12_381]":
|
test "Specific tests on Fp[BLS12_381]":
|
||||||
var r, x: Fp[BLS12_381]
|
block: # No inverse exist for 0 --> should return 0 for projective/jacobian to affine coordinate conversion
|
||||||
|
var r, x: Fp[BLS12_381]
|
||||||
|
x.setZero()
|
||||||
|
r.inv(x)
|
||||||
|
check: bool r.isZero()
|
||||||
|
|
||||||
# BN254 field modulus
|
block:
|
||||||
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
|
var r, x: Fp[BLS12_381]
|
||||||
|
x.setOne()
|
||||||
|
r.inv(x)
|
||||||
|
check: bool r.isOne()
|
||||||
|
|
||||||
let expected = "0x0636759a0f3034fa47174b2c0334902f11e9915b7bd89c6a2b3082b109abbc9837da17201f6d8286fe6203caa1b9d4c8"
|
block:
|
||||||
r.inv(x)
|
var r, x: Fp[BLS12_381]
|
||||||
let computed = r.toHex()
|
|
||||||
|
|
||||||
check:
|
# BN254 field modulus
|
||||||
computed == expected
|
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
|
||||||
|
|
||||||
|
let expected = "0x0636759a0f3034fa47174b2c0334902f11e9915b7bd89c6a2b3082b109abbc9837da17201f6d8286fe6203caa1b9d4c8"
|
||||||
|
r.inv(x)
|
||||||
|
let computed = r.toHex()
|
||||||
|
|
||||||
|
check:
|
||||||
|
computed == expected
|
||||||
|
|
||||||
test "Specific tests on Fp[BN254_Snarks]":
|
test "Specific tests on Fp[BN254_Snarks]":
|
||||||
block:
|
block:
|
||||||
|
|
Loading…
Reference in New Issue