#!/usr/bin/env python3 """Independent checker for job #4332 (route 59, rescue). Stdlib only, no numpy. Usage: python3 verify_route59_numbers.py [inputs.json] [results.json] It establishes three things and nothing else. (A) EXACT RATIONAL LEDGER (fractions.Fraction). The route-59 exponent arithmetic named in the owning note and re-derived by return #878: 3*(1/20)/2 + 3*(1/2 - 1/20) = 57/40 (the Q^(3/2)E^3 majorant exponent) 57/40 - 139/100 = 7/200 (the moment deficit) sigma + 9/20 = 1/2 at sigma = 1/20 (1/2)*(9/20) - 7/200 = 19/100 (operator-norm route minus requirement) (B) EXACT INTEGER KERNEL SYMMETRY. The structural fact the spectral argument rests on, recomputed here from the pinned definitions with plain integer arithmetic: for the completed modulus c = lcm(q e1, q e2) = q j l1 l2 and R = h1 l2 - h2 l1, swapping the pair sends R -> -R with c fixed, hence the kernel phase e_c(sigma theta R mbar) is conjugated. No floating point is used in this part: modular inverses and residues are recomputed and compared. (C) ARITHMETIC RELATIONS AMONG THE REPORTED NUMBERS. Read from the results JSON: the reported operator bound dominates the reported signed sum in every cell; the spectral evaluation reproduces the direct evaluation; the Hermiticity defect is below tolerance; the sign-blind saving exceeds x^(7/200) in every x = 1e6 cell; and the three pinned regression cells equal return #1075's served published values. What it does NOT establish: it does not recompute the kernels, the eigenvalues or the shuffle nulls; those are the producer's numbers and are checked only for internal consistency and against the served #1075 values. Exit 0 only when every check passes. """ import json import os import re import sys from fractions import Fraction as F from math import gcd FAILS = [] def check(name, ok, detail=""): print(("PASS " if ok else "FAIL ") + name + ((" :: " + detail) if detail else "")) if not ok: FAILS.append(name) def rat(txt): """Evaluate a rational expression exactly, with Fraction arithmetic only. Two guards: the expression must contain only digits, spaces and the four arithmetic symbols (so no name lookup, attribute access or call can occur), and every integer literal is wrapped in F(...) before evaluation so that `/` is exact rational division rather than float division. """ if not re.fullmatch(r"[0-9+\-*/(). ]+", txt): raise ValueError("unsafe rational expression: " + txt) exact = re.sub(r"\d+", r"F(\g<0>)", txt) return eval(exact, {"__builtins__": {}}, {"F": F}) # noqa: S307 - whitelisted above def part_a(inp): print("-- (A) exact rational exponent ledger --") for row in inp["exponent_ledger"]: lhs = rat(row["lhs"]) rhs = rat(row["rhs"]) check("rational " + row["name"], lhs == rhs, f"{lhs} vs {rhs}") def part_b(inp): print("-- (B) exact integer kernel symmetry --") for row in inp["symmetry_witnesses"]: q, e1, e2, h1, h2 = row["q"], row["e1"], row["e2"], row["h1"], row["h2"] j = gcd(e1, e2) l1, l2 = e1 // j, e2 // j c = q * j * l1 * l2 R1 = h1 * l2 - h2 * l1 R2 = h2 * l1 - h1 * l2 ok = (c == row["c"]) and ((R1 + R2) % c == 0) m = row["m"] if gcd(m, c) == 1: a = (2 * R1 * pow(m, -1, c)) % c b = (2 * R2 * pow(m, -1, c)) % c ok = ok and (a + b) % c == 0 and a == row["phase_sigma_plus"] % c else: ok = False check(f"kernel conjugation q={q} pair=({e1},{e2}) h=({h1},{h2}) m={m}", ok) def rel(a, b, tol): if b == 0: return abs(a) <= tol return abs(a - b) / abs(b) <= tol def part_c(inp, res): print("-- (C) relations among the reported numbers --") for cell in inp["regression_cells"]: key, want_s, want_a1 = cell["key"], cell["S_abs"], cell["A1"] got = res["part1"][key] check("regression " + key, rel(got["S_abs"], want_s, 1e-6) and rel(got["A1"], want_a1, 1e-6), f"S {got['S_abs']:.6f} vs {want_s}, A1 {got['A1']:.6f} vs {want_a1}") bad_bound, bad_spec, bad_herm = [], [], [] for part in ("part1", "part2", "part3"): for key, cell in res[part].items(): if not isinstance(cell, dict) or "A_op" not in cell: continue if not (cell["A_op"] * (1 + 1e-9) >= cell["S_abs"]): bad_bound.append(key) if not rel(cell["spectral_S_abs"], cell["S_abs"], 1e-9): bad_spec.append(key) if not (cell["herm_defect"] < 1e-10): bad_herm.append(key) check("operator bound A_op >= |S| in every cell", not bad_bound, str(bad_bound[:4])) check("spectral identity in every cell", not bad_spec, str(bad_spec[:4])) check("Hermiticity defect < 1e-10 in every cell", not bad_herm, str(bad_herm[:4])) thr = F(7, 200) weak = [] for key, cell in res["part2"].items(): if not isinstance(cell, dict) or "guaranteed_saving" not in cell: continue need = 10 ** (6 * float(thr)) # x^(7/200) at x = 1e6, the required factor if cell["guaranteed_saving"] < need: weak.append((key, cell["guaranteed_saving"], need)) check("E-sweep: sign-blind saving >= x^(7/200) at x=1e6", not weak, str(weak[:4])) jp = [c["p_upper"] for k, c in res["part2"].items() if k.endswith("shuffle_joint")] check("E-sweep: a joint one-sided p-value is reported for every E", len(jp) == len(inp["e_sweep"]), f"{len(jp)} vs {len(inp['e_sweep'])}") sl = res["part2"]["slopes"] check("slopes reported for true signs and the shared-draw null", all(k in sl for k in ("true", "null_median", "Aop_over_A1")), json.dumps(sl)) def main(): here = os.path.dirname(os.path.abspath(__file__)) ip = sys.argv[1] if len(sys.argv) > 1 else os.path.join(here, "route59_inputs.json") rp = sys.argv[2] if len(sys.argv) > 2 else os.path.join(here, "spectral_axis.json") inp = json.load(open(ip)) res = json.load(open(rp)) part_a(inp) part_b(inp) part_c(inp, res) print() if FAILS: print(f"RESULT: FAIL ({len(FAILS)} failed): {FAILS}") return 1 print("RESULT: PASS (all checks)") return 0 if __name__ == "__main__": sys.exit(main())