def key_S(i, j=None): if j is None: j = current_round return ret_str('S', i, '%03d' % (j % 120)) else: return ret_str('S', i, '%03d' % (j)) return
def get_key_as_eqs(): equations = [] def S_init(i, j): return ret_str('S', i, '%03d' % (j)) # S1 : DO I FIX BITS HERE? # 111 don't actually need to do the corr bits as they are already rep with partity bit equations. for i in range(1, 121): if i not in []: x1 = key_S(1, i) + '=' + ret_str('k', '%03d' % i) equations.append(x1) for i in range(1, 121): if i not in []: x2 = key_S(2, i) + '=' + ret_str('k', '%03d' % (i + 120)) equations.append(x2) # PARTITY EQUATIONS for i in xrange(1, 6): x3 = '' for m in xrange(1, 24): x3 += ret_str('k', '%03d' % (24 * (i - 1) + m)) + '+' x3 = ret_str('k', '%03d' % (24 * i)) + '=' + x3 equations.append(x3 + '1') x4 = '' for m in xrange(1, 24): x4 += ret_str('k', '%03d' % (24 * (i - 1) + m + 120)) + '+' x4 = ret_str('k', '%03d' % ((24 * i) + 120)) + '=' + x4 equations.append(x4 + '1') #print len(equations) return equations
def S_init(i, j): return ret_str('S', i, '%03d' % (j))
def rvar(v, n, i, r): return ret_str(v, '%02d' % n, '%03d' % i, '%04d' % r)
def pre(v, n): return ret_str(v, '%02d' % n, '%03d' % current_instance, '%04d' % (current_round - 1))
def var(v, n): return ret_str(v, '%02d' % n, '%03d' % current_instance, '%04d' % current_round)
def do_runs(runs, actions, passalong): global current_instance, current_round import sys runs_output = [] vectors = [] run_id = 0 realtime = open('realtime.txt', 'w') comment('I check realtime.txt for live updates!') pretty_print('I check realtime.txt for live updates!') for run in runs: equations = [] test_vectors = [] to_fix = [] round_vectors = [] run_id += 1 comment('=' * 16, '=' * 16, 'run %s' % str(run)) fix = run['fix'] rounds = run['rounds'] ins = run['ins'] cmd = get_command(sys.argv[0], run, actions) filename = get_filename(run, actions) x1 = get_key_as_eqs() equations += x1 comment(num_of_key_eqns=len(x1)) if fix is 0: comment("No bits fixed") else: random.seed() all_bits = range(1, 241) # WE DONT REALLY WANT ALL BITS! all_bits = [] for bb in range(1, rounds + 1): all_bits += [bb, bb + 120] comment("sampling fixes from:" + str(all_bits)) pretty_print("I sampling fixes from:" + str(all_bits)) partiy_bits = [ 24, 48, 72, 96, 120, 24 + 120, 48 + 120, 72 + 120, 96 + 120, 120 + 120 ] for p in partiy_bits: if p in all_bits: all_bits.remove(p) if int(fix) > len(all_bits): fix = len(all_bits) to_fix = random.sample(all_bits, int(fix)) to_fix = sorted(to_fix) kfixes_eq = get_k_fix_eqns(to_fix) comment('{} bits fixed'.format(len(to_fix))) test_vectors += kfixes_eq equations += kfixes_eq key_to_gen_default = 'ABCD1ABCD2ABCD3ABCD4ABCD5ABCD6ABCD7ABCD8ABCD9ABCDAABCDBABCDC' key_to_gen_default_bits = format(int(key_to_gen_default, 16), 'b') key_generated = [int(bii) for bii in key_to_gen_default_bits] for bit in to_fix: key_generated[bit - 1] = 1 key_generated = set_partity(key_generated) x88 = [] for mn in range(len(key_generated)): x88.append('{}={}'.format(ret_str('k', '%03d' % (mn + 1)), key_generated[mn])) test_vectors += x88 for instance in xrange(1, ins + 1): current_instance = instance current_round = 0 comment('Instance {}'.format(instance)) F = get_deterministic_iv() # len 61 iv = ''.join([str(i) for i in F]) x3 = get_iv_eqns(F) equations += x3 comment(iv_eqns=len(x3)) x4 = get_input_as_str('69C7C85A3') x4 = get_input_as_eq(x4) equations += x4 if rounds > 0: for r in range(1, rounds + 1): x5 = get_round_eqns(r) equations += x5 comment(no_rounds_to_gen=r) else: pass x45 = [] long_term_key_for_engine = { "alpha": 23, "D": D, "P": P, } engine = ARGON(long_term_key_for_engine, F, key_generated[:120], key_generated[120:]) engine.set_current_instance(current_instance) x456 = engine.u(rounds) x456 = x456[1:] x456 = [str(i) for i in x456] op = ''.join(x456) x4 = get_input_as_eq(op) x45 += x4 equations += x45 comment(output_equations=len(x45)) kkkey = ''.join([str(i) for i in key_generated]) test_vectors += engine.get_test_vectors() curr_vector = ('69C7C85A3', bin2hex_str9(op), bin2hex_str60(kkkey), rounds, bin2hex_str16(iv), keynum) round_vectors.append(curr_vector) comment('=' * 16, instances=instance, total_no_equations=len(equations)) with open(filename + '.txt', 'w') as f: def fcomment(*args, **kwargs): "Prints args & kwgars to stdout, uses a delimiter ( typically // )." delimiter = '//' for arg in args: f.write('{delimiter} {com}\n'.format(com=arg, delimiter=delimiter)) for key, value in kwargs.iteritems(): f.write('{delimiter} {key} = {value}\n'.format( key=key, value=value, delimiter=delimiter)) def wreq(equations): for eq in equations: f.write('{}\n'.format(eq)) # Write comments fcomment(command=cmd, run=run, actions=actions, passalong=passalong) fcomment(num_of_key_eqns=len(x1), num_of_parity_eqns=10, num_of_iv_eqns=len(x3)) fcomment(generated_instances=instance, generated_rounds=rounds) fcomment(total_number_of_equations=len(equations)) fcomment('vectors (in hex, also written to vectors.csv)') vectors += round_vectors for vector in round_vectors: fcomment('+++', ip=vector[0], op=vector[1], key=vector[2], rounds=vector[3], iv=vector[4], lzs=vector[5]) if len(vectors) == 0: fcomment('+++') # Write equations wreq(equations) comment('W {} equations written to {}.txt'.format( len(equations), filename)) pretty_print('W {} equations written to {}.txt'.format( len(equations), filename)) f.close() with open(filename + '_solution.txt', 'w') as f: def fcomment(*args, **kwargs): "Prints args & kwgars to stdout, uses a delimiter ( typically // )." delimiter = '//' for arg in args: f.write('{delimiter} {com}\n'.format(com=arg, delimiter=delimiter)) for key, value in kwargs.iteritems(): f.write('{delimiter} {key} = {value}\n'.format( key=key, value=value, delimiter=delimiter)) def wreq(eqs): for eq in eqs: f.write('{}\n'.format(eq)) fcomment(generated_instances=instance, generated_rounds=rounds) fcomment(command=cmd, run=run, actions=actions, passalong=passalong) for vector in round_vectors: fcomment('+++', ip=vector[0], op=vector[1], key=vector[2], rounds=vector[3], iv=vector[4], lzs=vector[5]) wreq(test_vectors) comment('W {} solutions written to {}_solution.txt'.format( len(test_vectors), filename)) pretty_print('W {} solutions written to {}_solution.txt'.format( len(test_vectors), filename)) f.close() #if 'printequationsprettilyplease' in passalong: # pp_eqn(equations) mode_ = None if platform == "linux" or platform == "linux2": comm_ = comm[0] elif platform == "darwin": comm_ = comm[0] elif platform == "win32": comm_ = comm[1] if actions['sat'] and actions['xl']: mode_ = '4444 /deg4' elif actions['xl']: mode_ = '4000 /deg4' if mode_ is not None and comm is not None: proc = ' '.join([comm_, mode_, filename + '.txt']) #+passalong) try: comment('C {}'.format(proc)) pretty_print('C {}'.format(proc)) time_it_took = timed_process(proc) comment('C ax64 returned') run_str = 'rounds={},ins={},fix={}'.format(rounds, ins, fix) run_str_2 = '{}, {}\n'.format(run_id, time_it_took) runs_output.append((run_id, run_str, time_it_took)) realtime.write(run_str_2) except Exception as e: raise e realtime.close() return runs_output, vectors
def get_iv_eqns(IV): return [ ret_str('F', '%04d' % (i), '%03d' % current_instance) + '=' + str(IV[i]) for i in range(len(IV)) ]
def var_F(n): j = n return ret_str('F', '%04d' % (j), '%03d' % current_instance)
def get_k_fix_eqns(to_fix): equations = [] for fix in to_fix: x = ret_str('k', '%03d' % (fix)) + '=1' equations.append(x) return equations