diff --git a/algo.py b/algo.py index 29305c0ed3fcf745d31300d6f57d9a4e955e79fa..1b76bd8ee5cf305339fdbe8d5907300fd61cf88b 100644 --- a/algo.py +++ b/algo.py @@ -75,7 +75,7 @@ def exponentiation_rapide(a, exp, n): return int(r) def is_square(a): - """Check if a number is a perfect square, using the Newton methode + """Check if a number is a perfect square, based on the "Babylonian algorithm" for square root, from : https://stackoverflow.com/questions/2489435/check-if-a-number-is-a-perfect-square Args: @@ -109,20 +109,21 @@ def fermat_factorization(n): tuple of uint: the two coeficient a and b """ - a = math.ceil(math.sqrt(n)) - # a = 26262277040 - 10 - b = 0 - while(a <= n): # a cannot be greater than n, because : n = (a + b) * q > 0 => a <= n - b2 = a**2 - n - - if (is_square(b2)): - b = int(math.sqrt(b2)) - break - + if (n % 2 == 0): + print("Fermat's factorization don't work on even number") + return + + a = math.ceil(math.sqrt(n)) # a = 26262277040 (for the key given) + b2 = a**2 - n + + while (not is_square(b2)): a += 1 + b2 = a**2 - n + + if (a >= n): + print("Warning : n could be prime !") - if(a > n): print("The Fermat's factorization didn't work on n") - + b = int(math.sqrt(b2)) return (a, b) def decode_msg(M):