Bitcoin and how it works
Clear, exam-focused notes on Bitcoin: its data structures (blocks, transactions, UTXO, Merkle tree), mining and proof-of-work, block validation and fees, with formulas and worked examples for engineering students.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Bitcoin matters because it is the first widely-used example of a secure, decentralised digital currency. Engineers building systems that interact with Bitcoin (wallets, miners, block explorers, exchanges) must understand how transactions are formed and verified, how blocks are produced (mining / proof-of-work), and how the network prevents double-spending without a central authority.
Key ideas
Start from first principles and define terms before use.
- Bitcoin: a protocol and network that implements a decentralised ledger of transfers of value (the unit of account is "bitcoin", plural often written "BTC").
- Blockchain: a data structure that records blocks in a strict sequence where each block contains a cryptographic link (hash) to the previous block; this link gives immutability and order.
- Block: a package of data containing a list of transactions plus a block header (metadata). Each block extends the blockchain when accepted.
- Transaction: a signed instruction that consumes previous unspent transaction outputs (UTXOs) and creates new UTXOs. A UTXO is an atomic chunk of bitcoin balance that can be spent once.
- UTXO (Unspent Transaction Output): a unit created by a transaction output; it records a value (amount) and a locking script (conditions to spend it). Transactions reference UTXOs by transaction id and output index.
- Miner: a network participant that collects transactions from the mempool, builds a candidate block, and runs the proof-of-work search to find a valid block header.
- Block header: contains fields: version, previous block hash, Merkle root (hash of all transactions), timestamp, bits (compact target), and nonce. Describe exactly what to draw: draw a rectangle labelled "Block header" with six sub-rectangles labelled (from left) "version", "prev_block_hash", "merkle_root", "timestamp", "bits (target)", "nonce". Below it draw a second rectangle labelled "Transactions" listing TX1, TX2, TX3… with the first TX being the coinbase.
- Nonce: a 32-bit field the miner changes when searching. Because a single 32-bit nonce may be insufficient to explore all candidate headers, miners also change the coinbase transaction (extraNonce) or other header fields to vary the hashed header.
- Merkle tree and Merkle root: the Merkle tree is a binary hash tree of transaction hashes; pair up transactions (duplicate last hash if odd count), compute parent hashes until one root remains. The Merkle root is placed into the block header and links the header to that exact transaction set. For an exam diagram: draw leaf hashes h1,..,h4, pair h1+h2->h12, h3+h4->h34, then h12+h34->root; label leaf txids as hash256(tx serialization) and internal nodes as hash256(left || right), where hash256 denotes double SHA-256; Bitcoin uses double SHA-256 for block headers and for transaction IDs (txid and wtxid), and the Merkle tree computes each parent as double SHA-256 of the concatenated child hashes.
- Proof-of-work (PoW): miners must find a header such that the double SHA-256 hash of the block header interpreted as a 256-bit unsigned integer is less than or equal to the target value encoded in the header (bits field). That is: H = SHA256(SHA256(header)); accept when H <= target.
- Target and difficulty: target is a number that controls how hard it is to find a valid hash. Difficulty is a normalized measure: difficulty = difficulty_1_target / current_target. Difficulty adjusts every 2016 blocks to keep average block time ≈ 10 minutes (expected interval 1209600 seconds). The adjustment is bounded (cannot change by more than 4× in one retarget in original Bitcoin rules).
- Block reward and fees: a miner who creates a block may include a special first transaction (the coinbase transaction) which claims the block subsidy (newly minted coins) plus transaction fees collected from transactions in the block. The coinbase transaction has no inputs. The subsidy halves every 210,000 blocks. (State the initial subsidy as 50 BTC.) Coinbase outputs require 100-confirmation maturity before spending.
- Double-spend: a situation where the same UTXO is used in two different transactions. The blockchain prevents final double-spend by preferring the longest (most cumulative PoW) valid chain; unconfirmed transactions can still be replaced or dead if conflicting transactions are mined.
- Transaction scripts: Bitcoin uses a simple stack-based language called Script. A transaction input contains a scriptSig (unlocking script) which, when combined with the output's scriptPubKey (locking script), must leave a true value on the stack after running Script. In SegWit (witness) spends, signatures and related data are carried in the witness (not scriptSig); the txid excludes witness data while the wtxid includes it.
- Mempool: the set of valid but unconfirmed transactions that nodes have accepted and may be included in future blocks.
Assumptions commonly used in proofs/exams: cryptographic hash functions (SHA-256) act as random oracles for miners' search; network propagation delays are small compared to block interval; majority of mining power behaves honestly.
Formulas
H = SHA256(SHA256(header)) — H is the 256-bit hash of the block header; no SI unit; applies when checking header PoW.
Valid if H ≤ target — condition for a header to be an accepted proof-of-work.
difficulty = difficulty_1_target / target — difficulty is unitless relative measure; difficulty_1_target is the canonical target at difficulty 1. difficulty_1_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000. Target from bits (nBits): let E be the first byte and C the next three bytes of nBits as an unsigned integer; target = C × 2^(8·(E−3)).
Expected_hashes ≈ difficulty · 2^32 — expected number of hashes to find a block at given difficulty (2^32 ≈ 4.294967296×10^9). Hashes is dimensionless count.
Expected_time (s) = (difficulty · 2^32) / H_rate — H_rate is hash rate in hashes per second (H/s); result in seconds. Use this to compute expected time for a miner or pool. P(at least one block in time t) = 1 − exp(−t / Expected_time) — mining arrivals are well-modeled as a Poisson process with rate λ = H_rate / (difficulty · 2^32).
fee = sum(inputs) − sum(outputs) — fee is in BTC (or satoshis); inputs and outputs measured in BTC or satoshis (1 BTC = 10^8 satoshis).
Total_supply_after_n_halvings = 210000 × 50 · 10^8 satoshis × sum_{i=0}^{n-1} (1/2)^i — describes supply minted per halving epoch (useful to derive cap 21 million BTC). (This formula is blocks per epoch × subsidy per block; summing epochs gives the 21 million BTC cap in the limit.)
Note: use satoshis (1 Satoshi = 1e-8 BTC) as smallest unit when doing exact integer arithmetic.
Worked examples
Example 1 — Mining expected time (numerical, step-by-step) Given: difficulty = 5.0×10^9 (5 billion), miner hash rate H_rate = 100 TH/s = 100 × 10^12 H/s. Step 1: write formula: Expected_time = (difficulty · 2^32) / H_rate. Step 2: compute 2^32 = 4.294967296×10^9. Step 3: expected_hashes = 5.0×10^9 × 4.294967296×10^9 = 2.147483648×10^19 hashes. Step 4: expected_time (s) = 2.147483648×10^19 / (100×10^12) = 2.147483648×10^19 / 1×10^14 = 2.147483648×10^5 s. Step 5: convert to hours: 2.147483648×10^5 s ÷ 3600 ≈ 59.65 hours ≈ 2.49 days. Answer: ≈ 2.49 days expected to find one block at that difficulty and hash rate.
Example 2 — Simple transaction composition and change (algorithmic trace) Alice has two UTXOs: UTXO_A = 5.0 BTC, UTXO_B = 3.0 BTC. She wants to send 6.0 BTC to Bob and pay fee 0.001 BTC. Step 1: required total = 6.0 + 0.001 = 6.001 BTC. Step 2: choose inputs: use UTXO_A (5.0) and UTXO_B (3.0) total inputs = 8.0 BTC. Step 3: outputs: Output1 to Bob = 6.0 BTC; Output2 change to Alice = inputs − outputs − fee = 8.0 − 6.0 − 0.001 = 1.999 BTC. Step 4: transaction contains input references to UTXO_A and UTXO_B and an output to Bob and an output back to Alice (1.999 BTC). Alice signs each input with her private key producing scriptSig values. Step 5: after broadcast, miners may include this TX into a block; fee included in miner reward calculation. Final: Outputs: Bob 6.0 BTC, Alice change 1.999 BTC; fee 0.001 BTC.
(Also mention: coin selection algorithms and dust thresholds are implementation details; exam-level answer should show the calculation and the change output.)
Common mistakes
- Using fee = outputs − inputs sign reversed. Correct is fee = sum(inputs) − sum(outputs).
- Forgetting coinbase maturity (100 blocks) when reasoning about spendability of minted coins.
- Misstating retarget interval/time: Bitcoin retargets every 2016 blocks targeting 2016×10 minutes = 1209600 s; the adjustment is bounded (verify constants). Put numeric values only when sure.
- Forgetting to duplicate last leaf in Merkle tree when odd number of transactions (Bitcoin duplicates last hash).
- Treating SHA-256 as reversible; it's one-way and collision-resistant (assume as cryptographic property, but exact security claims require more precise cryptanalysis beyond scope).
For your exam
Question types you will see: definitions (define blockchain, UTXO, Merkle root), diagram-based questions (draw block header and label fields; draw Merkle tree computation), derivations (expected time to find a block given hash rate and difficulty), short notes (proof-of-work, transaction lifecycle, double-spend defence), numerical problems (fees, change, mining probability), and algorithm traces (script execution or coin selection).
What full-marks answers include:
- Clear definitions with bold term names (UTXO, Merkle root, proof-of-work).
- Correct block header field list and explanation of each field's role.
- For derivations: write the formula, substitute numbers with units (hashes/s), show intermediate arithmetic and unit cancellations, and final answer with correct units.
- For diagrams: draw and label block header fields, show Merkle tree pairing and root, and indicate where coinbase transaction appears.
- Note assumptions used (random hash distribution, negligible network latency compared to block time, etc.).
Quick check
- What is a UTXO?
- State the PoW condition for an accepted Bitcoin block header in one line.
- If a miner has hash rate H and difficulty d, write the formula for expected time to find a block. Answers: 1) A UTXO is an Unspent Transaction Output: a discrete chunk of bitcoin value created by a transaction output that can be spent later by providing a satisfying unlocking script. 2) A block is valid if SHA256(SHA256(header)) interpreted as a 256-bit integer is ≤ target encoded by the header. 3) Expected_time (s) = (difficulty · 2^32) / H_rate.
Notes for reviewer: I have avoided asserting current block subsidy numeric value and current network difficulty, and I stated standard protocol constants (2016-block retarget, 210000-block halving, 100-block coinbase maturity, 2^32 factor) that should be verified against the latest Bitcoin Core documentation where noted below.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?