Bitcoin – Script Processing

Bitcoin developer has selected Forth programming language to write code. The code consists of the sender’s public key, signature, operations. Every transaction has a minimum of two codes, the first code is called ScriptSig, and the second one is called ScriptPubKey.

  • ScriptSig: It contains the public key and signature of the sender.
  • ScriptPubKey: It contains operations code, sender bitcoin address, and other data.

The Forth is stack-based, procedural, without type checking, and it uses reverse polish notation. It is not Turing complete Programming Language as it does not support looping (No halting problem). The selection of this language may be because of security reasons as it supports minimal functionalities that were sufficient to perform the necessary operations in the bitcoin ecosystem.

Understand the script with an Example

Step 1: Combine these two scripts together that looks as follows:

Combining the script together - bitcoin
Step 1: Combining the scripts together

Step 2: Push the operands such as signature and public key of Alice into the stack.

Push signature and public key into stack.
Step 2: Push Signature and Public Key into Stack.

Step 3: OP_DUP duplicates top operator inside the stack.

Duplicate top operator - bitcoin
Step 3: Duplicate top operator

Step 4: Pop the top operator and generate 160-bit hash and again push into stack.

Hashing - bitcoin to generate bitcoin address
Step 4: Generate bitcoin address from public key.

Step 5: Push the operand into stack.

Push the operand into stack
Step 5: Push the operand into stack.

Step 6: Pop top two operand and perform the operation.

Step 6: Compare Alice’s bitcoin address with generated address from public key

Step 7: Check the signature of Alice.

Check signature of alice - notepub
Step 7: Check signature of Alice.

The above example just illustrated how the Forth language works and how the bitcoin ecosystem executes the script to perform validation of transactions. There are many opcodes that are supported by bitcoin architecture.

Operations supported

Summary

This note shows how to write a script and how the system processes a script to perform the transaction validation and execution.

References:

  • NPTEL lecture series on Blockchains Architecture, Design and Use Cases by Prof. Sandip Chakraborty, IIT Kharagpur.

 170 total views,  1 views today

Scroll to Top
Scroll to Top