Skip to content

smartauditorAI/bitcoin-opcodes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

bitcoin-opcodes

Bitcoin Script opcodes plus Segwit and Taproot upgrades

Info retrieved from: https://en.bitcoin.it/wiki/Script

Hex Word Previous OPCODE Disabled Input Output Soft Fork Description
0x00 OP_0, OP_FALSE (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.)
0x01 - 0x4b N/A (special) data The next opcode bytes is data to be pushed onto the stack
0x4C OP_PUSHDATA1 (special) data The next byte contains the number of bytes to be pushed onto the stack.
0x4D OP_PUSHDATA2 (special) data The next two bytes contain the number of bytes to be pushed onto the stack in little endian order.
0x4E OP_PUSHDATA4 (special) data The next four bytes contain the number of bytes to be pushed onto the stack in little endian order.
0x4F OP_1NEGATE -1 The number -1 is pushed onto the stack.
0x50 OP_RESERVED Transaction is invalid unless occurring in an unexecuted OP_IF branch
0x51 OP_1, OP_TRUE 1 The number 1 is pushed onto the stack.
0x52 OP_2 2 The number 2 is pushed onto the stack.
0x53 OP_3 3 The number 3 is pushed onto the stack.
0x54 OP_4 4 The number 4 is pushed onto the stack.
0x55 OP_5 5 The number 5 is pushed onto the stack.
0x56 OP_6 6 The number 6 is pushed onto the stack.
0x57 OP_7 7 The number 7 is pushed onto the stack.
0x58 OP_8 8 The number 8 is pushed onto the stack.
0x59 OP_9 9 The number 9 is pushed onto the stack.
0x5A OP_10 10 The number 10 is pushed onto the stack.
0x5B OP_11 11 The number 11 is pushed onto the stack.
0x5C OP_12 12 The number 12 is pushed onto the stack.
0x5D OP_13 13 The number 13 is pushed onto the stack.
0x5E OP_14 14 The number 14 is pushed onto the stack.
0x5F OP_15 15 The number 15 is pushed onto the stack.
0x60 OP_16 16 The number 16 is pushed onto the stack.
0x61 OP_NOP Does nothing.
0x62 OP_VER Transaction is invalid unless occurring in an unexecuted OP_IF branch
0x63 OP_IF if [statements] If the top stack value is not False, the statements are executed. The top stack value is removed.
0x64 OP_NOTIF notif [statements] If the top stack value is False, the statements are executed. The top stack value is removed.
0x65 OP_VERIF Transaction is invalid even when occurring in an unexecuted OP_IF branch
0x66 OP_VERNOTIF Transaction is invalid even when occurring in an unexecuted OP_IF branch
0x67 OP_ELSE if [statements] else [statements]* endif If the preceding OP_IF, OP_NOTIF, or OP_ELSE was not executed, these statements are; if the preceding OP_IF, OP_NOTIF, or OP_ELSE was executed, these statements are not.
0x68 OP_ENDIF if [statements] else [statements]* endif Ends an if/else block. All blocks must end, or the transaction is invalid. An OP_ENDIF without OP_IF earlier is also invalid.
0x69 OP_VERIFY True / false Nothing / fail Marks transaction as invalid if top stack value is not true. The top stack value is removed.
0x6A OP_RETURN fail Marks transaction as invalid. 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.
0x6B OP_TOALTSTACK x1 (alt)x1 Puts the input onto the top of the alt stack. Removes it from the main stack.
0x6C OP_FROMALTSTACK (alt)x1 x1 Puts the input onto the top of the main stack. Removes it from the alt stack.
0x6D OP_2DROP x1 x2 Removes the top two stack items.
0x6E OP_2DUP x1 x2 x1 x2 x1 x2 Duplicates the top two stack items.
0x6F OP_3DUP x1 x2 x3 x1 x2 x3 x1 x2 x3 Duplicates the top three stack items.
0x70 OP_2OVER x1 x2 x3 x4 x1 x2 x3 x4 x1 x2 Copies the pair of items two spaces back in the stack to the front.
0x71 OP_2ROT x1 x2 x3 x4 x5 x6 x3 x4 x5 x6 x1 x2 The fifth and sixth items back are moved to the top of the stack.
0x72 OP_2SWAP x1 x2 x3 x4 x3 x4 x1 x2 Swaps the top two pairs of items.
0x73 OP_IFDUP x x / x x If the top stack value is not 0, duplicate it.
0x74 OP_DEPTH Puts the number of stack items onto the stack.
0x75 OP_DROP x Removes the top stack item.
0x76 OP_DUP x x x Duplicates the top stack item.
0x77 OP_NIP x1 x2 x2 Removes the second-to-top stack item.
0x78 OP_OVER x1 x2 x1 x2 x1 Copies the second-to-top stack item to the top.
0x79 OP_PICK xn ... x2 x1 x0 xn ... x2 x1 x0 xn The item n back in the stack is copied to the top.
0x7A OP_ROLL xn ... x2 x1 x0 ... x2 x1 x0 xn The item n back in the stack is moved to the top.
0x7B OP_ROT x1 x2 x3 x2 x3 x1 The 3rd item down the stack is moved to the top.
0x7C OP_SWAP x1 x2 x2 x1 The top two items on the stack are swapped.
0x7D OP_TUCK x1 x2 x2 x1 x2 The item at the top of the stack is copied and inserted before the second-to-top item.
0x7E OP_CAT x x1 x2 out Concatenates two strings. Disabled.
0x7F OP_SUBSTR x in begin size out Returns a section of a string. Disabled.
0x80 OP_LEFT x in size out Keeps only characters left of the specified point in a string. Disabled.
0x81 OP_RIGHT x in size out Keeps only characters right of the specified point in a string. Disabled.
0x82 OP_SIZE in in size Pushes the string length of the top element of the stack (without popping it).
0x83 OP_INVERT x in out Flips all of the bits in the input. Disabled.
0x84 OP_AND x x1 x2 out Boolean AND between each bit in the inputs. Disabled.
0x85 OP_OR x x1 x2 out Boolean OR between each bit in the inputs. Disabled.
0x86 OP_XOR x x1 x2 out Boolean exclusive OR between each bit in the inputs. Disabled.
0x87 OP_EQUAL x1 x2 True / false Returns 1 if the inputs are exactly equal, 0 otherwise.
0x88 OP_EQUALVERIFY x1 x2 Nothing / fail Same as OP_EQUAL, but runs OP_VERIFY afterward.
0x89 OP_RESERVED1 Transaction is invalid unless occurring in an unexecuted OP_IF branch.
0x8A OP_RESERVED2 Transaction is invalid unless occurring in an unexecuted OP_IF branch.
0x8B OP_1ADD in out 1 is added to the input.
0x8C OP_1SUB in out 1 is subtracted from the input.
0x8D OP_2MUL x in out The input is multiplied by 2. Disabled.
0x8E OP_2DIV x in out The input is divided by 2. Disabled.
0x8F OP_NEGATE in out The sign of the input is flipped.
0x90 OP_ABS in out The input is made positive.
0x91 OP_NOT in out If the input is 0 or 1, it is flipped. Otherwise, the output will be 0.
0x92 OP_0NOTEQUAL in out Returns 0 if the input is 0, 1 otherwise.
0x93 OP_ADD a b out a is added to b.
0x94 OP_SUB a b out b is subtracted from a.
0x95 OP_MUL x a b out a is multiplied by b. Disabled.
0x96 OP_DIV x a b out a is divided by b. Disabled.
0x97 OP_MOD x a b out Returns the remainder after dividing a by b. Disabled.
0x98 OP_LSHIFT x a b out Shifts a left b bits, preserving sign. Disabled.
0x99 OP_RSHIFT x a b out Shifts a right b bits, preserving sign. Disabled.
0x9A OP_BOOLAND a b out If both a and b are not 0, the output is 1. Otherwise 0.
0x9B OP_BOOLOR a b out If a or b is not 0, the output is 1. Otherwise 0.
0x9C OP_NUMEQUAL a b out Returns 1 if the numbers are equal, 0 otherwise.
0x9D OP_NUMEQUALVERIFY a b Nothing / fail Same as OP_NUMEQUAL, but runs OP_VERIFY afterward.
0x9E OP_NUMNOTEQUAL a b out Returns 1 if the numbers are not equal, 0 otherwise.
0x9F OP_LESSTHAN a b out Returns 1 if a is less than b, 0 otherwise.
0xA0 OP_GREATERTHAN a b out Returns 1 if a is greater than b, 0 otherwise.
0xA1 OP_LESSTHANOREQUAL a b out Returns 1 if a is less than or equal to b, 0 otherwise.
0xA2 OP_GREATERTHANOREQUAL a b out Returns 1 if a is greater than or equal to b, 0 otherwise.
0xA3 OP_MIN a b out Returns the smaller of a and b.
0xA4 OP_MAX a b out Returns the larger of a and b.
0xA5 OP_WITHIN x min max out Returns 1 if x is within the specified range (left-inclusive), 0 otherwise.
0xA6 OP_RIPEMD160 in hash The input is hashed using RIPEMD-160.
0xA7 OP_SHA1 in hash The input is hashed using SHA-1.
0xA8 OP_SHA256 in hash The input is hashed using SHA-256.
0xA9 OP_HASH160 in hash The input is hashed twice: first with SHA-256 and then with RIPEMD-160.
0xAA OP_HASH256 in hash The input is hashed two times with SHA-256.
0xAB OP_CODESEPARATOR All of the signature checking words will only match signatures to the data after the most recently-executed OP_CODESEPARATOR.
0xAC OP_CHECKSIG sig pubkey True / false The entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) 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.
0xAD OP_CHECKSIGVERIFY sig pubkey Nothing / fail Same as OP_CHECKSIG, but OP_VERIFY is executed afterward.
0xAE OP_CHECKMULTISIG x sig1 sig2 ... pub1 pub2 ... True / False Segwit Compares the first signature against each public key until it finds an ECDSA match. Starting with the subsequent public key, it compares the second signature against each remaining public key until it finds an ECDSA match. The process is repeated until all signatures have been checked or not enough public keys remain to produce a successful result. All signatures need to match a public key. Due to a bug, one extra unused value is removed from the stack.
0xAF OP_CHECKMULTISIGVERIFY x sig1 sig2 ... pub1 pub2 ... Nothing / fail Segwit Same as OP_CHECKMULTISIG, but OP_VERIFY is executed afterward.
0xB0 OP_NOP1 The word is ignored. Does not mark transaction as invalid.
0xB1 OP_CHECKLOCKTIMEVERIFY OP_NOP2 x x / fail Taproot Marks transaction as invalid if the top stack item is greater than the transaction's nLockTime field, otherwise script evaluation continues as though an OP_NOP was executed. Transaction is also invalid if: 1. the stack is empty; 2. the top stack item is negative; 3. the top stack item is greater than or equal to 500000000 while the transaction's nLockTime field is less than 500000000, or vice versa; 4. the input's nSequence field is equal to 0xffffffff. The precise semantics are described in BIP 0065.
0xB2 OP_CHECKSEQUENCEVERIFY OP_NOP3 x x / fail Taproot Marks transaction as invalid if the relative lock time of the input (enforced by BIP 0068 with nSequence) is not equal to or longer than the value of the top stack item. The precise semantics are described in BIP 0112.
0xB3 OP_NOP4 The word is ignored. Does not mark transaction as invalid.
0xB4 OP_NOP5 The word is ignored. Does not mark transaction as invalid.
0xB5 OP_NOP6 The word is ignored. Does not mark transaction as invalid.
0xB6 OP_NOP7 The word is ignored. Does not mark transaction as invalid.
0xB7 OP_NOP8 The word is ignored. Does not mark transaction as invalid.
0xB8 OP_NOP9 The word is ignored. Does not mark transaction as invalid.
0xB9 OP_NOP10 The word is ignored. Does not mark transaction as invalid.
0xBA OP_CHECKSIGADD sig n pub out Taproot Three values are popped from the stack. The integer n is incremented by one and returned to the stack if the signature is valid for the public key and transaction. The integer n is returned to the stack unchanged if the signature is the empty vector (OP_0). In any other case, the script is invalid. This opcode is only available in tapscript.

