Basics of Bitcoin Scripts
The Bitcoin scripting language consists of four main attributes:
- Forth-like
- Stack-based
- Reverse-polish Notation
- Turing incomplete
The first attribute forth-like describes the resemblance of Bitcoin script to the programming language forth, which is also stack-based. Stack-based describes a common and popular data structure where the operations are structured in a linear logical order. The basic idea behind this is last in first out, or LIFO for short. A stack works with two operations push and pop, where push is pushing something onto the stack and pop is popping something of the stack. The third attribute reverse polish describes the notation in a system where operators follow the operands.
With the fourth attribute, Turing incomplete, the bitcoin scripting language has only limited functionality and cannot make jumps or loops. Because bitcoin scripts are rather simple and don’t need to be as complicated as Ethereum smart contracts, this is fine for bitcoin and also adds a layer of security where malicious parties can’t create scripts that would negatively affect the hash rate of the bitcoin network.
The operations that a Bitcoin script can execute are called op_codes, below is a table with some of these op_codes and an explanation. A complete list can be found under the following link (https://wiki.bitcoinsv.io/index.php/Opcodes_used_in_Bitcoin_Script).
# | Identifier | Description |
1 | OP_CHECKSIG | The entire transaction’s outputs, inputs, and script are hashed. |
2 | OP_CHECKMULTISIG | Compares the first signature against each public key until it finds an ECDSA match. |
3 | OP_ADD | a is added to b. |
4 | OP_MAX | Returns the larger of a and b. |
5 | OP_RIPEMD160 | The input is hashed using RIPEMD-160. |
6 | OP_PUBKEY | Represents a public key compatible with OP_CHECKSIG. |