B++ Logo

OP Codes

The instruction set for Bitcoin. Bitcoin Script uses a stack-based programming language with various opcodes (operation codes) that perform different functions. This document provides a complete reference of Bitcoin OP codes with explanations and code examples. Try these opcodes in Stack Lab.

OP Code Categories

CategoryDescriptionExamples
Stack OperationsManipulate the stackOP_DUP, OP_DROP, OP_SWAP
ArithmeticMathematical operationsOP_ADD, OP_SUB
CryptographicHash functions and signaturesOP_SHA256, OP_CHECKSIG
ComparisonTest conditionsOP_EQUAL, OP_LESSTHAN
Control FlowConditional executionOP_IF, OP_ELSE
Time LocksTransaction timingOP_CLTV, OP_CSV
DisabledRemoved for securityOP_MUL, OP_CAT

Script Execution Example


Stack Operations

OP CodeHexFunction
OP_DUP0x76Duplicates top stack item
OP_DROP0x75Removes top stack item
OP_SWAP0x7cSwaps top two items
OP_OVER0x78Copies second-to-top to top
OP_ROT0x7bRotates top three items
OP_PICK0x79Copies nth item to top
OP_ROLL0x7aMoves nth item to top
OP_2DROP0x6dRemoves top two items
OP_2DUP0x6eDuplicates top two items
OP_3DUP0x6fDuplicates top three items
OP_NIP0x77Removes second-to-top item
OP_TUCK0x7dCopies top below second

Examples

OP_DUP:
  Before: [A]
  After:  [A, A]

OP_SWAP:
  Before: [A, B]
  After:  [B, A]

OP_ROT:
  Before: [A, B, C]
  After:  [B, C, A]

Arithmetic Operations

OP CodeHexFunctionStatus
OP_ADD0x93a + bActive
OP_SUB0x94a - bActive
OP_1ADD0x8ba + 1Active
OP_1SUB0x8ca - 1Active
OP_NEGATE0x8f-aActive
OP_ABS0x90abs(a)Active
OP_NOT0x91!a (logical)Active
OP_0NOTEQUAL0x92a != 0Active
OP_MUL0x95a * bDisabled
OP_DIV0x96a / bDisabled
OP_MOD0x97a % bDisabled
OP_LSHIFT0x98a << bDisabled
OP_RSHIFT0x99a >> bDisabled

Note: OP_MUL, OP_DIV, OP_MOD, OP_LSHIFT, and OP_RSHIFT were disabled early in Bitcoin's history to prevent potential denial-of-service attacks through expensive computations.


Comparison Operations

OP CodeHexFunction
OP_EQUAL0x87Returns 1 if equal, else 0
OP_EQUALVERIFY0x88OP_EQUAL + OP_VERIFY
OP_LESSTHAN0x9fa < b
OP_GREATERTHAN0xa0a > b
OP_LESSTHANOREQUAL0xa1a <= b
OP_GREATERTHANOREQUAL0xa2a >= b
OP_MIN0xa3min(a, b)
OP_MAX0xa4max(a, b)
OP_WITHIN0xa5min <= x < max

Cryptographic Operations

OP CodeHexFunction
OP_RIPEMD1600xa6RIPEMD-160 hash
OP_SHA10xa7SHA-1 hash
OP_SHA2560xa8SHA-256 hash
OP_HASH1600xa9SHA256 + RIPEMD160
OP_HASH2560xaaDouble SHA-256
OP_CHECKSIG0xacVerify ECDSA signature
OP_CHECKSIGVERIFY0xadOP_CHECKSIG + OP_VERIFY
OP_CHECKMULTISIG0xaeVerify multisig
OP_CHECKMULTISIGVERIFY0xafOP_CHECKMULTISIG + OP_VERIFY

Bitwise Operations (Disabled)

OP CodeHexFunctionStatus
OP_AND0x84Bitwise ANDDisabled
OP_OR0x85Bitwise ORDisabled
OP_XOR0x86Bitwise XORDisabled
OP_INVERT0x83Bitwise NOTDisabled

These were disabled to prevent potential vulnerabilities in early Bitcoin.


Control Flow

OP CodeHexFunction
OP_IF0x63Execute if top is non-zero
OP_NOTIF0x64Execute if top is zero
OP_ELSE0x67Else branch
OP_ENDIF0x68End conditional
OP_VERIFY0x69Fail if top is false
OP_RETURN0x6aMark output unspendable

Conditional Example

<condition>
OP_IF
    <execute if true>
OP_ELSE
    <execute if false>
OP_ENDIF

Time Lock Operations

OP_CHECKLOCKTIMEVERIFY (CLTV)

Code: 0xb1

Verifies the transaction's nLockTime is at least the specified value (absolute time lock).

<locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP <pubkey> OP_CHECKSIG

Use cases:

  • Payment channels
  • Time-locked savings
  • Inheritance planning

OP_CHECKSEQUENCEVERIFY (CSV)

Code: 0xb2

Verifies the input's sequence number enforces a relative time lock.

<relative_locktime> OP_CHECKSEQUENCEVERIFY OP_DROP <pubkey> OP_CHECKSIG

Use cases:


Push Operations