Script

Bitcoin Script language is used to validate Bitcoin transactions. The spender must provide:

  • scriptPubKey: One or more public keys and the OPCODES to validate them. Is the output of the previously unspent (available) transaction. ( ~to )
  • scriptSig: Signature to prove ownership. Is the input of the spending transaction. ( ~from )

A transaction is valid if no errors are triggered and the top stack item is True (non-zero) when the script exits.

Standard Transaction to Bitcoin address (pay-to-pubkey-hash) [P2PKH]

scriptPubKey: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
scriptSig: <sig> <pubKey>

In the stack scriptSig and scriptPubKey are combined in that order.

Stack layout after the OP_DUP is executed:

|    <pubKey>     | <---- top of the stack
|    <pubKey>     |
|      <sig>      | <---- bottom of the stack
|-----------------|

Obsolete pay-to-pubkey transaction

Used in initial Bitcoin core versions, but still compatible:

scriptPUbKey: <pubKey> OP_CHECKSIG
scriptSig: <sig>

Provably unspendable outputs

scriptPubKey: OP_RETURN (.*)

Those transactions are marked as invalid and get ignored to compute the UTXO. They are used to store data in the blockchain, as for example, Bitcoin Runes which implemented NFTs on Bitcoin. They can be also used to burn bitcoins.

References:

About

Bitcoin Script opcodes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published