Bitcoin’s Opcodes

Jennifer Ezeobi
3 min readJun 7, 2022

Introduction

Bitcoin Script is a stack-based scripting language that is used to create Bitcoin smart contracts. It encumbers the coins in that transaction and contains a list of instructions on how outputs can be unlocked to be spent.

This list of instructions is specified by operation codes (opcodes), which is the focus of this article.
This article will explore the role that opcodes play in scripts, the types of opcodes that exist, some commonly used opcodes, and opcodes that have been disabled in the past.

Role of Opcodes in Bitcoin Script

Bitcoin’s opcodes are used to carry out relevant operations on the operands within the stack-based Bitcoin Script. They are basically like commands that are carried out in a script. The commands are in the form of keywords and are usually prefixed with “OP_

In Bitcoin script, a locking script (scriptPubKey) is used to lock funds while an unlocking script is provided to spend the coin. When a new UTXO is created as part of a *previous* spend, it is sent to (or “locked by”) a `scriptPubKey`. To spend this UTXO in the future, the sender must provide an `unlocking script` that satisfies the `scriptPubKey`.

The full script(locking and unlocking) is then executed sequentially (starting with the unlocking script) by nodes to validate the transaction.

Opcodes in action

One basic example of a script is the one used in a Pay-to-Public-Key-Hash (P2PKH) transaction.

The Pay-to-Public-Key-Hash (P2PKH) locks a transaction’s output to the hash of the receiver’s public key. The receiver will only be able to spend that output as an input in another transaction if they can provide their public key and a digital signature with the corresponding private key.

In this case, the locking script would look like so:

OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

Where the unlocking script will be in the form:

<signature> <pubKey>

The locking and unlocking script are then combined(the unlocking script is executed first) and executed as illustrated in the figure below.

Image source: learnmeabitcoin

The opcodes in the script above are:

  • OP_DUP: This function duplicates the topmost item on the stack
  • OP_HASH160: This function performs first a SHA256 hash and then a RIPEMD160 hash.
  • OP_EQUALVERIFY: This function compares the last two items on the stack and verifies that they are the same.
  • OP_CHECKSIG: This function verifies that the ECDSA signature matches the provided public key. If the check passes, it pushes true to the stack and false if it fails.

Types of opcodes

Opcodes are grouped based on the type of function they perform. According to bitcoinwiki, they are grouped into:

  1. Constants: This group of opcodes pushes a specific number of data to the stack. An example is the OP_PUSHDATA1
  2. Flow control: This group of opcodes determines the flow of the script. An example is the OP_IF
  3. Stack: This group of opcodes is responsible for working on items on the stack and moving them around as required. An example is the OP_DROP
  4. Data manipulation: Opcodes in this group are responsible for specifying what sort of data manipulation to perform on the provided data. An example is OP_SIZE.
  5. Bitwise logic: This group of opcodes executes on specified data input data. An example is OP_EQUAL
  6. Arithmetic: This group of opcodes carries out arithmetic operations on the data. An example is the OP_ADD
  7. Cryptography: Opcodes in this group are used to carry out cryptographic functions on provided data. An example is OP_HASH256.

Frequently used opcodes in Bitcoin Script

Some of the commonly used opcodes in Bitcoin script include:

  1. OP_DUP
  2. OP_IF
  3. OP_ENDIF
  4. OP_EQUALVERIFY
  5. OP_HASH160
  6. OP_CHECKSIG
  7. OP_VERIFY
  8. OP_EQUAL
  9. OP_CHECKLOCKTIMEVERIFY
  10. OP_RETURN

A complete list of all opcodes used in Bitcoin is available here.

Disabled opcodes

Some opcodes have been disabled such that they are no longer valid in the Bitcoin protocol. OP_LSHIFT was disabled because processing transactions that contained this opcode caused bitcoin to crash. Other opcodes were also disabled as a security measure. Read more here
Any script that has any of these disabled opcodes will abort and fail.

The disabled opcodes include:

  1. OP_CAT
  2. OP_SUBSTR
  3. OP_LEFT
  4. OP_RIGHT
  5. OP_INVERT
  6. OP_AND
  7. OP_OR
  8. OP_XOR
  9. OP_2MUL
  10. OP_2DIV
  11. OP_MUL
  12. OP_DIV
  13. OP_MOD
  14. OP_LSHIFT
  15. OP_RSHIFT

Conclusion

Bitcoin’s opcodes are like functions that power Bitcoin Scripts. They aid in producing customizable locking conditions for bitcoin transactions. Various combinations of these opcodes can yield different locking conditions.

--

--