OP CodeHexFunction
OP_0 / OP_FALSE0x00Push empty array (false)
OP_1NEGATE0x4fPush -1
OP_1 / OP_TRUE0x51Push 1
OP_2 - OP_160x52-0x60Push 2-16
OP_PUSHDATA10x4cNext byte is length
OP_PUSHDATA20x4dNext 2 bytes are length
OP_PUSHDATA40x4eNext 4 bytes are length

Tapscript Opcodes (BIP 342)

Taproot introduced new opcodes for Tapscript:

OP_CHECKSIGADD

Code: 0xba

Enables efficient Schnorr signature aggregation for multisig.

<pubkey1> OP_CHECKSIG
<pubkey2> OP_CHECKSIGADD
<pubkey3> OP_CHECKSIGADD
<threshold> OP_NUMEQUAL

Benefits over OP_CHECKMULTISIG:

  • No dummy element needed
  • More efficient batch validation
  • Works with Schnorr signatures

OP_SUCCESS Opcodes

Codes 0x50, 0x62, 0x89-0x8a, 0x8d-0x8e, 0x95-0x99, 0xbb-0xfe

Reserved for future upgrades. Any script containing these immediately succeeds, allowing soft fork upgrades.


Disabled Opcodes

These opcodes were disabled for security reasons:

OP CodeHexReason
OP_CAT0x7eCould create oversized scripts
OP_SUBSTR0x7fSecurity concerns
OP_LEFT0x80Security concerns
OP_RIGHT0x81Security concerns
OP_INVERT0x83Security concerns
OP_AND0x84Security concerns
OP_OR0x85Security concerns
OP_XOR0x86Security concerns
OP_2MUL0x8dCan be done with OP_ADD
OP_2DIV0x8eSecurity concerns
OP_MUL0x95Expensive computation
OP_DIV0x96Division by zero issues
OP_MOD0x97Division by zero issues
OP_LSHIFT0x98Could create large numbers
OP_RSHIFT0x99Security concerns

OP_CAT Revival: There is ongoing discussion about re-enabling OP_CAT with proper limits, as it would enable new use cases like covenants.


Common Script Patterns

P2PKH (Pay-to-Pubkey-Hash)

Locking Script (scriptPubKey):
OP_DUP OP_HASH160 <pubkeyhash> OP_EQUALVERIFY OP_CHECKSIG

Unlocking Script (scriptSig):
<signature> <publickey>

P2SH (Pay-to-Script-Hash)

Locking Script:
OP_HASH160 <scripthash> OP_EQUAL

Unlocking Script:
<data> ... <redeemscript>

Multisig (2-of-3)

Locking Script:
OP_2 <pubkey1> <pubkey2> <pubkey3> OP_3 OP_CHECKMULTISIG

Unlocking Script:
OP_0 <sig1> <sig2>

Note: OP_0 is required due to a bug in OP_CHECKMULTISIG that pops one extra item.

HTLC (Hash Time-Locked Contract)

OP_IF
    OP_SHA256 <hash> OP_EQUALVERIFY
    <receiver_pubkey> OP_CHECKSIG
OP_ELSE
    <timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP
    <sender_pubkey> OP_CHECKSIG
OP_ENDIF

Script Limits

LimitValuePurpose
Max script size10,000 bytesPrevent oversized scripts
Max stack size1,000 itemsPrevent stack overflow
Max item size520 bytesPrevent oversized items
Max ops per script201Prevent expensive scripts
Max multisig keys20Limit signature checks
Max Tapscript sizeNo limit*Weight-based limits apply

*Tapscript has no explicit size limit but is constrained by block weight.


OP Code Quick Reference

HexNameDescriptionStatus
0x00OP_0Push empty arrayActive
0x4fOP_1NEGATEPush -1Active
0x51-0x60OP_1-OP_16Push 1-16Active
0x63OP_IFConditionalActive
0x64OP_NOTIFConditionalActive
0x67OP_ELSEConditionalActive
0x68OP_ENDIFConditionalActive
0x69OP_VERIFYVerifyActive
0x6aOP_RETURNData carrierActive
0x75OP_DROPRemove topActive
0x76OP_DUPDuplicate topActive
0x77OP_NIPRemove secondActive
0x78OP_OVERCopy secondActive
0x79OP_PICKCopy nthActive
0x7aOP_ROLLMove nthActive
0x7bOP_ROTRotate 3Active
0x7cOP_SWAPSwap 2Active
0x7dOP_TUCKTuck topActive
0x7eOP_CATConcatenateDisabled
0x84OP_ANDBitwise ANDDisabled
0x85OP_ORBitwise ORDisabled
0x86OP_XORBitwise XORDisabled
0x87OP_EQUALEqualityActive
0x88OP_EQUALVERIFYEqual + verifyActive
0x93OP_ADDAdditionActive
0x94OP_SUBSubtractionActive
0x95OP_MULMultiplicationDisabled
0x96OP_DIVDivisionDisabled
0x97OP_MODModuloDisabled
0xa6OP_RIPEMD160RIPEMD160Active
0xa7OP_SHA1SHA1Active
0xa8OP_SHA256SHA256Active
0xa9OP_HASH160SHA256+RIPEMD160Active
0xaaOP_HASH256Double SHA256Active
0xacOP_CHECKSIGCheck signatureActive
0xaeOP_CHECKMULTISIGCheck multisigActive
0xb1OP_CLTVAbsolute timelockActive
0xb2OP_CSVRelative timelockActive
0xbaOP_CHECKSIGADDTapscript multisigActive


Resources