Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2023 L4 FITE7409-Bitcoin1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

FITE7409

Blockchain and cryptocurrency


Lecture 4 Bitcoin (Part 1)
Part 1: Introduction

(slides from Dr KP Chow.


some were adopted from Shamo’s course ECOM6016)
What is the Bitcoin price earlier?
What’s the Bitcoin price this week?
Topics to be discussed
• Introduction to Bitcoin
• Explore the Bitcoin blockchain
• Bitcoin transactions and mining
Cryptocurrency
• A cryptocurrency (or crypto currency) is a digital asset designed to
work as a medium of exchange that uses strong cryptography to
secure financial transactions, control the creation of additional units,
and verify the transfer of assets.
• Cryptocurrencies use decentralized control as opposed to centralized
digital currency and central banking systems.
• The decentralized control of each cryptocurrency works through
distributed ledger technology, typically a blockchain, that serves as a
public financial transaction database.
Top 10 Crytocurrencies (2021)
Let’s start with
money?
What is money?
• Money is any item or verifiable record that is
generally accepted as payment for goods and
services and repayment of debts, such as taxes,
in a particular country or socio-economic
context.
• Any item or verifiable record that fulfils these
functions can be considered as money.
• The money supply of a country consists of
currency (banknotes and coins).
Counterfeit money (copy of real money)
How to detect Security features
counterfeit money?
Electronic Money (e-cash)
• Digital “money” – sequence of bits
• Can there be “counterfeit” electronic money?
• What is copy of “e-cash”?
• Digital copy is identical to the original
• A totally new problem: double spending
Key issues in e-cash
• How is it issued? How to spend it?
• Counterfeiting
• Loss
• Fraud, double spending
• Efficiency
• Possible for offline use?
• Anonymity

• Many existing systems – none can solve all these problems


Cryptocurrency == e-cash ???

• Cryptocurrency – money is not in the form of bitstring!!!


What’s behind cryptocurrency?
The Ledger
• What is “Ledger”?
• The ledger is a permanent summary of all amounts entered in
supporting journals which list individual transactions by date.
• Every transaction flows from a journal to ledger(s).
• A journal is named “the book of original entry”, from when
transactions were written in a journal prior to manually posting them
to the accounts in the ledger.
Ledger Entries
• Title to many things (in physical world) by making ledger entries:
• Money
• Land (deed recorded in the Land Registry)
• Cars
• Securities
• Risks of ledger
• Theft, fire
• Forgery (e.g. altered by insiders)
• Hacking (for electronic ledger)
• How about multiple copies of ledgers?
• How about every stake holder has its own copy of the ledger?
• Distributed Ledger Technology (DLT)
Let’s look at properties
in Hong Kong
Have you bought a property in Hong Kong?
• Assume A will buy an apartment from B at the price of 5 million HK$
• Provision Sales and Purchase Agreement: at the time an agreement of
the sales price, with a “small deposit”, e.g. HK$ 50,000
• Sales and Purchase Agreement (S&P Agreement): has to go through
the lawyers, need to pay 10% of the sales price,
i.e. HK$ 500,000 – the small deposit (HK$ 50,000)
• Assignment: at the time the completion of the transaction, need to
pay the outstanding balance, either by cash or by mortgage loan, i.e.
4.5 million HK$
• Both the S&P Agreement and the Assignment have to register with
the Land Registry
Chain of Title
• Property ownership is define by “chain of title”, a sequence of “S&P
Agreements” leading from the original owner to the present owner
• The S&P Agreements are “recorded” in the Land Registry
• How to determine the owner? Searching the Registry
• Is the property owned by E?

S&P S&P S&P S&P


Seller: A Seller: B Seller: C Seller: D
Buyer: B Buyer: C Buyer: D Buyer: E
Timestamp Timestamp Timestamp Timestamp
Double-selling
• Can someone create a forged S&P Agreement?
• How to prevent double-selling?
• The lawyer has to go to the Land Registry to confirm the ownership of the
property
• Land Registry is the central authority to store the transactions
• Every transaction has to pay stamp duty – timestamping to prevent double-
selling
Distributed Registry
• Can we broadcast ALL S&P Agreements to a distributed network?
• IF the S&P Agreements are genuine AND members of the network
agree on the chain of title, THEN everyone can conclude who owns
the property
• How members of the network agree on something?
• Consensus protocol
• What are the assumptions? Sufficient number of honest members?
• No “Land Registry” is needed, i.e. no government control
Part 2a: Introduction to Transactions and
Bitcoin Network
What is a Bitcoin Really?
• No physical object, not even a character string
• A chain of digitally signed transactions from the original owner to the
current holder (similar to the chain of S&P Agreements)
• The transaction contains
• Hashes that are “difficult” to compute AND
• Virtual owner IDs, called addresses
• Any centralized bitcoin registry? NO
• Bitcoin chains (not bitcoins) are broadcast to everyone, and anyone
can verify the chains
Bitcoin
• Virtual electronic money
• About 300,000 transactions per day
• Anonymous, used by criminals, e.g. Silk Road, Ransomware
• Not tied to any bank or currency
• No centralized trust authority is needed
• Can prevent double spending
• Some questions
• How does it work?
• How is the bitcoin created?
• What form does it take?
Bitcoin Protocol
• Bitcoin (BTC) was invented in 2008 by “Satoshi Nakamoto”
• The Bitcoin protocol for generating and exchanging bitcoin is
• Public available
• Implemented in open source C++ code
• Anyone can obtain and run a bitcoin client
• The chains of title are maintained in a distributed ledger called the
blockchain
• Over 10,000 nodes around the world maintain copies of the entire
blockchain
Bitcoin Nodes
A Bitcoin Transaction (simple view)
• Suppose Bob owns 10 BTC
• He wants to sends 2 BTC to Alice, i.e. transfer ownership of 2 BTC
• He asks his Bitcoin client software to transfer 2 BTC to Alice’s “bitcoin
address”
• Alice’s Bitcoin client software generates a “bitcoin address” for her
• Bob’s Bitcoin client broadcasts the transactions (2 BTC from Bob’s
address to Alice’s address) to the entire bitcoin network
Let’s look at the
details
A Bitcoin Transaction
3
2
1
Alice’s public key
Only Bob can
is her bitcoin
transfer Bob’s
A bitcoin address.
bitcoin because
wallet does his private key
not contain Bob’s private key
is needed to
bitcoin. is associated with
digitally sign
Bob’s bitcoin
the transaction.
address from
which he is
transferring
money.
A Bitcoin Transaction
6
5
4
A block contains
The verification about 10
prevents double minutes worth
What is spending.
mining? of the world’s
transactions.
The “ledger”
Computations checks if Bob
to “verify” has enough
transactions bitcoins for the
(to be transaction.
discussed).
A Bitcoin Transaction 9

7 8
The miner
“mines” a
Anyone can confirmed
“Proof of Work” verify the block is
process. correctness rewarded
of any with “12.5”
It protects the miner’s work. bitcoins.
ledger from
alteration
because a huge
amount of work
is needed at this
step.
A Bitcoin Transaction

10
11

Transaction There is no
confirmation separate
takes time. “Payment” step.

Alice now got the


bitcoins that she
can use.
Many terms!
What are the meaning?
Digital signature in 1
slide

Alice’s
key pair
Bitcoin Key Pair
Hash Function
A transaction of 0.015 BTC from 0.1BTC with a
change of 0.0845BTC – where is the 0.0005BTC?
How many bitcoins do
you have?
Unspent Transaction Output (UTXO)
1 bitcoin = 100 million satoshis = 100,000k satoshis

• UTXO: fundamental building


block of a bitcoin
transaction
• Indivisible chunks of bitcoin
currency locked to a specific
owner, recorded on the
blockchain
• When a user receives
bitcoin, the amount is
recorded within the
blockchain as UTXO
How many bitcoin you have?
• Scattered as UTXO amongst hundreds of transactions and hundreds
of blocks, i.e. no such thing as a stored balance of a bitcoin address or
account
• The wallet calculates the balance by scanning the blockchain and
aggregating all UTXO belonging to the user
UTXO Properties
• Indivisible, e.g.
• If you have 20 bitcoin UTXO, you want to pay 1 bitcoin to Peter
• The transaction must consume 20 bitcoin UTXO and produce 2 outputs:
one paying 1 bitcoin to Peter, another paying 19 bitcoin back to your wallet
• A transaction can combine many UTXOs to compose an amount >= the
desired transaction amount
The Bitcoin Network
The Bitcoin Network
• A peer-to-peer network architecture on top of the Internet
• All participants are peers to each other, i.e. they are all equal and there are no
“special” nodes
• There is no server, no centralized service and no hierarchy within the network
• All nodes provide and consume services at the same time
• Bitcoin network refers to the collection of nodes running the bitcoin
P2P protocol
• Any other protocols?
• Stratum protocol: for mining and lightweight or mobile wallets
• Pool-mining protocol
•…
Full Node
• Maintain a complete and up-to-date
copy of the blockchain with all
transactions
• In early years, all nodes were full nodes
• Current implementation: Bitcoin Core
client (aka Satoshi client) is a full
blockchain node
• Can autonomously and authoritatively
verify any transaction without external
reference
• Lightweight client: do not maintain a full
blockchain
Nodes that are not full node
Extended
Bitcoin
Network
Simplified Payment Verification (SPV) Nodes
• Not all nodes store the full blockchain, e.g. Bitcoin clients run on
smartphones and tablets
• Simplified Payment Verification (SPV) method can operate without
storing the full blockchain: SPV client or lightweight client
• Become the most common form of bitcoin node, such as Bitcoin
wallets
• SPV nodes download only the block headers and do not download
the transactions in each block (about 1000 times smaller in size)
• SPV nodes unable to construct a full picture of all the UTXOs that are
available
How does SPV node verify transactions?
• Full node verify a transaction by constructing a fully verified
chain of thousands of blocks and transactions all the way to the
genesis block: verify a transaction by checking the entire chain of
blocks in order to guarantee that the UTXO is not spent
• SPV node verify the chain of all blocks (not all transactions) and
link that chain to the transaction of interest: verify a transaction
by checking how deep the block is buried by a handful of blocks
above it
Bitcoin Blockchain Depth vs Height

The height of a block is the number of blocks


in the blockchain between the given block
and the first block in the blockchain.
The depth of a block is the number of blocks
that have been appended to the blockchain
after the block in question.
Verify a transaction T in block 300,000
• For a full node, it links all 300,000 • For SPV node, it will establish a link
blocks down to the genesis block between the transaction T and the
(the height) and builds a full block that contains it (block 300,000)
database of UTXO, establishing the using a Merkle path
validity of the transaction T by • The SPV node then waits until it sees
confirming that the UTXO remains the six blocks 300,001 through 300,006
unspent piled on the top of the block 300,000
• The SPV node then verifies T by
establishing its depth under blocks
300,001 to 300,006
• The fact that other nodes on the
network accept block 300,000 and
produces 6 more blocks prove that the
transaction is not double spend
Limitations of SPV transaction verification
• SPV node will not conclude a transaction exists in a block when the
transaction does not exist
• SPV node can prove a transaction exists but cannot verify that
transaction doesn’t exist because it does not have a record of all
transactions, i.e. double spending of the same UTXO cannot be
detected
• SPV node needs to connect randomly to several nodes to increase the
probability that it is in contact with at least one honest node
• Well-connected SPV nodes are secure enough, striking the balance
between resource needs, practically, and security
Bitcoin Part 2b: Bitcoin Blockchain
Block of the
Blockchain
Blockchain Data Structure
• The blockchain data structure is an ordered, back-linked list of blocks
of transactions
• The Bitcoin Core client stores the blockchain metadata using Google’s
LevelDB database
• Each block is identified by a hash: SHA256(SHA256(Header))
• Each block references the previous block (parent block): “previous
block hash” field

… …

Reference to previous block The chain Reference to previous block

A Block A Block
Previous Block Hash
• Block hash: identity of the block
• The sequence of hashes linking each
Bitcoin Block Header
block to its parent creates a chain
going back to the first block: the Previous block
Timestamp
genesis block hash

• The “previous block hash” field is Merkle Tree Root Nonce


inside the block header, i.e. affects
the current block’s hash: the child’s
own identity changes if the parent’s
identity change
Key Feature of Blockchain Security
• Any changes to the parent’s transactions will change the hash of the
parent block, which will cause a change of the previous block hash of
the child block, which will further cause the change of the hash of the
child block.
• Characteristics of blockchain: the cascade effect ensures that once a
block has many generations following it, it cannot be changed
without forcing a recalculation of all subsequent block.
• The recalculation requires enormous computation, the existence of
long chain of blocks make the blockchain’s deep history immutable.
Block and Block Header
Block Header (80 bytes)
Block structure
Size Field Description
Size Field Description
4 bytes Version
4 bytes Block size
32 bytes Previous Block
80 bytes Block Header Hash
1-9 bytes Transaction 32 bytes Merkle Root Hash of the root of
(Varint) Counter the Merkle tree
Variable Transactions 4 bytes Timestamp
4 bytes Difficult Target
4 bytes Nonce
How to identify a block in the blockchain?
• Block identifier (block hash): hashing the block header twice using
SHA256, i.e. SHA256(SHA256(block header))
• Block hash of the first block:
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
• Block hash is not included inside the block’s data structure: it is
calculated by each node when the block is received from the network
• Another identifier: block height (the first block is at block height 0)

Block hash is unique, is block height a unique identifier for a block?


NO, 2 or more blocks might have the same block height,
competing for the same position in the blockchain
The Genesis Block
Linking Blocks
What is Merkle Tree?
• Each block contains a summary of
all the transactions in the block,
using a Merkle tree: an overall
digital fingerprint of the entire set
of transactions
• Merkle tree (aka binary hash tree)
is a data structure used for
efficiently summarizing and
verifying the integrity of large sets The cryptographic hash algorithm used is
of data: binary tree containing double-SHA256: SHA256 applied twice
cryptographic hashes Hash(A) = SHA256(SHA256(A))
Why use Merkle tree?
Faster
Merkle Tree Efficiency
Number of transactions Approx. size of block Path size (hashes) Path size (bytes)
16 transactions 4 KB 4 hashes 128
512 transactions 129 KB 9 hashes 288
2048 transactions 512 KB 11 hashes 352
65,535 transactions 16 MB 16 hashes 512
How a Bitcoin transaction works (in details)
How a Bitcoin transaction works –
1. Sender and receiver addresses
How a Bitcoin
transaction works –
2. Submitting a payment
How a Bitcoin transaction
works –
3. Verifying the transaction
How a Bitcoin
transaction works –
4. Mining
How a Bitcoin transaction works –
5. Transaction verified
From Transactions to
Blockchain
Bitcoin Addresses
• Bitcoin client software generates bitcoin addresses for users
• Sample address: 3JoxZAjWVJ7k2oybm78CFykdwrWiwxLGhK
• Bitcoin uses elliptic curve public key cryptography to create the key pair
that controls access to Bitcoins:
• The private key is a random number.
What’s elliptic curve?
• From the private key, the public key can be generated using elliptic curve
multiplication, which is a one-way cryptographic function
Bitcoin Public Key and Private Key
• The hash of the public key is used to receive Bitcoins and the private
key is used to sign transaction to spend those Bitcoins.
• When spending Bitcoins, the current Bitcoin owner presents his/her
public key and digital signature in a Bitcoin transaction to spend those
Bitcoins. Receiving
Bitcoin

Spending Bitcoin by
digitally signed the Everyone can verify
transaction with the transaction with
private key the public key
Bitcoin Public Key and Private Key
• The transaction is digital signed by a private key, which indicates the
authorization to spend the funds referenced by the transaction.
• With the public key and the digital signature, everyone in the Bitcoin
network can verify and accept the transaction as valid, confirming
that the person transferring the Bitcoins owned them at the time of
the transfer.
The Blockchain – What is a block?
• Transactions are put together in a block.
• The blocks are chained together by keeping the reference to the
previous block in each block. The chain of blocks is called the
blockchain.
• The block:
• Each block contains more than 500 transactions.
• The header contains the reference to the previous block as well as characteristics
of transactions in the current block.
• Each block contains a set of transactions and is identified by a hash value,
generated using the SHA256 cryptographic hash function on the header of the
block.
• The blockchain is the public distributed ledger of all Bitcoin transactions
starting from January 3, 2009 and is continuously growing.
The Blockchain

… …

Reference to previous block The chain Reference to previous block

A block A block
The Distributed Ledger – Blockchain
• The ledger is stored in the Bitcoin network in a decentralized fashion
and the Bitcoin protocol specifies how to update the ledger and how
to synchronize all ledgers in the Bitcoin network.
• Since the blockchain is the public distributed ledger of all Bitcoin
transactions, the number of bitcoins that can be spent by a private
key can be retrieved by anyone who has accessed to the blockchain.
• The user who has the private key can spent that number of Bitcoins
received with the corresponding address.
The Bitcoin Network – emergent consensus
• There is no central authority providing a clearinghouse service that
verifying and clearing all transactions.
• The blockchain is created by nodes in the Bitcoin network based on
the decentralized mechanism called emergent consensus.

The principles of
emergence in nature can
be applied in the Bitcoin
network to enable
emergent consensus for
block-size limiting,
referred to as “EC”
The Bitcoin Network
• The consensus among the nodes in the Bitcoin network is derived
from interaction of thousands of independent nodes in the Bitcoin
network.
• Each node verifies the transactions independently according to some
criteria, including whether the spender has sufficient Bitcoins to
spend, any malicious input injected, etc.
• With sufficient number of verifications by different nodes in the
Bitcoin network, the trust of transactions will be confirmed.
Transaction verification
• The transaction is broadcast on the Bitcoin network, where each
participant validates and propagates the transaction until it reaches
almost every node in the network.
• The transaction is verified by a mining node and included in a block
of transaction that is recorded on the blockchain.
• Once recorded on the blockchain and confirmed by sufficient
subsequent blocks, the transaction is a permanent part of the Bitcoin
open distributed ledger and is accepted as valid by all participants.
• The Bitcoins received by a new owner with a transaction can then be
spent in a new transaction.
A. Discrete entities
B. Interaction
C. High level order
D. Top-down causation

B
C D

You might also like