Bitcoin Script is the language Bitcoin uses to do everything it can do, from sending funds from a wallet to allowing the creation of multi-user accounts. All these functionality contained in a simple extensible and powerful tool that we will know next.

CWhen we talk about Bitcoin Script, We are talking about a simple programming language used in Bitcoin for the processing of transactions that is read from left to right. This is based on a series of linear structures, known as stack, which contain existing data in order LIFO (Last In - Firt Out). Each instruction in this language is executed one after the other consecutively.

This language is not Full Turing because its functionality is limited and cannot loops. So it is not capable of solving any type of problem such as turing machines. However, this limitation is intentional as this prevents infinite or endless looping and error execution. Where malicious parts of the program can be free to create complicated operations to consume the rate of hash and slow down the Bitcoin system through infinite loops.

A programming language is necessary because it allows us to write programs and that computers execute our wishes. In BitcoinIn order to communicate our wishes, the opcodes (OP CODES), which serve various functions. Like memory manipulation, math, loops, function calls, among many others.

Therefore, Bitcoin Script is essentially a set of programmed instructions that are recorded with every transaction made. These instructions describe how users can access and make use of the bitcoins available on the network.

What is an OP_CODE or Operation Code?

In computing, a OP_CODE (Operation Code, in English), is a portion of a machine language instruction that specifies the operation to be performed. Its specification and format will be determined by the instruction set architecture (ISA) of the component that processes the instruction. Generally this processing is done by the computer hardware (usually a CPU).

But it can also be software specially prepared to emulate the operation of a CPU and process these instructions. In general, a complete machine language instruction contains an OP_CODE and, optionally, the specification of one or more operands, on which the operation code must act. Some operations have implicit operands, or none at all.

In Bitcoin and many other cryptocurrencies, OP_CODES are the backbone of the system that allows you to program transaction instructions. Let's remember that Bitcoin (and other cryptocurrencies derived from it) are programmable money. And here the keyword is "programmable", since Bitcoin Script is actually a programming language where instructions are given in OP_CODES that the Bitcoin network has defined beforehand for its operation. In fact, due to the structure in which BItcoin has been created, there is the possibility of defining 256 OP_CODES, different of which start from number 0 to 255. Of these 256 OP_CODES, a total of 116 are currently active.

These OP_CODES are the ones that allow you to carry out the different operations in Bitcoin and its transaction scheduling, such as data flow control, constant management, stack management, logical management, arithmetic, time lock, pseudo-words, cryptographic operations, and reserved words. You can see a complete and updated list of the different OP_CODES directly in the bitcoin code.

Bitcoin Script OP_CODES

scritpSig and scriptPubKey, the essential parts of all Bitcoin Script

In the Bitcoin network, each Bitcoin Script is divided into two types of scripts, the scriptSig y scriptPubKey. First, the scriptSig is the unlock script, which requires a public key and a digital signature. In fact, after detecting various problems in the early versions of the Bitcoin software, signature checks were included. Therefore, the system only accepts to carry out transactions if the signatures and their verification comply with a series of established rules that guarantee proper behavior on the network.

The second, the scriptPubKey, is the blocking script, which contains a public key hash, also called a Bitcoin address. Some Bitcoin scripts require multi-signatures, that is, the authorization of several users to carry out the transaction. In this case, the script is more complicated because it is a much larger operation than the standard peer-to-peer operation. In fact, the scheduling of Bitcoin transactions as such is stored in this part of the script.

Objective of its creation

The purpose of creating a script language in Bitcoin is to provide a series of easy and flexible parameters to enable a transaction. So when Satoshi Nakamoto developed Bitcoin, disabled various functions, including multiplication. So the script is kept simple in terms of programming. So, it is this programming language that determines whether or not an operation can be performed. That is, if said operation is authorized for the transfer of funds.

Simply put, the creation of the Bitcoin Script is what gives the programmable nature of Bitcoin. This is what allows us, for example, to send a transaction that is validated only if certain conditions are met. This property is something unique to Bitcoin and cryptocurrencies, something that today's digital money cannot reach, let alone archaic physical money. It is the evolution of money reached a point where even money can act independently and decentralized simply by attending to the programming embedded in it.

Moreover, The Bitcoin script also prevents the creation of errors in the system and the unnecessary use of very complex transactions. In fact, the script makes transactions relatively easy to calculate. Also, infinite loops require a lot more computing power and slow down networks, as mentioned in the beginning.

Example of a Bitcoin Script

Before continuing to delve further into what it is and the different elements that make up a Bitcoin Script, let's see a small example of it. Next we will see a small Bitcoin Script widely used today for Bitcoin transactions. We will explain its behavior and operation as well as the different parts of it, and then go deeper into its properties.

Example of a Bitcoin Script: Transactions in Bitcoin using the P2PKH script

Bitcoin Script are present in all Bitcoin actions. The truth is that each operation in Bitcoin is carried out by a specific script that defines what is going to be done. Outside of what that script says nothing can happen.

For example, if we want to make a transaction, a script for it would be the following:

Example of a Bitcoin Script

All the above content is a simple Bitcoin transaction of the type P2PKH (Pay to Public Key Hash). This type of script is a standard script and in fact it is the most used way to send bitcoins. At first glance it is shown as something very cryptic and difficult to understand. The truth is that behind such complexity, there is a simple system that we will explain now.

Thoroughly explaining the transaction

The previous Bitcoin Script is delimited in two parts: scriptPubKey y scriptSig. In the first, there are the actions to be performed, while the second contains the signature and public key to verify the authenticity of such actions. This ensures that only those who have access to the funds can carry out any operation with them. On the other hand, there are the OP_CODES that in the operation are the following:

  • OP_DUP: Duplicate the item on the top stack.
  • OP_HASH160: The input is encoded twice: first with SHA-256 and then with RIPEMD-160.
  • OP_EQUALVERIFY: Verify that the data entered is correct and valid.
  • OP_CHECKSIG: The outputs, inputs, and script of the entire transaction are summarized in a hash. The signature used must be a valid signature for this hash and must be next to the public key.

Now what happens during script execution is as follows:

  1. First, the original public key of the owner (which is in the scriptSig) of the funds is duplicated.
  2. The duplicate public key then goes through a hashing process. In this process, a hash is applied first SHA-256 and a RIPEMD-160 hash is then applied to the result.
  3. The result of the hashes is compared with the hash of the public key that is in the scriptPubKey to make sure it is EQUALVERIFY (that is, it is the same key and is verified as valid).
  4. If it matches, the script continues to run and CHECKSIG is performed to verify the signature with the public key.

In this way, everything indicated in the script is carried out in a safe and scheduled way. If you want to know more about how Bitcoin Script works, you can visit our Bit2Me TV service, where you can see some videos explaining much more about this interesting language.

Parts of a Bitcoin Script

Bitcoin Script, the smart contract language of Bitcoin

Many people are the ones who think and argue that Bitcoin is unable to execute smart contracts. The truth is that this is not true, since with Bitcoin Script as such you create smart contracts. Certainly your capabilities are limited compared to what you can do for example Ethereum, but that does not mean that Bitcoin Script has the ability to create smart contracts. But let's explain this in a simpler and more detailed way.

First of all, as we have said, Bitcoin Script is not Full Turing language. This is because it does not require greater complexity to perform its intended functions. But, one of the main reasons is that since there are no loops, you can know for sure when and how a certain program will end. There is no possibility for the execution of errors, which gives you greater security. Also there is no possibility that the programs are blocked or their executor is blocked, which in this case is Bitcoin.

A good example of the importance of these protections would be that with Bitcoin Script it is impossible to create scripts that run forever. In fact, the risk of a script that runs forever is that it would open doors to attack the network by means of a denial of service attack (DoS).

But the fact that Bitcoin Script limits this type of operations protects the network from such attacks. So there is no chance that a Script program with a continuous loop could prevent Bitcoin from working properly. Simply put, Bitcoin Script is much more secure than its more complex counterparts like those featured by Ethereum o NEO.

How much do you know, cryptonuta?

Does the existence of the Bitcoin Script facilitate the process of updating and compatibility between versions of the Bitcoin software?

TRUE!

One of the reasons why Bitcoin Script was created is to facilitate support and backward compatibility with the different possible versions of the Bitcoin software. By creating a programming language, if it did not undergo major changes it was possible to communicate different versions of the Bitcoin software.

Bitcoin Script Evolution

The Script language has been present in Bitcoin since its first version. Analyzing the structure of digital payments, Satoshi Nakamoto, the developer of Bitcoin, determined that a model was needed that would allow the machine to determine whether transactions were made by spending exits successfully or not. In other words, it will be verified if the signatures were correct and if they matched the public key.

This is one of the most studied problems in computing, determining whether spending is successful or unsuccessful in order to avoid double spending. And the solution to this was to use a machine that met certain established criteria. In Bitcoin, it must always stop, so the implementation of a Turing Incomplete language based on a stack system is ideal. Thus, the mechanism that manages the Scripts in Bitcoin is very simple and safe.

In its beginnings, the Script had several serious errors and some of them still exist. An example is the OP_LSHIFT bug, which was known as the bug that could kill Bitcoin. Or the also known Script verification bug that Gavin Andressen and Satoshi Nakamoto corrected in 2010.

The existence of this type of errors will lead many to think that Nakamoto did not perform sufficient tests on the Script before the launch of Bitcoin. In fact, many speculate that the script was an afterthought. Perhaps Nakamoto's original idea was to send bitcoins directly to public keys, but by adding support for Bitcoin's addresses, he realized that a scripting language would be really helpful in providing forward compatibility.

Features

The Bitcoin script language has several characteristics and qualities, among which we can mention:

  1. It is simple and requires minimal processing.
  2. Its functionality is limited, which provides greater security to the system.
  3. Being a Turing Incomplete language it does not have loops, so it is ensured that the program stops repeating and ends. Thus, the possibility of errors and malicious codes to the Bitcoin network is avoided.
  4. Its simplicity allows it to be implemented on a wide range of devices.
  5. There is no state before or after the script execution. All the information necessary to run the script must be contained in it.
  6. It is based on a stack and can use two types. One main and one alternative. Where the latter is used for the storage of intermediate step calculation data. In much the same way as the memory key on calculators.
  7. Bitcoin's scripting language is quite small. It can only contain 256 instructions, since each instruction is expressed in one byte.