コード例 #1
0
def findPrimeOrderPoint(Ep, p, a, b):
    if isPrime(len(Ep) + 1):
        return (Ep[random.randint(0, len(Ep) - 1)], len(Ep) + 1)
    for G in Ep:
        #        print(f'\nG = {G}\n')
        if G[1] == 0:
            continue
        n = findPointOrder(p, a, b, G)
        if isPrime(n):
            return (G, n)

    return 'Not Found'
コード例 #2
0
def generateSophieGermainPrime(left, right, return_size=1):
    assert (left < right)

    appended = 0
    prime_list = []

    while appended < return_size:
        p = random.randint(left, right)
        if isPrime(p):
            q = (p - 1) // 2
            if isPrime(q):
                prime_list.append(p)
                appended += 1

    return prime_list
コード例 #3
0
def findGenerator(Ep, p, a):
    N = len(Ep) + 1
    if isPrime(N):
        return Ep[random.randint(0, len(Ep)-1)]
    n = primeFactorization(N)[0]
    h = N//n
    G = 'temp'    
    visited = [0] * len(Ep)
    
    while G != 0:
        if visited == [1] * len(Ep):
            break
        
        index = random.randint(0, len(Ep)-1)
        if visited[index] == 0:
            visited[index] == 1
        else:
            continue
        
        P = Ep[index]
        G = doubleAndAdd(h, P, p, a)
    
    if G == 0:
        return P
    else:
        return 'error 404 generator not found :<'
コード例 #4
0
def getPrimitiveRootList(n):
    assert (isPrime(n))
    primitiveRootList = []

    for num in range(2, n):
        if isPrimitiveRoot(num, n):
            primitiveRootList.append(num)

    return len(primitiveRootList), primitiveRootList
コード例 #5
0
def hasPrimitiveRoot(n):
    if isPrime(n) or n == 4:
        return True

    factors = primeFactorization(n)
    counter = collections.Counter(factors)
    if counter[2] > 1:
        return False
    factors = list(set(factors))
    if len(factors) > 2:
        return False
    if len(factors) == 2:
        if 2 not in factors:
            return False
    if not isPrime(factors[0]):
        return False

    return True
コード例 #6
0
def generatePrimeAfter(n):
    if n % 2 == 0:
        n += 1
    else:
        n += 2

    while not isPrime(n):
        n += 2

    return n
コード例 #7
0
def isPrimitiveRoot(g, n):
    assert (isPrime(n))
    assert (0 < g and g < n)
    flag = True

    factors = list(set(primeFactorization(n - 1)))
    for f in factors:
        if pow(g, (n - 1) // f, n) == 1:
            flag = False
            break

    return flag
コード例 #8
0
def generatePrime(digits, return_size=1):
    assert (digits > 0 and return_size > 0)

    appended = 0
    prime_list = []

    while appended < return_size:
        n = random.randint(10**(digits - 1), 10**digits - 1)
        if isPrime(n):
            prime_list.append(n)
            appended += 1

    return prime_list
コード例 #9
0
def calculateE(p, a, b):
    assert(isPrime(p))
    assert(isEllipticCurve(p, a, b))
    
    Qp = []
    for x in range(1, (p-1)//2 + 1):
        Qp.append(pow(x, 2, p))
        
    f = lambda x: (x**3 + a*x + b) % p
    Ep = []
    for x in range(p):
        if f(x) in Qp:
            index = Qp.index(f(x)) + 1
            y1 = index
            y2 = p - index
            Ep.append((x, y1))
            Ep.append((x, y2))
        elif f(x) == 0:
            Ep.append((x, 0))
    
    return Ep
コード例 #10
0
def primeFactorization(n):
    factor = []
    prime_idx = 0
    global highPrime
    while n > 1:
        if isPrime(n):
            factor.append(n)
            break
        else:
            if prime_idx > 9999:
                p = pollardsRho(n)
                n //= p
                factor.append(p)
            else:
                if n % lowPrimes[prime_idx] == 0:
                    factor.append(lowPrimes[prime_idx])
                    n //= lowPrimes[prime_idx]
                else:
                    prime_idx += 1
                    if prime_idx >= len(lowPrimes):
                        highPrime = generatePrimeAfter(highPrime)
                        lowPrimes.append(highPrime)

    return factor
コード例 #11
0
def modularInverse2(a, n):
    if isPrime(n):
        return pow(a, n - 2, n)

    return pow(a, phi(n) - 1, n)