/* * streamfold.mjs — the certificate and its local alternation test, on the OLD word's gap stream. * * The transport predicts the new word from the old one, so the certificate at a fold p -> q is a * maximum over windows of T_p tested against the qualification g = 0, +-2 (mod q). For the fold * 31 -> 37 the old word T_31 has 6,226,553,025 gaps -- 6.2 GB as bytes, 12.5 GB as 16-bit, past the * 8 GB offered -- so it is generated and consumed streaming. The fold emits T_31's gaps in sorted * order (x = k*W_29 + s, ordered by (k, s)), a ring of the last CAP+1 gaps evaluates every window as * it arrives, and one replay of the first CAP+1 gaps -- the ring then holding the word's tail, so * negative indices resolve cyclically -- covers the wrap. Memory is O(CAP) for the certificate plus * the previous tile's slot array. * * tiles: W = z#, slots x with gcd(x,W) = gcd(x+2,W) = 1 * fold: x = k*W_p + s, keep unless x = 0 or x = -2 (mod q); the exclusion depends on s mod q only, * so the inner loop is two compares against precomputed residues. * * modes: "validate" (the fold at 13, 17, 19, 23, 29, 31 against the certificate table already on * the record) and "big" (generate T_31 from T_29, certify the fold 31 -> 37). * Deterministic; stdout is the artifact, progress goes to stderr. */ const P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]; const prim = (p) => P.filter((r) => r <= p).reduce((a, r) => a * r, 1); const CAP = 128; const MASK = 511; 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 }; } class Certifier { constructor(qCert) { this.q = qCert; 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; } ring(j) { return this.buf[(this.pos - 1 - j + MASK + 1) & MASK]; } // pos is post-increment: the newest gap sits at pos-1 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 (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; } report(label) { const nus = this.arg.map((h) => this.nu(h.gs)); const runs = this.arg.map((h) => { const o = []; let acc = 0; for (let j = 0; j < h.L; j++) { acc += h.gs[j]; o.push(acc % this.q); } return o.join(','); }); const real = nus.filter((v) => v > 0).length; console.log(`--- ${label} q_cert = ${this.q}`); console.log(`old word: D = ${this.D}, max gap = ${this.maxg}, longest L reaching CAP: ${this.lAtCap}`); console.log(`certificate M = ${this.M}; argmax windows ${this.hits} (${this.arg.length} shown)`); for (let k = 0; k < this.arg.length; k++) { const h = this.arg[k]; console.log(` i = ${String(h.i).padStart(10)} L = ${h.L} sum = ${this.M} = ${h.gs.join('+')} nu_q = ${nus[k]} interiors mod ${this.q} [${runs[k]}]${h.seam ? ' SEAM' : ''}`); } console.log(`LOCAL TEST: ${real}/${this.arg.length} argmax windows realizable => ${real === this.arg.length ? 'certificate EXACT at this fold' : 'certificate LOOSE here (phantom argmax)'}`); console.log(''); return { D: this.D, maxg: this.maxg, M: this.M, hits: this.hits, nus, real, arg: this.arg }; } } function gapsOf(p) { // materialised old word, small folds only 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; } function certifyArray(gaps, qCert, label) { const c = new Certifier(qCert); for (let i = 0; i < gaps.length; i++) c.push(gaps[i]); c.replay(); return c.report(label); } const mode = process.argv[2] ?? 'validate'; console.log(`# streamfold.mjs mode=${mode}, CAP=${CAP}`); console.log(''); if (mode === 'validate') { // [old word T_p, certification prime q, the record's certificate at q] const table = [[11, 13, 66], [13, 17, 108], [17, 19, 150], [19, 23, 204], [23, 29, 270]]; let ok = true; for (const [p, q, expM] of table) { const r = certifyArray(gapsOf(p), q, `fold ${p}# -> ${q}# (old word T_${p})`); const pass = r.M === expM; if (!pass) ok = false; console.log(` gate: M = ${r.M} (record ${expM}) ${pass ? 'PASS' : 'FAIL'}\n`); } // 29 -> 31: the old word T_29 generated from T_23 and materialised, then certified at 31 const f29 = foldToSlots(slotsSmall(23), prim(23), 29); console.log(` GATE T_29: D = ${f29.D} (expect 214708725) ${f29.D === 214708725 ? 'PASS' : 'FAIL'}, max gap = ${f29.maxGap} (expect 258) ${f29.maxGap === 258 ? 'PASS' : 'FAIL'}\n`); const g29 = new Uint16Array(f29.D); for (let i = 0; i < f29.D; i++) g29[i] = (i + 1 < f29.D ? f29.slots[i + 1] : f29.slots[0] + prim(29)) - f29.slots[i]; const r31 = certifyArray(g29, 31, 'fold 29# -> 31# (old word T_29)'); const p31 = r31.M === 348; if (!p31) ok = false; console.log(` gate: M = ${r31.M} (record 348) ${p31 ? 'PASS' : 'FAIL'}\n`); console.log(ok ? '# validate: all gates PASS' : '# validate: FAILURES above'); } else { const f29 = foldToSlots(slotsSmall(23), prim(23), 29); console.log(`GATE T_29: D = ${f29.D} (expect 214708725) ${f29.D === 214708725 ? 'PASS' : 'FAIL'}, max gap = ${f29.maxGap} (expect 258) ${f29.maxGap === 258 ? 'PASS' : 'FAIL'}`); console.log('generating T_31 and certifying the fold 31 -> 37; T_31 is never stored\n'); const c = new Certifier(37); const slots = f29.slots, q = 31, Wp = prim(29), Wq = Wp * q; const r = new Uint8Array(slots.length); for (let i = 0; i < slots.length; i++) r[i] = slots[i] % q; const WpMod = Wp % q; let prev = -1, first = -1, nSlots = 0; const t0 = Date.now(); 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) c.push(x - prev); prev = x; nSlots++; } process.stderr.write(` k=${k + 1}/${q} slots ${nSlots} elapsed ${((Date.now() - t0) / 1000).toFixed(1)}s\n`); } c.push(first + Wq - prev); c.replay(); const out = c.report('fold 31# -> 37# (old word T_31, generated, never stored)'); console.log(`GATE T_31: D = ${out.D} (expect 6226553025) ${out.D === 6226553025 ? 'PASS' : 'FAIL'}, max gap = ${out.maxg} (expect 348) ${out.maxg === 348 ? 'PASS' : 'FAIL'}`); console.log(`NEW: certificate at fold 37 M = ${out.M}; argmax windows ${out.hits}`); }