Scroll to turn the page
Blockchain · College Blockchain
proving who you are without a password
On a blockchain, an account is controlled by whoever holds its private key. There is no bank to call and no password reset. Spending means producing a digital signature that only the private key could make.
Each private key d (a random 256-bit number) has a matching public key Q, computed from it. Computing Q from d is easy; getting d back from Q is infeasible. So Q can be published while d stays secret.
Signing combines a message with d. Anyone holding the message, the signature and Q can check it, without learning d. Change the message by one character and the old signature fails.
This chapter builds the arithmetic behind it: elliptic curves over the real numbers (2.2) and over a finite field (2.3), the ECDSA signature (2.4), the mistake that leaks a key (2.5), and how a public key becomes an address (2.6) and a key becomes a recovery phrase (2.7). Section 2.1 sets out the idea of public-key cryptography.
two keys, one secret
In symmetric cryptography both sides share one secret key. That works for two friends, not for a network of strangers. Public-key (asymmetric) cryptography gives each user two keys.
| uses the private key | uses the public key | |
|---|---|---|
| signature | sign | verify |
| encryption | decrypt | encrypt |
Whitfield Diffie and Martin Hellman published the idea in 1976; RSA (Rivest, Shamir, Adleman) followed in 1977. Elliptic-curve cryptography, proposed independently by Neal Koblitz and Victor Miller in 1985, gets the same security from much smaller keys.
Blockchains use public keys for signatures, not encryption: transactions are public, and what matters is proving that the owner authorized them. A signature scheme must be unforgeable: seeing many signed messages must not let anyone sign a new one.
Bitcoin and Ethereum both use the curve secp256k1. Bitcoin signs with ECDSA, and since the Taproot upgrade of November 2021 also with Schnorr signatures (BIP 340).
y² = x³ + 7 over the real numbers
An elliptic curve here is the set of points with y² = x³ + ax + b. secp256k1 uses a = 0, b = 7. Its points can be added, by a rule that is pure geometry.
Draw the line through P and Q. A cubic meets a line in three points, so the line hits the curve once more. Reflect that third point in the x-axis: the result is P + Q.
To add a point to itself, use the tangent line instead (λ = 3x²/2y). A vertical line meets the curve at no third point; its sum is a special point at infinity, O, which acts as zero.
With this rule the points form a group: addition is associative, O is the identity, and each point (x, y) has an inverse (x, −y). Adding a point to itself k times gives k·P.
the same rule, mod p
Cryptography needs exact arithmetic, so the curve is taken modulo a prime p: x and y are whole numbers from 0 to p − 1, and the same addition formulas are computed mod p.
The figure uses p = 43. The curve y² ≡ x³ + 7 (mod 43) has 30 points, plus O: a group of 31. Since 31 is prime, any point but O generates the whole group. Take G = (2, 12); then G, 2G, 3G, … visit every point and return to O at 31G.
A private key is a number d; the public key is Q = d·G. Here d = 9 gives Q = (20, 40). The hops look random, and recovering d from Q is the discrete logarithm problem.
With 31 points you could just try every d. On secp256k1, p = 2256 − 232 − 977 and the group has about 1.158 × 1077 points; the best known attacks need about 2128 steps. Computing d·G forward is fast (by doubling and adding, a few hundred point operations); going back is not.
signing and verifying, step by step
The Elliptic Curve Digital Signature Algorithm signs the hash z of a message with the private key d, using a fresh secret random number, the nonce k. All arithmetic on numbers is mod n, the size of the group.
The signature is the pair (r, s). To verify with the public key Q, compute
and accept if xX mod n = r. It works because u1G + u2dG = s−1(z + rd)G = kG = R.
In the figure (n = 31), “Alice pays Bob 5” hashes to z = 17. With d = 9 and k = 7, the signature is (25, 8), and the check finds X = (25, 18) = R. The same signature on “Alice pays Bob 50” fails: its z is 1, and X lands elsewhere.
two signatures, one leaked key
ECDSA is only as secret as its nonce. If the same k signs two different messages, anyone can solve for the private key.
Same k means the same R, so both signatures share r, a giveaway. Subtracting the two equations s1k = z1 + rd and s2k = z2 + rd removes d:
signature 1 r = 25, s₁ = 8, z₁ = 17 signature 2 r = 25, s₂ = 12, z₂ = 14 same r: same nonce yes k = (z₁ − z₂)/(s₁ − s₂) 7 d = (s₁·k − z₁)/r 9 In the figure, Alice signs “Alice pays Bob 5” and “Alice pays Carol 3” both with k = 7. The algebra gives back k = 7 and d = 9, her real private key.
This is not hypothetical: in 2010 researchers showed that Sony signed PlayStation 3 software with the same nonce every time, and recovered its signing key. Modern wallets derive k deterministically from the key and the message (RFC 6979), so it never repeats for different messages.
hash it, shorten it, check it
An address is a short identifier derived from a public key by hashing. People pay to it; only the matching private key can spend from it.
| Bitcoin (legacy P2PKH) | size |
|---|---|
| public key: compressed: a 02/03 prefix and x | 33 bytes |
| SHA-256: hash | 32 bytes |
| RIPEMD-160: the public-key hash | 20 bytes |
| + version byte: 0x00 for a mainnet address | 21 bytes |
| + checksum: first 4 bytes of SHA-256(SHA-256(…)) | 25 bytes |
| Base58: starts with 1 | up to 34 characters |
The compressed public key is 33 bytes (the full point is 65). Hashing with SHA-256 then RIPEMD-160 gives a 20-byte public-key hash. A version byte and a 4-byte checksum are added, and Base58 (digits and letters without 0, O, I and l, which look alike) writes it out. A mistyped address fails the checksum instead of sending coins nowhere.
| Ethereum | size |
|---|---|
| public key: x and y, no prefix | 64 bytes |
| Keccak-256: hash | 32 bytes |
| last 20 bytes: the address | 20 bytes |
| 0x + hex: characters | 42 characters |
Ethereum hashes the 64-byte public key with Keccak-256 and keeps the last 20 bytes, written as 0x and 40 hex digits.
Hashing the key has a bonus: until the owner first spends, the public key itself is not on the chain, only its hash.
a private key you can write down
Wallets turn randomness into a list of words (BIP 39), so a person can back up every key in the wallet on paper.
Start from 128 random bits of entropy. Append a checksum: the first 4 bits (128 ÷ 32) of SHA-256 of the entropy. The 132 bits are cut into 12 groups of 11 bits, and each group, a number from 0 to 2047, picks a word from a fixed list of 2048.
entropy 921e929bbb09556beb514f539b784800 checksum 1110 indices 1168 1956 1335 944 1194 1455 1386 335 668 1758 144 14 The figure’s example (made up, not anyone’s wallet) gives the word indices above. 256 bits of entropy plus 8 checksum bits make 24 words instead.
The phrase is turned into a seed (PBKDF2 with SHA-512, 2,048 rounds), and the seed into as many private keys as the wallet needs. Whoever sees the phrase controls all of them, which is why it is never typed into a website.