#!/usr/bin/env python3 """Job 1302: reference-semantics state trace of the published R checker. Replays return #572's own R proof with return #572's own primitives (records.root_state, records.close, records.valid_pair, records.apply_left/right, records.find_big_clique), in the same DFS order as records.check, and emits for every node index a canonical hash of the state the checker recomputed for that node. This is the audit oracle for packed_check.py: the audit needs the reference semantics, so it may call them; the timing claim is made only about packed_check.py. The replication is gated: its outcome dict must equal records.check's outcome dict on the same proof (accepted, kinds, verified_discharges, frontier_unverified, problems). Usage: python3 ref_trace.py """ from __future__ import annotations import hashlib import json import sys import time import records W = 32 M64 = (1 << 64) - 1 FNV_OFF = 0xCBF29CE484222325 FNV_PRM = 0x100000001B3 def fnv(parts): h = FNV_OFF for x in parts: h ^= x & M64 h = (h * FNV_PRM) & M64 return h def pack(d, qindex, nb): """Canonical packed form of one node's state: prime-index fields of 32 bits per block, then rows.""" parts = [] for i in range(nb): if i not in d: parts.append(0) parts.append(0) continue acc = 0 for q, m in d[i].items(): acc |= m << (qindex[q] * W) parts.append(acc) return parts def state_hash(d, e, qindex, nb): parts = pack(d, qindex, nb) for i in range(nb): parts.append(e.get(i, 0)) return fnv(parts) def main(): proof = json.loads(open(sys.argv[1], encoding="utf-8").read()) out_path = sys.argv[2] nodes = proof["nodes"] slots, primes = proof["slots"], proof["Q"] nb = len(slots) qindex = {q: j for j, q in enumerate(primes)} d0, e0 = records.root_state(slots, primes) t0 = time.process_time() problems = [] kinds = {} seen = set() stack = [(proof["root"], d0, e0)] lines = [] while stack: ix, d, e = stack.pop() if ix in seen: problems.append(f"node {ix} reachable twice"); continue seen.add(ix) row = nodes[ix] if 0 <= ix < len(nodes) else None if row is None: problems.append(f"referenced row {ix} missing"); continue kind = row[0] kinds[kind] = kinds.get(kind, 0) + 1 if kind == "x": lines.append((ix, kind, 0)) continue d2, e2, empty = records.close(d, e) h = state_hash(d2, e2, qindex, nb) lines.append((ix, kind, h)) if kind == "e": if empty is None: problems.append(f"node {ix}: no empty domain in the recomputed closure") elif kind == "k": if empty is not None: problems.append(f"node {ix}: claims cardinality leaf but the node is empty") elif records.find_big_clique(sorted(d2), e2, primes) is None: problems.append(f"node {ix}: no separated clique > |Q| in the recomputed state") elif kind == "h": if empty is None and records.find_hall_violator(sorted(d2), d2) is None: problems.append(f"node {ix}: no Hall deficiency in the recomputed state") elif kind == "b": a, b = row[2], row[3] okp, err = records.valid_pair(a, b, d2, e2) if not okp: problems.append(f"node {ix}: {err}") continue common = {q: d2[a][q] & d2[b].get(q, 0) for q in d2[a] if d2[a][q] & d2[b].get(q, 0)} ld, le = records.apply_left(d2, e2, a, b, common) rd, re = records.apply_right(d2, e2, a, b) stack.append((row[4], ld, le)) stack.append((row[5], rd, re)) else: problems.append(f"node {ix}: unknown verdict {kind!r}") if len(seen) != len(nodes): problems.append(f"{len(nodes) - len(seen)} row(s) unreachable from the root") cpu = time.process_time() - t0 outcome = {"accepted": not problems, "problems": problems[:12], "node_count": len(nodes), "kinds": kinds, "verified_discharges": sum(v for k, v in kinds.items() if k in ("e", "k", "h")), "frontier_unverified": kinds.get("x", 0)} # gate: the replicated traversal must reproduce the published checker's verdict exactly ref = records.check(proof, "R") ref_cmp = {k: ref[k] for k in outcome if k in ref} ok = ref_cmp == outcome with open(out_path, "w", encoding="utf-8", newline="\n") as fh: fh.write(json.dumps({"tool": "job1302-ref-trace/1.0", "proof": sys.argv[1], "trace_cpu_seconds": round(cpu, 3), "outcome": outcome, "matches_published_checker": ok, "published_checker_outcome": ref_cmp}) + "\n") for ix, kind, h in sorted(lines): fh.write(json.dumps({"node": ix, "kind": kind, "state_hash": h}, sort_keys=True) + "\n") print(json.dumps({"trace_cpu_seconds": round(cpu, 3), "matches_published_checker": ok, "accepted": outcome["accepted"], "kinds": kinds, "rows": len(lines)}, sort_keys=True)) if not ok: print("GATE FAILED: replication differs from records.check") return 1 return 0 if __name__ == "__main__": raise SystemExit(main())