OP_FROMALTSTACK - Retrieving from Alt Stack
13. OP_FROMALTSTACK — Retrieving from Alt Stack
Overview
OP_FROMALTSTACK is the counterpart to OP_TOALTSTACK. It pops the top item from the alt stack and pushes it onto the main stack. It is always used in conjunction with a preceding OP_TOALTSTACK — using OP_FROMALTSTACK when the alt stack is empty causes script failure.
Opcode Reference
Opcode: OP_FROMALTSTACK
Hex: 0x6c
Byte value: 108 (decimal)
Category: Stack Operations / Flow Control
How It Works
Main stack before: [ ..., A ]
Alt stack before: [ B ] (B was previously stored with OP_TOALTSTACK)
OP_FROMALTSTACK:
pop B from alt stack
push B onto main stack
Main stack after: [ ..., A, B ] (B is now on top)
Alt stack after: []
Failure Case: Empty Alt Stack
If OP_FROMALTSTACK is called when the alt stack is empty, the script fails immediately:
; Script failure — no items on alt stack
OP_FROMALTSTACK ; ERROR: alt stack is empty
; SCRIPT_ERR_INVALID_ALTSTACK_OPERATION
Script authors must ensure every OP_FROMALTSTACK is matched with a prior OP_TOALTSTACK that has not already been consumed.
Counting Pushes and Pops
The alt stack operates LIFO (last in, first out), just like the main stack. If multiple values are stored:
Main stack: [ C, B, A ] (A on top)
OP_TOALTSTACK ; move A to alt stack
Main: [ C, B ], Alt: [ A ]
OP_TOALTSTACK ; move B to alt stack
Main: [ C ], Alt: [ A, B ] (B on top)
OP_FROMALTSTACK ; retrieve B (top of alt stack)
Main: [ C, B ], Alt: [ A ]
OP_FROMALTSTACK ; retrieve A (top of alt stack)
Main: [ C, B, A ], Alt: []
Note the LIFO order: the last item pushed to the alt stack is the first retrieved.
Practical Example: Preserving Signatures Across Hash Checks
; Script: Check two different conditions on the same key
<pubkey>
OP_TOALTSTACK ; save pubkey
<data_to_hash>
OP_SHA256
<expected_hash>
OP_EQUALVERIFY ; verify hash
OP_FROMALTSTACK ; retrieve pubkey
<signature>
OP_SWAP ; bring pubkey to top: stack is [sig, pubkey]
OP_CHECKSIG ; check signature against pubkey
OP_FROMALTSTACK in Lightning Network Scripts
The BOLT #3 HTLC scripts make extensive use of OP_TOALTSTACK/OP_FROMALTSTACK to preserve the remote pubkey while size and hash checks are performed:
; From BOLT #3 offered HTLC:
<remote_htlcpubkey>
OP_SWAP
OP_SIZE 32 OP_EQUAL
OP_NOTIF
OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
OP_ELSE
OP_SHA256 <payment_hash> OP_EQUALVERIFY
OP_CHECKSIG
OP_ENDIF
In more complex versions, OP_TOALTSTACK is used to stash the pubkey before OP_SIZE operations:
<remote_htlcpubkey>
OP_TOALTSTACK ; stash pubkey
OP_SIZE 32 OP_EQUAL
OP_IF
OP_SHA256 <payment_hash> OP_EQUALVERIFY
OP_FROMALTSTACK ; retrieve pubkey
OP_CHECKSIG
OP_ELSE
OP_DROP
<cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP
OP_FROMALTSTACK ; retrieve pubkey
OP_CHECKSIG
OP_ENDIF
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: