def xor_expr(self, expr): l = expr.left.convert(self) r = expr.right.convert(self) (hit, result) = self.checkCache("Xor", [l,r]) if hit: return result else: return self.cache(Xor(l,r), result) return Xor(l,r)
def __init__(self, resource, list_of_time_intervals, optional: Optional[bool] = False) -> None: """ :param resource: the resource :param list_of_intervals: periods for which the resource is unavailable for any task. for example [(0, 2), (5, 8)] """ super().__init__(optional) # for each interval we create a task 'UnavailableResource%i' if isinstance(resource, Worker): workers = [resource] elif isinstance(resource, CumulativeWorker): workers = resource.cumulative_workers for interval_lower_bound, interval_upper_bound in list_of_time_intervals: # add constraints on each busy interval for worker in workers: for start_task_i, end_task_i in worker.get_busy_intervals(): self.set_assertions( Xor( start_task_i >= interval_upper_bound, end_task_i <= interval_lower_bound, ))
def buildexpr(table): ret = True for row in table: expr = False assert len(row) == len(varset) for e, v in zip(row, varset): if e: expr = Xor(expr, v) ret = And(ret, expr) return ret
def __init__(self, task_1, task_2, optional: Optional[bool] = False) -> None: super().__init__(optional) scheduled_assertion = Xor( task_2.start >= task_1.end, task_1.start >= task_2.end ) if task_1.optional or task_2.optional: # if one task is not scheduledboth tasks must be scheduled so that the not overlap constraint applies self.set_z3_assertions( Implies(And(task_1.scheduled, task_2.scheduled), scheduled_assertion) ) else: self.set_z3_assertions(scheduled_assertion)
def all_persons_have_different_roles(): """ Encode ∀x, y : Xor(x = y, R(x) ≠ R(y)) Either the two Persons x,y are the same or they have different roles. This is an alternative to `abc_different`. Returns ------- Formula """ # We need to declare the variables fist. x = Const('x', Person) # This x defined on the domain of Person y = Const('y', Person) # And this is y return ForAll([x, y],Xor(x == y, R(x) != R(y))) # ForAll expression
def xor_(list_of_constraints: List[Union[BoolRef, Constraint]]) -> BoolRef: """Boolean 'xor' between two assertions or constraints. One assertion must be satisfied, the other is not satisfied. The list of constraint must have exactly 2 elements. """ if len(list_of_constraints) != 2: raise TypeError( "You list size must be 2. Be sure you have 2 constraints in the list." ) constraint_1 = list_of_constraints[0] constraint_2 = list_of_constraints[1] return Xor(And(_get_assertions(constraint_1)), And(_get_assertions(constraint_2)))
def accumulate(a, c): return Xor(a, Or(*[c == s for s in crossings]))
def find_n_root_domains_ignoring_wildcards(solver: z3.Solver, symbols: Dict[int, List[str]], require_literal_dot_in_domain: bool, levels: int, max_finds: int): """If enough root domains are found, report vulnerability. :param levels: number of levels of domains to consider as part of the root domain. Either 2 or 3. If 3, the TLD needs to be a Country Code. :param max_finds: threshold number of root domains to find before considering as vulnerability. :return: tuple of sat result, witness or debug info """ from z3 import Not, And, Or, Xor, Contains, String, StringVal, Length, Implies, SuffixOf, Concat tld = String('tld') sld = String('sld') third_ld = String('3ld') DNS_root = String('DNS_root') dot_in_domain = Contains(root_domain, '.') domain_expr = z3.simplify(And( Or(DNS_root == StringVal('.'), DNS_root == StringVal('')), Xor(Concat(root_domain, DNS_root) == fqdn, And(SuffixOf(Concat(StringVal('.'), root_domain, DNS_root), fqdn), dot_in_domain)), Implies(dot_in_domain, And(Not(Contains(tld, '.')), Not(Contains(sld, '.')), Length(tld) > 0, root_domain == (Concat(sld, StringVal('.'), tld) if levels < 3 else Concat(third_ld, StringVal('.'), sld, StringVal('.'), tld)))), Implies(Not(dot_in_domain), And(Concat(root_domain, DNS_root) == fqdn, tld == StringVal(''), sld == StringVal(''),)), )) with solver_frame(solver): solver.add(domain_expr) solver.add(RegexStringExpr.ignore_wildcards) if require_literal_dot_in_domain or levels >= 3: solver.add(dot_in_domain) if levels >= 3: solver.add(z3.simplify(And( Not(Contains(third_ld, '.')), Length(sld) > 0, Length(third_ld) > 0, Or(*(tld == StringVal(ss) for ss in cc_tlds)), ))) results = [] found = set() found_root_domains = set() result = z3.sat logger.info('searching for n root_domains') _concs = lambda zs: concretizations(z3_str_to_bytes(zs), symbols) solution = None while len(found_root_domains) < max_finds and result == z3.sat: result = solver.check() results.append(result) if result == z3.sat: model = solver.model() _root_domain = model[root_domain] parts = (model[proto], model[proto_delimiter], model[fqdn]) assert all(part is not None for part in parts) logger.info(public_vars(model)) found.update(_concs(z3.simplify(z3.Concat(*parts)))) found_root_domains.update(_concs(_root_domain)) solver.add(root_domain != _root_domain) solution = tz.first(_concs(model[unknown_string])) # if not sat, return the would-be witness for debugging if result == z3.sat: root_domains_label = 'witness' else: root_domains_label = 'root_domains' return result, {'strategy': 'find_n_root_domains_ignoring_wildcards', 'found': list(found), root_domains_label: list(found_root_domains), 'levels': levels, 'solution': solution}
def add_conds(rate, s, cols, guess): cond = Xor(False, False) if rate["red"] != 0 and rate["white"] != 0: for combi in itertools.combinations(cols, rate["red"]): mid_cond = And() not_combi = [col for col in cols if col not in combi] red_cond = And() for part in combi: x = cols.index(part) red_cond = And(red_cond, (part == guess[x])) for part in not_combi: x = cols.index(part) red_cond = And(red_cond, (part != guess[x])) mid_cond = And(mid_cond, red_cond) white_cond = Xor(False, False) for combi2 in itertools.combinations(not_combi, rate["white"]): not_combi2 = [ col for col in cols if (col not in combi and col not in combi2) ] tmp_white_cond = And() for part in combi2: x = cols.index(part) tmp_white_cond = And(tmp_white_cond, (part != guess[x])) tmp_white_cond_tmp = Xor(False, False) for col in cols: if col is not part and col not in combi: tmp_white_cond_tmp = Xor(tmp_white_cond_tmp, (col == guess[x])) tmp_white_cond = And(tmp_white_cond, tmp_white_cond_tmp) for col in not_combi2: x = cols.index(col) for col2 in cols: tmp_white_cond = And(tmp_white_cond, (col2 != guess[x])) white_cond = Xor(white_cond, tmp_white_cond) mid_cond = And(mid_cond, white_cond) cond = Xor(cond, mid_cond) elif rate["red"] != 0: for combi in itertools.combinations(cols, rate["red"]): mid_cond = And() not_combi = [col for col in cols if col not in combi] red_cond = And() for part in combi: x = cols.index(part) red_cond = And(red_cond, (part == guess[x])) for part in not_combi: x = cols.index(part) for col2 in cols: red_cond = And(red_cond, (col2 != guess[x])) mid_cond = And(mid_cond, red_cond) cond = Xor(cond, mid_cond) elif rate["white"] != 0: mid_cond = And() white_cond = Xor(False, False) for combi2 in itertools.combinations(cols, rate["white"]): not_combi2 = [col for col in cols if (col not in combi2)] tmp_white_cond = And() for part in combi2: x = cols.index(part) tmp_white_cond = And(tmp_white_cond, (part != guess[x])) tmp_white_cond_tmp = Xor(False, False) for col in cols: if col is not part: tmp_white_cond_tmp = Xor(tmp_white_cond_tmp, (col == guess[x])) tmp_white_cond = And(tmp_white_cond, tmp_white_cond_tmp) for col in not_combi2: x = cols.index(col) for col2 in cols: tmp_white_cond = And(tmp_white_cond, (col2 != guess[x])) white_cond = Xor(white_cond, tmp_white_cond) mid_cond = And(mid_cond, white_cond) cond = Xor(cond, mid_cond) else: cond = And() for x, col in enumerate(cols): for col2 in cols: cond = And(cond, (col2 != guess[x])) s.add(cond)