2020-04-14 20:40:10 +00:00
|
|
|
# Constantine
|
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# ############################################################
|
|
|
|
#
|
|
|
|
# Template tests for towered extension fields
|
|
|
|
#
|
|
|
|
# ############################################################
|
|
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
|
|
|
std/[unittest, times],
|
|
|
|
# Internals
|
2022-02-27 00:49:08 +00:00
|
|
|
../../constantine/platforms/abstractions,
|
|
|
|
../../constantine/math/extension_fields,
|
|
|
|
../../constantine/math/config/curves,
|
|
|
|
../../constantine/math/arithmetic,
|
|
|
|
../../constantine/math/io/io_extfields,
|
2020-04-14 20:40:10 +00:00
|
|
|
# Test utilities
|
2022-02-21 00:04:53 +00:00
|
|
|
../../helpers/[prng_unsafe, static_for]
|
2020-04-14 20:40:10 +00:00
|
|
|
|
2022-02-27 00:49:08 +00:00
|
|
|
export unittest # Generic sandwich
|
|
|
|
|
2020-06-15 20:58:56 +00:00
|
|
|
echo "\n------------------------------------------------------\n"
|
|
|
|
|
2020-04-14 20:40:10 +00:00
|
|
|
template ExtField(degree: static int, curve: static Curve): untyped =
|
|
|
|
when degree == 2:
|
|
|
|
Fp2[curve]
|
2021-02-09 21:57:45 +00:00
|
|
|
elif degree == 4:
|
|
|
|
Fp4[curve]
|
2020-04-14 20:40:10 +00:00
|
|
|
elif degree == 6:
|
|
|
|
Fp6[curve]
|
|
|
|
elif degree == 12:
|
|
|
|
Fp12[curve]
|
|
|
|
else:
|
|
|
|
{.error: "Unconfigured extension degree".}
|
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
type
|
|
|
|
RandomGen = enum
|
|
|
|
Uniform
|
|
|
|
HighHammingWeight
|
|
|
|
Long01Sequence
|
|
|
|
|
2020-09-21 21:24:00 +00:00
|
|
|
func random_elem(rng: var RngState, F: typedesc, gen: RandomGen): F {.inline, noInit.} =
|
|
|
|
if gen == Uniform:
|
2020-06-20 16:55:27 +00:00
|
|
|
result = rng.random_unsafe(F)
|
|
|
|
elif gen == HighHammingWeight:
|
|
|
|
result = rng.random_highHammingWeight(F)
|
|
|
|
else:
|
|
|
|
result = rng.random_long01Seq(F)
|
|
|
|
|
2020-04-14 20:40:10 +00:00
|
|
|
proc runTowerTests*[N](
|
|
|
|
ExtDegree: static int,
|
|
|
|
Iters: static int,
|
|
|
|
TestCurves: static array[N, Curve],
|
|
|
|
moduleName: string,
|
|
|
|
testSuiteDesc: string
|
|
|
|
) =
|
|
|
|
|
|
|
|
# Random seed for reproducibility
|
|
|
|
var rng: RngState
|
|
|
|
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
|
|
|
|
rng.seed(seed)
|
|
|
|
echo moduleName, " xoshiro512** seed: ", seed
|
|
|
|
|
2023-01-11 18:31:23 +00:00
|
|
|
suite testSuiteDesc & " [" & $WordBitWidth & "-bit mode]":
|
2020-04-14 20:40:10 +00:00
|
|
|
test "Comparison sanity checks":
|
|
|
|
proc test(Field: typedesc) =
|
|
|
|
var z, o {.noInit.}: Field
|
|
|
|
|
|
|
|
z.setZero()
|
|
|
|
o.setOne()
|
|
|
|
|
|
|
|
check: not bool(z == o)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve))
|
|
|
|
|
|
|
|
test "Addition, substraction negation are consistent":
|
2020-09-21 21:24:00 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
# Try to exercise all code paths for in-place/out-of-place add/sum/sub/diff/double/neg
|
|
|
|
# (1 - (-a) - b + (-a) - 2a) + (2a + 2b + (-b)) == 1
|
|
|
|
var accum {.noInit.}, One {.noInit.}, a{.noInit.}, na{.noInit.}, b{.noInit.}, nb{.noInit.}, a2 {.noInit.}, b2 {.noInit.}: Field
|
|
|
|
|
2020-04-15 00:12:45 +00:00
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
One.setOne()
|
2020-06-20 16:55:27 +00:00
|
|
|
a = rng.random_elem(Field, gen)
|
2020-04-15 00:12:45 +00:00
|
|
|
a2 = a
|
|
|
|
a2.double()
|
|
|
|
na.neg(a)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
b = rng.random_elem(Field, gen)
|
2020-04-15 00:12:45 +00:00
|
|
|
b2.double(b)
|
|
|
|
nb.neg(b)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
2020-04-15 00:12:45 +00:00
|
|
|
accum.diff(One, na)
|
|
|
|
accum -= b
|
|
|
|
accum += na
|
|
|
|
accum -= a2
|
2020-04-14 20:40:10 +00:00
|
|
|
|
2020-04-15 00:12:45 +00:00
|
|
|
var t{.noInit.}: Field
|
|
|
|
t.sum(a2, b2)
|
|
|
|
t += nb
|
2020-04-14 20:40:10 +00:00
|
|
|
|
2020-04-15 00:12:45 +00:00
|
|
|
accum += t
|
|
|
|
check: bool accum.isOne()
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-15 00:12:45 +00:00
|
|
|
|
|
|
|
test "Division by 2":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-15 00:12:45 +00:00
|
|
|
for _ in 0 ..< Iters:
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
2020-04-15 00:12:45 +00:00
|
|
|
var a2 = a
|
|
|
|
a2.double()
|
|
|
|
a2.div2()
|
|
|
|
check: bool(a == a2)
|
|
|
|
a2.div2()
|
|
|
|
a2.double()
|
|
|
|
check: bool(a == a2)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Squaring 1 returns 1":
|
|
|
|
proc test(Field: typedesc) =
|
|
|
|
let One = block:
|
|
|
|
var O{.noInit.}: Field
|
|
|
|
O.setOne()
|
|
|
|
O
|
|
|
|
block:
|
|
|
|
var r{.noinit.}: Field
|
|
|
|
r.square(One)
|
|
|
|
check: bool(r == One)
|
|
|
|
block:
|
|
|
|
var r{.noinit.}: Field
|
|
|
|
r.prod(One, One)
|
|
|
|
check: bool(r == One)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve))
|
|
|
|
|
|
|
|
test "Squaring 2 returns 4":
|
|
|
|
proc test(Field: typedesc) =
|
|
|
|
let One = block:
|
|
|
|
var O{.noInit.}: Field
|
|
|
|
O.setOne()
|
|
|
|
O
|
|
|
|
|
|
|
|
var Two: Field
|
|
|
|
Two.double(One)
|
|
|
|
|
|
|
|
var Four: Field
|
|
|
|
Four.double(Two)
|
|
|
|
|
|
|
|
block:
|
|
|
|
var r: Field
|
|
|
|
r.square(Two)
|
|
|
|
|
|
|
|
check: bool(r == Four)
|
|
|
|
block:
|
|
|
|
var r: Field
|
|
|
|
r.prod(Two, Two)
|
|
|
|
|
|
|
|
check: bool(r == Four)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve))
|
|
|
|
|
|
|
|
test "Squaring 3 returns 9":
|
|
|
|
proc test(Field: typedesc) =
|
|
|
|
let One = block:
|
|
|
|
var O{.noInit.}: Field
|
|
|
|
O.setOne()
|
|
|
|
O
|
|
|
|
|
|
|
|
var Three: Field
|
|
|
|
for _ in 0 ..< 3:
|
|
|
|
Three += One
|
|
|
|
|
|
|
|
var Nine: Field
|
|
|
|
for _ in 0 ..< 9:
|
|
|
|
Nine += One
|
|
|
|
|
|
|
|
block:
|
|
|
|
var u: Field
|
|
|
|
u.square(Three)
|
|
|
|
|
|
|
|
check: bool(u == Nine)
|
|
|
|
block:
|
|
|
|
var u: Field
|
|
|
|
u.prod(Three, Three)
|
|
|
|
|
|
|
|
check: bool(u == Nine)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve))
|
|
|
|
|
|
|
|
test "Squaring -3 returns 9":
|
|
|
|
proc test(Field: typedesc) =
|
|
|
|
let One = block:
|
|
|
|
var O{.noInit.}: Field
|
|
|
|
O.setOne()
|
|
|
|
O
|
|
|
|
|
|
|
|
var MinusThree: Field
|
|
|
|
for _ in 0 ..< 3:
|
|
|
|
MinusThree -= One
|
|
|
|
|
|
|
|
var Nine: Field
|
|
|
|
for _ in 0 ..< 9:
|
|
|
|
Nine += One
|
|
|
|
|
|
|
|
block:
|
|
|
|
var u: Field
|
|
|
|
u.square(MinusThree)
|
|
|
|
|
|
|
|
check: bool(u == Nine)
|
|
|
|
block:
|
|
|
|
var u: Field
|
|
|
|
u.prod(MinusThree, MinusThree)
|
|
|
|
|
|
|
|
check: bool(u == Nine)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve))
|
|
|
|
|
|
|
|
test "Multiplication by 0 and 1":
|
|
|
|
template test(Field: typedesc, body: untyped) =
|
|
|
|
block:
|
|
|
|
proc testInstance() =
|
|
|
|
let Z {.inject.} = block:
|
|
|
|
var Z{.noInit.}: Field
|
|
|
|
Z.setZero()
|
|
|
|
Z
|
|
|
|
let O {.inject.} = block:
|
|
|
|
var O{.noInit.}: Field
|
|
|
|
O.setOne()
|
|
|
|
O
|
|
|
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
let x {.inject.} = rng.random_unsafe(Field)
|
|
|
|
var r{.noinit, inject.}: Field
|
|
|
|
body
|
|
|
|
|
|
|
|
testInstance()
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve)):
|
|
|
|
r.prod(x, Z)
|
2021-02-12 20:27:58 +00:00
|
|
|
doAssert bool(r == Z),
|
2022-08-14 07:48:10 +00:00
|
|
|
"\nExpected zero but got \n(" & $ExtField(ExtDegree, curve) & "): " & x.toHex()
|
2020-04-14 20:40:10 +00:00
|
|
|
test(ExtField(ExtDegree, curve)):
|
|
|
|
r.prod(Z, x)
|
2021-02-12 20:27:58 +00:00
|
|
|
doAssert bool(r == Z),
|
2022-08-14 07:48:10 +00:00
|
|
|
"\nExpected zero but got \n(" & $ExtField(ExtDegree, curve) & "): " & x.toHex()
|
2020-04-14 20:40:10 +00:00
|
|
|
test(ExtField(ExtDegree, curve)):
|
|
|
|
r.prod(x, O)
|
2021-02-12 20:27:58 +00:00
|
|
|
doAssert bool(r == x),
|
|
|
|
"\n(" & $ExtField(ExtDegree, curve) & "): Expected one: " & O.toHex() & "\n" &
|
|
|
|
"got: " & x.toHex()
|
2020-04-14 20:40:10 +00:00
|
|
|
test(ExtField(ExtDegree, curve)):
|
|
|
|
r.prod(O, x)
|
2021-02-12 20:27:58 +00:00
|
|
|
doAssert bool(r == x),
|
|
|
|
"\n(" & $ExtField(ExtDegree, curve) & "): Expected one: " & O.toHex() & "\n" &
|
|
|
|
"got: " & x.toHex()
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Multiplication and Squaring are consistent":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
for _ in 0 ..< Iters:
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
2020-04-14 20:40:10 +00:00
|
|
|
var rMul{.noInit.}, rSqr{.noInit.}: Field
|
|
|
|
|
|
|
|
rMul.prod(a, a)
|
|
|
|
rSqr.square(a)
|
|
|
|
|
2022-08-14 07:48:10 +00:00
|
|
|
doAssert bool(rMul == rSqr), "Failure with a (" & $Field & "): \nInput:" & a.toHex() & "\n" &
|
2021-02-12 20:27:58 +00:00
|
|
|
"Mul: " & rMul.toHex() & "\n" &
|
|
|
|
"Sqr: " & rSqr.toHex() & "\n"
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Squaring the opposite gives the same result":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
for _ in 0 ..< Iters:
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
2020-04-14 20:40:10 +00:00
|
|
|
var na{.noInit.}: Field
|
|
|
|
na.neg(a)
|
|
|
|
|
|
|
|
var rSqr{.noInit.}, rNegSqr{.noInit.}: Field
|
|
|
|
|
|
|
|
rSqr.square(a)
|
|
|
|
rNegSqr.square(na)
|
|
|
|
|
2022-08-14 07:48:10 +00:00
|
|
|
doAssert bool(rSqr == rNegSqr), "Failure with a \n(" & $Field & "): " & a.toHex() & "\n" &
|
2021-02-12 20:27:58 +00:00
|
|
|
"Sqr: " & rSqr.toHex() & "\n" &
|
|
|
|
"SqrNeg: " & rNegSqr.toHex() & "\n"
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Multiplication and Addition/Substraction are consistent":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
let factor = rng.random_unsafe(-30..30)
|
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
if factor == 0: continue
|
|
|
|
|
|
|
|
var sum{.noInit.}, one{.noInit.}, f{.noInit.}: Field
|
|
|
|
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.}: Field
|
|
|
|
|
|
|
|
r.prod(a, f)
|
|
|
|
|
|
|
|
check: bool(r == sum)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Addition is associative and commutative":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
for _ in 0 ..< Iters:
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
|
|
|
let b = rng.random_elem(Field, gen)
|
|
|
|
let c = rng.random_elem(Field, gen)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
var tmp1{.noInit.}, tmp2{.noInit.}: Field
|
|
|
|
|
|
|
|
# r0 = (a + b) + c
|
|
|
|
tmp1.sum(a, b)
|
|
|
|
tmp2.sum(tmp1, c)
|
|
|
|
let r0 = tmp2
|
|
|
|
|
|
|
|
# r1 = a + (b + c)
|
|
|
|
tmp1.sum(b, c)
|
|
|
|
tmp2.sum(a, tmp1)
|
|
|
|
let r1 = tmp2
|
|
|
|
|
|
|
|
# r2 = (a + c) + b
|
|
|
|
tmp1.sum(a, c)
|
|
|
|
tmp2.sum(tmp1, b)
|
|
|
|
let r2 = tmp2
|
|
|
|
|
|
|
|
# r3 = a + (c + b)
|
|
|
|
tmp1.sum(c, b)
|
|
|
|
tmp2.sum(a, tmp1)
|
|
|
|
let r3 = tmp2
|
|
|
|
|
|
|
|
# r4 = (c + a) + b
|
|
|
|
tmp1.sum(c, a)
|
|
|
|
tmp2.sum(tmp1, b)
|
|
|
|
let r4 = tmp2
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
|
|
|
check:
|
|
|
|
bool(r0 == r1)
|
|
|
|
bool(r0 == r2)
|
|
|
|
bool(r0 == r3)
|
|
|
|
bool(r0 == r4)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Multiplication is associative and commutative":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
for _ in 0 ..< Iters:
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
|
|
|
let b = rng.random_elem(Field, gen)
|
|
|
|
let c = rng.random_elem(Field, gen)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
var tmp1{.noInit.}, tmp2{.noInit.}: Field
|
|
|
|
|
|
|
|
# r0 = (a * b) * c
|
|
|
|
tmp1.prod(a, b)
|
|
|
|
tmp2.prod(tmp1, c)
|
|
|
|
let r0 = tmp2
|
|
|
|
|
|
|
|
# r1 = a * (b * c)
|
|
|
|
tmp1.prod(b, c)
|
|
|
|
tmp2.prod(a, tmp1)
|
|
|
|
let r1 = tmp2
|
|
|
|
|
|
|
|
# r2 = (a * c) * b
|
|
|
|
tmp1.prod(a, c)
|
|
|
|
tmp2.prod(tmp1, b)
|
|
|
|
let r2 = tmp2
|
|
|
|
|
|
|
|
# r3 = a * (c * b)
|
|
|
|
tmp1.prod(c, b)
|
|
|
|
tmp2.prod(a, tmp1)
|
|
|
|
let r3 = tmp2
|
|
|
|
|
|
|
|
# r4 = (c * a) * b
|
|
|
|
tmp1.prod(c, a)
|
|
|
|
tmp2.prod(tmp1, b)
|
|
|
|
let r4 = tmp2
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
|
|
|
check:
|
|
|
|
bool(r0 == r1)
|
|
|
|
bool(r0 == r2)
|
|
|
|
bool(r0 == r3)
|
|
|
|
bool(r0 == r4)
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "Extension field multiplicative inverse":
|
2020-06-20 16:55:27 +00:00
|
|
|
proc test(Field: typedesc, Iters: static int, gen: static RandomGen) =
|
2020-04-14 20:40:10 +00:00
|
|
|
var aInv, r{.noInit.}: Field
|
|
|
|
|
|
|
|
for _ in 0 ..< Iters:
|
2020-06-20 16:55:27 +00:00
|
|
|
let a = rng.random_elem(Field, gen)
|
2020-04-14 20:40:10 +00:00
|
|
|
aInv.inv(a)
|
|
|
|
r.prod(a, aInv)
|
|
|
|
check: bool(r.isOne())
|
|
|
|
r.prod(aInv, a)
|
|
|
|
check: bool(r.isOne())
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2020-06-20 16:55:27 +00:00
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Uniform)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = HighHammingWeight)
|
|
|
|
test(ExtField(ExtDegree, curve), Iters, gen = Long01Sequence)
|
2020-04-14 20:40:10 +00:00
|
|
|
|
|
|
|
test "0 does not have a multiplicative inverse and should return 0 for projective/jacobian => affine coordinates conversion":
|
|
|
|
proc test(Field: typedesc) =
|
|
|
|
var z: Field
|
|
|
|
z.setZero()
|
|
|
|
|
|
|
|
var zInv{.noInit.}: Field
|
|
|
|
|
|
|
|
zInv.inv(z)
|
|
|
|
check: bool zInv.isZero()
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
test(ExtField(ExtDegree, curve))
|