Bitcoin Script: The Programming Language Behind Every Transaction
Every Bitcoin transaction isn't just a value transfer — it's a script execution. Bitcoin Script is a simple, stack-based programming language that defines the conditions under which bitcoin can be spent. It's not Turing-complete (by design), but it's expressive enough to define complex spending conditions while remaining safe and predictable to execute on thousands of nodes simultaneously.
Stack-Based Execution
Bitcoin Script is a stack-based language — operations push data onto a stack or pop data off it to perform computations. A transaction is valid if, when the locking script (from the previous output) and the unlocking script (from the spending input) are executed together, the stack ends with a single TRUE value.
- scriptPubKey (locking script): Defines conditions to spend the output; set by the sender
- scriptSig (unlocking script): Provides data that satisfies the locking conditions; provided by the spender
- Witness (SegWit): Post-SegWit signature data stored separately from the transaction
P2PKH Script Execution: Step by Step
ScriptPubKey (Locking):
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
ScriptSig (Unlocking):
<signature> <publicKey>
Stack execution:
1. Push <signature> → Stack: [sig]
2. Push <publicKey> → Stack: [sig, pubKey]
3. OP_DUP → Stack: [sig, pubKey, pubKey]
4. OP_HASH160 → Stack: [sig, pubKey, hash(pubKey)]
5. Push <pubKeyHash> → Stack: [sig, pubKey, hash(pubKey), pubKeyHash]
6. OP_EQUALVERIFY → Hashes match! Stack: [sig, pubKey]
7. OP_CHECKSIG → Signature valid! Stack: [TRUE]
Script Execution: Unlocking a UTXO
Why Not Turing-Complete?
Satoshi deliberately made Bitcoin Script not Turing-complete — it has no loops. This means every script terminates, execution time is bounded, and nodes can safely validate scripts without risk of infinite loops or resource exhaustion. Ethereum chose Turing-completeness (with gas limits to prevent infinite loops artificially) — a different trade-off with different security properties. Bitcoin's approach prioritises safety and predictability.
"Bitcoin Script is simple by design. It handles its use cases perfectly and nothing else. That limitation is its strength." — Bitcoin developer philosophy