Python z3.Or() Examples
The following are 11
code examples of z3.Or().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
z3
, or try the search function
.
Example #1
Source File: solver.py From cozy with Apache License 2.0 | 5 votes |
def any(self, *conds): return self.bfold(conds, z3.Or, False, True)
Example #2
Source File: crosstalk_adaptive_schedule.py From qiskit-terra with Apache License 2.0 | 5 votes |
def scheduling_constraints(self): """ DAG scheduling constraints optimization Sets overlap indicator variables """ for gate in self.gate_start_time: for dep_gate in self.dag.successors(gate): if not dep_gate.type == 'op': continue if isinstance(dep_gate.op, Measure): continue if isinstance(dep_gate.op, Barrier): continue fin_g = self.gate_start_time[gate] + self.gate_duration[gate] self.opt.add(self.gate_start_time[dep_gate] > fin_g) for g_1 in self.xtalk_overlap_set: for g_2 in self.xtalk_overlap_set[g_1]: if len(g_2.qargs) == 2 and self.gate_id[g_1] > self.gate_id[g_2]: # Symmetry breaking: create only overlap variable for a pair # of gates continue s_1 = self.gate_start_time[g_1] f_1 = s_1 + self.gate_duration[g_1] s_2 = self.gate_start_time[g_2] f_2 = s_2 + self.gate_duration[g_2] # This constraint enforces full or zero overlap between two gates before = (f_1 < s_2) after = (f_2 < s_1) overlap1 = And(s_2 <= s_1, f_1 <= f_2) overlap2 = And(s_1 <= s_2, f_2 <= f_1) self.opt.add(Or(before, after, overlap1, overlap2)) intervals_overlap = And(s_2 <= f_1, s_1 <= f_2) self.opt.add(self.overlap_indicator[g_1][g_2] == intervals_overlap)
Example #3
Source File: depgraph.py From miasm with GNU General Public License v2.0 | 5 votes |
def _gen_path_constraints(self, translator, expr, expected): """Generate path constraint from @expr. Handle special case with generated loc_keys """ out = [] expected = canonize_to_exprloc(self._ircfg.loc_db, expected) expected_is_loc_key = expected.is_loc() for consval in possible_values(expr): value = canonize_to_exprloc(self._ircfg.loc_db, consval.value) if expected_is_loc_key and value != expected: continue if not expected_is_loc_key and value.is_loc_key(): continue conds = z3.And(*[translator.from_expr(cond.to_constraint()) for cond in consval.constraints]) if expected != value: conds = z3.And( conds, translator.from_expr( ExprAssign(value, expected)) ) out.append(conds) if out: conds = z3.Or(*out) else: # Ex: expr: lblgen1, expected: 0x1234 # -> Avoid inconsistent solution lblgen1 = 0x1234 conds = translator.from_expr(self.unsat_expr) return conds
Example #4
Source File: z3_common.py From acsploit with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_collisions(hash_func, target, target_type, length, n_collisions, hash_table_size, *args): ret = [] s = z3.Solver() # houses the z3 variables for the potential hash match res = _generate_ascii_printable_string('res', length, s) # enforces the z3 constraint that the hash matches the target # if the target_type is 'preimage', then we compare the hash to the hash of target if target_type == 'preimage': s.add(hash_func(res, hash_table_size, *args) == hash_func(_str_to_BitVecVals8(target), hash_table_size, *args)) if length == len(target): # don't generate the given preimage as an output if generating inputs of that length s.add(z3.Or([r != ord(t) for r, t in zip(res, target)])) # otherwise the target_type is 'image', and we compare the hash to the target itself else: s.add(hash_func(res, hash_table_size, *args) == target) count = 0 # z3 isn't stateful; you have to run it again and again while adding constraints to ignore previous solutions while s.check() == z3.sat: x = s.model() # This is a z3 solution y = ''.join(chr(x[i].as_long()) for i in res) ret.append(y) count += 1 # add constraints s.add(z3.Or([r != x[r] for r in res])) if count >= n_collisions: ret.sort() break return ret
Example #5
Source File: AST.py From jeeves with MIT License | 5 votes |
def __or__(l, r): return Or(l, fexpr_cast(r))
Example #6
Source File: AST.py From jeeves with MIT License | 5 votes |
def __ror__(r, l): return Or(fexpr_cast(l), r)
Example #7
Source File: AST.py From jeeves with MIT License | 5 votes |
def z3Node(self): return z3.Or(self.left.z3Node(), self.right.z3Node())
Example #8
Source File: AST.py From jeeves with MIT License | 5 votes |
def remapLabels(self, policy, writer): return Or( self.left.remapLabels(policy, writer) , self.right.remapLabels(policy, writer))
Example #9
Source File: solver.py From cozy with Apache License 2.0 | 4 votes |
def visit_EBinOp(self, e, env): # optimization: x in (distinct y) --> x in y # ("distinct" is very expensive for the solver) if e.op == BOp.In and isinstance(e.e2, EUnaryOp) and e.e2.op == UOp.Distinct: return self.visit(EIn(e.e1, e.e2.e), env) # normal path v1 = self.visit(e.e1, env) v2 = self.visit(e.e2, env) if e.op == BOp.And: return self.all(v1, v2) elif e.op == BOp.Or: return self.any(v1, v2) elif e.op == "=>": return self.implies(v1, v2) elif e.op == "==": return self.eq(e.e1.type, v1, v2) elif e.op == "!=": return self.neg(self.eq(e.e1.type, v1, v2)) elif e.op == "===": return self.eq(e.e1.type, v1, v2, deep=True) elif e.op == ">": return self.gt(e.e1.type, v1, v2, env) elif e.op == "<": return self.lt(e.e1.type, v1, v2, env) elif e.op == ">=": return v1 >= v2 elif e.op == "<=": return v1 <= v2 elif e.op == "*": return v1 * v2 elif e.op == "+": if isinstance(e.type, TBag) or isinstance(e.type, TList): return (v1[0] + v2[0], v1[1] + v2[1]) elif isinstance(e.type, TSet): return self.visit(EUnaryOp(UOp.Distinct, EBinOp(e.e1, "+", e.e2).with_type(TBag(e.type.elem_type))).with_type(TBag(e.type.elem_type)), env) elif is_numeric(e.type): return v1 + v2 else: raise NotImplementedError(e.type) elif e.op == "-": if isinstance(e.type, TBag) or isinstance(e.type, TSet) or isinstance(e.type, TList): return self.remove_all(e.type, v1, v2, env) return v1 - v2 elif e.op == BOp.In: return self.is_in(e.e1.type, v2, v1, env) else: raise NotImplementedError(e.op)
Example #10
Source File: custom_hash.py From acsploit with BSD 3-Clause "New" or "Revised" License | 4 votes |
def run(output): ast = parse_input(options['hash']) variables = {} # map from names to z3_vars z3_expression = ast.convert_to_z3(variables) solver = z3.Solver() if options['target_type'] == 'image': solver.add(options['image'] == z3_expression) elif options['target_type'] == 'preimage': # extract and validate the user-provided preimage preimage = options['preimage'] var_defs = preimage.split(',') variable_values = {} if len(var_defs) < len(variables): raise ValueError('Not enough preimage values given for all variables used in the equation') for var_def in var_defs: try: variable_name, value = var_def.split('=', 1) except ValueError: raise ValueError('Invalid syntax for preimage values') variable_name = variable_name.strip() if variable_name in variable_values: raise ValueError('Multiple preimage values given for variable "%s"' % variable_name) try: value = int(value) except ValueError: raise ValueError('Preimage value "%s" for variable "%s" is not an integer' % (value, variable_name)) variable_values[variable_name] = value for variable_name in variables: if variable_name not in variable_values: raise ValueError('Preimage value not given for variable "%s"' % variable_name) # we have a preimage but we want an image to set z3_expression equal to for solving # so we set a new variable equal to z3_expression, provide the preimage values, # and then ask Z3 to solve for our new variable target_var = z3.BitVec('__v', ast.target_width) sub_solver = z3.Solver() sub_solver.add(target_var == z3_expression) for variable in variables: sub_solver.add(variables[variable] == variable_values[variable]) assert sub_solver.check() == z3.sat # this should always hold, since the target var is unbounded solution = sub_solver.model() target_value = solution[target_var] # we can now set z3_expression equal to the appropriate image solver.add(target_value == z3_expression) # and also prevent the preimage values being generated as a solution solver.add(z3.Or([var() != solution[var] for var in solution if var.name() != '__v'])) solutions = [] while solver.check() == z3.sat and len(solutions) < options['n_collisions']: solution = solver.model() solutions.append(solution) # prevent duplicate solutions solver.add(z3.Or([var() != solution[var] for var in solution])) output.output(solutions)
Example #11
Source File: mlil_ast.py From f-ing-around-with-binaryninja with MIT License | 4 votes |
def generate_reaching_constraints(self): visitor = ConditionVisitor(self.view) for ( (start, end), reaching_condition, ) in self.reaching_conditions.items(): or_exprs = [] for condition in reaching_condition: and_exprs = [] for edge in condition: if edge.type == BranchType.UnconditionalBranch: continue if edge.type == BranchType.TrueBranch: condition = edge.source[-1].condition if ( condition.operation == MediumLevelILOperation.MLIL_VAR ): condition = self.function.get_ssa_var_definition( edge.source[-1].ssa_form.condition.src ).src and_exprs.append(visitor.simplify(condition)) elif edge.type == BranchType.FalseBranch: condition = edge.source[-1].condition if ( condition.operation == MediumLevelILOperation.MLIL_VAR ): condition = self.function.get_ssa_var_definition( edge.source[-1].ssa_form.condition.src ).src and_exprs += Tactic("ctx-solver-simplify")( Not(visitor.simplify(condition)) )[0] if and_exprs != []: or_exprs.append(And(*and_exprs)) if or_exprs: or_exprs = Tactic("ctx-solver-simplify")(Or(*or_exprs))[0] reaching_constraint = ( And(*or_exprs) if len(or_exprs) > 1 else or_exprs[0] if len(or_exprs) else BoolVal(True) ) self._reaching_constraints[(start, end)] = reaching_constraint