Explore Website

Calculating the Merkle Root for a block

Overview

This tutorial is written to simplify the understanding of how bitcoin uses merkle trees for verification of transaction into a block. A merkle root is created by hashing together pairs of TXIDs, which gives you a short yet unique fingerprint for all the transactions in a block.

This merkle root is then used as a field in a block header, which means that every block header will have a short representation of every transaction inside the block.

This tutorial will demonstrate how you calculate the merkle root field.

Prerequisite

You will need access to a bitcoin node. We suggest executing against a node configured in regtest mode so that we can have the freedom of playing with various scenarios without having to loose real money. You can however execute these against either the testnet or mainnet configurations.

Note:
If you don't currently have access to a bitcoin development environment set up, dont' worry, we have your back! We've setup a web based mechanism which provisions your very own private session that includes these tools and comes preconfigured with a bitcoin node in regtest mode. https://bitcoindev.network/bitcoin-cli-sandbox/
Alternatively, we have also provided a simple docker container configured in regtest mode that you can install for testing purposes.

Understanding hashes

Before diving into merkle trees, let's look into a primitive required for their operation called hash functions, or trapdoor functions. These functions are easy to compute in one direction, yet difficult to compute in the opposite direction (finding its inverse) without special information, called the "trapdoor". Trapdoor functions are widely used in cryptography. A hash function is any function that can be used to map digital data of an arbitrary size to a fixed size with slight differences in input data producing very big differences in output data.

Here we are providing the data Hello World and piping it into the opnessl command with the digest flag of sha256. The output we receive here is the fingerprint of the input data a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e. Anyone using sha256 on the same input data would result in the same hash. A slight change to the data results in a completely different hash.

Some background on merkle trees

In the previous example we had a brief look into how you can generate a unique fingerprint from any arbitrary input data. This comes in useful when we need to provide a cryptographic proof for a large amount of data. This data can be presented in what is called a merkle or hash tree. Merkle trees are a data structure from which you can derive the same hash as we had indicated before.

A property of merkle trees is that any change in the leaf node layer would result in a completely different merkle root hash. We can therefore use this data structure to verify the integrity of a set of data.

Here we can see the two transaction identifiers for the block as 3bd3a1309a518c381248fdc26c3a6bd62c35db7705069f59206684308cc237b3 (our coinbase) and a99011a19e9894753d6c65c8fa412838ea8042886537588e7205734d5de8956d (the identifier for the transaction we executed above)

Next, we need to reverse the order of these from big to little endian (network byte order)

Here we can see that the merkle root is the same as the transaction id for the coinbase transaction. There is another where if you are left with an uneven amount of leaf nodes, that you appened an existing tx id to the list of leaf nodes to calculate the first level of hashing pairs.

Conclusion

In this tutorial we had a look at hash functions, merkle trees and how to calculate the merkle root. We then proceeded to demonstrate how you this can be done using the bitcoin core cli tool with some helper command line utilities.

Explore Website

About Bitcoin Developer Network

The Bitcoin Developer Network (BDN) is a community-driven project looking at educating the generation of Bitcoin developers. We invite developers, authors, editors, proofreaders, enthusiasts, subject matter experts and technical writers to get in touch so that we may reduce the barrier to entry in developing solutions at various layers of this technology.

Bitcoin Developer Network Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts by email.