You are on page 1of 41

BLOCKCHAIN TECHNOLOGIES

MODULE-4 Presented by-


Dr. Subhalaxmi Chakraborty
BITCOIN CST & CSIT Department

LECTURE 2
MODULE-5

Blockchain 1.0: Need for Bitcoin, Commonly used terminologies: Mining, Block
frequency, Mining Pool, Block: Block Header, Hash, Markle root, Timestamp. SHA 256,
Bitcoin Mining and its types. Proof of Work, Mining Pool, Hashcash. Block
Propagation and Relay. Bitcoin scripting language and their use. Transaction in
Bitcoin.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 2


POW AND HASHCASH IN BITCOIN
There are two components of in mining in Bitcoin.
POW uses Hash and Nonce—
 Mining comprises of hashing a block and then introducing a nonce to the
hashing function.
 A nonce is a number that can be used just once in cryptographic
communication.
Adding a nonce to a transaction’s identifier makes it additionally unique,
reducing chance of duplicate transaction.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 3


DESCRIPTION OF BLOCK IN BLOCKCHAIN
Each block of transaction has three necessary components.
Each block consists of a block header and transaction data

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 4


Block Header

A Block Header is a unique number, consisting of 80 bytes, which identifies the


block.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 5


BLOCK HEADER
The block header is crucial in
blockchain and mining as each
block is cryptographically
chained to the previous block
using a hash of the previous
block header.
It contains the hash of the
previous block header,
timestamp, difficulty target,
nonce, Merkle Root and other
information such as version and
block size.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 6


The blockchain is designed in such a way that is constantly appending new blocks of
validated transactions to the end of its chain. The chain can only grow and blocks
cannot be removed or amended once confirmed and appended. Since every block
contains a unique hash value of the previous block, if an attacker tries to change
content of the previous block, its hash value will change. Thus, the attack will be
detected because all subsequent hash values will not match. Just as the hash of a
transaction reflects the information within the transaction and the Merkle Root reflects
the transactions included in a block, the hash value of the block header reflects all the
information in the block.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 7


WHAT IS A NONCE?
In cryptography, a nonce is an arbitrary number that can be used just once in a
cryptographic communication (Gordon and Jeffrey, 2004).

The “nonce” in a bitcoin block is a 32-bit (4-byte) field whose value is adjusted by
miners so that the hash of the block will be less than or equal to the current target of
the network.

we can see that among the information included in a block header, all the rest of
the data are given and can be easily obtained except for the nonce.
So, to successfully mine a new block is equivalent to finding a correct nonce that
makes the hash value of the block header equal to or smaller than the target.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 8


HASH FUNCTION
A hash function is considered equivalent of fingerprint of a data.
Using a fingerprint like a person can be identified, here a data is identified.
Reverse engineering cannot be performed using fingerprint, like we cannot draw a
person using his or her fingerprint.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 9


CHARACTERISTICS OF HASH ALGORITHM
Hash is produced from data but data cannot be produced from hash.
The same document produces same hash.
Fast computation is another characteristics of hash.
Avalanche effect is the key characteristics of the hash function. Any small change in
the input will lead to drastic changes in the hash output.
It must withstand collision. It means same hash will not be produced for two different
types of document .

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 10


POW
In Bitcoin’s Proof-of-Work mechanism, the blockchain that was created with the
greatest cumulative effort expended is the one considered to be the latest and to
which new blocks are attached.

the Proof-of-Work process is a competition among miners to find “the Golden Nonce”
that will result in a block fingerprint (i.e., the hash value of the block header) that
satisfies a given condition or meets a given target.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 11


TARGET AND MINING DIFFICULTY
In the bitcoin blockchain, the target is a 256-bit number that all bitcoin nodes share.
It is sometimes referred to as the difficulty target as it measures how difficult it is to
mine or generate a new block.

It is usually expressed in hexadecimal. An example of target is as follows:


0x0000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFF.

We often see the block hash represented as hexadecimal numbers (64 digits) which
result from SHA-256 hashing. (64 hexadecimal digits × 4 = 256 bits)

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 12


MERKLE TREE AND MARKLE ROOT
A merkle tree(Binary hash tree) involves taking large
amount of transactions data and constructed.
A Merkle root is created by hashing together pairs of
Transaction IDs , to a short unique finger print for all
transactions in a block.
Markle trees have nodes and leaves.
Original transactions present in th block acts as the
leaves of the Merkle tree. Nodes of the tree act as a
hash of leaves.
Moving up the tree, new nodes are hashes of lower
nodes. The process is repeated until the top of the tree
is reached.
The hash value at the top (peak) of the tree acts as a
hash of overall block.It is called Merkle root.
4/12/2023 DR. SUBHALAXMI CHAKRABORTY 13
Here, Tx0, Tx1, Tx2, Tx3 represent a typical transaction. These transactions are
hashed (Using SHA-256 in bitcoin) separately to get their corresponding hash
value(which are at the next higher level nodes).

The separate hash values are combined to get Hash01 and Hash23.

This process is repeated until Final hash is obtained(called as Merkle root)

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 14


HASHCASH
It is a cryptographic hash based proof of work algorithm to identify email spammer.
For Example, the number of seconds/ minutes of CPU time taken to write an email is
hashed and written in the header of the mail.
Let us consider, to write an email it takes a min time of 20 sec.
When an email is received, the hash value is checked and if the value is lesser than
20 seconds then the email is classified as Email spam.
Same logic is used in bitcoin also.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 15


Let us assume
00000000000000000a218b09b54600043489bc726c2a3580eb214c821d43
Let us say there are 100 transactions in total.
We pick a random number 1 (Non All these three things are sent to the hash function.
The hashcash within Bitcoin works the following inequality:
H(nonce || previous hash||txn || … || txN) <target space.
a.Nonce: Random number
b. PreviousHash: Hash of the previous block
c. Txn1... txnN: Txn is the transactions in the block.
d. Target space: Target space responds to the network's difficulty target (a small or
minuscule part of the output space of the hash function). The rule is that the hash
produced against the above values should be less than target space.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 16


I. Working: The above formula works with a rule of hashing where y= H(x) such
that there exists no x’: x' when the hash does not yield y. y=H(x) and y H=/ (x’).
II. The logic behind finding a nonce within a target space is to make computing
difficult so that the advantage due to hardware advancement is negated.
III. To find a nonce, a miner needs to attack the inequality with brute force, i.c., keep
trying until he succeeds. Every fortnight, the bitcoin raises the target space
requirement, thus making it harder for the miner to find a nonce.
IV. Target space is a tiny space, As we can see from Fig1. the SHA-256 hash
function is not yielding the right string for Nonce 1, as the resulting string starts
with only four zeros whereas a string starting with 18 zeros is required.
V. Therefore, we choose another random number and start another calculation.
VI. This time, let us choose number 2 as nonce. This time, we get a string (refer Fig.
4.14) that starts with exactly 18 zeros. We verify this block and if we are the
first ones to get this result, we will get a reward for that block.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 17


HASHCASH MAPPING EXAMPLE 1(FIG 1)

Hash of previous block Block of Transactions


Nonce

SHA-256
()

0000557C7295ADCCE7F1DB2C3FF2C719C2728F005E43D66ACDD577AEB0AD3D0
4/12/2023 DR. SUBHALAXMI CHAKRABORTY 18
HASHCASH MAPPING EXAMPLE 2 (FIG 2)

Hash of previous block Block of Transactions


Nonce 2

SHA-256
()

000000000000000000cebf9362fa4d73e7f8d0a5947d1cf1601625da6bdbfab6
4/12/2023 DR. SUBHALAXMI CHAKRABORTY 19
A nonce is a finite number with a 32-bit unsigned integer.
The maximum nonce=2^32 = 4 x 10^9
It has a value between zero and 4 billion (approximately).
Probability that one of them becomes valid =0.0000000001%.
A modest miner can create 100 million hashes per second. To traverse 4 billion
hashes, it takes just 40 seconds.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 20


NODES
Nodes in the network do the following:
a. Submit transaction to other nodes
b. Validate transaction from other nodes
c. Maintain the fullblock of the bitcoin network
d. Come up with a nonce for the block of transaction
e. Publish your block to the network
f. Hope other miners accept your block.
Of these steps, Steps b, c and d are collectively called mining for the network.
All nodes in the network could participate in mining and earn mining reward.
4/12/2023 DR. SUBHALAXMI CHAKRABORTY 21
TYPE OF NODES IN BITCOIN NETWORK
There are three types of nodes:
1)Full Node
2)Mining Node
3)SPV node

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 22


TYPE OF NODES IN BITCOIN NETWORK
There are three types of nodes:
1)Full Node-
These are the nodes that do all the activities (a to f) that are listed(in the prevous
slide)diligently( they accept transactions, validate transactions and relay them to further
full nodes).
This needs lots of computing and there is some chance of creating a block and
earning a mining reward A node that operates in the bitcoin network would have
node rights, all nodes in the network are equal, as stated by the Satola Nakamoto.
 Many people and organizations volunteer themselves to run full nodes using spare
computing and bandwidth resources their computer-but more volunteers are needed
to allow Bitcoin to continue to grow.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 23


TYPE OF NODES IN BITCOIN NETWORK
There are three types of nodes:
2)Mining nodes:
Mining nodes are set of nodes with mining capability but they do not hold the entire blockchain
3)SPV nodes:
This stands for "Simplified Payment Verification." The vast majority of nodes today are SPV nodes.
SPV nodes do not involve in mining however, they bound to validate the block wing block scripts,
this feature can be easily achieved e via handheld devices.
Bitcoin network, as of May 2019.
Total no of nodes in the network: 82,640
No. of full nodes in the network: 9,384

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 24


TRANSACTION IN BITCOIN NETWORK
Bit coin network is an open network.
Any node within the network can submit a transaction to the network.
Since it is a public network, ledger needs to be shared with all the nodes.
So there exist a large no. of nodes having a copy of ledger.So, no one can forge
the record.
A transaction consists of
Version No.
Inputs Version No. Inputs Outputs Lock time
Outputs
Lock time
A simple transaction block
4/12/2023 DR. SUBHALAXMI CHAKRABORTY 25
TRANSACTION IN BITCOIN NETWORK
Version No. Inputs Outputs Lock time
Transaction 0

Version No. Inputs Outputs Lock time


Transaction 1

A simple transaction block: Outputs as inputs

 Output of the previous transaction will be the inputs for the next transaction as shown.
 Transaction 0’s outputs are the inputs for transaction 1.
 Outputs indicate Unspent Transaction Output(UTXO) from the previous transaction.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 26


TX(INPUT) CREATION FROM PREVIOUS
TRANSACTIONS
Suppose a sender needs to pull out bitcoins from the following four transactions.

TX(0) These four transactions are added, yielding the


input transaction represented as TX(Input)

TX(1) TX(Input) TX(Input)=TX(0)+TX(1)+TX(2)+TX(3)

TX(2)

TX(3)

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 27


A SIMPLE TRANSACTION BLOCK: INPUTS,
OUTPUTS AND CHANGE
For a valid transaction,
TX(INPUT)>TX(OUTPUT)
TX(OUTPUT)
So, one can send bitcoin if this is
satisfied.
TX(INPUT)
If TX(INPUT)<TX(OUTPUT),
Change It would reject the transaction.
(Left over)

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 28


TRANSACTIONS AND UTXOS
UTXO stands for “Unspent Transaction Output”

Murrey sent money to Monika 0.6 BTC

Anan sent money to Monika 0.5BTC


UTXOs

Bob sent money to Monika 0.3BTC


These are called
unspent
Kate sent money to Monika 0.2BTC
transaction
output of
Monika

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 29


UTXOS AND TRANSACTIONS
Transactions (first 2) UTXOs

0.6BTC FROM MURRY TO 0.9BTC TOABC


