OP_TOALTSTACK - Moving Items to Alt Stack
12. OP_TOALTSTACK — Moving Items to Alt Stack
Overview
OP_TOALTSTACK moves the top item from the main stack to the alt stack (alternative stack). This is one of two opcodes (along with OP_FROMALTSTACK) that interact with the alt stack. It is used to temporarily store values that are needed later in script execution but would otherwise be overwritten or require complex stack manipulation to preserve.
Opcode Reference
Opcode: OP_TOALTSTACK
Hex: 0x6b
Byte value: 107 (decimal)
Category: Stack Operations / Flow Control
How It Works
Main stack before: [ ..., A, B ] (B is top)
Alt stack before: []
OP_TOALTSTACK:
pop B from main stack
push B onto alt stack
Main stack after: [ ..., A ]
Alt stack after: [ B ]
The value is removed from the main stack. It is now on the alt stack, inaccessible to normal opcodes until OP_FROMALTSTACK retrieves it.
Why the Alt Stack Exists
The main stack in Bitcoin Script has no "random access" — you can only easily work with the top few elements using opcodes like OP_SWAP, OP_OVER, OP_ROT, OP_DUP. For deeper stack manipulation, you typically need complex sequences of these opcodes.
The alt stack provides a solution: temporarily move values you don't need right now out of the way, operate on the remaining main stack items, then retrieve the stored values later.
; Problem: need to apply two operations to the same value,
; but intermediate steps would consume it.
; Without alt stack:
<value>
OP_DUP ; duplicate so we have two copies
OP_SHA256 ; hash one copy
<expected_hash> OP_EQUALVERIFY ; verify hash
; Now use the original copy for the next step
<value> ; oops — need to push it again, or have saved a copy
; With alt stack:
<value>
OP_TOALTSTACK ; save value to alt stack
<do some work>
OP_FROMALTSTACK ; retrieve value
OP_SHA256 ; hash it
<expected_hash> OP_EQUALVERIFY
Preserving Values Across Complex Operations
A very common pattern in multisig scripts and HTLC scripts is to preserve a public key or hash value across an intervening set of opcodes:
; Save remote_pubkey while checking hash
<remote_pubkey>
OP_TOALTSTACK ; save remote_pubkey
OP_SIZE 32 OP_EQUAL
OP_IF
OP_SHA256 <payment_hash> OP_EQUALVERIFY
OP_FROMALTSTACK ; retrieve remote_pubkey
OP_CHECKSIG
OP_ELSE
OP_DROP
<cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP
OP_FROMALTSTACK ; retrieve remote_pubkey
OP_CHECKSIG
OP_ENDIF
Alt Stack Is Per-Script Evaluation
The alt stack is scoped to a single script evaluation. It does not persist across inputs, outputs, or separate script evaluations. At the end of script execution, the alt stack must generally be empty (or at least, any remaining items do not affect validity directly — but they are simply discarded).
OP_TOALTSTACK Cannot Be Used in scriptSig
In P2SH and SegWit contexts, there are constraints on where alt stack operations can appear. For SegWit witness scripts, OP_TOALTSTACK is allowed and commonly used. In legacy scriptSig, its use is more restricted by standardness rules.
; Valid in P2WSH witness script:
<value>
OP_TOALTSTACK
<operations>
OP_FROMALTSTACK
OP_EQUAL
; Standard usage in Lightning Network HTLC scripts
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: