More tests to ensure that inversion of zero is zero

This commit is contained in:
Mamy André-Ratsimbazafy 2020-04-14 13:39:03 +02:00 committed by Mamy Ratsimbazafy
parent c04721a04e
commit c8935f0a1d
2 changed files with 47 additions and 19 deletions

View File

@ -441,7 +441,8 @@ proc mainModularInverse() =
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)":
block:
let a = BigInt[16].fromUint(0'u16)
let M = BigInt[16].fromUint(2017'u16)
@ -456,6 +457,20 @@ proc mainModularInverse() =
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()
mainNeg()

View File

@ -153,7 +153,20 @@ proc main() =
computed == expected
suite "Modular inversion over prime fields":
test "Specific test on Fp[BLS12_381]":
test "Specific tests on 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()
block:
var r, x: Fp[BLS12_381]
x.setOne()
r.inv(x)
check: bool r.isOne()
block:
var r, x: Fp[BLS12_381]
# BN254 field modulus