#!/usr/bin/env python3 """Verify local clean-room artifacts against a sealed report without networking.""" import argparse import hashlib import json from pathlib import Path ROLES = {"independent_source_code", "run_log", "row_level_predictions"} MAX_BYTES = 2 * 1024 * 1024 * 1024 def canonical(value): return (json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + "\n").encode() def digest(path): total = 0 result = hashlib.sha256() with path.open("rb") as stream: for block in iter(lambda: stream.read(1024 * 1024), b""): total += len(block) if total > MAX_BYTES: raise ValueError(f"artifact exceeds {MAX_BYTES} bytes") result.update(block) return result.hexdigest(), total def main(): parser = argparse.ArgumentParser() parser.add_argument("--sealed-report", type=Path, required=True) parser.add_argument("--artifact", action="append", required=True, help="ROLE=PATH; provide each exact role once") parser.add_argument("--out", type=Path) args = parser.parse_args() sealed = json.loads(args.sealed_report.read_text(encoding="utf-8")) if set(sealed) != {"schema", "report_sha256", "report"} or sealed["schema"] != "kiwerb-lakeice-cleanroom-seal-v1": raise ValueError("wrong sealed report envelope") actual_report = hashlib.sha256(canonical(sealed["report"])).hexdigest() if sealed["report_sha256"] != actual_report: raise ValueError("sealed report hash mismatch") expected = {item["role"]: item["sha256"] for item in sealed["report"].get("artifacts", [])} if set(expected) != ROLES or len(expected) != 3: raise ValueError("sealed report must declare the exact three artifact roles") supplied = {} resolved = set() for value in args.artifact: if "=" not in value: raise ValueError("artifact must use ROLE=PATH") role, raw_path = value.split("=", 1) if role not in ROLES or role in supplied: raise ValueError("unknown or duplicate artifact role") path = Path(raw_path) if path.is_symlink() or not path.is_file(): raise ValueError("artifact must be a regular non-symlink file") identity = str(path.resolve()) if identity in resolved: raise ValueError("one file cannot satisfy multiple artifact roles") resolved.add(identity) supplied[role] = path if set(supplied) != ROLES: raise ValueError("all three artifact roles are required") rows = [] for role in sorted(ROLES): actual, size = digest(supplied[role]) if actual != expected[role]: raise ValueError(f"artifact hash mismatch for {role}") rows.append({"role": role, "bytes": size, "sha256": actual, "matches_sealed_report": True}) receipt = {"schema_version": "kiwerb-lakeice-cleanroom-local-artifact-check-v1", "status": "local_artifacts_match_sealed_report_not_scientifically_reviewed", "report_sha256": actual_report, "artifacts": rows, "network_requests": 0, "files_uploaded": 0, "identity_verified": False, "independence_verified": False, "scientific_validation_claimed": False, "scientific_discovery_claimed": False} body = json.dumps(receipt, indent=2) + "\n" if args.out: with args.out.open("x", encoding="utf-8") as stream: stream.write(body) print(body, end="") if __name__ == "__main__": main()