def buildmodel(config): """ Constructs the model for the Gurobi Solver """ model = Model("aeslike") # Parameters state_dim = config["statedimension"] num_rounds = config["rounds"] branch_number = config["branchnumber"] # Initialize all variables var_x = [] # state var_d = [] # dummy variable for MixColumns state_words = state_dim * state_dim for byte in range((num_rounds + 1) * state_words): var_x.append(model.addVar(vtype=GRB.BINARY, name="x[{}]".format(byte))) for col in range(num_rounds * state_dim): var_d.append(model.addVar(name="dummy[{}]".format(col))) activesboxes = model.addVar(name="Active S-boxes") model.update() # Constraints # Optimize number of active S-boxes model.setObjective(activesboxes, GRB.MINIMIZE) # Count Active S-boxes model.addConstr( quicksum(var_x[i] for i in range(num_rounds * state_words)) - activesboxes == 0, "Count Active S-boxes") # Add constraints from AES round function model = addAESrndconstraints(model, state_dim, var_x, var_d, branch_number, num_rounds) # No Zero Characteristic model.addConstr( quicksum(var_x[i] for i in range((num_rounds + 1) * state_words)) >= 1, "Avoid trivial solutions") return model
def buildmodel(config): """ Constructs the model for the Gurobi Solver """ model = Model("aeslike") # Parameters state_dim = config["statedimension"] num_rounds = config["rounds"] branch_number = config["branchnumber"] # Initialize all variables var_x = [] # state var_d = [] # dummy variable for MixColumns state_words = state_dim * state_dim for byte in range((num_rounds + 1) * state_words): var_x.append(model.addVar(vtype=GRB.BINARY, name="x[{}]".format(byte))) for col in range(num_rounds * state_dim): var_d.append(model.addVar(name="dummy[{}]".format(col))) activesboxes = model.addVar(name="Active S-boxes") model.update() # Constraints # Optimize number of active S-boxes model.setObjective(activesboxes, GRB.MINIMIZE) # Count Active S-boxes model.addConstr(quicksum(var_x[i] for i in range(num_rounds * state_words)) - activesboxes == 0, "Count Active S-boxes") # Add constraints from AES round function model = addAESrndconstraints(model, state_dim, var_x, var_d, branch_number, num_rounds) # No Zero Characteristic model.addConstr(quicksum(var_x[i] for i in range((num_rounds + 1) * state_words)) >= 1, "Avoid trivial solutions") return model
def buildmodel(config): """ Constructs the model for the Gurobi Solver """ model = Model("haraka") # Parameters rounds = config["rounds"] state_dim = config["statedimension"] branch_number = config["branchnumber"] aes_rounds = config["aesrounds"] aes_states = config["aesstates"] num_states = ((aes_rounds + 1) * rounds) + 1 words_state = state_dim * state_dim # Initialize all variables var_x = [[] for _ in range(aes_states)] var_d = [[] for _ in range(aes_states)] var_mccosts = [[] for _ in range(aes_states)] var_mcactive = [[] for _ in range(aes_states)] for aes_state in range(aes_states): for word in range(num_states * words_state): var_x[aes_state].append( model.addVar(vtype=GRB.BINARY, name="x[{}][{}]".format(aes_state, word)) ) for col in range(num_states * state_dim): var_d[aes_state].append( model.addVar(name="dummy[{}][{}]".format(aes_state, col)) ) var_mccosts[aes_state].append( model.addVar(name="MCCosts[{}][{}]".format(aes_state, col)) ) var_mcactive[aes_state].append( model.addVar(vtype=GRB.BINARY, name="MCActive[{}][{}]".format(aes_state, col)) ) activesboxes = model.addVar(name="Active S-boxes") costs = model.addVar() model.update() # Objective to minimize attack costs model.setObjective(costs, GRB.MINIMIZE) if config["securitymodel"] == "sbox": print("Finding minimum number of active S-boxes...") # Count number of active S-boxes model = addactivesboxconstraints(model, config, var_x, activesboxes) model.setObjective(activesboxes, GRB.MINIMIZE) elif config["securitymodel"] == "truncated": model = addtruncatedconstraints(model, config, var_x, var_mccosts, var_mcactive, costs) if config["collisiononly"]: if aes_states == 4: # If we have 4 states truncated to 256-bit model = addcolltruncoutput(model, config, var_x) else: model = addcollisionconstraints(model, config, var_x) for rnd in range(rounds): # Add AES round constraints for aes_state in range(aes_states): model = addAESrndconstraints(model, state_dim, var_x[aes_state][words_state * (aes_rounds + 1) * rnd:], var_d[aes_state][state_dim * (aes_rounds + 1) * rnd:], branch_number, aes_rounds) # Add MIX round constraints if config["mixlayer"] == "mix" and aes_states == 4: model = addmixconstraints512(model, config, var_x, rnd) elif config["mixlayer"] == "mix" and aes_states == 2: model = addmixconstraints256(model, config, var_x, rnd) # No all Zero model.addConstr(quicksum(var_x[aes_state][i] for aes_state in range(aes_states) for i in range((aes_rounds * rounds + 1) * state_dim * state_dim)) >= 1, "notrivialsolution") return model
def buildmodel(config): """ Constructs the model for the Gurobi Solver """ model = Model("haraka") # Parameters rounds = config["rounds"] state_dim = config["statedimension"] branch_number = config["branchnumber"] aes_rounds = config["aesrounds"] aes_states = config["aesstates"] num_states = ((aes_rounds + 1) * rounds) + 1 words_state = state_dim * state_dim # Initialize all variables var_x = [[] for _ in range(aes_states)] var_d = [[] for _ in range(aes_states)] var_mccosts = [[] for _ in range(aes_states)] var_mcactive = [[] for _ in range(aes_states)] for aes_state in range(aes_states): for word in range(num_states * words_state): var_x[aes_state].append( model.addVar(vtype=GRB.BINARY, name="x[{}][{}]".format(aes_state, word))) for col in range(num_states * state_dim): var_d[aes_state].append( model.addVar(name="dummy[{}][{}]".format(aes_state, col))) var_mccosts[aes_state].append( model.addVar(name="MCCosts[{}][{}]".format(aes_state, col))) var_mcactive[aes_state].append( model.addVar(vtype=GRB.BINARY, name="MCActive[{}][{}]".format(aes_state, col))) activesboxes = model.addVar(name="Active S-boxes") costs = model.addVar() model.update() # Objective to minimize attack costs model.setObjective(costs, GRB.MINIMIZE) if config["securitymodel"] == "sbox": print("Finding minimum number of active S-boxes...") # Count number of active S-boxes model = addactivesboxconstraints(model, config, var_x, activesboxes) model.setObjective(activesboxes, GRB.MINIMIZE) elif config["securitymodel"] == "truncated": model = addtruncatedconstraints(model, config, var_x, var_mccosts, var_mcactive, costs) if config["collisiononly"]: if aes_states == 4: # If we have 4 states truncated to 256-bit model = addcolltruncoutput(model, config, var_x) else: model = addcollisionconstraints(model, config, var_x) for rnd in range(rounds): # Add AES round constraints for aes_state in range(aes_states): model = addAESrndconstraints( model, state_dim, var_x[aes_state][words_state * (aes_rounds + 1) * rnd:], var_d[aes_state][state_dim * (aes_rounds + 1) * rnd:], branch_number, aes_rounds) # Add MIX round constraints if config["mixlayer"] == "mix" and aes_states == 4: model = addmixconstraints512(model, config, var_x, rnd) elif config["mixlayer"] == "mix" and aes_states == 2: model = addmixconstraints256(model, config, var_x, rnd) # No all Zero model.addConstr( quicksum(var_x[aes_state][i] for aes_state in range(aes_states) for i in range((aes_rounds * rounds + 1) * state_dim * state_dim)) >= 1, "notrivialsolution") return model