Skip to main content

Opcodes

A detailed introduction to Bitcoin opcodes.

Bitcoin uses a script system for transactions. Similar to the Forth language, Bitcoin's script system is simple, stack-based, and processed from left to right.

Scripts are essentially a series of instructions recorded in each transaction, describing how the next person wanting to spend the transferred Bitcoin can gain access. A typical script that transfers Bitcoin to a target Bitcoin address restricts future spending with two conditions: the spender must provide:

  1. A public key that, when hashed, matches the target embedded in the script.
  2. A signature proving ownership of the private key corresponding to the provided public key.

MVC has improved and expanded Bitcoin's script system to make it more powerful and flexible.

List of Opcodes

For a list of opcodes, refer to: https://en.bitcoin.it/wiki/Script

Constants

WordOpcodeHexInputOutputDescription
OP_0, OP_FALSE00x00Nothing(empty value)An empty array of bytes is pushed onto the stack. (This is not a no-op: an item is added to the stack.)
N/A1-750x01-0x4b(special)dataThe next opcode bytes is data to be pushed onto the stack
OP_PUSHDATA1760x4c(special)dataThe next byte contains the number of bytes to be pushed onto the stack.
OP_PUSHDATA2770x4d(special)dataThe next two bytes contain the number of bytes to be pushed onto the stack in little endian order.
OP_PUSHDATA4780x4e(special)dataThe next four bytes contain the number of bytes to be pushed onto the stack in little endian order.
OP_1NEGATE790x4fNothing-1The number -1 is pushed onto the stack.
OP_1, OP_TRUE810x51Nothing1The number 1 is pushed onto the stack.
OP_2-OP_1682-960x52-0x60Nothing2-16The number in the word name (2-16) is pushed onto the stack.

Flow Control

WordOpcodeHexInputOutputDescription
OP_NOP970x61NothingNothingDoes nothing.
OP_IF990x63<expression>if [statements] [else [statements]]* endifIf the top stack value is not False, the statements are executed. The top stack value is removed.
OP_NOTIF1000x64<expression>notif [statements] [else [statements]]* endifIf the top stack value is False, the statements are executed. The top stack value is removed.
OP_ELSE1030x67<expression>if [statements] [else [statements]]* endifIf the preceding OP_IF or OP_NOTIF or OP_ELSE was not executed then these statements are, and if the preceding OP_IF or OP_NOTIF or OP_ELSE was executed then these statements are not.
OP_ENDIF1040x68<expression>if [statements] [else [statements]]* endifEnds an if/else block. All blocks must end, or the transaction is invalid. An OP_ENDIF without OP_IF earlier is also invalid.
OP_VERIFY1050x69True / falseNothing / failMarks transaction as invalid if top stack value is not true. The top stack value is removed.
OP_RETURN1060x6aNothingfailMarks transaction as invalid. Since Bitcoin 0.9, a standard way of attaching extra data to transactions is to add a zero-value output with a scriptPubKey consisting of OP_RETURN followed by data. Such outputs are provably unspendable and specially discarded from storage in the UTXO set, reducing their cost to the network. Since 0.12, standard relay rules allow a single output with OP_RETURN, that contains any sequence of push statements (or OP_RESERVED) after the OP_RETURN provided the total scriptPubKey length is at most 83 bytes.

Stack Operations

WordOpcodeHexInputOutputDescription
OP_TOALTSTACK1070x6bx1(alt)x1Puts the input onto the top of the alt stack. Removes it from the main stack.
OP_FROMALTSTACK1080x6c(alt)x1x1Puts the input onto the top of the main stack. Removes it from the alt stack.
OP_IFDUP1150x73xx / x xIf the top stack value is not 0, duplicate it.
OP_DEPTH1160x74Nothing<Stack size>Puts the number of stack items onto the stack.
OP_DROP1170x75xNothingRemoves the top stack item.
OP_DUP1180x76xx xDuplicates the top stack item.
OP_NIP1190x77x1 x2x2Removes the second-to-top stack item.
OP_OVER1200x78x1 x2x1 x2 x1Copies the second-to-top stack item to the top.
OP_PICK1210x79xn ... x2 x1 x0 <n>xn ... x2 x1 x0 xnThe item n back in the stack is copied to the top.
OP_ROLL1220x7axn ... x2 x1 x0 <n>... x2 x1 x0 xnThe item n back in the stack is moved to the top.
OP_ROT1230x7bx1 x2 x3x2 x3 x1The 3rd item down the stack is moved to the top.
OP_SWAP1240x7cx1 x2x2 x1The top two items on the stack are swapped.
OP_TUCK1250x7dx1 x2x2 x1 x2The item at the top of the stack is copied and inserted before the second-to-top item.
OP_2DROP1090x6dx1 x2NothingRemoves the top two stack items.
OP_2DUP1100x6ex1 x2x1 x2 x1 x2Duplicates the top two stack items.
OP_3DUP1110x6fx1 x2 x3x1 x2 x3 x1 x2 x3Duplicates the top three stack items.
OP_2OVER1120x70x1 x2 x3 x4x1 x2 x3 x4 x1 x2Copies the pair of items two spaces back in the stack to the front.
OP_2ROT1130x71x1 x2 x3 x4 x5 x6x3 x4 x5 x6 x1 x2The fifth and sixth items back are moved to the top of the stack.
OP_2SWAP1140x72x1 x2 x3 x4x3 x4 x1 x2Swaps the top two pairs of items.

