// ============================================================================ // kstar-cover.c — K* at a doubling step P# -> P'# by exact covering search, one window at a time. // Job #193 (adversarial lane). An engine that shares nothing with attack-kstar-01.js's inclusion-exclusion // route or with any period walk. // ============================================================================ // OBJECT (attack-kstar-01.md §0-§2, attack-doubling-01.md). Level-P slots: residues r mod W = P# with // r mod p not in {0, p-2} for every prime p <= P (r and r+2 both coprime to W), sorted r_0 < ... < r_{D-1}, // repeated periodically: slot index n sits at X_n = r_{n mod D} + W*floor(n/D). A level-P slot is KILLED at // a lift M (position X_n + M*W) by an entering prime q in Q = {primes P < q <= P'} when q divides the // position or the position + 2. K* = the longest run of consecutive level-P slots all killed, over all // lifts M. By CRT the residues M*W mod q are independent and uniform over Q (W is a unit mod every q), // so a window of k consecutive slots starting at index i is all-killed at SOME lift iff there are shifts // s_q in Z/q with: for every j < k some q has X_{i+j} + s_q in {0, -2} mod q. // // SEARCH. For a start i, let f be the first uncovered position. Any assignment that covers position f // has a prime q with s_q = -x_f or s_q = -x_f - 2 (x_f = X_{i+f} mod q), which kills exactly the // positions j with x_j in {x_f, x_f - 2} (first case) or {x_f, x_f + 2} (second case) mod q. Branching on // that prime and case, depth <= |Q|, enumerates every assignment's covered prefix, so the largest first- // uncovered index over the leaves is the longest coverable prefix L(i), and K* = max_i L(i). Primes that // cover nothing in the prefix are irrelevant to it. Pruning (sound): to beat the current best b, positions // [0, b] must all be covered; each unused prime q covers at most cap_q(b) = max_rho |{j <= b : x_j in // {rho, rho-2}}| of them. // // WIDTH. 29# = 6,469,693,230 exceeds 2^32. A first version stored slot values in uint32 and truncated them // at base 29; its own run check shared the truncation and passed. Slots are now stored as k = (r - 5)/6 // (every level-P slot, P >= 3, is 5 mod 6), which fits uint32 while 6*2^32 > W, i.e. through base 29, and // the program refuses a base where it does not. Run endpoints are printed from 128-bit values. // // WITNESS. At K* the smallest start i with L(i) >= K* is re-searched single-threaded, its shifts are turned // into one lift M by CRT, and the K* positions are rebuilt and re-checked. The check in this file shares the // slot representation with the search, so it is a self-check only; verify_runs.py re-derives each printed // run from its endpoints alone, in other code. The upper side (no run of K*+1) rests on the exhaustive search. // // usage: kstar-cover P:P' [P:P' ...] stdout is deterministic; timing goes to stderr. // ============================================================================ #include #include #include #include #include #include #include #define KCAP 56 #define MAXQ 8 #define NTHREADS 2 static uint32_t *slots, D; // slots[i] = (r_i - 5)/6 for P >= 3; r_i itself for P = 2 static uint64_t W; static int P, Pp, nq, qv[MAXQ]; static uint32_t Wmod[MAXQ]; static uint8_t *res[MAXQ]; static atomic_int best; static inline uint64_t SL(uint32_t i) { return P >= 3 ? 6ULL * slots[i] + 5 : slots[i]; } static int is_prime(int n) { if (n < 2) return 0; for (int d = 2; d * d <= n; d++) if (n % d == 0) return 0; return 1; } static void print_u128(unsigned __int128 v) { char buf[48]; int n = 0; if (v == 0) buf[n++] = '0'; while (v) { buf[n++] = (char)('0' + (int)(v % 10)); v /= 10; } while (n) putchar(buf[--n]); } typedef struct { uint64_t eq[MAXQ][64]; uint8_t xr[MAXQ][KCAP]; int capv[MAXQ], capTarget; int path_t[MAXQ], path_s[MAXQ], depth; } ctx_t; static void load_window(ctx_t *c, uint32_t i) { for (int t = 0; t < nq; t++) { memset(c->eq[t], 0, sizeof(uint64_t) * (size_t)qv[t]); for (int j = 0; j < KCAP; j++) { uint64_t ij = (uint64_t)i + (uint64_t)j; uint32_t r = res[t][ij % D]; uint64_t wraps = ij / D; uint32_t x = (uint32_t)((r + wraps * Wmod[t]) % (uint64_t)qv[t]); c->xr[t][j] = (uint8_t)x; c->eq[t][x] |= 1ULL << j; } } c->capTarget = -1; } static void caps_for(ctx_t *c, int target) { uint64_t pm = target >= 64 ? ~0ULL : ((1ULL << target) - 1); for (int t = 0; t < nq; t++) { int q = qv[t], m = 0; for (int rho = 0; rho < q; rho++) { int v = __builtin_popcountll((c->eq[t][rho] | c->eq[t][(rho + q - 2) % q]) & pm); if (v > m) m = v; } c->capv[t] = m; } c->capTarget = target; } static void dfs(ctx_t *c, uint64_t cov, int used) { int f = __builtin_ctzll(~cov); if (f >= KCAP - 1) { fprintf(stderr, "KCAP too small at %d#->%d#\n", P, Pp); exit(2); } int b = atomic_load(&best); while (f > b) { if (atomic_compare_exchange_weak(&best, &b, f)) { b = f; break; } } if (used == (1 << nq) - 1) return; int target = b + 1; if (c->capTarget != target) caps_for(c, target); uint64_t pm = target >= 64 ? ~0ULL : ((1ULL << target) - 1); int unc = __builtin_popcountll(~cov & pm), capsum = 0; for (int t = 0; t < nq; t++) if (!(used >> t & 1)) capsum += c->capv[t]; if (capsum < unc) return; for (int t = 0; t < nq; t++) { if (used >> t & 1) continue; int q = qv[t], rho = c->xr[t][f]; uint64_t m0 = c->eq[t][rho] | c->eq[t][(rho + q - 2) % q]; uint64_t m1 = c->eq[t][rho] | c->eq[t][(rho + 2) % q]; dfs(c, cov | m0, used | (1 << t)); if (m1 != m0) dfs(c, cov | m1, used | (1 << t)); } } static int dfs_w(ctx_t *c, uint64_t cov, int used, int target) { int f = __builtin_ctzll(~cov); if (f >= target) return 1; if (used == (1 << nq) - 1) return 0; if (c->capTarget != target) caps_for(c, target); uint64_t pm = target >= 64 ? ~0ULL : ((1ULL << target) - 1); int unc = __builtin_popcountll(~cov & pm), capsum = 0; for (int t = 0; t < nq; t++) if (!(used >> t & 1)) capsum += c->capv[t]; if (capsum < unc) return 0; for (int t = 0; t < nq; t++) { if (used >> t & 1) continue; int q = qv[t], rho = c->xr[t][f]; for (int cls = 0; cls < 2; cls++) { uint64_t m = c->eq[t][rho] | c->eq[t][cls ? (rho + 2) % q : (rho + q - 2) % q]; if (cls && m == (c->eq[t][rho] | c->eq[t][(rho + q - 2) % q])) continue; c->path_t[c->depth] = t; c->path_s[c->depth] = cls ? (2 * q - rho - 2) % q : (q - rho) % q; c->depth++; if (dfs_w(c, cov | m, used | (1 << t), target)) return 1; c->depth--; } } return 0; } typedef struct { int tid; } targ_t; static void *worker(void *arg) { int tid = ((targ_t *)arg)->tid; ctx_t *c = calloc(1, sizeof(ctx_t)); for (uint32_t i = (uint32_t)tid; i < D; i += NTHREADS) { load_window(c, i); dfs(c, 0, 0); } free(c); return NULL; } static int64_t inv_mod(int64_t a, int64_t m) { int64_t r = 1, e = m - 2; a %= m; if (a < 0) a += m; while (e) { if (e & 1) r = r * a % m; a = a * a % m; e >>= 1; } return r; } static void build_tile(void) { W = 1; int ps[32], np = 0; for (int p = 2; p <= P; p++) if (is_prime(p)) { W *= (uint64_t)p; ps[np++] = p; } if (P >= 3 && (W - 5) / 6 > UINT32_MAX) { fprintf(stderr, "width: (W-5)/6 exceeds uint32 at P = %d\n", P); exit(3); } uint64_t cap = 1; for (int t = 0; t < np; t++) cap *= (uint64_t)(ps[t] > 2 ? ps[t] - 2 : 1); if (cap > UINT32_MAX) { fprintf(stderr, "width: D exceeds uint32 at P = %d\n", P); exit(3); } D = (uint32_t)cap; slots = malloc(sizeof(uint32_t) * D); uint32_t n = 0; uint64_t start = P >= 3 ? 5 : 0, step = P >= 3 ? 6 : 1; for (uint64_t r = start; r < W; r += step) { int ok = 1; for (int t = 0; t < np && ok; t++) { uint64_t m = r % (uint64_t)ps[t]; if (m == 0 || m == (uint64_t)(ps[t] - 2)) ok = 0; } if (ok) { if (n >= D) { fprintf(stderr, "tile overflow\n"); exit(3); } slots[n] = P >= 3 ? (uint32_t)((r - 5) / 6) : (uint32_t)r; if (SL(n) != r) { fprintf(stderr, "width: slot encoding lost r = %llu\n", (unsigned long long)r); exit(3); } n++; } } if (n != D) { fprintf(stderr, "tile count %u != prod(p-2) %u at P = %d\n", n, D, P); exit(3); } } int main(int argc, char **argv) { for (int a = 1; a < argc; a++) { if (sscanf(argv[a], "%d:%d", &P, &Pp) != 2) { fprintf(stderr, "bad step %s\n", argv[a]); return 1; } struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); build_tile(); nq = 0; for (int q = P + 1; q <= Pp; q++) if (is_prime(q)) { if (nq == MAXQ) { fprintf(stderr, "too many primes\n"); return 1; } qv[nq++] = q; } for (int t = 0; t < nq; t++) { Wmod[t] = (uint32_t)(W % (uint64_t)qv[t]); res[t] = malloc(D); for (uint32_t i = 0; i < D; i++) res[t][i] = (uint8_t)(SL(i) % (uint64_t)qv[t]); } atomic_store(&best, 0); pthread_t th[NTHREADS]; targ_t ta[NTHREADS]; for (int k = 0; k < NTHREADS; k++) { ta[k].tid = k; pthread_create(&th[k], NULL, worker, &ta[k]); } for (int k = 0; k < NTHREADS; k++) pthread_join(th[k], NULL); int K = atomic_load(&best); ctx_t *c = calloc(1, sizeof(ctx_t)); uint32_t wi = D; unsigned __int128 M = 0, mod = 1; for (uint32_t i = 0; i < D; i++) { load_window(c, i); c->depth = 0; if (dfs_w(c, 0, 0, K)) { wi = i; break; } } int selfcheck = 0; unsigned __int128 y0 = 0, ylast = 0; if (wi < D) { for (int d = 0; d < c->depth; d++) { uint64_t q = (uint64_t)qv[c->path_t[d]]; uint64_t need = (uint64_t)((int64_t)c->path_s[d] * inv_mod((int64_t)(W % q), (int64_t)q) % (int64_t)q); uint64_t cur = (uint64_t)(M % q), stp = (uint64_t)(mod % q), k = 0; while ((cur + k * stp) % q != need) k++; M += (unsigned __int128)k * mod; mod *= q; } selfcheck = 1; for (int j = 0; j < K; j++) { uint64_t ij = (uint64_t)wi + (uint64_t)j; unsigned __int128 y = (unsigned __int128)SL((uint32_t)(ij % D)) + (unsigned __int128)W * (ij / D) + (unsigned __int128)W * M; int killed = 0; for (int t = 0; t < nq && !killed; t++) { uint64_t m = (uint64_t)(y % (uint64_t)qv[t]); if (m == 0 || m == (uint64_t)qv[t] - 2) killed = 1; } if (j == 0) y0 = y; if (j == K - 1) ylast = y; if (!killed) selfcheck = 0; } } printf("%d#->%d# Q = {", P, Pp); for (int t = 0; t < nq; t++) printf("%s%d", t ? "," : "", qv[t]); printf("} D = %u K* = %d cert C2 <= %d witness start %u lift M = ", D, K, K + 1, wi); print_u128(M); printf(" run ["); print_u128(y0); printf(", "); print_u128(ylast); printf("] %s\n", selfcheck ? "self-check: all K* slots killed" : "SELF-CHECK FAILED"); fflush(stdout); free(c); for (int t = 0; t < nq; t++) free(res[t]); free(slots); clock_gettime(CLOCK_MONOTONIC, &t1); fprintf(stderr, "%d#->%d#: %.2f s\n", P, Pp, (double)(t1.tv_sec - t0.tv_sec) + (double)(t1.tv_nsec - t0.tv_nsec) / 1e9); } return 0; }