nim-eth/eth/trie/sparse_proofs.nim

87 lines
2.6 KiB
Nim
Raw Normal View History

2019-02-05 12:01:10 +00:00
import
"."/[trie_bitseq, trie_defs, trie_utils]
2019-02-05 12:01:10 +00:00
const
treeHeight* = 160
pathByteLen* = treeHeight div 8
emptyLeafNodeHash* = blankStringHash
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):
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.
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
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
curHash.data.keccakHash(node, curHash.data)
2019-02-05 12:01:10 +00:00
else:
curHash.data.keccakHash(curHash.data, node)
2019-02-05 12:01:10 +00:00
result = curHash.data == root
2019-02-05 12:01:10 +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
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.
proc compactProof*(proof: seq[seq[byte]]): seq[seq[byte]] =
2019-02-05 12:01:10 +00:00
if proof.len != treeHeight: return
var
data = newSeq[byte](pathByteLen)
bits = bits data
2019-02-05 12:01:10 +00:00
result = @[]
result.add @[]
2019-02-05 12:01:10 +00:00
for i in 0 ..< treeHeight:
var node = proof[i]
if node == emptyNodeHashes[i].data:
2019-02-05 12:01:10 +00:00
bits[i] = true
else:
result.add node
result[0] = bits.toBytes
2019-02-05 12:01:10 +00:00
# decompactProof decompacts a proof, so that it can be used for VerifyProof.
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
let bits = bits proof[0]
2019-02-05 12:01:10 +00:00
if proof.len != bits.count(false) + 1: return
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]:
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.
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)
template verifyCompactProof*(proof: seq[seq[byte]], root, key, value: openArray[byte]): bool =
verifyCompactProofAux(proof, root, key, value)