Cryptocurrencies lessons
A chain of blocks
A four block chain hashed with real SHA-256: one edit breaks every later block, then mining repairs them one by one.
A cryptocurrency is digital money with no bank in charge. Its record of who owns what is a chain of blocks, shared by thousands of computers.
Each block stores the hash of the block before it, a fingerprint made from its contents. Change one block, and every block after it breaks.
Repairing the chain means mining each broken block again, which takes real work. That work is what makes the record so hard to fake.
Money is a ledger
A ledger is a list of payments. Your bank balance is not a pile of coins, just a number worked out from the bank's ledger.
Start with what each person had, then apply every payment in order. A payment is only allowed if the payer has enough.
A ledger replayed row by row, with each person's balance updating and one payment refused for lack of funds.
A cryptocurrency keeps the same kind of ledger. The difference is who keeps it.
Practice: Read the ledger
Type one person's balance after a short random ledger of payments.
One ledger, many copies
A bank keeps its ledger on its own computers, so it is centralised. You have to trust the bank, and if its system goes down, nobody can pay.
A cryptocurrency is decentralised. Thousands of computers called nodes each keep a full copy of the ledger.
A network of nodes passing each new payment along until every copy has it; one node drops out and catches up later.
Each new payment is passed from node to node until every copy has it. If a node goes offline, the rest carry on, and it catches up when it returns.
Hash functions
A hash function turns any input into a short fingerprint called a hash. Bitcoin uses SHA-256, which always gives 256 bits, written as 64 hex digits.
The same input always gives the same hash. Change a single character and about half of the bits flip, which is called the avalanche effect.
A message changing one character at a time, its real SHA-256 hash, and the hex digits and bits that changed.
Hashes only work one way. Nobody can work back from a hash to its input, so all you can do is guess inputs and check.
Practice: Same hash or different?
Predict whether two inputs give the same hash, then see both real hashes.
Inside a block
A block holds its data (the payments), the previous hash and a number called the nonce. Hashing all of them together gives the block's own hash.
One block's fields lit in turn, hashed together into its hash, which becomes the next block's previous hash.
Editing an old block changes its hash. The next block's previous hash then no longer matches, so the break is easy to spot.
The very first block is called the genesis block. It has no block before it, so its previous hash is all zeros.
Practice: Find the edited block
Four blocks, one of them edited: find it from a hash that lost its zeros or a previous hash that no longer matches.
Mining
A block only counts if its hash starts with enough zeros. Its data and previous hash are fixed, so miners change the nonce and hash again.
There is no shortcut, so miners try nonce after nonce until one works. This is called proof of work.
A block mined live at one, two and three zeros, counting the tries against the average.
The first miner to succeed adds the block and earns its reward plus the payment fees. Since April 2024, each Bitcoin block creates 3.125 new bitcoins for its miner.
Difficulty
Each hex digit of a hash is one of 16 values, so it is 0 with a chance of 1 in 16. Each extra zero makes the search 16 times longer, on average.
\text{chance one digit is } 0 = \frac{1}{16}\text{chance of } 00 = \frac{1}{16} \times \frac{1}{16} = \frac{1}{256}(each digit is a fresh 1 in 16)\text{average tries for } n \text{ zeros} = \textcolor{#8b5cf6}{16^n}
Bitcoin resets its difficulty every 2,016 blocks, so that a block is found about every 10 minutes. In 2026, a valid Bitcoin block hash starts with about 19 zeros.
Finding a nonce is slow, but checking one is fast. Anyone can hash the block once and count the zeros.
Practice: How many tries?
Type the average number of tries for a number of leading zeros, or how many times harder one target is than another.
Practice: Check the miner's work
Four miners claim a nonce: hash each claim once and pick the one whose hash really starts with 00.
Keys and wallets
To spend coins you need a private key, a secret random number 256 bits long. From it, your wallet works out a public key, and from that an address that people can pay.
Each step only works one way. Anyone can see your address, but nobody can work back to your private key.
A real key pair made in the browser: private key, then public key, then an address hashed from it, each step one way.
A wallet is an app or device that stores your keys. The coins themselves are entries on the blockchain, not files in the wallet.
Digital signatures
To pay someone, your wallet signs the payment with your private key. The signature is a number that only your private key could have made.
Anyone can check the signature using your public key. If even one character of the payment changes, the check fails.
A payment signed and verified for real in the browser, then changed by one character so the same signature fails.
So nobody can spend your coins without your private key. Nobody can change your payment on its way, either.
Practice: Keys and signatures
Match private key, public key, address, signature and seed phrase to what each one does.
Transactions and the mempool
A signed payment is called a transaction. Your wallet sends it to a few nodes, which check it and pass it on.
Waiting transactions sit in a pool called the mempool. Blocks have limited room, so miners pick the transactions that pay the highest fees first.
Transactions arriving in the mempool with fees; each block takes the four highest fees and the rest wait.
Once your transaction is in a block, it is confirmed. Each new block on top makes it harder to undo, so people often wait for a few.
Practice: A transaction's journey
Put the steps of a payment, and of mining a block, in order.
Agreeing on one chain
Sometimes two miners find a block at almost the same moment. For a short while, nodes disagree about which one comes next.
Two blocks found at once split the network; the next block lands on one branch and every node switches to it.
Nodes always follow the chain with the most work behind it, usually the longest. When the next block lands on one branch, everyone switches to it.
To rewrite history, a cheater would need more computing power than the rest of the network put together. This is called a 51% attack.
Proof of stake
Proof of stake replaces mining with a deposit. Validators lock up coins, and the network picks one at random to add each block.
The bigger the stake, the more often a validator is picked. A validator who cheats loses part of their deposit, which is called slashing.
Four validators with different stakes, picked at random block after block; the tally settles to match the stakes.
Ethereum switched to proof of stake in September 2022 and cut its energy use by over 99.9%. Bitcoin still uses proof of work, which uses about as much electricity as a mid-sized country.
Practice: Work or stake?
Sort statements into proof of work, proof of stake, or both.
Smart contracts and tokens
A smart contract is a program stored on a blockchain. It runs exactly as written whenever someone calls it, with nobody in charge.
An escrow contract run call by call: refused calls, the state changing, and the payment released.
Once deployed, the code usually cannot be changed. That makes it hard to tamper with, but a bug can lose real money.
Many coins are tokens: balances kept by a smart contract. Stablecoins are tokens meant to stay worth a fixed amount, such as one US dollar, and NFTs are tokens that are each one of a kind.
Practice: Predict the contract
Read a small crowdfunding contract and say what each call does.
Keeping your keys safe
Most wallets back up your keys as a seed phrase of 12 or 24 words. Anyone who has those words can take your coins.
Write the words on paper and keep them offline. No real company, app or support team will ever ask for them.
If you keep coins on an exchange, the exchange holds the keys for you. When the exchange FTX collapsed in 2022, its customers could not get their money out.
Risks and scams
Crypto transactions cannot be reversed. If you send coins to the wrong address or to a scammer, there is no bank to call.
Prices can swing wildly. Bitcoin lost almost two thirds of its value during 2022.
Scams are common: fake giveaways, fake support agents and coins that promise guaranteed returns. A promise of easy profit is almost always a scam.
Practice: Spot the scam
Decide whether each situation is a red flag or a safe habit.