unit-i-introduction-to-blockchain
unit-i-introduction-to-blockchain
Blockchain
Blockchain is a decentralized and distributed ledger technology that records transactions across
multiple computers in a secure, immutable, and transparent manner. Unlike traditional centralized
systems, blockchain eliminates the need for a trusted intermediary, relying instead on cryptographic
methods and consensus mechanisms.
The key elements of Blockchain are the foundational components that enable its functionality and
distinct characteristics. Here's a breakdown:
1. Distributed Ledger
¥ A shared database where all participants have access to the same version of data.
¥ Each transaction is recorded only once, eliminating duplication.
¥ Example: In Bitcoin, every node has a complete copy of the blockchain.
2. Blocks
¥ The basic units of a blockchain, containing:
◦ Data: Information about the transaction (e.g., sender, receiver, amount).
◦ Hash: A unique identiÞer for the block, generated using cryptography.
◦ Previous BlockÕs Hash: Links the current block to the previous one, forming a
chain.
3. Cryptography
¥ Blockchain uses cryptographic techniques to secure data and ensure its integrity.
¥ Hash Functions: Algorithms like SHA-256 create Þxed-size output (hash) from input data.
¥ Digital Signatures: Verify the authenticity of transactions and participants.
4. Consensus Mechanism
¥ The process used to agree on the validity of transactions.
¥ Ensures trust in a decentralized network without a central authority.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 1 of 78
6. Immutability
¥ Once data is recorded in a block and added to the chain, it cannot be altered.
¥ Ensured by cryptographic hashing and the consensus mechanism.
¥ Provides a tamper-proof record of transactions.
7. Smart Contracts
¥ Self-executing programs stored on the blockchain that automatically enforce agreements.
¥ Triggered when predeÞned conditions are met.
¥ Example: Releasing funds only when goods are delivered.
8. Tokenization
¥ The process of converting assets into digital tokens on a blockchain.
¥ Tokens can represent ownership, value, or access rights.
¥ Example: Cryptocurrencies, Non-Fungible Tokens (NFTs).
9. Timestamping
¥ Every transaction on the blockchain is time-stamped, providing an accurate and
chronological order of events.
¥ Ensures data authenticity and enables traceability.
10. Nodes
¥ Computers or devices that participate in the blockchain network.
¥ Types of nodes:
◦ Full Nodes: Maintain a complete copy of the blockchain.
◦ Lightweight Nodes: Rely on full nodes for transaction validation.
11. Decentralization
¥ Removes the need for a central authority, distributing control among participants.
¥ Increases system resilience and reduces the risk of corruption or failure.
14. Forks
¥ A fork occurs when thereÕs a divergence in the blockchain, leading to multiple versions.
¥ Types of forks:
◦ Soft Fork: Backward-compatible updates.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 2 of 78
1. Block Header
The header contains metadata about the block, which is essential for identifying, validating, and
linking blocks together. It includes the following:
¥ Version:
◦ Defines the protocol version used for the current block.
◦ Allows the blockchain protocol to be updated over time (backward compatibility).
◦ Example: Bitcoin uses version numbers (e.g., 0x20000000) to signify changes in the
protocol.
¥ Merkle Root:
◦ The root of a Merkle tree that represents a cryptographic summary of all transactions
in the block.
◦ ItÕs a hash value that ensures integrity by providing a compact representation of the
block's transactions.
◦ Allows efficient and secure verification of transactions without downloading the
entire block.
◦ Example: A Merkle Root could be something like:
f59c699acfd9c9c9ec2c6d22acfa989d44cda9067959ffdf3d5d9703ab7a1d9.
¥ Timestamp:
◦ The time the block was created, typically in seconds from the Unix epoch (1970).
◦ This is important for maintaining the chronological order of blocks in the chain.
◦ Example: 1609459200 represents a timestamp corresponding to January 1, 2021.
¥ Difficulty Target:
◦ Represents the target value that the blockÕs hash must be lower than to be considered
valid.
◦ The difficulty level adjusts over time to ensure that blocks are mined at a steady rate
(e.g., every 10 minutes in Bitcoin).
◦ Example: In Bitcoin, this could be represented by a value like
0000000000000000000ffff.
¥ Nonce:
◦ A random number that miners alter in order to find a valid hash that meets the
difficulty target.
◦ In Proof of Work (PoW), miners iterate different nonce values to discover a hash
below the target, thus solving the puzzle.
◦ Example: 1762347 is an example nonce used to mine a block in Bitcoin.
2. Block Body
The body of the block contains the actual data, specifically the transactions, and it is the part that
miners validate. This data typically includes the following:
¥ Transactions:
◦ A list of all transactions included in the block.
◦ Each transaction includes the following details:
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 4 of 78
◦ Transaction IDs:
▪ Each transaction has a unique identifier, calculated by hashing the transaction
data.
▪ Example: A transaction ID could look like:
e3c7df6bc3b062b41f4e2c1efbcfa4a5d77b9d47bb6c000d1b56a5fd4d05dca3.
1. Decentralization
¥ Blockchain eliminates the need for a central authority or intermediary by distributing data
across a network of nodes.
¥ Each participant (node) has access to the entire ledger, ensuring transparency and reducing
dependency on a single entity.
¥ Example: Bitcoin transactions occur directly between parties without involving banks.
2. Transparency
¥ All transactions recorded on the blockchain are visible to participants, ensuring
accountability.
¥ Public blockchains, like Ethereum, allow anyone to verify transaction details while
maintaining user anonymity.
3. Immutability
¥ Once a transaction is recorded on the blockchain, it cannot be altered or deleted.
¥ This feature ensures the integrity of data, making blockchain resistant to fraud and
tampering.
¥ Changes, if necessary, require consensus from the network, maintaining security.
4. Security
¥ Transactions are encrypted using advanced cryptographic algorithms, making them highly
secure.
¥ Features like cryptographic hashing, digital signatures, and consensus mechanisms protect
the network against unauthorized access and attacks.
¥ Example: SHA-256 hash function used in Bitcoin ensures data security.
5. Distributed Ledger
¥ Blockchain is a shared ledger distributed across a network of nodes, ensuring redundancy
and eliminating single points of failure.
¥ Every node maintains a synchronized copy of the ledger, which updates in real time with
new transactions.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 6 of 78
6. Consensus Mechanisms
¥ Blockchain relies on consensus mechanisms to validate and agree on the state of the ledger.
¥ Popular consensus algorithms include:
◦ Proof of Work (PoW): Used in Bitcoin.
◦ Proof of Stake (PoS): Used in Ethereum 2.0.
◦ Delegated Proof of Stake (DPoS) and Byzantine Fault Tolerance (BFT) for faster
validation.
8. Traceability
¥ Every transaction is time-stamped and recorded in a chronological order, creating a
complete history of activities.
¥ This is particularly useful in supply chain management, where products can be tracked from
origin to destination.
9. Smart Contracts
¥ A smart contract is a self-executing program with predeÞned conditions that automatically
enforces agreements.
¥ These contracts eliminate the need for intermediaries, reducing costs and enhancing
efÞciency.
¥ Example: Escrow services for digital payments using Ethereum.
10. Programmability
¥ Blockchain can be programmed to execute custom operations, such as triggering payments
or notiÞcations based on certain conditions (via smart contracts).
¥ This feature enables the creation of decentralized applications (DApps).
12. Tokenization
¥ Blockchain allows the creation of digital assets or tokens representing ownership, rights, or
value.
¥ Tokens can represent physical assets (e.g., real estate), digital assets (e.g., NFTs), or access
rights.
Type of
Description Access Control Use Cases Examples
Blockchain
Fully decentralized network - Cryptocurrencies
where anyone can join, read, (Bitcoin,
Public Open to anyone; - Bitcoin
write, and validate Ethereum).
Blockchain no restrictions. - Ethereum
transactions. Transactions - Decentralized
are visible to all participants. Þnance (DeFi).
Centralized network
- Supply chain
controlled by a single Limited to
Private management. - Hyperledger
organization. Access is authorized
Blockchain - Internal enterprise - Quorum
restricted to selected users.
operations.
participants.
Partially decentralized - Interbank
Consortium network managed by a group Restricted to payment systems. - R3 Corda
(Federated) of organizations. Pre- member - Collaborative - Energy Web
Blockchain selected participants validate organizations. enterprise Chain
transactions. applications.
Combines elements of public - Healthcare
and private blockchains. systems.
Hybrid Access control
Some data is public, while - Government - Dragonchain
Blockchain is customizable.
sensitive information is records (e.g., voting
restricted. systems).
A blockchain connected to a
main chain, used for speciÞc - Scalability - Liquid
Dependent on
purposes or scalability. solutions. Network
Sidechain the main chainÕs
Assets can move between - Gaming - Loom
conÞguration.
the main chain and applications. Network
sidechain.
Controlled by a single - Banking systems
organization but allows Restricted; for external audits. - Ripple
Semiprivate
speciÞc authorized limited public - Government and (partially
Blockchains
participants for external participation. NGO semiprivate)
collaboration. collaborations.
Blockchain where
participants need permission Restricted; only - Enterprise supply
Permissione - Hyperledger
to join or validate approved chains.
d Ledger Fabric
transactions. Provides participants. - Financial services.
controlled transparency.
A distributed ledger shared - Distributed data
Depends on
Shared across multiple participants storage.
conÞguration - MultiChain
Ledger in a network, promoting - Collaborative
(public/private).
collaboration. applications.
Fully Controlled and operated - Internal record- - IBM
Fully restricted
Private and entirely by a single entity. keeping. Blockchain
to the
Proprietary No external access or - Compliance (private
organization.
Blockchains participation allowed. tracking. solutions)
Blockchains that use tokens Open or
- Cryptocurrencies.
Tokenized or cryptocurrencies to restricted based - Bitcoin,
- Decentralized
Blockchains incentivize transactions and on blockchain Ethereum
Þnance (DeFi).
provide economic value. type.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 8 of 78
The detailed comparison between Distributed System Architecture and Centralized System
Architecture in the context of blockchain were,
Feature Distributed System (Blockchain) Centralized System
Decentralized control; no single Centralized control; one entity has
Control
authority manages the system. full control over the system.
Data is distributed across multiple Data is stored in a single location or
Data Storage
nodes in the network. server.
No single point of failure; the system A single point of failure exists; if the
Single Point of
remains operational even if some central server fails, the system goes
Failure
nodes fail. down.
High security due to cryptography and Vulnerable to breaches if the central
Security
consensus mechanisms. server is compromised.
Ensured through consensus protocols Depends on the central authority to
Data Integrity
and immutability of blocks. maintain data integrity.
Transactions are transparent and Transparency is limited to what the
Transparency
visible to all participants. central authority allows.
Layers of Blockchain
Blockchain technology is structured into multiple layers, each with a distinct role in ensuring the
security, decentralization, and efÞciency of the system. Below is a detailed breakdown of the
layers of blockchain:
Implementation of Blockchain
Blockchain technology provides a decentralized and secure method of recording
transactions. Implementing a blockchain network involves selecting the right platform, setting up
nodes, developing smart contracts, and ensuring security and scalability.
B. Consensus Mechanisms
Consensus algorithms ensure agreement among participants about the state of the blockchain.
1. Proof of Work (PoW): Used in Bitcoin, requiring miners to solve complex mathematical
puzzles.
2. Proof of Stake (PoS): Validators are chosen based on the number of coins they hold.
3. Delegated Proof of Stake (DPoS): Users vote for delegates who validate transactions.
4. Practical Byzantine Fault Tolerance (PBFT): Used in Hyperledger Fabric for fast and
efÞcient consensus.
import hashlib
import time
def proof_of_work(last_proof):
proof = 0
while not valid_proof(last_proof, proof):
proof += 1
return proof
start = time.time()
new_proof = proof_of_work(100)
end = time.time()
C. Smart Contracts
Smart contracts are self-executing programs stored on the blockchain that automatically execute
transactions when predeÞned conditions are met.
Example of an Ethereum smart contract in Solidity:
// SPDX-License-IdentiÞer: MIT
pragma solidity ^0.8.0;
contract SimpleContract {
uint256 public count;
class PeerNode:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.peers = []
# Example usage
node1 = PeerNode("192.168.1.1", 5001)
node2 = PeerNode("192.168.1.2", 5002)
node1.add_peer(node2.ip, node2.port)
package main
import (
"fmt"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
./network.sh createChannel
Step 5: Test for Scalability, Security, and Functionality
¥ Perform load testing to check the scalability of the blockchain.
¥ Simulate attacks to assess security vulnerabilities.
¥ Validate transactions using blockchain explorers.
Example: Querying Hyperledger Fabric
peer chaincode query -C mychannel -n mychaincode -c '{"Args":["queryFunction", "arg1"]}'
Advantages of Blockchain
1. Transparency: Public visibility of transactions builds trust.
2. Security: Immutable records reduce fraud and unauthorized access.
3. Decentralization: Eliminates single points of failure.
4. EfÞciency: Reduces time and costs associated with intermediaries.
5. Automation: Smart contracts streamline processes.
Disadvantages of Blockchain
1. Energy-Intensive: Proof of Work systems consume large amounts of energy.
2. Scalability Issues: Limited transaction speeds and high costs in public blockchains.
3. Complexity: Implementing blockchain solutions requires specialized knowledge.
4. Regulatory Uncertainty: Changing policies can impact blockchain adoption.
5. Data Privacy: Public blockchains expose transaction details, posing privacy concerns.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 17 of 78
1. Describing Ownership
¥ Ownership in a P2P ledger is deÞned through cryptographic keys:
◦ Public Key: IdentiÞes the owner publicly.
◦ Private Key: Grants control and authority over the assets or data associated with the
public key.
¥ Transactions in the ledger explicitly transfer ownership from one public key to another,
ensuring that ownership is clear and traceable.
2. Protecting Ownership
¥ Ownership protection is achieved using cryptographic techniques:
◦ Digital Signatures: Owners sign transactions using their private key to authorize
actions. This ensures authenticity and prevents forgery.
◦ Immutability: Once a transaction is added to the ledger, it cannot be altered,
safeguarding ownership records from tampering.
◦ Decentralization: Multiple copies of the ledger are distributed across the network,
making it nearly impossible for malicious actors to alter ownership details without
detection.
◦ Nodes independently verify the data before updating their copy, ensuring consistency
across the network.
3. Security
¥ P2P networks leverage cryptographic techniques and consensus mechanisms to ensure data
integrity and prevent unauthorized access.
¥ In a decentralized system, there is no single point of failure, making it highly resistant to
hacking and Distributed Denial-of-Service (DDoS) attacks.
¥ Traditional Systems: Centralized databases are prime targets for attackers, as
compromising the central server exposes the entire network.
5. Elimination of Intermediaries
¥ P2P networks enable direct transactions between participants without requiring a
middleman.
¥ This reduces costs, increases efÞciency, and avoids delays caused by intermediaries.
¥ Example: In Bitcoin, users can send payments directly without involving banks or payment
processors.
6. Scalability
¥ P2P networks can scale more effectively as new nodes join the network, increasing overall
computational power and storage capacity.
¥ Traditional Systems: Scaling requires costly infrastructure upgrades to a centralized server,
which has physical and performance limitations.
7. Cost-Effectiveness
¥ P2P networks distribute computational and storage tasks across all nodes, reducing the need
for expensive centralized infrastructure.
¥ Traditional Systems: Require signiÞcant investment in server maintenance, security
measures, and intermediary fees.
8. Censorship Resistance
¥ In a P2P blockchain, no central entity can censor or block transactions.
¥ Nodes validate transactions based on predeÞned consensus rules, ensuring equal treatment
of all participants.
¥ Traditional Systems: Central authorities can block, reverse, or control access to services,
depending on their policies.
Public Ledgers
A public ledger is a digital ledger maintained across a decentralized network, where all transactions
are recorded chronologically and are visible to anyone. It is the backbone of blockchain-based
systems like Bitcoin and Ethereum.
1. Transparency
◦ Every participant can view all transactions recorded on the ledger.
◦ This openness ensures trust and accountability among users.
2. Decentralization
◦ No single entity controls the ledger. Instead, it is distributed across a peer-to-peer
network.
◦ All nodes maintain a synchronized copy of the ledger.
3. Immutability
◦ Once a transaction is recorded and added to the ledger, it cannot be altered or
deleted.
◦ This feature ensures the integrity and reliability of the ledger.
4. Cryptographic Security
◦ Transactions are secured using cryptographic algorithms, ensuring that only
authorized participants can initiate transactions.
5. Consensus Mechanisms
◦ Transactions are validated through consensus mechanisms like Proof of Work (PoW)
or Proof of Stake (PoS).
◦ This ensures that only legitimate transactions are added to the ledger.
2. Energy Consumption
◦ Some consensus mechanisms, like Proof of Work, consume large amounts of energy.
◦ Transitioning to more energy-efÞcient mechanisms (e.g., Proof of Stake) is a priority.
3. Privacy Concerns
◦ While transactions are pseudonymous, public accessibility may expose patterns or
sensitive information.
◦ Techniques like zero-knowledge proofs (e.g., zk-SNARKs) are being used to
enhance privacy.
4. Regulatory Challenges
◦ The openness of public ledgers can clash with regulations requiring data
conÞdentiality or control.
Public ledgers, like those used in blockchain networks, are decentralized, transparent, and
immutable records of transactions that are accessible to all participants. Below is a detailed
description of the key processes involved in how a public ledger works. It includes,
1. Transaction Initiation
¥ User Action: A user initiates a transaction, typically by sending cryptocurrency or digital
assets to another user. This can be done through a wallet or a smart contract interface.
¥ Transaction Data: The transaction includes the sender's address, recipient's address,
transaction amount, and potentially additional data (such as fees or smart contract code).
¥ Broadcast to Network: After initiating the transaction, it is broadcast to the entire network
of nodes (computers participating in the blockchain), ensuring that all participants are aware
of the transaction.
2. Transaction Validation
¥ Consensus Mechanism: The network nodes employ a consensus mechanism (like Proof of
Work, Proof of Stake, etc.) to validate the transaction. The role of miners or validators is to
ensure that the transaction is legitimate and that it follows the rules of the blockchain.
¥ Balance VeriÞcation: One of the key validation steps is ensuring that the sender has
sufÞcient funds in their account (or balance) to complete the transaction. This prevents fraud
and ensures the integrity of the blockchain.
¥ Validation Criteria: In addition to balance checks, validators check that the transaction
meets all other necessary criteria, such as correct formatting, authenticity of the signature,
and compliance with the protocol.
3. Block Creation
¥ Grouping Transactions: Once a transaction is validated, it is grouped together with other
validated transactions. These transactions are then bundled into a block.
¥ Hashing: A cryptographic hash function is applied to the block. This process creates a
unique identiÞer for the block based on its content, ensuring its integrity.
¥ Linking to Previous Block: Each block contains a reference to the previous block through
the inclusion of the previous block's hash. This creates an unbroken chain of blocks, where
each block is cryptographically linked to the previous one.
¥ Block Structure: The new block contains not only the list of transactions but also essential
data such as the block's hash, the previous blockÕs hash, and other metadata (e.g., timestamp,
nonce, etc.).
4. Ledger Update
¥ Reaching Consensus: After the block is created, it undergoes a consensus process where
validators or miners agree on the validity of the new block. This is done through the
blockchain's consensus algorithm (e.g., Proof of Work or Proof of Stake).
¥ Adding to Blockchain: Once consensus is reached, the new block is added to the
blockchain. This process updates the ledger by appending the new block to the end of the
chain, ensuring that the ledger remains up-to-date.
¥ Network Update: All nodes in the network then update their local copies of the blockchain
to reßect the newly added block. This ensures that all participants have the same version of
the ledger, maintaining consistency across the decentralized network.
5. Public Accessibility
¥ Visibility to All Participants: The transaction becomes part of the public ledger, meaning
that it is now accessible to everyone on the network. Every participant (node) in the
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 25 of 78
blockchain has a copy of the entire blockchain, and they can view the details of the
transaction (although personal information may be obfuscated via cryptographic methods).
¥ Transparency: Since the blockchain is a public ledger, it provides transparencyÑanyone
can verify transactions that have been added to the blockchain. This transparency is one of
the primary beneÞts of public ledgers, as it promotes trust and accountability.
¥ Immutability: Once a transaction is conÞrmed and added to the blockchain, it becomes
immutable. This means that it cannot be altered, reversed, or tampered with, ensuring that
the integrity of the transaction history is maintained.
Block Structure:
2. Transparency:
◦ Blockchain as a public ledger provides transparency to all participants. Each
transaction is recorded on the blockchain, and any participant can verify it. This
public accessibility ensures accountability, as anyone can trace transactions from the
start to the present block.
3. Immutability:
◦ Once a transaction is recorded on a blockchain, it cannot be altered or deleted. This
is due to the cryptographic nature of the ledger. Each block is linked to the previous
block through a cryptographic hash, making it nearly impossible to modify past
transactions without altering the entire blockchain. This feature makes public ledgers
highly secure and tamper-resistant.
4. Security:
◦ Blockchain employs cryptographic techniques like hashing and digital signatures to
secure the transactions. Each transaction is cryptographically signed by the sender,
ensuring authenticity. Additionally, consensus algorithms such as Proof of Work or
Proof of Stake are used to validate transactions and blocks, making it nearly
impossible to manipulate the ledger.
5. Consensus Mechanism:
◦ Public ledgers rely on consensus mechanisms to agree on the validity of transactions.
These mechanisms involve multiple nodes (computers) within the network working
together to validate and conÞrm transactions. Popular consensus mechanisms include
Proof of Work (PoW), Proof of Stake (PoS), and Delegated Proof of Stake
(DPoS).
6. Auditability:
◦ As transactions are publicly recorded, blockchain provides a built-in audit trail that
can be used to verify the authenticity of transactions. This auditability feature is
valuable for compliance and regulatory purposes, as it enables the tracking of every
single transaction back to its origin.
Block in a Blockchain
A block in a blockchain is a digital record containing a list of transactions or other relevant data that
is securely linked to previous blocks in a chronological order, forming a chain. Each block is a
fundamental unit that stores data and serves as a permanent and immutable record within the
blockchain. Blocks ensure the integrity, security, and transparency of blockchain systems by
employing cryptographic hashing and consensus algorithms.
A block in a blockchain is composed of two main components: the Block Header and the Block
Body (which includes the list of transactions). Below is a detailed explanation of the block's
structure.
Previous A double SHA-256 hash of the previous block's header. This links
Block's 32 bytes the current block to the previous one, ensuring the chain of blocks
Header Hash is intact and immutable.
A double SHA-256 hash of the Merkle tree, which represents the
Merkle Root
32 bytes hashes of all the transactions in the block. The Merkle tree ensures
Hash
data integrity.
The approximate creation time of the block, represented in Unix
Timestamp 4 bytes epoch time format. This is the time when the miner began hashing
the block header.
The target value that deÞnes the current difÞculty level of mining.
DifÞculty
4 bytes It adjusts dynamically to ensure that blocks are mined at a stable
Target
rate (e.g., every 10 minutes).
An arbitrary number that miners change to generate a valid block
Nonce 4 bytes hash. The nonce is used in the proof-of-work mining process to
match the networkÕs difÞculty target.
The block body consists of the actual transactions included in the block:
Field Size Description
This Þeld contains the total number of transactions in the block,
Transaction
Variable including the coinbase transaction (mining reward). The size can range
Counter
from 1 to 9 bytes.
This section contains all the transactions in the block. A transaction
Transactions Variable
includes the sender, recipient, amount, and other relevant details.
Block Size 4 bytes The size of the entire block, including the header and all transactions.
Blocks are linked together using hashing and cryptographic signatures. The block header contains
the hash of the previous block, which ensures that each block is connected to the one before it,
forming a continuous chain of blocks.
1. Hashing: Each block contains a unique cryptographic hash that is calculated based on the
contents of the block (including the block's header and transaction data). The hash serves as
a digital Þngerprint of the block, making it practically impossible to change the contents of
the block without altering its hash.
2. Linking Blocks: In the block header, the Previous Block Hash Þeld stores the hash of the
previous block. This creates an immutable chain of blocks, as any change to a previous
block would require re-mining all subsequent blocks, which is computationally infeasible in
a decentralized blockchain.
1. Block Header:
◦ The block header stores metadata about the block and is crucial for the functioning
of the blockchain. It includes several Þelds that help to validate the block and ensure
it is correctly linked to the previous block in the chain.
▪ Version (4 bytes): The version number of the block, indicating which rules
were used to create and validate the block.
▪ Previous Block Hash (32 bytes): A cryptographic hash of the previous
block's header, linking the current block to the previous one, thus forming a
chain. This is a critical element that ensures the immutability of the
blockchain.
▪ Merkle Root Hash (32 bytes): A hash that represents all the transactions
included in the block, derived from a Merkle tree structure. This allows for
quick veriÞcation of whether a speciÞc transaction is part of the block.
▪ Timestamp (4 bytes): The approximate time when the block was created,
often measured in Unix epoch format (seconds since Jan 1, 1970).
▪ DifÞculty Target (4 bytes): The target value that miners must match or
exceed to mine a new block. This determines the difÞculty of the mining
process and ensures that the time between blocks remains consistent.
▪ Nonce (4 bytes): A random number that miners adjust during the mining
process to Þnd a valid hash. The nonce is used in proof-of-work systems to
create a hash that meets the network's difÞculty target.
Lifecycle of a Block
1. Transaction Creation:
◦ Once the block is validated, it is added to the blockchain. The blockÕs hash is then
broadcast to the entire network, and each node updates its local copy of the
blockchain.
7. Finalization:
◦ The block becomes part of the permanent, immutable ledger, and the transactions
within it are considered conÞrmed. The new blockÕs hash is used to reference the
next block in the chain, ensuring that each new block is securely linked to the last.
Types of Blocks
1. Genesis Block
¥ The Genesis Block is the Þrst block in any blockchain. It is the initial block that begins the
blockchain and has no predecessor. It holds a special place because it is the starting point for
the chain.
¥ Characteristics:
◦ No Predecessor: Unlike other blocks, the Genesis Block does not reference a
previous block, since it is the Þrst block.
◦ Hardcoded Values: It typically contains hardcoded values such as speciÞc
initialization parameters, including the Þrst transaction that may have no input but
generates the Þrst set of coins (or tokens).
◦ Initialization: It is essential to initialize the blockchain as it provides the foundation
for all subsequent blocks.
¥ Example:
◦ In Bitcoin, the Genesis Block (Block #0) contains the Þrst transaction (known as the
"coinbase transactionÓ.
◦ Example Code:
genesis_block = {
'index': 0,
'timestamp': '2009-01-03 18:15:05',
'transactions': [],
'previous_hash': '0'*64, # No predecessor
'block_hash': 'hash_of_genesis_block',
'nonce': 0
}
2. Valid Block
¥ A Valid Block is any block that has been successfully created (or mined) and validated
according to the consensus mechanism (such as Proof of Work or Proof of Stake) of the
blockchain network. These blocks are accepted into the blockchain, and they form the
chainÕs backbone.
¥ Characteristics:
◦ Adheres to Consensus Rules: A valid block follows all rules outlined in the
blockchain protocol, such as correct formatting, proper references to previous
blocks, valid transactions, etc.
◦ Consensus ConÞrmation: For a block to be valid, miners (in PoW) or validators (in
PoS) must reach a consensus, ensuring the block is legitimate.
◦ Transaction Integrity: Valid blocks include transactions that are legitimate (e.g., no
double-spending, sufÞcient balance, correct signatures).
¥ Example:
◦ In Bitcoin, once a miner successfully mines a block by solving a cryptographic
puzzle, the block is validated and added to the blockchain.
¥ Example Code:
valid_block = {
'index': 2,
'timestamp': '2025-01-25 14:20:00',
'transactions': [
{'from': 'user1', 'to': 'user2', 'amount': 5, 'transaction_id': 'tx123'}
],
'previous_hash': 'previous_block_hash',
'block_hash': 'valid_hash_of_block',
'nonce': 123456
}
3. Orphaned Block
¥ An Orphaned Block (also known as a Stale Block) occurs when two miners or validators
solve a block at nearly the same time. While both blocks are valid, only one of them will be
accepted into the main blockchain (the longest chain). The other block, which is not part of
the longest chain, becomes an orphaned block.
¥ Characteristics:
◦ Simultaneous Mining: In a decentralized network, there is a possibility of two
blocks being mined at the same time. When this happens, the network has two
versions of the blockchain, but only the longest (most proof of work) chain is
considered valid.
◦ Discarded: The orphaned block does not become part of the main chain. Although it
is valid and contains transactions, it is effectively discarded, and any transactions in
it are moved to the next valid block.
◦ Temporary: Orphaned blocks are usually temporary. After the network conÞrms the
longest chain, the orphaned block is rejected, and the blockchain is uniÞed again.
¥ Example:
◦ In Bitcoin, miners may Þnd two valid blocks at nearly the same time. However, the
one that is appended to the longest chain is considered valid, while the other
becomes orphaned.
orphaned_block = {
'index': 3, # Simultaneously mined block
'timestamp': '2025-01-25 14:25:00',
'transactions': [
{'from': 'user3', 'to': 'user4', 'amount': 10, 'transaction_id': 'tx124'}
],
'previous_hash': 'previous_block_hash',
'block_hash': 'orphaned_hash_of_block',
'nonce': 654321
}
Type of
Description Key Characteristics Example Use Case
Block
Hardcoded values,
Genesis The Þrst block in the blockchain, Initializes Bitcoin
unique, initializes the
Block with no predecessor. blockchain, starting point.
blockchain.
A block that adheres to the Adheres to consensus
Valid Regular transactions,
consensus rules and is added to rules (PoW/PoS),
Block adding blocks to the chain.
the blockchain. validated transactions.
Valid but discarded,
A block that is valid but not part Temporary blockchain
Orphaned happens during
of the main chain due to a chain fork, discarded after
Block simultaneous block
split. longest chain wins.
discovery.
import hashlib
import time
def __str__(self):
return f"Block {self.index} | Hash: {self.hash} | Prev Hash: {self.previous_hash}"
Blockchain Transactions
In a blockchain, transactions are the fundamental operations that represent the transfer of
data or assets (like cryptocurrency) between participants. Each transaction is recorded and stored in
blocks, and these blocks form the blockchain. Mining plays an essential role in this process,
ensuring that new blocks are added to the blockchain through a cryptographic proof-of-work puzzle.
Together, these elements form the backbone of blockchain networks like Bitcoin and Ethereum.
A blockchain transaction is a digital action or event where assets are transferred from one party to
another, or some data is written onto the blockchain. Transactions can represent various activities,
such as:
4. Transaction ID: A unique identiÞer (hash) used to track and verify the transaction.
5. Nonce: A number assigned to each transaction to prevent replay attacks and ensure the
correct order of transactions.
6. Signature: A cryptographic signature generated using the senderÕs private key that ensures
the integrity and authenticity of the transaction.
1. version:
◦ The initial message sent by a node to the network to advertise its version and block
count. The remote node replies with similar information, establishing the connection.
2. verack:
◦ The response to the version message, conÞrming the connection.
3. inv:
◦ Nodes use this message to advertise their knowledge of blocks and transactions. It
contains hashes of the blocks or transactions.
4. getdata:
◦ Sent in response to an inv message, requesting a speciÞc block or transaction by its
hash.
5. getblocks:
◦ This message returns a list of blocks (up to 500) starting after a known block hash. It
helps nodes synchronize by requesting the latest blocks.
6. getheaders:
◦ Requests the block headers within a speciÞed range, useful for obtaining block
metadata without downloading full blocks.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 36 of 78
7. tx:
◦ Sends a transaction in response to a getdata request. It contains a serialized
transaction.
8. block:
◦ Sends a full block in response to a getdata request, including all transactions within
the block.
9. headers:
◦ Returns up to 2,000 block headers in response to a getheaders request.
10. getaddr:
◦ A request to get information about known peers (their IP addresses and port
numbers).
11. addr:
◦ Provides information about nodes on the network, including a list of known peer
addresses.
Mining in a blockchain network like Bitcoin is a key mechanism that ensures the integrity
and security of the blockchain. It involves solving a cryptographic puzzle (proof-of-work) to add
new blocks, containing valid transactions, to the blockchain. HereÕs how mining and blockchain
transactions work together:
1. Transaction Creation:
◦ A user creates a transaction with the sender's and receiverÕs addresses, the amount or
contract data, and gas parameters.
The mining process involves solving a cryptographic puzzle through the following steps:
1. Hash Calculation:
Block Hash = SHA-256(SHA-256(Block Header))
Where the Block Header contains the version, previous block hash, Merkle root, timestamp,
difÞculty target, and nonce.
3. Mining DifÞculty:
DifÞculty = (Target / Current Target)
DifÞculty is recalculated every 2016 blocks in Bitcoin to ensure block times remain
consistent.
4. Nonce:
Miners adjust the nonce value until they Þnd a hash smaller than the difÞculty target.
import hashlib
import time
import json
from uuid import uuid4
'receiver': self.receiver,
'amount': self.amount,
'fee': self.fee,
'nonce': self.nonce,
'timestamp': self.timestamp
}
transaction_json = json.dumps(transaction_data, sort_keys=True)
return hashlib.sha256(transaction_json.encode('utf-8')).hexdigest()
def __str__(self):
return f"Transaction({self.transaction_id}): {self.sender} -> {self.receiver}, Amount:
{self.amount}, Fee: {self.fee}"
# Broadcast the transaction (in a real network, this would propagate to other nodes)
def broadcast_transaction(self, transaction):
if self.validate_transaction(transaction):
print(f"Transaction {transaction.transaction_id} broadcasted to the network.")
return True
return False
}
self.chain.append(genesis_block)
# Create transactions
transaction1 = BlockchainTransaction(sender="Alice", receiver="Bob", amount=50, fee=0.1)
transaction2 = BlockchainTransaction(sender="Bob", receiver="Charlie", amount=30, fee=0.05)
transaction3 = BlockchainTransaction(sender="Alice", receiver="Charlie", amount=20, fee=0.1)
# Broadcast transactions
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 41 of 78
node1.broadcast_transaction(transaction1)
node1.broadcast_transaction(transaction2)
node2.broadcast_transaction(transaction3)
# Mine a block
blockchain.mine_block()
Output:
Transaction 4794e0fc-f8b4-45cc-b0d2-0d317157ced5 validated!
Transaction 4794e0fc-f8b4-45cc-b0d2-0d317157ced5 broadcasted to the network.
Transaction 8309b8e6-e4cd-4d26-b76b-c5b68ed7b94c validated!
Transaction 8309b8e6-e4cd-4d26-b76b-c5b68ed7b94c broadcasted to the network.
Transaction 3b5be460-b9b0-4626-92b4-42991c3d31b9 validated!
Transaction 3b5be460-b9b0-4626-92b4-42991c3d31b9 broadcasted to the network.
Mining block with 3 transactions...
Block 1 - Transactions: 3
Transaction ID: 4794e0fc-f8b4-45cc-b0d2-0d317157ced5, Sender: Alice, Receiver: Bob, Amount:
50, Fee: 0.1
Transaction ID: 8309b8e6-e4cd-4d26-b76b-c5b68ed7b94c, Sender: Bob, Receiver: Charlie,
Amount: 30, Fee: 0.05
Transaction ID: 3b5be460-b9b0-4626-92b4-42991c3d31b9, Sender: Alice, Receiver: Charlie,
Amount: 20, Fee: 0.1
Blockchain:
Block #0 | Hash: 5d8f7a0139c64f0e27b88e4734c8cfc3a2424d89a8fd0abbd4da10719b4d39b1
Block #1 | Hash: 1fa7a04de15b2d56c7f99a37841a678d5cb38c7ca0438e285de0c25fdfb57318
In a blockchain, the chain refers to a sequence of blocks that are linked together, where each block
contains a list of transactions and a hash of the previous block, ensuring the integrity and
immutability of the blockchain. The longest chain refers to the chain with the greatest number of
blocks or the chain that has the highest accumulated proof of work (or consensus mechanism,
depending on the blockchain).
In the context of a decentralized network, nodes generally follow the longest chain rule. This
means that, in case of a fork (when two competing chains emerge), nodes will consider the longest
valid chain as the "true" version of the blockchain. This ensures that the blockchain with the highest
computational effort or the most valid blocks is considered the canonical chain.
Chain: A collection of blocks, where each block is linked to the previous one using its hash.
Longest Chain: The chain that is the longest in terms of block count, or the chain with the most
accumulated proof of work.
In a blockchain network, a fork occurs when two or more blocks are found at the same time. This is
common in Proof-of-Work (PoW) blockchains, where multiple miners can discover blocks
simultaneously. At this point, the network temporarily has two competing chains. Eventually, one
chain becomes the "longest" and is considered valid, while the other fork is discarded.
Let's break down the key steps involved in blockchain forks and how they are visualized:
Below is a simple Python simulation illustrating the fork event, where two competing blocks are
added to the blockchain before the network converges on the longest chain.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 44 of 78
import hashlib
import time
import json
from uuid import uuid4
def __str__(self):
return f"Transaction({self.transaction_id}): {self.sender} -> {self.receiver}, Amount:
{self.amount}, Fee: {self.fee}"
return False
print(f"Transaction {transaction.transaction_id} validated!")
return True
# Broadcast the transaction (in a real network, this would propagate to other nodes)
def broadcast_transaction(self, transaction):
if self.validate_transaction(transaction):
print(f"Transaction {transaction.transaction_id} broadcasted to the network.")
return True
return False
# Create transactions
transaction1 = BlockchainTransaction(sender="Alice", receiver="Bob", amount=50, fee=0.1)
transaction2 = BlockchainTransaction(sender="Bob", receiver="Charlie", amount=30, fee=0.05)
transaction3 = BlockchainTransaction(sender="Alice", receiver="Charlie", amount=20, fee=0.1)
# Broadcast transactions
node1.broadcast_transaction(transaction1)
node1.broadcast_transaction(transaction2)
node2.broadcast_transaction(transaction3)
# Network reconvergence
blockchain.reconverge()
Output:
Blockchain:
Block #0 | Hash: 5d8f7a0139c64f0e27b88e4734c8cfc3a2424d89a8fd0abbd4da10719b4d39b1
Block #1 | Hash: 1fa7a04de15b2d56c7f99a37841a678d5cb38c7ca0438e285de0c25fdfb57318
import hashlib
import time
import json
from uuid import uuid4
class BlockchainTransaction:
def __init__(self, sender, receiver, amount, fee, nonce=None):
self.transaction_id = str(uuid4()) # Unique transaction ID
self.sender = sender
self.receiver = receiver
self.amount = amount
self.fee = fee
self.nonce = nonce if nonce else str(uuid4()) # To prevent replay attacks
self.timestamp = time.time()
def generate_hash(self):
transaction_data = {
'sender': self.sender,
'receiver': self.receiver,
'amount': self.amount,
'fee': self.fee,
'nonce': self.nonce,
'timestamp': self.timestamp
}
transaction_json = json.dumps(transaction_data, sort_keys=True)
return hashlib.sha256(transaction_json.encode('utf-8')).hexdigest()
def __str__(self):
return f"Transaction({self.transaction_id}): {self.sender} -> {self.receiver}, Amount:
{self.amount}, Fee: {self.fee}"
class BlockchainNode:
def __init__(self, node_id):
self.node_id = node_id
self.transaction_pool = []
class Blockchain:
def __init__(self):
self.chains = [] # List of all blockchain forks
self.create_genesis_block()
def create_genesis_block(self):
genesis_block = {
'index': 0,
'timestamp': time.time(),
'transactions': [],
'previous_hash': '0'
}
genesis_block['hash'] = self.calculate_hash(genesis_block)
self.chains.append([genesis_block]) # Initialize with a single chain
def get_longest_chain(self):
longest_chain = max(self.chains, key=len)
return longest_chain
if __name__ == "__main__":
blockchain = Blockchain()
node1 = BlockchainNode(node_id="Node-1")
node2 = BlockchainNode(node_id="Node-2")
node1.add_transaction(transaction1)
node1.add_transaction(transaction2)
node2.add_transaction(transaction3)
node1.broadcast_transaction(transaction1)
node1.broadcast_transaction(transaction2)
node2.broadcast_transaction(transaction3)
blockchain.print_chain(chain_idx=0)
The Permissioned Model of Blockchain and Permissioned Ledger are closely related
concepts. A Permissioned Ledger is a type of blockchain where network participants are pre-
identiÞed and trusted. Instead of using a traditional distributed consensus mechanism, it relies on an
agreement protocol to maintain a shared and consistent state of the records. VeriÞcation of
transactions is performed by preselected veriÞers, designated by a central authority, eliminating the
need for mining or energy-intensive consensus processes. Permissioned blockchains restrict write
access to a select group of preapproved nodes or users who have undergone a trust-based
onboarding process. Only these designated nodes are authorized to verify transactions and
participate in the distributed consensus process.
1. Access Control
¥ Restricted Participation: Only preapproved participants, veriÞed through an onboarding
process, can join the network.
¥ Role-Based Permissions: Participants are assigned roles, such as validators, administrators,
or users, based on their level of authorization.
¥ Identity Management: Each participant's identity is known and veriÞed, reducing
anonymity while ensuring accountability.
2. Centralized Governance
¥ Controlled by a Central Authority or Consortium: A governing body, such as a
consortium of organizations, establishes the network rules and maintains oversight.
¥ Policy Enforcement: The authority decides who gets access, how consensus is achieved,
and how disputes are resolved.
3. Privacy and ConÞdentiality
¥ Limited Data Sharing: Sensitive information can be kept off-chain or shared selectively
among authorized nodes.
¥ Encrypted Transactions: Data and transactions can be encrypted to provide additional
layers of privacy.
4. Customizability
¥ Tailored Consensus Mechanisms: Permissioned blockchains often use lightweight
consensus mechanisms, such as Practical Byzantine Fault Tolerance (PBFT) or Raft, that do
not require energy-intensive processes like Proof of Work (PoW).
¥ Adaptable Features: Organizations can adjust the blockchain protocols to meet industry-
speciÞc needs, such as throughput, latency, or regulatory compliance.
5. EfÞciency
¥ High Throughput: By limiting the number of participants and optimizing consensus
mechanisms, permissioned blockchains achieve faster transaction speeds.
¥ Scalability: Smaller, controlled networks can handle larger transaction volumes compared
to public blockchains.
1. Network Setup
¥ Infrastructure Establishment:
A permissioned blockchain begins with the setup of infrastructure by a central authority,
consortium, or a governing organization. This entity decides the rules, protocols, and
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 52 of 78
structure of the blockchain network. The infrastructure includes hardware, software, and
network components required for nodes to interact with the blockchain.
¥ DeÞning Rules for Participation:
The governing body establishes strict rules for who can join the network, their roles (e.g.,
user, validator, auditor), and the responsibilities of each participant. This includes guidelines
for transaction validation, data sharing, and dispute resolution.
2. Onboarding Participants
¥ Participant Vetting:
Potential participants are carefully vetted through processes such as identity veriÞcation,
compliance checks, and proof of eligibility. This ensures that all members of the blockchain
are trustworthy entities.
¥ Assigning Permissions:
Participants are granted speciÞc permissions based on their roles:
◦ Users: Can initiate transactions but have limited access to sensitive network
operations.
◦ Validators/Miners: Responsible for verifying and adding transactions to the
blockchain.
◦ Auditors/Observers: Can view the blockchain for compliance or oversight but
cannot write to it.
¥ Digital Identity Creation:
Each participant is assigned a unique digital identity (e.g., cryptographic key pair) to interact
securely with the blockchain. The private key is used to sign transactions, ensuring
authenticity and integrity.
3. Transaction Flow
¥ Proposing Transactions:
Authorized participants initiate transactions (e.g., transferring assets, executing contracts).
These transactions include the sender, receiver, transaction details, and a digital signature for
validation.
¥ VeriÞcation:
Transactions are sent to nodes with write permissions for veriÞcation. Validators check:
◦ Authenticity of the sender using their digital signature.
◦ Adequate balance or resource availability.
◦ Compliance with network rules and policies.
¥ Consensus:
Instead of traditional Proof of Work (PoW) or Proof of Stake (PoS), permissioned
blockchains use efÞcient consensus mechanisms suited for trusted environments:
◦ Practical Byzantine Fault Tolerance (PBFT): Ensures fault tolerance and
agreement among known participants.
◦ Raft: A lightweight leader-based consensus mechanism offering high throughput.
◦ Proof of Authority (PoA): Trusted validators with known identities approve
transactions.
¥ Adding Transactions to Blocks:
Once consensus is achieved, the veriÞed transactions are added to a new block. The block is
cryptographically linked to the previous block, ensuring the immutability of the blockchain.
4. Data Sharing
¥ Selective Sharing:
Permissioned blockchains implement granular access controls to ensure data is shared only
with authorized participants.
5. Blockchain Maintenance
¥ Protocol Updates:
Governing bodies or consortia manage updates to the blockchain's protocols, ensuring the
system evolves with technological and regulatory changes.
¥ Dispute Resolution:
Disputes (e.g., invalid transactions or network misbehavior) are resolved through predeÞned
processes, such as arbitration or voting among governing members.
¥ Participant Management:
Permissions for participants can be revoked or updated based on their actions or network
requirements.
Consensus mechanisms
Because of the structure of permissioned blockchains, they donÕt use the same types of
consensus protocols as permissionless ones. Most commonly, organizations that deploy
permissioned blockchains use one (or more) of the following three protocols: Practical Byzantine
Fault Tolerance (PBFT), federated, or round-robin consensus.
1. PBFT Ð PBFT is an improved version of the original BFT protocol where all voting nodes much
reach a consensus, but one or more parties are considered unreliable. In this model, a networkÕs
safety and stability are guaranteed so long as the required minimum percentage of nodes are
behaving honestly and properly.
3. Finance:
◦ Process interbank settlements and manage asset tokenization with secure and
efÞcient transaction validation.
4. Government Applications:
◦ Facilitate secure voting systems, land registry management, and welfare distribution
with transparency and privacy.
5. Enterprise Resource Planning (ERP):
◦ Enable secure collaboration among different departments or subsidiaries within an
organization.
Cryptography
1. Security:
◦ Ensures that the data stored on the blockchain is protected from unauthorized access
or tampering.
◦ Transactions are encrypted, making them difficult to decode without the appropriate
keys.
2. Integrity:
◦ Guarantees that data in the blockchain remains unchanged once written.
◦ Hash functions detect even the smallest alterations in data.
3. Authentication:
◦ Digital signatures validate the authenticity of transactions, ensuring they come from
a legitimate sender.
4. Transparency and Privacy:
◦ While blockchain is transparent, cryptographic techniques (like public-key
cryptography) maintain the confidentiality of user information.
5. Decentralization:
◦ Cryptographic algorithms eliminate the need for a centralized authority by enabling
trustless interactions between parties.
1. Hash Functions:
◦ Converts input data into a fixed-size string of characters (hash value).
◦ Properties:
▪ Deterministic: Same input always gives the same hash.
▪ Irreversible: Impossible to determine the original data from its hash.
▪ Collision-resistant: No two different inputs should produce the same hash.
◦ Example: SHA-256 used in Bitcoin.
2. Public-Key Cryptography (Asymmetric Encryption):
◦ Involves a pair of keys: a public key (shared openly) and a private key (kept secret).
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 57 of 78
◦ Applications:
▪ Digital signatures for transaction validation.
▪ Encryption for data privacy.
◦ Example: Elliptic Curve Digital Signature Algorithm (ECDSA) in Bitcoin.
3. Digital Signatures:
◦ Prove the authenticity of a transaction and ensure it hasn't been tampered with.
◦ Combine the sender's private key with transaction data to generate a unique
signature.
4. Merkle Trees:
◦ Data structure that organizes transactions using hash functions in a tree-like format.
◦ Enables efficient verification of data without downloading the entire blockchain.
1. Security:
◦ Prevents unauthorized access, data breaches, and tampering.
2. Data Integrity:
◦ Guarantees that transactions and blocks cannot be altered after inclusion in the
blockchain.
3. Decentralized Trust:
◦ Eliminates the need for central authorities by enabling cryptographic proof of
validity.
4. Privacy:
◦ Protects user identities and transaction details using encryption.
5. Transparency and Verifiability:
◦ Allows all participants to verify the authenticity of transactions.
1. Complexity:
◦ Cryptographic algorithms are computationally intensive, requiring significant
processing power.
2. Energy Consumption:
◦ Proof of Work (PoW), a consensus mechanism that relies heavily on cryptography,
consumes large amounts of energy.
3. Irreversible Errors:
◦ Losing private keys permanently locks access to associated funds or data.
4. Scalability Issues:
◦ Cryptographic computations and veriÞcations can limit transaction throughput in
large-scale systems.
5. Quantum Vulnerability:
◦ Quantum computers, once mature, could potentially break existing cryptographic
algorithms, threatening blockchain security.
Hash functions are fundamental to public key cryptography and cryptocurrencies. A hash
function is a mathematical algorithm that transforms an input (or "message") into a fixed-length
output, typically a string of characters. Cryptographic hash functions are designed to be one-way
(meaning they cannot be reversed) and collision-resistant (meaning it's computationally infeasible
to find two different inputs that produce the same output). In public key cryptography, hash
functions are used for creating digital signatures, ensuring data integrity, and generating unique
identifiers. They are also central to the creation of addresses in cryptocurrencies, especially Bitcoin.
A hash function is a fundamental concept in computer science and cryptography that transforms an
input (or message) of arbitrary length into a Þxed-length output, called a hash value or digest.
Mathematically, a hash function can be represented as:
H(M) = h
where:
• H is the hash function,
• M is the input message,
• h is the output hash value of Þxed size.
3. Fast Computation:
◦ The hash value is generated quickly, making it efÞcient for real-time applications.
4. Pre-image Resistance:
◦ It is computationally infeasible to reverse-engineer the original input from its hash.
5. Collision Resistance:
◦ It is highly unlikely for two different inputs to produce the same hash.
6. Avalanche Effect:
◦ A small change in the input causes a signiÞcant and unpredictable change in the hash
output.
7. Non-reversibility:
◦ Hash functions are one-way, meaning you cannot determine the input from the hash
output.
4. Immutability:
◦ Altering one block would require recalculating hashes for all subsequent blocks,
making tampering infeasible.
5. Verification:
◦ Simplifies verifying the authenticity of transactions or data.
Cryptocurrencies:
In cryptocurrencies like Bitcoin, hash functions are crucial for:
¥ Address Generation: The process of generating public addresses involves the
transformation of the public key through various hashing mechanisms.
¥ Transaction Verification: Hash functions validate transactions and blocks by creating
unique identifiers.
¥ Proof of Work (PoW): Cryptocurrencies like Bitcoin use hash functions in the mining
process to find valid block hashes, ensuring consensus without a central authority.
¥ Security: Hash functions ensure that transaction data cannot be altered without detection,
providing the security necessary to build trust in the system.
¥ Compressed Public Key: A compressed public key is 33 bytes long and only stores the x-
coordinate. Depending on whether the y-coordinate is odd or even, it is prefixed with 0x02
or 0x03, respectively.
◦ Format: 0x02/0x03 + 32-byte x-coordinate
The compressed format is used in Bitcoin for space-saving reasons as it reduces the size of
transactions and optimizes blockchain efficiency.
ECC relies on a fundamental operation known as point multiplication. Given a base point G (the
generator point on the curve) and an integer k (the private key), the resulting public key P is
computed as:
P = k⋅G
This operation involves adding the point G to itself k times, which results in a new point P. This
operation is computationally difÞcult to reverse, providing the security for the system. The
challenge of reversing this process is known as the Elliptic Curve Discrete Logarithm Problem
(ECDLP).
To visualize point multiplication, imagine moving along the curve starting from G and repeatedly
adding the point to itself. The result, after k multiplications, is the point P, which forms the public
key. The private key k remains secret, while P is used as the public key.
1. Generate Public Key: The public key P is obtained by multiplying the private key k with
the generator point G on the elliptic curve:
P = k⋅G
2. Hash the Public Key: The public key is hashed using two different hashing algorithms:
◦ First, apply SHA-256 to the public key P: SHA−256(P)
◦ Then, apply RIPEMD-160 to the result of the SHA-256 hash:
RIPEMD160(SHA−256(P))
◦ This produces the public key hash (PKH).
3. Add Version Byte: A version byte is added to the public key hash to differentiate between
different types of Bitcoin addresses. For the main Bitcoin network, this version byte is 0x00.
The result is:
Version Byte+PKH
Base58Check is a custom encoding scheme used in Bitcoin to represent public keys, private
keys, and Bitcoin addresses. The main advantage of Base58 over Base64 is that it avoids confusing
characters like 0, O, I, and l, making it more suitable for human input.
Sample Output:
import hashlib
import base58
from ecdsa import SECP256k1, ellipticcurve, util
from ecdsa.keys import ECPrivateKey
# Constants for Bitcoin address version byte (mainnet) and elliptic curve parameters
VERSION_BYTE = b'\x00' # Version byte for Bitcoin mainnet addresses
CURVE = SECP256k1.curve
G = SECP256k1.generator
ORDER = SECP256k1.order
def sha256(data):
"""SHA-256 hash function."""
return hashlib.sha256(data).digest()
def ripemd160(data):
"""RIPEMD160 hash function."""
return hashlib.new('ripemd160', data).digest()
def base58_check_encode(data):
"""Encodes data with Base58Check encoding."""
checksum = hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]
return base58.b58encode(data + checksum)
def private_key_to_public_key(private_key):
"""Converts a private key to a public key using ECC (SECP256k1)."""
# Calculate the public key: P = k * G
pub_point = private_key * G
x, y = pub_point.x(), pub_point.y()
def public_key_to_bitcoin_address(public_key):
"""Converts a public key to a Bitcoin address using Base58Check encoding."""
# Step 1: Hash the public key (SHA256 then RIPEMD160)
sha256_hash = sha256(public_key)
ripemd160_hash = ripemd160(sha256_hash)
return bitcoin_address.decode()
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 65 of 78
# Example Usage:
# Generate a random private key (for demonstration purposes, in a real scenario you should
generate securely)
private_key = int.from_bytes(hashlib.sha256("random_string".encode()).digest(), byteorder='big')
% ORDER
Output:
Private Key (hex): 0x3d2fe4b626f34ac6016cd8e79534956d22641e7b1d8815b2baf1532c07a7c8e3
Public Key (hex):
04b0b8f226a945d2078391ac2f0cf9f74cfd4644d5f8bc6c748a2b8e08a9b7e5b31b06a01aee7580eafc
e17925487ff1054ed41bb4ad9f56450f0c2005b7b99a8f0
Bitcoin Address: 1FfRgfA4qsgxzvT5aSwnF7V7DGrXtHk4XQ
1. Deterministic
A hash function is deterministic, meaning that for the same input, it will always produce the
same output. This is essential for ensuring data consistency. The process of hashing should be
repeatable and reliable.
• Formula: H(x) = y
• Where:
◦ x is the input (message or data).
◦ y is the hash value (output).
◦ H(x) will always produce the same y for the same x.
Example:
For the input string "hello", the hash value produced by a function like SHA-256 will always be the
same.
Example in practice:
¥ Input: "hello"
¥ Output (SHA-256):
2cf24dba5fb0a30e26e83b2ac5b9e29e1b168ee29f1954eeb17a75ab2d6fe758
¥ Where:
◦ n is fixed (e.g., for SHA-256, n = 256 bits).
¥ This property ensures that large inputs, such as files or messages of varying sizes, are
compressed into a consistent-sized output.
Example in practice:
¥ Input: "hello"
¥ Output (SHA-256):
2cf24dba5fb0a30e26e83b2ac5b9e29e1b168ee29f1954eeb17a75ab2d6fe758
¥ Output length: 256 bits (or 64 hexadecimal characters)
3. EfÞcient Computation
A good hash function should be computationally efÞcient, meaning that it should take a
minimal amount of time to compute the hash, regardless of the input size. This ensures that the
system works fast enough for practical use in real-time applications.
Formula: If t(x) represents the time to compute the hash of input x, then the time complexity
should be close to O(n), where n is the size of the input.
t(x)=O(n)
Example:
¥ For SHA-256:
◦ The function will compute the hash in a time complexity that is proportional to the
size of the data, i.e., linear in the number of bytes.
◦ For very large data (e.g., multi-gigabyte Þles), efÞcient implementations ensure the
hash can be calculated quickly.
6. Collision Resistance
Collision resistance means that it is computationally infeasible to Þnd two distinct inputs x1
and x2 such that: H(x1 )=H(x2 )
This is a critical property for security because if two different pieces of data hash to the same value,
it can lead to vulnerabilities in applications like digital signatures or data integrity checks.
¥ Formula: For any two distinct inputs x1 and x2 , the probability of H(x1 )=H(x2 ) should
be extremely low.
7. Avalanche Effect
The avalanche effect means that a small change in the input (even a single bit) should
produce a significantly different hash output. This ensures that the hash function spreads the
information uniformly across the output space, which improves security by making patterns in the
input unpredictable in the output.
¥ Formula: If x and x′ differ by one bit, the difference between H(x) and H(x′) should be
maximal in terms of bit difference. This can be observed as a large Hamming distance
between the hash values.
H(x) ≠ H ( x′) for x ≠ x′
8. Non-reversible
A good hash function is non-reversible, meaning that thereÕs no feasible way to reverse the process
and recover the original input x from its hash value H(x).
¥ Formula: There should be no efficient algorithm that can recover x from H(x). That is:
Given H(x), find x should be computationally infeasible.
9. Uniform Distribution
The output of a hash function should appear uniformly distributed over its output space. This means
that every possible hash value should be equally likely to occur, regardless of the input. A non-
uniform distribution could lead to predictable patterns, which would reduce security.
¥ Formula: The hash values H(x) for all possible inputs x should be evenly distributed across
the output space.
Pr[H(x)=h]=1/ 2n
¥ Where n is the length of the hash value in bits, and h represents any possible hash output.
SHA-256 in Blockchain
SHA-256 is particularly critical in Bitcoin and other cryptocurrencies, where it is used for
transaction integrity, mining (Proof-of-Work), and block validation.
Key Properties
¥ Deterministic: The same input always results in the same hash.
¥ Pre-image Resistance: It is computationally infeasible to Þnd the original input from the
hash.
¥ Collision Resistance: No two different inputs should produce the same hash.
¥ Avalanche Effect: A small change in input results in a completely different hash.
Prepared by, Gautham Praveen R / AP- CSBS, SAEC Page 68 of 78
Role in Blockchain
¥ Block Hashing: Each block's header is hashed using SHA-256, linking blocks securely.
¥ Proof of Work (PoW): Miners solve a cryptographic puzzle by Þnding a hash below a
target value.
¥ Transaction Integrity: Ensures that transactions cannot be altered after being recorded.
Hash Pointer :
Key Features:
¥ Pointer: The pointer gives the location of the data, such as a memory address, index, or Þle
location.
¥ Cryptographic Hash: The hash of the data stored at that address is computed using a hash
function (e.g., SHA-256, SHA-3). A cryptographic hash function takes an input (data) and
produces a Þxed-size hash value that uniquely represents the data.
3. Tamper Detection:
◦ If an attacker tries to modify the data, the hash will change, and the tampered data
will be easily detected.
4. Unambiguous Representation:
◦ The hash uniquely represents the data, making it ideal for referencing data in a
secure and compact manner.
Mathematical Representation:
Given some data D, the cryptographic hash function H maps the data to a unique value:
H(D)=hash of data D.
In this case, the pointer points to the memory location M, and the hash H("Hello, World!") acts as a
Þngerprint. If the document changes, its hash would also change, alerting you to tampering.
Applications:
¥ Linked Data Structures: In blockchains, the blocks are linked via hash pointers. Each
block contains a pointer to the previous block and a hash of its data, ensuring that tampering
with any block will invalidate the subsequent blocks.
¥ File Systems: Merkle Trees (which use hash pointers) are used to maintain the integrity of
Þles stored in a decentralized Þle system, such as IPFS (InterPlanetary File System).
Merkle Tree
A Merkle Tree (or binary hash tree) is a tree-like data structure used to efficiently verify the
integrity and consistency of large datasets. ItÕs a cryptographic structure where each leaf node
represents a hash of data, and each non-leaf node represents the hash of its children. At the top of
the tree is the Merkle Root, which represents the cryptographic fingerprint of all the data in the
tree.
Merkle Tree Structure:
1. Leaf Nodes:
◦ Each leaf node contains the cryptographic hash of an individual data block.
2. Internal Nodes:
◦ Non-leaf nodes contain the hash of the concatenation of their two child nodes'
hashes.
3. Merkle Root:
◦ The root node of the tree represents the cryptographic hash of all the data in the tree.
ItÕs a single hash value that provides a unique representation of the entire dataset.
3. Pair adjacent leaf nodes and compute the hash of each pair to create the internal nodes: H12
=H(H1 ∣∣H2 ),H34 =H(H3 ∣∣H4 ),É
4. Continue this process, pairing and hashing, until a single Merkle Root is
obtained:Merkle Root
Merkle Root=H(H(H1 ∣∣H2 )∣∣H(H3 ∣∣H4 ))
Merkle Proof:
A Merkle Proof is a subset of the tree required to prove that a particular piece of data is
included in the dataset represented by the Merkle Root. It includes the following:
1. The hash of the data (leaf node).
2. The hashes of the sibling nodes up the tree.
3. The Merkle Root.
Example:
First, we compute the cryptographic hash of each data block using a hash function, such as
SHA-256.
• H(D1 )=Hash of D1
• H(D2 )=Hash of D2
• H(D3 )=Hash of D3
• H(D4 )=Hash of D4
Merkle Root
|
-------------------------
| |
H_{12} H_{34}
| |
------------- -------------
| | | |
H(D_1) H(D_2) H(D_3) H(D_4)
To prove that D3 is part of the Merkle Tree, you would use the Merkle Path to recompute the
Merkle Root:
1. Start with H(D3 ).
2. Use the sibling hash H(D4 ) and compute: H(H(D3 )∣∣H(D4 ))=H(H(D3 )∣∣Hash4)
3. Then, use H34 to recompute the next step: H(H34 ∣∣H12 )=Merkle Root
4. If the recomputed Merkle Root matches the Merkle Root in the tree, then D3 is part of the
Merkle Tree.
Layers of Blockchain
Blockchain technology is structured into multiple layers, each with a distinct role in ensuring the
security, decentralization, and efÞciency of the system.
contract SimpleContract {
uint storedData;
function set(uint x) public { storedData = x; }
function get() public view returns (uint) { return storedData; }
}
Key Components:
¥ DApps (Decentralized Applications):
◦ Applications running on a blockchain instead of centralized servers.
◦ Examples: Uniswap, OpenSea, Aave.
¥ APIs (Application Programming Interfaces):
◦ Allow developers to interact with blockchain networks.
¥ Wallets:
◦ Secure applications for managing cryptocurrencies.
◦ Examples: Metamask, Trust Wallet, Ledger.
Functions:
¥ Provides an intuitive interface for interacting with blockchain.
¥ Supports Þnancial services, identity management, and supply chain applications.
¥ Bridges blockchain technology with real-world use cases.
+----------------------------------------------------------------------+
| Application Layer |
| - Decentralized Applications (DApps) |
| - Digital Wallets (Metamask, Trust Wallet) |
| - APIs and User Interfaces |
+----------------------------------------------------------------------+
| Smart Contract Layer |
| - Smart Contracts (Solidity, Chaincode) |
| - Ethereum Virtual Machine (EVM) |
| - Oracles for External Data |
+----------------------------------------------------------------------+
| Consensus Layer |
| - Proof of Work (PoW), Proof of Stake (PoS), PoET. |
| - Mining, Validator Nodes |
| - Transaction Validation |
+----------------------------------------------------------------------+
| Network Layer |
| - Peer-to-Peer (P2P) Communication |
| - Node Discovery and Transaction Propagation |
| - Block Propagation and Synchronization |
+----------------------------------------------------------------------+
| Data Layer |
| - Blocks and Transactions |
| - Merkle Tree for EfÞcient Data VeriÞcation |
| - Cryptographic Hashing (SHA-256, Keccak-256) |
+----------------------------------------------------------------------+
| Infrastructure Layer |
| - Physical Hardware (Nodes, Servers, Storage) |
| - Network Protocols (TCP/IP, Blockchain Protocols). |
| - Distributed Ledger Technology (DLT) |
+----------------------------------------------------------------------+
*****