Shanks's square forms factorization

From HandWiki

Shanks' square forms factorization is a method for integer factorization devised by Daniel Shanks as an improvement on Fermat's factorization method.

The success of Fermat's method depends on finding integers [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] such that [math]\displaystyle{ x^2-y^2=N }[/math], where [math]\displaystyle{ N }[/math] is the integer to be factored. An improvement (noticed by Kraitchik) is to look for integers [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] such that [math]\displaystyle{ x^2\equiv y^2\pmod{N} }[/math]. Finding a suitable pair [math]\displaystyle{ (x, y) }[/math] does not guarantee a factorization of [math]\displaystyle{ N }[/math], but it implies that [math]\displaystyle{ N }[/math] is a factor of [math]\displaystyle{ x^2-y^2=(x-y)(x+y) }[/math], and there is a good chance that the prime divisors of [math]\displaystyle{ N }[/math] are distributed between these two factors, so that calculation of the greatest common divisor of [math]\displaystyle{ N }[/math] and [math]\displaystyle{ x-y }[/math] will give a non-trivial factor of [math]\displaystyle{ N }[/math].

A practical algorithm for finding pairs [math]\displaystyle{ (x,y) }[/math] which satisfy [math]\displaystyle{ x^2\equiv y^2\pmod{N} }[/math] was developed by Shanks, who named it Square Forms Factorization or SQUFOF. The algorithm can be expressed in terms of continued fractions or in terms of quadratic forms. Although there are now much more efficient factorization methods available, SQUFOF has the advantage that it is small enough to be implemented on a programmable calculator. Shanks programmed it on an HP-65, made in 1974, which has storage for only nine digit numbers and allows only 100 steps/keystrokes of programming. There are versions of the algorithm that use little memory and versions that store a list of values that run more quickly.

In 1858, the Czech mathematician Václav Šimerka used a method similar to SQUFOF to factor [math]\displaystyle{ (10^{17} - 1)/9 }[/math] [math]\displaystyle{ = }[/math] [math]\displaystyle{ 11111111111111111 }[/math] [math]\displaystyle{ = }[/math] [math]\displaystyle{ 2071723 \cdot 5363222357 }[/math].[1]

Algorithm

Note This version of the algorithm works on some examples but often gets stuck in a loop.

This version does not use a list.

Input: [math]\displaystyle{ N }[/math], the integer to be factored, which must be neither a prime number nor a perfect square, and a small positive integer, [math]\displaystyle{ k }[/math].

Output: a non-trivial factor of [math]\displaystyle{ N }[/math].

The algorithm:

Initialize [math]\displaystyle{ i=0,P_0=\lfloor\sqrt{kN}\rfloor,Q_{-1}=1,Q_0=kN-P_0^2. }[/math]

Repeat

[math]\displaystyle{ i=i+1,b_i=\left\lfloor\frac{P_0+P_{i-1}}{Q_{i-1}}\right\rfloor,P_i=b_iQ_{i-1}-P_{i-1},Q_i=Q_{i-2}+b_i(P_{i-1}-P_i) }[/math]

until [math]\displaystyle{ Q_i }[/math] is a perfect square at some odd value of [math]\displaystyle{ i }[/math].

Start the second phase (reverse cycle).

Initialize [math]\displaystyle{ b_0=\left\lfloor\frac{P_0-P_i}{\sqrt{Q_i}}\right\rfloor }[/math], [math]\displaystyle{ Q_{-1}=\sqrt{Q_i} }[/math], and [math]\displaystyle{ P_0=b_0\sqrt{Q_i}+P_i }[/math], where [math]\displaystyle{ P_0, P_i }[/math], and [math]\displaystyle{ Q_{i} }[/math] are from the previous phase. The [math]\displaystyle{ b_0 }[/math] used in the calculation of [math]\displaystyle{ P_0 }[/math] is the recently calculated value of [math]\displaystyle{ b_0 }[/math].

Set [math]\displaystyle{ i=0 }[/math] and [math]\displaystyle{ Q_0=\frac{kN-P_0^2}{Q_{-1}} }[/math], where [math]\displaystyle{ P_0 }[/math] is the recently calculated value of [math]\displaystyle{ P_0 }[/math].

Repeat

[math]\displaystyle{ i=i+1,b_i=\left\lfloor\frac{P_0+P_{i-1}}{Q_{i-1}}\right\rfloor,P_i=b_iQ_{i-1}-P_{i-1},Q_i=Q_{i-2}+b_i(P_{i-1}-P_i) }[/math]

until [math]\displaystyle{ P_i=P_{i-1}. }[/math][citation needed]

Then if [math]\displaystyle{ f=\gcd(N,P_i) }[/math] is not equal to [math]\displaystyle{ 1 }[/math] and not equal to [math]\displaystyle{ N }[/math], then [math]\displaystyle{ f }[/math] is a non-trivial factor of [math]\displaystyle{ N }[/math]. Otherwise try another value of [math]\displaystyle{ k }[/math].[citation needed]

Shanks' method has time complexity [math]\displaystyle{ O(\sqrt[4]{N}) }[/math].[2]

Stephen S. McMath wrote a more detailed discussion of the mathematics of Shanks' method, together with a proof of its correctness.[3]

Example

Let [math]\displaystyle{ N = 11111 }[/math]

[math]\displaystyle{ Q_{-1}=1 }[/math]

Cycle forward
[math]\displaystyle{ i }[/math] [math]\displaystyle{ b_{i} }[/math] [math]\displaystyle{ P_{i} }[/math] [math]\displaystyle{ Q_{i} }[/math]
[math]\displaystyle{ 0 }[/math] [math]\displaystyle{ }[/math] [math]\displaystyle{ 105 }[/math] [math]\displaystyle{ 86 }[/math]
[math]\displaystyle{ 1 }[/math] [math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 67 }[/math] [math]\displaystyle{ 77 }[/math]
[math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 87 }[/math] [math]\displaystyle{ 46 }[/math]
[math]\displaystyle{ 3 }[/math] [math]\displaystyle{ 4 }[/math] [math]\displaystyle{ 97 }[/math] [math]\displaystyle{ 37 }[/math]
[math]\displaystyle{ 4 }[/math] [math]\displaystyle{ 5 }[/math] [math]\displaystyle{ 88 }[/math] [math]\displaystyle{ 91 }[/math]
[math]\displaystyle{ 5 }[/math] [math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 94 }[/math] [math]\displaystyle{ 25 }[/math]

Here [math]\displaystyle{ Q_{5} = 25 }[/math] is a perfect square, so the first phase ends.

For the second phase, set [math]\displaystyle{ Q_{-1} = \sqrt{25} = 5 }[/math]. Then:

Reverse cycle
[math]\displaystyle{ i }[/math] [math]\displaystyle{ b_{i} }[/math] [math]\displaystyle{ P_{i} }[/math] [math]\displaystyle{ Q_{i} }[/math]
[math]\displaystyle{ 0 }[/math] [math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 104 }[/math] [math]\displaystyle{ 59 }[/math]
[math]\displaystyle{ 1 }[/math] [math]\displaystyle{ 3 }[/math] [math]\displaystyle{ 73 }[/math] [math]\displaystyle{ 98 }[/math]
[math]\displaystyle{ 2 }[/math] [math]\displaystyle{ 1 }[/math] [math]\displaystyle{ 25 }[/math] [math]\displaystyle{ 107 }[/math]
[math]\displaystyle{ 3 }[/math] [math]\displaystyle{ 1 }[/math] [math]\displaystyle{ 82 }[/math] [math]\displaystyle{ 41 }[/math]
[math]\displaystyle{ 4 }[/math] [math]\displaystyle{ 4 }[/math] [math]\displaystyle{ 82 }[/math] [math]\displaystyle{ }[/math]

Here [math]\displaystyle{ P_{3} = P_{4} = 82 }[/math], so the second phase ends. Now calculate [math]\displaystyle{ gcd(11111, 82) = 41 }[/math], which is a factor of [math]\displaystyle{ 11111 }[/math].

Thus, [math]\displaystyle{ N = 11111 = 41 \cdot 271 }[/math].

Example implementation

Below is an example of C function for performing SQUFOF factorization on unsigned integer not larger than 64 bits, without overflow of the transient operations.[citation needed]

#include <inttypes.h>
#define nelems(x) (sizeof(x) / sizeof((x)[0]))

const int multiplier[] = {1, 3, 5, 7, 11, 3*5, 3*7, 3*11, 5*7, 5*11, 7*11, 3*5*7, 3*5*11, 3*7*11, 5*7*11, 3*5*7*11};

uint64_t SQUFOF( uint64_t N )
{
    uint64_t D, Po, P, Pprev, Q, Qprev, q, b, r, s;
    uint32_t L, B, i;
    s = (uint64_t)(sqrtl(N)+0.5);
    if (s*s == N) return s;
    for (int k = 0; k < nelems(multiplier) && N <= UINT64_MAX/multiplier[k]; k++) {
        D = multiplier[k]*N;
        Po = Pprev = P = sqrtl(D);
        Qprev = 1;
        Q = D - Po*Po;
        L = 2 * sqrtl( 2*s );
        B = 3 * L;
        for (i = 2 ; i < B ; i++) {
            b = (uint64_t)((Po + P)/Q);
            P = b*Q - P;
            q = Q;
            Q = Qprev + b*(Pprev - P);
            r = (uint64_t)(sqrtl(Q)+0.5);
            if (!(i & 1) && r*r == Q) break;
            Qprev = q;
            Pprev = P;
        };
        if (i >= B) continue;
        b = (uint64_t)((Po - P)/r);
        Pprev = P = b*r + P;
        Qprev = r;
        Q = (D - Pprev*Pprev)/Qprev;
        i = 0;
        do {
            b = (uint64_t)((Po + P)/Q);
            Pprev = P;
            P = b*Q - P;
            q = Q;
            Q = Qprev + b*(Pprev - P);
            Qprev = q;
            i++;
        } while (P != Pprev);
        r = gcd(N, Qprev);
        if (r != 1 && r != N) return r;
    }
    return 0;
}

References

  1. Lemmermeyer, F. (2013). "Václav Šimerka: quadratic forms and factorization". LMS Journal of Computation and Mathematics 16: 118–129. doi:10.1112/S1461157013000065. 
  2. (Riesel 1994:189)
  3. "Daniel Shanks' Square Forms Factorization". 2004. CiteSeerX 10.1.1.107.9984.

External links