The elliptic curve method

The elliptic curve method

Pollard's p−1p - 1 method finds a prime factor when one particular number happens to be smooth. Lenstra's idea was to replace that one number by the orders of as many groups as you care to try: an elliptic curve for each attempt. It finds factors of twenty and more digits whatever the size of the number they divide.

From p − 1 to curves

The p−1p - 1 method computes aM mod na^M \bmod n for MM a product of small prime powers. If p∣np \mid n and p−1p - 1 divides MM, then aM≡1(modp)a^M \equiv 1 \pmod p, and gcd⁡(aM−1,n)\gcd(a^M - 1, n) reveals pp. It works in the group (Z/p)∗(\mathbb{Z}/p)^*, of order p−1p - 1, and if p−1p - 1 has a large prime factor there is nothing to be done: there is only one such group.

An elliptic curve modulo nn is, modulo each prime p∣np \mid n, a group E(Fp)E(\mathbb{F}_p) whose order is somewhere in the Hasse interval [p+1−2p, p+1+2p][p + 1 - 2\sqrt p,\ p + 1 + 2\sqrt p], and different curves give different orders, spread over that interval. Compute M⋅PM \cdot P on the curve modulo nn. If the order of E(Fp)E(\mathbb{F}_p) divides MM, the result is the point at infinity modulo pp but (almost always) not modulo the other primes, and a gcd with nn gives pp. If the curve is unlucky, take another.

import { ecm, ecmCurve, factor, formatFactors } from "@ratmath/numtheory";

const F7 = 2n ** 128n + 1n;
ecm(F7);                                  // 59649589127497217n
ecm(F7, { details: true });               // { factor: 59649589127497217n, stage: 2, sigma: 26n, B1: 2000, B2: 200000 }
formatFactors(factor(F7));                // "59649589127497217 · 5704689200685129054721", both proven prime

The seventh Fermat number was first factored in 1970 by Morrison and Brillhart, with the continued fraction method on an IBM mainframe. Here the twenty-first curve tried finds the 17-digit factor in a fraction of a second. Pollard's rho would need about 6⋅1016≈2⋅108\sqrt{6\cdot10^{16}} \approx 2\cdot10^8 steps, and p−1p - 1 fails because p−1=29⋅116503103764643p - 1 = 2^9 \cdot 116503103764643.

Why that curve worked

The curve with σ=26\sigma = 26, taken modulo p=59649589127497217p = 59649589127497217, has

59649589504338456=23⋅3⋅7⋅67⋅233⋅331⋅599⋅114713 59649589504338456 = 2^3 \cdot 3 \cdot 7 \cdot 67 \cdot 233 \cdot 331 \cdot 599 \cdot 114713

points. Every prime power in it is below B1=2000B_1 = 2000 except one prime, 114713, which is below B2=200000B_2 = 200000. That is exactly the pattern the two stages look for. Stage 1 multiplies the starting point by every prime power up to B1B_1. Stage 2 then allows for one more prime qq between B1B_1 and B2B_2: instead of multiplying by each qq in turn, it compares the x-coordinates of a few hundred precomputed multiples, which costs two multiplications per prime. (The code box below recomputes this group order with the point counting of the previous page.)

The curves

Curves are in Montgomery form By2=x3+Ax2+xBy^2 = x^3 + Ax^2 + x, where a point can be doubled, and two points added when their difference is known, using x-coordinates only and no divisions. Suyama's parametrisation produces from a number σ≥6\sigma \ge 6 a curve and a point on it, with group order divisible by 12, which makes smoothness a little more likely. ecm tries σ=6,7,8,…\sigma = 6, 7, 8, \dots, so a run is reproducible, and ecmCurve(n, sigma, { B1, B2 }) runs a single curve.

Digits of the factorB1Curves, roughly
152,00025
2011,00090
2550,000300
30250,000700

These are rough guideline figures for this method with B2=100 B1B_2 = 100\,B_1; luck varies. The cost of a curve is proportional to B1B_1 and to the square of the number of digits of nn.

Inside factor

factor now runs trial division, a short Pollard rho, p−1p - 1, and then the elliptic curve method with growing bounds (B1B_1 = 2000, 11000, 50000) before it gives up. Option effort multiplies the number of curves at each level. What comes out is still checked: each prime factor is proven, by Baillie–PSW below 2642^{64} and by a certificate above.

formatFactors(factor(10n ** 40n + 1n));   // "17 · 5070721 · 5882353 · 19721061166646717498359681"
formatFactors(factor(2n ** 137n - 1n));   // "32032215596496435569 · 5439042183600204290159"

A failure is not a proof of anything: when no factor is found, the remaining number may be a product of two primes of thirty digits each, and factor refuses with the code "too-hard". Numbers like that need the quadratic or number field sieve, which are not in this library.

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