nim-poseidon2/tests/poseidon2/testPoseidon2.nim
Mark Spanbroek d8a176930d Pad byte sequences with 0x1
To ensure unique field elements for sequences that
end with 0x0.
2023-11-13 11:46:45 +01:00

44 lines
1.4 KiB
Nim

import std/unittest
import std/math
import std/sequtils
import constantine/math/arithmetic
import constantine/math/io/io_fields
import constantine/math/io/io_bigints
import constantine/serialization/codecs
import poseidon2/types
import poseidon2
suite "poseidon2":
test "merkle root of field elements":
let m = 17
let n = 2^m
var xs: seq[F]
for i in 1..n:
xs.add( toF(i) )
let root = merkleRoot(xs)
check root.toHex(littleEndian) == "0xd1111b3515a663bb48278bfe453fe2508487014a1c6093d3ec5a6db764bbab1e"
test "merkle root of even elements":
let elements = toSeq(1..4).mapIt(toF(it))
let expected = compress(compress(1.toF, 2.toF), compress(3.toF, 4.toF))
check bool(merkleRoot(elements) == expected)
test "merkle root of odd elements":
let elements = toSeq(1..3).mapIt(toF(it))
let expected = compress(compress(1.toF, 2.toF), compress(3.toF, 0.toF))
check bool(merkleRoot(elements) == expected)
test "merkle root of bytes":
let bytes = toSeq 1'u8..80'u8
let root = merkleRoot(bytes)
check root.toHex(littleEndian) == "0x4f317667856aa66125dfa14c09096e08573803c91f87f20e2940238ede66a02d"
test "merkle root of bytes converted to bytes":
let bytes = toSeq 1'u8..80'u8
let rootAsBytes = merkleRoot(bytes).toBytes()
check rootAsBytes.toHex == "0x4f317667856aa66125dfa14c09096e08573803c91f87f20e2940238ede66a02d"