A Curve Small Enough to Print
The entropy series said that you “multiply the curve’s generator by that integer to get a public key” and moved on. That sentence hides the entire security argument. If multiplication could be undone as easily as it is done, every public key would give away its private key.
Bitcoin’s curve has roughly 2²⁵⁶ points, which is too many to look at. So here is the same equation over a field small enough to print in full.
The picture in the textbook, and the one that matters
If you have seen an elliptic curve before, it was the smooth shape on the left
below. Over the real numbers, y² = x³ + 7 is a single curve, and the
addition rule has a geometric meaning: a line through two points P and Q
meets the curve a third time, and reflecting that third point across the
x-axis gives P + Q.
P and Q meets the curve at a third point; its mirror image across the x-axis is P + Q. On the right the same equation with x and y taken mod 43 has 30 solutions and no curve.Cryptography uses the right panel. Take x and y as integers mod a prime and
the curve disappears: what remains is a scatter of pairs with no lines, no
neighbors, and nothing to zoom into. The picture on the left survives only as
an explanation of the rules. The mirror line in the right panel is the x-axis
of the left, and everything else is arithmetic. The security of Bitcoin lives
entirely in the scatter.
The same equation, 43 instead of 2²⁵⁶
Bitcoin’s curve, secp256k1, is the set of pairs (x, y) satisfying
y² = x³ + 7 (mod p)
for one specific 256-bit prime p. Replace that prime with 43 and the equation
still has solutions. toy_curve.py from the companion scripts finds every
solution: 30 points, plus one extra element called the point at infinity,
which plays the role of zero. That makes 31, a prime, which matters shortly.
Pick the point G = (2, 31). Check it: 31² = 961 = 15 (mod 43), and
2³ + 7 = 15. It is on the curve.
Adding two points
Over a finite field there is no line to draw, so the rule from the left panel
has to be written as algebra. To add P = (x₁, y₁) and Q = (x₂, y₂):
def add(self, p1, p2):
if p1 is None: return p2 # None is the point at infinity
if p2 is None: return p1
p = self.p
x1, y1 = p1
x2, y2 = p2
if x1 == x2:
if (y1 + y2) % p == 0:
return None # P + (-P)
lam = (3 * x1 * x1 + self.a) * pow(2 * y1, -1, p) % p # tangent
else:
lam = (y2 - y1) * pow(x2 - x1, -1, p) % p # chord
x3 = (lam * lam - x1 - x2) % p
return (x3, (lam * (x1 - x3) - y1) % p)
lam is the slope of the line. Division is multiplication by a modular
inverse, which is what pow(value, -1, p) computes. When the two points are
the same, the line is the tangent. That is the whole operation.
One example by hand. Take (7, 36) and (12, 31):
slope = (31 − 36) / (12 − 7) = −5 / 5 = −1 = 42 (mod 43)
x₃ = 42² − 7 − 12 = 1 − 19 = −18 = 25 (mod 43)
y₃ = 42 · (7 − 25) − 36 = 18 − 36 = −18 = 25 (mod 43)
The sum is (25, 25).
Multiplying is repeated addition
kG means G + G + … + G, k times. Here is every multiple of G on the
toy curve:
k k*G k k*G k k*G
1 ( 2, 31) 11 (40, 18) 21 (42, 7)
2 ( 7, 36) 12 (37, 7) 22 (20, 40)
3 (35, 22) 13 (13, 22) 23 (32, 40)
4 (21, 25) 14 (34, 3) 24 (25, 18)
5 (12, 31) 15 (38, 22) 25 (29, 31)
6 (29, 12) 16 (38, 21) 26 (12, 12)
7 (25, 25) 17 (34, 40) 27 (21, 18)
8 (32, 3) 18 (13, 21) 28 (35, 21)
9 (20, 3) 19 (37, 36) 29 ( 7, 7)
10 (42, 36) 20 (40, 25) 30 ( 2, 12)
31 infinity
Three things are visible in this table.
Every point appears exactly once. The group has a prime number of
elements, so any point other than infinity generates all of it. secp256k1 has
the same property. Its number of points is a 256-bit prime called n, and a
private key is an integer from 1 to n − 1.
The table wraps. 31G is infinity, so 32G = G. Scalars work modulo 31.
The table is symmetric. kG and (31 − k)G share an x-coordinate and
have opposite y: 2G = (7, 36) and 29G = (7, 7), and 36 = −7 (mod 43). A
signature reveals only an x-coordinate, which is why Part 5 recovers two
candidate public keys from one signature.
The hand calculation above was 2G + 5G, and the answer (25, 25) is 7G.
Adding points adds their scalars. That is the property everything else rests
on.
Plotted on the 43 × 43 grid they live in, the same 30 points look like this:
G, labelled with its k. The line through 2G and 5G meets the curve at a third point, 24G; its mirror image is 7G. Most chords wrap around the edges of the grid. This one happens not to.The symmetry is the dotted line. The hand calculation is the blue one. Nothing
else in the picture suggests an order: 3G is next to 15G, and 8G is next
to 14G.
Forward is cheap
Nobody computes 29G with 28 additions. Write 29 in binary as 11101 and
read the bits left to right, doubling at each step and adding G when the bit
is 1:
forward : 29*G = (7, 7) using 4 doublings and 3 additions
Seven operations instead of 28. For a 256-bit scalar the count is at most 255 doublings and 255 additions. The deliberately slow Python in the companion scripts manages about 180 such multiplications per second on secp256k1; the C library behind the entropy series’ scanner did about 31,000. The cost grows with the length of the number.
Backward is a search
Now the other direction. Given Q = (7, 7), which k satisfies kG = Q?
backward: which k gives (7, 7)? The table says k = 29.
The table has 31 rows. On secp256k1 it would have about 2^256.
On the toy curve you look it up. There is no formula that reads k off the
coordinates, because the addition rule scrambles them: consecutive multiples
land all over the field. Finding k from kG is the elliptic-curve
discrete logarithm problem (ECDLP).
Scanning the table is the worst method. Part 4 implements a much better one, which needs about the square root of the table size, and measures it. Even that method needs around 2¹²⁸ steps on secp256k1.
So a Bitcoin key pair is a row of this table: the private key is the row
number d, the public key is the point Q = dG. The security claim is that
on a large enough curve nobody can find the row from the point.
The table is the public half. A public key is no use until its owner can prove they hold the row number without saying it, and Part 2 shows how a signature does that.