TeachMeBitcoin

Bitcoin Script Language

From TeachMeBitcoin, the free encyclopedia ⏱️ 4 min read

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 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

$$\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.

β˜• Help support TeachMeBitcoin

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:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!