2020-02-25 22:52:56 +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.
|
|
|
|
|
|
|
|
import
|
2022-02-27 00:49:08 +00:00
|
|
|
../constantine/platforms/abstractions,
|
|
|
|
../constantine/math/arithmetic,
|
2022-08-16 10:07:57 +00:00
|
|
|
../constantine/math/arithmetic/limbs_montgomery,
|
2022-02-27 00:49:08 +00:00
|
|
|
../constantine/math/config/curves,
|
|
|
|
../constantine/math/elliptic/[
|
2020-10-01 22:01:09 +00:00
|
|
|
ec_shortweierstrass_affine,
|
|
|
|
ec_shortweierstrass_projective,
|
2021-12-29 00:54:17 +00:00
|
|
|
ec_shortweierstrass_jacobian,
|
|
|
|
ec_twistededwards_affine,
|
|
|
|
ec_twistededwards_projective],
|
2022-02-27 00:49:08 +00:00
|
|
|
../constantine/math/io/io_bigints,
|
|
|
|
../constantine/math/extension_fields/towers
|
2020-02-25 22:52:56 +00:00
|
|
|
|
|
|
|
# ############################################################
|
|
|
|
#
|
|
|
|
# Pseudo-Random Number Generator
|
2020-04-14 18:02:21 +00:00
|
|
|
# Unsafe: for testing and benchmarking purposes
|
2020-02-25 22:52:56 +00:00
|
|
|
#
|
|
|
|
# ############################################################
|
|
|
|
#
|
|
|
|
# Our field elements for elliptic curve cryptography
|
|
|
|
# are in the 2^256~2^512 range.
|
|
|
|
# For pairings, with embedding degrees of 12 to 48
|
|
|
|
# We would need 12~48 field elements per point on the curve
|
|
|
|
#
|
|
|
|
# The recommendation by Vigna at http://prng.di.unimi.it
|
|
|
|
# is to have a period of t^2 if we need t values (i.e. about 2^1024)
|
|
|
|
# but also that for all practical purposes 2^256 period is enough
|
|
|
|
#
|
|
|
|
# We use 2^512 to cover the range the base field elements
|
|
|
|
|
|
|
|
type RngState* = object
|
2020-04-14 18:02:21 +00:00
|
|
|
## This is the state of a Xoshiro512** PRNG
|
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2020-02-25 22:52:56 +00:00
|
|
|
s: array[8, uint64]
|
|
|
|
|
|
|
|
func splitMix64(state: var uint64): uint64 =
|
|
|
|
state += 0x9e3779b97f4a7c15'u64
|
|
|
|
result = state
|
|
|
|
result = (result xor (result shr 30)) * 0xbf58476d1ce4e5b9'u64
|
|
|
|
result = (result xor (result shr 27)) * 0xbf58476d1ce4e5b9'u64
|
|
|
|
result = result xor (result shr 31)
|
|
|
|
|
|
|
|
func seed*(rng: var RngState, x: SomeInteger) =
|
|
|
|
## Seed the random number generator with a fixed seed
|
|
|
|
var sm64 = uint64(x)
|
|
|
|
rng.s[0] = splitMix64(sm64)
|
|
|
|
rng.s[1] = splitMix64(sm64)
|
|
|
|
rng.s[2] = splitMix64(sm64)
|
|
|
|
rng.s[3] = splitMix64(sm64)
|
|
|
|
rng.s[4] = splitMix64(sm64)
|
|
|
|
rng.s[5] = splitMix64(sm64)
|
|
|
|
rng.s[6] = splitMix64(sm64)
|
|
|
|
rng.s[7] = splitMix64(sm64)
|
|
|
|
|
|
|
|
func rotl(x: uint64, k: static int): uint64 {.inline.} =
|
|
|
|
return (x shl k) or (x shr (64 - k))
|
|
|
|
|
|
|
|
template `^=`(x: var uint64, y: uint64) =
|
|
|
|
x = x xor y
|
|
|
|
|
|
|
|
func next(rng: var RngState): uint64 =
|
|
|
|
## Compute a random uint64 from the input state
|
|
|
|
## using xoshiro512** algorithm by Vigna et al
|
|
|
|
## State is updated.
|
|
|
|
result = rotl(rng.s[1] * 5, 7) * 9
|
|
|
|
|
|
|
|
let t = rng.s[1] shl 11
|
|
|
|
rng.s[2] ^= rng.s[0];
|
|
|
|
rng.s[5] ^= rng.s[1];
|
|
|
|
rng.s[1] ^= rng.s[2];
|
|
|
|
rng.s[7] ^= rng.s[3];
|
|
|
|
rng.s[3] ^= rng.s[4];
|
|
|
|
rng.s[4] ^= rng.s[5];
|
|
|
|
rng.s[0] ^= rng.s[6];
|
|
|
|
rng.s[6] ^= rng.s[7];
|
|
|
|
|
|
|
|
rng.s[6] ^= t;
|
|
|
|
|
|
|
|
rng.s[7] = rotl(rng.s[7], 21);
|
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
# Integer ranges
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
func random_unsafe*(rng: var RngState, maxExclusive: uint32): uint32 =
|
|
|
|
## Generate a random integer in 0 ..< maxExclusive
|
|
|
|
## Uses an unbiaised generation method
|
|
|
|
## See Lemire's algorithm modified by Melissa O'Neill
|
|
|
|
## https://www.pcg-random.org/posts/bounded-rands.html
|
|
|
|
let max = maxExclusive
|
|
|
|
var x = uint32 rng.next()
|
|
|
|
var m = x.uint64 * max.uint64
|
|
|
|
var l = uint32 m
|
|
|
|
if l < max:
|
|
|
|
var t = not(max) + 1 # -max
|
|
|
|
if t >= max:
|
|
|
|
t -= max
|
|
|
|
if t >= max:
|
|
|
|
t = t mod max
|
|
|
|
while l < t:
|
|
|
|
x = uint32 rng.next()
|
|
|
|
m = x.uint64 * max.uint64
|
|
|
|
l = uint32 m
|
|
|
|
return uint32(m shr 32)
|
|
|
|
|
|
|
|
func random_unsafe*[T: SomeInteger](rng: var RngState, inclRange: Slice[T]): T =
|
|
|
|
## Return a random integer in the given range.
|
|
|
|
## The range bounds must fit in an int32.
|
|
|
|
let maxExclusive = inclRange.b + 1 - inclRange.a
|
|
|
|
result = T(rng.random_unsafe(uint32 maxExclusive))
|
|
|
|
result += inclRange.a
|
|
|
|
|
|
|
|
# Containers
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
func sample_unsafe*[T](rng: var RngState, src: openarray[T]): T =
|
|
|
|
## Return a random sample from an array
|
|
|
|
result = src[rng.random_unsafe(uint32 src.len)]
|
|
|
|
|
2020-04-13 17:25:59 +00:00
|
|
|
# BigInts and Fields
|
|
|
|
# ------------------------------------------------------------
|
2020-06-20 16:55:27 +00:00
|
|
|
#
|
|
|
|
# Statistics note:
|
|
|
|
# - A skewed distribution is not symmetric, it has a longer tail in one direction.
|
|
|
|
# for example a RNG that is not centered over 0.5 distribution of 0 and 1 but
|
|
|
|
# might produces more 1 than 0 or vice-versa.
|
|
|
|
# - A bias is a result that is consistently off from the true value i.e.
|
|
|
|
# a deviation of an estimate from the quantity under observation
|
|
|
|
|
2022-08-16 10:07:57 +00:00
|
|
|
func reduceViaMont[N: static int, F](reduced: var array[N, SecretWord], unreduced: array[2*N, SecretWord], _: typedesc[F]) =
|
|
|
|
# reduced.reduce(unreduced, FF.fieldMod())
|
|
|
|
# ----------------------------------------
|
|
|
|
# With R ≡ (2^WordBitWidth)^numWords (mod p)
|
|
|
|
# redc2xMont(a) computes a/R (mod p)
|
|
|
|
# mulMont(a, b) computes a.b.R⁻¹ (mod p)
|
|
|
|
# Hence with b = R², this computes a (mod p).
|
|
|
|
# Montgomery reduction works word by word, quadratically
|
|
|
|
# so for 384-bit prime (6-words on 64-bit CPUs), so 6*6 = 36, twice for multiplication and reduction
|
|
|
|
# significantly faster than division which works bit-by-bit due to constant-time requirement
|
|
|
|
reduced.redc2xMont(unreduced, # a/R
|
|
|
|
F.fieldMod().limbs, F.getNegInvModWord(),
|
|
|
|
F.getSpareBits())
|
|
|
|
reduced.mulMont(reduced, F.getR2modP().limbs, # (a/R) * R² * R⁻¹ ≡ a (mod p)
|
|
|
|
F.fieldMod().limbs, F.getNegInvModWord(),
|
|
|
|
F.getSpareBits())
|
|
|
|
|
|
|
|
func random_unsafe(rng: var RngState, a: var Limbs) =
|
|
|
|
## Initialize standalone limbs
|
|
|
|
for i in 0 ..< a.len:
|
|
|
|
a[i] = SecretWord(rng.next())
|
|
|
|
|
2022-09-15 07:33:34 +00:00
|
|
|
template clearExtraBitsOverMSB(a: var BigInt) =
|
|
|
|
## If we do bit manipulation at the word level,
|
|
|
|
## for example a 381-bit BigInt stored in a 384-bit buffer
|
|
|
|
## we need to clear the upper 3-bit
|
|
|
|
when a.bits != a.limbs.len * WordBitWidth:
|
|
|
|
const posExtraBits = a.bits - (a.limbs.len-1) * WordBitWidth
|
|
|
|
const mask = (One shl posExtraBits) - One
|
|
|
|
a.limbs[^1] = a.limbs[^1] and mask
|
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
func random_unsafe(rng: var RngState, a: var BigInt) =
|
|
|
|
## Initialize a standalone BigInt
|
2022-08-16 10:07:57 +00:00
|
|
|
rng.random_unsafe(a.limbs)
|
2022-09-15 07:33:34 +00:00
|
|
|
a.clearExtraBitsOverMSB()
|
2020-02-25 22:52:56 +00:00
|
|
|
|
2021-01-21 23:09:52 +00:00
|
|
|
func random_unsafe(rng: var RngState, a: var FF) =
|
|
|
|
## Initialize a Field element
|
2020-04-14 18:02:21 +00:00
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2022-08-16 10:07:57 +00:00
|
|
|
var unreduced{.noinit.}: Limbs[2*a.mres.limbs.len]
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_unsafe(unreduced)
|
2022-08-16 10:07:57 +00:00
|
|
|
a.mres.limbs.reduceViaMont(unreduced, FF)
|
2020-02-25 22:52:56 +00:00
|
|
|
|
2021-01-21 23:09:52 +00:00
|
|
|
func random_unsafe(rng: var RngState, a: var ExtensionField) =
|
|
|
|
## Recursively initialize an extension Field element
|
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2021-02-07 13:03:56 +00:00
|
|
|
for i in 0 ..< a.coords.len:
|
|
|
|
rng.random_unsafe(a.coords[i])
|
2020-02-25 22:52:56 +00:00
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
func random_word_highHammingWeight(rng: var RngState): BaseType =
|
|
|
|
let numZeros = rng.random_unsafe(WordBitWidth div 3) # Average Hamming Weight is 1-0.33/2 = 0.83
|
|
|
|
result = high(BaseType)
|
|
|
|
for _ in 0 ..< numZeros:
|
|
|
|
result = result.clearBit rng.random_unsafe(WordBitWidth)
|
|
|
|
|
2022-08-16 10:07:57 +00:00
|
|
|
func random_highHammingWeight(rng: var RngState, a: var Limbs) =
|
|
|
|
## Initialize standalone limbs
|
|
|
|
## with high Hamming weight
|
|
|
|
## to have a higher probability of triggering carries
|
|
|
|
for i in 0 ..< a.len:
|
|
|
|
a[i] = SecretWord rng.random_word_highHammingWeight()
|
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
func random_highHammingWeight(rng: var RngState, a: var BigInt) =
|
2020-06-04 18:37:29 +00:00
|
|
|
## Initialize a standalone BigInt
|
2020-06-20 16:55:27 +00:00
|
|
|
## with high Hamming weight
|
|
|
|
## to have a higher probability of triggering carries
|
2022-08-16 10:07:57 +00:00
|
|
|
rng.random_highHammingWeight(a.limbs)
|
2022-09-15 07:33:34 +00:00
|
|
|
a.clearExtraBitsOverMSB()
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-01-21 23:09:52 +00:00
|
|
|
func random_highHammingWeight(rng: var RngState, a: var FF) =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Recursively initialize a BigInt (part of a field) or Field element
|
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
|
|
|
## The result will have a high Hamming Weight
|
|
|
|
## to have a higher probability of triggering carries
|
2022-08-16 10:07:57 +00:00
|
|
|
var unreduced{.noinit.}: Limbs[2*a.mres.limbs.len]
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_highHammingWeight(unreduced)
|
2022-08-16 10:07:57 +00:00
|
|
|
a.mres.limbs.reduceViaMont(unreduced, FF)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-01-21 23:09:52 +00:00
|
|
|
func random_highHammingWeight(rng: var RngState, a: var ExtensionField) =
|
|
|
|
## Recursively initialize an extension Field element
|
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2021-02-07 13:03:56 +00:00
|
|
|
for i in 0 ..< a.coords.len:
|
|
|
|
rng.random_highHammingWeight(a.coords[i])
|
2020-06-20 16:55:27 +00:00
|
|
|
|
|
|
|
func random_long01Seq(rng: var RngState, a: var openArray[byte]) =
|
|
|
|
## Initialize a bytearray
|
|
|
|
## It is skewed towards producing strings of 1111... and 0000
|
|
|
|
## to trigger edge cases
|
|
|
|
# See libsecp256k1: https://github.com/bitcoin-core/secp256k1/blob/dbd41db1/src/testrand_impl.h#L90-L104
|
|
|
|
let Bits = a.len * 8
|
|
|
|
var bit = 0
|
|
|
|
zeroMem(a[0].addr, a.len)
|
|
|
|
while bit < Bits :
|
|
|
|
var now = 1 + (rng.random_unsafe(1 shl 6) * rng.random_unsafe(1 shl 5) + 16) div 31
|
|
|
|
let val = rng.sample_unsafe([0, 1])
|
|
|
|
while now > 0 and bit < Bits:
|
|
|
|
a[bit shr 3] = a[bit shr 3] or byte(val shl (bit and 7))
|
|
|
|
dec now
|
|
|
|
inc bit
|
|
|
|
|
|
|
|
func random_long01Seq(rng: var RngState, a: var BigInt) =
|
|
|
|
## Initialize a bigint
|
|
|
|
## It is skewed towards producing strings of 1111... and 0000
|
|
|
|
## to trigger edge cases
|
|
|
|
var buf: array[(a.bits + 7) div 8, byte]
|
|
|
|
rng.random_long01Seq(buf)
|
|
|
|
let order = rng.sample_unsafe([bigEndian, littleEndian])
|
|
|
|
if order == bigEndian:
|
2022-02-28 08:23:26 +00:00
|
|
|
a.unmarshal(buf, bigEndian)
|
2020-06-20 16:55:27 +00:00
|
|
|
else:
|
2022-02-28 08:23:26 +00:00
|
|
|
a.unmarshal(buf, littleEndian)
|
2022-09-15 07:33:34 +00:00
|
|
|
a.clearExtraBitsOverMSB()
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2022-08-16 10:07:57 +00:00
|
|
|
func random_long01Seq(rng: var RngState, a: var Limbs) =
|
|
|
|
## Initialize standalone limbs
|
|
|
|
## It is skewed towards producing strings of 1111... and 0000
|
|
|
|
## to trigger edge cases
|
|
|
|
const bits = a.len*WordBitWidth
|
|
|
|
|
|
|
|
var t{.noInit.}: BigInt[bits]
|
|
|
|
rng.random_long01Seq(t)
|
|
|
|
a = t.limbs
|
|
|
|
|
2021-01-21 23:09:52 +00:00
|
|
|
func random_long01Seq(rng: var RngState, a: var FF) =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Recursively initialize a BigInt (part of a field) or Field element
|
|
|
|
## It is skewed towards producing strings of 1111... and 0000
|
|
|
|
## to trigger edge cases
|
2022-08-16 10:07:57 +00:00
|
|
|
var unreduced{.noinit.}: Limbs[2*a.mres.limbs.len]
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_long01Seq(unreduced)
|
2022-08-16 10:07:57 +00:00
|
|
|
a.mres.limbs.reduceViaMont(unreduced, FF)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-01-21 23:09:52 +00:00
|
|
|
func random_long01Seq(rng: var RngState, a: var ExtensionField) =
|
|
|
|
## Recursively initialize an extension Field element
|
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2021-02-07 13:03:56 +00:00
|
|
|
for i in 0 ..< a.coords.len:
|
|
|
|
rng.random_long01Seq(a.coords[i])
|
2020-06-04 18:37:29 +00:00
|
|
|
|
2020-04-13 17:25:59 +00:00
|
|
|
# Elliptic curves
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
type ECP = ECP_ShortW_Aff or ECP_ShortW_Prj or ECP_ShortW_Jac or
|
|
|
|
ECP_TwEdwards_Aff or ECP_TwEdwards_Prj
|
|
|
|
type ECP_ext = ECP_ShortW_Prj or ECP_ShortW_Jac or
|
|
|
|
ECP_TwEdwards_Prj
|
|
|
|
|
|
|
|
template trySetFromCoord[F](a: ECP, fieldElem: F): SecretBool =
|
|
|
|
when a is (ECP_ShortW_Aff or ECP_ShortW_Prj or ECP_ShortW_Jac):
|
|
|
|
trySetFromCoordX(a, fieldElem)
|
|
|
|
else:
|
|
|
|
trySetFromCoordY(a, fieldElem)
|
|
|
|
|
|
|
|
template trySetFromCoords[F](a: ECP, fieldElem, scale: F): SecretBool =
|
|
|
|
when a is (ECP_ShortW_Prj or ECP_ShortW_Jac):
|
|
|
|
trySetFromCoordsXandZ(a, fieldElem, scale)
|
|
|
|
else:
|
|
|
|
trySetFromCoordsYandZ(a, fieldElem, scale)
|
|
|
|
|
|
|
|
func random_unsafe(rng: var RngState, a: var ECP) =
|
2020-04-13 17:25:59 +00:00
|
|
|
## Initialize a random curve point with Z coordinate == 1
|
2020-04-14 18:02:21 +00:00
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2020-10-09 05:51:47 +00:00
|
|
|
var fieldElem {.noInit.}: a.F
|
2020-04-13 17:25:59 +00:00
|
|
|
var success = CtFalse
|
|
|
|
|
|
|
|
while not bool(success):
|
|
|
|
# Euler's criterion: there are (p-1)/2 squares in a field with modulus `p`
|
|
|
|
# so we have a probability of ~0.5 to get a good point
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_unsafe(fieldElem)
|
2021-12-29 00:54:17 +00:00
|
|
|
success = trySetFromCoord(a, fieldElem)
|
2020-04-13 17:25:59 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_unsafe_with_randZ(rng: var RngState, a: var ECP_ext) =
|
2020-04-13 17:25:59 +00:00
|
|
|
## Initialize a random curve point with Z coordinate being random
|
2020-04-14 18:02:21 +00:00
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2020-10-09 05:51:47 +00:00
|
|
|
var Z{.noInit.}: a.F
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_unsafe(Z) # If Z is zero, X will be zero and that will be an infinity point
|
2020-04-13 17:25:59 +00:00
|
|
|
|
2020-10-09 05:51:47 +00:00
|
|
|
var fieldElem {.noInit.}: a.F
|
2020-04-13 17:25:59 +00:00
|
|
|
var success = CtFalse
|
|
|
|
|
|
|
|
while not bool(success):
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_unsafe(fieldElem)
|
2021-12-29 00:54:17 +00:00
|
|
|
success = trySetFromCoords(a, fieldElem, Z)
|
2020-04-13 17:25:59 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_highHammingWeight(rng: var RngState, a: var ECP) =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Initialize a random curve point with Z coordinate == 1
|
|
|
|
## This will be generated with a biaised RNG with high Hamming Weight
|
|
|
|
## to trigger carry bugs
|
2020-10-09 05:51:47 +00:00
|
|
|
var fieldElem {.noInit.}: a.F
|
2020-06-20 16:55:27 +00:00
|
|
|
var success = CtFalse
|
2020-04-14 18:02:21 +00:00
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
while not bool(success):
|
|
|
|
# Euler's criterion: there are (p-1)/2 squares in a field with modulus `p`
|
|
|
|
# so we have a probability of ~0.5 to get a good point
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_highHammingWeight(fieldElem)
|
2021-12-29 00:54:17 +00:00
|
|
|
success = trySetFromCoord(a, fieldElem)
|
2020-04-14 18:02:21 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_highHammingWeight_with_randZ(rng: var RngState, a: var ECP_ext) =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Initialize a random curve point with Z coordinate == 1
|
|
|
|
## This will be generated with a biaised RNG with high Hamming Weight
|
|
|
|
## to trigger carry bugs
|
2020-10-09 05:51:47 +00:00
|
|
|
var Z{.noInit.}: a.F
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_highHammingWeight(Z) # If Z is zero, X will be zero and that will be an infinity point
|
2020-04-13 17:25:59 +00:00
|
|
|
|
2020-10-09 05:51:47 +00:00
|
|
|
var fieldElem {.noInit.}: a.F
|
2020-06-20 16:55:27 +00:00
|
|
|
var success = CtFalse
|
2020-04-14 18:46:39 +00:00
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
while not bool(success):
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_highHammingWeight(fieldElem)
|
2021-12-29 00:54:17 +00:00
|
|
|
success = trySetFromCoords(a, fieldElem, Z)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_long01Seq(rng: var RngState, a: var ECP) =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Initialize a random curve point with Z coordinate == 1
|
|
|
|
## This will be generated with a biaised RNG
|
|
|
|
## that produces long bitstrings of 0 and 1
|
|
|
|
## to trigger edge cases
|
2020-10-09 05:51:47 +00:00
|
|
|
var fieldElem {.noInit.}: a.F
|
2020-06-20 16:55:27 +00:00
|
|
|
var success = CtFalse
|
|
|
|
|
|
|
|
while not bool(success):
|
|
|
|
# Euler's criterion: there are (p-1)/2 squares in a field with modulus `p`
|
|
|
|
# so we have a probability of ~0.5 to get a good point
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_long01Seq(fieldElem)
|
2021-12-29 00:54:17 +00:00
|
|
|
success = trySetFromCoord(a, fieldElem)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_long01Seq_with_randZ(rng: var RngState, a: var ECP_ext) =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Initialize a random curve point with Z coordinate == 1
|
|
|
|
## This will be generated with a biaised RNG
|
|
|
|
## that produces long bitstrings of 0 and 1
|
|
|
|
## to trigger edge cases
|
2020-10-09 05:51:47 +00:00
|
|
|
var Z{.noInit.}: a.F
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_long01Seq(Z) # If Z is zero, X will be zero and that will be an infinity point
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2020-10-09 05:51:47 +00:00
|
|
|
var fieldElem {.noInit.}: a.F
|
2020-06-20 16:55:27 +00:00
|
|
|
var success = CtFalse
|
|
|
|
|
|
|
|
while not bool(success):
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_long01Seq(fieldElem)
|
2021-12-29 00:54:17 +00:00
|
|
|
success = trySetFromCoords(a, fieldElem, Z)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
|
|
|
# Generic over any Constantine type
|
|
|
|
# ------------------------------------------------------------
|
2020-04-14 18:02:21 +00:00
|
|
|
|
|
|
|
func random_unsafe*(rng: var RngState, T: typedesc): T =
|
2020-04-13 17:25:59 +00:00
|
|
|
## Create a random Field or Extension Field or Curve Element
|
2020-04-14 18:02:21 +00:00
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
2021-12-29 00:54:17 +00:00
|
|
|
when T is ECP:
|
2020-04-14 18:02:21 +00:00
|
|
|
rng.random_unsafe(result)
|
2020-04-14 18:46:39 +00:00
|
|
|
elif T is SomeNumber:
|
|
|
|
cast[T](rng.next()) # TODO: Rely on casting integer actually converting in C (i.e. uint64->uint32 is valid)
|
2020-06-04 18:37:29 +00:00
|
|
|
elif T is BigInt:
|
|
|
|
rng.random_unsafe(result)
|
|
|
|
else: # Fields
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_unsafe(result)
|
2020-04-13 17:25:59 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_unsafe_with_randZ*(rng: var RngState, T: typedesc[ECP_ext]): T =
|
2020-04-13 17:25:59 +00:00
|
|
|
## Create a random curve element with a random Z coordinate
|
2020-04-14 18:02:21 +00:00
|
|
|
## Unsafe: for testing and benchmarking purposes only
|
|
|
|
rng.random_unsafe_with_randZ(result)
|
|
|
|
|
2020-06-20 16:55:27 +00:00
|
|
|
func random_highHammingWeight*(rng: var RngState, T: typedesc): T =
|
|
|
|
## Create a random Field or Extension Field or Curve Element
|
|
|
|
## Skewed towards high Hamming Weight
|
2021-12-29 00:54:17 +00:00
|
|
|
when T is ECP:
|
2020-06-20 16:55:27 +00:00
|
|
|
rng.random_highHammingWeight(result)
|
|
|
|
elif T is SomeNumber:
|
|
|
|
cast[T](rng.next()) # TODO: Rely on casting integer actually converting in C (i.e. uint64->uint32 is valid)
|
|
|
|
elif T is BigInt:
|
|
|
|
rng.random_highHammingWeight(result)
|
|
|
|
else: # Fields
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_highHammingWeight(result)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_highHammingWeight_with_randZ*(rng: var RngState, T: typedesc[ECP_ext]): T =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Create a random curve element with a random Z coordinate
|
|
|
|
## Skewed towards high Hamming Weight
|
|
|
|
rng.random_highHammingWeight_with_randZ(result)
|
|
|
|
|
|
|
|
func random_long01Seq*(rng: var RngState, T: typedesc): T =
|
|
|
|
## Create a random Field or Extension Field or Curve Element
|
|
|
|
## Skewed towards long bitstrings of 0 or 1
|
2021-12-29 00:54:17 +00:00
|
|
|
when T is ECP:
|
2020-06-20 16:55:27 +00:00
|
|
|
rng.random_long01Seq(result)
|
|
|
|
elif T is SomeNumber:
|
|
|
|
cast[T](rng.next()) # TODO: Rely on casting integer actually converting in C (i.e. uint64->uint32 is valid)
|
|
|
|
elif T is BigInt:
|
|
|
|
rng.random_long01Seq(result)
|
|
|
|
else: # Fields
|
2021-01-21 23:09:52 +00:00
|
|
|
rng.random_long01Seq(result)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
2021-12-29 00:54:17 +00:00
|
|
|
func random_long01Seq_with_randZ*(rng: var RngState, T: typedesc[ECP_ext]): T =
|
2020-06-20 16:55:27 +00:00
|
|
|
## Create a random curve element with a random Z coordinate
|
|
|
|
## Skewed towards long bitstrings of 0 or 1
|
|
|
|
rng.random_long01Seq_with_randZ(result)
|
|
|
|
|
2020-12-15 18:18:36 +00:00
|
|
|
# Byte sequences
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
func random_byte_seq*(rng: var RngState, length: int): seq[byte] =
|
|
|
|
result.newSeq(length)
|
|
|
|
for b in result.mitems:
|
|
|
|
b = byte rng.next()
|
|
|
|
|
2020-04-14 18:02:21 +00:00
|
|
|
# Sanity checks
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
when isMainModule:
|
2020-06-20 16:55:27 +00:00
|
|
|
import std/[tables, times, strutils]
|
2020-04-14 18:02:21 +00:00
|
|
|
|
|
|
|
var rng: RngState
|
|
|
|
let timeSeed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
|
|
|
|
rng.seed(timeSeed)
|
|
|
|
echo "prng_sanity_checks xoshiro512** seed: ", timeSeed
|
|
|
|
|
|
|
|
|
|
|
|
proc test[T](s: Slice[T]) =
|
|
|
|
var c = initCountTable[int]()
|
|
|
|
|
|
|
|
for _ in 0 ..< 1_000_000:
|
|
|
|
c.inc(rng.random_unsafe(s))
|
|
|
|
|
|
|
|
echo "1'000'000 pseudo-random outputs from ", s.a, " to ", s.b, " (incl): ", c
|
|
|
|
|
|
|
|
test(0..1)
|
|
|
|
test(0..2)
|
|
|
|
test(1..52)
|
|
|
|
test(-10..10)
|
2020-06-20 16:55:27 +00:00
|
|
|
|
|
|
|
echo "\n-----------------------------\n"
|
|
|
|
echo "High Hamming Weight check"
|
|
|
|
for _ in 0 ..< 10:
|
|
|
|
let word = rng.random_word_highHammingWeight()
|
|
|
|
echo "0b", cast[BiggestInt](word).toBin(WordBitWidth), " - 0x", word.toHex()
|
|
|
|
|
|
|
|
echo "\n-----------------------------\n"
|
|
|
|
echo "Long strings of 0 or 1 check"
|
|
|
|
for _ in 0 ..< 10:
|
|
|
|
var a: BigInt[127]
|
|
|
|
rng.random_long01seq(a)
|
|
|
|
stdout.write "0b"
|
|
|
|
for word in a.limbs:
|
|
|
|
stdout.write cast[BiggestInt](word).toBin(WordBitWidth)
|
|
|
|
stdout.write " - 0x"
|
|
|
|
for word in a.limbs:
|
|
|
|
stdout.write word.BaseType.toHex()
|
|
|
|
stdout.write '\n'
|