/* * exactness-ladder.mjs — the fold-local exactness verdict, emitted by the same pass that computes M, * at every reachable fold. One row per fold: M, the truth, the local test's verdict, the window interior * run, and the phantom check. * * PRIOR ART, NAMED FIRST. The criterion below is not new. `research/history/staging/verify-tailcount-transport.md` * (2026-08-19), section (c), separates three certificates at folds 11..31: * M_loose (the record's Q_L: interiors qualify mod q), M_alt (+ the interiors' walk stays inside a 2-set * {a-2, a}), M_full (+ both endpoints live), tabling M_loose = 42, 66, 108, 150, 204, 270, 348 and * M_alt = 42, 66, 108, 150, 204, 258, 348 against a truth of the same values, and proposing M_alt as a * zero-cost correction the instrument should take. It also states that the alternation constraint "is the * only one that has ever cost anything (12 units at fold 29)". What is added here is the instrument, not * the criterion: M and the verdict emitted by the same pass, at every fold in reach including the two * above that table (31 -> 37 and 37 -> 41), one scalar in place of the walk existence test, and the * phantom check below. * * THE QUESTION. The certificate at a fold p -> q is M = max over windows of T_p of the window sum, over the * windows whose interiors qualify mod q. The record's own columns are: * - M, the certificate (a restricted maxsum over the OLD word); * - the truth, the maximal gap of the NEW word (which the alignment sum realises when the merge exists); * - the local test, whether the windows attaining M are realisable (nu_q > 0, the alignment count). * This script emits those and one more statistic, so a fold decides its own exactness rather than needing a * full scan of the word: * - the interior run: for the windows attaining M, the longest block of consecutive interior residues * whose centred step (mod q) is non-zero and of one sign. The candidate criterion is run <= 1. * It also answers the phantom question exhaustively where M exceeds the truth: how many windows carry a sum * above the truth, and how many of those are realisable (must be 0, else the truth is understated). * * WHAT IS COMPUTED HERE AND WHAT IS READ. Folds 11 -> 13 through 29 -> 31 are computed here, in one pass * each, on tiles built from first principles. The folds 31 -> 37 and 37 -> 41 are READ from the stdout of * the producers that already ran them (same Certifier code, one pass each: 241 s and 1,808 s respectively), * because regenerating them here would cost 34 minutes again; their sha256 is checked before anything is * printed. The truth column at the computed folds is the NEW tile's maximal gap, computed, not cited. * * modes: ladder (default) folds through 29 -> 31 computed, the two big folds read * ladder-big the same plus a fresh 31 -> 37 pass (about 4 minutes) * Deterministic; stdout is the artifact, progress goes to stderr. Node >= 18, stdlib only. */ import fs from 'node:fs'; import crypto from 'node:crypto'; const P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]; const prim = (p) => P.filter((r) => r <= p).reduce((a, r) => a * r, 1); const CAP = 128; const MASK = 511; const sha = (f) => crypto.createHash('sha256').update(fs.readFileSync(f)).digest('hex'); function slotsSmall(p) { const W = prim(p), rs = P.filter((r) => r <= p); const cop = new Uint8Array(W).fill(1); for (const r of rs) for (let x = r; x < W; x += r) cop[x] = 0; cop[0] = 0; const out = new Uint32Array(W); let n = 0; for (let x = 1; x < W; x++) if (cop[x] && cop[(x + 2) % W]) out[n++] = x; return out.subarray(0, n); } function foldToSlots(slots, Wp, q) { const Wq = Wp * q, want = Math.round(slots.length * (q - 2)); const out = new Float64Array(want); const r = new Uint8Array(slots.length); for (let i = 0; i < slots.length; i++) r[i] = slots[i] % q; const WpMod = Wp % q; let n = 0, prev = -1, first = -1, maxg = 0; for (let k = 0; k < q; k++) { const base = k * Wp; const e1 = (q - ((k * WpMod) % q)) % q, e2 = (q - (((k * WpMod) + 2) % q)) % q; for (let i = 0; i < slots.length; i++) { if (r[i] === e1 || r[i] === e2) continue; const x = base + slots[i]; if (first < 0) first = x; if (prev >= 0) { const g = x - prev; if (g > maxg) maxg = g; } prev = x; if (n < want) out[n++] = x; } } { const g = first + Wq - prev; if (g > maxg) maxg = g; } return { slots: out.subarray(0, n), D: n, maxGap: maxg, W: Wq }; } function gapsOf(p) { const W = prim(p), r = slotsSmall(p), n = r.length; const g = new Uint16Array(n); for (let i = 0; i < n; i++) g[i] = (i + 1 < n ? r[i + 1] : r[0] + W) - r[i]; return g; } /* the interior run: centred consecutive steps of the interior residues, longest block non-zero and one sign */ function interiorRun(res) { let best = 0, cur = 0, sign = 0; for (let j = 1; j < res.length; j++) { let d = res[j] - res[j - 1]; const q = interiorRun.q; d = ((d + Math.floor(q / 2)) % q + q) % q - Math.floor(q / 2); // centred representative in (-q/2, q/2] if (d === 0) { cur = 0; sign = 0; continue; } const s = d > 0 ? 1 : -1; cur = s === sign ? cur + 1 : 1; sign = s; if (cur > best) best = cur; } return best; } class Certifier { constructor(qCert, truth) { this.q = qCert; this.truth = truth; this.buf = new Float64Array(MASK + 1); this.pos = 0; this.D = 0; this.maxg = 0; this.M = 0; this.hits = 0; this.arg = []; this.lAtCap = 0; this.head = new Float64Array(CAP + 3); this.headN = 0; this.above = 0; this.aboveReal = 0; this.examples = []; } ring(j) { return this.buf[(this.pos - 1 - j + MASK + 1) & MASK]; } qual(x) { const r = x % this.q; return r === 0 || r === 2 || r === this.q - 2; } consider(i, L, s, seam) { if (L >= CAP) this.lAtCap++; if (this.truth && s > this.truth) { // the phantom check: exhaustive, this is the only place windows are seen this.above++; const gs = []; for (let j = L; j >= 0; j--) gs.push(this.ring(j)); const nu = this.nu(gs); if (nu > 0) { this.aboveReal++; if (this.examples.length < 4) this.examples.push({ i, L, gs, nu, sum: s }); } } if (s > this.M) { this.M = s; this.hits = 0; this.arg = []; } if (s === this.M) { this.hits++; if (this.arg.length < 8) { const gs = []; for (let j = L; j >= 0; j--) gs.push(this.ring(j)); this.arg.push({ i, L, gs, seam }); } } } scan(i, seam) { const inc = seam ? (L) => L > i : (L) => L <= i; let s = this.ring(1) + this.ring(0); if (inc(1)) this.consider(i, 1, s, seam); for (let L = 2; L <= CAP; L++) { if (!this.qual(this.ring(L - 1))) break; s += this.ring(L); if (inc(L)) this.consider(i, L, s, seam); } } push(g) { const i = this.D; this.buf[this.pos++ & MASK] = g; this.D++; if (g > this.maxg) this.maxg = g; if (this.headN <= CAP + 2) this.head[this.headN++] = g; this.scan(i, false); } replay() { for (let j = 0; j < this.headN; j++) { this.buf[this.pos++ & MASK] = this.head[j]; this.scan(j, true); } } nu(gs) { const G = []; let s = 0; for (const v of gs) { s += v; G.push(s); } const L = gs.length - 1; let c = 0; for (let a = 0; a < this.q; a++) { if (a === 0 || a === 2) continue; if (a === G[L] % this.q || a === (G[L] + 2) % this.q) continue; let ok = true; for (let j = 0; j < L; j++) { const rr = G[j] % this.q; if (rr !== a && rr !== (a - 2 + this.q) % this.q) { ok = false; break; } } if (ok) c++; } return c; } rows() { interiorRun.q = this.q; return this.arg.map((h) => { const res = []; let acc = 0; for (let j = 0; j < h.L; j++) { acc += h.gs[j]; res.push(acc % this.q); } const nu = this.nu(h.gs); return { i: h.i, L: h.L, gs: h.gs, res, nu, run: interiorRun(res) }; }); } } function certifyArray(gaps, qCert, label, truth) { const c = new Certifier(qCert, truth); for (let i = 0; i < gaps.length; i++) c.push(gaps[i]); c.replay(); return { D: c.D, maxg: c.maxg, M: c.M, hits: c.hits, above: c.above, aboveReal: c.aboveReal, examples: c.examples, rows: c.rows(), label }; } /* ---------- the ladder ---------- */ const mode = process.argv[2] ?? 'ladder'; console.log(`# exactness-ladder.mjs mode=${mode}, CAP=${CAP}`); console.log(''); const rows = []; const push = (fold, p, q, r, truth, truthSrc) => { const real = r.rows.filter((w) => w.nu > 0).length; rows.push({ fold, p, q, M: r.M, truth, truthSrc, hits: r.hits, shown: r.rows.length, real, run: Math.max(0, ...r.rows.map((w) => w.run)), D: r.D, maxg: r.maxg, above: r.above, aboveReal: r.aboveReal, examples: r.examples, ws: r.rows }); }; // computed folds 11 -> 13 ... 29 -> 31 const S = {}; S[11] = slotsSmall(11); S[13] = slotsSmall(13); S[17] = slotsSmall(17); S[19] = slotsSmall(19); S[23] = slotsSmall(23); S[29] = foldToSlots(S[23], prim(23), 29); const F = [[11, 13], [13, 17], [17, 19], [19, 23], [23, 29], [29, 31]]; for (const [p, q] of F) { const g = p <= 23 ? gapsOf(p) : (() => { const a = new Uint16Array(S[29].D); for (let i = 0; i < S[29].D; i++) a[i] = (i + 1 < S[29].D ? S[29].slots[i + 1] : S[29].slots[0] + prim(29)) - S[29].slots[i]; return a; })(); // the new tile is materialised only while it fits: T_31 would be 6.2e9 slots (50 GB as Float64), so the // fold 29 -> 31 takes the record's own maximal gap as its truth instead of building the tile. let truth, truthSrc; if (q <= 29) { const nw = foldToSlots(p <= 23 ? S[p] : S[29], prim(p), q); truth = nw.maxGap; truthSrc = "computed (the new tile's maximal gap)"; } else { truth = 348; truthSrc = 'record (T_31 maximal gap; T_31 is 50 GB to materialise here)'; } const r = certifyArray(g, q, `fold ${p}# -> ${q}#`, truth); push(`${p} -> ${q}`, p, q, r, truth, truthSrc); process.stderr.write(` fold ${p} -> ${q} done: M = ${r.M}, truth = ${truth}\n`); } // fold 31 -> 37: read from the producer that ran it (or recompute with mode ladder-big) const BIG_SHA = '765281abaef1c86dc3f290ec49fb943d57ad07ed818e3ffbd4d451ba0b516708'; // streamfold-big.out (#254) if (mode === 'ladder-big') { const s31 = S[29]; const c = new Certifier(37, 528); const q = 31, Wp = prim(29), Wq = Wp * q; const r = new Uint8Array(s31.slots.length); for (let i = 0; i < s31.slots.length; i++) r[i] = s31.slots[i] % q; const WpMod = Wp % q; let prev = -1, first = -1; for (let k = 0; k < q; k++) { const base = k * Wp; const e1 = (q - ((k * WpMod) % q)) % q, e2 = (q - (((k * WpMod) + 2) % q)) % q; for (let i = 0; i < s31.slots.length; i++) { if (r[i] === e1 || r[i] === e2) continue; const x = base + s31.slots[i]; if (first < 0) first = x; if (prev >= 0) c.push(x - prev); prev = x; } process.stderr.write(` k=${k + 1}/${q}\n`); } c.push(first + Wq - prev); c.replay(); const rr = { D: c.D, maxg: c.maxg, M: c.M, hits: c.hits, above: c.above, aboveReal: c.aboveReal, examples: c.examples, rows: c.rows(), label: 'fold 31# -> 37#' }; console.log(`GATE T_31: D = ${rr.D} (record 6226553025) ${rr.D === 6226553025 ? 'PASS' : 'FAIL'}, max gap = ${rr.maxg} (record 348) ${rr.maxg === 348 ? 'PASS' : 'FAIL'}`); push('31 -> 37', 31, 37, rr, 528, 'record (T_37 maximal gap, computed by the #254 pass)'); } else { const f = '../job617/streamfold-big.out'; if (sha(f) !== BIG_SHA) throw new Error(`${f}: sha256 moved, refusing to read it`); const t = fs.readFileSync(f, 'utf8'); const block = t.slice(t.indexOf('--- fold 31# -> 37#')); const M = +block.match(/certificate M = (\d+)/)[1]; const ws = [...block.matchAll(/i = \s*(\d+) L = (\d+) sum = (\d+) = ([\d+]+) nu_q = (\d+) interiors mod 37 \[([\d,]+)\]/g)] .map((m) => ({ i: +m[1], L: +m[2], gs: m[4].split('+').map(Number), nu: +m[5], res: m[6].split(',').map(Number) })); interiorRun.q = 37; for (const w of ws) w.run = interiorRun(w.res); push('31 -> 37', 31, 37, { D: 6226553025, maxg: 348, M, hits: 2, above: 0, aboveReal: 0, examples: [], rows: ws, label: 'fold 31# -> 37#' }, 528, `record (${f} sha256 ${BIG_SHA.slice(0, 8)}…)`); } // fold 37 -> 41: read from the parallel pass (#266) const PAR_SHA = '49d0c8b0b500690577d3a8bf0a385f96bd0f6d0bcf1554b8d7258439afa9d968'; if (sha('parfold-big.out') !== PAR_SHA) throw new Error('parfold-big.out: sha256 moved, refusing to read it'); { const t = fs.readFileSync('parfold-big.out', 'utf8'); const M = +t.match(/certificate M = (\d+)/)[1]; const ws = [...t.matchAll(/i = \s*(\d+) L = (\d+) sum = (\d+) = ([\d+]+) nu_q = (\d+) interiors mod 41 \[([\d,]+)\]/g)] .map((m) => ({ i: +m[1], L: +m[2], gs: m[4].split('+').map(Number), nu: +m[5], res: m[6].split(',').map(Number) })); interiorRun.q = 41; for (const w of ws) w.run = interiorRun(w.res); push('37 -> 41', 37, 41, { D: 217929355875, maxg: 528, M, hits: 4, above: 0, aboveReal: 0, examples: [], rows: ws, label: 'fold 37# -> 41#' }, 546, `record (parfold-big.out sha256 ${PAR_SHA.slice(0, 8)}…)`); } /* ---------- the emission ---------- */ console.log('| fold | M | truth | exact (M = truth) | local test (realisable) | interior run | phantom check (windows above truth / realisable) |'); console.log('|---|---|---|---|---|---|---|'); for (const r of rows) { const exact = r.M === r.truth; console.log(`| ${r.fold} | ${r.M} | ${r.truth} | ${exact ? 'yes' : '**no**'} | ${r.real}/${r.shown} | ${r.run} | ${r.above} / ${r.aboveReal} |`); } console.log(''); for (const r of rows) { console.log(`--- fold ${r.fold} (q = ${r.q}) old word D = ${r.D}, max gap ${r.maxg}`); console.log(`certificate M = ${r.M}; truth = ${r.truth} [${r.truthSrc}]; argmax windows ${r.hits} (${r.shown} shown)`); for (const w of r.ws) console.log(` i = ${String(w.i).padStart(12)} L = ${w.L} sum = ${w.M ?? r.M} = ${w.gs.join('+')} nu_q = ${w.nu} interiors mod ${r.q} [${w.res}] run = ${w.run}`); if (r.above) for (const e of r.examples) console.log(` ABOVE TRUTH: i = ${e.i} sum = ${e.sum} = ${e.gs.join('+')} nu_q = ${e.nu}`); console.log(''); } /* ---------- where the two verdicts disagree ---------- */ const dis = rows.filter((r) => (r.M === r.truth) !== (r.run <= 1)); console.log('# where the window interior test and the truth disagree'); if (!dis.length) { console.log(`none: at all ${rows.length} reachable folds, "certificate equals the truth" (${rows.filter((r) => r.M === r.truth).length} yes, ${rows.filter((r) => r.M !== r.truth).length} no) and "every argmax window has interior run <= 1" (${rows.filter((r) => r.run <= 1).length} yes, ${rows.filter((r) => r.run > 1).length} no) agree.`); } else { for (const r of dis) console.log(` fold ${r.fold}: M = ${r.M}, truth = ${r.truth}, run = ${r.run}`); } const phantomFail = rows.filter((r) => r.aboveReal > 0); console.log(`# phantom check: windows above the truth that are realisable: ${phantomFail.length ? phantomFail.map((r) => `${r.fold} (${r.aboveReal})`).join(', ') : '0 at every fold'} (must be 0, else the truth column is understated)`); const runsOver = rows.filter((r) => r.run > 1).map((r) => r.fold); console.log(`# folds with interior run > 1: ${runsOver.length ? runsOver.join(', ') : 'none'}`); console.log(`# VERDICT: ${dis.length === 0 ? 'the criterion and the truth agree at every reachable fold' : 'DISAGREEMENT above — the criterion is falsified at these folds'}`);