TeachMeBitcoin

OP_2ROT - Rotating Top Three Pairs

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

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

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.

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