#!/usr/bin/env python3 from __future__ import annotations import argparse, gzip, hashlib, json from pathlib import Path def sha(path): h=hashlib.sha256() with path.open("rb") as f: for block in iter(lambda:f.read(1<<20),b""): h.update(block) return h.hexdigest() def load_mtx(path): with gzip.open(path,"rt",encoding="ascii") as f: header=f.readline().strip() assert header=="%%MatrixMarket matrix coordinate integer general",(path,header) line=f.readline() while line.startswith("%"): line=f.readline() rows,cols,nnz=map(int,line.split()); out=[[0]*cols for _ in range(rows)] seen=0 for line in f: if not line.strip(): continue r,c,v=map(int,line.split()); assert v==1 out[r-1][c-1]^=1; seen+=1 assert seen==nnz return out def rank(a): a=[sum((v&1)<12*n for key in ("lower_bound","upper_bound","exact"): d=row["distance"][key]; f=row["fom"][key] assert (d is None)==(f is None) if d is not None: assert abs(f-k*d*d/n)<1e-12 proof=proofs[cid] witnesses=[] report=proof.get("logical_basis_upper_bound_evidence") or {} if isinstance(report.get("witness"),dict): witnesses.append(report["witness"]) audit=proof.get("trusted_audit") or {} if isinstance(audit.get("symplectic_weight_witness"),dict): witnesses.append(audit["symplectic_weight_witness"]) oracle=proof.get("formal_lower_bound_evidence") or {} if oracle.get("outcome")=="SAT" and isinstance(oracle.get("witness"),dict): witnesses.append(oracle["witness"]) for witness in witnesses: bits=witness.get("bits"); side=witness.get("side") or witness.get("sector") if not isinstance(bits,list): continue assert len(bits)==n and sum(bits)==int(witness["weight"]) if side=="X": assert all(parity(row,bits)==0 for row in hz) and not in_row_space(hx,bits) elif side=="Z": assert all(parity(row,bits)==0 for row in hx) and not in_row_space(hz,bits) manifest=json.loads((root/"manifests/selection.json").read_text()) assert manifest["selected_candidates"]==len(candidates) assert manifest["certified_fom_gt_12_count"]==sum(r["target"]["certified_win"] for r in candidates.values()) print(f"PASS: {checked} file hashes, {len(candidates)} candidates, all matrices and witnesses verified") if __name__=="__main__": p=argparse.ArgumentParser(); p.add_argument("root",type=Path,nargs="?",default=Path(".")); a=p.parse_args(); main(a.root.resolve())