"""Reconstruct and replay the evidence stored as Hexagon ancillary data.""" import argparse import gzip from hashlib import sha256 import json from pathlib import Path import shutil import subprocess import sys def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--work", type=Path, default=Path("proof-replay")) parser.add_argument("--workers", type=int, default=8) parser.add_argument("--extract-only", action="store_true") args = parser.parse_args() source = Path(__file__).resolve().parent work = args.work.resolve() evidence = json.loads((source / "certificate-manifest.json").read_text(encoding="ascii")) data = work / "data" verification = work / "verification" data.mkdir(parents=True, exist_ok=True) verification.mkdir(parents=True, exist_ok=True) certificate = data / "k9_round4_certificate.txt.gz" raw_hash = sha256() raw_size = 0 with certificate.open("wb") as packed, gzip.GzipFile( filename="", fileobj=packed, mode="wb", mtime=0 ) as output: for shard in evidence["shards"]: contents = (source / shard["name"]).read_bytes() if sha256(contents).hexdigest() != shard["sha256"]: raise SystemExit(f"Invalid shard SHA-256: {shard['name']}") for record in contents.splitlines(): line = json.loads(record).encode("ascii") output.write(line) raw_hash.update(line) raw_size += len(line) if raw_hash.hexdigest() != evidence["raw_sha256"] or raw_size != evidence["raw_bytes"]: raise SystemExit("Wrong uncompressed certificate hash or length") print(f"Verified certificate reconstruction: {raw_size} bytes, SHA-256 {raw_hash.hexdigest()}", flush=True) for name in ( "verify_k9_proof.py", "round3_strict_bounds.py", "round3_exact_checks.py", "test_round4_verifier.py", "check_low_c4_round4.py" ): shutil.copyfile(source / name, verification / name) shutil.copyfile(source / "low-reference.tsv", data / "global_c4_le7.txt") if args.extract_only: return python = [sys.executable, "-B", "-O"] subprocess.run([sys.executable, "-B", verification / "round3_exact_checks.py"], check=True) subprocess.run([*python, verification / "test_round4_verifier.py"], check=True) subprocess.run( [*python, verification / "verify_k9_proof.py", certificate, "--workers", str(args.workers)], check=True, ) subprocess.run( [*python, verification / "check_low_c4_round4.py", source / "low-original.tsv", source / "low-weak.tsv"], check=True, ) if __name__ == "__main__": main()