#!/usr/bin/env python from random import randrange as rndrng from primelist import primeList fpl = primeList(10000) def nBR(n): return rndrng(2**(n-1)+1, 2**n-1) def gLLP(n): while True: pc = nBR(n) for d in fpl: if pc % d == 0 and d**2 <= pc: break else: return pc def iMRP(mrc): mDBT = 0 ec = mrc-1 while ec % 2 == 0: ec >>= 1 mDBT += 1 assert(2**mDBT * ec == mrc -1) def tC(rt): if pow(rc, ec, mrc) == 1: return False for i in range(mDBT): if pow(rc, 2**i * ec, mrc) == mrc-1: return False return True nORT = 20 for i in range(nORT): rc = rndrng(2, mrc) if tC(rc): return False return True def genPrime(): while True: n = 512 pc = gLLP(n) if not iMRP(pc): continue else: return pc