"""Job #2065 — triage of route 91: run the priced chain cell 23#->43# on the pinned K* engine. REGISTERED BEFORE ANY NUMBER BELOW WAS COMPUTED (nothing in this file was tuned after seeing a cell; every threshold is fixed here and reported as-is if it fires): R1 ANCHOR (convention-free, exact, not merely checked). For a single-slot window at a level-P slot r, the number of copies c in [0, NCOPY) for which the slot is KILLED is NCOPY - prod_{q in Q}(q - 2) and it is the SAME for every r: for each q the forbidden c-classes are the two distinct classes c = -r*P#^-1 and c = (-r-2)*P#^-1 mod q, so "avoid both classes mod every q" has exactly prod(q-2) solutions mod NCOPY = prod q by CRT. Hence N_1 = D*(NCOPY - prod(q-2)) IDENTICALLY, with no reference to how a window is laid across copies -- which is why it gates the engine's whole convention, and why it is the falsifier pre-registered here: 23#->43# N_1 = 117867726310950 (D = 7952175, NCOPY = 58642669, prod(q-2) = 43820595) 19#->43# N_1 = 162280751678100 (D = 378675, NCOPY = 1348781387, prod(q-2) = 920232495) A run whose FIRST cell misses this is defective at k = 1; report, do not retune. R2 PATCH-CONSERVATION. The u64 patch (artifacts/kstar-engine-u64.py) may only be used on the new cells if it reproduces ALL committed (step, k) cells of the three frozen steps exactly. Any mismatch stops the run before the new cell is read. R3 MONOTONICITY. N_k must be non-increasing in k at the new cell too. R4 INDEPENDENT SAMPLED CENSUS (different algorithm, seed pre-registered = 2065). For U uniform (slot start, copy) pairs, test the k consecutive slots directly against the kill classes and estimate N_k = p_hat * D * NCOPY with SE = sqrt(p_hat(1-p_hat)/U) * D * NCOPY. This is the independent instrument route 91's method (d) declares unaffordable; as a SAMPLE it is affordable, and its coverage (U of D*NCOPY windows) is stated rather than implied. R5 CERTIFICATE. K* = max{k : N_k >= 1}; if N_kmax = 0 the run certifies K* <= kmax-1 and, with the Bridging Lemma, C2 <= kmax at that step. A kmax reached with N_kmax >= 1 certifies nothing and reports the curve and the price of the next kmax instead. """ import argparse import importlib.util import json import os import sys import time import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) ENGINE = os.path.join(HERE, "kstar-engine-u64.py") SEED = 2065 COMMITTED = { (13, 29): [105221160, 29114520, 6942634, 1470674, 285422, 52048, 9456, 1712, 270, 36, 0], (13, 31): [3691273410, 1208132640, 357506328, 98048374, 25570726, 6426266, 1601992, 403958, 103170, 25738, 5568, 1180, 296, 80, 18, 6, 2, 0], (17, 31): [2524470300, 607806150, 128515868, 25793636, 5037414, 929548, 167144, 29148, 4894, 710, 138, 22, 6, 0], } ANCHORS = {(23, 43): 117867726310950, (19, 43): 162280751678100} def load(path): spec = importlib.util.spec_from_file_location("eng", path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def closed_form(P, Pp, PRIMES): Q = [q for q in PRIMES if P < q <= Pp] ncopy = 1 prod2 = 1 for q in Q: ncopy *= q prod2 *= (q - 2) return Q, ncopy, prod2 def sample_census(slots, Psharp, Q, D, ncopy, kmax, U, seed): """R4: direct residue test on U uniform (start, copy) pairs; no inclusion-exclusion.""" rng = np.random.default_rng(seed) i = rng.integers(0, D, size=U, dtype=np.int64) c = rng.integers(0, ncopy, size=U, dtype=np.int64) alive = np.ones(U, dtype=bool) out = {} Qn = len(Q) for j in range(kmax): t = i + j idx = t % D carry = c + t // D val = slots[idx] + Psharp * carry # int64: max ~ ncopy * Psharp ~ 5e15, safe killed = np.zeros(U, dtype=bool) for a in range(Qn): q = Q[a] r = val % q killed |= (r == 0) | (r == q - 2) alive &= killed p = float(alive.mean()) nhat = p * D * ncopy se = (p * (1.0 - p) / U) ** 0.5 * D * ncopy out[j + 1] = {"p_hat": p, "N_hat": nhat, "se": se, "hits": int(alive.sum())} return out def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", required=True) ap.add_argument("--target", default="23,43") ap.add_argument("--kmax", type=int, default=9) ap.add_argument("--validate", default="0,1,2") ap.add_argument("--sample-u", type=int, default=4000000, dest="sample_u") a = ap.parse_args() checks = [] def chk(name, ok, detail): checks.append({"name": name, "ok": bool(ok), "detail": detail}) print("%-56s %s %s" % (name, "PASS" if ok else "FAIL", detail)) sys.stdout.flush() eng = load(ENGINE) import hashlib esha = hashlib.sha256(open(ENGINE, "rb").read()).hexdigest() res = {"tool": "check-2065.py", "job": 2065, "route": 91, "engine": ENGINE, "engine_sha256": esha, "seed": SEED, "target_kmax": a.kmax, "checks": checks} # ---------------------------------------------------------------- R2 validation print("== R2 u64 patch conservation on the frozen steps ==") val = [] for si in [int(x) for x in a.validate.split(",") if x.strip()]: P, Pp = [(13, 29), (13, 31), (17, 31)][si] slots, Psharp = eng.slot_word(P) D = len(slots) Q, ncopy, prod2 = closed_form(P, Pp, eng.PRIMES) curve = COMMITTED[(P, Pp)] kmax = len(curve) t0 = time.time() got = eng.nk_curve(slots, Psharp, Q, kmax, D) dt = time.time() - t0 mism = [(k + 1, got[k], curve[k]) for k in range(kmax) if got[k] != curve[k]] chk("R2.frozen %d#->%d# %d cells" % (P, Pp, kmax), not mism, "%.1f s, mismatches %s" % (dt, mism[:3])) chk("R1.frozen-anchor %d#->%d#" % (P, Pp), got[0] == D * (ncopy - prod2), "engine N_1 %d vs closed form %d" % (got[0], D * (ncopy - prod2))) val.append({"step": "%d#->%d#" % (P, Pp), "D": D, "kmax": kmax, "seconds": round(dt, 1), "mismatches": mism, "N_1": got[0]}) res["validation"] = val if any(v["mismatches"] for v in val): print("STOP: the patch does not reproduce the record; the target cell is not run.") json.dump(res, open(a.out, "w", encoding="utf-8", newline="\n"), indent=1, sort_keys=True) return 1 # ---------------------------------------------------------------- R1 anchor + target run P, Pp = [int(x) for x in a.target.split(",")] slots, Psharp = eng.slot_word(P) D = len(slots) Q, ncopy, prod2 = closed_form(P, Pp, eng.PRIMES) anchor = D * (ncopy - prod2) res["target"] = {"step": "%d#->%d#" % (P, Pp), "D": D, "Q": Q, "NCOPY": ncopy, "prod_q_minus_2": prod2, "anchor_N1": anchor, "anchor_matches_this_run_prediction": anchor == ANCHORS.get((P, Pp))} print("== target %d#->%d#: D=%d |Q|=%d NCOPY=%d anchor N_1=%d ==" % (P, Pp, D, len(Q), ncopy, anchor)) chk("R1.anchor equals the value pre-registered in this file", anchor == ANCHORS.get((P, Pp)), "closed form %d vs pre-registered %d" % (anchor, ANCHORS.get((P, Pp)))) t0 = time.time() curve = eng.nk_curve(slots, Psharp, Q, a.kmax, D) dt_target = time.time() - t0 chk("R1.target k=1 cell equals the anchor", curve[0] == anchor, "engine N_1 = %d, anchor %d, %s" % (curve[0], anchor, "MATCH" if curve[0] == anchor else "MISMATCH")) chk("R3.target N_k non-increasing", all(curve[k] >= curve[k + 1] for k in range(len(curve) - 1)), "curve first 6: %s" % curve[:6]) kstar = max([k for k in range(1, len(curve) + 1) if curve[k - 1] >= 1] or [0]) zero_at = next((k for k in range(1, len(curve) + 1) if curve[k - 1] == 0), None) res["target"].update({"kmax": a.kmax, "seconds": round(dt_target, 1), "curve": curve, "kstar_lower_bound": kstar, "first_zero_at_k": zero_at, "certificate": ("C2 <= %d" % kstar) if zero_at else None, "N_kmax_is_zero": curve[-1] == 0}) print(" %.1f s; curve %s" % (dt_target, curve)) print(" K* >= %d; first zero at k = %s" % (kstar, zero_at)) # ---------------------------------------------------------------- R4 independent sample print("== R4 independent sampled census (direct residue test, seed %d) ==" % SEED) U = a.sample_u t0 = time.time() samp = sample_census(slots, Psharp, Q, D, ncopy, len(curve), U, SEED) dts = time.time() - t0 rows = [] for k in sorted(samp): s = samp[k] nk = curve[k - 1] rel = (s["N_hat"] - nk) / nk if nk else float("nan") z = (s["N_hat"] - nk) / s["se"] if s["se"] > 0 else float("nan") rows.append({"k": k, "engine": nk, "sample_N_hat": round(s["N_hat"], 1), "sample_se": round(s["se"], 1), "rel_error": round(rel, 5), "z": round(z, 3), "hits": s["hits"], "p_hat": s["p_hat"]}) print(" k=%-2d engine %-22d sample %-22.0f +/- %-14.0f z=%+.2f (%d hits)" % (k, nk, s["N_hat"], s["se"], z, s["hits"])) res["sample_census"] = {"U": U, "seconds": round(dts, 1), "rows": rows, "windows_per_period": D * ncopy, "coverage_fraction": U / (D * ncopy)} worst = max((abs(r["z"]) for r in rows if r["z"] == r["z"]), default=0.0) chk("R4.sample agrees with the engine within 5 sigma at every k", worst <= 5.0, "worst |z| = %.2f over %d cells; coverage %d of %d windows (%.2e)" % (worst, len(rows), U, D * ncopy, U / (D * ncopy))) res["summary"] = {"pass": sum(1 for c in checks if c["ok"]), "fail": sum(1 for c in checks if not c["ok"]), "of": len(checks)} with open(a.out, "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1, sort_keys=True) print(json.dumps(res["summary"])) return 0 if res["summary"]["fail"] == 0 else 1 if __name__ == "__main__": sys.exit(main())