Merkle Tree

An abstract data structure that organizes a list of data items in a tree of their hashes (such as Git, Mercurial or ZFS). In Bitcoin, the merkle tree is used only to organize transactions within a block (the block header contains only one hash of a tree) so that full nodes may prune fully spent transactions to save disk space. SPV clients store only block headers and validate transactions if they are provided with a list of all intermediate hashes.

Another useful data structure that we can build using hash pointers is a binary tree. A binary tree with hash pointers is known as a Merkle Tree, after its inventor Ralph Merkle. In a Merkle tree, data blocks are grouped into pairs, and the hash of each of these blocks is stored in a parent node. The parent nodes are in turn grouped into pairs, and their hashes stored one level up the tree. This pattern continues up the tree until we reach the root node.

Suppose we have some blocks containing data. These blocks make up the leaves of our tree. We group these data blocks into pairs of two, and then for each pair we build a data structure that has two hash pointers, one for each of the blocks. These data structures make up the next level of the tree. We in turn group these into groups of two, and for each pair create a new data structure that contains the hash of each. We continue doing this until we reach a single block, the root of the tree.

As before, we remember just one hash pointer: in this case, the one at the root of the tree. We now have the ability to traverse through the hash pointers to any point in the list. This allows us to make sure that the data has not been tampered with because, just as we saw for the block chain, if an adversary tampers with some data block at the bottom of the tree, his change will cause the hash pointer one level up to not match, and even if he continues to tamper with the other blocks farther up the three, the change will eventually propagate to the top, where he won't be able to tamper with the hash pointer that we've stored. So again, any attempt to tamper with any piece of data will be detected by just remembering the hash pointer at the top.