Bitcoin Forum
May 05, 2024, 03:27:01 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Merkle Trees and Merkle Roots for dummies  (Read 260 times)
Husires (OP)
Legendary
*
Offline Offline

Activity: 1596
Merit: 1285



View Profile WWW
September 01, 2020, 07:06:05 PM
Merited by DdmrDdmr (3), vapourminer (2), ABCbits (2), Heisenberg_Hunter (2), pooya87 (1), webtricks (1)
 #1


Disclaimer: The information's contained in this explanation is the result of my understanding of Merkle Trees and Merkle Roots and may contain some errors. Search and confirm about it.


Table of contents

      1. What is a Merkle tree?
      2. Cryptographic Hash Functions
      3. How do Merkle trees work?
      4. Merkle trees Uses
      5. Why it used in Bitcoin?


Source: Bitcoin whitepaper


What is a Merkle tree?

Merkle tree (also known as hash tree) a tree in which every leaf node is labelled with the cryptographic hash of a data block, and every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes.

In a Merkle tree, every non-leaf node is labelled with the hash of the labels of its child nodes (or the hash of the value of a leaf at the bottom of the tree), a process which repeats itself moving up the tree until a single hash remains: the Merkle root.


Merkle root: is the hash of all the hashes of all the transactions that are part of a block in a blockchain network.

Its structure is used to efficiently verify the integrity of data in a group.
The idea was created and patented by Ralph C. Merkle in 1979, with the patent expiring in 2002.



Cryptographic Hash Functions

Hash function is any function that enables us to obtain an output of a fixed size as it is completely unique to the input and the function itself is deterministic.
It also leads to significant savings in storage and helps increase efficiency.
You can find out more information by reading the previous topic that I wrote about hash function for dummies

Example:

Code:
In-put: H
Out-put: 44bd7ae60f478fae1061e11a7739f4b94d1daf917982d33b6fc8a01a63f89c21

In-put: Husires
Out-put: 198d93f2c0bff9767d4cdc047f2191b0921d81e410c10c0744311fadfdb516f9

In-put: Today is 1/9/2020 and i used my username: Husires
Out-put: 2afbf1c88e101259002a592dbcc9340af2f0fa8f51a06e77910b6aca63a97c0c

In-put: Today is 1/9/2020 and i used my username: Husires
Today is 1/9/2020 and i used my username: Husires
Today is 1/9/2020 and i used my username: Husires
Today is 1/9/2020 and i used my username: Husires
Today is 1/9/2020 and i used my username: Husires
Out-put: 43a322d0eb09caaf26762ae18a369f5e8e06ac32c9e6a0d9fb4972aa53654db8
What did you get from this example?



How do Merkle trees work?

Let's simplify it, suppose you have a large file size of 8 GB and you want to verify its authenticity, so you match the hash that you have with the hash that was made available to the public by the developers and therefore if each file in it matches all the data then you downloaded the correct file, but how will we do that?

you can compare each file separately until you reach the last file (so you need to compare 8 GB files) but it will take a long time. Merkle trees offer a solution which will reduce your running time.

First, we will divide the big problem into smaller ones. For example, instead of having an 8 GB file, we will divide it into eight pieces. Call the different parts A through H. Then each part is passed through a hash function, which gives us eight different hashes.


Now we take each pair and compare them (hA + hB, hC + hD, hE + hF, and hG + hH,) and then combine them, using the hash function and repeat.


So any slight modification to the data will give us a completely different Merkle root. We can also check the non-conforming part easily.



Merkle trees Uses

can be used to verify any kind of data stored, help ensure that data blocks received from other peers in a peer-to-peer network are received undamaged and unaltered, used in the IPFS, Btrfs and ZFS file systems, the Bitcoin and Ethereum peer-to-peer networks, and a number of NoSQL systems such as Apache Cassandra, Riak, and Dynamo.



Why it used in Bitcoin?

They are an integral part of every block, as they can be found in the block headers.

Use it in SPV: Many of us have limited resources in terms of space and internet so we all rely on light clients, and instead of downloading all block transactions, you require Merkle proof - evidence provided by the full node proving that your transaction is in a particular block.

If we go back to the previous example, we can verify through 3 steps instead of 7 steps, for example to verify hB. If we have hA, we can work out hAB. so we can calculate hABCD by using hCD and with hEFGH, we can check that the resulting Merkle root matches the one from the block header.

3 Steps only.

You can read more in https://bitcoin.org/bitcoin.pdf

It has some Drawbacks with a successful 51% attack  which will talk about it in another topic.

Mining: Bitcoin block is made up of two pieces, Header (fixed-size) and a list of transactions (variable-size)
Often times, much of the block remains the same, and the nonce is changed.

Merkle root simplifies the whole process, the miner selects all the transactions it wants to include in the block, creating a Merkle tree and only need to hash the head of the block, rather than the entire block.

Hence, any change in the transaction list will change the Merkle root and thus reject the block.



Sources

Code:
https://en.wikipedia.org/wiki/Merkle_tree
https://brilliant.org/wiki/merkle-tree/
https://www.youtube.com/watch?v=fB41w3JcR7U
https://golden.com/wiki/Merkle_tree-W3DMKV
https://academy.binance.com/blockchain/merkle-trees-and-merkle-roots-explained
https://blockonomi.com/merkle-tree/

.BEST..CHANGE.███████████████
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
███████████████
..BUY/ SELL CRYPTO..
1714922821
Hero Member
*
Offline Offline

Posts: 1714922821

View Profile Personal Message (Offline)

Ignore
1714922821
Reply with quote  #2

1714922821
Report to moderator
1714922821
Hero Member
*
Offline Offline

Posts: 1714922821

View Profile Personal Message (Offline)

Ignore
1714922821
Reply with quote  #2

1714922821
Report to moderator
Be very wary of relying on JavaScript for security on crypto sites. The site can change the JavaScript at any time unless you take unusual precautions, and browsers are not generally known for their airtight security.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714922821
Hero Member
*
Offline Offline

Posts: 1714922821

View Profile Personal Message (Offline)

Ignore
1714922821
Reply with quote  #2

1714922821
Report to moderator
1714922821
Hero Member
*
Offline Offline

Posts: 1714922821

View Profile Personal Message (Offline)

Ignore
1714922821
Reply with quote  #2

1714922821
Report to moderator
Mrhollownow
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
September 07, 2020, 05:07:04 AM
 #2

Really good thread !
Can you post the practical thing(A example from the block) to people get more Knowledge on this
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10549



View Profile
September 07, 2020, 07:24:32 AM
 #3

Really good thread !
Can you post the practical thing(A example from the block) to people get more Knowledge on this

go to a block explorer like blockchair.com and sort the blocks based on the number transactions inside like this:
https://blockchair.com/bitcoin/blocks?s=transaction_count(asc)#f=id,transaction_count,merkle_root
then start from 1 tx and compute the merkle root yourself to learn how it works (when it is only one, merkle root is the same as tx hash). you should do this by opening the link to that block and copying its transaction hashes (keep in mind hash strings in bitcoin are reported in reverse order)
then move the count higher to 2 transactions and do the same computation then move it to 3 transactions and finally move it to 4 transactions and repeat calculation for the last time.

now you know almost all the cases and anything more than this number is mostly repetition of the same thing.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!