OP_3DUP - Duplicating Top Three Items
5. OP_3DUP — Duplicating Top Three Items
Overview
OP_3DUP extends the duplication family by copying the top three stack items and placing them on top of the existing stack. After execution, the stack contains the original three items plus three new copies above them.
Opcode Reference
Opcode: OP_3DUP
Hex: 0x6f
Word: 3DUP
Input: x1 x2 x3
Output: x1 x2 x3 x1 x2 x3
Stack Before: [ ..., x1, x2, x3 ] ← x3 is top
Stack After: [ ..., x1, x2, x3, x1, x2, x3 ]
Execution Trace
Script: OP_1 OP_2 OP_3 OP_3DUP
Step 1: Stack: [ 1 ]
Step 2: Stack: [ 1, 2 ]
Step 3: Stack: [ 1, 2, 3 ]
Step 4 — OP_3DUP: Stack: [ 1, 2, 3, 1, 2, 3 ]
Use Case in Complex Script Verification
OP_3DUP is useful in elliptic curve arithmetic scripts and in scripts that implement custom cryptographic protocols where three values (like x-coordinate, y-coordinate, and a scalar) must each be used twice.
<x> <y> <z>
OP_3DUP
<verify_set_1_operations>
OP_DROP OP_DROP OP_DROP ← clean up first set result
<verify_set_2_operations> ← use the original x, y, z
Stack Depth Implications
Using OP_3DUP doubles three items, growing the stack by 3. Since Bitcoin enforces a maximum stack depth of 1000 elements, scripts that use OP_3DUP in loops or repeated patterns must carefully account for stack growth.
Max stack depth: 1000 items
OP_3DUP adds: 3 items per call
Failure Conditions
- Requires at least 3 items on the stack.
Summary
OP_3DUP serves specialized use cases where three items must be reused across multiple operations without being consumed. It is less common than OP_DUP or OP_2DUP but essential in complex script templates involving triple-element state.
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: