def getPrimePair(bits=512): assert bits % 4 == 0 p = MillerRabin.gen_prime(bits) q = MillerRabin.gen_prime_range(p + 1, 2 * p) return p, q
def gen_prime(): # 测试三次 prime = gen_random.pseudoprime_number() flag1 = MillerRabin.millerrabin(prime) flag2 = MillerRabin.millerrabin(prime) flag3 = MillerRabin.millerrabin(prime) if not (flag1 and flag2 and flag3): prime = gen_prime() '''if MillerRabin.millerrabin(prime) == False: prime = gen_prime()''' return prime
def generateKey(): global p, q, n, e, d, phiN start = time.time() p = mr.generateLargePrime(548) q = mr.generateLargePrime(548) n = p * q phiN = (p-1) * (q-1) e = random.randrange(3, phiN-1) while (gcd(e, phiN) != 1): e = random.randrange(2, phiN-1) d = modinv(e, phiN)
def getPrimePair(bits=512): ''' genera un par de primos p , q con p de nbits y p < q < 2p ''' assert bits%4==0 p = MillerRabin.gen_prime(bits) q = MillerRabin.gen_prime_range(p+1, 2*p) return p,q
def __init__(self,nonPrimePeriod): while True: if MillerRabin.miller_rabin(nonPrimePeriod): self.period = nonPrimePeriod break else: nonPrimePeriod = nonPrimePeriod + 1
def gen_prime(n): if (n < 200): n = 200 a = 10**n b = a * 10 x = random.randrange(a, b) x |= 1 while (pow(2, x - 1, x) != 1 or MillerRabin.is_Prime(x) == False): x += 2 return x
def gen_RSApq(n): x0 = gen_prime(n // 3) a = 10**(n - n // 3) b = a * 100 #on genere un grand nombre premier #puis on en cherche un de la forme k*x+1 #ainsi p-1 ne sera pas seulement composé de petits facteurs k = random.randrange(a, b) x = k * x0 + 1 while (x & 1 and pow(2, x - 1, x) != 1 and not MillerRabin.is_Prime(x)): x += x0 return x
def is_good_prime(self, p): return p % 4 == 3 and MillerRabin.is_prime(p)
def generate_prime(num_bit): x = generate_bbs(num_bit) while not MillerRabin.is_prime(x): x = generate_bbs(num_bit) return x
import MillerRabin import time import sys if __name__ == "__main__": # some primes lst = [] for i in range(2, 30): if MillerRabin.miller_rabin(i): lst.append(i) print("{} are the primes up to 30".format(lst)) # some bigger primes count = 0 for i in range(2, 10000): if MillerRabin.miller_rabin(i): count += 1 print("{} primes up to 10000 ({}%)".format(count, count/100)) # some time expensive calculation (takes ~18 secs) t1 = time.time() i = 2 count = 0 while i < 1000000: if MillerRabin.miller_rabin(i): count += 1 i += 1 if (i % 100000 == 0): print("{}%".format(i/10000))
def generate_prime(rng, bits): import MillerRabin return MillerRabin.gen_prime(bits, rng)
#!/usr/bin/python3 import random import math import sys import MillerRabin import algo factors = [int(nb) for nb in range(3, 100, 2) if MillerRabin.is_Prime(nb)] def main(): lenNb = random.randint(200, 210) eps = random.randint(1, 10) p = gen_RSApq(lenNb) q = gen_RSApq(lenNb + eps) n = p * q e = gen_exp(p, q) d = congru_equation(e, 1, (p - 1) * (q - 1)) m = random.randint(10**100, 10**200) print(m) c = pow(m, e, n) plain = pow(c, d, n) print(plain == m) def gen_exp(p, q): e = 1 prod = (p - 1) * (q - 1)
def get_big_prime(bits): return MillerRabin.gen_prime(bits)