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. 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 = nxt q *= p return slots, q def popcount(x): x = x.astype(np.uint32) x = x - ((x >> np.uint32(1)) & np.uint32(0x55555555)) x = (x & np.uint32(0x33333333)) + ((x >> np.uint32(2)) & np.uint32(0x33333333)) x = (x + (x >> np.uint32(4))) & np.uint32(0x0F0F0F0F) return (x * np.uint32(0x01010101)) >> np.uint32(24) 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 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. """ masks = np.zeros((kmax, len(Q)), dtype=np.uint32) for k in range(kmax): t = i + k if NONWRAP and t >= D: return None u = slots[t % D] if CONV == 0 and t >= D: u += Psharp 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.uint32((1 << int(r1)) | (1 << int(r2))) 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.uint32) for k in range(kmax): U[1 << k] = masks[k, a] for b in range(kmax): # SOS transform of OR bit = np.uint32(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 only = None args = [a for a in sys.argv[1:]] if "--conv1" in args: CONV = 1 if "--nowrap" in args: NONWRAP = 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} 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()