TeachMeBitcoin

OP_TUCK - Copying Top Item Below Second

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

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

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.

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