Wednesday, July 30, 2014

Scripting in Bitcoin: Part 1

Scripting In Bitcoin: Part 1

This is a work in progress, and the contents of these articles may be edited for corrections and clarifications. 

Part 1
2.Basics of the Scripting Language.
3.Basics of Bitcoin Transactions

Part 2
3.Pay to Pub Key Hash
4.Multi-Signature Transactions
5.Pay To Script Hash

1. Introduction

    The “virtual money” aspect of Bitcoin is simple to explain to the average person. However, there is a hidden and rather complex technical specification that makes Bitcoin revolutionary and more than just coins inside a computer. The technical specification that is revolutionary is the scripting language in Bitcoin.  Satoshi was a smart guy and understood that there was much more that could be done with Bitcoin beyond its use as “virtual money”. He figured out that in theory, Bitcoin's blockchain technology could be used for any kind of complex financial agreements. It could be used to handle more than just simple transactions of Bitcoins between Alice's and Bob's. It could be used to create contracts, escrow, options, derivatives, or any kind of conditional transactions that depend on input from the outside world.

    Satoshi determined that in order to describe complex financial agreements within the blockchain, there would need to be a language built into it. This is what we call the scripting language of Bitcoin. The definition of a “scripting language” is generally interchangeable with “programming language”. To a programmer, something that is a “scripting language” is a programming language that is simple and designed to do one specific task very well. For example, Bash is a scripting language designed to call other programs in the Linux operating system. The scripting language in Bitcoin is designed to specify cryptographic financial transactions and agreements.

    In this series of articles, we will explain the technical details of how the scripting language works in Bitcoin to facilitate transactions. We will begin by describing the basics of the language. Than we will cover the basics of Bitcoin's transactions. Next, we will describe how the standard “Pay to Pub Key Hash” method and multi-signature transactions work. Finally, we will cover the “Pay to Script Hash” method.

    A lot of the information found here can be found elsewhere such as in the Bitcoin wiki for Scripts and Transactions , but I hope to present the material in a more sequential and easy to understand manner. The currently available reading material on multi-signature transactions, and “Pay to Script Hash” is especially lacking, so these articles will try to fill that gap. You should have a basic knowledge of Bitcoin in order to fully understand these articles. At minimum, you should know the details behind how Bitcoin addresses work. The pages below are recommended prior reading materials.

2. Basics of the Scripting Language

    The scripting language in Bitcoin is a stack based language. A stack is a data structure where the first object you put into the stack is the last thing that comes out. All operation in a stack based programming language is done on the stack. Satoshi implemented the stack based scripting language in the very first version of the Bitcoin software released in January of 2009. Since he made no mention of the scripting language in the original white paper released in November 2008, it could be assumed that this was something he created between those two times.

    In the Bitcoin scripting language, there are two stacks: the instruction stack and the data stack. The instruction stack contains the actual script that needs to be executed. Instructions from the instruction stack is removed and executed one by one. Each instruction performs its task by manipulating the data stack. A walk through of a simple program is the easiest way to understand how a stack based language works. Below, we walk through an example program “OP_4 OP_6 OP_ADD”. “OP_4 OP_6 OP_ADD” contains three op codes (or instructions) and you can see in Figure A how they function. 
Data Stack
Instruction Stack
The entire script is loaded into the instruction stack. The first command to be executed is on the top of the stack, and the last command to be executed is on the bottom of the stack.
OP_4 is an instruction that says take the number 4 and put it on the data stack.”, thus OP_4 gets removed from the instruction stack and the number 4 gets put on the data stack.

OP_6 works the same way as OP_4, except it puts the number “6” on the data stack.
OP_ADD is an instruction that says remove the two topmost data on the data stack, add it together, and put the result on the data stack.
Figure A

    Walking through this simple example, we can see the reason that Satoshi chose a stack based language for the scripting. The advantage of a stack based language is that the state of the program is always clearly defined by the data stack. This makes it easy to spot potential errors in the implementation of the scripting language. It is incredibly important for the scripting language to be error free as bugs can threaten the security of the blockchain.

3. Basics of Bitcoin Transactions

    We will start by covering the basics of what a Bitcoin transaction looks like. This is necessary in order to understand the scripting language in Bitcoin, because the scripts are contained in the transactions. In a Bitcoin transaction, there is always at least one input and one output. Most transactions will contain multiple inputs and outputs. An input describes where the Bitcoins come from and an output describes where the Bitcoins will be spent.

    An input must describe where the Bitcoins come from by referring to the output of a previous transaction. This becomes obvious when you realize that any coins you want to send, must have been sent to you previously by another person. Furthermore, not only does an input need to refer to the output that it comes from, it needs to prove that the transaction creator has rights to access that specific output. This is done through a cryptographic signature derived from the private key that the transaction creator owns ( if this is confusing to you, please read the Bitcoin wiki on Bitcoin Addresses and Elliptic Curve Digital Signature Algorithm before proceeding).

    The output must describe how much Bitcoins are spent from the input and describe where the Bitcoins will be spent. The output commonly describes where the Bitcoins will be spent by referring to the Bitcoin address of the transaction receiver. Essentially, this allows us to link outputs from previous transactions to inputs of new transactions.
    Walking through a simple scenario will make this more clear. Referring to the below Figure B, we have a scenario where Bob wants to send 1 Bitcoin to Chris. Bob received 1 Bitcoin from Alice some time ago in transaction 1, and this is what he will send to Chris. Thus in Bob's transaction input, he must refer to the output in transaction 1 and also provide his signature using his private key. In his output, he will specify where the Bitcoins will be spent by using Chris's Bitcoin address.

Figure B

No comments:

Post a Comment