OP_2ROT - Rotating Top Three Pairs
11. OP_2ROT — Rotating Top Three Pairs
Overview
OP_2ROT is the pair-level version of OP_ROT. It operates on the top six items of the stack, treating them as three pairs. The bottom pair is moved to the top, while the upper two pairs shift down.
Opcode Reference
Opcode: OP_2ROT
Hex: 0x71
Word: 2ROT
Input: x1 x2 x3 x4 x5 x6
Output: x3 x4 x5 x6 x1 x2
Stack Before: [ ..., x1, x2, x3, x4, x5, x6 ]
Stack After: [ ..., x3, x4, x5, x6, x1, x2 ]
Execution Trace
After pushing 1–6: Stack: [ 1, 2, 3, 4, 5, 6 ]
After OP_2ROT: Stack: [ 3, 4, 5, 6, 1, 2 ]
The bottom pair (1, 2) is moved to the top.
Use Case: Pair-Level Pipeline Processing
In scripts handling multiple (sig, pubkey) pairs:
<sig_A> <pub_A> <sig_B> <pub_B> <sig_C> <pub_C>
OP_2ROT
→ Stack: [ <sig_B>, <pub_B>, <sig_C>, <pub_C>, <sig_A>, <pub_A> ]
Now (sig_A, pub_A) — which was pushed first — is at the top for first processing.
Failure Conditions
- Requires at least 6 items on the stack.
Summary
OP_2ROT is the most stack-depth-intensive of the standard rotation opcodes, requiring six items. It is used for reordering three pairs of values, particularly in multisig pipeline scripts.
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: