#!/usr/bin/env python3 """ Independent verifier for job #2043's finite claims (run bf4-911665bf27f8081b1e). Consumes the target `check-2043.out.json` and checks every claim it contains against the OEIS definition of A144311 verbatim: there exist L consecutive integers, each equal to 1 or -1 modulo at least one of the first n primes (n = 2..28, primes 2..107). The verifier does NOT trust the producer's offset: it recomputes x from the claimed residues by solving x = r_i (mod p_i) from scratch, requires that x to equal the x recorded in the target, scans every admissible start offset in the producer's own window, finds a covering window if one exists, and then re-derives the same window integer by integer. stdlib only; nothing of the producer is imported except the primes. Claims checked * `levels.calibration[n]` with `covered` true: a cover of R_target; * `levels.calibration[n]`: the run `best_run_in_state` of `best_run_residues`; * `levels.n{23..28}`: the run `best_run_in_best_state` of `residues`. Usage python3 verify-2043.py --target check-2043.out.json [--scan 600] [--selftest] --selftest corrupts one residue of the first claim; the verifier MUST then report that claim as failed (negative control). Exit 0 iff every claim verifies and, under --selftest, the corrupted copy is caught. One JSON object on stdout, nothing else. """ import argparse import json import sys PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107] def classes(p): return (1,) if p == 2 else (1, p - 1) def crt(residues): """x = residues[i] (mod PRIMES[i]), 0 <= x < prod; returns (x, modulus).""" x, W = 0, 1 for p, r in zip(PRIMES, residues): t = ((r - x) * pow(W % p, -1, p)) % p x += W * t W *= p return x % W, W def check_claim(n, residues, L, start_claimed, x_claimed, scan=600): """Find a covered window of length L and re-derive it integer by integer.""" residues = [int(v) for v in residues[:n]] problems = [] if len(residues) != n: return None, ["residue_count %d != n %d" % (len(residues), n)] x, _W = crt(residues) if x_claimed is not None and int(x_claimed) != x: problems.append("crt_mismatch: recomputed x = %d, target records %s" % (x, x_claimed)) if L <= 0: return x, problems + ["non-positive length %d" % L] span = int(scan) + L P = PRIMES[:n] cls = [classes(p) for p in P] covered = [False] * span for i in range(span): for k in range(n): if (residues[k] + i) % P[k] in cls[k]: covered[i] = True break # sliding window of length L: find a start with no uncovered offset holes = 0 for i in range(L): if not covered[i]: holes += 1 found = 0 if holes == 0 else None for s in range(1, span - L + 1): if not covered[s - 1]: holes -= 1 if not covered[s + L - 1]: holes += 1 if holes == 0: found = s break if found is None: idx = [i for i in range(span) if not covered[i]][:4] problems.append("no covered window of length %d found in %d offsets (first hole " "offsets %s; window starts scanned 0..%d)" % (L, span, idx, span - L)) return x, problems if start_claimed is not None and int(start_claimed) != found: problems.append("note: producer recorded start %s, verifier found %d; both are " "checks of the same residue tuple, not a disagreement" % (start_claimed, found)) # integer-by-integer re-derivation of the window the verifier itself found bad = [] for j in range(x + found, x + found + L): if not any(j % p == 1 or j % p == p - 1 for p in P): bad.append(j) if len(bad) > 3: break if bad: problems.append("integer-by-integer failed in the window found: %s" % bad[:4]) return x, problems, found def collect(cal, levels, claims): for level in sorted(cal, key=lambda s: int(s)): rec = cal[level] if rec.get("covered") and rec.get("residues"): claims.append({"id": "calibration n=%s cover of %s" % (level, rec.get("R_target")), "n": int(level), "residues": rec["residues"], "L": int(rec["R_target"]), "start": rec.get("window_start"), "x": rec.get("x")}) if rec.get("best_run_in_state") and rec.get("best_run_residues"): claims.append({"id": "calibration n=%s best run %s" % (level, rec["best_run_in_state"]), "n": int(level), "residues": rec["best_run_residues"], "L": int(rec["best_run_in_state"]), "start": rec.get("best_run_start"), "x": rec.get("best_run_x")}) for level in ("n23", "n24", "n25", "n26", "n27", "n28"): rec = levels.get(level) if not rec: continue if rec.get("best_run_in_best_state") and rec.get("residues"): claims.append({"id": "%s best run %s" % (level, rec["best_run_in_best_state"]), "n": int(level[1:]), "residues": rec["residues"], "L": int(rec["best_run_in_best_state"]), "start": rec.get("window_start"), "x": rec.get("x")}) def run_claim(c, scan): out = check_claim(c["n"], c["residues"], c["L"], c.get("start"), c.get("x"), scan) if len(out) == 2: x, problems = out found = None else: x, problems, found = out hard = [p for p in problems if not p.startswith("note:")] return {"claim": c["id"], "n": c["n"], "L": c["L"], "window_start_found": found, "passed": not hard, "problems": problems, "x_head": str(x)[:24]} def main(): ap = argparse.ArgumentParser() ap.add_argument("--target", required=True) ap.add_argument("--scan", type=int, default=600) ap.add_argument("--selftest", action="store_true") args = ap.parse_args() with open(args.target, encoding="utf-8") as fh: d = json.load(fh) levels = d.get("levels") or {} claims = [] collect(levels.get("calibration") or {}, levels, claims) results = [run_claim(c, args.scan) for c in claims] ok = all(r["passed"] for r in results) control = None if args.selftest and claims: c = dict(claims[0]) r = list(c["residues"]) r[0] = (r[0] + 1) % PRIMES[0] c["residues"] = r c["x"] = None res = run_claim(c, args.scan) control = {"claim": c["id"], "corrupted_index": 0, "caught": not res["passed"], "problems": res["problems"][:2]} ok = ok and bool(control["caught"]) print(json.dumps({"tool": "verify-2043.py", "target": args.target, "scan_offsets": args.scan, "claims": len(claims), "passed": sum(1 for r in results if r["passed"]), "ok": bool(ok), "results": results, "negative_control": control}, sort_keys=True)) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())