Merkle trees
Goal: reconstruct a root from one leaf and its siblings.
Keep three questions separate:
- How is this tree constructed?
- Does this path reproduce the expected root?
- Why do we trust that root?
1 / 10
Goal: reconstruct a root from one leaf and its siblings.
Keep three questions separate:
H(x) = SHA256(SHA256(x))
Hex strings and raw hashes are different inputs.
a b c d e
p=H(a||b) q=H(c||d) r=H(e||e)
u=H(p||q) v=H(r||r)
root=H(u||v)
Predict: how many steps does the proof for e contain?
Three. One sibling at each reduction level.
| Index | Sibling | Sibling on left? |
|---|---|---|
| 4 | e | no |
| 2 | r | no |
| 1 | u | yes |
After duplication: sibling=index ^ 1; parent=index/2.
Direction describes the sibling, not the current node.
Start h=H(leaf).
Left=true: h=H(sibling||h)
Left=false: h=H(h||sibling)
For e: H(e||e)=r, H(r||r)=v, H(u||v)=root.
Invariant: h is the reconstructed subtree root at this height.
One leaf: compare H(leaf) with root.
MerkleProof also returns nil for an invalid index.
Therefore an empty proof alone is not an error signal.
The verifier receives no claimed index or leaf count.
[A,B,C] → H(H(a||b)||H(c||c))
[A,B,C,C] → H(H(a||b)||H(c||c))
Same root, different lists. No hash collision required.
Deterministic odd duplication does not uniquely commit to list length.
A hashing path agrees with the supplied root.
It does not establish:
Those need evidence outside this proof API.
Test handcrafted roots and paths, direction flips, singleton proofs, and odd duplication at multiple levels. Round trips alone can share a bug.
For five leaves, give b's sibling sequence and flags.
Answer: a,q,v; true,false,false.
Explain why a proof against a server-chosen root cannot establish chain membership.