r"""kstar-engine-check.py -- reproduce the sealed K* pre-registration's committed cells. WHAT IS BEING CHECKED. `attack-kstar-01-prereg.md` (committed alone at 552143c, 2026-08-21) predicts K* and the whole N_k curve at three steps. Its producer claimed the I-E engine was validated against an independent scan on eleven earlier steps. Nobody has published the complementary check: that the COMMITTED NUMBERS are what the STATED FORMULA gives. This script implements the formula exactly as the pre-registration states it, from the definitions only (no corpus producer, no scan), and compares cell for cell. THE FORMULA, as stated (section "The object" of the pre-registration): N_k = sum_shapes sum_{J subset of window} (-1)^|J| prod_{q in Q} (q - nu_q(J)) nu_q(J) = #distinct residues of {x_j, x_j+2 : j in J} mod q K* = max{k : N_k >= 1}; N_k = 0 is the certificate C_2 <= K*+1. MY DEFINITIONS (all from the project's own tile): * level-P slots = {r mod P# : r coprime to P# and r+2 coprime to P#} -- built by lifting the level-5 word [11,17,29] mod 30 by each odd prime p <= P, keeping r%p and (r+2)%p both nonzero. Counts must come out 3, 15, 135, 1485, 22275 ... (the corpus D column). * a window of k consecutive level-P slots (cyclic) is an index i and a copy c in [0, NCOPY), NCOPY = P'#/P# = prod_{P P), so each (slot, q) contributes TWO residue classes for c mod q, which is exactly the 2-element nu-case set the pre-registration describes. Note the free variable is the COPY c, and since NCOPY = prod q over the SAME coprime set Q, choosing its residue per prime is independent: that is why the alternating sum is a product over q and why integer arithmetic is exact. PATCHED COPY, job #2065 (`bf4-911665bf27f8081b1e`), 2026-09-18. Copied byte-for-byte from job #2048's pinned engine (sha256 6d6c80ecff2015127f21deaf059a51638e9d9b9f07f41a64f6927faf7e2e09d6) and changed in exactly ONE respect: the residue-class masks are widened from `uint32` to `uint64`, with the SWAR popcount widened to 64 bits to match. WHY: the mask holds one bit per copy class mod q, so it needs q bits, and the priced chain cells 19#->43# and 23#->43# have entering primes 37, 41 and 43 in Q -- the unpatched engine raises `OverflowError: Python int too large to convert to C long` at the FIRST shape (measured: 0.0 s, both cells), i.e. route 91's next step as written is not executable at all. For q <= 31 every bit sits below 32 and the widened code is bit-identical in behaviour, which the frozen-step validation in `check-2065.py` enforces: all 43 committed (step, k) cells must come back exactly. FIRST FALSIFIER (registered before any cell was compared, in the header of this file): F1 for any step, N_1 must equal D_P * (NCOPY - prod_{q in Q}(q-2)) -- the closed form for one-slot windows. Both operands are computed here from the tile and the prime list, so F1 can fail independently of the subset engine. F2 N_k must be non-increasing in k (a killed (k+1)-window has a killed k-prefix). F3 K* = max{k : N_k >= 1} must equal the pre-registration's committed K*. F4 custody: D_P must equal the corpus's slot counts 3, 15, 135, 1485, 22275, 378675. Reported, not assumed: any cell that disagrees is reported as a disagreement. """ import json import os import sys import time import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) OUT = os.path.join(HERE, "kstar-engine-check.json") PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73] D_EXPECT = {5: 3, 7: 15, 11: 135, 13: 1485, 17: 22275, 19: 378675, 23: 7952175} def slot_word(P): """Level-P twin-slot word: residues mod P# coprime to P# and to P# shifted by 2.""" slots = [11, 17, 29] # mod 30, the level-5 word q = 30 for p in PRIMES: if p <= 5: continue if p > P: break nxt = [] for r in slots: for k in range(p): x = r + k * q if x % p and (x + 2) % p: nxt.append(x) slots = sorted(nxt) # FIX (job #2048 follow-up): the word is read BY POSITION. q *= p return sorted(slots), q def popcount(x): # #2065: widened to 64 bits. The masks index the copy classes mod q, so q = 43 needs bit 42, # which cannot exist in a uint32 -- the unpatched engine raises OverflowError at the first # shape of 19#->43# and 23#->43#. For q <= 31 this is bit-identical to the uint32 version. x = x.astype(np.uint64) x = x - ((x >> np.uint64(1)) & np.uint64(0x5555555555555555)) x = (x & np.uint64(0x3333333333333333)) + ((x >> np.uint64(2)) & np.uint64(0x3333333333333333)) x = (x + (x >> np.uint64(4))) & np.uint64(0x0F0F0F0F0F0F0F0F) return (x * np.uint64(0x0101010101010101)) >> np.uint64(56) CONV = 0 # 0 = wrap adds P# (physical), 1 = cyclic word inside one copy (no add) NONWRAP = False # True = count only windows that do not wrap the copy boundary DESC = False # True = read the window descending (i - k) instead of ascending LASTONLY = False # True = only the final slot of a window may cross the boundary WRAPSUB = False # True = negate the cross-boundary P# term def nu_masks_for_shape(slots, D, Psharp, Q, i, kmax): """For a window start i, the (kmax x |Q|) table of 2-element residue bitmasks. Bit r of mask[k, a] is set iff entering prime Q[a] is killed by residue r of c mod q. The slot index is SIGNED and resolved by floor division, so one expression covers every reading of the cross-copy rule: t = i + k ascending (the pinned reading) or t = i - k descending; copy = t // D is 0 inside the first copy and +/-1 past either boundary; u is the residue plus Psharp * copy. VARIANT READING (added after the pinned one reproduced all 43 committed cells, to close the variant space named in return 1092's next_step): pos t = i + k, copy by floor division <- REPRODUCES all 43 cells conv1 t = i + k, copy forced to 0 (no P# term) nowrap t = i + k, windows crossing the boundary are dropped, not counted desc t = i - k, copy by floor division (descending index order) wrapsub t = i + k, copy by floor division but with the sign of the P# term negated lastonly t = i + k, only the LAST slot of the window may leave the first copy """ masks = np.zeros((kmax, len(Q)), dtype=np.uint64) # #2065: was uint32 (see header) for k in range(kmax): t = i - k if DESC else i + k if NONWRAP and t >= D: return None if LASTONLY and k < kmax - 1 and t >= D: return None copy = int(t // D) if CONV == 0 else 0 if CONV == 0 and WRAPSUB and copy: copy = -abs(copy) u = slots[int(t % D)] + Psharp * copy for a, q in enumerate(Q): inv = pow(Psharp % q, -1, q) r1 = (-u * inv) % q r2 = (-u - 2) * inv % q masks[k, a] = np.uint64((1 << int(r1)) | (1 << int(r2))) # #2065: was uint32 return masks def nk_curve(slots, Psharp, Q, kmax, D): """N_k for k = 1..kmax by the pre-registration's own alternating sum, over all shapes. For each shape the union mask over the slots of J is built for ALL J at once by the standard subset (SOS) transform of OR: U[J] = OR of the singleton masks of J. Then nu_q(J) = popcount(U[J]) and the product over q gives the local factor of the term. Subsets live in kmax bits, so one table per shape serves every k by restriction. """ n = 1 << kmax Jidx = np.arange(n, dtype=np.uint32) parity = (popcount(Jidx) & np.uint32(1)).astype(bool) # True = |J| odd -> sign -1 # J < 2^k restricts to subsets of the first k slots prefixes = [int((Jidx < np.uint32(1 << k)).sum()) for k in range(kmax + 1)] Nk = [0] * (kmax + 1) for i in range(D): masks = nu_masks_for_shape(slots, D, Psharp, Q, i, kmax) if masks is None: continue prod = np.ones(n, dtype=np.int64) for a, q in enumerate(Q): U = np.zeros(n, dtype=np.uint64) # #2065: was uint32 (see header) for k in range(kmax): U[1 << k] = masks[k, a] for b in range(kmax): # SOS transform of OR bit = np.uint64(1 << b) sel = (Jidx & bit).astype(bool) U[sel] |= U[Jidx[sel] ^ bit] prod *= (q - popcount(U).astype(np.int64)) term = np.where(parity, -prod, prod) for k in range(1, kmax + 1): Nk[k] += int(term[:1 << k].sum()) return Nk[1:] def main(): steps = [ # (base P, target P', committed K*, committed N_k curve) (13, 29, 10, [105221160, 29114520, 6942634, 1470674, 285422, 52048, 9456, 1712, 270, 36, 0]), (13, 31, 17, [3691273410, 1208132640, 357506328, 98048374, 25570726, 6426266, 1601992, 403958, 103170, 25738, 5568, 1180, 296, 80, 18, 6, 2, 0]), (17, 31, 13, [2524470300, 607806150, 128515868, 25793636, 5037414, 929548, 167144, 29148, 4894, 710, 138, 22, 6, 0]), ] global CONV, NONWRAP, DESC, LASTONLY, WRAPSUB only = None args = [a for a in sys.argv[1:]] if "--conv1" in args: CONV = 1 if "--nowrap" in args: NONWRAP = True if "--desc" in args: DESC = True if "--lastonly" in args: LASTONLY = True if "--wrapsub" in args: WRAPSUB = True only = None for a in args: if a.startswith("--step"): only = int(a.split("=")[1]) if "=" in a else int(args[args.index(a) + 1]) report = {"steps": [], "D_check": {}, "conv": CONV, "nowrap": NONWRAP, "desc": DESC, "lastonly": LASTONLY, "wrapsub": WRAPSUB} for si, (P, Pp, kstar, curve) in enumerate(steps): if only is not None and si != only: continue slots, Psharp = slot_word(P) D = len(slots) Q = [q for q in PRIMES if P < q <= Pp] report["D_check"][P] = {"D": D, "expected": D_EXPECT[P], "ok": D == D_EXPECT[P]} print(f"\n=== {P}# -> {Pp}# D={D} (expect {D_EXPECT[P]}) Q={Q} K*={kstar}") # F1: closed form for k = 1 ncopy = 1 for q in Q: ncopy *= q prod = 1 for q in Q: prod *= (q - 2) n1_closed = D * (ncopy - prod) print(f"F1 closed form N_1 = D*(NCOPY - prod(q-2)) = {n1_closed} " f"committed {curve[0]} {'MATCH' if n1_closed == curve[0] else 'MISMATCH'}") kmax = len(curve) t0 = time.time() got = 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]] kstar_got = max([k for k in range(1, kmax + 1) if got[k - 1] >= 1] or [0]) mono = all(got[k] >= got[k + 1] for k in range(kmax - 1)) print(f" computed in {dt:.1f}s; mismatches {mism}") print(f" F2 non-increasing: {mono}; F3 K* computed {kstar_got} vs committed {kstar} " f"{'MATCH' if kstar_got == kstar else 'MISMATCH'}") report["steps"].append({ "step": f"{P}#->{Pp}#", "D": D, "Q": Q, "seconds": round(dt, 1), "closed_form_N1": n1_closed, "committed_N1": curve[0], "computed": got, "committed": curve, "mismatches": mism, "kstar_computed": kstar_got, "kstar_committed": kstar, }) with open(OUT, "w", encoding="utf-8", newline="\n") as fh: json.dump(report, fh, indent=1) print(f"\nreport: {OUT}") if __name__ == "__main__": main()