String Operations

If any opcode marked as disabled is present in the script, the script must abort and fail.

| Word | Opcode | Hex | Input | Output

| Description | |-----------|--------|------|---------------|---------|----------------------------------------| | OP_CAT | 126 | 0x7e | x1 x2 | out | Concatenates two strings. Disabled. | | OP_SUBSTR | 127 | 0x7f | in begin size | out | Returns a section of a string. Disabled. | | OP_LEFT | 128 | 0x80 | in size | out | Keeps only characters left of the specified point in the string. Disabled. | | OP_RIGHT | 129 | 0x81 | in size | out | Keeps only characters right of the specified point in the string. Disabled. | | OP_SIZE | 130 | 0x82 | in | in size | Pushes the string length of the top stack item (without popping it). |

Bitwise Logic Operations

If any opcode marked as disabled is present in the script, the script must abort and fail.

WordOpcodeHexInputOutputDescription
OP_INVERT1310x83inoutFlips all the bits in the input. Disabled.
OP_AND1320x84x1 x2outBoolean AND between each bit of the inputs. Disabled.
OP_OR1330x85x1 x2outBoolean OR between each bit of the inputs. Disabled.
OP_XOR1340x86x1 x2outBoolean XOR between each bit of the inputs. Disabled.
OP_EQUAL1350x87x1 x2True / falseReturns 1 if the inputs are exactly equal, 0 otherwise.
OP_EQUALVERIFY1360x88x1 x2Nothing / failSame as OP_EQUAL, but runs OP_VERIFY afterward.

Arithmetic Operations

WordOpcodeHexInputOutputDescription
OP_1ADD1390x8binout1 is added to the input.
OP_1SUB1400x8cinout1 is subtracted from the input.
OP_2MUL1410x8dinoutThe input is multiplied by 2. Disabled.
OP_2DIV1420x8einoutThe input is divided by 2. Disabled.
OP_NEGATE1430x8finoutThe sign of the input is flipped.
OP_ABS1440x90inoutThe input is made positive.
OP_NOT1450x91inoutIf the input is 0 or 1, it is flipped. Otherwise, the output will be 0.
OP_0NOTEQUAL1460x92inoutReturns 0 if the input is 0. 1 otherwise.
OP_ADD1470x93a bouta is added to b.
OP_SUB1480x94a boutb is subtracted from a.
OP_MUL1490x95a bouta is multiplied by b. Disabled.
OP_DIV150a boutouta is divided by b. Disabled.
OP_MOD1510x97a boutReturns the remainder after dividing a by b. Disabled.
OP_LSHIFT1520x98a boutShifts a left b bits, preserving sign. Disabled.
OP_RSHIFT1530x99a boutShifts a right b bits, preserving sign. Disabled.
OP_BOOLAND1540x9aa boutIf both a and b are not 0, the output is 1. Otherwise 0.
OP_BOOLOR1550x9ba boutIf a or b is not 0, the output is 1. Otherwise 0.
OP_NUMEQUAL1560x9ca boutReturns 1 if the numbers are equal, 0 otherwise.
OP_NUMEQUALVERIFY1570x9da bNothing / failSame as OP_NUMEQUAL, but runs OP_VERIFY afterward.
OP_NUMNOTEQUAL1580x9ea boutReturns 1 if the numbers are not equal, 0 otherwise.
OP_LESSTHAN1590x9fa boutReturns 1 if a is less than b, 0 otherwise.
OP_GREATERTHAN1600xa0a boutReturns 1 if a is greater than b, 0 otherwise.
OP_LESSTHANOREQUAL1610xa1a boutReturns 1 if a is less than or equal to b, 0 otherwise.
OP_GREATERTHANOREQUAL1620xa2a boutReturns 1 if a is greater than or equal to b, 0 otherwise.
OP_MIN1630xa3a boutReturns the smaller of a and b.
OP_MAX1640xa4a boutReturns the larger of a and b.
OP_WITHIN1650xa5x min maxoutReturns 1 if x is within the specified range (left-inclusive), 0 otherwise.

Cryptographic Operations

WordOpcodeHexInputOutputDescription
OP_RIPEMD1601660xa6inhashThe input is hashed using RIPEMD-160.
OP_SHA11670xa7inhashThe input is hashed using SHA-1.
OP_SHA2561680xa8inhashThe input is hashed using SHA-256.
OP_HASH1601690xa9inhashThe input is hashed twice: first with SHA-256 and then with RIPEMD-160.
OP_HASH2561700xaainhashThe input is hashed two times with SHA-256.
OP_CODESEPARATOR1710xabNoneNoneAll of the signature checking words will only match signatures to the data after the most recently-executed OP_CODESEPARATOR.
OP_CHECKSIG1720xacsig pubkeyTrue / falseThe entire transaction's outputs, inputs, and script are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.
OP_CHECKSIGVERIFY1730xadsig pubkeyNothing / failSame as OP_CHECKSIG, but OP_VERIFY is executed afterward.
OP_CHECKMULTISIG1740xaex sig1 sig2 ...<number of signatures> pub1 pub2 ... <number of public keys>True / False