nim-codex/tests/codex/merkletree/testposeidon2tree.nim
Dmitriy Ryajov fffb674bba
Integrate slot builder (#666)
* rework merkle tree support

* rename merkletree -> codexmerkletree

* treed and proof encoding/decoding

* style

* adding codex merkle and coders tests

* use default hash codec

* proof size changed

* add from nodes test

* shorte file names

* wip poseidon tree

* shorten file names

* root returns a result

* import poseidon tests

* fix merge issues and cleanup a few warnings

* setting up slot builder

* Getting cids in slot

* ensures blocks are devisable by number of slots

* wip

* Implements indexing strategies

* Swaps in indexing strategy into erasure.

* wires slot and indexing tests up

* Fixes issue where indexing strategy stepped gives wrong values for smallest of ranges

* debugs indexing strategies

* Can select slot blocks

* finding number of pad cells

* Implements building slot tree

* finishes implementing slot builder

* Adds check that block size is a multiple of cell size

* Cleanup slotbuilder

* Review comments by Tomasz

* Fixes issue where ecK was used as numberOfSlots.

* rework merkle tree support

* deps

* rename merkletree -> codexmerkletree

* treed and proof encoding/decoding

* style

* adding codex merkle and coders tests

* remove new codecs for now

* proof size changed

* add from nodes test

* shorte file names

* wip poseidon tree

* shorten file names

* fix bad `elements` iter

* bump

* bump

* wip

* reworking slotbuilder

* move out of manifest

* expose getCidAndProof

* import index strat...

* remove getMHash

* remove unused artifacts

* alias zero

* add digest for multihash

* merge issues

* remove unused hashes

* add option to result converter

* misc

* fix tests

* add helper to derive EC block count

* rename method

* misc

* bump

* extract slot root building into own proc

* revert to manifest to accessor

---------

Co-authored-by: benbierens <thatbenbierens@gmail.com>
2024-01-08 14:52:46 -08:00

88 lines
2.2 KiB
Nim

import std/unittest
import std/sequtils
import std/sugar
import pkg/poseidon2
import pkg/poseidon2/io
import pkg/questionable/results
import pkg/results
import pkg/stew/byteutils
import pkg/stew/arrayops
import constantine/math/arithmetic
import constantine/math/io/io_bigints
import pkg/constantine/math/io/io_fields
import pkg/constantine/platforms/abstractions
import pkg/codex/merkletree
import ./generictreetests
import ./helpers
const
data =
[
"0000000000000000000000000000001".toBytes,
"0000000000000000000000000000002".toBytes,
"0000000000000000000000000000003".toBytes,
"0000000000000000000000000000004".toBytes,
"0000000000000000000000000000005".toBytes,
"0000000000000000000000000000006".toBytes,
"0000000000000000000000000000007".toBytes,
"0000000000000000000000000000008".toBytes,
"0000000000000000000000000000009".toBytes, # note one less to account for padding of field elements
]
suite "Test Poseidon2Tree":
var
expectedLeaves: seq[Poseidon2Hash]
setup:
expectedLeaves = toSeq( data.concat().elements(Poseidon2Hash) )
test "Should fail init tree from empty leaves":
check:
Poseidon2Tree.init( leaves = newSeq[Poseidon2Hash](0) ).isErr
test "Init tree from poseidon2 leaves":
let
tree = Poseidon2Tree.init( leaves = expectedLeaves ).tryGet
check:
tree.leaves == expectedLeaves
test "Init tree from byte leaves":
let
tree = Poseidon2Tree.init(
leaves = expectedLeaves.mapIt(
array[31, byte].initCopyFrom( it.toBytes )
)).tryGet
check:
tree.leaves == expectedLeaves
test "Should build from nodes":
let
tree = Poseidon2Tree.init(leaves = expectedLeaves).tryGet
fromNodes = Poseidon2Tree.fromNodes(
nodes = toSeq(tree.nodes),
nleaves = tree.leavesCount).tryGet
check:
tree == fromNodes
let
compressor = proc(
x, y: Poseidon2Hash,
key: PoseidonKeysEnum): Poseidon2Hash {.noSideEffect.} =
compress(x, y, key.toKey)
makeTree = proc(data: seq[Poseidon2Hash]): Poseidon2Tree =
Poseidon2Tree.init(leaves = data).tryGet
testGenericTree(
"Poseidon2Tree",
toSeq( data.concat().elements(Poseidon2Hash) ),
zero,
compressor,
makeTree)