"""Reconstruct and validate the exact B4 face-volume certificate.""" import argparse from hashlib import sha256 import json from pathlib import Path def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("output", type=Path, help="Path for the reconstructed JSON") args = parser.parse_args() directory = Path(__file__).resolve().parent manifest = json.loads((directory / "B4_face_volume_manifest.json").read_bytes()) certificate = manifest["certificate"] rows = [] for part in manifest["parts"]: chunk = json.loads((directory / part["file"]).read_bytes()) if not isinstance(chunk, list) or len(chunk) != part["rows"]: raise SystemExit(f"Unexpected row count in {part['file']}") rows.extend(chunk) if len(rows) != certificate["pairs_checked"]: raise SystemExit("Incomplete B4 coefficient table") certificate["geometric_coefficient_rows"] = rows data = json.dumps(certificate, allow_nan=False).encode("utf-8") if sha256(data).hexdigest() != manifest["original_sha256"]: raise SystemExit("Reconstructed B4 certificate does not match its original SHA-256") with args.output.open("xb") as output: output.write(data) if __name__ == "__main__": main()