build-a-merkle-tree / lesson-11.md
Lesson 11 · Detecting tampering

Verifying data against a root

If you trust a root you can check a whole dataset against it without keeping the old copy - just rebuild and compare. Today you build that check.

The goal

Verify a dataset against a known-good root by rebuilding and comparing.

Start here - the target
TO DO
Scenario: Verify accepts matching data and rejects corrupted data
Giventhe trusted root 0xfd610c23 for ["alice", "bob", "carol", "dave"]
WhenVerify is called with that same data and that root
Thenit returns true
AndVerify with the corrupted data ["alice", "bob", "trent", "dave"] against root 0xfd610c23 returns false
Background

Detecting tampering does not require keeping the original data - just its root. A verifier who was handed a trusted root once can, at any later time, rebuild the tree from whatever data they now hold and compare. Match means intact; mismatch means something changed. This is exactly how a downloaded dataset is checked against a published root, or how a system confirms its own storage has not been corrupted.

Verify is a one-liner precisely because all the work already lives in Build. The value is in the pattern: a tiny trusted anchor (32 bits here, 256 in a real system) guards an arbitrarily large dataset. The next chapter makes this even cheaper - you will confirm a single item belongs without rebuilding, or even seeing, the rest.

Make it work
func Verify(data [][]byte, root Hash) bool {
return Build(data).Root() == root
}
CheckpointDONE
A dataset can be checked against a trusted root, accepting good data and rejecting corrupted data. Commit and stop here.