#!/usr/bin/env python3 """Job 3307 (route 157): fail-closed exact replay of #1606's public d = 21 witness. Inputs (repository-relative, fetched from /files/ and hash-checked here): src/even-engine.py pinned engine (#1599/#1606), sha 0ad32e25... src/indexed-witness.json #1635's indexed witness, sha 27a66bdf... src/out-cert_k46_eps25_861_d27.json #1606's stored certificate, sha 04418bd2... Path A (this file, `--path A`): the pinned engine's own entries M1(i,j), M2(i,j), summed over the upper triangle of the nonzero support; I = c^T M1 c and J = c^T M2 c are kept separately, Q = J - tau*I. No Ritz call, no eigensolve. Path B (`--path B`): an independent grouped evaluator (own derivation, see grouped3307.py) of the same I and J. Any manifest mismatch exits 2 before evaluation. stdout is one JSON line. """ import hashlib, importlib.util, json, os, sys, time from fractions import Fraction as Fr HERE = os.path.dirname(os.path.abspath(__file__)) SRC = os.path.join(HERE, "src") PIN = { "even-engine.py": "0ad32e25276d2ae403be353569ebad10cb54df13c64675961caa6e23e58144e1", "indexed-witness.json": "27a66bdfc2b3cf50e53eb922f6fbe402aebac6fd77d89b28bd641324281b8d20", "out-cert_k46_eps25_861_d27.json": "04418bd2e76c0c32dc71cb09f4deaaee2efacf250b50060644d475cbfe48aac1", } VEC_SHA = "a7a6942669aade3493eb25c172ea4c4555861ff025ab80be776612f9ca4b0cc0" K, EPS, D, N, TAU = 46, Fr(25, 861), 21, 846, Fr(10000, 2583) def die(msg): print(json.dumps({"ok": False, "refused": msg})) sys.exit(2) def sha(p): return hashlib.sha256(open(p, "rb").read()).hexdigest() def ensure_src(): """Fetch any missing pinned input from the server-root /files/ (public, no credential).""" import urllib.request base = os.environ.get("SAH_BASE", "https://solveathome.org") os.makedirs(SRC, exist_ok=True) for f, h in PIN.items(): p = os.path.join(SRC, f) if not os.path.exists(p): req = urllib.request.Request(f"{base}/files/{h}", headers={"User-Agent": "solveathome-job3307-replay"}) data = urllib.request.urlopen(req, timeout=60).read() if hashlib.sha256(data).hexdigest() != h: die(f"fetched {f} has the wrong sha256") open(p, "wb").write(data) def load_engine(): s = importlib.util.spec_from_file_location("even_engine", os.path.join(SRC, "even-engine.py")) m = importlib.util.module_from_spec(s) s.loader.exec_module(m) return m def manifest(): got = {f: sha(os.path.join(SRC, f)) for f in PIN} for f, h in PIN.items(): if got[f] != h: die(f"sha mismatch {f}: {got[f]}") w = json.load(open(os.path.join(SRC, "indexed-witness.json"))) cert = json.load(open(os.path.join(SRC, "out-cert_k46_eps25_861_d27.json"))) if (w["k"], Fr(w["epsilon"]), Fr(w["threshold"]), w["original_degree"], w["original_basis_size"], w["nonzero_coefficients"]) != (K, EPS, TAU, D, N, N): die("witness header mismatch") if (cert["k"], Fr(cert["eps"]), Fr(cert["tau"]), cert["dvec"], cert["n_vec"]) != (K, EPS, TAU, D, N): die("certificate header mismatch") if w["source_engine_sha256"] != PIN["even-engine.py"] or \ w["source_certificate_sha256"] != PIN["out-cert_k46_eps25_861_d27.json"]: die("witness provenance mismatch") terms = w["terms"] if len(terms) != N or len(cert["vec"]) != N: die("coefficient count mismatch") c = [Fr(t["coefficient"]) for t in terms] if [str(x) for x in c] != cert["vec"]: die("witness coefficients differ from certificate vec") vs = hashlib.sha256(",".join(str(x) for x in c).encode()).hexdigest() if vs != VEC_SHA or cert["vec_sha256"] != VEC_SHA or w["coefficient_sha256"] != VEC_SHA: die(f"canonical vector sha mismatch {vs}") if sum(1 for x in c if x != 0) != N: die("support is not the full 846") basis = [(t["a"], tuple(t["partition"])) for t in terms] return w, cert, c, basis, got def path_A(eng, c): e = eng.EvenEngine(K, EPS, D, "exact") if e.n != N: die(f"engine basis size {e.n}") I = Fr(0); J = Fr(0); pairs = 0 for i in range(N): ci = c[i] for j in range(i, N): w = ci * c[j] if i == j else 2 * ci * c[j] I += w * e.M1(i, j) J += w * e.M2(i, j) pairs += 1 return e, I, J, pairs def main(): path = sys.argv[sys.argv.index("--path") + 1] if "--path" in sys.argv else "A" t0 = time.time() ensure_src() w, cert, c, basis, got = manifest() eng = load_engine() e21 = None if path == "A": e21, I, J, pairs = path_A(eng, c) if [tuple(b) for b in e21.B] != basis: die("engine basis order differs from witness terms") elif path == "B": sys.path.insert(0, HERE) from grouped3307 import grouped_IJ I, J, pairs = grouped_IJ(c, basis, K, EPS) else: die("path must be A or B") Q = J - TAU * I stored = {d: Fr(cert["streams"][d]["Q"]) for d in ("21", "27")} out = { "ok": True, "path": path, "k": K, "eps": str(EPS), "d": D, "n": N, "tau": str(TAU), "input_sha256": got, "vec_sha256": VEC_SHA, "I": str(I), "J": str(J), "Q": str(Q), "I_float": float(I), "J_float": float(J), "Q_float": float(Q), "rayleigh_J_over_I": float(J / I) if I else None, "eig_hp_stored": cert["eig_hp"], "I_positive": I > 0, "Q_positive": Q > 0, "Q_equals_stored_d21": Q == stored["21"], "Q_equals_stored_d27": Q == stored["27"], "pairs_evaluated": pairs, "sha256_of_I_J_Q": hashlib.sha256(f"{I}|{J}|{Q}".encode()).hexdigest(), } print(f"elapsed {time.time()-t0:.1f}s", file=sys.stderr) print(json.dumps(out)) ok = out["I_positive"] and out["Q_positive"] and out["Q_equals_stored_d21"] and out["Q_equals_stored_d27"] sys.exit(0 if ok else 1) if __name__ == "__main__": main()