0% found this document useful (0 votes)
21 views

Unit-II

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit-II

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Blockchain and Cryptocurrencies

– IOE411

Dr. Shajulin Benedict


[email protected]
Syllabus
• Introduction – blockchain history, basics, architectures, Types of
blockchain, Base technologies – dockers, docker compose, and
data structures, hashes, Digital Signature - ECDSA, Zero
Knowledge Proof.

• Bitcoins – Fundamentals, aspects of bitcoins, properties of


bitcoins, bitcoin transactions, bitcoin P2P networks, block
generation at bitcoins, consensus algorithms.

• Introduction to Ethereum- Consensus Mechanism -


Implementation, Transactions, demonstrations, Smart Contracts
(Blockchain hyperledger – Fabric architecture, implementation,
networking, fabric transactions, demonstration, smart contracts.)

• Applications – blockchain applications, e-governance, smart


cities, smart industries, anomaly detections, use cases, trends on
blockchains, serverless blocks, scalability issues, blockchain on
clouds.
Bitcoins
• Bitcoin is a collection of concepts and technologies
that form the basis of a digital money ecosystem.
• It is a unit of currency.
• The users can transfer bitcoin over the network.
• They can buy or sell goods using bitcoins.
• Bitcoins are entirely a virtual money.
• It is the transactions of value between sender and
recipient.
• Users of bitcoin own keys that allow them to prove
ownership of bitcoin in the bitcoin network.
• Keys are often stored in a digital wallet on each user’s
computer or smartphone.
Bitcoins
• Bitcoin is a cryptocurrency.
• It is not managed by a bank or agency.
• The transactions are recorded in a public blockchain.
• The records of each and every transactions are
entered into the ledger.
Bitcoins
• Bitcoin consists of
• A P2P network
• Public transaction ledger
• Set of rules for independent transaction validation.
• A mechanism for reaching the global decentralized consensus
on the valid blockchain.
• Bitcoins are created using a process called mining.
• Mining is the process of adding transactions to the large
distributed public ledger of existing transactions.
• Bitcoin is a protocol that can be accessed using a
client application.
• A bitcoin wallet is the most common user interface of
the bitcoin system (as like the browser).
Most Common bitcoin wallets
Satoshi client

Bitcoin core

Coinbase

Electrum

Blockchain.info

Exodus

Mycelium
https://www.thebalance.com/best-bitcoin-wallets-4160642
Categories of Bitcoin wallets – Based on
Platforms
• Desktop wallet
• Runs on general use OS such as Windows and Mac OS.
• Has a few security disadvantages.
• Mobile wallet
• Runs on smartphones such as Apple IoS or Android.
• Ease-of use, simplicity.
• Web wallet
• Runs through webbrowser and store the user wallet on a
server owned by third party.
• It is not advisable to store a large volume of bitcoins on web
wallet.
Categories of Bitcoin wallets – Based on
Platforms
• Hardware wallet
• These are devices that operate a secure self-contained bitcoin
wallet on special-purpose hardware.
• Paper wallet
• The keys controlling bitcoin can also be printed for long-term
storage. These are known as paper wallets.
Categories of Bitcoin wallets – Based on
Degree of Autonomy
• Full node wallet
• It is a client that stores the entire history of bitcoin
transactions.
• A full node handles all aspects of the protocols.
• Lightweight wallet
• A lightweight client (also known as simple-payment-
verification client) connects to bitcoin full nodes for access to
the bitcoin transaction information.
• Third-party API client
• Here, a third party API client interacts with bitcoin through a
third-party system (i.e, the client will not be connected with the
bitcoin network directly)
How to get bitcoins?
• Buy bitcoins online
• Eg. coinbase
• Buy bitcoins in person
• Use websites like Coin ATM Radar
• Use websites like LibertyX
• Use websites like LocalBitcoins
• Get bitcoins for free
• Play mobile or online games
• you can join a bitcoin casino
• Do online jobs to earn bitcoins (e.g., Bitcoinget, Bitfortip)
• Read classic books to earn bitcoins (e.g, Bitcoin Aliens,
PaidBooks)
• Write about cryptocurrency to earn bitcoins (E.g, Bitcointalk,
Coinality).
https://blog.hubspot.com/marketing/how-to-get-bitcoins
Who sets the bitcoin price?
• Bitcoin is like a commodity.
• Its price is determined by the market in which it
trades.
• i.e. the price of bitcoin is determined by how much someone
is willing to pay for that bitcoin.
• When the currency was first launched, it had no official
price at all, because no one was selling it for US
dollars.
• It started small, at around 6 cents, and didn’t hit a full
dollar until around February 2011.
• It started climbing rapidly, reaching over $140 that
April, and then topping $1,000 by December of 2013.
Current Status…

https://www.coindesk.com/price/bitcoin
BitCoins
• Money transfer (Centralized)
• Decentralized issues
• Double spending

• What is bitcoin?
• Public ledger.
• Issues to consider:
– Tamper-proof
– Size of ledger
– Maintain anonymity
Bitcoin Transactions
• Transactions are the most important part of the bitcoin
system.
• Transactions are data structures that encode the
transfer of value between participants in the bitcoin
system.
• Each transaction is a public entry in a bitcoin system.
What they contain? See via.Blockchain explorer

Input=output

https://www.blockchain.com/btc/tx/06decc8f081ef6323cfd53835f798edc0d7afcfeebd8dbc1158817ce1066b281
https://live.blockcypher.com/btc/tx/06decc8f081ef6323cfd53835f798edc0d7afcfeebd8dbc1158817ce1066b281/
Transaction Inputs/Outputs
• Every transaction outputs are indivisible chunks of
bitcoin currency that are recorded in a public
blockchain and recognized as valid by the entire
network.
• As like USD has two decimal points to represent
cents, bitcoins have 8 decimal points to represent
BTC.
• Each wallet has Unspent Transaction Outputs
(UTXO).
• Transaction outputs consists of two parts:
• An amount of bitcoin.
• Cryptographic addresses
• Each transactions have two outputs:
• To modify your wallet
• To modify others wallet
Transactions – contd…
• Transactions include transaction fees
• Fees (or incentives or rewards) compensate
– the bitcoin miners for securing the network.
– The security software to protect attackers (who could flood the
network with unnecessary transactions).
• Fees are not included in the transaction datastructure.
However,
– Fees = Sum(inputs) – Sum(outputs)
• A new block containing transactions is mined every 10
minutes on average (before adding into the ledger).
• Transactions include status information
• Confirmed (validated by miners)
• Unconfirmed transaction (not yet validated)
Outcome of transactions…
• If person X has 1 BTC and transfers 0.25BTC to
person Y (who has 0.25BTC in his wallet).
• To confirm the transaction is valid, the Bitcoin network
will “ask” the full nodes to confirm
• if person X has enough coins and
• how much person Y will have after the transaction.
• Once valid, the transaction will be evaluated by miners
to add the information to a block.
Keys and Addresses
• Bitcoin is based on cryptography (i.e., secret writing).
• Ownership of bitcoin is established through

Digital
keys

Digital Bitcoin
signatures addresses
Digital Keys
• Digital keys are generated and managed by the user’s
wallet software.
• Keys are represented in pairs (private and public key).
• Analogous example
• Public key – similar to bank account number
• Private key – secret PIN number
• Bitcoin address – Pay to (address to whom you need to make
the transaction).
Public key cryptography
• It was invented in 1970.
• They are mathematical functions
• They are practically irreversible (ie., easy to calculate in one
direction; not possible to calculate in the opposite direction).
• In general, keys are utilized to provide encryption.
Plaintext + key = ciphertext:
hello + 2jd8932kd8 = X5xJCSycg14=

• The traditional method was a symmetric approach


(encrypting messages using one key).
• But, sharing the keys are complicated (or, insecure) in
the symmetric approach.
Public key cryptography
• Bitcoins uses public key cryptography to create a key
pair to get access to bitcoin network.
• In this approach, two keys are utilized
• public key is used for encryption and signature verification
• the private key is used for decryption and signing.
Public key cryptography

• In a public-key
encryption system, anyone
with a public key can encrypt a
message (Wikipedia).
• But, only those who know the
corresponding private key can
decrypt the ciphertext to obtain
the original message.
• Hence, it is named as
asymmetric encryption
technique.
Public key cryptography
• A bitcoin wallet will contain a collection of key pairs
(i.e., private and public key).
Bitcoin P2P network
• Bitcoin is structured as a P2P network architecture on
top of the internet.
• Bitcoin P2P network consists of computing nodes.
• i.e., A bitcoin network refers to the collection of nodes
running the bitcoin P2P protocol.
• Ie. There is no centralized service, centralized server,
or hierarchy within a network.
Bitcoin Nodes and Functions
• A blockchain node is responsible for a collection of
functions such as
• Routing, Peer discovery
• Blockchain database, initial block download
• Mining
• Wallet services
• Types of nodes (as similar to wallets) in a bitcoin
network
• Full node
– It maintains a complete copy of the blockchain (MBs)
• Lightweight nodes 0r SPV nodes
– Some nodes maintain only a subset of the blockchain and verify
transactions using the Simplified Payment Verification (SPV).
• Mining nodes
– They compete to create new blocks by running Proof of Work
algorithm.
Bitcoin nodes and functions
Block Generation at Bitcoins
• What is a blockchain?
• Blockchain is a data structure.
• It is an ordered, back-linked list of blocks of transactions.
(NOTE: The transactions are ordered…)
• What is a block?
• A block is a bunch of transactions that have been added to a
blockchain.
• As of Feb. 22, 2024, there are 831639 blocks.
• A blockchain can be stored as a flat file or in a simple
database.
• A blockchain is visualized as a vertical stack …
• with blocks layered on top of each other, and
• The first block serves as the foundation of the stack.
How are blocks formed?
• When you make a bitcoin transaction, it isn’t added to
the blockchain straight away. Instead, it is held in a
transaction pool (or memory pool).
How are blocks formed?
• If you are a miner, your job is to gather transactions
from the transaction pool in to a “candidate block”, and
to try and add this candidate block to the blockchain.
Structure of a block
• A block is a container data structure that aggregates
transactions for inclusion in the public ledger.
• The block header is 80 bytes.
• An average transaction is atleast 250 bytes.
• An average block contains more than 500
transactions.
• A complete block, with all transactions is over 1000
times larger than the block header.
Structure of a block

• The block header consists of three pieces of


information in the block metadata.
• First, there is a reference to the previous block in the
blockchain using SHA256 (Secure Hash Algorithm).
• Second, it describes the difficulty, timestamp, and nonce (a
counter used for the Proof-of-Work)
• Third, merkle tree root ( a data structure used to efficiently
summarize all the transactions in the block.
https://www.movable-type.co.uk/scripts/sha256.html
Merkle tree
• A merkle tree is a data structure used for efficiently
summarizing and verifying the integrity of large sets of
data.
• Merkle trees are created by repeatedly hashing pairs
of nodes until there is only one hash left (this hash is
called the Root Hash, or the Merkle Root).
• Experiment with online hashing tool:
https://www.movable-type.co.uk/scripts/sha256.html
• Merkle tree is a binary hash tree.
• It has its root at the top.
• A Merkle tree summarizes all the transactions in a
block by producing a digital fingerprint of the entire set
of transactions, thereby enabling a user to verify
whether or not a transaction is included in a block.
https://hackernoon.com/merkle-trees-181cb4bc30b4
Merkle Tree
Merkle tree -- Advantages

• They provide a means to prove


the integrity and validity of
data.
• They require little memory or
disk space as the proofs are
Merkle trees computationally easy and fast.
have three • Their proofs and management
only require tiny amounts of
major information to be transmitted
across networks
benefits:
Hashing
• SHA (secure hashing algorithm) is utilized in merkle
trees.
• It is a cryptographic technique developed by National
Security Agency (NSA).
• Now, it is standardized by NIST (National Institute of
Standards and Technology).
• A hash function is an irreversible mathematical
function (in design) that takes the input from data and
provides a fixed length hash value.
• Note: Encryption techniques are reversible. But,
hashes are not reversible.
Hash value
Data (any length) Hash function
(Fixed size)
Hashing
• In internet-based applications, hashing is quite often
utilized to store passwords in servers or public
databases.
• Here, the size will be fixed. And, if you type wrong password,
the hashes will be verified and the access will be denied.
• Similarly, hashes are utilized to verify the integrity of
files (i.e. file corruptions?).
• For e.g., when you download some container images or
ubuntu images (large sizes files), hashes are utilized to verify
the integrity of such files.
Hashing

The goals of a hash function


• It has to be impossible for the attacker to develop a message that
matches the hash value.
• i.e., given a hash value, the corresponding input should not be
matched (as the hash function is an irreversible function).
• It should not develop two hash values for the same input files.
Hashing

• The length of original message


should be less than 2^64 bits.
• The length of the digest should
be 160 bits in length.
SHA-256
• The digest should not be able to
Characteristics produce the original message.
• Iterating hashes should produce
new digests (i.e., hashing a
hash value of an input)
Hashing

• Step 1 – Padding 0s (64-bit – in the multiple of 512)


• Step 2 – Include padding length (i.e., how many 512
bits are added?)

Message Padding bits Padding Length

Data (To Hash)


Hashing
• Step 3: Break the entire message into 512 blocks.
• Step 4: Separate 512 blocks into 16 sub blocks.
• Step 5: 5 buffers of 32-bits are chosen.
• Step 6: Name it as A, B, C, D, E.
• Step 7: Iterations – Perform some mathematical
operations for 80 times (i.e., 4 rounds (each round with
20 iterations)
• Each round performs E+Process P + S5(A)+W(t)+K(t)
– E is a buffer
– P is a logical formula for each round.
– S5 is a circular shift by 5 bits
– W(t) is a 32-bit string derived from existing sub block
– K(t) is a constant (changes for every round).
Hashing – Each round
• Let 5 buffers be as follows

• Shuffle buffers as follows:


Hashing – Each round
• Use a non-linear process P to use B, C, D and add the
values to E register.
Hashing – Each round
• Circular shift and add it to the register
Hashing – Each round
• Add W(t) and K(t) and store it in A

• Repeat the process for 80 rounds.


Block Identifiers

Block Header Block height


hash • The second way
• Each block’s to identify a block
header is hashed is by its position
using SHA256 in the blockchain
(TWICE). – named as block
• It serves as an height.
identifier for the • The genesis block
block in a has block height
blockchain. “0”
Genesis Block
• It is the first block in a blockchain.
• On Jan. 3, 2009, an anonymous developer called
Satoshi Nakamoto made history when he released the
Genesis Block, the original block containing the first
50 bitcoins, onto Sourceforge.

https://www.investopedia.com/news/what-genesis-block-bitcoin-terms/
Mining and Consensus
• Bitcoin’s consensus is a decentralized mechanism.
• 4 processes are involved :
• Independent verification of each transactions (by every full
node).
• Independent aggregation of those transactions into new
blocks (by mining nodes)
• Independent verification of new blocks (by all nodes) and
assembly into the chain.
• Independent selection of the chain with the most cumulative
computation demonstrated through Proof-of-Work.
Independent Verification of Transactions
The transaction’s syntax and data structure

Lists of inputs and outputs – Are they empty?

Is the transaction size exceeds MAX_BLOCK_SIZE?

Whether input hash has value 0?

Is the transaction size in bytes is greater than 100


bytes? Eg. BitcoinCore has

Is the sum of input value is less than the sum of Functions such as
output values?
i) AcceptToMemoryPool,

Is the transaction fee is very low?


ii) CheckTransaction,
iii) and CheckInputs
….
Independent aggregation of transactions into
new blocks
• Mining nodes do it.
• Miners are
• Keen to listen for new blocks (that are propagated on the
bitcoin network).
• The way Bitcoin makes sure there is only one block chain is
by making blocks really hard to produce.
• So instead of just being able to make blocks at will, miners
have to compute a cryptographic hash of the block that meets
certain criteria. Bitcoiners refer to this process as "hashing".
• There will be competition among similar miners in the network
(who creates the block at FIRST?).
• The competition ends when the successful miner propagates
the new block after winning Proof-of-Work.
• COLLECTS → VALIDATES → RELAYS NEW
Transactions
Mining and Consensus
• Mining is a mechanism in which transactions are
validated and cleared.
• Miners validate new transactions and record them on
the global ledger.
• Mining is a process of hashing the block header
repeatedly (changing each parameters) until the
resulting hash matches a specific target.
• Only way is to try, try, try….. (as like lottery!)
Mining and Consensus (Repetition)
• Miners receive two types of rewards:
• Eligibility to create new coins to the blockchain.
• Transaction fees
• To earn rewards, miners compete to solve a difficult
mathematical problem based on cryptographic hash
algorithm. – The solution to the problem is called
Proof-of-Work.
Exploring bitcoin-cli…
• https://chainquery.com/bitcoin-cli/getblockhash
• Bitcoin-cli get blockhash 277316

• Some commands (via. online)


• https://chainquery.com/bitcoin-cli#blockchain
• Getblockhash
• Getblockheight
• Getblockheader
• Getblock
• Previousblock

• bitcoin-cli getmininginfo
PoW – Puzzle – the Mathematical Problem
• Why the mathematical problem should be difficult to
solve?
• If anyone could produce as many Bitcoin as they wanted at
anytime, Bitcoin would be worthless...
• The Bitcoin algorithm releases some Bitcoin to a winning
member of its network every 10 minutes, with a maximum
supply to be reached in about 122 years (i.e., around year
2131).
• This release schedule also controls inflation to a certain
extent, since the entire fixed supply isn’t released at the
beginning.
Proof of Work -- Math
• One-way cryptography takes in a human readable
input like “Hello world” and applies a function to it (i.e.
the math problem) to produce an indecipherable
output (verifiable output).
• These functions (or algorithms) vary in nature and
complexity.
• The more complicated the algorithm, the harder it is to
reverse engineer.
• Eg. SHA-256
• http://www.xorbin.com/tools/sha256-hash-calculator
Cryptographic Hashes - Math
• Try hashing “Hello world” over and over again. You get
the same hash every time.
• A fundamental property of cryptographic algorithms is
that they should be extremely hard to reverse engineer
to find the input, but extremely easy to verify the
output.

https://medium.com/@mycoralhealth/code-your-own-blockchain-mining-algorithm-
in-go-82c6a71aba1f
What is PoW?
• A proof of work is a consensus algorithm in which it’s
costly and time-consuming to produce a piece of data,
but it’s easy for others to verify that the data is correct.
• How complex a puzzle is depends on
• the number of users,
• the current power and
• the network load.

https://cointelegraph.com/explained/proof-of-work-explained
PoW systems
• 2 major systems apply PoW
• Bitcoin
• Ethereum
• The puzzle applied in the PoW of bitcoin is called as
hashcash puzzle.
• Hashcash algorithm allows changing the complexity of
a puzzle based on the total power of the network
• i.e., it maintains the avg block formation to 10 minutes.
Disadvantages of PoW
• PoW consensus algorithm has several drawbacks
• Useless Puzzle or mathematical problem (Miners do a lot of
work to generate blocks and consume a lot of power.)
• Huge expenditures. Mining requires highly specialized
computer hardware to run the complicated algorithms.
• 51% Attack: A 51 percent attack, or majority attack, is a case
when a user or a group of users control the majority of mining
power.
– The attackers get enough power to control most events in the
network.
– They can monopolize generating new blocks and receive rewards
since they’re able to prevent other miners from completing
blocks.

You might also like