Probability

A collision that finds a factor

A walk through the remainders modulo a number must eventually repeat, and it repeats modulo each hidden prime factor long before it repeats modulo the number. Pollard saw that the earlier repeat can be detected without knowing the prime — and that its timing is the birthday problem, so the cost is the square root of the factor.

Worth reading first: Any unevenness brings the match sooner.

The birthday problem is usually met as a hazard. A room of twenty-three probably contains a coincidence nobody arranged, a hash function with 21282^{128} outputs probably yields two colliding documents after only 2642^{64} tries, and the square root in both is the reason collisions arrive far sooner than intuition expects.

In 1975 John Pollard turned the hazard into a tool. He wanted to factor a number nn and he knew that a collision among remainders modulo an unknown factor pp would arrive after about p\sqrt p steps — long before a collision modulo nn itself. The difficulty is that pp is unknown, so the remainders modulo pp cannot be computed and the collision cannot be looked for. The method’s whole content is a way of detecting a coincidence in numbers nobody can see.

The walk x² + 1 modulo 337, drawn as the letter ρ. Starting at 2 and squaring and adding 1 modulo 337, the walk visits 8 values once on a tail and then runs round a cycle of 11 values for ever.
Fig. 1 The walk that squares and adds one, modulo the prime 337, started at 2. Eight values appear once and are never seen again; the ninth, 253, is where the walk closes onto a cycle of eleven and runs round it for ever. Nineteen distinct values out of 337 possible, against a birthday estimate of π337/223\sqrt{\pi \cdot 337/2} \approx 23.

A walk that has to come back

Pick a modulus mm, a constant cc, and a starting value, and apply xx2+cmodmx \mapsto x^2 + c \bmod m over and over. There are only mm possible values, so by the pigeonhole principle some value must recur within m+1m+1 steps. And because each value determines the next, the moment a value recurs the walk is trapped: from then on it retraces the same stretch forever.

That forces the shape in the figure. Some number μ\mu of values form a tail, visited once; the rest form a cycle of length λ\lambda. The picture looks like the Greek letter rho, which is where the method’s name comes from, and the same shape appears whenever a deterministic process runs in a finite space — an orbit that must come back in a system with finitely many states is a rho with its tail and loop.

What pigeonhole does not say is when. It guarantees a repeat within m+1m+1 steps. The walk above repeats after nineteen, out of a possible 338, and that is typical rather than lucky.

Why the first repeat is a birthday

Suppose the map were not x2+cx^2 + c but a function chosen completely at random from all functions on mm values. Then the walk’s successive values would be independent uniform draws for as long as it keeps visiting new values — each step applies the function to a point it has never been applied to before, and a random function’s value at a fresh point is a fresh random number.

So the walk keeps producing new values exactly as long as a sequence of independent draws avoids every earlier draw. The step at which it first repeats is the moment a room of people first contains a shared birthday, with mm days in the year. The chance of lasting past kk steps is the familiar product m1mm2m\tfrac{m-1}{m}\cdot\tfrac{m-2}{m}\cdots, and the expected number of distinct values before the repeat is about πm/2\sqrt{\pi m/2}the same πd/2\sqrt{\pi d/2} that gives the average room size at which a match first appears.

That is a derivation about random functions, and x2+1x^2 + 1 is not one. It is a single, very specific polynomial, and nothing in the argument above applies to it.

How long x² + 1 runs before repeating, over 400 primes. For 400 primes the length of the walk x² + 1 before its first repeated value, in units of √p, drawn as the share still unrepeated against e^(−t²/2), the birthday curve for a function chosen at random. The two differ by at most 3.6 percentage points.
Fig. 2 Four hundred primes between five thousand and two hundred thousand. For each, the number of distinct values x2+1x^2 + 1 visits from 2 before repeating, measured in units of p\sqrt p. The staircase — the share of walks not yet repeated — follows the random-function curve et2/2e^{-t^2/2} to within 3.6 percentage points everywhere, with a mean of 1.265p1.265\sqrt p against 1.253p1.253\sqrt p.

The polynomial behaves as though it were random. Across four hundred primes its first-repeat time has the birthday distribution to within the sampling wobble of four hundred draws, and its mean lands within one per cent of the random-function value. This is an observation and not a theorem. Nobody has proved that x2+cx^2 + c has rho lengths of order p\sqrt p for all primes, or for most of them, and the whole running-time analysis of the method rests on the observation. It is one of the better-supported unproved heuristics in computational number theory, and it is unproved.

There is also a reason to expect the fit to be imperfect. A square has two square roots, so x2+cx^2 + c is two-to-one: half the values modulo pp have no predecessor at all and can only ever be a starting point. A genuinely random function has no such constraint. That the distribution still matches as closely as it does says the constraint barely affects when the first repeat happens — the walk never gets far enough into the space for the missing preimages to matter.

Finding the repeat without remembering the walk

To detect a repeat, the obvious method stores every value and checks each new one against the store. That needs memory proportional to the walk’s length, and at the sizes where the method is useful, p\sqrt p values may be billions.

Robert Floyd’s alternative stores two numbers. Run two copies of the walk from the same start: a tortoise that takes one step at a time and a hare that takes two. Once both are on the cycle, the hare gains one position per step, so it must catch the tortoise within one lap.

The walk x² + 1 modulo 337, drawn as the letter ρ. Starting at 2 and squaring and adding 1 modulo 337, the walk visits 8 values once on a tail and then runs round a cycle of 11 values for ever. A pointer moving one step and a pointer moving two first coincide after 11 steps, on 53.
Fig. 3 The same walk modulo 337 with Floyd’s search run on it. A pointer moving one step at a time and a pointer moving two first land on the same value, 53, after eleven steps — a multiple of the cycle length and no earlier than the end of the eight-value tail.

The meeting step has a clean characterisation. The tortoise at step ii and the hare at step 2i2i hold the same value exactly when both are on the cycle and the gap ii between their positions is a multiple of λ\lambda. So the first meeting is at the smallest multiple of λ\lambda that is at least μ\mu, which is never more than μ+λ\mu + \lambda — the length of the rho. Floyd’s method finds the repeat at most one lap late, using constant memory.

The price is a factor of about three in steps, since each round moves the hare twice and the tortoise once. Richard Brent’s variant, which teleports the tortoise to the hare at each power of two instead, is usually a quarter to a third faster and is what practical implementations use. Neither changes the square root.

The shadow walks

Now take m=nm = n, the number to be factored, and suppose n=pqn = pq with pp and qq unknown primes. Run the walk modulo nn.

The crucial property of x2+cx^2 + c is that it respects reduction. If xy(modp)x \equiv y \pmod p then x2+cy2+c(modp)x^2 + c \equiv y^2 + c \pmod p, because a polynomial with whole-number coefficients commutes with taking remainders. So the walk modulo nn, reduced modulo pp, is the walk modulo pp — a shadow walk on only pp values, running in lockstep with the visible one. The same is true modulo qq. That is exactly the correspondence two dials turning at once describes: a residue modulo nn is a pair of residues modulo pp and qq, and a polynomial map acts on each dial separately.

The shadow modulo pp closes its rho after about p\sqrt p steps. The visible walk modulo nn does not close until about n\sqrt n steps — much later. In between, there are tortoise and hare values xix_i and x2ix_{2i} that agree modulo pp but not modulo nn, so pp divides x2ixix_{2i} - x_i and nn does not. And that is visible: the greatest common divisor of x2ixix_{2i} - x_i with nn is pp, or at least a proper factor of nn, computed by Euclid’s algorithm with no knowledge of pp at all.

Pollard's method splitting 8051. A table of 3 steps of Pollard's rho method on 8051: the tortoise and hare values, the same values reduced modulo 83 and 97, and the greatest common divisor of their difference with 8051, which stays 1 until step 3 and then returns 97.
Fig. 4 The method run on 8051=83×978051 = 83 \times 97. Three steps of tortoise and hare modulo 8051, with the same values reduced modulo each prime in the grey columns the method never computes. At step three the pair agrees modulo 97 — both are 95 — and not modulo 83, so the divisor of their difference with 8051 is 97.

The two grey columns are the heart of the picture and the one thing the algorithm cannot see. It computes only the tortoise, the hare and one greatest common divisor per step. The coincidence happens entirely in a residue system defined by a number nobody knows, and the divisor computation reads it out.

Pollard's method splitting 455459. A table of 9 steps of Pollard's rho method on 455459: the tortoise and hare values, the same values reduced modulo 613 and 743, and the greatest common divisor of their difference with 455459, which stays 1 until step 9 and then returns 743.
Fig. 5 A longer run on 455459=613×743455459 = 613 \times 743. Eight steps show the shadows disagreeing in both columns; at step nine the hare and tortoise agree modulo 743, and the method returns 743 — the larger prime, because its shadow happened to close first.

The second table makes a point the arithmetic might hide. The method does not find the smallest factor; it finds whichever shadow’s rho is detected first. The expected time favours the smaller prime, since 613\sqrt{613} is less than 743\sqrt{743}, but the two are close and each rho length is a random-looking quantity with a wide spread, so either can win. When a number’s prime factors differ greatly in size, the smallest wins nearly always.

And there is a failure case worth naming. If both shadows close at the same step, the difference is divisible by pp and qq both, the divisor is nn itself, and nothing has been learned. The remedy is to change cc and start again, which gives new and effectively independent walks.

Two constants to avoid, and why

Implementations are told never to use c=0c = 0 or c=2c = -2, and the reasons are different and both worth seeing.

With c=0c = 0 the walk is xx2x \mapsto x^2, so the kk-th value is x02kx_0^{2^k}. Its rho is governed entirely by the multiplicative order of 22 modulo divisors of p1p - 1 — a clean algebraic structure, which is precisely what the method does not want. A structured walk can have a very long tail or a very long cycle for a particular prime, and nothing about its length is birthday-like.

With c=2c = -2 the structure is less obvious and more beautiful. Write x=t+t1x = t + t^{-1}; then x22=t2+t2x^2 - 2 = t^2 + t^{-2}. The map squares tt, which is the same doubling that makes the logistic map at parameter four solvable, where 4x(1x)4x(1-x) turns out to be the Chebyshev polynomial 2u212u^2 - 1 in disguise. The walk x22x^2 - 2 is therefore a disguised x2x^2, and inherits its algebraic orbit lengths.

The one polynomial family that makes a chaotic map exactly solvable is the one that makes a factoring walk fail, and for the same reason: being solvable and being random are opposites, and Pollard’s method needs the second.

What it costs

If the shadow modulo pp behaves like a random function, its rho is about πp/2\sqrt{\pi p/2} values long, and Floyd’s search spends a small multiple of that. So the cost depends on the size of the factor, not of the number being factored.

What Pollard's method costs, against the smaller prime. Steps to the first factor for 60 products of two primes, plotted against the smaller prime on logarithmic axes. The points follow a line of slope 0.47, close to the one half of √p.
Fig. 6 Sixty products of two primes, the smaller from about a hundred to over a hundred thousand and the larger up to forty times bigger. Steps to the first factor against the smaller prime on logarithmic axes: the fitted slope is 0.47, against the one half of p\sqrt p, and the largest number split is about 5.1×10115.1 \times 10^{11}.

The scatter around the line is not noise in the measurement; it is the spread of the rho length itself, which for a single prime ranges over a factor of several. Averaged across sixty products the slope is close to a half. The point well below the line near p=105p = 10^5 is a product whose larger factor’s shadow closed very early — the same effect as the 743 above, at a larger scale.

The comparison with trial division is the reason the method mattered. Dividing by every candidate up to pp costs about pp operations. Pollard’s method costs about p\sqrt p. A twelve-digit number with a six-digit factor takes hundreds of thousands of trial divisions and something over a thousand steps of the walk. Before the elliptic-curve method arrived in 1985, Pollard’s rho was the standard way to strip medium-sized factors off a large number, and in 1980 Brent and Pollard used it to factor the eighth Fermat number, 2256+12^{256} + 1, finding a sixteen-digit factor.

What the square root does not do is threaten well-chosen cryptographic keys. An RSA modulus is a product of two primes each around 1030010^{300}, and 10300\sqrt{10^{300}} is 1015010^{150} steps. The method is decisive against numbers with a small factor and irrelevant against numbers designed to have none, and factoring as a whole remains hard in exactly the cases that matter.

One divisor computation in a hundred

The tables compute a greatest common divisor at every step, and in practice nobody does. A divisor computation on numbers of a few hundred digits costs far more than a squaring, and at every step but the last it returns 1.

The standard repair uses the fact that a divisor of a product is visible in the product. Multiply the differences x2ixix_{2i} - x_i together modulo nn for a hundred steps, then take one divisor of the accumulated product with nn. If pp divided any one of the hundred differences, it divides the product, and the single computation finds it. The cost falls to one divisor computation per hundred steps, plus a hundred cheap multiplications.

There is one way this goes wrong, and it is the same failure as before in a new place. If pp divides one of the hundred differences and qq divides a later one, the accumulated product is divisible by both, and the divisor is nn. The fix is to back up: return to the start of the batch and take divisors one step at a time until the first non-trivial one appears. Since that happens only on the last batch, it costs at most a hundred extra computations once.

