2020-09-21 23:24:00 +02: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.
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
# Standard library
|
|
|
|
|
std/[tables, unittest, times],
|
|
|
|
|
# Internals
|
2022-02-27 01:49:08 +01:00
|
|
|
|
../../constantine/platforms/abstractions,
|
|
|
|
|
../../constantine/math/arithmetic,
|
|
|
|
|
../../constantine/math/extension_fields,
|
|
|
|
|
../../constantine/math/config/curves,
|
|
|
|
|
../../constantine/math/io/io_extfields,
|
|
|
|
|
../../constantine/math/pairing/lines_eval,
|
2020-09-21 23:24:00 +02:00
|
|
|
|
# Test utilities
|
2022-02-21 01:04:53 +01:00
|
|
|
|
../../helpers/[prng_unsafe, static_for]
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
Iters = 8
|
|
|
|
|
TestCurves = [
|
2020-09-25 21:58:20 +02:00
|
|
|
|
BN254_Nogami,
|
|
|
|
|
BN254_Snarks,
|
2020-09-27 09:15:14 +02:00
|
|
|
|
BLS12_377,
|
2020-09-21 23:24:00 +02:00
|
|
|
|
BLS12_381
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
RandomGen = enum
|
|
|
|
|
Uniform
|
|
|
|
|
HighHammingWeight
|
|
|
|
|
Long01Sequence
|
|
|
|
|
|
|
|
|
|
var rng: RngState
|
|
|
|
|
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
|
|
|
|
|
rng.seed(seed)
|
|
|
|
|
echo "\n------------------------------------------------------\n"
|
|
|
|
|
echo "test_pairing_fp12_sparse xoshiro512** seed: ", seed
|
|
|
|
|
|
|
|
|
|
func random_elem(rng: var RngState, F: typedesc, gen: RandomGen): F {.inline, noInit.} =
|
|
|
|
|
if gen == Uniform:
|
|
|
|
|
result = rng.random_unsafe(F)
|
|
|
|
|
elif gen == HighHammingWeight:
|
|
|
|
|
result = rng.random_highHammingWeight(F)
|
|
|
|
|
else:
|
|
|
|
|
result = rng.random_long01Seq(F)
|
|
|
|
|
|
|
|
|
|
suite "Pairing - Sparse 𝔽p12 multiplication by line function is consistent with dense 𝔽p12 mul":
|
2020-09-24 17:18:23 +02:00
|
|
|
|
test "Dense 𝔽p4 by Sparse 0y":
|
|
|
|
|
proc test_fp4_0y(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
let a = rng.random_elem(Fp4[C], gen)
|
|
|
|
|
let y = rng.random_elem(Fp2[C], gen)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let b = Fp4[C](coords: [Fp2[C](), y])
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
var r, r2: Fp4[C]
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
|
|
|
|
r.prod(a, b)
|
|
|
|
|
r2.mul_sparse_by_0y(a, y)
|
|
|
|
|
|
|
|
|
|
check: bool(r == r2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp4_0y(curve, gen = Uniform)
|
|
|
|
|
test_fp4_0y(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp4_0y(curve, gen = Long01Sequence)
|
|
|
|
|
|
2020-09-21 23:24:00 +02:00
|
|
|
|
test "Dense 𝔽p6 by Sparse 0y0":
|
|
|
|
|
proc test_fp6_0y0(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
let a = rng.random_elem(Fp6[C], gen)
|
|
|
|
|
let y = rng.random_elem(Fp2[C], gen)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let b = Fp6[C](coords: [Fp2[C](), y, Fp2[C]()])
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
var r, r2: Fp6[C]
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
|
|
|
|
r.prod(a, b)
|
|
|
|
|
r2.mul_sparse_by_0y0(a, y)
|
|
|
|
|
|
|
|
|
|
check: bool(r == r2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp6_0y0(curve, gen = Uniform)
|
|
|
|
|
test_fp6_0y0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp6_0y0(curve, gen = Long01Sequence)
|
|
|
|
|
|
|
|
|
|
test "Dense 𝔽p6 by Sparse xy0":
|
2021-02-14 13:06:57 +01:00
|
|
|
|
proc test_fp6_xy0(C: static Curve, gen: static RandomGen) =
|
2020-09-21 23:24:00 +02:00
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
let a = rng.random_elem(Fp6[C], gen)
|
|
|
|
|
let x = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
let y = rng.random_elem(Fp2[C], gen)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let b = Fp6[C](coords: [x, y, Fp2[C]()])
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
var r, r2: Fp6[C]
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
|
|
|
|
r.prod(a, b)
|
2022-08-14 09:48:10 +02:00
|
|
|
|
r2.mul_sparse_by_xy0(a, x, y)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
|
|
|
|
check: bool(r == r2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2021-02-14 13:06:57 +01:00
|
|
|
|
test_fp6_xy0(curve, gen = Uniform)
|
|
|
|
|
test_fp6_xy0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp6_xy0(curve, gen = Long01Sequence)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
test "Dense 𝔽p6 by Sparse 0yz":
|
|
|
|
|
proc test_fp6_0yz(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
let a = rng.random_elem(Fp6[C], gen)
|
|
|
|
|
let y = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
let z = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
let b = Fp6[C](coords: [Fp2[C](), y, z])
|
|
|
|
|
|
|
|
|
|
var r, r2: Fp6[C]
|
|
|
|
|
|
|
|
|
|
r.prod(a, b)
|
|
|
|
|
r2.mul_sparse_by_0yz(a, y, z)
|
|
|
|
|
|
|
|
|
|
check: bool(r == r2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp6_0yz(curve, gen = Uniform)
|
|
|
|
|
test_fp6_0yz(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp6_0yz(curve, gen = Long01Sequence)
|
|
|
|
|
|
2020-09-21 23:24:00 +02:00
|
|
|
|
when Fp12[BN254_Snarks]().c0.typeof is Fp6:
|
2022-08-14 09:48:10 +02:00
|
|
|
|
# =========== Towering 𝔽p12/𝔽p6 ======================================
|
|
|
|
|
|
|
|
|
|
test "Sparse 𝔽p12/𝔽p6 resulting from a00bc0 line function":
|
|
|
|
|
proc test_fp12_a00bc0(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == D_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var a = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var a2 = a
|
|
|
|
|
|
|
|
|
|
var x = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line = Line[Fp2[C]](a: x, b: y, c: z)
|
|
|
|
|
let b = Fp12[C]( coords: [
|
|
|
|
|
Fp6[C](coords: [ x, Fp2[C](), Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [ y, z, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
a *= b
|
|
|
|
|
a2.mul_sparse_by_line_a00bc0(line)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
check: bool(a == a2)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp12_a00bc0(curve, gen = Uniform)
|
|
|
|
|
test_fp12_a00bc0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_a00bc0(curve, gen = Long01Sequence)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
test "Sparse 𝔽p12/𝔽p6 resulting from cb00a0 line function":
|
|
|
|
|
proc test_fp12_cb00a0(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == M_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var a = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var a2 = a
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
var x = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line = Line[Fp2[C]](a: x, b: y, c: z)
|
|
|
|
|
let b = Fp12[C](coords: [
|
|
|
|
|
Fp6[C](coords: [ z, y, Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [Fp2[C](), x, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
a *= b
|
|
|
|
|
a2.mul_sparse_by_line_cb00a0(line)
|
|
|
|
|
|
|
|
|
|
check: bool(a == a2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp12_cb00a0(curve, gen = Uniform)
|
|
|
|
|
test_fp12_cb00a0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_cb00a0(curve, gen = Long01Sequence)
|
|
|
|
|
|
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p6 resulting from a00bc0*a00bc0 line functions (D-twist only)":
|
|
|
|
|
proc test_fp12_a00bc0_a00bc0(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == D_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
|
|
|
|
let f0 = Fp12[C]( coords: [
|
|
|
|
|
Fp6[C](coords: [ x0, Fp2[C](), Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [ y0, z0, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
|
|
|
|
let f1 = Fp12[C]( coords: [
|
|
|
|
|
Fp6[C](coords: [ x1, Fp2[C](), Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [ y1, z1, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
|
|
|
|
var rl: Fp12[C]
|
|
|
|
|
rl.prod_x00yz0_x00yz0_into_abcdefghij00(line0, line1)
|
|
|
|
|
|
|
|
|
|
check: bool(r == rl)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2022-08-14 09:48:10 +02:00
|
|
|
|
test_fp12_a00bc0_a00bc0(curve, gen = Uniform)
|
|
|
|
|
test_fp12_a00bc0_a00bc0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_a00bc0_a00bc0(curve, gen = Long01Sequence)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p6 resulting from cb00a0*cb00a0 line functions (M-twist only)":
|
|
|
|
|
proc test_fp12_cb00a0_cb00a0(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == M_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
|
|
|
|
let f0 = Fp12[C](coords: [
|
|
|
|
|
Fp6[C](coords: [ z0, y0, Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [Fp2[C](), x0, Fp2[C]()])
|
|
|
|
|
])
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
|
|
|
|
let f1 = Fp12[C](coords: [
|
|
|
|
|
Fp6[C](coords: [ z1, y1, Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [Fp2[C](), x1, Fp2[C]()])
|
|
|
|
|
])
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
2022-08-14 09:48:10 +02:00
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
|
|
|
|
var rl: Fp12[C]
|
|
|
|
|
rl.prod_zy00x0_zy00x0_into_abcdef00ghij(line0, line1)
|
|
|
|
|
|
|
|
|
|
check: bool(r == rl)
|
2020-09-21 23:24:00 +02:00
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2022-08-14 09:48:10 +02:00
|
|
|
|
test_fp12_cb00a0_cb00a0(curve, gen = Uniform)
|
|
|
|
|
test_fp12_cb00a0_cb00a0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_cb00a0_cb00a0(curve, gen = Long01Sequence)
|
|
|
|
|
|
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p6 mul by the product a00bc0*a00bc0 of line functions (D-twist only)":
|
|
|
|
|
proc test_fp12_abcdefghij00(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == D_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
|
|
|
|
let f0 = Fp12[C]( coords: [
|
|
|
|
|
Fp6[C](coords: [ x0, Fp2[C](), Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [ y0, z0, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
|
|
|
|
let f1 = Fp12[C]( coords: [
|
|
|
|
|
Fp6[C](coords: [ x1, Fp2[C](), Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [ y1, z1, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
|
|
|
|
var rl: Fp12[C]
|
|
|
|
|
rl.prod_x00yz0_x00yz0_into_abcdefghij00(line0, line1)
|
|
|
|
|
|
|
|
|
|
var f = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var f2 = f
|
|
|
|
|
|
|
|
|
|
f *= rl
|
|
|
|
|
f2.mul_sparse_by_abcdefghij00_quad_over_cube(rl)
|
|
|
|
|
|
|
|
|
|
check: bool(f == f2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp12_abcdefghij00(curve, gen = Uniform)
|
|
|
|
|
test_fp12_abcdefghij00(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_abcdefghij00(curve, gen = Long01Sequence)
|
|
|
|
|
|
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p6 mul by the product (cb00a0*cb00a0) of line functions (M-twist only)":
|
|
|
|
|
proc test_fp12_abcdef00ghij(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == M_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
|
|
|
|
let f0 = Fp12[C](coords: [
|
|
|
|
|
Fp6[C](coords: [ z0, y0, Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [Fp2[C](), x0, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
|
|
|
|
let f1 = Fp12[C](coords: [
|
|
|
|
|
Fp6[C](coords: [ z1, y1, Fp2[C]()]),
|
|
|
|
|
Fp6[C](coords: [Fp2[C](), x1, Fp2[C]()])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
|
|
|
|
var rl: Fp12[C]
|
|
|
|
|
rl.prod_zy00x0_zy00x0_into_abcdef00ghij(line0, line1)
|
|
|
|
|
|
|
|
|
|
var f = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var f2 = f
|
|
|
|
|
|
|
|
|
|
f *= rl
|
|
|
|
|
f2.mul_sparse_by_abcdef00ghij_quad_over_cube(rl)
|
|
|
|
|
|
|
|
|
|
check: bool(f == f2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp12_abcdef00ghij(curve, gen = Uniform)
|
|
|
|
|
test_fp12_abcdef00ghij(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_abcdef00ghij(curve, gen = Long01Sequence)
|
|
|
|
|
|
|
|
|
|
else: # =========== Towering 𝔽p12/𝔽p4 ======================================
|
2020-09-24 17:18:23 +02:00
|
|
|
|
static: doAssert Fp12[BN254_Snarks]().c0.typeof is Fp4
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test "Sparse 𝔽p12/𝔽p4 resulting from ca00b0 line function (M-twist only)":
|
|
|
|
|
proc test_fp12_ca00b0(C: static Curve, gen: static RandomGen) =
|
2020-10-09 07:51:47 +02:00
|
|
|
|
when C.getSexticTwist() == M_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var a = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var a2 = a
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
2020-10-09 07:51:47 +02:00
|
|
|
|
var x = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z = rng.random_elem(Fp2[C], gen)
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line = Line[Fp2[C]](a: x, b: y, c: z)
|
2020-10-09 07:51:47 +02:00
|
|
|
|
let b = Fp12[C](
|
2021-02-14 13:06:57 +01:00
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [z, x]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C](),
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [y, Fp2[C]()])
|
2021-02-14 13:06:57 +01:00
|
|
|
|
]
|
2020-10-09 07:51:47 +02:00
|
|
|
|
)
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
2020-10-09 07:51:47 +02:00
|
|
|
|
a *= b
|
2022-04-04 10:10:36 +02:00
|
|
|
|
a2.mul_sparse_by_line_ca00b0(line)
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
2020-10-09 07:51:47 +02:00
|
|
|
|
check: bool(a == a2)
|
2020-09-24 17:18:23 +02:00
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test_fp12_ca00b0(curve, gen = Uniform)
|
|
|
|
|
test_fp12_ca00b0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_ca00b0(curve, gen = Long01Sequence)
|
2020-09-25 21:58:20 +02:00
|
|
|
|
|
2020-10-09 07:51:47 +02:00
|
|
|
|
test "Sparse 𝔽p12/𝔽p4 resulting from xyz000 line function (D-twist only)":
|
2022-04-04 10:10:36 +02:00
|
|
|
|
proc test_fp12_acb000(C: static Curve, gen: static RandomGen) =
|
2020-10-09 07:51:47 +02:00
|
|
|
|
when C.getSexticTwist() == D_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var a = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var a2 = a
|
|
|
|
|
|
|
|
|
|
var x = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line = Line[Fp2[C]](a: x, b: y, c: z)
|
2020-10-09 07:51:47 +02:00
|
|
|
|
let b = Fp12[C](
|
2021-02-14 13:06:57 +01:00
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [x, z]),
|
|
|
|
|
Fp4[C](coords: [y, Fp2[C]()]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C]()
|
|
|
|
|
]
|
2020-10-09 07:51:47 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
a *= b
|
2022-04-04 10:10:36 +02:00
|
|
|
|
a2.mul_sparse_by_line_acb000(line)
|
2020-10-09 07:51:47 +02:00
|
|
|
|
|
|
|
|
|
check: bool(a == a2)
|
2020-09-25 21:58:20 +02:00
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test_fp12_acb000(curve, gen = Uniform)
|
|
|
|
|
test_fp12_acb000(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_acb000(curve, gen = Long01Sequence)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p4 resulting from ca00b0*ca00b0 line functions (M-twist only)":
|
|
|
|
|
proc test_fp12_ca00b0_ca00b0(C: static Curve, gen: static RandomGen) =
|
2021-02-14 13:06:57 +01:00
|
|
|
|
when C.getSexticTwist() == M_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f0 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [z0, x0]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C](),
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [y0, Fp2[C]()])
|
2021-02-14 13:06:57 +01:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f1 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [z1, x1]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C](),
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [y1, Fp2[C]()])
|
2021-02-14 13:06:57 +01:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
|
|
|
|
var rl: Fp12[C]
|
2022-04-04 10:10:36 +02:00
|
|
|
|
rl.prod_zx00y0_zx00y0_into_abcd00efghij(line0, line1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
|
|
|
|
check: bool(r == rl)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp12_ca00b0_ca00b0(curve, gen = Uniform)
|
|
|
|
|
test_fp12_ca00b0_ca00b0(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_ca00b0_ca00b0(curve, gen = Long01Sequence)
|
|
|
|
|
|
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p4 resulting from acb000*acb000 line functions (D-twist only)":
|
|
|
|
|
proc test_fp12_acb000_acb000(C: static Curve, gen: static RandomGen) =
|
2021-02-14 13:06:57 +01:00
|
|
|
|
when C.getSexticTwist() == D_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f0 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [x0, z0]),
|
|
|
|
|
Fp4[C](coords: [y0, Fp2[C]()]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C]()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f1 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [x1, z1]),
|
|
|
|
|
Fp4[C](coords: [y1, Fp2[C]()]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C]()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
|
|
|
|
var rl: Fp12[C]
|
2022-04-04 10:10:36 +02:00
|
|
|
|
rl.prod_xzy000_xzy000_into_abcdefghij00(line0, line1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
|
|
|
|
check: bool(r == rl)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test_fp12_acb000_acb000(curve, gen = Uniform)
|
|
|
|
|
test_fp12_acb000_acb000(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_acb000_acb000(curve, gen = Long01Sequence)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p4 mul by the product (acb000*acb000) of line functions (D-twist only)":
|
2021-02-14 13:06:57 +01:00
|
|
|
|
proc test_fp12_abcdefghij00(C: static Curve, gen: static RandomGen) =
|
|
|
|
|
when C.getSexticTwist() == D_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f0 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [x0, z0]),
|
|
|
|
|
Fp4[C](coords: [y0, Fp2[C]()]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C]()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f1 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [x1, z1]),
|
|
|
|
|
Fp4[C](coords: [y1, Fp2[C]()]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C]()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
2021-02-14 13:06:57 +01:00
|
|
|
|
var rl: Fp12[C]
|
2022-04-04 10:10:36 +02:00
|
|
|
|
rl.prod_xzy000_xzy000_into_abcdefghij00(line0, line1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
|
|
|
|
var f = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var f2 = f
|
|
|
|
|
|
|
|
|
|
f *= rl
|
2022-08-14 09:48:10 +02:00
|
|
|
|
f2.mul_sparse_by_abcdefghij00_cube_over_quad(rl)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
|
|
|
|
check: bool(f == f2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
|
|
|
|
test_fp12_abcdefghij00(curve, gen = Uniform)
|
|
|
|
|
test_fp12_abcdefghij00(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_abcdefghij00(curve, gen = Long01Sequence)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test "Somewhat-sparse 𝔽p12/𝔽p4 mul by the product (ca00b0*ca00b0) of line functions (M-twist only)":
|
|
|
|
|
proc test_fp12_abcdef00ghij(C: static Curve, gen: static RandomGen) =
|
2021-02-14 13:06:57 +01:00
|
|
|
|
when C.getSexticTwist() == M_Twist:
|
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
var x0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z0 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line0 = Line[Fp2[C]](a: x0, b: y0, c: z0)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f0 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [z0, x0]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C](),
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [y0, Fp2[C]()])
|
2021-02-14 13:06:57 +01:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var x1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var y1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
var z1 = rng.random_elem(Fp2[C], gen)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
let line1 = Line[Fp2[C]](a: x1, b: y1, c: z1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
let f1 = Fp12[C](
|
|
|
|
|
coords: [
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [z1, x1]),
|
2021-02-14 13:06:57 +01:00
|
|
|
|
Fp4[C](),
|
2022-04-04 10:10:36 +02:00
|
|
|
|
Fp4[C](coords: [y1, Fp2[C]()])
|
2021-02-14 13:06:57 +01:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2022-04-04 10:10:36 +02:00
|
|
|
|
var r: Fp12[C]
|
|
|
|
|
r.prod(f0, f1)
|
|
|
|
|
|
2021-02-14 13:06:57 +01:00
|
|
|
|
var rl: Fp12[C]
|
2022-04-04 10:10:36 +02:00
|
|
|
|
rl.prod_zx00y0_zx00y0_into_abcd00efghij(line0, line1)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
|
|
|
|
var f = rng.random_elem(Fp12[C], gen)
|
|
|
|
|
var f2 = f
|
|
|
|
|
|
|
|
|
|
f *= rl
|
2022-08-14 09:48:10 +02:00
|
|
|
|
f2.mul_sparse_by_abcd00efghij_cube_over_quad(rl)
|
2021-02-14 13:06:57 +01:00
|
|
|
|
|
|
|
|
|
check: bool(f == f2)
|
|
|
|
|
|
|
|
|
|
staticFor(curve, TestCurves):
|
2022-04-04 10:10:36 +02:00
|
|
|
|
test_fp12_abcdef00ghij(curve, gen = Uniform)
|
|
|
|
|
test_fp12_abcdef00ghij(curve, gen = HighHammingWeight)
|
|
|
|
|
test_fp12_abcdef00ghij(curve, gen = Long01Sequence)
|