#!/usr/bin/env python3 """Standalone E86 verifier. Standard library + cryptography; imports no CAIN code.""" from __future__ import annotations import base64 import hashlib import json import sys from pathlib import Path from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey def sha(p: Path) -> str: h = hashlib.sha256() with p.open("rb") as f: for c in iter(lambda: f.read(1 << 20), b""): h.update(c) return h.hexdigest() def merkle(leaves): if not leaves: return "0" * 64 level = [bytes.fromhex(x) for x in leaves] while len(level) > 1: nxt = [] for i in range(0, len(level), 2): nxt.append(hashlib.sha256(b"\x01" + level[i] + level[i + 1]).digest() if i + 1 < len(level) else level[i]) level = nxt return level[0].hex() def main() -> int: root = Path(sys.argv[1] if len(sys.argv) > 1 else "artifacts/e86/bundle") checks = [] def add(n, ok, d=""): checks.append({"check": n, "ok": bool(ok), "detail": d}) mb = (root / "MANIFEST.json").read_bytes() m = json.loads(mb) add("manifest_present", True) add("file_count", m.get("file_count") == len(m.get("files", [])), m.get("file_count")) bad = [e["path"] for e in m["files"] if not (root / e["path"]).is_file() or sha(root / e["path"]) != e["sha256"]] add("file_hashes", not bad, bad[:10]) on_disk = {str(p.relative_to(root)) for p in root.rglob("*") if p.is_file() and p.name not in ("MANIFEST.json", "MANIFEST.sig")} add("no_unlisted_files", on_disk <= {e["path"] for e in m["files"]}, sorted(on_disk - {e["path"] for e in m["files"]})[:10]) root_hash = merkle([e["sha256"] for e in m["files"]]) add("merkle_root", root_hash == m.get("merkle_root"), root_hash) try: pub = Ed25519PublicKey.from_public_bytes(base64.b64decode(m["signer_public_key_b64"])) pub.verify(base64.b64decode((root / "MANIFEST.sig").read_text().strip()), mb) add("signature", True) except Exception as exc: add("signature", False, type(exc).__name__) add("independent_witness_false", m.get("independent_witness") is False) # Recompute eval accuracy from raw rows. rows_path = root / "eval/rows.jsonl" summary_path = root / "eval/summary.json" if rows_path.is_file() and summary_path.is_file(): rows = [json.loads(x) for x in rows_path.read_text().splitlines() if x.strip()] s = json.loads(summary_path.read_text()) held = [r for r in rows if r.get("split") == "heldout"] ok = True detail = {} for arm, rec in s.get("arms", {}).items(): ar = [r for r in held if r.get("arm") == arm] k = sum(1 for r in ar if r.get("correct")) detail[arm] = [k, len(ar)] ok = ok and k == rec.get("correct") and len(ar) == rec.get("n") add("eval_accuracy_recomputed", ok, detail) add("eval_no_remote", all("REAL_REMOTE_MODEL" not in r.get("execution_classes", []) for r in rows)) else: add("eval_rows_present", False) gpath = root / "acceptance_matrix.json" if gpath.is_file(): g = json.loads(gpath.read_text()) counts = {} for x in g["gates"]: counts[x["status"]] = counts.get(x["status"], 0) + 1 add("gates_counts", counts == g.get("counts"), counts) add("no_pass_without_evidence", all(x.get("evidence") for x in g["gates"])) add("external_not_pass", all(x["status"] != "PASS" for x in g["gates"] if x["gate"] in ("G33", "G34"))) else: add("gates_present", False) for rel, key in (("foundation/interop.json", "ok"), ("foundation/summary.json", "ok")): p = root / rel if p.is_file(): add(rel, json.loads(p.read_text()).get(key) is True) ok = all(c["ok"] for c in checks) print(json.dumps({"schema": "cain2027.e86.offline_verification.v1", "ok": ok, "checks_passed": sum(1 for c in checks if c["ok"]), "checks_total": len(checks), "checks": checks, "merkle_root": root_hash, "evidence_class": "LOCAL_OFFLINE", "limits": ["self-generated key", "no third-party audit", "same project produced and verified"]}, indent=1, sort_keys=True)) return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())