Lecture 2
Lecture 2
(cs251.stanford.edu)
Bitcoin Mechanics
Dan Boneh
Applications:
signing
secret signing
algorithm public verification
key (sk) key (pk)
Digital signatures: syntax
Def: a signature scheme is a triple of algorithms:
• Gen(): outputs a key pair (pk, sk)
• Sign(sk, msg) outputs sig. σ
• Verify(pk, msg, σ) outputs ‘accept’ or ‘reject’
Sign(sk, m) ⇾ σ, Verify(pk, m, σ) ⇾
accept/reject
signing key
verification key
Bitcoin mechanics
This lecture: Bitcoin mechanics
Oct. 2008: paper by Satoshi Nakamoto
Jan. 2009: Bitcoin network launched
Total market value:
Sep. 2023: $528B
This lecture: Bitcoin mechanics
user facing tools (cloud servers)
skA
skB
skC
typically, miners are connected to
eight other peers (anyone can join)
First: overview of the Bitcoin consensus
layer mempool
miners broadcast received Tx
to the P2P network
every miner:
validates received Tx and
stores them in its mempool
(unconfirmed Tx)
6.25 BTC
Selected miner is paid 6.25 BTC
in coinbase Tx (first Tx in the block)
• only way new BTC is created
• block reward halves every four years
⇒ max 21M BTC (currently 19.6M BTC)
Next week:
Safety / Persistence:
• to remove a block, need to convince 51% of mining power *
Liveness:
• to block a Tx from being posted, need to convince 51% of
mining power **
(some sub 50% censorship attacks, such as feather
forks)
Bitcoin blockchain: a sequence of block headers, 80 bytes each
genesis
block BH1 BH2 BH3
version (4 bytes)
H H prev H prev
prev (32 bytes)
time (4 bytes) …
bits (4 bytes)
nonce (4 bytes)
Tx root (32 bytes) Tx root Tx root
80 bytes
coinbase Tx
coinbase Tx
Bitcoin blockchain: a sequence of block headers, 80 bytes each
Merkle tree: payer can give a short proof that Tx is in the block
1855
2826
1128
2774
2075
2622
Block 648493
coinbase Tx
Tx structure (non-coinbase)
input[0] input: TxID 32 byte hash
inputs input[1] out-index 4 byte index
input[2] ScriptSig program
seq ignore
outputs output[0] TxID = H(Tx)
output[1] (excluding witnesses)
value 8 bytes
(segwit) witnesses output:
(part of input) ScriptPK program
(4 bytes)
locktime
#BTC = value/108
earliest block # that can include Tx
Example
null locktime
TxIn[0] TxOut[0] TxOut[1]
Tx1: input 2 ScriptPK 5 ScriptPK 0
(funding Tx) value value
UTXO1 UTXO2
UTXO: unspent Tx output
☓
null locktime
TxIn[0] TxOut[0] TxOut[1]
Tx1: input 2 ScriptPK 5 ScriptPK 0
(funding Tx) value value
UTXO1 UTXO2
UTXO: unspent Tx output
Tx0
6.25 + Tx fees =
Tx1 outputs
(Tx fee)
$2.11
130M
from TxInp[0]
Bitcoin Script
3. control:
99 OP_IF <statements> OP_ELSE <statements> OP_ENDIF
105 OP_VERIFY: abort fail if top = false
ECDSA malleability:
Given (m, sig) anyone can create (m, sig’) with sig ≠ sig’
⇒ miner can change sig in Tx and change TxID = SHA256(Tx)
⇒ Tx issuer cannot tell what TxID is, until Tx is posted
⇒ leads to problems and attacks
payer can specify complex conditions for when UTXO can be spent
P2SH
Miner verifies:
(1) <ScriptSig> ScriptPK = true ⟵ payee gave correct script
(2) ScriptSig = true ⟵ script is satisfied
Example P2SH: multisig