File size: 4,656 Bytes
c760ceb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/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)<<j for j,v in enumerate(row)) for row in a]
    out=0
    while a:
        pivot=max(a)
        if not pivot: break
        bit=1<<(pivot.bit_length()-1); out+=1
        a=[x^pivot if x&bit else x for x in a if x!=pivot]
    return out

def content_sha(a):
    shape=[len(a),len(a[0]) if a else 0]
    header=json.dumps({"dtype":"uint8-gf2","shape":shape},sort_keys=True,separators=(",",":"))
    raw=bytes(v&1 for row in a for v in row)
    return hashlib.sha256(header.encode()+b"\n"+raw).hexdigest()

def in_row_space(a,bits): return rank(a+[bits])==rank(a)
def parity(row,bits): return sum(x&y for x,y in zip(row,bits))&1

def main(root):
    sums=root/"manifests/SHA256SUMS"; checked=0
    for line in sums.read_text().splitlines():
        expected,rel=line.split("  ",1); path=root/rel
        assert sha(path)==expected,(rel,"file sha mismatch"); checked+=1
    candidates={}
    for line in (root/"data/candidates.jsonl").read_text().splitlines():
        row=json.loads(line); cid=row["candidate_id"]; assert cid not in candidates; candidates[cid]=row
    proofs={json.loads(line)["candidate_id"]:json.loads(line) for line in (root/"evidence/proofs.jsonl").read_text().splitlines()}
    assert candidates.keys()==proofs.keys()
    for cid,row in candidates.items():
        mx=row["matrices"]; hx=load_mtx(root/mx["hx_path"]); hz=load_mtx(root/mx["hz_path"])
        assert sha(root/mx["hx_path"])==mx["hx_file_sha256"]
        assert sha(root/mx["hz_path"])==mx["hz_file_sha256"]
        assert content_sha(hx)==mx["hx_content_sha256"]
        assert content_sha(hz)==mx["hz_content_sha256"]
        n=row["parameters"]["n"]; assert len(hx[0])==len(hz[0])==n
        rx,rz=rank(hx),rank(hz); assert rx==row["matrix_checks"]["rank_x"] and rz==row["matrix_checks"]["rank_z"]
        assert n-rx-rz==row["parameters"]["k"]
        assert all(parity(x,z)==0 for x in hx for z in hz)
        k=row["parameters"]["k"]; target=row["target"]["required_distance"]
        assert k*(target-1)*(target-1)<=12*n and k*target*target>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())