The elliptic curve method
Pollard's 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 method computes for a product of small prime powers. If and divides , then , and reveals . It works in the group , of order , and if has a large prime factor there is nothing to be done: there is only one such group.
An elliptic curve modulo is, modulo each prime , a group whose order is somewhere in the Hasse interval , and different curves give different orders, spread over that interval. Compute on the curve modulo . If the order of divides , the result is the point at infinity modulo but (almost always) not modulo the other primes, and a gcd with gives . 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 steps, and fails because .
Why that curve worked
The curve with , taken modulo , has
points. Every prime power in it is below except one prime, 114713, which is below . That is exactly the pattern the two stages look for. Stage 1 multiplies the starting point by every prime power up to . Stage 2 then allows for one more prime between and : instead of multiplying by each 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 , 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 a curve and a point on it, with group order divisible by 12, which makes smoothness a little more likely. ecm tries , so a run is reproducible, and ecmCurve(n, sigma, { B1, B2 }) runs a single curve.
| Digits of the factor | B1 | Curves, roughly |
|---|---|---|
| 15 | 2,000 | 25 |
| 20 | 11,000 | 90 |
| 25 | 50,000 | 300 |
| 30 | 250,000 | 700 |
These are rough guideline figures for this method with ; luck varies. The cost of a curve is proportional to and to the square of the number of digits of .
Inside factor
factor now runs trial division, a short Pollard rho, , and then the elliptic curve method with growing bounds ( = 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 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.
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.