# Shanks' square forms factorization

(Redirected from SQUFOF)

Shanks's 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 ${\displaystyle x}$ and ${\displaystyle y}$ such that ${\displaystyle x^{2}-y^{2}=N}$, where ${\displaystyle N}$ is the integer to be factored. An improvement (noticed by Kraitchik) is to look for integers ${\displaystyle x}$ and ${\displaystyle y}$ such that ${\displaystyle x^{2}\equiv y^{2}{\pmod {N}}}$. Finding a suitable pair ${\displaystyle (x,y)}$ does not guarantee a factorization of ${\displaystyle N}$, but it implies that ${\displaystyle N}$ is a factor of ${\displaystyle x^{2}-y^{2}=(x-y)(x+y)}$, and there is a good chance that the prime divisors of ${\displaystyle N}$ are distributed between these two factors, so that calculation of the greatest common divisor of ${\displaystyle N}$ and ${\displaystyle x-y}$ will give a non-trivial factor of ${\displaystyle N}$.

A practical algorithm for finding pairs ${\displaystyle (x,y)}$ which satisfy ${\displaystyle x^{2}\equiv y^{2}{\pmod {N}}}$ 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.

## Algorithm

Input: ${\displaystyle N}$, the integer to be factored, which must be neither a prime number nor a perfect square, and a small multiplier ${\displaystyle k}$.

Output: a non-trivial factor of ${\displaystyle N}$.

The algorithm:

Initialize ${\displaystyle P_{0}=\lfloor {\sqrt {kN}}\rfloor ,Q_{0}=1,Q_{1}=kN-P_{0}^{2}.}$

Repeat

${\displaystyle b_{i}=\left\lfloor {\frac {P_{0}+P_{i-1}}{Q_{i}}}\right\rfloor ,P_{i}=b_{i}Q_{i}-P_{i-1},Q_{i+1}=Q_{i-1}+b_{i}(P_{i-1}-P_{i})}$

until ${\displaystyle Q_{i}}$ is a perfect square at some even ${\displaystyle i}$.

Initialize ${\displaystyle b_{0}=\left\lfloor {\frac {P_{0}-P_{i-1}}{\sqrt {Q_{i}}}}\right\rfloor ,P_{0}=b_{0}{\sqrt {Q_{i}}}+P_{i-1},Q_{0}={\sqrt {Q_{i}}},Q_{1}={\frac {kN-P_{0}^{2}}{Q_{0}}}}$

Repeat

${\displaystyle b_{i}=\left\lfloor {\frac {P_{0}+P_{i-1}}{Q_{i}}}\right\rfloor ,P_{i}=b_{i}Q_{i}-P_{i-1},Q_{i+1}=Q_{i-1}+b_{i}(P_{i-1}-P_{i})}$

until ${\displaystyle P_{i}=P_{i-1}.}$

Then if ${\displaystyle f=\gcd(N,P_{i})}$ is not equal to ${\displaystyle 1}$ and not equal to ${\displaystyle N}$, then ${\displaystyle f}$ is a non-trivial factor of ${\displaystyle N}$. Otherwise try another value of ${\displaystyle k}$.

Shanks's method has time complexity ${\displaystyle O({\sqrt[{4}]{N}})}$.

Stephen S. McMath (see link in External Link section) wrote a more detailed discussion of the mathematics of Shanks's method, together with a proof of its correctness.

## Example

N = 11111, k = 1

P0 = 105 Q0 = 1 Q1 = 86

P1 = 67 Q1 = 86 Q2 = 77

P2 = 87 Q2 = 77 Q3 = 46

P3 = 97 Q3 = 46 Q4 = 37

P4 = 88 Q4 = 37 Q5 = 91

P5 = 94 Q5 = 91 Q6 = 25

Here Q6 is a perfect square

P0 = 104 Q0 = 5 Q1 = 59

P1 = 73 Q1 = 59 Q2 = 98

P2 = 25 Q2 = 98 Q3 = 107

P3 = 82 Q3 = 107 Q4 = 41

P4 = 82

Here P3 = P4

gcd(11111, 82) = 41, which is a factor of 11111.

## Implementations

The PSIQS Java package contains a SquFoF implementation.