OP_TUCK - Copying Top Item Below Second
15. OP_TUCK — Copying Top Item Below Second
Overview
OP_TUCK is the conceptual inverse of OP_OVER in a certain sense. It copies the top item and inserts that copy below the second item. After execution, the copied item appears both at the top and also beneath the original second item.
Opcode Reference
Opcode: OP_TUCK
Hex: 0x7d
Word: TUCK
Input: x1 x2
Output: x2 x1 x2
Stack Before: [ ..., x1, x2 ]
Stack After: [ ..., x2, x1, x2 ] ← x2 copied and inserted below x1
Execution Trace
Script: OP_3 OP_8 OP_TUCK
Step 1: Stack: [ 3 ]
Step 2: Stack: [ 3, 8 ]
Step 3 — OP_TUCK: Stack: [ 8, 3, 8 ]
Note: 8 (the former top) now appears at both the new bottom and the new top of the expanded triplet. 3 is sandwiched between them.
OP_TUCK as a Compound Operation
OP_TUCK is equivalent to:
OP_SWAP OP_OVER
Proof:
Start: [ ..., x1, x2 ]
OP_SWAP: [ ..., x2, x1 ]
OP_OVER: [ ..., x2, x1, x2 ]
OP_TUCK saves one byte compared to OP_SWAP OP_OVER.
Use Case: Pre-positioning a Value
OP_TUCK is used when the top value needs to be both used immediately and saved for later use in a position below the current second item:
<pubkey_hash> <pubkey>
OP_TUCK ← saves pubkey below pubkey_hash
OP_HASH160 ← hashes the top pubkey
OP_EQUALVERIFY ← checks against pubkey_hash
OP_CHECKSIG ← checks sig against saved pubkey (remaining on stack)
Wait — this doesn't include a <sig> in this simplified form, but it demonstrates the positional intent of OP_TUCK.
Failure Conditions
- Requires at least 2 items on the stack.
Summary
OP_TUCK creates a "tucked" copy of the top item beneath the second item, producing a three-item result from a two-item input. It is the compact form of OP_SWAP OP_OVER.
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: