1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
| from tqdm import tqdm from z3 import Solver, Bool, BoolVal, And, Or, sat, is_true, unsat
SPECIAL_CHARS = "(),/-"
cur_pc = None
def format_z3(pc, tag): return Bool(f"{pc}_{tag}")
class Token: def __init__(self, value): self.value = value
def __eq__(self, other): return isinstance(other, Token) and self.value == other.value
def __repr__(self): return f"Token({self.value!r})"
class Lexer: def __init__(self, text: str): self.text = text self.pos = 0
def __iter__(self): return self
def char(self): if self.pos >= len(self.text): raise StopIteration return self.text[self.pos]
def __next__(self): while self.char().isspace(): self.pos += 1
ch = self.char() if ch in SPECIAL_CHARS: self.pos += 1 return Token(ch)
start = self.pos while not (ch.isspace() or ch in SPECIAL_CHARS): self.pos += 1 if self.pos >= len(self.text): break ch = self.char()
return self.text[start : self.pos]
class Query: def __and__(self, other): return Group("and", [self, other])
def __or__(self, other): return Group("or", [self, other])
def __invert__(self): if isinstance(self, Neg): return self.query return Neg(self)
def unwrap(self, type): return [self]
def simplify(self): pass
class Tag(Query): def __init__(self, tag: str): self.tag = tag
def __str__(self): return self.tag
def __eq__(self, other): return isinstance(other, Tag) and self.tag == other.tag
def __hash__(self): return hash(self.tag)
def simplify(self): return self
def tags(self): yield self.tag
def to_z3(self): assert cur_pc is not None if self.tag in pc_order: return BoolVal(True) if self.tag.startswith("flag_bin_") or self.tag == "check_flag": return Bool(self.tag)
for i in reversed(tag_pcs.get(self.tag, [])): if i < cur_pc: return format_z3(i, self.tag)
return BoolVal(False)
class Neg(Query): def __init__(self, query: Query): self.query = query
def __str__(self): return f"-{self.query}"
def __eq__(self, other): return isinstance(other, Neg) and self.query == other.query
def __hash__(self): return hash(self.query)
def simplify(self): if isinstance(self.query, Neg): return self.query.query.simplify() return ~self.query.simplify()
def tags(self): return self.query.tags()
def to_z3(self): return ~self.query.to_z3()
class Group(Query): def __init__(self, type: str, queries: list[Query]): self.type = type self.queries = queries
def __str__(self): assert self.queries sep = ", " if self.type == "and" else " / " return f"({sep.join(map(str, self.queries))})"
def unwrap(self, type): if self.type == type: result = [] for query in self.queries: result.extend(query.unwrap(type)) return result return [self]
def __eq__(self, other): return ( isinstance(other, Group) and self.type == other.type and self.queries == other.queries )
def __hash__(self): return hash((self.type, tuple(self.queries)))
def simplify(self): negs = set() queries = [] for query in self.queries: for item in query.simplify().unwrap(self.type): if isinstance(item, Group) and not item.queries: assert item.type != self.type return item if item in negs: return Group("and" if self.type == "or" else "or", []) negs.add(~item)
queries.append(item)
if len(queries) == 1: return queries[0]
return Group(self.type, queries)
def tags(self): for query in self.queries: yield from query.tags()
def to_z3(self): queries = [query.to_z3() for query in self.queries] return And(queries) if self.type == "and" else Or(queries)
def take_atom(lexer): token = next(lexer) if token == Token("("): return take_expr(lexer) elif token == Token("-"): return ~take_atom(lexer) elif isinstance(token, str): return Tag(token) else: raise ValueError(f"Unexpected {token}")
def take_expr(lexer): stack = [take_atom(lexer)] while True: try: token = next(lexer) except StopIteration: break
if token == Token("/"): value = take_atom(lexer) stack[-1] = stack[-1] | value elif token == Token(","): stack.append(take_atom(lexer)) elif token == Token(")"): break else: raise ValueError(f"Unexpected {token}")
return Group("and", stack)
def parse_query(query: str): lexer = Lexer(query) return take_expr(lexer)
class Implication: def __init__(self, condition, consequence: list[str]): self.condition = condition self.consequence = consequence
def __str__(self): cond = str(self.condition) if cond.startswith("("): cond = cond[1:-1] cons = ", ".join(map(str, self.consequence)) return f"{cond} -> {cons}"
def parse_implication(implication: str) -> Implication: lhs, rhs = implication.split("->") return Implication(parse_query(lhs), parse_query(rhs).unwrap("and"))
with open("implications_new.txt") as f: imps = [] for i, line in enumerate(f): line = line.strip() if not line: continue
imps.append(parse_implication(line))
for imp in tqdm(imps): imp.condition = imp.condition.simplify()
who_implies = {} who_implies_neg = {} for i, imp in enumerate(imps): for tag in imp.consequence: if isinstance(tag, Tag): who_implies.setdefault(tag.tag, []).append(i) elif isinstance(tag, Neg): who_implies_neg.setdefault(tag.query.tag, []).append(i)
pcs = ["check_flag"] while True: pc = pcs[-1] if pc not in who_implies_neg: break assert len(who_implies_neg[pc]) == 1 imp = imps[who_implies_neg[pc][0]] assert len(imp.consequence) == 2 other = ( imp.consequence[0] if isinstance(imp.consequence[1], Neg) else imp.consequence[0] ) assert isinstance(other, Tag) pcs.append(other.tag)
print(len(pcs))
pc_order = {pc: i for i, pc in enumerate(pcs)}
important_imps = [] tag_pcs = {} for imp in tqdm(imps): if isinstance(imp.condition, Tag) and imp.condition.tag in pcs: continue assert len(imp.consequence) == 1 tag = imp.consequence[0] if isinstance(tag, Neg): continue
tag = tag.tag if tag == "hooray": continue
pc = None for t in imp.condition.tags(): if t in pc_order: assert pc is None pc = t
assert pc pc = pc_order[pc] imp.pc = pc important_imps.append(imp) tag_pcs.setdefault(tag, []).append(pc)
for pcs in tag_pcs.values(): pcs.sort()
defs = {}
solver = Solver() for imp in tqdm(important_imps): tag = imp.consequence[0].tag pc = imp.pc
cur_pc = pc
key = (pc, tag) val = defs.setdefault(key, BoolVal(False)) defs[key] = val | imp.condition.to_z3()
for (pc, tag), val in defs.items(): solver.add(format_z3(pc, tag) == val)
pcs = tag_pcs["flag_correct"] assert len(pcs) == 1 solver.add(format_z3(pcs[0], "flag_correct"))
assert solver.check() == sat model = solver.model()
ors = []
bits = [] flags = set() for i in range(256): fl = f"flag_bin_{i:02x}" bits.append("01"[int(is_true(model[Bool(fl)]))]) ors.append(Bool(fl) != is_true(model[Bool(fl)])) if is_true(model[Bool(fl)]): flags.add(fl)
solver.add(Or(*ors)) assert solver.check() == unsat
print(flags)
chs = [] for i in range(32): bs = bits[i * 8 : (i + 1) * 8] chs.append(chr(int("".join(reversed(bs)), 2)))
print("".join(chs))
|