Counting points
Over the field of 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 a certificate is found first). Every Weierstrass form works, including and , where the short form does not exist. A curve over reduces with curve.modulo(p).
Hasse's theorem
About half of the give a square on the right-hand side and so two points, which suggests about points in all. Hasse proved the error is small:
The number is the trace of Frobenius: the map sends the curve to itself and satisfies . The group is always with and ; 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 .
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. , where is the Legendre symbol. One table of squares and one pass over : about steps, fine up to a few million.
Baby steps and giant steps. The order of any point divides , and lies in the Hasse interval, which has length . Shanks's method finds a multiple of the order of inside that interval in about steps: store for small 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 ): he showed that for 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 . For a small prime , the equation is tested on the points of order : is the one for which
The points of order are never found. They are the roots of the division polynomial , of degree , and all of them are handled at once by computing with polynomials modulo and the curve equation. Enough primes , with product above , determine 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 }]
is indeed , , , and , and exceeds . One subtlety: need not be irreducible, so a division modulo it can fail. When it does, the failure hands over a factor of , and the computation is repeated modulo the factor; any factor will do, because the equation holds at every point of order .
With method: "auto" (the default) the character sum is used below , baby steps and giant steps below , and Schoof above. The implementation of Schoof here uses schoolbook polynomial arithmetic and is meant to be read: it takes about a second at 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 -th division polynomial are the x-coordinates of the points with . For even the full is times the polynomial returned, the factor 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 points and standardised parameters, and real implementations must also resist timing attacks, which this code does not try to do.
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.