Merkle Trees — One Hash for Many Transactions
Meera runs a light client on her phone. She wants to be sure that Asha's payment really is inside block 812, but that block holds 3,000 transactions and she does not want to download them all. A Merkle tree lets a full node send her a proof of just twelve hashes, and lets her check that proof herself.
The rule
Hash in pairs, level by level, until one root remains.
Here is the tree for four transactions, A, B, C and D. H means "hash of", and + means the two hashes are joined end to end before hashing.
Root = H(H_AB + H_CD)
/ \
H_AB = H(H_A + H_B) H_CD = H(H_C + H_D)
/ \ / \
H_A H_B H_C H_D
| | | |
A B C D- Hash each transaction to get the leaves: H_A, H_B, H_C and H_D.
- Join neighbouring pairs and hash each pair: H_AB and H_CD.
- Repeat until a single hash is left. That is the Merkle root, and it goes in the block header.
When a level has an odd number of hashes, Bitcoin pairs the last one with a copy of itself; other chains use their own rule. Change any transaction and its leaf changes, then its parent, and so on up to the root. This is why the block hash, computed from the header, commits to every single transaction.