#!/usr/bin/env python3

from Cryptotools.Numbers.factorization import pollard_p_minus_1
from Cryptotools.Numbers.primeNumber import _millerRabinTest, getPrimeNumber
from sympy.ntheory.factor_ import pollard_pm1
from sympy import factorint



def pollard(n):
    p = pollard_p_minus_1(n, B=10)
    if p is not None:
        print(p, n / p)
        print(pollard_pm1(n, B=10))
        print()
        return True
    return False

def safePrime(p):
    nP = 2 * p + 1
    return _millerRabinTest(nP)

#pollard(299)
#pollard(257 * 1009)
#pollard(1684) # Doesn't works

# Test for RSA
#p = 281
while True:
    while True:
        p = getPrimeNumber(64)
        if ((p - 1) / 2520) % 2 == 0.0:
            #print(p)
            break
    #p = 322506219347091343
    #q = 13953581789873249851
    # n = p * q
    while True:
        q = getPrimeNumber(64)
        if int(((q - 1) / 2520) % 2) == 1:
            #print(q)
            break

    #q = 223
    n = p * q
    ##print(n)
    #print(pollard(n))
    #break
    if pollard(n): # We can factorize n
        break

print(safePrime(p))
#print(safePrime(q))
