# P-1 factorization method

P-1 is a factorization method invented by John Pollard in 1974.

It is based in the Fermat's Little Theorem that states that:

Let p be a prime which does not divide the integer a, then $a^{p-1}\equiv 1 \mbox{( mod } p)$.

From the previous congruence we also get:

$a^{k(p-1)}\equiv 1$, where k is a non-negative integer. This means that $a^{k(p-1)} - 1$

is multiple of p.

Let p be a prime divisor of the number N to be factored. If we somehow find a multiple of p-1 we will find a multiple of p which (in general) will not be also a multiple of N, so a greatest common divisor operation will reveal the prime divisor.

## Contents

### Step 1

Suppose that the largest prime factor of p-1 is less than a bound B1. Then the method proceeds to compute $a^E\,\pmod{N}$ where N is the number to factor.

$E = 2^{E_2} * 3^{E_3} * 5^{E_5} * ... * B$

where $E_2$ is selected so that $2^{E_2}$ is about B1 and the same for the other prime numbers. B is the greatest prime number less than or equal to B1.

In general, the number E should be multiple of p-1 because it is multiple of all prime factors of p-1. Notice that when p-1 has a repeated prime factor greater than $\sqrt{B1}$, E will not be multiple of p-1 and the method fails, but this happens with a very low probability.

The Little Fermat Theorem states that $a^{p-1}\equiv 1\,\pmod {p}$. Since E is multiple of p-1 we get $a^E\equiv 1\,\pmod {p}$. This means that $a^E-1$ will be multiple of p but not of N (in general).

So computing $\gcd(a^E-1,\,N)$ we will get a proper factor of N.

When factoring a number of the form $2^p-1$ (as in GIMPS) its prime factors have the form $2kp+1$, so the value p must always be a factor of E. In this case the value of a must not be 2, because $2^E\equiv 1\,\pmod {N}$, so $\gcd(2^E - 1, \,N) = N$, which is a trivial factor of N.

### Step 2

Most of the composite numbers have a prime factor much greater than the other prime factors. Suppose that all prime factors of p-1 are less than the bound B1 except for one prime factor which is in the range B1 to B2. In this case step 1 will not succeed and thus it will not reveal a factor of N.

Let $F\equiv a^E\,\pmod N$ be the output of the step 1.

Also precompute the values $S\equiv F^2\,\pmod N$ and $H\equiv F^6\,\pmod N$.

Let C be the previous multiple of 6 to B1.

Then we compute:

$Z_1\equiv F^{C+1}\,\pmod N$
$Z_2\equiv F^{C+7}\equiv Z_1 * H\,\pmod N$
$Z_3\equiv F^{C+13}\equiv Z_2 * H\,\pmod N$
$Z_4\equiv F^{C+19}\equiv Z_3 * H\,\pmod N$
...

and so on until the exponent reaches B2. Since there is a prime factor of p-1 in the range B1 to B2, there is one value computed that must be congruent to 1 (mod p). This does not cover the case that the factor has the form C+5, C+11, C+17, i.e. congruent to 5 modulo 6. In the first case we have:

$F^{C+5}\equiv 1\,\pmod p$

multiplying by $F^2$ we get

$Z_2\equiv F^{C+7}\equiv S\,\pmod p$

So we compute $Z\equiv (Z_1-1)(Z_2-1)(Z_2-S)(Z_3-1)(Z_3-S)(Z_4-1)(Z_4-S)...\pmod N$ and finally gcd(Z, N) will reveal a proper factor of N.

### Example

Suppose we want to find a factor of 229-1 with B1 = 10.

Then E = 23 × 32 × 5 × 7 × 29.

The number 29 was included as indicated on the last paragraph in Step 1.

Let a = 3. So the steps are:

$3^2\equiv 9$ (mod $2^{29} - 1$)
$9^2\equiv 81$ (mod $2^{29} - 1$)
$81^2\equiv 6561$ (mod $2^{29} - 1$)
$6561^3\equiv 35437295$ (mod $2^{29} - 1$)
$35437295^3\equiv 518991192$ (mod $2^{29} - 1$)
$518991192^5\equiv 142236746$ (mod $2^{29} - 1$)
$142236746^7\equiv 237876521$ (mod $2^{29} - 1$)
$237876521^{29}\equiv 171331425$ (mod $2^{29} - 1$)
gcd($171331425-1$,$2^{29}-1$) = $486737$.

So 229 - 1 = 486737 × 1103.

Notice that the factor found is composite. This normally occurs when B1 is too high (10 in this case).

For GIMPS, where the exponents are very high, the values of B1 are selected above 1,000,000.