Bitcoin Script Language
Bitcoin Script: A Stack-Based Assembly Language
Many people believe that Bitcoin has no "smart contracts." This is a misconception. Bitcoin was launched with a native, highly secure programming language called Script.
Script is a low-level, stack-based, assembly-like language designed specifically to write smart cryptographic conditions on money.
π₯ The Stack-Based LIFO Architecture
Bitcoin's virtual machine uses a data structure called a Stack to execute instructions. A stack is like a vertical stack of dinner plates in a cafeteria:
- Push: You can only place a new plate on the top of the stack.
- Pop: You can only remove a plate from the top of the stack.
- LIFO (Last In, First Out): The last plate placed on the stack is always the first one to be taken off.
[ PUSH OPERATION ] [ POP OPERATION ]
ββββββββββββββββββββββββ ββββββββββββββββββββββββ
β Pushing New Item β β Popping Top Item β
ββββββββββββ¬ββββββββββββ ββββββββββββ²ββββββββββββ
β β
βΌ β
βββββββββββββββ ββββββββ΄βββββββ
β Item C β (Top of Stack) β Item C β (Popped)
βββββββββββββββ€ βββββββββββββββ€
β Item B β β Item B β (New Top)
βββββββββββββββ€ βββββββββββββββ€
β Item A β (Bottom of Stack) β Item A β
βββββββββββββββ βββββββββββββββ
All operations in Bitcoin Script read data from, manipulate, or push data onto this stack.
π« Why Bitcoin Script Is NOT Turing-Complete
A programming language is "Turing-complete" if it can run any algorithm or calculation, including infinite loops. Ethereum's EVM is Turing-complete. Bitcoinβs Script is intentionally NOT Turing-complete.
1. No Loops or Jumps
Bitcoin Script has no loop instructions (for, while) and no recursive jump operations. Execution starts at the first opcode, moves strictly left-to-right, and terminates.
2. The Infinite Loop DoS Vector
If Bitcoin supported loops, a malicious actor could broadcast a transaction containing an infinite loop:
while(true) {
// Infinite computation
}
Every validating full node on the network would receive this transaction, attempt to validate it, and get permanently stuck in the loop. The global network would freeze instantly.
3. Gas Fees vs. Deterministic Validation
- Ethereum's Solution: To support loops, Ethereum must use Gas fees. Every step of computation costs Gas. If a contract runs out of Gas, it is aborted, preventing infinite loops. This adds massive complexity to transactions.
- Bitcoin's Solution: By removing loops, Bitcoin ensures that the execution time of any script is strictly linear:
$$\text{Validation Time} = O(N)$$
where $N$ is the number of bytes in the transaction. A node can never get stuck. Validation time is completely predictable, making complex Gas fees completely unnecessary!
ποΈ Key Opcode Classifications
Bitcoin Script consists of approximately 115 active Operation Codes (Opcodes), which can be categorized into five major classes:
1. Constants (Opcodes 0x00 to 0x4f)
Instructions that push raw data (such as numbers or public keys) onto the stack. For example, OP_1 pushes the number 1, and OP_0 pushes an empty byte string.
2. Stack Manipulation
Opcodes that rearrange or clean up the stack:
* OP_DUP: Duplicates the top stack item.
* OP_DROP: Discards the top stack item.
* OP_SWAP: Swaps the position of the top two stack items.
3. Cryptography
High-level cryptographic operations run natively in the virtual machine:
* OP_SHA256: Hashes the top item using SHA-256.
* OP_HASH160: Hashes the top item using SHA-256 followed by RIPEMD-160.
* OP_CHECKSIG: Verifies an ECDSA signature against a public key.
4. Arithmetic
Basic math operations that operate on stack integers:
* OP_ADD: Adds the top two items.
* OP_SUB: Subtracts the top item from the second item.
5. Flow Control
Conditional logic structures:
* OP_IF / OP_ELSE / OP_ENDIF: Executes code branches conditionally. If the top item on the stack is TRUE, execute the OP_IF branch; otherwise, skip to OP_ELSE.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: