Bitcoin Script is a low-level, stack-based programming language used
in the Bitcoin blockchain. It is purposefully simple, not Turing-
complete, and designed for processing transactions securely. Bitcoin
Script operates by combining instructions (opcodes) in scripts to define
conditions under which Bitcoin can be spent.
Here’s an overview:
Key Features
1. Stack-based Execution: Scripts operate on a data stack and
sometimes an additional alt stack.
2. Stateless: Each script runs independently and does not retain state
beyond execution.
3. Non-Turing Complete: This design ensures no infinite loops or
computational risks.
4. Deterministic: Same input always produces the same result.
Script Structure
A Bitcoin transaction has two scripts:
• ScriptPubKey (locking script): Set by the sender, specifies the
conditions to spend funds.
• ScriptSig (unlocking script): Provided by the spender, contains data to
meet the conditions in the ScriptPubKey.
When a transaction is validated:
1. The ScriptSig is run.
2. Its output is combined with the ScriptPubKey.
3. The resulting script is executed. If it ends with TRUE, the transaction is
valid.
Common Script Examples
1. Pay-to-PubKey-Hash (P2PKH)
The most common script type for simple Bitcoin transactions.
ScriptPubKey:
OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG
ScriptSig (provided by the spender):
2. Pay-to-Script-Hash (P2SH)
Enables more complex spending conditions.
ScriptPubKey:
OP_HASH160 OP_EQUAL
ScriptSig:
3. Multi-Signature (M-of-N signatures required):
A multi-signature script requiring M valid signatures from N public
keys.
ScriptPubKey:
OP_ ... OP_ OP_CHECKMULTISIG
ScriptSig:
OP_0 ...
Opcode Categories
• Stack Operations: OP_DUP, OP_DROP, OP_SWAP, etc.
• Arithmetic: OP_ADD, OP_SUB, OP_EQUAL, etc.
• Crypto Operations: OP_HASH160, OP_CHECKSIG, etc.
• Flow Control: OP_IF, OP_ELSE, OP_ENDIF, etc.
Example: P2PKH in Action
1. Sender creates:
2. Receiver spends:
ScriptSig:
Combined script:
OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG
Applications
Bitcoin Script underpins Bitcoin’s functionality, including:
• Standard transactions (P2PKH, P2SH).
• Multi-signature wallets.
• Timelocks (e.g., OP_CHECKLOCKTIMEVERIFY).
Although limited in complexity, Bitcoin Script’s security and
determinism make it essential for trustless financial systems. If you
have a specific use case in mind, let me know!