Merkle tree is the essential component of a blockchain. Data entered into the blockchain is immutable, and this is a critical future of blockchain. Even though there are many futures, many deploy blockchain for this one significant future. This future is primarily achieved using the concept of a Merkle tree. Before dwelling into further about the idea, it is essential to understand cryptographic hash functions.
What are cryptographic hash functions?
Cryptographic hash functions are another integral part of blockchain technology. Cryptography is often used for military purposes. In war zones, the data is shared between two parties of a country at different places using cryptography.
Cryptographic hash functions are algorithms that transform any input given to the algorithm in the output of fixed length. The outputs change drastically, even if a single letter of the input is changed. At the same time, the same input gives the same output all the time. It is highly unlikely to determine the output based on the input unless one has a set of public/private keys. Any length of the input gives a fixed-length output; this feature is handy when a large amount of data sets is used. To check any set of data is modified or not, we can check the fixed-length hash.
Let us see the usage of cryptographic hash functions in the bitcoin blockchain network. Blockchain is essentially a series of blocks of transactions joined together using cryptographic hash functions. Each block has header data and transactions associated with it. Header data contains the previous hash, nonce, Merkle root, block hash.
Data of the complete block, including the header data, is hashed, and this hash is stored in the present block and also in the next block as the previous block hash. This previous block hash represents the entire state of the blockchain at any given point of time. Hence if we make any changes to the transactions in the last block, the hash of all the blocks up to the present block will be disturbed, which is why it is highly impossible to change the transactions and hence the concept of immutability.
Now how do we verify the hashes to check the data integrity? It is highly inefficient and time consuming to check the hash of every block. Hence the concept of Merkle tree is used as it is efficient to check the data integrity.
What is a Merkle tree, and how is it used?
Merkle tree developed by Ralph Merkle is also called a Binary hash tree. It is a data structure used to store hashes of individual data in an extensive data set in a way to make the verification of the date set efficient.
An example of the Merkle tree is as below.
It would be easy to understand the Merkle tree with the example above. It is essentially a tree of hashes with branches of individual hashes. These hashes come from the transactions of the blockchain platform when it comes to a cryptocurrency platform.
In the above figure, we have transactions from TA represents a transaction, while HA represents a hash of that transaction. All the transactions are hashed to produce a hash value of its own transaction. Then adjacent transactions are hashed together to form a hash of both transactions. Like HAB is the hash of transactions A and B. If there are an odd number of transactions, then the transaction is combined by its own, and a hash value is created. The same process is repeated until the last hash value is generated, which is called the Merkle root. In this case, HABCDEFGH is the Merkle root of transactions from TA to TH. This is how a Merkle tree is formed.
Hence because of the tree, it would easy to find if any transactions are tampered with, uses very few resources to check any fraudulent behavior, and easy to add new transactions to the block.
This allows for simple payment verification, and the new nodes need not download the entire blockchain but only the block headers of the longest chain. Thus Merkle trees help to maintain the immutability and integrity of the blockchain.