#!/usr/bin/env python3 """The `circuit` check: the circuits and lower-bound proofs a route hands in, verified exactly. Usage: verify.py FOLDER Only files directly in FOLDER (the route's folder) are judged: .ckt a circuit for function (checks/circuits.py: mod3__, sum, maj, thr_), checked on all 2^n inputs; it shows the function can be computed with that many gates .gt.lrat[.gz] an LRAT proof that no circuit of at most s gates computes : the check builds the CNF itself with the approved encoder (circuits.encode) and checks every step of the proof Other files are left alone; a .ckt or .lrat file whose name is not of this form is named as ignored. The last line is what the site shows on the commit, and the board reads it: each function the project tables (scienceslack.json `instances`) as ": ", in the table's order, with "(exact)" when a proof shows one gate fewer is impossible; other functions after it, by id. Lower bounds are written with the id, never with a table name, so the board never reads one as a circuit's size. Exit 0 with that line (or "Nothing to check yet"); exit 1 with the first thing wrong, the file and the line to fix. """ import json import re import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from circuits import Wrong, computes, encode, proof_lines, shown, unsat # noqa: E402 MAX_PROOF = 60_000_000 # bytes on disk; a larger proof is checked by the maintainers' own runner (docs/submission.md) CKT = re.compile(r"(mod3_\d+_[012]|sum\d+|maj\d+|thr\d+_\d+)\.ckt") LRAT = re.compile(r"(mod3_\d+_[012]|sum\d+|maj\d+|thr\d+_\d+)\.gt(\d+)\.lrat(\.gz)?") def table(): spec = json.loads((Path(__file__).resolve().parents[1] / "scienceslack.json").read_text(encoding="utf-8")) return spec.get("instances", []) def judge(folder): up, low, ignored = {}, {}, [] for f in sorted(p for p in folder.iterdir() if p.is_file()): if CKT.fullmatch(f.name): fid = f.name[:-4] up[fid] = computes(fid, f.read_text(encoding="utf-8", errors="replace"), f.name) elif m := LRAT.fullmatch(f.name): fid, s = m[1], int(m[2]) if f.stat().st_size > MAX_PROOF: raise Wrong(f"{f.name}: {f.stat().st_size // 1_000_000} MB, over this check's {MAX_PROOF // 1_000_000} MB; see docs/submission.md for larger proofs") nv, clauses, _ = encode(fid, s) unsat(clauses, proof_lines(f), f.name) low[fid] = max(low.get(fid, 0), s) elif f.suffix in (".ckt", ".lrat", ".gz"): ignored.append(f.name) return up, low, ignored def line(up, low): rows = table() said = [] for fid in sorted(set(up) | set(low), key=lambda x: (shown(x) not in rows, rows.index(shown(x)) if shown(x) in rows else 0, x)): exact = fid in up and low.get(fid) == up[fid] - 1 if shown(fid) in rows and fid in up: said.append(f"{shown(fid)}: {up[fid]}{' (exact)' if exact else ''}") elif fid in up: said.append(f"{fid} in {up[fid]} gates{' (exact)' if exact else ''}") if fid in low and not exact: said.append(f"size({fid}) > {low[fid]}") return "; ".join(said) def main(argv): if len(argv) != 1: sys.exit(__doc__) folder = Path(argv[0]) if not folder.is_dir(): print(f"Nothing to check yet: no folder {folder.name} on this commit (a route's files go in routes//).") return 0 t = time.time() try: up, low, ignored = judge(folder) except Wrong as e: print(f"FAILED: {e}", file=sys.stderr) print(f"FAILED: {e}") return 1 for name in ignored: print(f"ignored {name}: not .ckt or .gt.lrat[.gz]") if not up and not low: print("Nothing to check yet: no .ckt or .gt.lrat file directly in the route's folder.") return 0 print(f"checked {len(up)} circuit(s) and {len(low)} proof(s) in {time.time() - t:.2f} s") print(line(up, low)) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))