import os
import sys
from math import isqrt
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))
if sys.path[1] != path:
sys.path.insert(1, path)
from shared import is_square
def factorize(N):
"""
Recovers the prime factors from a modulus using Fermat's factorization method.
:param N: the modulus
:return: a tuple containing the prime factors, or None if the factors were not found
"""
a = isqrt(N)
b = a * a - N
while b < 0 or not is_square(b):
a += 1
b = a * a - N
p = a - isqrt(b)
q = N // p
return p, q if p * q == N else None