Part III ยท The Key Chapter 7

A 256-Bit Number

โ–ถ The Story

Here is a Bitcoin private key, in full: a number. Not yet โ€” first you need to see the size of the space it comes from:

115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,
665,640,564,039,457,584,007,913,129,639,936

That is \(2^{256}\) โ€” seventy-eight digits, the count of every possible 256-bit string. A private key is one number chosen fairly from a space essentially that large. This chapter is about what those two facts โ€” a number, that space โ€” conspire to produce: the unguessable vault at the center of digital money. If it feels anticlimactic that the vault door is "just a number," good. The anticlimax is the design.

7.1The Number Itself

A Bitcoin private key is a whole number from 1 up to a ceiling called \(n\) โ€” a parameter of Bitcoin's curve, secp256k1, whose precise value is

\[ n = 115{,}792{,}089{,}237{,}316{,}195{,}423{,}570{,}985{,}008{,}687{,}907{,}852{,}837{,}564{,}279{,}074{,}904{,}382{,}605{,}163{,}141{,}518{,}161{,}494{,}337. \]

That is 78 digits, and it is a hair under \(2^{256}\): the difference \(2^{256} - n\) is a 39-digit number โ€” enormous in absolute terms, invisible relatively, a rounding that shows up only in the 39th digit. For every purpose in this book, the keyspace is \(2^{256}\). (The curve that fixes \(n\), and what keys are for arithmetically, is the companion volume's subject โ€” The Curve That Powers Bitcoin opens secp256k1 parameter by parameter. This book's subject is the choosing of the number, not the curve arithmetic built on it.)

So a private key is an integer in a range. No structure, no format, no checksum at this level โ€” a number. What makes it a key is only how it was chosen: uniformly, so that every one of the roughly \(2^{256}\) values is exactly as likely as every other. Chapters 4 and 5 already priced what that means: 256 bits of entropy, and 256 bits of min-entropy. The most likely key is no likelier than any other.

7.2The Space You Hide In

How big is \(2^{256}\)? Written out:

\[ 115{,}792{,}089{,}237{,}316{,}195{,}423{,}570{,}985{,}008{,}687{,}907{,}853{,}269{,}984{,}665{,}640{,}564{,}039{,}457{,}584{,}007{,}913{,}129{,}639{,}936. \]

Seventy-eight digits โ€” a 1 followed by 77 more digits, or about \(10^{77}\) (a 1 with 77 zeros; Chapter 1's power notation, scaled cosmic). Comparisons are the only way in:

These comparisons are not security poetry; they are Chapter 3's arithmetic with cosmic inputs. The next chapter makes the pricing rigorous, including the energy floor under it. For now, hold the shape of it: the space is not large the way a nation is large, it is large the way physics is large.

7.3Where the Key Lives in Bitcoin

What does the number do? One paragraph, because the machinery is the companion volume's. From your private key, wallet software derives a public key (multiplication on secp256k1 โ€” easy forward, infeasible backward), and from the public key an address (hashes and encodings โ€” On Hashing's territory). The address is what the world sees; the public key surfaces when you spend; the private key never leaves your side, because it signs: it produces a proof that the holder of the number authorized a payment, and anyone can check the proof without learning the number.

That is the entire custody model of Bitcoin compressed: possession of the number is ownership. No registry, no account, no institution mediating. Which is why the choosing of the number is the whole ballgame โ€” and why this book spends twelve chapters on 256 bits.

7.4How Bitcoin Core Draws a Key

ยง6.5 left 32 fresh bytes at the lip of Bitcoin Core's well. Thirty-two bytes are 256 bits: a whole number from 0 up to \(2^{256} - 1\). But a private key must be at least 1 and below \(n\), so two kinds of draw are not keys โ€” zero, and anything at or above \(n\). Core's answer is the bluntest one available: check the number, and if it fails, throw it away and draw again.

In the Codebase: Bitcoin Core v31.0, src/key.cpp
void CKey::MakeNewKey(bool fCompressedIn) {
    MakeKeyData();
    do {
        GetStrongRandBytes(*keydata);
    } while (!Check(keydata->data()));

GetStrongRandBytes is ยง6.5's well. Check hands the 32 bytes to the curve library, libsecp256k1, which accepts them only if the number is not zero and is less than \(n\).

How often does the loop run twice? The rejected values are the \(2^{256} - n\) numbers at or above \(n\) โ€” the 39-digit gap from ยง7.1 โ€” plus zero. Against a space of \(2^{256}\), that is about 1 draw in \(2.7 \times 10^{38}\). In practice the loop runs once; the check is there so that "almost always a key" becomes "always a key."

Why redraw rather than patch? A tempting shortcut is to take a too-big number and subtract \(n\) from it. Then each of the smallest \(2^{256} - n\) keys could be reached two ways โ€” drawn directly, or drawn as itself plus \(n\) โ€” and would be twice as likely as every other key. The favored keys would be a sliver too thin to matter in practice, but redrawing makes the bias exactly zero: every key from 1 to \(n - 1\) stays equally likely, which is the uniformity ยง7.1 said turns a number into a key.

One draw per wallet. When you create a wallet, this loop runs once โ€” to make a seed, not a spending key. Core feeds the seed to a keyed hash (HMAC-SHA512, keyed with the text Bitcoin seed) and cuts the 64-byte result in two: the first 32 bytes become a master private key, the last 32 a chain code. From that pair a fixed public recipe, BIP 32, derives as many child keys as the wallet will ever need, along four standard paths โ€” one per address type, each split into a receiving branch and a change branch. The recipe's arithmetic is curve work and belongs to the companion volume, The Curve That Powers Bitcoin. What matters here is the entropy accounting: randomness enters the wallet exactly once, 256 bits of it, and every later key is deterministic arithmetic on those bits. The wallet's backup is its descriptors (or the wallet file), not a word list โ€” Bitcoin Core does not use the seed phrases Chapter 11 opens, and ยง11.6 explains why.

It was not always so:

VersionsWalletHow keys are made
before 0.13independent keysevery key its own fresh draw; a backup covered only the keys drawn before it was made
0.13 to 22legacy HDone seed; BIP 32 path m/0'/0'/k'
23 onwarddescriptor (default)one seed; four descriptor paths (44h, 49h, 84h, 86h), receive and change

The trend is the book's argument in miniature: the one random draw got more careful, and everything after it became arithmetic.

7.5Looking Ahead

So the defense rests on a single sentence: a fairly chosen 256-bit number cannot be guessed. Chapter 8 turns that sentence from intuition into an invoice: what a search of this space costs in time, in machines, and in raw energy โ€” and why the one loophole that survives (batch attacks, many targets at once) buys the attacker almost nothing. Then Part IV asks the question that actually empties wallets: what happens when the choosing is not fair?

Key Takeaways

Exercises

Exercise 7.1 โ˜…   The ceiling. In Python, compute \(2^{256}\) and the secp256k1 group order int("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16), and confirm \(n < 2^{256}\). What is \(2^{256} - n\)? You should get a 39-digit number: \(432{,}420{,}386{,}565{,}659{,}656{,}852{,}420{,}866{,}394{,}968{,}145{,}599\).

Exercise 7.2 โ˜…   Counting the space. How many digits does \(2^{256}\) have? Confirm with len(str(2**256)): exactly \(78\).

Exercise 7.3 โ˜…โ˜…   The hopeless fraction. A billion machines (\(10^9\)) each test a billion keys per second (\(10^9\)) for a billion years (about \(3.2 \times 10^{16}\) seconds). Compute the total keys tested, and the fraction of the \(2^{256}\) space covered. You should get about \(3.2 \times 10^{34}\) keys and a fraction of about \(1\) in \(3.6 \times 10^{42}\).

Exercise 7.4 โ˜…โ˜…   Your own keyspace fact. Compute how many seconds old the universe is (13.8 billion years; a year is 31,557,600 seconds), and find the smallest power of 2 that exceeds it. You should get about \(4.35 \times 10^{17}\) seconds, and \(2^{59} \approx 5.8 \times 10^{17}\) โ€” the age of the universe in seconds is smaller than a 59-bit space.

Exercise 7.5 โ˜…โ˜…โ˜…   A skeptic says: "A 256-bit key is too long to remember and too big to mean anything โ€” surely something smaller would do." Using this chapter and Chapter 5, write the two-paragraph reply: what property the 256-bit size buys (min-entropy of a uniform draw), and what would concretely go wrong at, say, 32 bits. Your reply should quote at least one number from Chapter 6.

Exercise 7.6 โ˜…โ˜…โ˜…   The redraw odds. Using Exercise 7.1's numbers, compute in Python the number of 32-byte draws MakeNewKey rejects (zero, plus every value at or above \(n\)) and the odds against a rejection: 2**256 - n + 1 and 2**256 / (2**256 - n + 1). You should get rejects \(= 432{,}420{,}386{,}565{,}659{,}656{,}852{,}420{,}866{,}394{,}968{,}145{,}600\) and odds of about \(1\) in \(2.678 \times 10^{38}\). Then explain in two sentences why Core redraws instead of subtracting \(n\) from a too-big draw.

Solution Sketches

Sketches, not model answers โ€” enough to check your reasoning against. The computational exercises embed their expected outputs in the exercise text.

Exercise 7.5. The 256-bit size buys a min-entropy of 256 for any fairly chosen key: the attacker's best single guess faces 1 in \(2^{256}\), and Chapter 8 will price what that means in energy. Shrink to 32 bits and the space collapses to about 4.3 billion keys โ€” and Chapter 6 already priced that space: full enumeration in about 72 minutes at a million tries a second, before lunch. A 32-bit "key" is not a small key; it is no key. The length is not for your memory โ€” the seed phrase of Chapter 11 handles memory โ€” it is for the thief's arithmetic: every added bit doubles his bill.

Exercise 7.6. Subtracting \(n\) would give each of the smallest \(2^{256} - n\) keys two ways to be drawn, so those keys would be twice as likely as the rest โ€” a non-uniform draw. The bias is far too thin to exploit, but redrawing removes it completely at a cost of one extra draw in about \(2.7 \times 10^{38}\): exact uniformity for free.