2023-08-15 11:23:35 +00:00
|
|
|
## Nim-Codex
|
2023-11-14 12:02:17 +00:00
|
|
|
## Copyright (c) 2023 Status Research & Development GmbH
|
2023-08-15 11:23:35 +00:00
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2023-08-15 11:23:35 +00:00
|
|
|
import std/bitops
|
|
|
|
|
2024-10-28 14:35:40 +00:00
|
|
|
import pkg/chronos
|
2023-08-15 11:23:35 +00:00
|
|
|
import pkg/questionable/results
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
import ../errors
|
|
|
|
|
2023-08-15 11:23:35 +00:00
|
|
|
type
|
2023-12-21 06:41:43 +00:00
|
|
|
CompressFn*[H, K] = proc (x, y: H, key: K): ?!H {.noSideEffect, raises: [].}
|
|
|
|
|
|
|
|
MerkleTree*[H, K] = ref object of RootObj
|
|
|
|
layers* : seq[seq[H]]
|
|
|
|
compress*: CompressFn[H, K]
|
|
|
|
zero* : H
|
|
|
|
|
|
|
|
MerkleProof*[H, K] = ref object of RootObj
|
|
|
|
index* : int # linear index of the leaf, starting from 0
|
|
|
|
path* : seq[H] # order: from the bottom to the top
|
|
|
|
nleaves* : int # number of leaves in the tree (=size of input)
|
|
|
|
compress*: CompressFn[H, K] # compress function
|
|
|
|
zero* : H # zero value
|
|
|
|
|
|
|
|
func depth*[H, K](self: MerkleTree[H, K]): int =
|
|
|
|
return self.layers.len - 1
|
|
|
|
|
|
|
|
func leavesCount*[H, K](self: MerkleTree[H, K]): int =
|
|
|
|
return self.layers[0].len
|
|
|
|
|
|
|
|
func levels*[H, K](self: MerkleTree[H, K]): int =
|
|
|
|
return self.layers.len
|
|
|
|
|
|
|
|
func leaves*[H, K](self: MerkleTree[H, K]): seq[H] =
|
|
|
|
return self.layers[0]
|
|
|
|
|
|
|
|
iterator layers*[H, K](self: MerkleTree[H, K]): seq[H] =
|
|
|
|
for layer in self.layers:
|
|
|
|
yield layer
|
|
|
|
|
|
|
|
iterator nodes*[H, K](self: MerkleTree[H, K]): H =
|
|
|
|
for layer in self.layers:
|
|
|
|
for node in layer:
|
|
|
|
yield node
|
|
|
|
|
|
|
|
func root*[H, K](self: MerkleTree[H, K]): ?!H =
|
|
|
|
let last = self.layers[^1]
|
|
|
|
if last.len != 1:
|
|
|
|
return failure "invalid tree"
|
|
|
|
|
|
|
|
return success last[0]
|
|
|
|
|
|
|
|
func getProof*[H, K](
|
|
|
|
self: MerkleTree[H, K],
|
|
|
|
index: int,
|
|
|
|
proof: MerkleProof[H, K]): ?!void =
|
|
|
|
let depth = self.depth
|
|
|
|
let nleaves = self.leavesCount
|
|
|
|
|
|
|
|
if not (index >= 0 and index < nleaves):
|
|
|
|
return failure "index out of bounds"
|
|
|
|
|
|
|
|
var path : seq[H] = newSeq[H](depth)
|
|
|
|
var k = index
|
|
|
|
var m = nleaves
|
|
|
|
for i in 0..<depth:
|
|
|
|
let j = k xor 1
|
|
|
|
path[i] = if (j < m): self.layers[i][j] else: self.zero
|
|
|
|
k = k shr 1
|
|
|
|
m = (m + 1) shr 1
|
|
|
|
|
|
|
|
proof.index = index
|
|
|
|
proof.path = path
|
|
|
|
proof.nleaves = nleaves
|
|
|
|
proof.compress = self.compress
|
2023-11-14 12:02:17 +00:00
|
|
|
|
|
|
|
success()
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
func getProof*[H, K](self: MerkleTree[H, K], index: int): ?!MerkleProof[H, K] =
|
2023-11-14 17:52:27 +00:00
|
|
|
var
|
2023-12-21 06:41:43 +00:00
|
|
|
proof = MerkleProof[H, K]()
|
2023-08-15 11:23:35 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
? self.getProof(index, proof)
|
2023-08-15 11:23:35 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
success proof
|
2023-08-15 11:23:35 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
func reconstructRoot*[H, K](proof: MerkleProof[H, K], leaf: H): ?!H =
|
|
|
|
var
|
|
|
|
m = proof.nleaves
|
|
|
|
j = proof.index
|
|
|
|
h = leaf
|
|
|
|
bottomFlag = K.KeyBottomLayer
|
|
|
|
|
|
|
|
for p in proof.path:
|
|
|
|
let oddIndex : bool = (bitand(j,1) != 0)
|
|
|
|
if oddIndex:
|
|
|
|
# the index of the child is odd, so the node itself can't be odd (a bit counterintuitive, yeah :)
|
|
|
|
h = ? proof.compress( p, h, bottomFlag )
|
2023-11-14 12:02:17 +00:00
|
|
|
else:
|
2023-12-21 06:41:43 +00:00
|
|
|
if j == m - 1:
|
|
|
|
# single child => odd node
|
|
|
|
h = ? proof.compress( h, p, K(bottomFlag.ord + 2) )
|
|
|
|
else:
|
|
|
|
# even node
|
|
|
|
h = ? proof.compress( h , p, bottomFlag )
|
|
|
|
bottomFlag = K.KeyNone
|
|
|
|
j = j shr 1
|
|
|
|
m = (m+1) shr 1
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
return success h
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
func verify*[H, K](proof: MerkleProof[H, K], leaf: H, root: H): ?!bool =
|
|
|
|
success bool(root == ? proof.reconstructRoot(leaf))
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2024-10-28 14:35:40 +00:00
|
|
|
proc merkleTreeWorker*[H, K](
|
2023-12-21 06:41:43 +00:00
|
|
|
self: MerkleTree[H, K],
|
2024-10-28 14:35:40 +00:00
|
|
|
xs: seq[H],
|
|
|
|
isBottomLayer: static bool): Future[?!seq[seq[H]]] {.async.} =
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
let a = low(xs)
|
|
|
|
let b = high(xs)
|
|
|
|
let m = b - a + 1
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
when not isBottomLayer:
|
|
|
|
if m == 1:
|
|
|
|
return success @[ @xs ]
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
let halfn: int = m div 2
|
|
|
|
let n : int = 2 * halfn
|
|
|
|
let isOdd: bool = (n != m)
|
2023-11-14 12:02:17 +00:00
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
var ys: seq[H]
|
|
|
|
if not isOdd:
|
|
|
|
ys = newSeq[H](halfn)
|
2023-11-14 12:02:17 +00:00
|
|
|
else:
|
2023-12-21 06:41:43 +00:00
|
|
|
ys = newSeq[H](halfn + 1)
|
|
|
|
|
|
|
|
for i in 0..<halfn:
|
|
|
|
const key = when isBottomLayer: K.KeyBottomLayer else: K.KeyNone
|
2024-10-28 14:35:40 +00:00
|
|
|
without y =? self.compress( xs[a + 2 * i], xs[a + 2 * i + 1], key = key ), err:
|
|
|
|
return failure err
|
|
|
|
ys[i] = y
|
2024-10-29 09:00:33 +00:00
|
|
|
await sleepAsync(1.micros) # cooperative scheduling
|
2023-12-21 06:41:43 +00:00
|
|
|
if isOdd:
|
|
|
|
const key = when isBottomLayer: K.KeyOddAndBottomLayer else: K.KeyOdd
|
2024-10-28 14:35:40 +00:00
|
|
|
without y =? self.compress( xs[n], self.zero, key = key ), err:
|
|
|
|
return failure err
|
|
|
|
ys[halfn] = y
|
2023-12-21 06:41:43 +00:00
|
|
|
|
2024-10-28 14:35:40 +00:00
|
|
|
without v =? (await self.merkleTreeWorker(ys, isBottomLayer = false)), err:
|
|
|
|
return failure err
|
|
|
|
success @[ @xs ] & v
|