0% found this document useful (0 votes)
66 views33 pages

Merkle Tree in Blockchain

Uploaded by

anand.dhawale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views33 pages

Merkle Tree in Blockchain

Uploaded by

anand.dhawale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

• blockchain is formed by “blocks” connected via hash pointers and

each block consists of transactions that have been validated and


approved by a majority of the miners.
• The popularity of Merkle trees has grown over the years and these
are used in cryptocurrencies such as Bitcoin and Ethereum.
Hash code to verify integrity
• hashes can be used to verify integrity.
• Example::Consider there is a text file with important data. Pass the
contents of the text file into a hash function and then store the hash
in the phone.
• A hacker manages to open the text file and changes the data. Now
when you open the file again, you can compute the hash again and
compare this hash with the one stored previously on the phone.
• It will be clearly evident that the two hashes do not match and
hence the file has been tampered with.
Hash Pointer

• A regular pointer stores the memory address of data. With this


pointer, the data can be accessed easily.
• A hash pointer can be used to build all kinds of data structures such
as blockchain and Merkle tree.
• So a hash pointer points to the data and also allows us to verify the
data.
• A hash pointer can be used to build all kinds of data structures such
as blockchain and Merkle tree.
Blockchain Structure

• The blockchain is a proficient combination of two hash-based data


structures-
1.Linked list- This is the structure of the blockchain itself, which is
a linked list of hash pointers.
A regular linked list consists of nodes. Each node has 2 parts- data and
pointer. The pointer points to the next node.
In the blockchain, simply replace the regular pointer with a hash
pointer.
2. Merkle tree- A Merkle tree is a binary tree formed by hash pointers,
and named after its creator, Ralph Merkle.
Blockchain as linked-list with hash pointers
Merkle Tree in Blockchain
• Merkle tree, is a tree in which each leaf node is labeled with the
cryptographic hash of a data block, and each non-leaf node is
labeled with the cryptographic hash of its child nodes' labels.

• The majority of hash tree implementations are binary (each node has
two child nodes), but they can also have many more child nodes.
Need of Merkel Tree
• Merkle trees, also known as Binary hash trees, are a prevalent sort of
data structure in computer science.
• In bitcoin and other cryptocurrencies, they're used to encrypt
blockchain data more efficiently and securely.
• It enables quick and secure content verification across big datasets
and verifies the consistency and content of the data.
• As mentioned earlier, each block is supposed to hold a certain number of transactions.
• Now the question arises, how to store these transactions within a block?
• One approach can be to form a hash pointer-based linked list of transactions and store
this complete linked list in a block.
• However, when we put this approach into perspective, it does not seem practical to
store a huge list of hundreds of transactions.
• What if there is a need to find whether a particular transaction belongs to a block?
Then we will have to traverse the blocks one by one and within each block traverse the
linked list of transactions.
• This is a huge overhead and can reduce the efficiency of the blockchain.
• Now, this is where the Merkle tree comes into the picture.
• Merkle tree is a per-block tree of all the transactions that are included in the block.
• It allows us to have a hash/digest of all transactions and provides proof of membership
in a time-efficient manner.
• what is this block header?
• A block is composed of a header and a body. The block header
contains Merkel root, Timestamp, Block Version number (indicates
which set of block validation rules to follow), Difficulty Target, Nonce,
and Previous Hash. On the other hand, the block body contains all
confirmed transactions within the block.
• Block header: The header data contains metadata of the block, i.e
information about the block itself. The contents of the block header
include-
• Hash of the previous block header
• Hash of the current block
• Timestamp
• Cryptographic nonce
• Merkle root
What Is a Merkle Root?

• A Merkle root is a simple mathematical method for confirming the


facts on a Merkle tree.
• They're used in cryptocurrency to ensure that data blocks sent
through a peer-to-peer network are whole, undamaged, and
unaltered.
• They play a very crucial role in the computation required to keep
cryptocurrencies like bitcoin and ether running.
Cryptographic Hash Functions

• A hash function maps any type of arbitrary data of any length to a fixed-
size output. It is commonly used in cryptography since it is a cryptographic
function.
• They are efficient and are well-known for one property: they are
irreversible. It's a one-way function that's only meant to work in one
direction.
• Some of the Hash families available are Message Direct (MD), Secure Hash
Function (SHF), and RIPE Message Direct (RIPEMD).
• Now, take an example, if you use the SHA256 hash algorithm and pass
101Blockchains as input, you will get the following output

fbffd63a60374a31aa9811cbc80b577e23925a5874e86a17f712bab874f33ac9
Working of Merkle Trees

• A Merkle tree totals all transactions in a block and generates a digital


fingerprint of the entire set of operations, allowing the user to verify
whether it includes a transaction in the block
• Merkle trees are made by hashing pairs of nodes repeatedly until only
one hash remains; this hash is known as the Merkle Root or the Root
Hash.
• They're built from the bottom, using Transaction IDs, which are
hashes of individual transactions.
• Each non-leaf node is a hash of its previous hash, and every leaf node
is a hash of transactional data.
• Now, look at a little example of a Merkle Tree in Blockchain to help you
understand the concept.

• Consider the following scenario: A, B, C, and D are four transactions, all


executed on the same block. Each transaction is then hashed, leaving you
with:

• Hash A
• Hash B
• Hash C
• Hash D
• The hashes are paired together, resulting in:

• Hash AB
• and

• Hash CD
• And therefore, your Merkle Root is formed by combining these two
hashes: Hash ABCD.
Benefits of Merkle Tree in Blockchain

• Merkle trees provide four significant advantages -

• Validate the data's integrity: It can be used to validate the data's


integrity effectively.
• Takes little disk space: Compared to other data structures, the Merkle
tree takes up very little disk space.
• Tiny information across networks: Merkle trees can be broken down
into small pieces of data for verification.
• Efficient Verification: The data format is efficient, and verifying the
data's integrity takes only a few moments.
Why Is It Essential to Blockchain?

• Think of a blockchain without Merkle Trees to get a sense of how vital


they are for blockchain technology. Let’s have one of Bitcoin because
its use of Merkle Trees is essential for the cryptocurrency and easier
to grasp.
• If Bitcoin didn't include Merkle Trees, per se, every node on the
network would have to retain a complete copy of every single
Bitcoin transaction ever made. One can imagine how much
information that would be.
• Any authentication request on Bitcoin would require an enormous
amount of data to be transferred over the network: therefore, you'll
need to validate the data on your own.
• To confirm that there were no modifications, a computer used for
validation would need a lot of computing power to compare ledgers.
• Merkle Trees are a solution to this issue. They hash records in
accounting, thereby separating the proof of data from the data itself.

• Furthermore, it enables you to demonstrate that both ledger


variations are identical in terms of nominal computer power and
network bandwidth.
• So to recap, the blockchain is a hash-based linked list of blocks, where
each block consists of header and transactions. The transactions are
arranged in a tree-like fashion, known as the Merkle tree.
• A Merkle tree is constructed from the leaf nodes level all the way up
to the Merkle root level by grouping nodes in pairs and calculating
the hash of each pair of nodes in that particular level. This hash
value is propagated to the next level. This is a bottom to up type of
construction where the hash values are flowing from down to up
direction.
• Hence, by comparing the Merkle tree structure to a regular binary
tree data structure, one can observe that Merkle trees are actually
inverted down.

You might also like