If you’ve been around cryptocurrencies for a while then you’ve probably heard the term Merkle Root mentioned in technical discussions. Today we’ll take a look at this interesting construct which is used to guarantee the integrity of cryptocurrency blocks.
Before discussing Merkle Roots, let’s take a quick look at Merkle Trees. (Trees have roots that hold them in place: that’s a hint of where we’re going next.)
A Merkle Tree is illustrated at the top of this article.
Suppose your boss walks up to you and says these are your requirements: you’ve got data that cannot change and if any of the data changes, the change should be immediately noticed by everyone.
To achieve this, you can build a binary tree out of the data somehow (order it into pairs and build a tree with lexicographic order, for instance) and then compute hashes for all leaves. Then hash pairs of nodes in the tree and attribute the pair hash to the parent node.
This is an immutable structure because the slightest change on any of the leaves would immediately propagate upwards towards the….
A tree data structure has necessarily one, and only one, root.
So if you hash your way up from the leaves to the root, you’ll end up with one root cryptographic hash which represents the integrity of all the data contained in the tree!
How is this used in Bitcoin Core, you might ask?
Each transaction in a Bitcoin blockchain block has a hash. In fact Bitcoin TX’s are publicly identified by their hash.
We know that the Tx hash is universally unique.
If we organize a Bitcoin block into a tree of pairs of Tx’s, and proceed to hash their hashes together, we’ll end up with a root hash that is absolutely unique. If a block were to be modified, or even moved within the tree, the root hash would reflect the change as well.
Bitcoin Core and other cryptocurrency implementations use this mechanism to verify blocks.
An individual block can be uniquely identified by its Merkle Root hash because the hash of transaction hashes is guaranteed to be unique.
Why, then, are block hashes different from the Merkle root?
Simple, because the blockchain requires not only for each block be uniquely identified, but also that each block have a cryptographic link to the block before.
Therefore, the block hash is the Merkle Root hashed together with the previous block’s hash.
This is necessary to guarantee that any modification made to previous blocks propagates forward into newer blocks, modifying every forward block hash.
If the merkle tree were used as the block hash, then you would be able to modify previous blocks without it propagating forward.
Merkle roots identify a single block. Bblock hashes include previous block hashes as well.
Merkle trees are widely used in cryptographic software to generate unique identifiers for data sets.
A Merkle Root is the resulting hashing computation of a Merkle Tree.
Merkle Roots are impossible to tamper with, and uniquely identify individual Bitcoin blocks.
Hash blocks are different from Merkle roots on the blockchain because they must include not only the unique block signature but also the previous block’s hash.
Bitcoin consensus and block validation depend fundamelly on Merkle Tree computation.