Counting points

Counting points

Over the field of pp elements a curve has finitely many points, and they still form a group. How many there are is the question behind elliptic curve cryptography, factoring and primality proving. Three methods, each faster and subtler than the last, and each a proof.

A curve over Fp

import { ellipticCurve } from "@ratmath/numtheory";

const C = ellipticCurve([2, 3], { mod: 97 });   // y² = x³ + 2x + 3 over F_97
C.count();            // 100n, counting the point at infinity
C.trace();            // -2n: #E = p + 1 − t
C.points().length;    // 99
C.liftX(3);           // [(3, 6), (3, 91)]
const P = C.point(3, 6);
C.add(P, P);          // (80, 10)
C.order(P);           // 5n
C.structure();        // { order: 100n, structure: [2n, 50n] }: the group is Z/2 × Z/50

Coordinates are BigInt residues. The modulus must be prime (proven: above 2642^{64} a certificate is found first). Every Weierstrass form works, including p=2p = 2 and p=3p = 3, where the short form does not exist. A curve over Q\mathbb{Q} reduces with curve.modulo(p).

Hasse's theorem

About half of the xx give a square on the right-hand side and so two points, which suggests about p+1p + 1 points in all. Hasse proved the error is small:

#E(Fp)=p+1−t,∣t∣≤2p. \#E(\mathbb{F}_p) = p + 1 - t, \qquad |t| \le 2\sqrt p.

The number tt is the trace of Frobenius: the map φ(x,y)=(xp,yp)\varphi(x, y) = (x^p, y^p) sends the curve to itself and satisfies φ2−tφ+p=0\varphi^2 - t\varphi + p = 0. The group is always Z/n1×Z/n2\mathbb{Z}/n_1 \times \mathbb{Z}/n_2 with n1∣n2n_1 \mid n_2 and n1∣p−1n_1 \mid p - 1; structure() proves it cyclic at once when no square factor of the order is compatible with that, and otherwise decides by the orders of all points, for small pp.

Three ways to count

const B = ellipticCurve([2, 3], { mod: 1000003 });
B.count({ method: "naive" });    // 999708n
B.count({ method: "bsgs" });     // 999708n
B.count({ method: "schoof" });   // 999708n

The character sum. #E=p+1+∑xχ(x3+ax+b)\#E = p + 1 + \sum_x \chi(x^3 + ax + b), where χ\chi is the Legendre symbol. One table of squares and one pass over xx: about pp steps, fine up to a few million.

Baby steps and giant steps. The order of any point PP divides #E\#E, and #E\#E lies in the Hasse interval, which has length 4p4\sqrt p. Shanks's method finds a multiple of the order of PP inside that interval in about p1/4p^{1/4} steps: store jPjP for small jj and stride through the interval. From the multiple, the exact order follows by removing primes. One point may not settle it, because several multiples of its order can fit in the interval. Mestre's remedy is to work on the curve and on its quadratic twist together (their counts add up to 2p+22p + 2): he showed that for p>229p > 229 one of the two has a point whose order has a single multiple in the interval. The count is accepted only when that uniqueness has been checked, so it is a proof.

Schoof's algorithm. Polynomial in log⁡p\log p. For a small prime ll, the equation φ2−tφ+p=0\varphi^2 - t\varphi + p = 0 is tested on the points of order ll: t mod lt \bmod l is the one τ\tau for which

φ2(P)+[p mod l] P=[τ] φ(P)for every P of order l. \varphi^2(P) + [p \bmod l]\,P = [\tau]\,\varphi(P) \quad\text{for every } P \text{ of order } l.

The points of order ll are never found. They are the roots of the division polynomial ψl\psi_l, of degree (l2−1)/2(l^2 - 1)/2, and all of them are handled at once by computing with polynomials modulo ψl\psi_l and the curve equation. Enough primes ll, with product above 4p4\sqrt p, determine tt by the Chinese remainder theorem.

const big = ellipticCurve([2, 3], { mod: 1000000007 });
const log = [];
big.count({ method: "schoof", log });   // 1000004178n
big.trace();                            // -4170n
log;   // [{ l: 2, t: 0 }, { l: 3, t: 0 }, { l: 5, t: 0 }, { l: 7, t: 2 }, { l: 11, t: 10 }, { l: 13, t: 3 }, { l: 17, t: 12 }]

−4170-4170 is indeed 0 mod 2,3,50 \bmod 2, 3, 5, 2 mod 72 \bmod 7, 10 mod 1110 \bmod 11, 3 mod 133 \bmod 13 and 12 mod 1712 \bmod 17, and 2⋅3⋅5⋅7⋅11⋅13⋅17=5105102\cdot3\cdot5\cdot7\cdot11\cdot13\cdot17 = 510510 exceeds 4p≈1264914\sqrt p \approx 126491. One subtlety: ψl\psi_l need not be irreducible, so a division modulo it can fail. When it does, the failure hands over a factor of ψl\psi_l, and the computation is repeated modulo the factor; any factor will do, because the equation holds at every point of order ll.

With method: "auto" (the default) the character sum is used below 2222^{22}, baby steps and giant steps below 2722^{72}, and Schoof above. The implementation of Schoof here uses schoolbook polynomial arithmetic and is meant to be read: it takes about a second at p≈1012p \approx 10^{12} and grows quickly from there. The Elkies and Atkin improvements that make it fast for cryptographic sizes are not here.

Division polynomials

C.divisionPolynomial(3);   // [93n, 36n, 12n, 0n, 3n]: 3x⁴ + 12x² + 36x + 93 = 3x⁴ + 6ax² + 12bx − a² mod 97

The roots of the nn-th division polynomial are the x-coordinates of the points P≠OP \ne O with nP=OnP = O. For even nn the full ψn\psi_n is 2y2y times the polynomial returned, the factor yy accounting for the points of order 2.

A key exchange

In a group where multiplying is easy and undoing it is hard, two people can agree on a secret in public: each picks a secret number, publishes that multiple of a base point, and multiplies the other's point by their own number. With toy sizes it looks like this; real curves have about 22562^{256} points and standardised parameters, and real implementations must also resist timing attacks, which this code does not try to do.

Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs