Sub topic 4
Bitcoin: UTXOs, limited scripting, and design goals
This chapter assumes you know what a hash function is, what a public/private key pair does, and how a blockchain orders transactions into blocks. It also assumes you can read short Python snippets. Here you will learn how Bitcoin tracks ownership with unspent transaction outputs (UTXOs), why its scripting language is deliberately small, and which design goals explain those choices.
Design goals behind Bitcoin
Bitcoin was built to move value between strangers without a trusted operator. That goal drives most of its technical choices.
- Verifiability. Any participant should be able to check the whole history with modest hardware.
- Predictability. Rules should be simple enough that every node reaches the same result for the same data.
- Conservatism. Changes to consensus rules are rare and cautious, because a bug can split the network or destroy value.
- A narrow job. Bitcoin focuses on transferring and securing its own unit, rather than running general applications.
Keep these goals in mind. The UTXO model and the limited scripting language both follow from them.
The UTXO model
Bitcoin has no account balances stored on the ledger. The ledger records transactions, and each transaction consumes earlier outputs and creates new ones.
An unspent transaction output (UTXO) is an output of a past transaction that no later transaction has spent yet. Each UTXO has two main parts:
- an amount, measured in satoshis (1 BTC = 100,000,000 satoshis)
- a locking condition, called a script, that says what must be proven to spend it
A transaction has inputs and outputs. Each input points to one existing UTXO and supplies data that satisfies its locking condition. Each output creates a new UTXO. An output can be spent only once, and it must be spent in full.
A wallet's "balance" is therefore a sum. The wallet software scans the set of all UTXOs, finds those it can unlock, and adds up their amounts.
A worked example
Alice owns two UTXOs: 0.6 BTC and 0.3 BTC. She wants to pay Bob 0.7 BTC. Neither UTXO is large enough alone, so she spends both.
- Inputs: 0.6 BTC and 0.3 BTC, totaling 0.9 BTC.
- Output 1: 0.7 BTC locked to Bob.
- Output 2: 0.1999 BTC locked back to Alice, called change.
- The remaining 0.0001 BTC is the fee, which goes to the miner.
The fee is not written in the transaction. It is the difference between the input total and the output total.
Checking the arithmetic in code
The following script models the rule that inputs must cover outputs, and that the difference is the fee. It uses integer satoshis, which avoids floating-point rounding errors.
SATS_PER_BTC = 100_000_000
def to_sats(btc):
return round(btc * SATS_PER_BTC)
def validate(inputs_sats, outputs_sats):
total_in = sum(inputs_sats)
total_out = sum(outputs_sats)
if total_out > total_in:
raise ValueError("outputs exceed inputs")
return total_in - total_out # the fee
inputs = [to_sats(0.6), to_sats(0.3)]
outputs = [to_sats(0.7), to_sats(0.1999)]
fee = validate(inputs, outputs)
print(fee) # 10000 satoshis, which is 0.0001 BTCIf you forget the change output, validate still succeeds. The whole difference becomes the fee, and no one can recover it. Losing money to an omitted change output is a real, known mistake, which is why wallet software builds the change output automatically.
Properties that follow from UTXOs
| Property | What it means in practice |
|---|---|
| Parallel validation | Each input refers to one specific UTXO, so nodes can check unrelated transactions independently. |
| Double-spend detection | A node only needs to ask whether the referenced UTXO is still in the unspent set. |
| Simple state | A node keeps the UTXO set, not a history of balances per account. |
| Coarser privacy tools | Wallets can use a fresh address for every output, though analysis can still link them. |
| Awkward shared state | Anything needing a shared, changing value (such as a counter) fits poorly. |
The UTXO set is the list of all currently unspent outputs. Full nodes keep it ready for fast lookups. Its size matters because it grows as people create many small outputs, and each one costs storage on every full node.
Common errors with UTXOs
- Treating a balance as a stored number. There is no balance field. Different wallets can report the same funds differently if they scan different address sets.
- Dust. An output so small that spending it would cost more in fees than it is worth is called dust. Nodes by default refuse to relay transactions that create such outputs.
- Address reuse. Sending to the same address repeatedly links your payments together and weakens privacy.
- Fee surprises. Fees depend on transaction size in bytes (more precisely, virtual bytes), not on the amount sent. A transaction that gathers many small UTXOs is large and costs more.
How spending is authorized
A UTXO is locked by a script, and an input unlocks it. In the common case, the lock says "provide a public key that hashes to this value, and a valid signature from the matching private key". The wallet's private key never appears on the chain. Only the signature and public key do.
Nodes check three things for each input:
- The referenced UTXO exists and is unspent.
- The unlocking data satisfies the locking script.
- Total inputs are at least total outputs.
If all inputs pass, the transaction is valid. Whether a miner includes it is a separate question, and usually depends on the fee.
Limited scripting
Bitcoin Script is the language for locking conditions. It is a stack-based language, meaning operations push and pop values on a stack, and it reads left to right.
Its design is intentionally restrictive:
- No loops. Every script finishes after a bounded number of steps, so a script can't run forever.
- Limited size and operation counts. Nodes can estimate the maximum cost of verifying any script.
- No access to outside data. A script sees only the transaction and its own stack, not web data or a clock beyond simple time locks.
- Deterministic results. Every node gets the same answer.
The following Python function mimics the idea of a stack machine for a very small subset. It is a teaching model and not real Bitcoin Script.
def run(script, stack=None):
stack = [] if stack is None else list(stack)
for op in script:
if op == "ADD":
b, a = stack.pop(), stack.pop()
stack.append(a + b)
elif op == "EQUAL":
b, a = stack.pop(), stack.pop()
stack.append(a == b)
elif isinstance(op, int):
stack.append(op)
else:
raise ValueError("unknown op: " + str(op))
return bool(stack) and stack[-1] is True
# "Unlock" data is 2 and 3. The lock adds them and checks for 5.
print(run([2, 3, "ADD", 5, "EQUAL"])) # True
print(run([2, 2, "ADD", 5, "EQUAL"])) # FalseNotice that no instruction can jump backward. Without loops, the cost of running a script is bounded by its length.
What limited scripting still allows
Despite the restrictions, Bitcoin supports several useful patterns:
- Pay to public key hash: the standard single-signature payment.
- Multisignature: require, for example, 2 of 3 keys to sign.
- Time locks: forbid spending until a certain block height or time.
- Hash locks: require revealing a secret whose hash is known, which is the basis for some payment-channel designs.
Newer upgrades such as SegWit and Taproot changed how signatures and scripts are committed and revealed. They kept the philosophy: add flexibility and efficiency carefully, as backward-compatible changes.
Trade-offs of this design
| Choice | Benefit | Cost |
|---|---|---|
| UTXO ledger | Easy parallel checking, clear spend rules | Change outputs, wallet complexity, UTXO set growth |
| Non-looping scripts | Bounded verification cost, small attack surface | Hard to express stateful applications |
| Conservative upgrades | Lower risk of consensus failure | Slow feature development |
| Small block capacity | Keeps full nodes affordable | Limited throughput and fee pressure during busy periods |
Later chapters compare this with Ethereum's account model and discuss scalability in more depth. For now, the key point is that Bitcoin's constraints are deliberate and consistent with its goals.
Edge cases to remember
- Coinbase transactions. The first transaction in each block creates new coins for the miner. It has no regular input. Its outputs can't be spent until 100 blocks later, which guards against a chain reorganization invalidating them.
- Unconfirmed chains. You can spend an output from a transaction that is not yet in a block, but if the parent is dropped or replaced, the child becomes invalid.
- Transaction malleability. Before SegWit, third parties could alter certain parts of a transaction without invalidating it, changing its ID. SegWit removed signature data from the ID calculation and fixed this for most cases.
- Consolidation. Merging many small UTXOs into one is cheaper when fees are low. Doing it during a busy period costs more.
Exercise
A wallet holds UTXOs of 50,000, 30,000, and 20,000 satoshis. You want to send 60,000 satoshis and pay a 1,000 satoshi fee. Which UTXOs could you spend, and how much change comes back? Why can you not spend just part of one UTXO?
Show answer
Spending 50,000 and 30,000 gives 80,000 in. Outputs are 60,000 to the recipient and 19,000 as change, leaving a 1,000 fee. Spending 50,000 and 20,000 gives 70,000 in, with 9,000 change. Spending all three also works, with 39,000 change. Spending 50,000 alone is not enough, since it cannot cover 61,000. A UTXO must be consumed in full, because it is a single unit whose lock is checked as a whole. Any unused part must be returned through a new change output.
Exercise
Why does Bitcoin Script omit loops, and what kind of application does that make awkward?
Show answer
Without loops, every script ends after a bounded number of steps, so nodes can predict the verification cost and an attacker cannot make a script run forever. The cost is expressiveness. Applications that need shared, changing state or complex repeated logic are awkward to build directly in Bitcoin Script, so they are usually built in layers above the base chain.