Slide 1

Slide 1 text

Introduction to Bitcoin Saravanan Vijayakumaran Department of Electrical Engineering IIT Bombay October 12, 2017 EE Students Reading Group 1 / 27

Slide 2

Slide 2 text

What is Bitcoin?

Slide 3

Slide 3 text

What is Bitcoin? • Cryptocurrency • Open source • Decentralized 3 / 27

Slide 4

Slide 4 text

Decentralization Challenges • Counterfeiting • Currency creation rules • Double spending • Alice pays Bob n digicoins for pizza • Alice uses the same n digicoins to pay Carol for burgers • Centralization solves all three problems Solution without a central coordinator? 4 / 27

Slide 5

Slide 5 text

Double Spending • Familiar to academics • Submitting same paper to two conferences • Possible solution Reviewers google paper contents to find duplicates • Solution fails if • Conferences accepting papers at same time • Conference proceedings not published/indexed • Better solution A single public database to store all submissions to all conferences • What about spam? 5 / 27

Slide 6

Slide 6 text

The Blockchain Bitcoin’s public database for storing transactions Block Header List of Transactions Block Header List of Transactions Block Header List of Transactions Block 0 (Genesis Block) Block 1 Block N (Present day) · · · I see blocks. Where is the “chain”? 6 / 27

Slide 7

Slide 7 text

Block Header nVersion hashPrevBlock hashMerkleRoot nTime nBits nNonce 4 bytes 32 bytes 32 bytes 4 bytes 4 bytes 4 bytes Previous Block Header nVersion hashPrevBlock hashMerkleRoot nTime nBits nNonce Current Block Header nVersion hashPrevBlock hashMerkleRoot nTime nBits nNonce Double SHA-256 SHA-256: NSA-designed cryptographic hash function 7 / 27

Slide 8

Slide 8 text

SHA-256 • Accepts bit strings of length upto 264 − 1 and outputs 256 bits • Easy to compute but difficult to invert • Given SHA-256(x), infeasible to find x • Collision resistant • Infeasible to find x = y with SHA-256(x) = SHA-256(y) • Pseudorandom function y3 y1 · · · y2 Bin 1 Bin 2 Bin 3 Bin 2256 − 1 Bin 2256 SHA-256 Messages M1, M2, M3, . . . 8 / 27

Slide 9

Slide 9 text

Mining • Process of adding new blocks to the blockchain • Nodes which want to perform transactions broadcast them Block Header Number of Transactions n Coinbase Transaction Regular Transaction 1 Regular Transaction 2 . . . Regular Transaction n − 1 nVersion hashPrevBlock hashMerkleRoot nTime nBits nNonce h = H(h0 h1) h0 = H(h00 h01) h00 = H(t0) t0 h01 = H(t1) t1 h1 = H(h10 h10) h10 = H(t2) t2 h10 • Miners collect some of these transactions into a candidate block • hashPrevBlock and hashMerkleRoot fields populated • hashMerkleRoot is root hash of transaction Merkle tree • nBits encodes a 256-bit target value T, say T = 0x 00 · · · 00 16 times FFFFF · · · FFFFF 48 times • Miner who can find nNonce such that SHA256 (SHA256 (nVersion HashPrevBlock . . . nNonce)) ≤ T can add a new block 9 / 27

Slide 10

Slide 10 text

Why should anyone mine blocks? • Successful miner gets rewarded in bitcoins • Every block contains a coinbase transaction which creates 12.5 bitcoins • Each miner specifies his own address as the destination of the new coins • Every miner is competing to solve their own search puzzle • Miners also collect the transaction fees in the block 10 / 27

Slide 11

Slide 11 text

Block Addition Workflow • Nodes broadcast transactions • Miners accept valid transactions and reject invalid ones (solves double spending) • Miners try extending the latest block Block N − 2 Block N − 1 Candidate Block B Candidate Block A Candidate Block C · · · • Miners compete to solve the search puzzle and broadcast solutions • Unsuccessful miners abandon their current candidate blocks and start work on new ones Block N − 2 Block N − 1 Block N Candidate Block B Candidate Block A Candidate Block C · · · 11 / 27

Slide 12

Slide 12 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Solution from miner A Solution from miner B 12 / 27

Slide 13

Slide 13 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Solution from miner A Solution from miner B • Both miners will broadcast their solution on the network 12 / 27

Slide 14

Slide 14 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Solution from miner A Solution from miner B • Both miners will broadcast their solution on the network • Nodes will accept the first solution they hear and reject others 12 / 27

Slide 15

Slide 15 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Solution from miner A Solution from miner B • Both miners will broadcast their solution on the network • Nodes will accept the first solution they hear and reject others MA MB A A A A A A B B B B B B B 12 / 27

Slide 16

Slide 16 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Solution from miner A Solution from miner B • Both miners will broadcast their solution on the network • Nodes will accept the first solution they hear and reject others MA MB A A A A A A B B B B B B B • Nodes always switch to the longest chain they hear 12 / 27

Slide 17

Slide 17 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Block N + 1 Block N + 2 Stale block • Both miners will broadcast their solution on the network • Nodes will accept the first solution they hear and reject others MA MB A A A A A A B B B B B B B • Nodes always switch to the longest chain they hear 12 / 27

Slide 18

Slide 18 text

What if two miners solve the puzzle at the same time? Block N − 2 Block N − 1 Block N · · · Block N + 1 Block N + 2 Stale block • Both miners will broadcast their solution on the network • Nodes will accept the first solution they hear and reject others MA MB A A A A A A B B B B B B B • Nodes always switch to the longest chain they hear • Eventually the network will converge and achieve consensus 12 / 27

Slide 19

Slide 19 text

How often are new blocks created? • Once every 10 minutes nVersion hashPrevBlock hashMerkleRoot nTime nBits nNonce • Every 2016 blocks, the target T is recalculated • Let tsum be the time take to mine the last 2016 blocks Tnew = tsum 14 × 24 × 60 × 60 × T 13 / 27

Slide 20

Slide 20 text

Tamper Resistance • Suppose Alice wants to modify block BN BN−1 BN BN+1 · · · BN+m−1 · · · Block Height N − 1 N N + 1 · · · N + m − 1 • Alice works on AN branch; other miners work on BN branch BN−1 BN AN BN+1 AN+1 · · · · · · BN+n−1 AN+n−1 AN+n · · · Block Height N − 1 N N + 1 · · · N + n − 1 N + n • She needs to mine blocks faster than the rest of the miners • Possible if she controls 50% or more of network hashrate 14 / 27

Slide 21

Slide 21 text

Bitcoin Supply • The coinbase reward was initially 50 BTC per block • Halves every 210,000 blocks ≈ 4 years • Became 25 BTC in Nov 2012 and 12.5 BTC in July 2016 • Total Bitcoin supply is 21 million • The last bitcoin will be mined in 2140 15 / 27

Slide 22

Slide 22 text

Bitcoin Transactions

Slide 23

Slide 23 text

Coinbase Transaction Format Block Header Number of Transactions n Coinbase Transaction Regular Transaction 1 Regular Transaction 2 . . . Regular Transaction n − 1 Amount x1 Challenge Script C1 Amount x2 Challenge Script C2 Coinbase Transaction Output 0 Output 1 nValue scriptPubkeyLen scriptPubkey Output Format Block Format • nValue contains number of satoshis locked in output • scriptPubkey contains the challenge script • scriptPubkeyLen contains byte length of challenge script 17 / 27

Slide 24

Slide 24 text

Regular Transaction Format TXID1 Output Index = 0 Response Script R1 TXID1 Output Index = 1 Response Script R2 TXID2 Output Index = 0 Response Script R3 Amount y1 Challenge Script C4 Amount y2 Challenge Script C5 Input 0 Input 1 Input 2 Output 0 Output 1 One or more inputs Amount x1 Challenge Script C1 Amount x2 Challenge Script C2 Previous Regular Transaction with Transaction Identifier = TXID1 Output 0 Output 1 Amount x3 Challenge Script C3 Previous Coinbase Transaction with Transaction Identifier = TXID2 Regular Transaction Output 0 hash n scriptSigLen scriptSig nSequence nValue scriptPubkeyLen scriptPubkey Input Format Output Format • hash and n identify output being unlocked • scriptSig contains the response script 18 / 27

Slide 25

Slide 25 text

Bitcoin Scripting Language

Slide 26

Slide 26 text

Script • Forth-like stack-based language • One-byte opcodes OP_2 OP_3 OP_ADD 2 OP_3 OP_ADD 3 2 OP_ADD 5 Stack State Remaining Script 20 / 27

Slide 27

Slide 27 text

Challenge/Response Script Execution x1 x2 . . . xn y1 y2 . . . ym Stack State Remaining Script Response is valid if top element y1 evaluates to True 21 / 27

Slide 28

Slide 28 text

Challenge Script Example OP_HASH256 0x20 <256-bit string> S OP_EQUAL x OP_HASH256 0x20 S OP_EQUAL H(x) 0x20 S OP_EQUAL S H(x) OP_EQUAL 0 or 1 Stack State Remaining Script Unsafe challenge script! Guess why? 22 / 27

Slide 29

Slide 29 text

Pay to Public Key • Challenge script: 0x21 OP_CHECKSIG • Response script: OP_CHECKSIG OP_CHECKSIG OP_CHECKSIG True/False Stack State Remaining Script 23 / 27

Slide 30

Slide 30 text

Signatures Protect Transactions nVersion 0x02 hash0 n0 scriptSigLen0 scriptSig0 nSequence0 hash1 n1 scriptSigLen1 scriptSig1 nSequence1 0x02 nValue0 scriptPubkeyLen0 scriptPubkey0 nValue1 scriptPubkeyLen1 scriptPubkey1 nLockTime nVersion 0x02 hash0 n0 prevScriptPubkeyLen0 prevScriptPubkey0 nSequence0 hash1 n1 0x00 nSequence1 0x02 nValue0 scriptPubkeyLen0 scriptPubkey0 nValue1 scriptPubkeyLen1 scriptPubkey1 nLockTime nHashType Regular Transaction Message for Input 0 signatures Input 0 Input 1 Output 0 Output 1 Input 0 Fields Input 1 Fields Output 0 Fields Output 1 Fields 24 / 27

Slide 31

Slide 31 text

Summary • Bitcoin’s blockchain prevents double spending and tampering • Secure only if nobody controls 50% or more of network hashrate • Mining difficulty adjusted to regulate coin supply • Miners incentivized by coinbase reward • Script allows flexible spending conditions • Signatures prevent tampering of unconfirmed transactions 25 / 27

Slide 32

Slide 32 text

Learning Resources • Books • Princeton book http://bitcoinbook.cs.princeton.edu/ • Mastering Bitcoin, Andreas Antonopoulos • Papers • SoK Paper http://www.jbonneau.com/doc/ BMCNKF15-IEEESP-bitcoin.pdf • CABRA https://github.com/cdecker/btcresearch • Notes • https://www.ee.iitb.ac.in/~sarva/bitcoin.html 26 / 27

Slide 33

Slide 33 text

Thanks for your attention 27 / 27