2019-02-05 12:01:10 +00:00
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
"."/[trie_bitseq, trie_defs, trie_utils]
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
treeHeight* = 160
|
|
|
|
pathByteLen* = treeHeight div 8
|
|
|
|
emptyLeafNodeHash* = blankStringHash
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc makeInitialEmptyTreeHash(H: static[int]): array[H, KeccakHash] =
|
|
|
|
result[^1] = emptyLeafNodeHash
|
2019-02-05 12:01:10 +00:00
|
|
|
for i in countdown(H-1, 1):
|
2020-04-20 18:14:39 +00:00
|
|
|
result[i - 1] = keccakHash(result[i].data, result[i].data)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
# cannot yet turn this into compile time constant
|
|
|
|
let emptyNodeHashes* = makeInitialEmptyTreeHash(treeHeight)
|
|
|
|
|
|
|
|
# VerifyProof verifies a Merkle proof.
|
2020-04-20 18:14:39 +00:00
|
|
|
proc verifyProofAux*(proof: seq[seq[byte]], root, key, value: openArray[byte]): bool =
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(root.len == 32)
|
|
|
|
doAssert(key.len == pathByteLen)
|
2019-02-05 12:01:10 +00:00
|
|
|
var
|
2020-04-20 18:14:39 +00:00
|
|
|
path = bits key
|
2019-02-05 12:01:10 +00:00
|
|
|
curHash = keccakHash(value)
|
|
|
|
|
|
|
|
if proof.len != treeHeight: return false
|
|
|
|
|
|
|
|
for i in countdown(treeHeight - 1, 0):
|
|
|
|
var node = proof[i]
|
|
|
|
if node.len != 32: return false
|
|
|
|
if path[i]: # right
|
|
|
|
# reuse curHash without more alloc
|
2020-04-20 18:14:39 +00:00
|
|
|
curHash.data.keccakHash(node, curHash.data)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2020-04-20 18:14:39 +00:00
|
|
|
curHash.data.keccakHash(curHash.data, node)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
result = curHash.data == root
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
template verifyProof*(proof: seq[seq[byte]], root, key, value: openArray[byte]): bool =
|
|
|
|
verifyProofAux(proof, root, key, value)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc count(b: TrieBitSeq, val: bool): int =
|
2019-02-05 12:01:10 +00:00
|
|
|
for c in b:
|
|
|
|
if c == val: inc result
|
|
|
|
|
|
|
|
# CompactProof compacts a proof, to reduce its size.
|
2020-04-20 18:14:39 +00:00
|
|
|
proc compactProof*(proof: seq[seq[byte]]): seq[seq[byte]] =
|
2019-02-05 12:01:10 +00:00
|
|
|
if proof.len != treeHeight: return
|
|
|
|
|
|
|
|
var
|
2020-04-20 18:14:39 +00:00
|
|
|
data = newSeq[byte](pathByteLen)
|
|
|
|
bits = bits data
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
result = @[]
|
2020-04-20 18:14:39 +00:00
|
|
|
result.add @[]
|
2019-02-05 12:01:10 +00:00
|
|
|
for i in 0 ..< treeHeight:
|
|
|
|
var node = proof[i]
|
2020-04-20 18:14:39 +00:00
|
|
|
if node == emptyNodeHashes[i].data:
|
2019-02-05 12:01:10 +00:00
|
|
|
bits[i] = true
|
|
|
|
else:
|
|
|
|
result.add node
|
2020-04-20 18:14:39 +00:00
|
|
|
result[0] = bits.toBytes
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
# decompactProof decompacts a proof, so that it can be used for VerifyProof.
|
2020-04-20 18:14:39 +00:00
|
|
|
proc decompactProof*(proof: seq[seq[byte]]): seq[seq[byte]] =
|
2019-02-05 12:01:10 +00:00
|
|
|
if proof.len == 0: return
|
|
|
|
if proof[0].len != pathByteLen: return
|
2020-04-20 18:14:39 +00:00
|
|
|
let bits = bits proof[0]
|
2019-02-05 12:01:10 +00:00
|
|
|
if proof.len != bits.count(false) + 1: return
|
2020-04-20 18:14:39 +00:00
|
|
|
result = newSeq[seq[byte]](treeHeight)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
var pos = 1 # skip bits
|
|
|
|
for i in 0 ..< treeHeight:
|
|
|
|
if bits[i]:
|
2020-04-20 18:14:39 +00:00
|
|
|
result[i] = @(emptyNodeHashes[i].data)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
result[i] = proof[pos]
|
|
|
|
inc pos
|
|
|
|
|
|
|
|
# verifyCompactProof verifies a compacted Merkle proof.
|
2020-04-20 18:14:39 +00:00
|
|
|
proc verifyCompactProofAux*(proof: seq[seq[byte]], root, key, value: openArray[byte]): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
var decompactedProof = decompactProof(proof)
|
|
|
|
if decompactedProof.len == 0: return false
|
|
|
|
verifyProofAux(decompactedProof, root, key, value)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
template verifyCompactProof*(proof: seq[seq[byte]], root, key, value: openArray[byte]): bool =
|
|
|
|
verifyCompactProofAux(proof, root, key, value)
|