Batching changes nothing about the square root, and it is worth noticing why it is allowed at all. The method never needs to know which step produced the coincidence, only that one did. A procedure that tested each candidate separately would have to test them separately; a procedure looking for a coincidence among many can pool them, because a common divisor is preserved by multiplication in a way that almost nothing else is.

Collisions sought for their own sake

The rho idea applies to anything that can be iterated, and the most direct application goes back to where this essay began: finding two inputs a hash function sends to the same output.

Iterate the hash on its own output, starting anywhere. The walk lives in a space of 2b2^{b} values for a bb-bit hash, so it closes a rho after about 2b/22^{b/2} steps, and the two distinct values that lead into the joining point of the rho — the last value of the tail and the last value of the cycle — hash to the same thing. That is a collision, found with constant memory, which is exactly the saving the first calculation of the birthday bound noted a naive attack needed.

Paul van Oorschot and Michael Wiener made it parallel in 1994. Many machines each run a walk from a different start and report only distinguished points — values with, say, their first thirty bits equal to zero. Two walks that ever meet are identical from then on, so they reach the same distinguished point, and a central list of those points detects the merge. The work is shared across the machines with almost no communication, and it is the standard way a generic collision search — or a discrete-logarithm search of the kind below — is spread across many machines.

The idea outlived the factoring application, and its most important use now is in a different problem entirely.

The discrete logarithm problem asks, given gg and h=gxh = g^x in a group of order NN, for xx. Pollard adapted the rho walk in 1978: walk through elements of the form gahbg^a h^b, tracking the exponents aa and bb, and wait for a collision gahb=gahbg^a h^b = g^{a'} h^{b'}. The collision gives a linear equation for xx modulo NN. Its expected time is again a birthday time, about πN/2\sqrt{\pi N / 2} steps, with constant memory.

For the elliptic-curve groups used in modern public-key cryptography, this is the best general attack known. It is why a curve over a field of about 22562^{256} elements provides about 128 bits of security rather than 256: the attacker’s work is the square root of the group’s size, set by the same birthday arithmetic that sets a hash function’s collision resistance. Every such system’s key length is chosen with this walk in mind.

What the tables leave out

The rho figures are drawn modulo primes small enough to fit on a page, and at that size the method is pointless — a prime of 337 is found faster by dividing. The drawings show the shape the argument relies on, not a regime where the method earns its keep.

The tables show columns the algorithm cannot compute. That is their purpose, and it is also a hazard: the reader sees why the divisor becomes 97 at step three, while the method itself sees only that it did. Nothing in a run tells the method how close it is to success, which is why its running time can only be stated as an expectation.

And the cost figure is an average over sixty products, fitted with a line. It shows that the typical cost follows p\sqrt p closely enough to be useful. It does not show that any particular number will be split in about p\sqrt p steps, and it cannot show the thing the analysis would need, which is a proof that the walk behaves randomly at all.

Still open: why a polynomial walks like a random function

Every running-time claim above assumes that x2+cx^2 + c modulo a prime has rho lengths distributed like a random function’s. The evidence is overwhelming and the proof is missing. Eric Bach showed in 1991 that the first few collisions occur at the rate a random function would give — the probability of a collision within kk steps is about (k2)/p\binom{k}{2}/p for kk small compared with p1/4p^{1/4} — but the argument does not reach the p\sqrt p scale where the method actually finishes.

The difficulty is that the walk is deterministic and its values are polynomials of rapidly growing degree in the starting point, and bounding when two of them coincide modulo pp for most primes is a question about the distribution of roots of those polynomials. Proving that a specific simple map is as unstructured as a random one is the same kind of question as proving that a generator is unpredictable: easy to test, easy to believe, and without a general method.

A coincidence in numbers nobody can see

The method is worth keeping for its logic more than its speed. It exploits a coincidence it cannot observe, in a residue system defined by a number it does not know, and it converts that coincidence into something observable by a greatest common divisor — the one computation that reports on a hidden divisor without needing to name it.

And its cost is a birthday cost for a precise reason. The shadow walk is a sequence of values in a space of size pp, and a sequence of values in a space of size pp first repeats after about p\sqrt p of them, whether the values are birthdays, hash outputs or remainders of a squared number. The room of twenty-three and the eight-digit factor are the same event at two scales, and Pollard’s contribution was noticing that a coincidence which is a nuisance in one setting is, in another, the whole of the work.

What links here

Computed from the collection, not written here: the essays that point at this one.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

Birthday problemChinese remainder theoremCollisionFactoringGreatest common divisorIterationModular arithmeticPigeonhole principle