OP_2DUP - Duplicating Top Two Items
4. OP_2DUP — Duplicating Top Two Items
Overview
OP_2DUP duplicates the top two items on the stack, preserving their order. After execution, the stack contains the original two items followed by two copies of those items on top. This opcode is useful when a pair of values — such as a signature and public key — need to be used in two separate verification steps.
Opcode Reference
Opcode: OP_2DUP
Hex: 0x6e
Word: 2DUP
Input: x1 x2
Output: x1 x2 x1 x2
Stack Before: [ ..., x1, x2 ] ← x2 is top
Stack After: [ ..., x1, x2, x1, x2 ]
Execution Trace
Script: OP_3 OP_7 OP_2DUP
Step 1 — OP_3: Stack: [ 3 ]
Step 2 — OP_7: Stack: [ 3, 7 ]
Step 3 — OP_2DUP: Stack: [ 3, 7, 3, 7 ]
The pair (3, 7) is duplicated and placed on top in the same order.
Use Case: Dual Verification
OP_2DUP is used when a pair of values must be passed through two different verification routines:
<sig> <pubkey>
OP_2DUP
OP_CHECKSIG ← first check
OP_SWAP OP_HASH160 <expected_hash> OP_EQUALVERIFY ← second check
In this pattern:
-
The pair
<sig> <pubkey>is duplicated. -
One copy is used in
OP_CHECKSIG. -
The other copy is used to verify the public key hash.
Relationship to OP_DUP
OP_DUP → Duplicates 1 item: [ x ] → [ x, x ]
OP_2DUP → Duplicates 2 items: [ x1, x2 ] → [ x1, x2, x1, x2 ]
OP_2DUP is NOT equivalent to executing OP_DUP twice:
OP_DUP twice on [ x1, x2 ]:
After 1st OP_DUP: [ x1, x2, x2 ]
After 2nd OP_DUP: [ x1, x2, x2, x2 ] ← x2 duplicated twice!
OP_2DUP on [ x1, x2 ]:
Result: [ x1, x2, x1, x2 ] ← both items duplicated once
Failure Conditions
- Requires at least 2 items on the stack; fails with underflow otherwise.
Summary
OP_2DUP provides efficient two-item duplication, preserving order and enabling paired-value operations like dual-channel verification in a single script execution pass.
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: