Professional Documents
Culture Documents
Blockchain
Under the Hood
by Galin Dinkov
https://www.linkedin.com/in/galindinkov/
Introduction
Motivation for Bitcoin Creation
● The initial idea was to create decentralized secure, open,
self-regulated digital money
● Accounts?
● Transactions?
● Security?
● Immutability?
● Consensus?
How They Made It?
Where are the balances?
● No balances
● Only transactions
● No user accounts
● Transactions are made
of Inputs and Outputs
● Transactions are
chained
● Coinbase transaction
generates initial coins
● Balance = Sum(UTXOs)
How to verify the owner?
How to ensure the transactions order?
● Order chronologically
● Immutability?
● Consensus?
How to reach consensus in the network?
● Mining
○ Only one version is accepted
○ Although forks exists
● Proof of Work
○ Hard computation
○ Такеs certain amount of time
○ Guarantees the immutability
How to deal with forks?
Going Deep...
Cryptography
Hash Function
● Maps any data to fixed bits data or number
● Deterministic
● One-Way
● Uniformly distributed
● Collisions
● Cryptographic hash functions
○ MD 2/4/5/6 - Message-Digest Algorithm
○ SHA 1/2/3 - Secure Hash Algorithms
○ RIPEMD - RACE Integrity Primitives
Evaluation Message Digest
○ BLAKE 1/2
Hash Usage
‘hello’
SHA-256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256d: 9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50
RIPEMD-160: b6a9c8c230722b7c748331a8b450f05566dc7d0f
Used:
● For Addresses
● Block IDs
● Transaction IDs
● Verifications / Checksums
● In Merkle Trees
Public-Key Cryptography
● Symmetrical (one key)
● Asymmetrical (public and private key)
● Key exchange
● Usage:
○ Encryption
○ Digital signature
○ Authentication
● Math behind:
○ Integer factorization problem
○ Discrete logarithm problem
○ Elliptic curve discrete logarithm
problem
Diffie-Hellman Key Exchange
Example: https://git.io/v7S3A
RSA
Example: https://git.io/v5exi
Elliptic curve cryptography (ECC)
ECC Domain Parameters:
p - prime field (modulo)
a,b - curve parameters
G - generator point
n - ord(G) (n x G = 0)
h - cofactor of n
K = 1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD * G
K = (x, y)
x = F028892BAD7ED57D2FB57BF33081D5CFCF6F9ED3D3D7F159C2E2FFF579DC341A
y = 07CF33DA18BD734C600B96A72BBC4749D5141C90EC8AC328AE52DDFE2E505BDB
Explanation:
https://youtu.be/dCvB-mhkT0w
https://youtu.be/F3zzNa42-tQ
Key Size Recommendations
Security Risks
● Pool cracking - https://lbc.cryptoguru.org
● Quantum computers
● NSA backdoors in ECC?
● Solution: post quantum cryptography
○ NTRUEncrypt - based on the shortest vector problem in a lattice
○ More: https://en.wikipedia.org/wiki/Post-quantum_cryptography
Merkel Tree
Merkle Tree
Keys &
Addresses
Bitcoin Address
Private Key (k) = Random Number < n - 1
Public Key (K) = k ∗ G
14fxjE3qepjaYxiqs5WPtDx5y7vMQP81mW
Wallets
● Keep private keys
● Types
○ Deterministic (Seeded) Wallets - private keys are derived from a common seed
○ Brain Wallet - derived from a word or phrase which one can remember
○ Hierarchical Deterministic Wallets (HD Wallet) - tree structure where new keys can be
generated from any child branch (public key) without revealing the private key
P2P Network
P2P Network
Node Types
● Reference Client (Bitcoin Core) - Wallet, Miner, Full Blockchain, Network Routing
● Connection
● Relay transactions
● Heartbeat
https://en.bitcoin.it/wiki/Network
Messages
● version - Information about program version and block count. Exchanged when first connecting.
● verack - Sent in response to a version message to acknowledge that we are willing to connect.
● addr - List of one or more IP addresses and ports.
● inv - "I have these blocks/transactions: ..." Normally sent only when a new block or transaction is being relayed. This
is only a list, not the actual data.
● getdata - Request a single block or transaction by hash.
● getblocks - Request an inv of all blocks in a range.
● getheaders - Request a headers message containing all block headers in a range.
● tx - Send a transaction. This is sent only in response to a getdata request.
● block - Send a block. This is sent only in response to a getdata request.
● headers - Send up to 2,000 block headers. Non-generators can download the headers of blocks instead of entire
blocks.
● getaddr - Request an addr message containing a bunch of known-active peers (for bootstrapping).
● submitorder, checkorder, and reply - Used when performing an IP transaction.
● alert - Send a network alert.
● ping - Does nothing. Used to check that the connection is still online. A TCP error will occur if the connection has
died.
Blockchain
Block Structure
missing predecessor
Forks
Transactions
Transaction Lifecycle
Transaction Structure
Transaction Structure
Signature and Verification
Standard Transaction Scripts:
● Pay-to-Public-Key-Hash (P2PKH)
● Pay-to-Public-Key
● Multi-Signature
● Data Output
● Pay-to-Script-Hash (P2SH)
● P2WPKH
● Validate transactions
consensus
Generation (Coinbase) Transaction
● Creates coins from nothing
● First 50KiB in the block are reserved for high priority transactions
● High priority transaction can be included in a block even without any fees
References
● Bitcoin Wiki, https://en.bitcoin.it/wiki/
● Bitcoin.ORG, https://bitcoin.org
Final
by Galin Dinkov
https://www.linkedin.com/in/galindinkov/