def run(self): if len(sys.argv) == 1: print('======= Z_n STRUCTURE EMULATOR =======\n') while True: n = self.input_int('Let n = ', 'Sorry! Please input an integer n >= 2...', ZN_COND_N) self.zn = Zn(n) a = self.input_int('Please input a number "a" to calculate: ', 'Sorry! Please input a valid integer...') self.a = self.zn.consider(a) b = self.input_int('And a second number "b": ', 'Sorry! Please input a valid integer...') self.b = self.zn.consider(b) if not self.exec(): return else: parser = ap.ArgumentParser(description = 'Z_n STRUCTURE EMULATOR', epilog = 'Demonstrating calculations e.g. +, -, *, finding multiplicative inverse, and fast modular exponentiation!\n') parser.add_argument('n', action = 'store', type = int, help = 'Base n for Z_n structure') parser.add_argument('a', action = 'store', type = int, help = 'A number to calculate') parser.add_argument('b', action = 'store', type = int, help = 'And a second number') try: args = parser.parse_args() except: parser.print_help() return if not ZN_COND_N(args.n): print('ERROR: n must be >= 2! Exitting...') else: self.zn = Zn(args.n) self.a, self.b = self.zn.consider(args.a), self.zn.consider(args.b) self.exec(False)
def process(): fin = open(FIN, 'r') fout = open(FOUT, 'w') bits = [int(bit) for bit in next(fin).split(' ') if bit != '\n'] n_samples = int(next(fin)) ns = [int(n) for n in next(fin).split(' ') if n != '\n'] total = n_samples * len(bits) * len(ns) ints = [] for line in fin: a, b = [int(num) for num in line.split(' ')] ints.append((a, b)) running_times = [] with alive_bar(total) as bar: for n in ns: print('----- In Z_{} -----'.format(n)) zn = Zn(n) mib = [] mie = [] me = [] for a, b in ints: res, res_time = process_sample(zn.consider(a), zn.consider(b)) res += res_time for r in res: fout.write('%s ' % r) fout.write('\n') mib.append(res_time[0]) mie.append(res_time[1]) me.append(res_time[2]) bar() sums = [0, 0, 0] for i in range(len(mib)): sums[0] += mib[i] sums[1] += mie[i] sums[2] += me[i] if i % n_samples == n_samples - 1: avgs = [s / n_samples for s in sums] for avg in avgs: fout.write('%s ' % avg) fout.write('\n') sums = [0, 0, 0] fout.write('------------------------\n') fin.close() fout.close()