/* Finite weighted deletion-run census. No asymptotic claim. Two methods share only the old gap word: endpoint-live phase windows and weighted components of the two-state kill graph. Literal folds gate them. */ #include #include #include #include #define LEVELS 25 #define BINS 1024 typedef struct { int top; uint64_t D, W, first; uint8_t *g; } Tile; typedef struct { uint64_t h[LEVELS][BINS], count[LEVELS], left[LEVELS]; int maximum[LEVELS], residue[LEVELS], maxL; } Census; static void require(int ok, const char *message) { if (!ok) { fprintf(stderr, "FAIL: %s\n", message); exit(2); } } static void *allocate(size_t n) { void *p = calloc(1, n); require(p != NULL, "allocation"); return p; } static int gap(const Tile *t, uint64_t i) { return t->g[i >= t->D ? i % t->D : i]; } static void add(Census *c, int L, int span6, uint64_t n, uint64_t left, int residue) { require(L >= 0 && L < LEVELS && span6 > 0 && span6 < BINS, "census bounds"); c->h[L][span6] += n; c->count[L] += n; if (n && span6 > c->maximum[L]) { c->maximum[L] = span6; c->left[L] = left; c->residue[L] = residue; } if (n && L > c->maxL) c->maxL = L; } static int killed(int r, int q) { return r == 0 || r == q - 2; } static int nu0(int m, int q) { return q - 4 + (m == 0 ? 2 : (m == 2 || m == q - 2 ? 1 : 0)); } static void validate_tile(const Tile *t) { uint64_t sum = 0, expectedD = 1, expectedW = 6; const int primes[] = {5, 7, 11, 13, 17, 19, 23, 29}; const int maxima[] = {12, 30, 42, 66, 108, 150, 204, 258}; int maximum = 0, reference = 6; for (int j = 0; j < 8 && primes[j] <= t->top; j++) { expectedD *= primes[j] - 2; expectedW *= primes[j]; reference = maxima[j]; } for (uint64_t i = 0; i < t->D; i++) { sum += 6 * t->g[i]; if (6 * t->g[i] > maximum) maximum = 6 * t->g[i]; } require(t->D == expectedD && t->W == expectedW && sum == t->W, "tile census/width/gap mass"); require(maximum == reference, "tile G2 reference"); } /* Method A starts from a live left endpoint. Only two phases can kill its next slot. Endpoint conditions give each maximal dead run exactly once. */ static void windows(const Tile *t, int q, Census *c) { uint64_t left = t->first; int residues[256]; for (int b = 0; b < 256; b++) residues[b] = (6 * b) % q; for (uint64_t i = 0; i < t->D; i++) { int b = gap(t, i), m = residues[b], r0 = 0; while (killed(r0, q) || killed((r0 + m) % q, q)) r0++; add(c, 0, b, nu0(m, q), left, r0); for (int stateIndex = 0; stateIndex < 2; stateIndex++) { int state = stateIndex ? q - 2 : 0; int r = (state - m + q) % q; if (killed(r, q)) continue; int L = 1, span = b; for (;;) { int next = gap(t, i + L); span += next; state = (state + residues[next]) % q; if (!killed(state, q)) break; L++; require(L < LEVELS, "untruncated window exceeds declared cap"); } add(c, L, span, 1, left, r); } left += 6 * b; } } typedef struct { int L, span; } Run; /* Method B carries component lengths and weighted left-to-current distances. Closing a component adds its right flanking gap. An inert edge provides a valid cyclic cut; there is no linear-seam approximation. */ static void graph(const Tile *t, int q, Census *c) { uint64_t cut = 0; for (; cut < t->D; cut++) { int m = (6 * gap(t, cut)) % q; if (m != 0 && m != 2 && m != q - 2) break; } require(cut < t->D, "no inert edge; cyclic component case needs separate treatment"); Run a = {1, gap(t, cut)}, b = a; for (uint64_t step = 0; step < t->D; step++) { int g = gap(t, cut + 1 + step), m = (6 * g) % q; add(c, 0, g, nu0(m, q), 0, 0); Run fresh = {1, g}; if (m == 0) { a.L++; a.span += g; b.L++; b.span += g; } else if (m == 2) { add(c, a.L, a.span + g, 1, 0, 0); a = (Run){b.L + 1, b.span + g}; b = fresh; } else if (m == q - 2) { add(c, b.L, b.span + g, 1, 0, 0); b = (Run){a.L + 1, a.span + g}; a = fresh; } else { add(c, a.L, a.span + g, 1, 0, 0); add(c, b.L, b.span + g, 1, 0, 0); a = fresh; b = fresh; } require(a.L < LEVELS && b.L < LEVELS, "graph exceeds declared cap"); } } static void compare(const Census *a, const Census *b) { require(memcmp(a->h, b->h, sizeof(a->h)) == 0, "histogram disagreement"); } static void conservation(const Tile *t, int q, const Census *c) { uint64_t nodes = 0, gaps = 0, mass = 0; for (int L = 0; L < LEVELS; L++) { nodes += L * c->count[L]; gaps += c->count[L]; for (int b = 1; b < BINS; b++) mass += 6ULL * b * c->h[L][b]; } require(nodes == 2 * t->D, "dead node conservation"); require(gaps == (q - 2) * t->D, "new gap census"); require(mass == q * t->W, "new gap mass"); } /* Independent literal folding enumerates all q copies and actual slot values. It records each live-to-live gap and its intervening dead-slot count. */ static Tile fold(const Tile *t, int q, Census *direct) { Tile next = {q, t->D * (q - 2), t->W * q, 0, NULL}; next.g = allocate(next.D); uint64_t n = 0, first = 0, last = 0; int started = 0, dead = 0, leading = 0; for (int k = 0; k < q; k++) { uint64_t value = t->first + k * t->W; for (uint64_t i = 0; i < t->D; i++) { int r = value % q; if (killed(r, q)) dead++; else { if (!started) { first = value; leading = dead; started = 1; } else { uint64_t span = value - last; require(span % 6 == 0 && span / 6 <= 255 && n < next.D, "literal gap storage"); next.g[n++] = span / 6; add(direct, dead, span / 6, 1, last, last % q); } last = value; dead = 0; } value += 6 * t->g[i]; } } uint64_t span = first + next.W - last; require(span % 6 == 0 && span / 6 <= 255 && n < next.D, "literal cyclic gap storage"); next.g[n++] = span / 6; add(direct, dead + leading, span / 6, 1, last, last % q); require(n == next.D, "literal census"); next.first = first; validate_tile(&next); return next; } static void print_census(const Tile *t, int q, const Census *c, int literal) { int inverse = 1; while ((t->W % q) * inverse % q != 1) inverse++; printf("{\"old_top\":%d,\"q\":%d,\"D_old\":%llu,\"W_old\":%llu,\"literal_control\":%s,\"maxL\":%d,\"classes\":[", t->top, q, (unsigned long long)t->D, (unsigned long long)t->W, literal ? "true" : "false", c->maxL); for (int L = 0; L <= c->maxL; L++) { if (L) printf(","); int k = ((c->residue[L] - (int)(c->left[L] % q) + q) % q) * inverse % q; uint64_t left = (c->left[L] + k * t->W) % (q * t->W); printf("{\"L\":%d,\"count\":%llu,\"max_span\":%d,\"max_count\":%llu,\"witness_left\":%llu,\"histogram\":[", L, (unsigned long long)c->count[L], 6 * c->maximum[L], (unsigned long long)c->h[L][c->maximum[L]], (unsigned long long)left); int firstBin = 1; for (int b = 1; b < BINS; b++) if (c->h[L][b]) { if (!firstBin) printf(","); firstBin = 0; printf("[%d,%llu]", 6 * b, (unsigned long long)c->h[L][b]); } printf("]}"); } printf("]}"); } int main(int argc, char **argv) { require(argc == 2 && (!strcmp(argv[1], "small") || !strcmp(argv[1], "target")), "mode small or target"); int small = !strcmp(argv[1], "small"); Tile t = {3, 1, 6, 5, allocate(1)}; t.g[0] = 1; Census *a = allocate(sizeof(Census)), *b = allocate(sizeof(Census)), *direct = allocate(sizeof(Census)); const int primes[] = {5, 7, 11, 13, 17, 19, 23, 29, 31}; printf("{\"job_id\":739,\"mode\":\"%s\",\"folds\":[", argv[1]); int first = 1; for (int pi = 0; pi < 9; pi++) { int q = primes[pi]; memset(a, 0, sizeof(*a)); memset(b, 0, sizeof(*b)); memset(direct, 0, sizeof(*direct)); if (t.top >= 5) { windows(&t, q, a); graph(&t, q, b); compare(a, b); conservation(&t, q, a); } if (q == 31) { const uint64_t expected[] = {0, 413380422, 7999018, 12992, 4}; require(a->maxL == 4, "accepted 161 maximum length"); for (int L = 1; L <= 4; L++) require(a->count[L] == expected[L], "accepted 161 spectrum"); int maximum = 0; for (int L = 0; L <= a->maxL; L++) if (a->maximum[L] > maximum) maximum = a->maximum[L]; require(maximum * 6 == 348, "current G2(T31) reference"); require(t.D == 214708725ULL && t.D * (q - 2) == 6226553025ULL, "accepted 162 census references"); if (!first) printf(","); print_census(&t, q, a, 0); break; } Tile next = fold(&t, q, direct); if (t.top >= 5) { compare(a, direct); if (!first) printf(","); first = 0; print_census(&t, q, a, 1); } free(t.g); t = next; if (small && q == 23) break; } printf("],\"all_histogram_and_conservation_gates_passed\":true}\n"); free(t.g); free(a); free(b); free(direct); return 0; }