diff --git a/tests/poseidon2/fuzzing.nim b/tests/poseidon2/fuzzing.nim new file mode 100644 index 0000000..9affa0c --- /dev/null +++ b/tests/poseidon2/fuzzing.nim @@ -0,0 +1,5 @@ +import std/random + +randomize() +let seed*: int64 = int64.rand() +randomize(seed) diff --git a/tests/poseidon2/reference.nim b/tests/poseidon2/reference.nim new file mode 100644 index 0000000..0e4376b --- /dev/null +++ b/tests/poseidon2/reference.nim @@ -0,0 +1,50 @@ +import std/sequtils +import constantine/math/arithmetic +import constantine/math/io/io_fields +import poseidon2/types +import poseidon2/io +import poseidon2/compress + +const KeyNone = F.fromHex("0x0") +const KeyBottomLayer = F.fromHex("0x1") +const KeyOdd = F.fromHex("0x2") +const KeyOddAndBottomLayer = F.fromhex("0x3") + +# Reference implementation of merkle root algorithm +# Only used in tests +func merkleRoot(xs: openArray[F], isBottomLayer: static bool) : F = + let a = low(xs) + let b = high(xs) + let m = b-a+1 + + when isBottomLayer: + assert m > 0, "merkle root of empty sequence is not defined" + + when not isBottomLayer: + if m==1: + return xs[a] + + let halfn : int = m div 2 + let n : int = 2*halfn + let isOdd : bool = (n != m) + + var ys : seq[F] + if not isOdd: + ys = newSeq[F](halfn) + else: + ys = newSeq[F](halfn+1) + + for i in 0..