Block Chain DLT
Block Chain DLT
● Major challenge:
● what prevents the adversary from learning how to sign
messages by analyzing the verification-key?
● Exercise: construct a digital signature based on a hash-
function that is one-time secure (i.e., it is secure for
signing only a single message)
Digital Signature Implementations
● Bittrex
● Kraken
● Coinbase
● CoinMama
● SpectroCoin
● BitPanda
● LocalBitcoins (Buy / Sell from people near you)
● Bisq (Decentralized)
● Friends!!
Order book
Coinmarketcap
Addresses
Addresses
Trivial solution:
● Client does not delete D
● When server returns D’, client compares D and D’
...what if client doesn’t have enough memory to store D for a long
time?
File storage: Hash-based protocol
● Client creates Merkle Tree root MTR from initial file data D
● Client sends file data D to server
● Client deletes data D, but stores MTR (32 bytes)
● Client requests chunk x from server
● Server returns chunk x and short proof-of-inclusion π
● Client checks that chunk x is included in MTR using proof π
Merkle tree: proof of inclusion
Merkle tree: proof of inclusion
Merkle tree: proof of inclusion
Merkle tree: proof of inclusion
Merkle tree: proof of inclusion
● Start at root
● Split string into characters
● For every character, follow an edge labelled by that character
● If edge does not exist, create it
● Mark the node you arrive at
Tries: query(string)
● Start at root
● Split string into characters
● For every character, follow an edge labelled by that character
● If edge does not exist, return false
● When you arrive at a node and your string is consumed,
check if node is marked
● If it is marked, return yes
● Otherwise, return no
Tries: example
● Space-optimized trie
● An isolated path (with nodes which are only children) with
unmarked nodes is merged into one edge
● The label of the merged edge is the concatenation of the
merged symbols
Tries / Patricia tries as key/value store
{ 'cab8': 'dog', 'cabe': 'cat', '39': 'chicken', '395': 'duck', '56f0': 'horse' }
Merkle patricia trie: example
Merkle patricia trie: example
Proof-of-work in blocks
● Computer programs
● Contract code is executed by all full nodes
● The outcome of a smart contract is the same for everyone
● Context: Internal storage, transaction context, most recent blocks
● The code of a smart contract cannot change
Bitcoin programs
● Transaction: a transfer of value in the Bitcoin network
● Each transaction consists of the following main fields:
○ input: a transaction output from which it spends bitcoins:
i. previous transaction address
ii. Index
iii. ScriptSig
○ output: instructions for spending the sent bitcoins:
i. value: amount of bitcoins to send
ii. ScriptPubKey: instructions on how to spend the sent
bitcoins
● To validate a transaction:
○ concatenate ScriptSig of the current transaction with
ScriptPubKey of the referenced transaction
○ check if it successfully compiles with no errors
Bitcoin Script
● Stack-based
● Data in the script is enclosed in <>: <sig>, <pubKey>, etc
● Opcodes: commands or functions
○ Arithmetic, e.g. OP_ABS, OP_ADD
○ Stack, e.g. OP_DROP, OP_SWAP
○ Flow control, e.g. OP_IF, OP_ELSE
○ Bitwise logic, e.g. OP_EQUAL, OP_EQUALVERIFY
○ Hashing, e.g. OP_SHA1, OP_SHA256
○ (Multiple) Signature Verification, e.g. OP_CHECKSIG,
OP_CHECKMULTISIG
○ Locktime, e.g. OP_CHECKLOCKTIMEVERIFY,
OP_CHECKSEQUENCEVERIFY
Bitcoin Script example
Bitcoin’s scripting language limitations
● Namecoin:
○ Bitcoin fork: Currency NMC
○ Decentralized name registration database: DNS, identities etc
● Colored coins:
○ On top of Bitcoin
○ Allows people to create their own digital currencies
● OmniLayer (formerly Mastercoin)
○ On top of Bitcoin
○ Distributed exchange, smart property, distributed e-commerce, etc
● OpenBazaar
○ On top of Bitcoin
○ Decentralized marketplace
Ethereum
Same principles as Bitcoin
● UTXOs pros:
○ Higher degree of privacy
○ Scalability (parallelism, sharding)
● Accounts pros:
○ Space saving
○ Better fungibility
○ Simplicity
○ Efficiency
Two types of accounts
● Similar to Bitcoin
● Blocks contain: transaction list and most recent state
● Block time: ~12 - 15 seconds
● Proof-of-work: Ethash (designed to be memory-hard)
● Casper: Future transition to proof-of-stake
● Winner of the block: 3 ETH
Ethereum Mining
● State variables:
○ Contract variables
○ Permanently stored in contract storage
○ Must declare at compilation time
● Local variables
○ Within a function: cannot be accessed outside
○ Complex types: at storage by default
○ Value types: in the stack
○ Function arguments: in memory by default
Types
● Storage:
○ Persistent.
○ All state variables are saved to storage.
○ Function’s complex local values are saved to storage by
default. (Solidity versions >= 5.0.0 force you to declare the data
location).
● Memory: ○ Non-persistent.
○ Function’s arguments and returned values are stored to
memory by default. (Solidity versions >= 5.0.0 force you to declare
the data location for complex types).
Data location: assignment
● Multiple inheritance
● One contract is created on the blockchain: codes concatenates
● The general inheritance system is very similar to Python’s
Solidity: Inheritance
● Ether Units
○ A literal number can take a suffix of wei, finney, szabo or ether (2
ether == 2000 finney evaluates to true)
● Time Units
○ Suffixes like seconds, minutes, hours, days, weeks and years (1
hours == 60 minutes)
Solidity: units and globally available
variables
● Error Handling
○ assert
○ require
○ revert
● Mathematical and Cryptographic Functions
○ addmod, mulmod
○ Keccak256 (SHA-3), sha256, ripemd160
Solidity: units and globally available
variables
● Address Related
○ <address>.balance
○ <address>.transfer
○ <address>.send
○ <address>.call, <address>.callcode, <address>.delegatecall
● Contract Related
○ this, selfdestruct
Send ether
Send ether
Interacting with other contracts
Interacting with other contracts
Interacting with other contracts
Security and fairness of Smart
Contracts
Known Attacks
DoS: Unbounded operation
DoS: Unbounded operation
DoS: Wallet Griefing
DoS: Wallet Griefing
Forcibly Sending Ether to a Contract
● Exploits
○ misuse of this.balance
○ Complicated (vulnerable) fallback function
● How can you send ether to a contract without firing contact’s
fallback function ?
○ selfdestruct(victim)
○ Contract’s address = hash(sender address, nonce)
○ Anyone can calculate a contract’s address before it is created
(contract addresses generation is deterministic) and send ether to
that address.
Reentrancy
Reentrancy
Reentrancy
Reentrancy
Reentrancy
Reentrancy
Reentrancy
Reentrancy
Re-entrancy in the wild: The DAO
The DAO Attack
Reentrancy: solutions
Checks-Effects-Interactions Pattern
● Do not transfer ether to users (push) but let the users withdraw
(pull) their funds.
● Isolates each external call into its own transaction.
● Avoids multiple send() calls in a single transaction.
● Reduces problems with gas limits.
● Trade-off between security and user experience.
Pull over push: example
Keep fallback function simple
Front-Running
Front-Running: user
Front-Running: user
Front-Running: miner
Front-Running: example
Front-Running: example
Front-Running: example
Front-Running: example
Front-Running: example
Randomness
Randomness: sources (?)
Randomness: sources (?)
Randomness
Randomness: blockhash
Randomness: blockhash
Randomness: attack pattern
Randomness: intra-transaction
information leak
What about future blocks ?
Is the hash of a block in the future a
good source of randomness against a
malicious miner ?
Randomness: towards safer PRNG
● Common Prefix
● Chain Quality
● Chain Growth
Common Prefix, Ι
Common Prefix, ΙΙ
Racing Attacks
Attacher splits from the main chain and tries to overtake the
“honest chain”
=> Common prefix breaks
Intuition why the attack is a small probability event: concentration
bounds help honest parties.
Chain Growth, I
Chain Growth, II
Chain Quality, II
Block withholding attack
● (Potentially) simpler:
● run consensus for each set of transactions (blocks) with each
party proposing a set of transactions.
● plain validity is insufficient.
Consensus <= Ledger, I
int counter;
counter = 0
while Hash( block_header, counter) > Target
increment counter
int counter;
counter = 0
while Hash( block_header, counter) > Target
increment counter
https://en.bitcoin.it/wiki/Non-specialized_hardware_comparison
Parallelising mining
● Now you are given a choice: you can decrease the size of the
clay pigeon target by a ratio β and augment your “kills” by
multiplying with 1/β.
● Suppose your accuracy is just linear with β.
● do you accept to play like this (while your opponent will
keep playing in the same way) ?
Analysis, IV
Mining parameters
Mining incentives
● Extremely volatile
● April 2019: 1 BTC = 4,429 EUR
● mid 2018: 1 BTC = 5,502 EUR
● late 2017: 1 BTC = 17,000 EUR
● beginning 2015: 1 BTC = 208 EUR
● max 2013: 1 BTC = 900 EUR
● min 2013: 1 BTC = 73 EUR
● 2012: 1 BTC = 4 EUR
● 2010: 1 BTC = 0.06 EUR
● 22 May 2010: First real purchase with bitcoin
Market capitalization
● T0 = 2256 - 32
● The target T is not constant but dynamically adjusted
● This is the way the network ensures a rate of 1 block per 10
minutes
● T is adjusted collectively by the network
● Adjustment happens every 2016 blocks (expected 2 weeks)
● The period of 2016 blocks is known as an epoch
● Difficulty is adjusted so that if the previous 2016 blocks had
been generated with the new T, they would have taken exactly 2
weeks
Difficulty adjustment
● T0 = 2256 - 32
● The target T is not constant but dynamically adjusted
● This is the way the network ensures a rate of 1 block per 10
minutes
● T is adjusted collectively by the network
● Adjustment happens every 2016 blocks (expected 2 weeks)
● The period of 2016 blocks is known as an epoch
● Difficulty is adjusted so that if the previous 2016 blocks had
been generated with the new T, they would have taken exactly 2
weeks
The target adjustment equation
Difficulty
- Why can’t a pool miner mine shares with the pool address, but
blocks with his own address?
- Why can’t a pool miner mine shares with the pool address, but
blocks with his own address?
- - They don’t know if it will be a share or a block during mining!
After mining is completed, changing the address will invalidate
the PoW.
Ways to mine
CPU: Mining takes place in the CPU
GPU: Mining takes place in the graphics card (high parallelization)
ASIC: Specialized hardware for mining, separate hardware device
Is it worth it to mine? Probably not...
● The SPV wallet sends to the SPV server the bitcoin addresses they
have
○ But not the private keys!
○ The SPV server knows which transactions to send to the SPV client
○ The bitcoin addresses are shared in the form of a Bloom filter
● The SPV wallet verifies each block’s PoW and authenticated ancestry
○ Keeps a longest chain as usual -- but without transactions
● The SPV wallet verifies each tx it receives
○ Sigs
○ Law of conservation
● The SPV wallet verifies that the tx belongs to the Merkle Tree root of
a block
SPV
SPV Security
Paper wallet
● Private key is printed on a piece of paper
● Can be kept in a physical safe or a real bank vault
● Can optionally be encrypted with a secret password (which is
remembered) Brain wallet
● Private key is literally SHA256(“my dog’s name is Barbie”) or
some other passphrase
● Full private key can be recovered by memory
● Extremely unsafe! (More than $100,000 stolen due to low
entropy passwords)
Hardware wallets
● All the participants are rational and want to maximize the utility
they obtain at the end of the execution.
● Utility of a participant is a function that takes as input the
strategies of all the participants and has as output a real number
that represents the gains of this participant at the end of the
execution.
Protocol is Nash Equilibrium, II
Protocol is Nash Equilibrium, III
Protocol is Nash Equilibrium, IV
Generalisation to Coalitions
Is Bitcoin a Nash Equilibrium ?