#!/usr/bin/env python3 """k=46 shard-level validation of the recorded capped-Gram functionals. For one *pair* witness `e_i + e_j` (cheap at k=46, unlike a full witness) this compares the recorded per-`(region, r)` slot functionals against the direct contraction: * numerator: `sum over all numerator shards of W(sig_pair)` vs `sum over C,D,E of correction_compact(region, ..., pair signature)`; * denominator: `sum over all wden shards of WD(SF2_pair)` vs `violation_F2(k, d, U, caps, SF2_pair)`. Bit-exact rational equality is required. This validates the k=46 slot coverage of `capped_gram` on its own, independently of the small-k gate. python3 tests/test_gram_shard_k46.py [i j] """ from __future__ import annotations import os import pickle import sys from fractions import Fraction as Fr HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "src")) sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "lib", "maynard")) from capped_gram import denominator_value, numerator_value # noqa from capped_numerator import violation_F2 # noqa from compact_contract import correction_compact # noqa from even_engine import EvenEngine # noqa A_SRC = Fr(2583, 10000) def main(): i = int(sys.argv[1]) if len(sys.argv) > 1 else 373 j = int(sys.argv[2]) if len(sys.argv) > 2 else 372 k, eps, dvec = 46, Fr(25, 861), 17 U, ell = 1 + eps, 1 - eps d = Fr(3, 250) / A_SRC caps = [Fr(0)] + [Fr(3, 20) / A_SRC] * 2 + [Fr(4, 25) / A_SRC] * (k + 2) e = EvenEngine(k, eps, dvec, "exact") c = [Fr(0)] * e.n c[i] = Fr(1) c[j] = Fr(1) from capped_gram import signature_parts sG2, sGT, sT2, SF2 = signature_parts(e, c) print(f"pair ({i},{j}): slots {len(sG2)},{len(sGT)},{len(sT2)} " f"SF2={len(SF2)}", file=sys.stderr, flush=True) sharddir = os.path.join(HERE, "..", "out", "gram_shards_k46") def conv(v): return v if isinstance(v, Fr) else Fr(int(v.p), int(v.q)) W = {} for f in sorted(os.listdir(sharddir)): if f.startswith("wnum_"): for key, val in pickle.load(open(os.path.join(sharddir, f), "rb")).items(): W[key] = W.get(key, Fr(0)) + conv(val) WD = {} for f in sorted(os.listdir(sharddir)): if f.startswith("wden_"): for key, val in pickle.load(open(os.path.join(sharddir, f), "rb")).items(): WD[key] = WD.get(key, Fr(0)) + conv(val) W = {kk: v for kk, v in W.items() if v} WD = {kk: v for kk, v in WD.items() if v} got = numerator_value(W, sG2, sGT, sT2) dirn = sum(correction_compact(K, k, d, U, ell, caps, sG2, sGT, sT2) for K in ("C", "D", "E")) gotD = denominator_value(WD, SF2) dirD = violation_F2(k, d, U, caps, SF2) ok = (got == dirn) and (gotD == dirD) print(f"numerator: W={float(got):+.12e} direct={float(dirn):+.12e} " f"equal={got == dirn}", file=sys.stderr, flush=True) print(f"denominator: W={float(gotD):+.12e} direct={float(dirD):+.12e} " f"equal={gotD == dirD}", file=sys.stderr, flush=True) print("GRAM-SHARD", "PASS" if ok else "FAIL") return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())