mirror of
https://github.com/codex-storage/constantine.git
synced 2025-02-07 00:13:35 +00:00
e5612f5705
* unoptimized msm * MSM: reorder loops * add a signed windowed recoding technique * improve wNAF table access * use batchAffine * revamp EC tests * MSM signed digit support * refactor MSM: recode signed ahead of time * missing test vector * refactor allocs and Alloca sideeffect * add an endomorphism threshold * Add Jacobian extended coordinates * refactor recodings, prepare for parallelizable on-the-fly signed recoding * recoding changes, introduce proper NAF for pairings * more pairings refactoring, introduce miller accumulator for EVM * some optim to the addchain miller loop * start optimizing multi-pairing * finish multi-miller loop refactoring * minor tuning * MSM: signed encoding suitable for parallelism (no precompute) * cleanup signed window encoding * add prefetching * add metering * properly init result to infinity * comment on prefetching * introduce vartime inversion for batch additions * fix JacExt infinity conversion * add batchAffine for MSM, though slower than JacExtended at the moment * add a batch affine scheduler for MSM * Add Multi-Scalar-Multiplication endomorphism acceleration * some tuning * signed integer fixes + 32-bit + tuning * Some more tuning * common msm bench + don't use affine for c < 9 * nit
147 lines
4.7 KiB
Nim
147 lines
4.7 KiB
Nim
# 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 elliptic curve operations
|
|
#
|
|
# ############################################################
|
|
|
|
import
|
|
# Standard library
|
|
std/[unittest, times],
|
|
# Internals
|
|
../../constantine/platforms/abstractions,
|
|
../../constantine/math/[arithmetic, extension_fields],
|
|
../../constantine/math/elliptic/[
|
|
ec_shortweierstrass_affine,
|
|
ec_shortweierstrass_jacobian,
|
|
ec_shortweierstrass_projective,
|
|
ec_shortweierstrass_batch_ops_parallel],
|
|
../../constantine/platforms/threadpool/threadpool,
|
|
# Test utilities
|
|
../../helpers/prng_unsafe
|
|
|
|
export unittest, abstractions, arithmetic # Generic sandwich
|
|
|
|
type
|
|
RandomGen* = enum
|
|
Uniform
|
|
HighHammingWeight
|
|
Long01Sequence
|
|
|
|
func random_point*(rng: var RngState, EC: typedesc, randZ: bool, gen: RandomGen): EC {.noInit.} =
|
|
when EC is ECP_ShortW_Aff:
|
|
if gen == Uniform:
|
|
result = rng.random_unsafe(EC)
|
|
elif gen == HighHammingWeight:
|
|
result = rng.random_highHammingWeight(EC)
|
|
else:
|
|
result = rng.random_long01Seq(EC)
|
|
else:
|
|
if not randZ:
|
|
if gen == Uniform:
|
|
result = rng.random_unsafe(EC)
|
|
elif gen == HighHammingWeight:
|
|
result = rng.random_highHammingWeight(EC)
|
|
else:
|
|
result = rng.random_long01Seq(EC)
|
|
else:
|
|
if gen == Uniform:
|
|
result = rng.random_unsafe_with_randZ(EC)
|
|
elif gen == HighHammingWeight:
|
|
result = rng.random_highHammingWeight_with_randZ(EC)
|
|
else:
|
|
result = rng.random_long01Seq_with_randZ(EC)
|
|
|
|
|
|
proc run_EC_batch_add_parallel_impl*[N: static int](
|
|
ec: typedesc,
|
|
numPoints: array[N, int],
|
|
moduleName: string
|
|
) =
|
|
|
|
# Random seed for reproducibility
|
|
var rng: RngState
|
|
let seed = 1674654772 # uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
|
|
rng.seed(seed)
|
|
echo "\n------------------------------------------------------\n"
|
|
echo moduleName, " xoshiro512** seed: ", seed
|
|
|
|
when ec.G == G1:
|
|
const G1_or_G2 = "G1"
|
|
else:
|
|
const G1_or_G2 = "G2"
|
|
|
|
const testSuiteDesc = "Elliptic curve parallel sum reduction for Short Weierstrass form"
|
|
|
|
suite testSuiteDesc & " - " & $ec & " - [" & $WordBitWidth & "-bit mode]":
|
|
|
|
for n in numPoints:
|
|
test $ec & " sum reduction (N=" & $n & ")":
|
|
proc test(EC: typedesc, gen: RandomGen) =
|
|
var tp = Threadpool.new()
|
|
defer: tp.shutdown()
|
|
|
|
var points = newSeq[ECP_ShortW_Aff[EC.F, EC.G]](n)
|
|
|
|
for i in 0 ..< n:
|
|
points[i] = rng.random_point(ECP_ShortW_Aff[EC.F, EC.G], randZ = false, gen)
|
|
|
|
var r_batch{.noinit.}, r_ref{.noInit.}: EC
|
|
|
|
r_ref.setInf()
|
|
for i in 0 ..< n:
|
|
r_ref += points[i]
|
|
|
|
tp.sum_reduce_vartime_parallel(r_batch, points)
|
|
|
|
check: bool(r_batch == r_ref)
|
|
|
|
|
|
test(ec, gen = Uniform)
|
|
test(ec, gen = HighHammingWeight)
|
|
test(ec, gen = Long01Sequence)
|
|
|
|
test "EC " & G1_or_G2 & " sum reduction (N=" & $n & ") - special cases":
|
|
proc test(EC: typedesc, gen: RandomGen) =
|
|
var tp = Threadpool.new()
|
|
defer: tp.shutdown()
|
|
|
|
var points = newSeq[ECP_ShortW_Aff[EC.F, EC.G]](n)
|
|
|
|
let halfN = n div 2
|
|
|
|
for i in 0 ..< halfN:
|
|
points[i] = rng.random_point(ECP_ShortW_Aff[EC.F, EC.G], randZ = false, gen)
|
|
|
|
for i in halfN ..< n:
|
|
# The special cases test relies on internal knowledge that we sum(points[i], points[i+n/2]
|
|
# It should be changed if scheduling change, for example if we sum(points[2*i], points[2*i+1])
|
|
let c = rng.random_unsafe(3)
|
|
if c == 0:
|
|
points[i] = rng.random_point(ECP_ShortW_Aff[EC.F, EC.G], randZ = false, gen)
|
|
elif c == 1:
|
|
points[i] = points[i-halfN]
|
|
else:
|
|
points[i].neg(points[i-halfN])
|
|
|
|
var r_batch{.noinit.}, r_ref{.noInit.}: EC
|
|
|
|
r_ref.setInf()
|
|
for i in 0 ..< n:
|
|
r_ref += points[i]
|
|
|
|
tp.sum_reduce_vartime_parallel(r_batch, points)
|
|
|
|
check: bool(r_batch == r_ref)
|
|
|
|
test(ec, gen = Uniform)
|
|
test(ec, gen = HighHammingWeight)
|
|
test(ec, gen = Long01Sequence)
|