nim-dagger/tests/codex/merkletree/generictreetests.nim

131 lines
3.2 KiB
Nim
Raw Permalink Normal View History

Rework merkle tree (#654) * rework merkle tree support * deps * rename merkletree -> codexmerkletree * treed and proof encoding/decoding * small change to invoke proof verification * rename merkletree to codexmerkletree * style * adding codex merkle and coders tests * fixup imports * remove new codecs for now * bump deps * adding trace statement * properly serde of manifest block codecs * use default hash codec * add more trace logging to aid debugging * misc * remove double import * revert un-needded change * proof size changed * bump poseidon2 * add from nodes test * shorte file names * remove upraises * wip poseidon tree * adjust file names * misc * shorten file names * fix bad `elements` iter * don't do asserts * add fromNodes and converters * root and getProof now return result * add poseidon2 tree tests * root now returns result * misc * had to make merkletree a ref, because nim blows up otherwise * root returns a result * root returns a result * import poseidon tests * bump * merkle poseidon2 digest * misc * add merkle digest tests * bump * don't use checksuite * Update tests/codex/merkletree/generictreetests.nim Co-authored-by: markspanbroek <mark@spanbroek.net> Signed-off-by: Dmitriy Ryajov <dryajov@gmail.com> * Update codex/merkletree/merkletree.nim Co-authored-by: markspanbroek <mark@spanbroek.net> Signed-off-by: Dmitriy Ryajov <dryajov@gmail.com> * Update codex/merkletree/merkletree.nim Co-authored-by: markspanbroek <mark@spanbroek.net> Signed-off-by: Dmitriy Ryajov <dryajov@gmail.com> * Update tests/codex/merkletree/generictreetests.nim Co-authored-by: markspanbroek <mark@spanbroek.net> Signed-off-by: Dmitriy Ryajov <dryajov@gmail.com> * missing return * make toBool private (it's still needed otherwise comparison won't work) * added `digestTree` that returns a tree and `digest` for root * test against both poseidon trees - codex and poseidon2 * shorten merkle tree names * don't compare trees - it's going to be too slow * move comparison to mekrle helper * remove merkle utils --------- Signed-off-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: markspanbroek <mark@spanbroek.net>
2023-12-21 06:41:43 +00:00
import std/unittest
import std/sequtils
import pkg/codex/merkletree
proc testGenericTree*[H, K, U](
name: string,
data: openArray[H],
zero: H,
compress: proc(z, y: H, key: K): H,
makeTree: proc(data: seq[H]): U) =
let
data = @data
suite "Correctness tests - " & name:
test "Should build correct tree for even bottom layer":
let
expectedRoot = compress(
compress(
compress(data[0], data[1], K.KeyBottomLayer),
compress(data[2], data[3], K.KeyBottomLayer),
K.KeyNone
),
compress(
compress(data[4], data[5], K.KeyBottomLayer),
compress(data[6], data[7], K.KeyBottomLayer),
K.KeyNone
),
K.KeyNone
)
let
tree = makeTree( data[0..7] )
check:
tree.root.tryGet == expectedRoot
test "Should build correct tree for odd bottom layer":
let
expectedRoot = compress(
compress(
compress(data[0], data[1], K.KeyBottomLayer),
compress(data[2], data[3], K.KeyBottomLayer),
K.KeyNone
),
compress(
compress(data[4], data[5], K.KeyBottomLayer),
compress(data[6], zero, K.KeyOddAndBottomLayer),
K.KeyNone
),
K.KeyNone
)
let
tree = makeTree( data[0..6] )
check:
tree.root.tryGet == expectedRoot
test "Should build correct tree for even bottom and odd upper layers":
let
expectedRoot = compress(
compress(
compress(
compress(data[0], data[1], K.KeyBottomLayer),
compress(data[2], data[3], K.KeyBottomLayer),
K.KeyNone
),
compress(
compress(data[4], data[5], K.KeyBottomLayer),
compress(data[6], data[7], K.KeyBottomLayer),
K.KeyNone
),
K.KeyNone
),
compress(
compress(
compress(data[8], data[9], K.KeyBottomLayer),
zero,
K.KeyOdd
),
zero,
K.KeyOdd
),
K.KeyNone
)
let
tree = makeTree( data[0..9] )
check:
tree.root.tryGet == expectedRoot
test "Should get and validate correct proofs":
let
expectedRoot = compress(
compress(
compress(
compress(data[0], data[1], K.KeyBottomLayer),
compress(data[2], data[3], K.KeyBottomLayer),
K.KeyNone
),
compress(
compress(data[4], data[5], K.KeyBottomLayer),
compress(data[6], data[7], K.KeyBottomLayer),
K.KeyNone
),
K.KeyNone
),
compress(
compress(
compress(data[8], data[9], K.KeyBottomLayer),
zero,
K.KeyOdd
),
zero,
K.KeyOdd
),
K.KeyNone
)
let
tree = makeTree( data )
for i in 0..<data.len:
let proof = tree.getProof(i).tryGet
check:
proof.verify(tree.leaves[i], expectedRoot).isOk