mirror of
https://github.com/codex-storage/constantine.git
synced 2025-02-02 05:55:26 +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
90 lines
3.0 KiB
Nim
90 lines
3.0 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.
|
|
|
|
import std/[unittest,times],
|
|
../../constantine/platforms/abstractions,
|
|
../../constantine/math/config/curves,
|
|
../../constantine/math/arithmetic,
|
|
../../constantine/math/arithmetic/limbs_unsaturated,
|
|
../../constantine/math/io/io_bigints,
|
|
../../helpers/prng_unsafe
|
|
|
|
# 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 "\n------------------------------------------------------\n"
|
|
echo "test_io_unsaturated xoshiro512** seed: ", seed
|
|
|
|
type
|
|
RandomGen = enum
|
|
Uniform
|
|
HighHammingWeight
|
|
Long01Sequence
|
|
|
|
func random_bigint*(rng: var RngState, curve: static Curve, gen: static RandomGen): auto =
|
|
when gen == Uniform:
|
|
rng.random_unsafe(matchingBigInt(curve))
|
|
elif gen == HighHammingWeight:
|
|
rng.random_highHammingWeight(matchingBigInt(curve))
|
|
else:
|
|
rng.random_long01Seq(matchingBigInt(curve))
|
|
|
|
# debug
|
|
import std/strutils
|
|
|
|
proc testRoundtrip(curve: static Curve, gen: static RandomGen) =
|
|
const bits = curve.getCurveBitwidth()
|
|
const Excess = 2
|
|
const UnsatBitwidth = WordBitWidth - Excess
|
|
const N = (bits + UnsatBitwidth-1) div UnsatBitwidth
|
|
|
|
let a = rng.random_bigint(curve, gen)
|
|
var u: LimbsUnsaturated[N, Excess]
|
|
var b: typeof(a)
|
|
|
|
u.fromPackedRepr(a.limbs)
|
|
b.limbs.fromUnsatRepr(u)
|
|
|
|
doAssert bool(a == b), block:
|
|
"\n a: " & a.toHex() &
|
|
"\n b: " & b.toHex()
|
|
|
|
proc main() =
|
|
suite "Packed <-> Unsaturated limbs roundtrips" & " [" & $WordBitWidth & "-bit words]":
|
|
const Iters = 10000
|
|
test "BN254_Snarks":
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(BN254_Snarks, Uniform)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(BN254_Snarks, HighHammingWeight)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(BN254_Snarks, Long01Sequence)
|
|
test "Edwards25519":
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(Edwards25519, Uniform)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(Edwards25519, HighHammingWeight)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(Edwards25519, Long01Sequence)
|
|
test "secp256k1":
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(Secp256k1, Uniform)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(Secp256k1, HighHammingWeight)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(Secp256k1, Long01Sequence)
|
|
test "BLS12-381":
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(BLS12_381, Uniform)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(BLS12_381, HighHammingWeight)
|
|
for _ in 0 ..< Iters:
|
|
testRoundtrip(BLS12_381, Long01Sequence)
|
|
|
|
main() |