What is the Alt Stack and Why It Exists
14. What is the Alt Stack and Why It Exists
Overview
The alt stack (alternative stack) is a secondary stack provided by the Bitcoin Script virtual machine. It exists alongside the main stack and serves as a temporary holding area for values that need to be preserved across operations that would otherwise displace or consume them.
Architecture of Bitcoin Script's VM
The Bitcoin Script virtual machine (VM) is a simple, stack-based interpreter. At its most fundamental level, it has:
Bitcoin Script VM State:
┌─────────────────────────────┐
│ Script (bytecode) │ ← Being executed
├─────────────────────────────┤
│ Program Counter (pc) │ ← Current position
├─────────────────────────────┤
│ Main Stack │ ← Primary computation
│ [ bottom ... top ] │
├─────────────────────────────┤
│ Alt Stack │ ← Temporary storage
│ [ bottom ... top ] │
├─────────────────────────────┤
│ vfExec (condition stack) │ ← Tracks OP_IF state
│ [ true, false, ... ] │
└─────────────────────────────┘
Why the Alt Stack Was Introduced
Bitcoin Script is deliberately limited. It has no general-purpose memory, no named variables, no registers. All computation happens on the main stack. This creates a practical problem: the main stack is a LIFO structure, but many scripts need to work with multiple values at different depths simultaneously.
Consider the challenge without an alt stack:
; Trying to verify both a hash AND a signature using the same pubkey:
; Stack: [ <sig>, <pubkey>, <data> ]
; Step 1: Hash the data
OP_SHA256 ; [ <sig>, <pubkey>, <hash> ]
<expected> ; [ <sig>, <pubkey>, <hash>, <expected> ]
OP_EQUALVERIFY ; [ <sig>, <pubkey> ]
; Step 2: Now check signature — stack is correct!
OP_CHECKSIG ; fine in this case
; But what if there's more data between steps, or the order is different?
; Stack manipulation becomes a maze of OP_SWAP, OP_OVER, OP_ROT, OP_PICK...
The alt stack solves this by providing a "scratch pad" that is separate from the main computation space:
; Clean solution using alt stack:
; Stack: [ <sig>, <pubkey>, <data> ]
<pubkey>
OP_TOALTSTACK ; remove pubkey, save it: Main: [<sig>, <data>], Alt: [<pubkey>]
<data>
OP_SHA256
<expected>
OP_EQUALVERIFY ; Main: [<sig>], Alt: [<pubkey>]
OP_FROMALTSTACK ; Main: [<sig>, <pubkey>], Alt: []
OP_CHECKSIG
The Alt Stack Is Not Accessible to Most Opcodes
A critical constraint: no opcode other than OP_TOALTSTACK and OP_FROMALTSTACK can directly access the alt stack. You cannot OP_DUP from the alt stack, you cannot OP_SWAP between stacks. The only interaction is explicit move operations.
This constraint is intentional. It keeps the VM simple and auditable. There is no "alt stack indexing" — the alt stack is purely LIFO.
Alt Stack Limits
The alt stack is subject to the same size limits as the main stack:
; Bitcoin Core limits:
MAX_STACK_SIZE = 1000 ; combined main + alt stack items
MAX_SCRIPT_ELEMENT_SIZE = 520 bytes ; per item
; In Tapscript:
MAX_STACK_SIZE = 1000 ; same combined limit
If pushing to either stack would cause the combined count to exceed 1000, the script fails.
Alt Stack in SegWit
In SegWit v0 (P2WPKH and P2WSH), the alt stack behaves identically to its legacy behavior. In Tapscript (SegWit v1, BIP 342), the alt stack is also preserved, and scripts in different Taproot leaves each get a fresh VM state (including fresh, empty alt stack).
Historical Context
The alt stack was present in Bitcoin's original design from the genesis block. Satoshi included it anticipating that scripts might need more flexible temporary storage. While many early scripts never used it, the Lightning Network's HTLC scripts rely on it heavily, and it has become one of the unsung heroes of Bitcoin's script system.
The Alt Stack as a Design Pattern
Experienced Bitcoin script authors treat the alt stack as a way to express clear intent:
; Pattern: "save for later"
OP_TOALTSTACK ; I'm done with this for now, save it
; Pattern: "retrieve when needed"
OP_FROMALTSTACK ; Now I need it again
; Good script hygiene: match every TOALTSTACK with FROMALTSTACK
; and leave the alt stack empty at script completion
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: