Merkle Trees in Bitcoin
By Kurt Wuckert Jr.
Merkle trees are not just an abstract data structure; in Bitcoin they are part of how the system treats satoshis as cash-like objects that can be verified quickly and reliably. To see how this works, we need to connect three pieces: how Bitcoin models electronic cash, how transactions are formed, and how those transactions are organized into Merkle trees.
Merkle Trees and Cash-like Tokens in Bitcoin
Bitcoin is designed as a peer-to-peer electronic cash system. With physical cash, central banks use watermarks, special inks and other techniques to make counterfeiting difficult. In a digital cash system, we need different tools to ensure that the units of value are authentic and not duplicated.
On Bitcoin, those units are satoshis, and their history is expressed through transactions. For the system to function as money, the movement of satoshis must be verifiable from issuance through every transfer. Merkle trees are one of the mechanisms that allow the network and its participants to efficiently check that transactions are valid and belong to a consistent view of history.
How Transactions Become Data Elements
Every time satoshis change hands on Bitcoin, a transaction is created. Wallet software constructs this transaction using Bitcoin Script, the native scripting language of the protocol.
At a minimum, a transaction must do two things:
Provide a valid solution to a previous locking puzzle that controls some satoshis.
Create one or more new locking conditions that will govern those satoshis in the hands of the next owner.
The statements that solve previous puzzles are called inputs. The statements that set up new puzzles for the next custodians are outputs. This input-output model lets the network trace where coins came from and where they are going without tracking accounts or balances in the traditional sense.
Scripts, Signatures and Locking Puzzles
The locking and unlocking parts of a transaction often rely on digital signatures created with asymmetric keys. Wallets generate public and private keys using an elliptic curve digital signature algorithm (ECDSA). The private key stays with the owner; the public key can be shared.
In simple terms, a digital signature is an electronic stamp that proves a particular private key holder approved a given message. In Bitcoin, that message is tied to transaction data. The most common template is Pay-to-Public-Key-Hash (P2PKH). In this pattern:
A hash of the public key is used in the locking script to define who can spend the output.
When spending, the unlocking script includes the public key and a signature over the transaction preimage.
The public key itself is revealed only when the coin is spent, and the hash acts as a short handle for it before that.
Script opcodes describe how to combine and check these pieces of data, so a node can evaluate the script and decide whether it returns true.
From Raw Transactions to TXIDs
Under the hood, all the fields of a transaction are serialized into a string of raw bytes. This includes version, inputs, outputs, scripts and other metadata. Because each transaction is tied to specific previous outputs and has its own set of new outputs, its raw data is unique as long as it is not a duplicate attempt to spend the same inputs.
To avoid passing around this entire blob of data every time we need to reference a transaction, Bitcoin uses a transaction identifier, or TXID. The TXID is created by hashing the serialized transaction data twice with SHA-256. This double-hash, sometimes written as HASH256, gives a 32-byte value that stands in for the entire transaction. Anyone with the raw data can recompute the TXID and verify that it matches.
Transaction Merkle Trees
Merkle trees in Bitcoin are built from these TXIDs. Each leaf node in the tree is a 32-byte TXID. The leaf set is usually ordered in the sequence transactions are observed on the network, subject to the realities of propagation and latency. There is no hard rule about global transaction order at this stage, but miners will work with the set they see.
To build the tree:
The set of TXIDs is placed at the leaf layer.
Adjacent TXIDs are concatenated in pairs and hashed with HASH256 to form the next layer of nodes.
That process repeats, layer by layer, using HASH256 on concatenated child values, until a single hash remains at the top.
This top hash is the Merkle root. In Bitcoin, the leaves, the interior nodes and the root are all computed with double SHA-256. Because the concatenation is ordered, swapping two transactions will change the result for that pair and all nodes above it. As a result, the Merkle tree does not merely attest to which transactions are present; it also reflects their ordering within the set.
As new transactions are added to a block in progress, the miner updates the Merkle tree to include the additional TXIDs and recomputes the root. Once the block is finalized, that Merkle root is placed in the block header. From that point, any participant who knows the root and has a Merkle path for a particular TXID can prove that the transaction is part of that block.
Scripting, Embedded Data and Merkle Verification
Bitcoin’s scripting language uses a fixed set of opcodes to describe complex conditions under which outputs can be spent. Beyond simple payments, these scripts can:
Require multiple signatures.
Check structured data fields.
Implement contract-like behavior with detailed conditions.
Reference or evaluate data written in earlier transactions.
Users can embed various forms of data into transaction outputs, then construct scripts that operate on that data or use it as part of a larger workflow. Because embedded data lives inside the same transactions that generate TXIDs, it is indirectly covered by the Merkle tree. A Merkle proof that a transaction is in a block also gives assurance that any associated data is present in its original form.
This means Bitcoin can serve as a verification layer for many types of external objects. Files, records or application data can be hashed, stored, or referenced in transactions. Merkle trees and TXIDs provide a compact way to prove that those objects have not changed since they were first committed.
Working Blockchains and Shared Records
In practice, many different parties may maintain their own databases of transactions that are relevant to them. Each stored transaction can be accompanied by a Merkle proof that ties it back to a known block header. A collection of such transactions and proofs can be thought of as a “working blockchain.” It is not the full chain mined by nodes, but a subset that remains anchored to it via proof of work and Merkle roots.
The Merkle structure makes it straightforward for two parties to compare their records. If they share a block header and Merkle root, they can exchange proofs to confirm that individual transactions match. If there is a discrepancy, they can narrow it down by comparing interior nodes in the tree and tracing differences to particular branches.
When there are competing views of which transactions belong in a given block, there will be differing Merkle roots. Resolving that conflict and deciding which root is accepted as canonical is the job of the mining network and its consensus process, which is driven by proof of work. Once consensus is reached, the agreed Merkle root becomes the reference point for all future validation. New transactions that try to spend the same inputs as already confirmed ones can be detected as double spends by checking against that established history.
Merkle trees, TXIDs and Bitcoin Script work together in Bitcoin to provide a compact, verifiable structure for all transaction data and any information embedded within it. This combination allows wallets, services and auditors to prove inclusion and ordering without needing to hold or transmit the full blockchain, while still relying on the same proof-of-work backbone that secures the network.