From c8935f0a1ddd80bf49251ba9b6aedae356bfb252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Tue, 14 Apr 2020 13:39:03 +0200 Subject: [PATCH] More tests to ensure that inversion of zero is zero --- tests/test_bigints.nim | 35 ++++++++++++++++++++--------- tests/test_finite_fields_powinv.nim | 31 +++++++++++++++++-------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/tests/test_bigints.nim b/tests/test_bigints.nim index 9010d1d..af3240d 100644 --- a/tests/test_bigints.nim +++ b/tests/test_bigints.nim @@ -441,21 +441,36 @@ proc mainModularInverse() = check: bool(r == expected) - test "0^-1 (mod 0) = 0 (need for tower of extension fields)": - let a = BigInt[16].fromUint(0'u16) - let M = BigInt[16].fromUint(2017'u16) + 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) - var mp1div2 = M - mp1div2.shiftRight(1) - discard mp1div2.add(Word 1) + var mp1div2 = M + mp1div2.shiftRight(1) + discard mp1div2.add(Word 1) - let expected = BigInt[16].fromUint(0'u16) - var r {.noInit.}: BigInt[16] + let expected = BigInt[16].fromUint(0'u16) + 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() mainNeg() diff --git a/tests/test_finite_fields_powinv.nim b/tests/test_finite_fields_powinv.nim index 7e966b5..79dce00 100644 --- a/tests/test_finite_fields_powinv.nim +++ b/tests/test_finite_fields_powinv.nim @@ -153,18 +153,31 @@ proc main() = computed == expected suite "Modular inversion over prime fields": - test "Specific test on Fp[BLS12_381]": - var r, x: 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() - # BN254 field modulus - x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") + block: + var r, x: Fp[BLS12_381] + x.setOne() + r.inv(x) + check: bool r.isOne() - let expected = "0x0636759a0f3034fa47174b2c0334902f11e9915b7bd89c6a2b3082b109abbc9837da17201f6d8286fe6203caa1b9d4c8" - r.inv(x) - let computed = r.toHex() + block: + var r, x: Fp[BLS12_381] - check: - computed == expected + # BN254 field modulus + x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") + + let expected = "0x0636759a0f3034fa47174b2c0334902f11e9915b7bd89c6a2b3082b109abbc9837da17201f6d8286fe6203caa1b9d4c8" + r.inv(x) + let computed = r.toHex() + + check: + computed == expected test "Specific tests on Fp[BN254_Snarks]": block: