BSV blockchainBSV blockchain

Script opcodes

Every Script opcodeA one byte instruction in a locking or unlocking script, such as OP_DUP. Scripts run on a stack. in the protocol documentation has a hex byte value and a fixed effect on the stack. Look one up here by name to see its value and what it does. The Opcodes page of the BSV blockchain protocol documentation gives the stack input and output of each opcode, its limits, such as number size by era, and its error names. To learn how scripts run on the stack, read How Script works.

Some opcodes behave differently by protocol era. The eras are Pre-Genesis, Genesis and Chronicle. The node applies the era in which the output being spent was confirmed, and the protocol documentation lists when each era started.

Push values

A byte from 0x01 to 0x4b has no name and pushes the next 1 to 75 bytes as one item. These are the named opcodes that put a value on the stack:

OpcodeHexWhat it does
OP_0, OP_FALSE0x00Pushes an empty byte array. An item is added to the stack.
OP_PUSHDATA10x4cThe next 1 byte is a length, then that many bytes are pushed.
OP_PUSHDATA20x4dThe next 2 bytes, little endian, are a length, then that many bytes are pushed.
OP_PUSHDATA40x4eThe next 4 bytes, little endian, are a length, then that many bytes are pushed.
OP_1NEGATE0x4fPushes the number -1.
OP_1, OP_TRUE0x51Pushes the number 1.
OP_2 to OP_160x52 to 0x60Pushes the number in the name.

Flow control

OP_IF and OP_NOTIF choose which part of a script runs, and OP_RETURN ends it, or fails it before Genesis. A conditional must be closed with OP_ENDIF:

OpcodeHexWhat it does
OP_NOP0x61Does nothing.
OP_VER0x62Pushes the version of the spending transaction. Invalid when executed before Chronicle.
OP_IF0x63Runs the next branch if the top value is true, and removes the value. Closed by OP_ENDIF.
OP_NOTIF0x64Runs the next branch if the top value is false, and removes the value. Closed by OP_ENDIF.
OP_VERIF0x65Like OP_IF, but the condition is that the top item equals the transaction version. Invalid when executed before Chronicle.
OP_VERNOTIF0x66The negation of OP_VERIF. Invalid when executed before Chronicle.
OP_ELSE0x67Runs if the matching OP_IF or OP_NOTIF branch did not.
OP_ENDIF0x68Ends a conditional block. An unbalanced conditional makes the script invalid.
OP_VERIFY0x69Fails the script if the top value is not true, otherwise removes it.
OP_RETURN0x6aBefore Genesis it fails the script. From Genesis it ends evaluation without an error and the rest of the script is ignored, but the spend is valid only if the top stack item is true.

A common convention for carrying data in an output is the prefix OP_FALSE OP_RETURN, followed by the data. The OP_FALSE leaves a false item on the stack, so the output can never be spent.

Stack

These opcodes move items between the main stack and the alt stack, copy them, or remove them. The top item is the last one pushed:

OpcodeHexWhat it does
OP_TOALTSTACK0x6bMoves the top item to the alt stack.
OP_FROMALTSTACK0x6cMoves the top item of the alt stack to the main stack.
OP_2DROP0x6dRemoves the top two items.
OP_2DUP0x6eDuplicates the top two items.
OP_3DUP0x6fDuplicates the top three items.
OP_2OVER0x70Copies the second pair from the top to the top.
OP_2ROT0x71Moves the fifth and sixth items to the top.
OP_2SWAP0x72Swaps the top two pairs.
OP_IFDUP0x73Duplicates the top item if it is not zero.
OP_DEPTH0x74Pushes the number of items on the stack.
OP_DROP0x75Removes the top item.
OP_DUP0x76Duplicates the top item.
OP_NIP0x77Removes the second item from the top.
OP_OVER0x78Copies the second item from the top to the top.
OP_PICK0x79Takes n from the top, then copies the item n places down to the top. Fails if n is negative or not less than the stack depth.
OP_ROLL0x7aTakes n from the top, then moves the item n places down to the top. Fails if n is negative or not less than the stack depth.
OP_ROT0x7bRotates the top three items, so the third becomes the top.
OP_SWAP0x7cSwaps the top two items.
OP_TUCK0x7dCopies the top item to behind the second item.

Data manipulation

Join, split and measure byte arrays, and convert between byte arrays and numbers:

OpcodeHexWhat it does
OP_CAT0x7eJoins two byte arrays. Before Genesis the result must be at most 520 bytes.
OP_SPLIT0x7fSplits a byte array at position n into two items. Fails if n is negative or longer than the array.
OP_NUM2BIN0x80Encodes a number as a byte array of a given length. Fails if the number does not fit. Before Genesis the length must be at most 520 bytes.
OP_BIN2NUM0x81Re-encodes a byte array as a minimal number.
OP_SIZE0x82Pushes the byte length of the top item, and leaves the item in place.

Bitwise logic

Bitwise opcodes work on byte arrays bit by bit, and OP_EQUAL compares two items:

OpcodeHexWhat it does
OP_INVERT0x83Flips every bit of the top item.
OP_AND0x84Bitwise AND of two items of equal length.
OP_OR0x85Bitwise OR of two items of equal length.
OP_XOR0x86Bitwise XOR of two items of equal length.
OP_EQUAL0x87Pushes 1 if the two top items are byte for byte equal, otherwise 0.
OP_EQUALVERIFY0x88OP_EQUAL, then OP_VERIFY.

Arithmetic

Numbers on the stack are little endian with the sign in the top bit of the last byte. OP_LSHIFT and OP_RSHIFT shift a byte array by bits. OP_LSHIFTNUM and OP_RSHIFTNUM shift a number. In the rows below, b is the top item and a is the item under it:

OpcodeHexWhat it does
OP_1ADD0x8bAdds 1.
OP_1SUB0x8cSubtracts 1.
OP_2MUL0x8dMultiplies by 2. Disabled before Chronicle.
OP_2DIV0x8eDivides by 2, rounding toward zero. Disabled before Chronicle.
OP_NEGATE0x8fFlips the sign.
OP_ABS0x90Absolute value.
OP_NOT0x910 becomes 1, anything else becomes 0.
OP_0NOTEQUAL0x920 stays 0, anything else becomes 1.
OP_ADD0x93a plus b.
OP_SUB0x94a minus b.
OP_MUL0x95a times b.
OP_DIV0x96a divided by b. Fails if b is 0.
OP_MOD0x97The remainder of a divided by b. Fails if b is 0.
OP_LSHIFT0x98Shifts a byte array left by b bits. Fails if b is negative.
OP_RSHIFT0x99Shifts a byte array right by b bits. Fails if b is negative.
OP_BOOLAND0x9a1 if a and b are both not zero, otherwise 0.
OP_BOOLOR0x9b1 if a or b is not zero, otherwise 0.
OP_NUMEQUAL0x9c1 if a equals b as numbers, otherwise 0.
OP_NUMEQUALVERIFY0x9dOP_NUMEQUAL, then OP_VERIFY.
OP_NUMNOTEQUAL0x9e1 if a and b differ as numbers, otherwise 0.
OP_LESSTHAN0x9f1 if a is less than b.
OP_GREATERTHAN0xa01 if a is greater than b.
OP_LESSTHANOREQUAL0xa11 if a is less than or equal to b.
OP_GREATERTHANOREQUAL0xa21 if a is greater than or equal to b.
OP_MIN0xa3The smaller of a and b.
OP_MAX0xa4The larger of a and b.
OP_WITHIN0xa51 if x is at least min and less than max. The three are the top items, with max on top.
OP_LSHIFTNUM0xb6Shifts a number left by b bits and keeps its sign. Chronicle only, a no-op before. It took the slot of OP_NOP7.
OP_RSHIFTNUM0xb7Shifts a number right by b bits and keeps its sign. Chronicle only, a no-op before. It took the slot of OP_NOP8.

Strings

Three opcodes took the slots of the former OP_NOP4 to OP_NOP6. Before Chronicle they do nothing. From Chronicle they work on byte arrays:

OpcodeHexWhat it does
OP_SUBSTR0xb3Pushes length bytes of a byte array, starting at a byte offset. Fails if the range is outside the array.
OP_LEFT0xb4Pushes the leftmost n bytes. Fails if n is negative or longer than the array.
OP_RIGHT0xb5Pushes the rightmost n bytes. Fails if n is negative or longer than the array.

Cryptography

Hash the top item, check a signature against a public key, or limit which part of the script a signature covers:

OpcodeHexWhat it does
OP_RIPEMD1600xa6RIPEMD-160 hash of the top item.
OP_SHA10xa7SHA-1 hash of the top item.
OP_SHA2560xa8SHA-256 hash of the top item.
OP_HASH1600xa9RIPEMD-160 of the SHA-256 of the top item.
OP_HASH2560xaaSHA-256 applied twice to the top item.
OP_CODESEPARATOR0xabA signature check after it covers only the script that follows the most recent OP_CODESEPARATOR.
OP_CHECKSIG0xacChecks a signature against a public key over the signature hash of the transaction. Pushes 1 or 0.
OP_CHECKSIGVERIFY0xadOP_CHECKSIG, then OP_VERIFY.
OP_CHECKMULTISIG0xaeChecks m signatures against n public keys, in order. It takes one extra item from the stack. Pushes 1 or 0.
OP_CHECKMULTISIGVERIFY0xafOP_CHECKMULTISIG, then OP_VERIFY.

Locktime

Two opcodes took former no-op slots. They enforce lock times only before Genesis, and only when the matching verify flag is set. From Genesis they do nothing:

OpcodeHexWhat it does
OP_CHECKLOCKTIMEVERIFY, OP_NOP20xb1Before Genesis, fails the script unless the transaction lock time has passed the top value.
OP_CHECKSEQUENCEVERIFY, OP_NOP30xb2Before Genesis, fails the script unless the relative lock time of the input is met.

Reserved, no-op and invalid

Executing a reserved opcode makes a script invalid. The no-op opcodes are ignored, and the template words are used internally to match transaction templates, and are invalid in a real script:

OpcodeHexWhat it does
OP_RESERVED0x50Invalid when executed.
OP_RESERVED10x89Invalid when executed.
OP_RESERVED20x8aInvalid when executed.
OP_NOP10xb0Ignored. Fails if the DISCOURAGE_UPGRADABLE_NOPS flag is set.
OP_NOP9, OP_NOP100xb8 to 0xb9Ignored. Fails if the DISCOURAGE_UPGRADABLE_NOPS flag is set.
OP_SMALLINTEGER0xfaMatches a small integer push in a template.
OP_PUBKEYS0xfbMatches a sequence of public keys in a template.
OP_PUBKEYHASH0xfdMatches a public key hashed with OP_HASH160.
OP_PUBKEY0xfeMatches a public key usable by OP_CHECKSIG.
OP_INVALIDOPCODE0xffStands for any opcode not yet assigned.

Use the names in code

@bsv/sdk exports OP, which maps opcode names to byte values in decimal. This page was written against @bsv/sdk 4.0.1: every name and hex value on this page matches its OP, and the SDK also defines names this page does not list, such as OP_NOP11.

Install the SDK with npm install @bsv/sdk@4.0.1, save the snippet as op.mjs and run node op.mjs. It prints 118, the decimal value of 0x76.

import { OP } from '@bsv/sdk'

console.log(OP.OP_DUP)

To learn more about Script on the BSV blockchain, review these articles: