Bitcoin Script Explained
Script is a stack-based scripting programming language used within the Bitcoin protocol for transaction processing. It has found application within the protocol due to its limited complexity and minimal processing requirements. The Script programming language is Turing incomplete and lacks the functionalities of modern programming languages, such as loops. However, this limited functionality is intentional as it prevents what are known as infinite loops from occurring, something which is possible with Turing complete languages.
As mentioned previously, Script is a stack-based language because it uses a stack data structure in processing and executing commands. To illustrate, imagine a stack of five books placed on top of one another. Operations can then be performed on the stack of books, for example, a push operation would add a new book to the existing stack, whereas a pop operation would result in the removal of a book from the stack. Operations can only be performed on the topmost item in a stack.
The Script programming language uses what are known as operation codes, or opcodes, to remove items, perform a calculation and then push the result back onto the stack. For example, consider the following code written in Script:
2 3 OP_ADD 6 OP_EQUAL
OP_ADD is an opcode that pops (removes) two items from the stack, adds them together and pushes the result back onto the stack. OP_EQUAL is an opcode that pops two items from the stack and compares them to check if they are equal, if they are, it then pushes the result TRUE back onto the stack.
Scripts written in Script are executed by processing each item from left to right. In the example above, the numbers 2 and 3 will be pushed onto the stack, with the number 3 being the topmost item. The OP_ADD opcode will pop numbers 2 and 3 from the stack, add them together and push the result, 5, back onto the stack such that it is now the only item in the stack. The number 6 is then pushed onto the stack, resulting in a total of two items now being in the stack, with the number 6 being above 5 in the stack. The OP_EQUAL opcode then pops the numbers 6 and 5 from the stack, compares them to check if they are equal and pushes the result FALSE back onto the stack. The net result of this script is that only the item FALSE will be left in the stack.
scriptPubKey & scriptSig
This system of stack-based execution is used in the same manner when processing Bitcoin transactions. The validation of a transaction on the Bitcoin network requires two types of scripts: a locking script and an unlocking script, also known as scriptPubKey and scriptSig.
scriptPubKey is a locking script placed on the output of a Bitcoin transaction that requires certain conditions to be met in order for a recipient to spend his/her bitcoins. Whereas, scriptSig is the unlocking script that satisfies the conditions placed on the output by the scriptPubKey and is what allows it to be spent.
As can be seen from the image above, the unlocking script, scriptSig, contains a <sig> and <PubK>, or better known as a digital signature and public key. Both must be provided in order for the locking script to be satisfied. Conversely, the locking script, scriptPubKey, contains a <PubKHash>, also known as a public key hash, or more simply, a Bitcoin address. DUP is an opcode that duplicates the topmost item in the stack and pushes the result back onto it. HASH160 is another opcode that hashes the topmost item in the stack through the following function:
A = RIPEMD160(SHA-256(X))
Where A = the result of the hash and X = the topmost item in the stack
RIPEMD160 and SHA-256 are both hash functions used extensively within the Bitcoin protocol. A detailed explanation of a hash function can be found here: What is SHA-256 and How is it Related to Bitcoin?
EQUALVERIFY is another opcode that compares the <PubKHash> value placed in the locking script with the <PubKHash> value calculated from the <PubK> placed in the unlocking script using the following hash function:
<PubKHash> = RIPEMD160(SHA256(<PubK>))
If the resulting <PubKHash> value calculated from the <PubK> value in the unlocking script, is the same as the <PubKHash> value in the locking script, the execution will be allowed to continue.
Lastly, CHECKSIG is an opcode that checks that the digital signature <sig> matches the public key <PubK> and pushes the result TRUE onto the stack. Digital signatures are created by the owner of the public keys, and therefore serve as a way of ensuring that the individual is authorized to spend the funds.
To conclude, Script is a scripting language that uses a stack-based data structure and has found application within Bitcoin transaction processing due it’s limited functionality, and therefore, added security.
Two notable example scripts used within the Bitcoin protocol are scriptPubKey and scriptSig.
More information on Script can be found here: