def test_NotPrimeValues(self): """ Testing MR on not prime numbers """ list_not_primes = [4294967297, 123456789101112] for a in list_not_primes : mr = MillerRabin(a) mr.run() self.assertFalse(mr.is_prime)
def test_PrimeValues(self): """ Testing MR on different known prime numbers src= http://fr.wikipedia.org/wiki/Nombre_premier """ list_primes = [13, 131071, 524287, 2147483647, 3203431780337, 170141183460469231731687303715884105727, 20988936657440586486151264256610222593863921] for a in list_primes : mr = MillerRabin(a) mr.run() self.assertTrue(mr.is_prime)
def test_PrimeCount(self): """ Counts the number of primes in the interval [1024, 2047] The result should be 137 """ counter = 0 for a in range(1024, 2048): mr = MillerRabin(a) mr.run() if mr.is_prime : counter += 1 self.assertEqual(counter, 137)
def primes(top): """ returns the top- prime numbers uses and completes _smallprimes """ if top <= max(_smallprimes) : return [x for x in _smallprimes if x < top] else : mr = MillerRabin() for i in range(max(_smallprimes) + 1, top): mr.initialize(i, 30) mr.run() if (i % 2) and mr.is_prime : _smallprimes.append(i) return _smallprimes
def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.mr = MillerRabin() self.pm = Pm1Pollard() self.pg = PrimeGen() # Set up the user interface from Designer. self.ui = Ui_MainWindow() self.ui.setupUi(self) # Connecting signals and slots QtCore.QObject.connect(self.ui.actionA_propos, QtCore.SIGNAL(_fromUtf8("triggered()")), self.open_about) QtCore.QObject.connect(self.ui.btn_mr_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_mr) QtCore.QObject.connect(self.ui.btn_mr_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_mr) QtCore.QObject.connect(self.ui.btn_mr_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_mr) QtCore.QObject.connect(self.ui.btn_pm_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_pm) QtCore.QObject.connect(self.ui.btn_pm_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_pm) QtCore.QObject.connect(self.ui.btn_pm_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_pm) QtCore.QObject.connect(self.ui.btn_pg_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_pg) QtCore.QObject.connect(self.ui.btn_pg_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_pg) QtCore.QObject.connect(self.ui.btn_pg_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_pg)
def primes(top): """ returns the top- prime numbers uses and completes _smallprimes """ if top <= max(_smallprimes): return [x for x in _smallprimes if x < top] else: mr = MillerRabin() for i in range(max(_smallprimes) + 1, top): mr.initialize(i, 30) mr.run() if (i % 2) and mr.is_prime: _smallprimes.append(i) return _smallprimes
def __init__(self, size=1): self.mr = MillerRabin() self.initialize(size)
class PrimeGen(object): def __init__(self, size=1): self.mr = MillerRabin() self.initialize(size) def initialize(self, size): ''' Initializes the variables ''' self.log_msg = "" self.size = size self.bot = 10**(size - 1) self.top = (10**size) - 1 self.cand = 0 self.cpt = 1 self.nb_mr_tests = 5 self.is_prime = False self.is_over = False self.step_by_step = False self.not_primes = [] self.log("Running Prime Gen: Size = %d" % (self.size)) def run(self): ''' Iterates until the end of the algorithm ''' while not self.is_prime: self.step() def step(self): ''' Computes only 1 iteration Adds logs at each step ''' if not self.step_by_step and not self.is_over: self.step_by_step = True self.start_time = time.time() if not self.is_over: bot = self.bot top = self.top offset = 10 self.log("Iteration %d" % (self.cpt), offset) self.cpt += 1 self.cand = randint(bot, top) self.log( "Generating random integer of size %d: R = %d" % (self.size, self.cand), offset * 2) # génération d'un nombre impair dans les limites while ((self.cand % 2) == 0 or self.cand in self.not_primes): self.log("R is even or composite, try an other one", offset * 2) self.cand = randint(bot, top) self.log( "Generating random integer of size %d: R = %d" % (self.size, self.cand), offset * 2) #test de primalité self.log( "Running Miller Rabin: N = %d, K = %d" % (self.cand, self.nb_mr_tests), offset * 2) self.mr.initialize(self.cand, self.nb_mr_tests) self.mr.run() self.is_prime = self.mr.is_prime if self.is_prime: self.step_by_step = False self.is_over = True self.log("%d is Prime => End" % (self.cand)) self.log("Duration: %fs" % (time.time() - self.start_time)) elif not self.is_prime: self.log("%d is Composite => Continue" % (self.cand), offset * 2) self.not_primes.append(self.cand) def log(self, msg, offset=0): ''' Logs a message ''' for _ in range(offset): self.log_msg = self.log_msg + " " self.log_msg = self.log_msg + msg + "\n" def get_state(self): ''' Retuns the current state with all the variables ''' return "Size: %d - Number: %d - Prime: %d" % (self.size, self.cand, self.is_prime)
class PrimeGen(object): def __init__(self, size=1): self.mr = MillerRabin() self.initialize(size) def initialize(self, size): ''' Initializes the variables ''' self.log_msg = "" self.size = size self.bot = 10 ** (size - 1) self.top = (10 ** size) - 1 self.cand = 0 self.cpt = 1 self.nb_mr_tests = 5 self.is_prime = False self.is_over = False self.step_by_step = False self.not_primes = [] self.log("Running Prime Gen: Size = %d" % (self.size)) def run(self): ''' Iterates until the end of the algorithm ''' while not self.is_prime: self.step() def step(self): ''' Computes only 1 iteration Adds logs at each step ''' if not self.step_by_step and not self.is_over: self.step_by_step = True self.start_time = time.time() if not self.is_over: bot = self.bot top = self.top offset = 10 self.log("Iteration %d" % (self.cpt), offset) self.cpt += 1 self.cand = randint(bot, top) self.log("Generating random integer of size %d: R = %d" % (self.size, self.cand), offset * 2) # génération d'un nombre impair dans les limites while ((self.cand % 2) == 0 or self.cand in self.not_primes): self.log("R is even or composite, try an other one", offset * 2) self.cand = randint(bot, top) self.log("Generating random integer of size %d: R = %d" % (self.size, self.cand), offset * 2) #test de primalité self.log("Running Miller Rabin: N = %d, K = %d" % (self.cand, self.nb_mr_tests), offset * 2) self.mr.initialize(self.cand, self.nb_mr_tests) self.mr.run() self.is_prime = self.mr.is_prime if self.is_prime: self.step_by_step = False self.is_over = True self.log("%d is Prime => End" % (self.cand)) self.log("Duration: %fs" % (time.time() - self.start_time)) elif not self.is_prime: self.log("%d is Composite => Continue" % (self.cand), offset * 2) self.not_primes.append(self.cand) def log(self, msg, offset=0): ''' Logs a message ''' for _ in range(offset): self.log_msg = self.log_msg + " " self.log_msg = self.log_msg + msg + "\n" def get_state(self): ''' Retuns the current state with all the variables ''' return "Size: %d - Number: %d - Prime: %d" % (self.size, self.cand, self.is_prime)
class Main(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.mr = MillerRabin() self.pm = Pm1Pollard() self.pg = PrimeGen() # Set up the user interface from Designer. self.ui = Ui_MainWindow() self.ui.setupUi(self) # Connecting signals and slots QtCore.QObject.connect(self.ui.actionA_propos, QtCore.SIGNAL(_fromUtf8("triggered()")), self.open_about) QtCore.QObject.connect(self.ui.btn_mr_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_mr) QtCore.QObject.connect(self.ui.btn_mr_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_mr) QtCore.QObject.connect(self.ui.btn_mr_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_mr) QtCore.QObject.connect(self.ui.btn_pm_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_pm) QtCore.QObject.connect(self.ui.btn_pm_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_pm) QtCore.QObject.connect(self.ui.btn_pm_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_pm) QtCore.QObject.connect(self.ui.btn_pg_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_pg) QtCore.QObject.connect(self.ui.btn_pg_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_pg) QtCore.QObject.connect(self.ui.btn_pg_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_pg) def open_about(self): dial = QDialog() self.about = Ui_Dialog() self.about.setupUi(dial) dial.exec() ''' Miller Rabin ''' def run_mr(self): if not self.mr.step_by_step: self.reset_mr() self.mr.initialize(int(self.ui.txt_mr_n.text()), int(self.ui.txt_mr_k.text())) self.mr.run() self.add_log_mr(self.mr.log_msg) if self.mr.is_prime: self.ui.txt_mr_res.setText("Prime") else: self.ui.txt_mr_res.setText("Composite") def step_mr(self): if not self.mr.step_by_step: self.reset_mr() self.mr.initialize(int(self.ui.txt_mr_n.text()), int(self.ui.txt_mr_k.text())) self.mr.step() self.add_log_mr(self.mr.log_msg) if self.mr.is_over: if self.mr.is_prime: self.ui.txt_mr_res.setText("Prime") else: self.ui.txt_mr_res.setText("Composite") else: self.ui.txt_mr_res.setText("") def reset_mr(self): self.ui.txt_mr_exec.setPlainText("") self.ui.txt_mr_res.setText("") self.mr.step_by_step = False ''' Pm1 Pollard ''' def run_pm(self): if not self.pm.step_by_step: self.reset_pm() self.pm.initialize(int(self.ui.txt_pm_n.text()), int(self.ui.txt_pm_b.text()), int(self.ui.txt_pm_it.text())) self.pm.run() self.add_log_pm(self.pm.log_msg) if self.pm.g == 0 or self.pm.g == 1: self.ui.txt_pm_res.setText("Factorization failed") else: self.ui.txt_pm_res.setText("Success: %d" % self.pm.g) def step_pm(self): if not self.pm.step_by_step: self.reset_pm() self.pm.initialize(int(self.ui.txt_pm_n.text()), int(self.ui.txt_pm_b.text()), int(self.ui.txt_pm_it.text())) self.pm.step() self.add_log_pm(self.pm.log_msg) if self.pm.is_over: if self.pm.g == 0 or self.pm.g == 1: self.ui.txt_pm_res.setText("Factorization failed") else: self.ui.txt_pm_res.setText("Success: %d" % self.pm.g) else: self.ui.txt_pm_res.setText("") def reset_pm(self): self.ui.txt_pm_exec.setPlainText("") self.ui.txt_pm_res.setText("") self.pm.step_by_step = False ''' Prime Gen ''' def run_pg(self): if not self.pg.step_by_step: self.reset_pg() self.pg.initialize(int(self.ui.txt_pg_size.text())) self.pg.run() self.add_log_pg(self.pg.log_msg) self.ui.txt_pg_res.setText(str(self.pg.cand)) def step_pg(self): if not self.pg.step_by_step: self.reset_pg() self.pg.initialize(int(self.ui.txt_pg_size.text())) self.pg.step() self.add_log_pg(self.pg.log_msg) if self.pg.is_over: self.ui.txt_pg_res.setText(str(self.pg.cand)) else: self.ui.txt_pg_res.setText("") def reset_pg(self): self.ui.txt_pg_exec.setPlainText("") self.ui.txt_pg_res.setText("") self.pg.step_by_step = False def add_log_mr(self, log): self._add_log(log, self.ui.txt_mr_exec) def add_log_pm(self, log): self._add_log(log, self.ui.txt_pm_exec) def add_log_pg(self, log): self._add_log(log, self.ui.txt_pg_exec) def _add_log(self, log, txt_wdgt): txt_wdgt.setPlainText(log) txt_wdgt.verticalScrollBar().setSliderPosition(txt_wdgt.verticalScrollBar().maximum())
class Main(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.mr = MillerRabin() self.pm = Pm1Pollard() self.pg = PrimeGen() # Set up the user interface from Designer. self.ui = Ui_MainWindow() self.ui.setupUi(self) # Connecting signals and slots QtCore.QObject.connect(self.ui.actionA_propos, QtCore.SIGNAL(_fromUtf8("triggered()")), self.open_about) QtCore.QObject.connect(self.ui.btn_mr_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_mr) QtCore.QObject.connect(self.ui.btn_mr_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_mr) QtCore.QObject.connect(self.ui.btn_mr_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_mr) QtCore.QObject.connect(self.ui.btn_pm_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_pm) QtCore.QObject.connect(self.ui.btn_pm_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_pm) QtCore.QObject.connect(self.ui.btn_pm_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_pm) QtCore.QObject.connect(self.ui.btn_pg_run, QtCore.SIGNAL(_fromUtf8("clicked()")), self.run_pg) QtCore.QObject.connect(self.ui.btn_pg_step, QtCore.SIGNAL(_fromUtf8("clicked()")), self.step_pg) QtCore.QObject.connect(self.ui.btn_pg_reset, QtCore.SIGNAL(_fromUtf8("clicked()")), self.reset_pg) def open_about(self): dial = QDialog() self.about = Ui_Dialog() self.about.setupUi(dial) dial.exec() ''' Miller Rabin ''' def run_mr(self): if not self.mr.step_by_step: self.reset_mr() self.mr.initialize(int(self.ui.txt_mr_n.text()), int(self.ui.txt_mr_k.text())) self.mr.run() self.add_log_mr(self.mr.log_msg) if self.mr.is_prime: self.ui.txt_mr_res.setText("Prime") else: self.ui.txt_mr_res.setText("Composite") def step_mr(self): if not self.mr.step_by_step: self.reset_mr() self.mr.initialize(int(self.ui.txt_mr_n.text()), int(self.ui.txt_mr_k.text())) self.mr.step() self.add_log_mr(self.mr.log_msg) if self.mr.is_over: if self.mr.is_prime: self.ui.txt_mr_res.setText("Prime") else: self.ui.txt_mr_res.setText("Composite") else: self.ui.txt_mr_res.setText("") def reset_mr(self): self.ui.txt_mr_exec.setPlainText("") self.ui.txt_mr_res.setText("") self.mr.step_by_step = False ''' Pm1 Pollard ''' def run_pm(self): if not self.pm.step_by_step: self.reset_pm() self.pm.initialize(int(self.ui.txt_pm_n.text()), int(self.ui.txt_pm_b.text()), int(self.ui.txt_pm_it.text())) self.pm.run() self.add_log_pm(self.pm.log_msg) if self.pm.g == 0 or self.pm.g == 1: self.ui.txt_pm_res.setText("Factorization failed") else: self.ui.txt_pm_res.setText("Success: %d" % self.pm.g) def step_pm(self): if not self.pm.step_by_step: self.reset_pm() self.pm.initialize(int(self.ui.txt_pm_n.text()), int(self.ui.txt_pm_b.text()), int(self.ui.txt_pm_it.text())) self.pm.step() self.add_log_pm(self.pm.log_msg) if self.pm.is_over: if self.pm.g == 0 or self.pm.g == 1: self.ui.txt_pm_res.setText("Factorization failed") else: self.ui.txt_pm_res.setText("Success: %d" % self.pm.g) else: self.ui.txt_pm_res.setText("") def reset_pm(self): self.ui.txt_pm_exec.setPlainText("") self.ui.txt_pm_res.setText("") self.pm.step_by_step = False ''' Prime Gen ''' def run_pg(self): if not self.pg.step_by_step: self.reset_pg() self.pg.initialize(int(self.ui.txt_pg_size.text())) self.pg.run() self.add_log_pg(self.pg.log_msg) self.ui.txt_pg_res.setText(str(self.pg.cand)) def step_pg(self): if not self.pg.step_by_step: self.reset_pg() self.pg.initialize(int(self.ui.txt_pg_size.text())) self.pg.step() self.add_log_pg(self.pg.log_msg) if self.pg.is_over: self.ui.txt_pg_res.setText(str(self.pg.cand)) else: self.ui.txt_pg_res.setText("") def reset_pg(self): self.ui.txt_pg_exec.setPlainText("") self.ui.txt_pg_res.setText("") self.pg.step_by_step = False def add_log_mr(self, log): self._add_log(log, self.ui.txt_mr_exec) def add_log_pm(self, log): self._add_log(log, self.ui.txt_pm_exec) def add_log_pg(self, log): self._add_log(log, self.ui.txt_pg_exec) def _add_log(self, log, txt_wdgt): txt_wdgt.setPlainText(log) txt_wdgt.verticalScrollBar().setSliderPosition( txt_wdgt.verticalScrollBar().maximum())