What Is Merkle Tree? Complete Guide For Beginners

A Merkle tree (hash tree) is an important part of blockchain technology. It is a structure allowing the secure verification of content. Bitcoin (BTC) and Ethereum (ETH) are the cryptocurrencies using the concept of the Merkle tree. Other applications like BitTorrent and Git use the Merkle tree. The name of Merkle tree is derived after a computer scientist named Ralph C. Merkle. The computer scientist Ralph C. Merkle patented the Merkle tree in the year 1979.
Working Of Merkle Tree
A Merkle tree produces a digital fingerprint of transactions of a block. Nodes will verify the transactions.
Hashing pairs of nodes generate Merkle trees repeatedly until only one hash is left. This hash is the Root Hash or Merkle Root. In simple words, a Merkle tree is defined as the complete hash of all the hashes of all the transactions in a single block in the existing blockchain network.
In a Merkle tree, a leaf node is defined as a hash of transactional data while a non-leaf node is defined as a hash of its previous hashes.
An even number of leaf nodes is required in Merkle trees as they are binary trees. A number of transactions can be odd. In this case, an even number of leaf nodes is created by duplicating the last hash.
There are four transactions like transaction A, transaction B, transaction C, and transaction D in a block. All these four transactions will be hashed. A hash of transaction A will be stored in a leaf node called Hash A. Other three hashes of three transactions B, C, and D will be stored in leaf nodes called Hash B, Hash C, and Hash D. The leaf nodes Hash A and Hash B will be hashed again to form a leaf node called Hash AB, while the leaf nodes Hash C and Hash D will be hashed again to form a leaf node called CD. The leaf nodes Hash AB and Hash CD will be hashed to form a Merkle root or hash root called Hash ABCD. SHA-2 cryptographic hash function is used to perform hashing.
The Merkle root contains all of the data of the four transactions which will be stored in the block header. The main purpose of the Merkle tree is to maintain the integrity of the data. The Merkle Root will be changed due to changes of a single detail in any of the transactions. A specific transaction is added or not can be recognized with the help of a quick and simple test provided by a Merkle tree.
Uses Of Merkle Tree
A trusted authority has to maintain an amount of data for verification. The amount of data can be reduced significantly using a Merkle tree. The validation of the data is separated from the data by the Merkle tree. The Merkle tree resides locally. It can also reside on a distributed system.
There should be an ability to proving that a log is complete and consistent in the concept of blockchain technology and general ledger. A Merkle tree will help in verifying everything of a log from an earlier version to later version. All data will be recorded and presented in chronological order.
How a Log Is Proved?
It has to prove that a log is consistent
No previous records have been added and altered or tampered
The log has not been branched or forked
Nodes can benefit from Merkle trees on the blockchain. Hashes can be calculated by a miner progressively when the miner receives transactions from any of the peers. A block can be verified by a user individually. Transactions can be checked using hashes of other branches of the tree.
Simplified Payment Verification (SPV)
Simplified Payment Verification (SPV) is a method to verify transactions. It is implemented in the situation where particular transactions are kept in a block with no downloading of the entire block. Nodes in the SPV use merkle trees extensively. The nodes don’t have data from all transactions in a block. Block headers have been downloaded by the nodes. The nodes can check whether miners verified the transactions in a block with no downloading of all the transactions in the block. Some lightweight Bitcoin clients use the method at the current.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store