Blockchain and the Merkle tree: how it works and why history cannot be rewritten
The blockchain is Bitcoin’s ledger. Payments are not added to it one by one but in batches called blocks; a new batch is written roughly every ten minutes. No bank keeps the book in a drawer. Copies lie on thousands of computers scattered around the world, known as nodes, and each of them checks for itself that new blocks follow the rules: nobody spends more than they have, every payment was signed by the owner of the money (the part on the address shows how) and the miner has not credited themselves with a bigger reward than the one set. How the whole of Bitcoin fits together is described in the opening part of the series.
Why bother with it at all? Because of two questions that the blockchain answers with maths, not with trust. How can it be that nobody can quietly rewrite a payment that is already in the book? And how can a phone wallet check that somebody has really paid you, when it cannot download the whole book, nearly 800 GB today? The first question is answered by a chain of hashes, the second by a Merkle tree.
Both rest on the hash. It is a short string of characters that stands in for any data, as a fingerprint stands in for a person. It is worked out by the SHA-256 function; think of it as a black box that a computer runs through in a fraction of a second. The same data always gives the same hash, but change a single character and the hash is unrecognisable. The text “Adam posílá Báře 1 BTC” (Czech for “Adam sends Bára 1 BTC”) has a hash beginning 9afdc343, and the text “Adam posílá Báře 2 BTC” a hash beginning 01ccb05a. You will find more about SHA-256 in the part on mining and in the article on hash functions.
A chain of hashes
Every block carries the hash of the block before it, and that is what turns the blocks into a chain: block 101 points to block 100, block 102 to block 101, and so on back to the very first block from January 2009. That one has no predecessor, so where its hash would be there are only zeros.
Why can’t it simply be rewritten? Only someone who finds a number that makes the hash of the block begin with a long string of zeros may add a block. That can be done only by trying on an enormous scale, which is called mining, and the effort is called work. Rewriting one block therefore means mining it again together with all the later ones, and doing it faster than the rest of the network.
Let’s build a chain in miniature. Each block will hold a single payment, and the hash of a block will be the first 8 characters of the SHA-256 of the text made by writing the hash of the previous block, a space and the payment (a full hash has 64 characters; to keep things clear we take only the beginning). Block 100 opens the chain, so instead of the hash of a predecessor it gets all zeros. Its text is therefore 00000000 Adam posílá Báře 1 BTC and the hash comes out as
Block 101 carries the payment “Bára posílá Adamovi 2 BTC” (“Bára sends Adam 2 BTC”). Its text begins with the hash of block 100, so it is 4aeb0b57 Bára posílá Adamovi 2 BTC, and the hash comes out as c94bac93. Block 102 with the payment “Adam posílá Báře 3 BTC” (“Adam sends Bára 3 BTC”) gets the hash 5d27edef in the same way.
Enter the attacker Eva. She would like the first payment to go to her instead of Bára, so in block 100 she changes “Adam posílá Báře 1 BTC” to “Adam posílá Evě 1 BTC” (“Adam sends Eva 1 BTC”). The hash of block 100 changes to ca3444dc. But block 101 still contains 4aeb0b57. The hashes do not match, and a node needs just one calculation to spot the discrepancy.
So Eva has to rewrite block 101 as well and put the new hash ca3444dc into it. But that changes the hash of block 101 too, from c94bac93 to 8ee92eff, so she has to rewrite block 102, whose hash changes to 4c85a2ea. Changing a single payment changes the hash of its block and of all the blocks after it.
In our little chain Eva’s rewrite comes to three hash calculations, which is child’s play. In real Bitcoin, though, every block is mined, as the part on Bitcoin mining describes. Anyone who rewrites a payment in a block with twenty more blocks on top of it has to mine all twenty-one again. Meanwhile the rest of the network does not wait but keeps lengthening the chain. Nodes treat as valid the history with the most work behind it, so Eva would have to outrun the honest miners. Her prospects are worked out in the part on the 51% attack.
For a node, on the other hand, checking is easy: it recalculates the hashes and looks at whether every block points to the one before it. The latest hash therefore guards the whole history: if it agrees with the one the other nodes have, everything beneath it agrees too.
The Merkle tree
A real block carries not one payment but thousands. Block 978,859 of 5 December 2026, for example, has 2,583 of them and weighs 1.57 MB. But the hash of a block is not worked out from the whole block, only from its header, which has a mere 80 bytes (a byte is 8 bits), as the part on mining shows. For all the payments the header has a single field of 32 bytes, room for exactly one full hash. How do thousands of payments fit into it?
The payments are arranged in a tree. Take eight payments, which we label T1 to T8: “Adam posílá Báře 1 BTC”, “Adam posílá Báře 2 BTC” and so on up to 8 BTC. We work out a hash of each, again only the first 8 characters; the hash of the first payment, 9afdc343, we already know. Then we pair the hashes, the first with the second, the third with the fourth, and from each pair we work out a new hash: we write the two hashes one after the other, this time without a space (the space was only for readability), and calculate the SHA-256 of the resulting text. Eight hashes become four, four become two and two become one. That last one is the root, and in our example it came out as 63344159.
This tree is called a Merkle tree, after Ralph Merkle. He described it in a patent that he filed on 5 September 1979, and his example tree had eight leaves, just like ours.
The root is written into the header. If a single payment is changed, the change fights its way up hash by hash to the root. The header changes, and so does the hash of the block, and we are back at the chain.
Real Bitcoin works with full hashes, each one calculated twice in a row, and an odd last pair is filled out with a copy (documentation). We tried it: from mempool.space we downloaded the hashes of all 2,583 payments of block 978,859, built a tree from them, and the root came out exactly as it stands in the block header.
Verifying a single payment
Wouldn’t it be enough to stick all the payment hashes together and work out one hash from them? Why a tree? That would work, but the proof would then require downloading all the payments. The tree proves that one payment is in the block, using just a few hashes.
Bára has the block header in her phone, and so the root 63344159 too. She wants to know whether the block contains the payment T5, “Adam posílá Báře 5 BTC”. She does not want to download the whole block for that. All she needs is the payment itself and three hashes, the ones shown in orange in the picture:
First Bára works out the hash of the payment herself, bab3a80c. Then she climbs up the tree:
- She joins it to the first orange hash,
27a52872. From the textbab3a80c27a52872comesbb252ff2. - She joins the result to the second orange hash,
6bdaadbb, and gets3a605011. - The third orange hash,
c5c8bde0, lies on the left in the tree, so she writes it first; the order matters, “AB” gives a different hash from “BA”. From the textc5c8bde03a605011comes63344159.
It matches the root in the header character for character, so the payment really is in the block. If somebody slipped her a payment that is not in the block, they would have to find hashes for it such that the same root came out in the end, and nobody can do that with SHA-256: it would mean breaking the hash function itself, as the part on SHA-256 explains.
And where did the three come from? Every step up the tree halves the number of hashes. And on each level Bára needs a single hash, the sibling of hers. The number of hashes in the proof is therefore the number of times eight can be halved before one is left. In maths that number is called a logarithm to base 2:
because 23 = 8.
The whole trick is how lazily a logarithm grows. Since 212 = 4096, a block with 4,000 payments needs only 12 levels, and so 12 hashes. For a million payments it would be 20 hashes (220 = 1,048,576), for a billion 30. The payments multiplied a thousandfold, the hashes grew by only ten.
For our real block 978,859 with 2,583 payments that makes 12 hashes of 32 bytes each, 384 bytes altogether. The whole block is 1.57 MB, about four thousand times as much.
A wallet in your phone
This very trick is what the so-called light wallets in phones live on. Satoshi Nakamoto described them as long ago as the original Bitcoin paper of 2008, under the name simplified payment verification. The phone does not store the whole chain; the headers of all the blocks are enough for it. When somebody sends it a payment, it asks a server or a node for a proof from the Merkle tree and checks it against the root in the header, exactly like Bára.
How much space does that take? On 5 December 2026 the chain has 978,880 blocks (the last has the number 978,879, as counting starts from zero). Each header has 80 bytes:
In a year about 80 · 6 · 24 · 365 ≈ 4.2 MB is added, that is, six blocks an hour all year round. Satoshi worked it out the same way.
The whole chain with all the payments was about 790 GB on 4 December 2026 (the sum of the block sizes according to blockchain.com). That is roughly ten thousand times more than the headers alone.
The complete chain is kept by nodes. They download every block since 2009 and check every payment in it themselves. The Bitnodes project counted about 22,000 nodes on 5 December 2026 that can be connected to from outside.
A phone wallet wins on convenience but loses on knowledge. It does not check a payment itself; it only sees that some node has accepted it into a block and that further blocks follow that block. A node cannot slip it a fake payment, though, because it would have to mine the headers for it, and it could manage that only if it outdid the whole network. Satoshi says so outright: verification is reliable as long as honest nodes control the network, and businesses that receive many payments will probably want their own node.
Confirmations
The blocks that follow the block with a payment are visible to the phone in the headers. A payment that has made it into a block has one confirmation. With every further block on top of it, one more is added.
For larger amounts, merchants therefore wait for six confirmations, about an hour. Why six, and what chance an attacker has with one, two or six confirmations, is worked out in the part on the 51% attack.
Blockchain beyond Bitcoin
Neither the chain of hashes nor the tree of hashes was invented by Bitcoin; it just combined them cleverly. In his paper Satoshi cites earlier work on timestamping electronic documents. The same principle underlies, for example, Git, the program in which programmers store the history of their code: every saved version carries the hash of the one before it and the files are arranged in a tree of hashes, as the Pro Git book describes. A chain of hashes mines nothing and promises nothing; it only guarantees that what has already been written cannot be changed without anyone noticing.
See also
Chapters: Bitcoin: what it is and how it works, The Bitcoin halving: why there are only 21 million bitcoins, Bitcoin mining: how proof of work works, Blockchain and the Merkle tree: how it works and why history cannot be rewritten, The Bitcoin address, private key and signature, Seed phrase: why nobody can guess the 12 words of a wallet, Ethereum: smart contracts and proof of stake, Is Bitcoin secure? What its cryptography rests on, The 51% attack: can Bitcoin be spent twice?, Quantum computers and Bitcoin: what if elliptic curves fell, Bitcoin and SHA-256: what if the hash function fell, Post-quantum Bitcoin: how Bitcoin would defend itself against quantum computers