From 3a1a5f88473512cd154a33ed233464a5a079d831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Thu, 9 Apr 2020 13:58:56 +0200 Subject: [PATCH] More coverage and crosscheck between multiplication, squaring, addition, substraction, negation --- tests/test_fp12.nim | 69 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_fp2.nim | 69 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_fp6.nim | 69 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+) diff --git a/tests/test_fp12.nim b/tests/test_fp12.nim index 4494d65..e408eb1 100644 --- a/tests/test_fp12.nim +++ b/tests/test_fp12.nim @@ -250,6 +250,75 @@ suite "𝔽p12 = 𝔽p6[√∛(1+𝑖)]": test(BLS12_461) test(BN462) + test "Squaring the opposite gives the same result": + template test(C: static Curve) = + block: + proc testInstance() = + for _ in 0 ..< Iters: + let a = rng.random(Fp12[C]) + var na{.noInit.}: Fp12[C] + na.neg(a) + + var rSqr{.noInit.}, rNegSqr{.noInit.}: Fp12[C] + + rSqr.square(a) + rNegSqr.square(na) + + check: bool(rSqr == rNegSqr) + + testInstance() + + test(BN254) + test(BLS12_377) + test(BLS12_381) + test(BN446) + test(FKM12_447) + test(BLS12_461) + test(BN462) + + test "Multiplication and Addition/Substraction are consistent": + template test(C: static Curve) = + block: + proc testInstance() = + for _ in 0 ..< Iters: + let factor = rand(-30..30) + + let a = rng.random(Fp12[C]) + + if factor == 0: continue + + var sum{.noInit.}, one{.noInit.}, f{.noInit.}: Fp12[C] + one.setOne() + + if factor < 0: + sum.neg(a) + f.neg(one) + for i in 1 ..< -factor: + sum -= a + f -= one + else: + sum = a + f = one + for i in 1 ..< factor: + sum += a + f += one + + var r{.noInit.}: Fp12[C] + + r.prod(a, f) + + check: bool(r == sum) + + testInstance() + + test(BN254) + test(BLS12_377) + test(BLS12_381) + test(BN446) + test(FKM12_447) + test(BLS12_461) + test(BN462) + test "𝔽p12 = 𝔽p6[√∛(1+𝑖)] addition is associative and commutative": proc abelianGroup(curve: static Curve) = for _ in 0 ..< Iters: diff --git a/tests/test_fp2.nim b/tests/test_fp2.nim index 73bfe72..15303bd 100644 --- a/tests/test_fp2.nim +++ b/tests/test_fp2.nim @@ -149,6 +149,75 @@ suite "𝔽p2 = 𝔽p[𝑖] (irreducible polynomial x²+1)": test(BLS12_461) test(BN462) + test "Squaring the opposite gives the same result": + template test(C: static Curve) = + block: + proc testInstance() = + for _ in 0 ..< Iters: + let a = rng.random(Fp2[C]) + var na{.noInit.}: Fp2[C] + na.neg(a) + + var rSqr{.noInit.}, rNegSqr{.noInit.}: Fp2[C] + + rSqr.square(a) + rNegSqr.square(na) + + check: bool(rSqr == rNegSqr) + + testInstance() + + test(BN254) + test(BLS12_377) + test(BLS12_381) + test(BN446) + test(FKM12_447) + test(BLS12_461) + test(BN462) + + test "Multiplication and Addition/Substraction are consistent": + template test(C: static Curve) = + block: + proc testInstance() = + for _ in 0 ..< Iters: + let factor = rand(-30..30) + + let a = rng.random(Fp2[C]) + + if factor == 0: continue + + var sum{.noInit.}, one{.noInit.}, f{.noInit.}: Fp2[C] + one.setOne() + + if factor < 0: + sum.neg(a) + f.neg(one) + for i in 1 ..< -factor: + sum -= a + f -= one + else: + sum = a + f = one + for i in 1 ..< factor: + sum += a + f += one + + var r{.noInit.}: Fp2[C] + + r.prod(a, f) + + check: bool(r == sum) + + testInstance() + + test(BN254) + test(BLS12_377) + test(BLS12_381) + test(BN446) + test(FKM12_447) + test(BLS12_461) + test(BN462) + test "𝔽p2 = 𝔽p[𝑖] addition is associative and commutative": proc abelianGroup(curve: static Curve) = for _ in 0 ..< Iters: diff --git a/tests/test_fp6.nim b/tests/test_fp6.nim index bafc4e8..8a582ab 100644 --- a/tests/test_fp6.nim +++ b/tests/test_fp6.nim @@ -250,6 +250,75 @@ suite "𝔽p6 = 𝔽p2[∛(1+𝑖)] (irreducible polynomial x³ - (1+𝑖))": test(BLS12_461) test(BN462) + test "Squaring the opposite gives the same result": + template test(C: static Curve) = + block: + proc testInstance() = + for _ in 0 ..< Iters: + let a = rng.random(Fp6[C]) + var na{.noInit.}: Fp6[C] + na.neg(a) + + var rSqr{.noInit.}, rNegSqr{.noInit.}: Fp6[C] + + rSqr.square(a) + rNegSqr.square(na) + + check: bool(rSqr == rNegSqr) + + testInstance() + + test(BN254) + test(BLS12_377) + test(BLS12_381) + test(BN446) + test(FKM12_447) + test(BLS12_461) + test(BN462) + + test "Multiplication and Addition/Substraction are consistent": + template test(C: static Curve) = + block: + proc testInstance() = + for _ in 0 ..< Iters: + let factor = rand(-30..30) + + let a = rng.random(Fp6[C]) + + if factor == 0: continue + + var sum{.noInit.}, one{.noInit.}, f{.noInit.}: Fp6[C] + one.setOne() + + if factor < 0: + sum.neg(a) + f.neg(one) + for i in 1 ..< -factor: + sum -= a + f -= one + else: + sum = a + f = one + for i in 1 ..< factor: + sum += a + f += one + + var r{.noInit.}: Fp6[C] + + r.prod(a, f) + + check: bool(r == sum) + + testInstance() + + test(BN254) + test(BLS12_377) + test(BLS12_381) + test(BN446) + test(FKM12_447) + test(BLS12_461) + test(BN462) + test "𝔽p6 = 𝔽p2[∛(1+𝑖)] addition is associative and commutative": proc abelianGroup(curve: static Curve) = for _ in 0 ..< Iters: