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:
| Opcode | Hex | What it does |
|---|---|---|
OP_0, OP_FALSE | 0x00 | Pushes an empty byte array. An item is added to the stack. |
OP_PUSHDATA1 | 0x4c | The next 1 byte is a length, then that many bytes are pushed. |
OP_PUSHDATA2 | 0x4d | The next 2 bytes, little endian, are a length, then that many bytes are pushed. |
OP_PUSHDATA4 | 0x4e | The next 4 bytes, little endian, are a length, then that many bytes are pushed. |
OP_1NEGATE | 0x4f | Pushes the number -1. |
OP_1, OP_TRUE | 0x51 | Pushes the number 1. |
OP_2 to OP_16 | 0x52 to 0x60 | Pushes 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:
| Opcode | Hex | What it does |
|---|---|---|
OP_NOP | 0x61 | Does nothing. |
OP_VER | 0x62 | Pushes the version of the spending transaction. Invalid when executed before Chronicle. |
OP_IF | 0x63 | Runs the next branch if the top value is true, and removes the value. Closed by OP_ENDIF. |
OP_NOTIF | 0x64 | Runs the next branch if the top value is false, and removes the value. Closed by OP_ENDIF. |
OP_VERIF | 0x65 | Like OP_IF, but the condition is that the top item equals the transaction version. Invalid when executed before Chronicle. |
OP_VERNOTIF | 0x66 | The negation of OP_VERIF. Invalid when executed before Chronicle. |
OP_ELSE | 0x67 | Runs if the matching OP_IF or OP_NOTIF branch did not. |
OP_ENDIF | 0x68 | Ends a conditional block. An unbalanced conditional makes the script invalid. |
OP_VERIFY | 0x69 | Fails the script if the top value is not true, otherwise removes it. |
OP_RETURN | 0x6a | Before 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:
| Opcode | Hex | What it does |
|---|---|---|
OP_TOALTSTACK | 0x6b | Moves the top item to the alt stack. |
OP_FROMALTSTACK | 0x6c | Moves the top item of the alt stack to the main stack. |
OP_2DROP | 0x6d | Removes the top two items. |
OP_2DUP | 0x6e | Duplicates the top two items. |
OP_3DUP | 0x6f | Duplicates the top three items. |
OP_2OVER | 0x70 | Copies the second pair from the top to the top. |
OP_2ROT | 0x71 | Moves the fifth and sixth items to the top. |
OP_2SWAP | 0x72 | Swaps the top two pairs. |
OP_IFDUP | 0x73 | Duplicates the top item if it is not zero. |
OP_DEPTH | 0x74 | Pushes the number of items on the stack. |
OP_DROP | 0x75 | Removes the top item. |
OP_DUP | 0x76 | Duplicates the top item. |
OP_NIP | 0x77 | Removes the second item from the top. |
OP_OVER | 0x78 | Copies the second item from the top to the top. |
OP_PICK | 0x79 | Takes 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_ROLL | 0x7a | Takes 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_ROT | 0x7b | Rotates the top three items, so the third becomes the top. |
OP_SWAP | 0x7c | Swaps the top two items. |
OP_TUCK | 0x7d | Copies the top item to behind the second item. |
Data manipulation
Join, split and measure byte arrays, and convert between byte arrays and numbers:
| Opcode | Hex | What it does |
|---|---|---|
OP_CAT | 0x7e | Joins two byte arrays. Before Genesis the result must be at most 520 bytes. |
OP_SPLIT | 0x7f | Splits a byte array at position n into two items. Fails if n is negative or longer than the array. |
OP_NUM2BIN | 0x80 | Encodes 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_BIN2NUM | 0x81 | Re-encodes a byte array as a minimal number. |
OP_SIZE | 0x82 | Pushes 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:
| Opcode | Hex | What it does |
|---|---|---|
OP_INVERT | 0x83 | Flips every bit of the top item. |
OP_AND | 0x84 | Bitwise AND of two items of equal length. |
OP_OR | 0x85 | Bitwise OR of two items of equal length. |
OP_XOR | 0x86 | Bitwise XOR of two items of equal length. |
OP_EQUAL | 0x87 | Pushes 1 if the two top items are byte for byte equal, otherwise 0. |
OP_EQUALVERIFY | 0x88 | OP_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:
| Opcode | Hex | What it does |
|---|---|---|
OP_1ADD | 0x8b | Adds 1. |
OP_1SUB | 0x8c | Subtracts 1. |
OP_2MUL | 0x8d | Multiplies by 2. Disabled before Chronicle. |
OP_2DIV | 0x8e | Divides by 2, rounding toward zero. Disabled before Chronicle. |
OP_NEGATE | 0x8f | Flips the sign. |
OP_ABS | 0x90 | Absolute value. |
OP_NOT | 0x91 | 0 becomes 1, anything else becomes 0. |
OP_0NOTEQUAL | 0x92 | 0 stays 0, anything else becomes 1. |
OP_ADD | 0x93 | a plus b. |
OP_SUB | 0x94 | a minus b. |
OP_MUL | 0x95 | a times b. |
OP_DIV | 0x96 | a divided by b. Fails if b is 0. |
OP_MOD | 0x97 | The remainder of a divided by b. Fails if b is 0. |
OP_LSHIFT | 0x98 | Shifts a byte array left by b bits. Fails if b is negative. |
OP_RSHIFT | 0x99 | Shifts a byte array right by b bits. Fails if b is negative. |
OP_BOOLAND | 0x9a | 1 if a and b are both not zero, otherwise 0. |
OP_BOOLOR | 0x9b | 1 if a or b is not zero, otherwise 0. |
OP_NUMEQUAL | 0x9c | 1 if a equals b as numbers, otherwise 0. |
OP_NUMEQUALVERIFY | 0x9d | OP_NUMEQUAL, then OP_VERIFY. |
OP_NUMNOTEQUAL | 0x9e | 1 if a and b differ as numbers, otherwise 0. |
OP_LESSTHAN | 0x9f | 1 if a is less than b. |
OP_GREATERTHAN | 0xa0 | 1 if a is greater than b. |
OP_LESSTHANOREQUAL | 0xa1 | 1 if a is less than or equal to b. |
OP_GREATERTHANOREQUAL | 0xa2 | 1 if a is greater than or equal to b. |
OP_MIN | 0xa3 | The smaller of a and b. |
OP_MAX | 0xa4 | The larger of a and b. |
OP_WITHIN | 0xa5 | 1 if x is at least min and less than max. The three are the top items, with max on top. |
OP_LSHIFTNUM | 0xb6 | Shifts a number left by b bits and keeps its sign. Chronicle only, a no-op before. It took the slot of OP_NOP7. |
OP_RSHIFTNUM | 0xb7 | Shifts 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:
| Opcode | Hex | What it does |
|---|---|---|
OP_SUBSTR | 0xb3 | Pushes length bytes of a byte array, starting at a byte offset. Fails if the range is outside the array. |
OP_LEFT | 0xb4 | Pushes the leftmost n bytes. Fails if n is negative or longer than the array. |
OP_RIGHT | 0xb5 | Pushes 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:
| Opcode | Hex | What it does |
|---|---|---|
OP_RIPEMD160 | 0xa6 | RIPEMD-160 hash of the top item. |
OP_SHA1 | 0xa7 | SHA-1 hash of the top item. |
OP_SHA256 | 0xa8 | SHA-256 hash of the top item. |
OP_HASH160 | 0xa9 | RIPEMD-160 of the SHA-256 of the top item. |
OP_HASH256 | 0xaa | SHA-256 applied twice to the top item. |
OP_CODESEPARATOR | 0xab | A signature check after it covers only the script that follows the most recent OP_CODESEPARATOR. |
OP_CHECKSIG | 0xac | Checks a signature against a public key over the signature hash of the transaction. Pushes 1 or 0. |
OP_CHECKSIGVERIFY | 0xad | OP_CHECKSIG, then OP_VERIFY. |
OP_CHECKMULTISIG | 0xae | Checks m signatures against n public keys, in order. It takes one extra item from the stack. Pushes 1 or 0. |
OP_CHECKMULTISIGVERIFY | 0xaf | OP_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:
| Opcode | Hex | What it does |
|---|---|---|
OP_CHECKLOCKTIMEVERIFY, OP_NOP2 | 0xb1 | Before Genesis, fails the script unless the transaction lock time has passed the top value. |
OP_CHECKSEQUENCEVERIFY, OP_NOP3 | 0xb2 | Before 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:
| Opcode | Hex | What it does |
|---|---|---|
OP_RESERVED | 0x50 | Invalid when executed. |
OP_RESERVED1 | 0x89 | Invalid when executed. |
OP_RESERVED2 | 0x8a | Invalid when executed. |
OP_NOP1 | 0xb0 | Ignored. Fails if the DISCOURAGE_UPGRADABLE_NOPS flag is set. |
OP_NOP9, OP_NOP10 | 0xb8 to 0xb9 | Ignored. Fails if the DISCOURAGE_UPGRADABLE_NOPS flag is set. |
OP_SMALLINTEGER | 0xfa | Matches a small integer push in a template. |
OP_PUBKEYS | 0xfb | Matches a sequence of public keys in a template. |
OP_PUBKEYHASH | 0xfd | Matches a public key hashed with OP_HASH160. |
OP_PUBKEY | 0xfe | Matches a public key usable by OP_CHECKSIG. |
OP_INVALIDOPCODE | 0xff | Stands 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)Related articles
To learn more about Script on the BSV blockchain, review these articles: