learn-blockchain-with-phoebe / Leader session 3 of 6
Learn Blockchain with Phoebe · Leader session 3 of 6

What it costs to rewrite history

The attacker in session 2 had to redo 4 blocks, and at difficulty 0 that took no time at all. Difficulty is the mechanism that turns those four blocks into work somebody has to pay for. It does it by requiring each block's hash to begin with a run of zeros, which cannot be arranged and has to be found by guessing. The expected number of guesses is known exactly. What any one attempt will cost is not, and the gap between those two statements is the more useful half of tonight.

🟡 Foundations Anyone being sold a chain Anyone who has to sign off on one 45 min
0-3 · Welcome 3-18 · Guessing, on purpose 18-27 · The mean and the spread 27-45 · Build-along and Q&A
Part 0

Making the rewrite expensive on purpose

Nothing in the Harbourgate register stopped the attacker. Four hashes were recomputed, verification came back clean, and the whole rewrite cost four computations. Difficulty is the deliberate answer to that: a rule saying a block's hash is only acceptable if it begins with a required number of zero characters. The rule adds no security to the hash itself. It adds price, and price is the only thing that was missing.

Live = presented in the 45 minutes Self-study = the full depth, after class 🔍 Every diagram zooms

★ What you walk out with tonight. A plain account of what mining is: trying nonces until a block's hash begins with the required zeros, with no method better than trying. The four measured means, 16, 277, 4,115 and 74,190 attempts at difficulties 1 to 4, each with its trial count. And a habit that outlives the subject: a cost quoted without a trial count beside it is not a cost, it is an anecdote.

LiveThe one field a block will let you change4 min▶

A block's hash is taken over its position, its timestamp, its entry, the previous block's hash and its nonce. The first four are what the block is for and cannot be altered without altering the record. The nonce is different. It says nothing, carries no meaning, and exists so that the hash can be moved without the record being touched.

That is why the nonce looked like a flaw when it was introduced. It is the handle the whole mechanism turns on. Set a rule about what an acceptable hash must look like, and the nonce becomes the only thing anyone can turn while looking for one.

Real world

Harbourgate's six entries were built at difficulty 0, which imposes no rule at all, so every nonce stayed at zero and every block was accepted on its first computation. Everything about tamper evidence held anyway. Difficulty was never about detection.

Self-studyThe words that will keep coming back4 min▶
  • Nonce. A number in every block that can be set to anything without changing what the block says.
  • Difficulty. How many leading zero characters a block's hash is required to have before the block is accepted.
  • Mining. Trying nonce values until the block's hash meets that requirement.
  • Attempt. One nonce tried and one hash computed. The unit every cost figure here is counted in.
  • Expected attempts. 16 to the power of the difficulty, because each hexadecimal character takes one of 16 values and the leading ones must all land on zero.
  • Mean. The average over many independent blocks. The only honest way to state a mining cost.
  • Spread. How far individual runs sit from that mean. At difficulty 4 the measured runs went from 149 attempts to 339,789.
  • Reforge. Redoing a block and every block after it, now at a price per block. Practitioner session 5.
Part 1 · covers proof of work

Guessing is not a weakness of the method, it is the method

15 min live

The requirement is arbitrary and that is the point: a valid block is one whose hash begins with a given number of zeros. Nobody can compute a nonce that produces such a hash, because the avalanche property means the digest for nonce 1 carries no information about the digest for nonce 2. So the nonce is counted upwards and the hash is computed again, until one lands.

One field may be changed freely. Mining is changing it until the hash lands. ONE BLOCK, FIVE FIELDS index fixed timestamp fixed entry fixed previous hash fixed nonce free says nothing, changes the hash SHA-256 over the whole block EACH TRY IS A FRESH DRAW nonce 0 not the required zeros rejected nonce 1 not the required zeros rejected nonce n first 3 characters are 000 accepted There is no way to aim, only to try. A near miss brings the next attempt no closer, because neighbouring nonces do not produce neighbouring digests. Expected attempts at difficulty d are 16 to the power of d: 16 at difficulty 1, 256 at 2, 4,096 at 3, 65,536 at 4. WHAT EACH DIFFICULTY ASKS FOR difficulty 1 hash must begin 0 expected 16 attempts difficulty 2 hash must begin 00 expected 256 attempts difficulty 3 hash must begin 000 expected 4,096 attempts difficulty 4 hash must begin 0000 expected 65,536 attempts
🔍 Click to zoom - each step up in difficulty multiplies the expected number of attempts by sixteen.
Live Real mining, in this page. Take one draw, then a batch, and watch the mean settle while the draws do not.

...

...

...
LiveWhy sixteen to the power of the difficulty4 min▶

A digest is written in hexadecimal, so each character is one of sixteen. A hash function with no gradient scatters its output evenly across every possible digest, which means the first character lands on zero about one time in sixteen. Requiring two leading zeros asks for two independent one in sixteen events at once, and so on upwards.

That gives the expected attempt counts directly: 16, 256, 4,096 and 65,536 for difficulties 1 to 4. It is worth noticing what this expectation is not. It is not a limit, not a guarantee, and not a schedule. It is the average of a process whose individual outcomes are wild.

LiveWhat difficulty does to the attacker's four blocks3 min▶

Session 2 left the attacker needing to redo blocks 2, 3, 4 and 5. At difficulty 0 each of those cost one computation. At difficulty 3 each one costs a mean of 4,115 attempts, measured over 120 trials, and the attacker has to pay that four times over rather than once.

There is a second cost that is easy to miss. While the attacker is redoing four blocks, an honest register is still adding new ones, and every new block is another block the attacker must also redo before the rewritten version can be as long as the real one. The work is not a fixed bill, it is a race against a register that keeps moving.

Self-studyWhere the disagreement really is4 min▶

One camp treats proof of work as a security mechanism in the full sense. There is no administrator to appeal to, so the only thing that can deter a rewrite is making it cost more than it is worth, and a cost that anybody can verify from the data alone is exactly the right shape for a system with nobody in charge.

The other camp says calling cost security mislabels a deterrent as a guarantee. A cryptographic property holds regardless of who is attacking. A cost only holds while the attacker's budget is smaller than the honest work being done, which is a claim about economics, not about mathematics, and it can change without anything in the code changing.

Both positions are held by people who understand the mechanism perfectly well, and the disagreement is about what kind of assurance has been bought rather than about how the mechanism works. It matters in a procurement conversation because the two camps write very different warranties.

Part 2 · covers the measurement

The mean is known. Any single run is not.

9 min live

Every figure below was measured by mining many independent blocks and counting attempts. The means sit close to the expectation, which is reassuring and mostly uninteresting. The columns worth looking at are the last two, because they are the ones that decide whether a number somebody quotes you means anything.

Attempts to mine one block, measured over many independent blocks DIFFICULTY TRIALS MEAN EXPECTED MIN MAX THE MEASURED RANGE, MEAN MARKED IN AMBER 1 500 16 16 1 101 2 300 277 256 1 2,147 3 120 4,115 4,096 40 17,343 4 150 74,190 65,536 149 339,789 The bars share one logarithmic scale, so equal lengths mean equal multiples rather than equal counts. The mean tracks the expectation. The spread does not. At difficulty 3 the measured range runs from 40 attempts to 17,343, more than 400 times, around a mean of 4,115 over 120 trials.
🔍 Click to zoom - four means, four trial counts, and four ranges that are each far wider than the mean sitting inside them.

The first time this measurement was taken it was taken once, and once is what produced the two numbers below. Both are real results from the real engine. Both would have been quoted with a straight face, and both would have been wrong, in opposite directions.

What one run would have told you Difficulty 1 ONE RUN 3 attempts MEAN OVER 500 TRIALS 16 attempts Quoting the run would have read far too cheap. Difficulty 3 ONE RUN 12,462 attempts MEAN OVER 120 TRIALS 4,115 attempts Quoting the run would have read far too dear. Every cost figure is a mean with its trial count beside it. The same measurement overstated one difficulty and understated another on the same afternoon. Nothing inside a single draw says which of the two you are holding, which is why the count of draws has to travel with the number.
🔍 Click to zoom - two honest runs, both misleading, neither detectable as misleading from inside itself.
LiveA lottery with a known ticket count4 min▶

Mining behaves like drawing until a rare event happens. The average wait is exactly what the arithmetic predicts, and a single wait can be almost anything. At difficulty 1 the measured runs went from 1 attempt to 101 around a mean of 16. At difficulty 4 they went from 149 to 339,789 around a mean of 74,190, all over 150 trials.

The practical consequence is that a lucky attacker exists, always, and does not need to be clever. Difficulty does not promise that a rewrite is slow. It promises that rewriting a long stretch of register, block after block, is slow on average, and averages only assert themselves over many blocks.

LiveWhat to ask when somebody quotes you a cost3 min▶

Three questions, in order. How many runs is that averaged over. What were the smallest and largest results. And was the measurement taken on the same configuration you are being sold. A supplier who has the first two answers has measured something. A supplier who has only a single impressive figure has run the thing once.

This generalises well past mining. Any figure produced by a process with a long tail behaves the same way, and a benchmark quoted from one run of a model, one load test or one query plan carries exactly the same defect.

Self-studyWhy difficulty protects a public register better than a private one4 min▶

Difficulty raises the cost of producing a block for everybody, honest or not. On a register maintained by many independent parties, the honest total is large and an attacker has to outspend it, so the price is paid by the attacker and inherited by everyone else as protection.

On a register where one organisation runs all the machines, the same rule raises that organisation's own running cost and raises nothing for an insider, because the insider is the one with the capacity. Adding difficulty to a single operator register buys a slower system and very little else, which is an awkward result for a lot of internal chain projects.

Part 3 · build-along

Take the draws yourself

18 min hands on

No code. Six steps using the live box above, which mines real blocks with the real hash. The aim is to leave with a number you would defend and a refusal you would hold.

Predict, then take one draw

At difficulty 2, write down how many attempts you expect before you press anything. Take one draw. Write down what you got. Resist the temptation to explain the gap, because there is nothing to explain.

Take four more single draws

Say out loud, after each one, whether you would be comfortable quoting it as the cost. By the fourth you should be able to say why the question is badly formed.

Run a batch and watch which number moves

Add twenty five draws. The mean walks towards 256 and settles. The smallest and largest keep moving apart. Name which of those two behaviours a cost estimate depends on.

Step up one difficulty and estimate first

Before switching to difficulty 3, say what you expect the mean to become. Then run a batch and compare against 4,115 over 120 trials. Being close is less interesting than knowing why you were able to predict it at all.

Price the attacker's rewrite in blocks

The rewrite from session 2 is 4 blocks. State its cost as a sentence a sceptic could not pull apart, using the mean per block and its trial count, and without multiplying anything you have not measured.

Write the refusal

One sentence you will say when somebody quotes a single benchmark figure as a cost. Keep it short enough to use in a meeting without sounding like an accusation.

The habit to keep. A mean without a trial count is an anecdote wearing a decimal point. Ask how many runs, ask for the smallest and largest, and treat a refusal to answer as the answer.

Check yourself

Three questions

What does a machine actually do when it mines a block at difficulty 3?

There is no method for computing a suitable nonce, because neighbouring nonces do not produce neighbouring digests. The expected number of tries at difficulty 3 is 4,096 and the measured mean over 120 trials is 4,115, which is what trying rather than solving looks like.

A single mining run at difficulty 3 comes back at 12,462 attempts. What have you learned?

12,462 was a real first run and it sits inside the measured range. A draw that far from the mean is unremarkable in a process with this spread, and treating it as evidence about the cost is the exact mistake the trial counts exist to prevent.

Why does raising difficulty change what an attacker faces, when session 2 showed the rewrite succeeding at difficulty 0?

Difficulty changes price, not detectability and not reversibility. The edit is still possible and still visible; what changes is that the attacker now pays per block, four times over, against a target that keeps moving.

Before session 4

Homework

Twenty minutes, on a number somebody in your organisation is already relying on.

Find a single run being quoted as a cost

A latency figure, a training time, a batch duration, a price per request. Find out how many runs it came from. Write down the answer, including when the answer is that nobody knows.

Name who holds the other copy

For the register you have been tracking since session 1, list every party outside your own team that holds an independent copy. Session 4 is about what that list does and what happens when it is empty.

Covered tonight

What this session covers

✓Mining as a search over noncesA block is accepted when its hash begins with the required zeros, and the only method is trying.
✓Expected attempts are 16 to the power of the difficulty16, 256, 4,096 and 65,536 for difficulties 1 to 4.
✓The measured means, each with its trial count16 over 500 trials, 277 over 300, 4,115 over 120, 74,190 over 150.
✓The spread, which is the real lesson40 to 17,343 at difficulty 3; 149 to 339,789 at difficulty 4.
◐Why a race against a growing register is the deeper costNamed tonight, worked through in session 4.
◐Reforging from a block, in codePractitioner sessions 4 and 5.
○Any cryptocurrency, price or marketA separate subject in the crypto course, and nothing here is investment advice.
○Key management, rotation and certificate hygieneA discipline of its own, covered in the security and access control courses.
○Named commercial chains and their roadmapsThey move faster than a course can, and none of the mechanism depends on which one.
Take this with you

Cheat sheet

The one sentence. Mining is guessing nonces until the block's hash begins with the required zeros.
The nonce. The only field that can change without changing what the block says.
Expected attempts. 16 to the power of the difficulty: 16, 256, 4,096, 65,536.
Measured means. 16 over 500 trials, 277 over 300, 4,115 over 120, 74,190 over 150.
The spread at difficulty 3. 40 to 17,343 around a mean of 4,115, more than 400 times.
The spread at difficulty 4. 149 to 339,789 around a mean of 74,190.
The trap. A first run gave 3 at difficulty 1 and 12,462 at difficulty 3. Both would have misled.
The habit. Every cost is a mean with its trial count beside it, or it is an anecdote.