An OP_CODE or Operation Code, is a primitive code or instruction that allows us to program operations within a blockchain like Bitcoin. In fact, thanks to the set of these codes, we can create advanced programming structures or smart contracts that are then read and executed by the nodes of a blockchain network.
Lhe instructions and instructions used in computing for programming computer equipment and virtual machines are known as OP_CODE (Operation Code) o operation codes.
These codes indicate the operations or functions that must be performed and executed by the hardware or software. In the case of hardware, the shape of the OP_CODE will be defined according to the type of operations set architecture (ISA) of the computer. That is, according to the instructions that the device has for the interpretation and execution of the operation codes. In the case of software, OP_CODE are created with a type of ISA, or operations architecture, specially designed to be interpreted by the virtual machine or the byte interpreter.
Operation codes allow a computer program to issue an order to the computer and for it to interpret and execute it.
How does an OP_CODE work?
In order for machines, equipment and computers to receive and execute a specific order, it is necessary to issue instructions in a language that they can understand. Therefore, these instructions are written in a programming language that contains operation codes or is ultimately translated into a series of opcodes.
These codes are written in a language that we generally know, such as English, and are then translated by a compiler or translator program for machines to understand it in their binary language. That is, the mnemonic instructions written in our language for the interpretation of us humans, have their equivalent in binary language so that machines can easily interpret them. And thus be able to act on the instruction received and execute it correctly.
Example of an OP_CODE
A good example of OP_CODE can be seen in the following image:
In it we can see some OP_CODES belonging to the instructions capable of executing a processor or CPU computer (specifically the x86 from Intel). As you can see it is somewhat cryptic to read and it can hardly be programmed using it (although some developments are made in this type of language due to its speed).
The situation in the crypto world is not very different and in Bitcoin For example, we can see something like this:
It's certainly just as cryptic, but this simple statement is equivalent to a transaction in Bitcoin. If you want to know more about it, you can read our article dedicated to Bitcoin script where we explain in depth what exactly these instructions do.
Types of opcodes in the Bitcoin script
The OP_CODES for Bitcoin Script they can be classified according to the function and indications that they are going to execute in the system. For example, these codes can be used to remove, add, move or rotate elements on the stack, analyze and perform arithmetic operations, validate or invalidate transactions, among many other functions.
Next we will present a list of the operation codes classified according to their type and functions. However it is important to note that this list is neither conclusive nor radical, since there are many opcodes in the Bitcoin script and only a few of them are mentioned here.
Embed codes
OP_0 = 0x00 y OP_FALSE = OP_0: both codes they push or add an element or an empty byte array to the stack. Neither one should be confused with NO_OP
OP_PUSHDATA1 = 0x4c, OP_PUSHDATA2 = 0x4d y OP_PUSHDATA4 = 0x4e: In the first code, the next byte contains the number of bytes to be inserted into the stack. In the second code the next 2 bytes they contain the number of bytes to be inserted in the stack, while in the third they are the next 4 bytes those containing the number of bytes to be added to the stack. In these codes the process of adding the bytes will be done in format endian.
OP_NEGATIVE = 0x4f y OP_TRUE = OP_1: In this case, the negative number -1 is pushed to the stack, whereas in OP_TRUE positive number 1 is pushed to the stack.
Control codes
OP_NOP = 0x61: This code does not execute any operation, but is used in cases where some space has to be filled within the program.
OP_IF = 0x63 y OP_NOTIF = 0x64: Both codes are used to execute the declarations. In the first code, they are executed if the top stack value No. is FALSE; in the second code, they are executed if the top stack values si is FALSE. Thus in both cases, the value of the top stack is removed.
OP_ELSE = 0x67- Used to affirm or deny statements. In case the two previous codes have been executed (OP_IF and OP_NOTIF) the statements are denied. In case of No. having been executed, the statements are affirmed.
OP_ENDIF = 0x68: used to finish a block, since without this, the transactions would not be valid. A code OP_ENDIF without a code OP_IF above will also not be valid.
OP_RETURN = 0x6a- Used to mark a transaction exit as invalid.
Application codes and stack operations
OP_TOALTSTACK = 0x6b y OP_FROMALTSTACK = 0x6c- The first code is used to move the entry to the top of the alternate stack and remove it from the main stack. The second code executes the opposite process; moves the entry to the top of the main stack and removes it from the alternate stack.
OP_DROP = 0x75 y OP_DUP = 0x76: are used to delete an item from the top stack and to duplicar one item in the top stack, respectively.
Splice Operation Codes
OP_SIZE = 0x82- This code pushes the string length of the top element of the stack, without breaking it.
The codes OP_SUBSTR = 0x7f, OP_LEFT = 0X80 y OP_RIGHT = 0x81 have been disabled. They were used to:
- Give back a section in a chain.
- Keep only the remaining characters of a specific point in a string, and
- Keep only the remaining characters to the right side of a specific point in a string, respectively.
Codes for numerical operations
OP_1ADD = 0x8b y OP_1SUB = 0x8c: are used to add (sumar) or delete (subtract) 1 at the entrance respectively.
OP_2MUL = 0x8d y OP_1DIV = 0x8e: were used for multiply o divide by 2 the entry respectively, but both were disabled.
OP_ABS = 0x90 y OP_NEGATE = 0x8f: they are used to invert the sign of the input, that is, to make the input positive or negative respectively.
OP_MIN = 0xa3 y OP_MAX = 0xa4: these codes are used to return the lower value of a and b, or in your case to return the higher value of a and b respectively.
Coding and encryption codes
OP_RIPEMD160 = 0xa6: It is used to encode the input with the RIPEMD-160 algorithm, in order to obtain shorter but equally safe hashes for creating addresses.
OP_SHA1 = 0xa7: it is used to encode the input with the SHA-1 algorithm, which is used to calculate and obtain a single check value for all inputs not greater than 264 - 1 bit.
OP_SHA256 = 0xa8: used to encode the input with the algorithm SHA-256.
OP_HASH160 = 0xa9: used to encode the input 2 times. The first encoding through the SHA-256 algorithm and the second with the RIPEMD-160 algorithm
OP_HASH256 = 0xay- Used to encode the input 2 times through the SHA-256 algorithm.
OP_CHECKMULTISIG = 0xae: used to make a comparison between the first signature with each public key until a match is found ECDSA. Then repeat the process with the second signature and subsequent public keys and so on until all the signatures have been verified.
Time lock codes
OP_CHECKLOCKTIMEVERIFY = 0xb1: this code marks as No. the entry is valid if the top element of the stack is greater than the field set as nLockTime, which represents the condition that must be met. In the application of this code there are several implications that also make the entry invalid. For example, if the stack is empty or its top element is negative, among others.
OP_CHECKSEQUENCESSEVERIFY = 0xb2: also used to mark the entry as No. Valid if the blocking time is different or greater than the value of the top element of the stack.
Each of these OP_CODE performs a well-defined function, and the joint arrangement of these OP_CODE is what enables Bitcoin to function as it does. However, OP_CODEs are not unique to Bitcoin. Ethereum for example also has its own OP_CODE register which allows you to enter instructions to your Ethereum Virtual Machine, and from there carry out all the operations of this blockchain. That ranges from the simplest transactions to complex smart contract operations on that platform. The same goes for the rest of cryptocurrencies, which also have their OP_CODES or similar tools for their operation.
Hence the importance of them, not only knowing them but developing them to expand the functionalities of cryptocurrencies and the ecosystems they contain.
OP_CODES on other blockchain
When we hear that cryptocurrencies have the ability to be programmed, the truth is that we are saying that blockchains and their tokens can be manipulated thanks to OP_CODES. Certainly, the first sounds much easier, but the technical reality is that: the OP_CODES is in all the blockchains and they are the ones that allow us to program the transactions and other functionalities of the same.
But how big and advanced can these OP_CODES be on other blockchain compared to Bitcoin? The answer to this is simple: there are no limits, just the imagination of the developers.
To give us a good and clear example, let's look at Ethereum. At Ethreum we see a blockchain made up of three elements, the Ether token, the concept of Gas or cost of operation, and the Ethereum Virtual Machine. Seen individually, they seem to have no relationship, but the truth is that they are intimately integrated, and such integration depends on the relationship created by the OP_CODES within Ethereum.
So every time we make a transaction in Ethereum, what we do is program the operation with OP_CODES. This operation will allow us to manage Ether and the payment of the operation we will do with GAS, but all this programming is read and executed by the Ethereum Virtual Machine. Thus we are clear how these three elements communicate and how OP_CODES play an important role in the operation of the network.
The same can be observed in networks such as Litecoin (which uses an equivalent to Bitcoin Script), Dash, EOS, Bitcoin Cash (It also uses an equivalent although modified Bitcoin Script) and the same can be seen with the rest of the cryptocurrencies. In short, OP_CODES are not only in Bitcoin, they are an essential part of cryptocurrencies and blockchain where you are making life.