MONIKA COMPANY

0.5 BTC FROM ANAN TO


0.2 BTC TO MONIKA
MONIKA

Now, after first 2 transactions monika decides to spend some money by buying some
commodity from ABC Motors.
So, the remaining BTC will be 0.2BTC , considered as UTXO

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 30


UTXOS AND TRANSACTIONS
Transactions (first 2) UTXOs

0.9BTC TOABC
0.6BTC FROM MURRY Murrey sent money to Monika
COMPANY
0.6 BTC

0.5BTC FROM ANAN 0.2 BTC TO MONIKA Anan sent money to Monika
0.5BTC
Bob sent money to Monika
Now, after first 2 transactions Monika decides to spend 0.3BTC
some money by buying some commodity from ABC
Motors. Kate sent money to Monika
0.2BTC
So, the remaining BTC will be 0.2BTC , considered as
UTXO Monika sent money to Monika
0.2BTC

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 31


FEES CALCULATION IN TRANSACTION
Transactions (second 2) UTXOs

0.3BTC FROM bob 0.4BTC TO Amazon Murrey sent money to Monika


0.6 BTC

0.2BTC FROM kate Anan sent money to Monika


0.5BTC
Bob sent money to Monika
Now, Monika wants to buy some products from Amazon 0.3BTC
for 0.4BTC.
Kate sent money to Monika
The above picture shows the transaction details. 0.2BTC
In the above transaction the input is 0.5BTC,FEES is Monika sent money to Monika
0.1BTC 0.2BTC

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 32


TYPES OF TRANSACTIONS IN BITCOIN
As the transaction is submitted, all nodes start validating
Zero confirmation transaction-
In this scenario, the user submits the transaction and the other party accepts the same.
There is no additional confirmation from other nodes.
One confirmation transaction
In this case, the receivers wait until at least one node has confirmed the transaction as
valid.
Six confirmation transaction
In this case, the receivers wait until at least six nodes have confirmed the transaction as
valid.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 33


BITCOIN SCRIPT
‘Forth’ – is the programming language used for writing bitcoin script.
It is having limited set of instructions(256).
Each one is represented by 1 byte.
The scripting language supports conditional statements; however no loop is allowed.
If statement and if else statements are supported.
Another necessary instruction is MULTISIG; this instruction is accessible on platforms
where validation of multiple nodes are mandated.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 34


COMMON CHARACTERISTICS OF BITCOIN SCRIPT
Simple to execute.
Lightweight(does not rely on other software packages)
Less computing energy.
Built for bitcoin
No loop
Uses Cryptography(hash, digital signature)
Validation of multiple nodes supported by (MULTISIG instruction)
Basic arithmetic operations like addition, subtraction, multiplication, division amd
logic blocks such as else if, throwing an error, returning function value

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 35


VARIOUS TYPES OF INSTRUCTIONS
Data Instruction-
If data instructions are encountered by bitcoin script, the data corresponding to the
particular instruction is simply hard pressed on the top of the stack.
Data instructions are given by <>.
Examples of Data instructions such as <sig>,<Pubkey>,<PubKeyHash>
<sig>- indicates signature
<Pubkey> indicates the public key used to verify the signature
<PubKeyHash> computes the cryptographic hash

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 36


Opcode Instruction:
Opcode instruction is used in a bitcoin script, to execute a particular function.
A. OP_DUP- This command duplicate the item on the top of the stack
B. OP_HASH160- It pops out the top value from the stack and compute
cryptographic hash 160 and then push the result in the stack.

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 37


DIFFERENT INSTRUCTION

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 38


BITCOIN SCRIPT EXAMPLE
<Sig>
<PubKey>
OP_DUP
OP_HASH160
<PubKeyHash>
OP_EQUALVERIFY
OP_CHECKSIG

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 39


BITCOIN SCRIPT EXAMPLE

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 40


BITCOIN SCRIPT (STACK BASED )

4/12/2023 DR. SUBHALAXMI CHAKRABORTY 41

You might also like