def findAllCharacteristics(cipher, parameters): """ Outputs all characteristics of a specific weight by excluding solutions iteratively. """ rnd_string_tmp = '%030x' % random.randrange(16**30) start_time = time.time() total_num_characteristics = 0 while not reachedTimelimit(start_time, parameters["timelimit"]) and \ parameters["sweight"] != parameters["endweight"]: stp_file = "tmp/{}{}.stp".format(cipher.name, rnd_string_tmp) # Start STP TODO: add boolector support cipher.createSTP(stp_file, parameters) result = "" if parameters["boolector"]: result = solveBoolector(stp_file) else: result = solveSTP(stp_file) # Check for solution if foundSolution(result): print(("Characteristic for {} - Rounds {} - Wordsize {}- " "Weight {}".format(cipher.name, parameters["rounds"], parameters["wordsize"], parameters["sweight"]))) characteristic = "" if parameters["boolector"]: characteristic = parsesolveroutput.getCharBoolectorOutput( result, cipher, parameters["rounds"]) else: characteristic = parsesolveroutput.getCharSTPOutput( result, cipher, parameters["rounds"]) characteristic.printText() parameters["blockedCharacteristics"].append(characteristic) else: print("Found {} characteristics with weight {}".format( total_num_characteristics, parameters["sweight"])) parameters["sweight"] += 1 total_num_characteristics = 0 continue total_num_characteristics += 1 if parameters["dot"]: with open(parameters["dot"], "w") as dot_file: dot_file.write("strict digraph graphname {") #dot_file.write("graph [ splines = false ]") dot_graph = "" for characteristic in parameters["blockedCharacteristics"]: dot_graph += characteristic.getDOTString() dot_file.write(dot_graph) dot_file.write("}") print("Wrote .dot to {}".format(parameters["dot"])) return
def findMinWeightCharacteristic(cipher, parameters): """ Find a characteristic of minimal weight for the cipher parameters = [rounds, wordsize, sweight, isIterative, fixedVariables] """ print(("Starting search for characteristic with minimal weight\n" "{} - Rounds: {} Wordsize: {}".format(cipher.name, parameters["rounds"], parameters["wordsize"]))) print("---") start_time = time.time() while not reachedTimelimit(start_time, parameters["timelimit"]) and \ parameters["sweight"] < MAX_WEIGHT: print("Weight: {} Time: {}s".format(parameters["sweight"], round(time.time() - start_time, 2))) # Construct problem instance for given parameters stp_file = "tmp/{}{}.stp".format(cipher.name, parameters["wordsize"]) cipher.createSTP(stp_file, parameters) result = "" if parameters["boolector"]: result = solveBoolector(stp_file) else: result = solveSTP(stp_file) # Check if a characteristic was found if foundSolution(result): current_time = round(time.time() - start_time, 2) print("---") print(("Characteristic for {} - Rounds {} - Wordsize {} - " "Weight {} - Time {}s".format(cipher.name, parameters["rounds"], parameters["wordsize"], parameters["sweight"], current_time))) characteristic = "" if parameters["boolector"]: characteristic = parsesolveroutput.getCharBoolectorOutput( result, cipher, parameters["rounds"]) else: characteristic = parsesolveroutput.getCharSTPOutput( result, cipher, parameters["rounds"]) characteristic.printText() if parameters["dot"]: with open(parameters["dot"], "w") as dot_file: dot_file.write("digraph graphname {") dot_file.write(characteristic.getDOTString()) dot_file.write("}") print("Wrote .dot to {}".format(parameters["dot"])) if parameters["latex"]: with open(parameters["latex"], "w") as tex_file: tex_file.write(characteristic.getTexString()) print("Wrote .tex to {}".format(parameters["latex"])) break parameters["sweight"] += 1 return parameters["sweight"]