Prerequisites:
Fermat's Factorisation is based on the representation of odd integer as difference of two squares:
This difference is factorable as (a+b)*(a-b)
We can use the above property to factorise modulus N in RSA when the difference between the primes is small:
Difference ~ (N)1/4
To implement basic approach of Fermat's Factorisation, we can do the following:
- Calculate
a
as ceil of square root of N - Calculate
b2
= a**2 - N - Check if
b2
is a perfect square. If b2 is a perfect square thenreturn a - Square_root(b2)
anda + Square_root(b2)
as the factors. If not, do the following:- Increment
a
by 1, i.e. a += 1 - Calculate
b2
again as a**2 - N - Repeat Step-3
- Increment
Check out the implementation of basic Fermat's Factorisation here