Happy Weekend, Dear Reader!

I hope that this email finds you well. We are meeting after a month. I apologise, I was struggling in my personal life. Back to back jolts! Anyways, life appears to calm down now and I can hopefully get back to writing for you regularly.

In this article, I will be exploring Merkle Trees.

## Introduction

**Merkle trees** have found the most popular use-case in Bitcoin blockchain. They are tree-like data structures where each node is a **cryptographic hash** of its child nodes. The Merkle root, located at the top of the tree, represents the entire data set.

A **Merkle tree** is a **collision-resistant hash** function, denoted by **𝖬𝖧𝖳**, which takes n inputs (x1,x2,……,xn) and it outputs a Merkle root hash h=𝖬𝖧𝖳(x1,x2,……,xn).

**Merkle Tree**s are used to store and verify transactions. Transactions are first hashed individually and then grouped together in pairs, which are then hashed again to create new nodes. This process is repeated until there is only one node left, the Merkle root.

A **Verifier** has the root node hash h and it can be given an xi with an associated Merkle proof that proves that xi was the ith input used to compute h.

## Creating a Merkle Tree

Let us start by taking an example of an input array A = (x1, x2, x3, x4, x5, x6, x7, x8). It has 8 elements. If we have to create a Merkle Tree from it, following will be the process.

Take pairs of input and pass it through a cryptographic hash functions which is collision resistant.

Take the output of that and go recursive until you reach one value.

That value becomes the root node.

A hash function H is collision resistant if it is computationally intractable(

It is algorithmically impossible to find a solution for breaking collision resistance in this lifetime!) for an adversary to find two different inputs x and x′ with the same hash (i.e., with H(x)=H(x′)).

## Merkle Tree Verification

Mathematically a Merkle Hash Tree can be represented as follows -

`Merkle root = MHT(x1, x2, … , x8) `

`= h(`

`h( `

` h( h(x1), h(x2) ), h( h(x3), h(x4) ) ), `

`h( `

` h( h(x5), h(x6) ), h( h(x7), h(x8) ) `

`) `

`)`

This again shows us the recursiveness of Merkle Trees.

Now, Let us say we are given an input x* and we are asked if it belongs to the array which was used to create the Merkle tree.

We can use part of a Merkle Hash Tree and fill in the blanks in the diagram below to get a Merkle Root Node. Now, if the Merkle Root node matches the original Merkle Root node then we can say with absolute certainty that x* belonged in the Array at 5th spot.

## Merkle Tree Advantages

Using Merkle Trees has several advantages for a blockchain. Firstly, it allows for efficient transaction verification. Instead of verifying each transaction individually, a node can verify the Merkle root, which represents the entire dataset.

Secondly, it allows for compact storage of transactions. The Merkle tree enables only the Merkle root to be stored instead of storing all transactions in one block, which is much smaller.

Finally, Merkle Tree allows for quick synchronization of nodes. When a new node joins the network, it requests only the Merkle root instead of the entire dataset, allowing for faster and more efficient sync.

## Merkle Tree Drawbacks

While Merkle Trees provide several benefits to blockchain technology, there are some potential issues to consider.

One issue is the possibility of a collision attack, where two different sets of data result in the same Merkle root. This can potentially allow for malicious actors to tamper with the data without being detected.

Another issue is the potential for a 51% attack, where a single entity or group controls the majority of the network's computing power. In this scenario, the attacker could potentially modify transactions and their corresponding hashes in the Merkle Tree, which could then be accepted by the network as valid.

It's important to note that while these issues exist, they do not necessarily render Merkle Trees ineffective. Proper implementation and security measures can help mitigate these risks and ensure the integrity of the blockchain.

## Conclusion

Apart from blockchains, Merkle Trees can also be used to **detect** malicious or accidental modifications of any of your files if downloading from malicious channels.

While Merkle Trees are a widely used and effective method for storing and verifying data in blockchain technology, there are some alternative approaches as well.

One such approach is the use of Sparse Merkle Trees, which are similar to regular Merkle Trees but only store the non-empty leaves of the tree. This can reduce the storage requirements and improve efficiency in some cases.

A Merkle Tree with n leaves has O(log2 n)-sized proofs. In large trees, sending the proofs can dominate bandwidth consumption. Vector Commitments (VCs) pose a potential alternative to Merkle Trees, with constant-sized proofs.

Ultimately, the choice of data structure for a blockchain system will depend on the specific needs and requirements of the network. Merkle Trees remain a popular and effective choice for many blockchain applications, but other options may be more suitable in certain cases.

Please feel free to reach out to me if you need help with building large scale distributed systems, deep learning or data engineering solutions at your organisation, I would be more than happy to help.

Read more of my work -

Also check out my Youtube Channel -

I hope that you enjoyed this read, if so then please share this article with your friends, let’s build a solid community. I will be back soon with another well thought/researched article delivered straight in your inbox. See you!