The finale runs the whole library at once - build a tree, prove a leaf belongs, tamper with the data, show the tamper is caught, and diff the two versions to name the changed leaf.
Run a full build, prove, verify, tamper, and diff workflow and assert every result.
This is the promise the whole project was built to keep: a real Merkle tree
library. The script exercises every layer at once. Build condenses four items into
the root 0xfd610c23. Prove and VerifyProof show that bob can be proven a member
from just two sibling hashes, without the rest of the data. Then a tamper - carol
becomes trent - flips the root to 0xa10cca2a, and the old inclusion proof for
carol, which verified a moment ago, now fails against the new root. Finally Diff
pinpoints that leaf 2, and only leaf 2, changed.
From a single deterministic hash you built content-addressed leaves, a tree that
fingerprints a whole dataset, audit proofs that demonstrate membership in log n
hashes, a consistency check for append-only logs, and an efficient diff - the same
machinery inside Git, Certificate Transparency, Bitcoin, and peer-to-peer sync, minus
the real cryptographic hash they swap in for FNV-1a. The structure is hash-agnostic, so
that swap is the only change between this and the production article. That is a real
Merkle tree, and it is yours.
v1 := Build(data("alice", "bob", "carol", "dave"))pb := v1.Prove(1) // proof for "bob"VerifyProof(v1.Root(), []byte("bob"), pb) // truev2 := Build(data("alice", "bob", "trent", "dave"))v2.Root() // 0xa10cca2a != v1.Root()VerifyProof(v2.Root(), []byte("carol"), v1.Prove(2)) // false: tamper caughtDiff(v1, v2) // [2]