Muhammad Hammad Mechanics of Blockchain: Scripts & Block
Muhammad Hammad Mechanics of Blockchain: Scripts & Block
Muhammad Hammad Mechanics of Blockchain: Scripts & Block
99% of Bitcoin
Sample the Bitcoin transactions has this exact same
Script:
Bitcoin Script Features
There are total 256 opcodes
● Arithmetic Operations
● If/Then Conditions
● Logic/Data Handling
● Cryptographic Functions
○ Computation of Hashes
○ Signature Verification
○ Multi-signature Verification
Bitcoin Script opcodes
opcode Description
OP_CHECKSI Takes public key and signature and validates the signature of the hash
G of the transaction.
OP_DUP Duplicates the top item in the stack.
OP_EQUAL Returns 1 if inputs are exactly equal, returns 0 if otherwise.
OP_IF If top stack value is not False, the statements are executed. The top
stack value is removed.
OP_AND Boolean AND between each bit of the inputs
OP_SPLIT Split byte sequence x at position n
OP_DROP Removes the top stack item
OP_HASH160 The input is hashed twice: first with SHA-256 and then with RIPEMD-
160.
OP_DIV Input a is divided by input b
OP_MOD Return the remainder after input a is divided by input b
Bitcoin Script Execution
● Execution starts from left-to-right side of the script
● First step is to push the digital signature “<sig>” to
the stack
Bitcoin Script Execution
● Public key of the sender “<pubKey>” is also
pushed to stack
Bitcoin Script Execution
● “OP_DUP” operation pops an item from stack,
makes a duplicate of the item and then pushes it
back
Bitcoin Script Execution
● “OP_HASH160” takes the top value of stack,
computes its hash and pushes it back to the stack
Bitcoin Script Execution
● Public key hash, provided by the Bitcoin owner, is
pushed to stack
Bitcoin Script Execution
● “OP_EQUALVERIFY” operation takes top 2
values from stack and compares them
● If both are equal then a execution moves towards
next instruction
● If both are not equal then an error message is
generated and the execution is stopped
Bitcoin Script Execution
● “OP_CHECKSIG” verifies the digital signature of
the sender
● 2 required parameters to verify signature, i.e.
signature and public key, are popped from stack
and consumed for this operation
● Since signature is tied to the whole transaction, so
this operation verifies that the whole transaction is
valid or not
● If signature is verified then it means the transaction
Advanced Scripts
● Multi-signature
○ Escrow (Guarantor node)
○ Green Addresses (Guarantor bank)
● Micro-payments (Deliberate Double Spending)
● Multiplayer Lotteries (Complex lottery system)
● Hash pre-image challenge (Paid brute-force job)
● Coin swapping protocols
Mechanics of Blockchain: Blocks
● Blocks are files where data pertaining to the
Blockchain are permanently recorded.
● Each time a block is ‘completed’, it gives way to
the next block in the blockchain.
● A block is thus a permanent store of records which,
once written, cannot be altered or removed.
● A block is like a page of a ledger which bundles
multiple transactions together.
Why Bundle Transactions Together?
● Single unit of work for miners
● Shorten the length of the chain
● Less time to verify the chain
● Less amount of metadata
A Typical Blockchain Data Structure
Example Bitcoin Block
Example Bitcoin Block Header
Example Special Coin-Creation
Transaction
Real-Time Bitcoin Block View
Reference Book
Bitcoin and Cryptocurrency Technologies
Authors: Arvind Narayanan, Joseph Bonneau,
Edward Felten, Andrew Miller, Steven Goldfeder