#!/usr/bin/env python3 """check4288.py — independent verification of job #4288's claim (route 112, P = 2310 class). Claim (results4288.json): with U = {13,17,19,23,29,31}, C = K*(2310,U) = 20, A_q = K*(2310,U\\{q}) = 13, 13, 13, 15, 14, 14 for q = 13, 17, 19, 23, 29, 31, marginals C - A_q in {5,6,7}, max 7; the eight custody gates reproduce exactly; the forced brackets hold. This checker does NOT trust the C engine's numbers: route 1 pure-Python direct enumeration over the slot sequence for the small rows (M <= 1.4e7); route 2 chunked numpy full-period scan for the big rows (three P=210 gates and the seven P=2310 rows) — a different implementation, different memory layout, no wheel-byte buffer; route 3 definition-level verification of the witness file (every witness run is re-walked slot by slot against r = 0, -2 (mod q)); plus custody: sha256 of every served input and of the raw outputs, the gate values in the served P=210 table, the row list against the step's definition, and the pre-registered falsifiers. Object (as in the served files): slots are the r in [0, M) with r != 0, -2 (mod q) for every prime q | P; a slot is killed iff r = 0 or -2 (mod q) for some q in R; K*(P,R) is the maximal CYCLIC run of consecutive killed slots; M = P*prod(R). All rows here have p = 1 (no tile-dial prime), so the enumeration is exactly the slot sequence. Usage: python check4288.py [--skip-heavy] Exit 0 only if every check passes. """ import argparse import hashlib import json import os import sys import time HERE = os.path.dirname(os.path.abspath(__file__)) def sha_of(name): return hashlib.sha256(open(os.path.join(HERE, name), "rb").read()).hexdigest() def alive_classes(P): """the r mod P with r and r+2 both coprime to P (sorted).""" fac = [] t = P d = 2 while d * d <= t: if t % d == 0: fac.append(d) while t % d == 0: t //= d d += 1 if t > 1: fac.append(t) return [a for a in range(P) if all(a % q not in (0, q - 2) for q in fac)] def is_killed(r, R): return any(r % q == 0 or r % q == (q - 2) % q for q in R) def prod(xs): p = 1 for x in xs: p *= x return p def direct_K(P, R, collective=None): """route 1: walk the slot sequence in order, straight from the definition (no wheel bytes).""" cls = alive_classes(P) m = len(cls) nk = prod(R) total = nk * m best = cur = prefix = 0 seen = False for k in range(nk): base = P * k for a in cls: r = base + a if is_killed(r, R): cur += 1 continue if not seen: prefix = cur seen = True if cur > best: best = cur cur = 0 if not seen: return total if cur > best: best = cur return max(best, prefix + cur) def numpy_K(P, R, chunk=1 << 22, verbose=False): """route 2: chunked full-period scan; killed marked by strided writes on the k index.""" import numpy as np cls = alive_classes(P) m = len(cls) nk = prod(R) total = nk * m # k-residues per (q, class, which): k = -a*P^-1 and (-a-2)*P^-1 (mod q) offs = [] for q in R: Pi = pow(P % q, -1, q) per = [] for a in cls: per.append(((q - a % q) % q) * Pi % q) # r = 0 (mod q) per.append(((2 * q - a % q - 2) % q) * Pi % q) # r = -2 (mod q) offs.append((q, per)) best = 0 carry = 0 # length of the killed run ending exactly at the previous chunk's last index prefix = None # length of the killed run starting at index 0 of the whole period first = True kb = 0 t0 = time.time() while kb < nk: w = min(chunk // m, nk - kb) blk = np.zeros(w * m, dtype=bool) for q, per in offs: kbm = kb % q for j in range(m): for s in range(2): r0 = per[2 * j + s] off = (r0 - kbm) % q blk[off * m + j::q * m] = True kk = blk.astype(np.int8) d = np.diff(kk, prepend=np.int8(0), append=np.int8(0)) starts = np.flatnonzero(d == 1) ends = np.flatnonzero(d == -1) if starts.size: lens = ends - starts if starts[0] == 0: lens = lens.copy() lens[0] += carry b = int(lens.max()) if b > best: best = b if first: prefix = int(lens[0]) if starts[0] == 0 else 0 first = False carry = int(lens[-1]) if ends[-1] == blk.size else 0 else: # whole chunk killed carry += blk.size if carry > best: best = carry kb += w if verbose and (kb * m) % (1 << 30) < blk.size: print(" ... %d/%d slots, %.1f s" % (kb * m, total, time.time() - t0), flush=True) if prefix is None: return total return max(best, prefix + carry) def main(): ap = argparse.ArgumentParser() ap.add_argument("--skip-heavy", action="store_true") a = ap.parse_args() checks = [] def check(name, ok, detail=""): checks.append((name, bool(ok), detail)) print("%-58s %s %s" % (name, "ok " if ok else "FAIL", detail), flush=True) res = json.load(open(os.path.join(HERE, "results4288.json"), encoding="utf-8")) # ---- custody ---------------------------------------------------------- for name in ("kstar.c", "route112-P210-class.json", "PREREG-4288.md", "gates-in.txt", "gates.out", "rows2310-in.txt", "rows2310.out", "gates.witness.out", "rows2310.witness.out", "pthread-shim.h", "witness4288.c", "build.py", "route-112.json"): check("sha256 %s (as declared)" % name, sha_of(name) == res["shas"][name]) check("served kstar.c is return #1812's file", res["shas"]["kstar.c"] == "4d3faa05291d3533890f6fb2ad11b8b5d22e1697ba85bcb86b713ce531ac7aba") check("served P=210 table is return #1806's file", res["shas"]["route112-P210-class.json"] == "02b36b00075b861c19bd59b9decdfebe02af5f76a3a4c2633c8b45eec61b160d") # the served P=210 table's own gates must match the record p210 = json.load(open(os.path.join(HERE, "route112-P210-class.json"), encoding="utf-8")) g = p210["gates"] check("served P=210 table gates (5 values) present", [g["K*(30,{7,11,19})"], g["K*(390,{7,11,19})"], g["K*(30,{7,11,13,19})"], g["K*(30,{7,13,19,23})"], g["K*(330,{7,13,19,23})"]] == [9, 8, 10, 12, 10]) check("served P=210 cross-check rows (11..29 chain)", p210["K_of_full_U"] == 18 and p210["marginal_max"] == 7) # ---- the step's rows, parsed from the raw inputs ---------------------- rows_in = [dict(zip(("P", "p", "n", "R"), (int(f[0]), int(f[1]), int(f[2]), [int(x) for x in f[3:]]))) for f in (ln.split() for ln in open(os.path.join(HERE, "rows2310-in.txt")) if ln.split())] U = res["U"] check("rows2310-in.txt is C plus the six A_q of the step's class", rows_in[0]["P"] == 2310 and rows_in[0]["R"] == U and rows_in[0]["p"] == 1 and [r["R"] for r in rows_in[1:]] == [[x for x in U if x != q] for q in U] and all(r["p"] == 1 for r in rows_in)) # ---- engine outputs vs the declaration -------------------------------- eng = {} for ln in open(os.path.join(HERE, "rows2310.out"), encoding="utf-8"): f = [int(x) for x in ln.split()] if f: eng[(f[0], tuple(f[2:-1]))] = f[-1] check("engine C row == declared C", eng[(2310, tuple(U))] == res["C"], "C = %d" % res["C"]) for q in U: R = tuple(x for x in U if x != q) check("engine A_%d row == declared A_q" % q, eng[(2310, R)] == res["A_q"][str(q)], "A_%d = %d" % (q, res["A_q"][str(q)])) marg = {q: res["C"] - res["A_q"][str(q)] for q in U} check("marginals as declared", {str(q): marg[q] for q in U} == res["marginal"], str(sorted(marg.values()))) # ---- witnesses, verified against the definition (route 3) ------------- def read_wit(path): out = {} for ln in open(os.path.join(HERE, path), encoding="utf-8"): f = [int(x) for x in ln.split()] if not f: continue n = f[1] out[(f[0], tuple(f[2:2 + n]))] = dict(K=f[2 + n], start_x=f[3 + n], len=f[4 + n], wrapped=f[5 + n]) return out def witness_ok(P, R, w): cls = alive_classes(P) m = len(cls) total = prod(R) * m if w["len"] <= 0 or w["wrapped"] != 0: return False, "wrapped/empty witness not used here" for t in range(w["len"]): x = (w["start_x"] + t) % total k, j = divmod(x, m) r = P * k + cls[j] if not is_killed(r, R): return False, "slot x=%d (r=%d) is not killed" % (x, r) return True, "" for path, labels in (("rows2310.witness.out", None), ("gates.witness.out", None)): wit = read_wit(path) for key, w in wit.items(): P, R = key ok, why = witness_ok(P, R, w) check("witness %s %s valid run of %d" % (P, list(R), w["len"]), ok, why) # ---- route 1: pure-Python direct enumeration on the small rows -------- small = [(30, (7, 11, 19), 9), (390, (7, 11, 19), 8), (30, (7, 11, 13, 19), 10), (30, (7, 13, 19, 23), 12), (330, (7, 13, 19, 23), 10)] for P, R, want in small: if a.skip_heavy: continue t0 = time.time() got = direct_K(P, list(R)) check("route 1 direct scan K*(%d,%s)" % (P, list(R)), got == want, "%d (want %d, %.1f s)" % (got, want, time.time() - t0)) # the engine's own gate output for the same rows ge = {} for ln in open(os.path.join(HERE, "gates.out"), encoding="utf-8"): f = [int(x) for x in ln.split()] if f: ge[(f[0], tuple(f[2:-1]))] = f[-1] for P, R, want in small: check("engine gate K*(%d,%s) == %d" % (P, list(R), want), ge[(P, tuple(R))] == want, str(ge[(P, tuple(R))])) # ---- route 2: independent numpy full-period scans --------------------- if not a.skip_heavy: heavy = [(210, (11, 13, 17, 19, 23, 29), 18), (210, (13, 17, 19, 23, 29), 11), (210, (11, 13, 17, 19, 23, 31), 18)] for P, R, want in heavy: t0 = time.time() got = numpy_K(P, list(R)) check("route 2 numpy K*(%d,%s)" % (P, list(R)), got == want, "%d (want %d, %.1f s)" % (got, want, time.time() - t0)) for P, R, want in [(2310, tuple(U), res["C"])] + \ [(2310, tuple(x for x in U if x != q), res["A_q"][str(q)]) for q in U]: t0 = time.time() got = numpy_K(P, list(R), verbose=True) check("route 2 numpy K*(%d,%s)" % (P, list(R)), got == want, "%d (want %d, %.1f s)" % (got, want, time.time() - t0)) # ---- the record's forced brackets and the pre-registered falsifiers --- A = {q: res["A_q"][str(q)] for q in U} check("bracket A_31 in [9,17]", 9 <= A[31] <= 17, "A_31 = %d" % A[31]) check("bracket A_29 <= 17", A[29] <= 17, "A_29 = %d" % A[29]) check("fold entry C >= A_q + 1 for every q", all(res["C"] >= A[q] + 1 for q in U)) check("P2 (step's question): max marginal <= 7", max(marg.values()) <= 7, "max = %d" % max(marg.values())) check("F1 did not fire (no marginal > 7)", all(m <= 7 for m in marg.values())) check("ceiling non-increasing over P = 30, 210, 2310", 10 >= 7 >= max(marg.values()), "10, 7, %d" % max(marg.values())) check("C is one number for the class (definitional)", True, "R u {q} = U in all six rows") bad = [c for c in checks if not c[1]] print("\n%d checks, %d FAIL" % (len(checks), len(bad))) for name, _ok, detail in bad: print(" FAIL", name, detail) return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())