#!/usr/bin/env python3 from __future__ import annotations import gzip import hashlib import json import sys from pathlib import Path def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for block in iter(lambda: handle.read(1 << 20), b""): digest.update(block) return digest.hexdigest() def canonical_sha256(value: dict, omitted: str) -> str: payload = {key: item for key, item in value.items() if key != omitted} encoded = json.dumps(payload, sort_keys=True, separators=(",", ":")) return hashlib.sha256(encoded.encode()).hexdigest() def load_jsonl_one(path: Path) -> dict: lines = [line for line in path.read_text().splitlines() if line.strip()] assert len(lines) == 1, (path, len(lines)) return json.loads(lines[0]) def load_mtx(path: Path) -> list[list[int]]: with gzip.open(path, "rt", encoding="ascii") as handle: assert handle.readline().strip() == ( "%%MatrixMarket matrix coordinate integer general" ) line = handle.readline() while line.startswith("%"): line = handle.readline() rows, cols, nnz = map(int, line.split()) matrix = [[0] * cols for _ in range(rows)] seen = 0 for line in handle: if not line.strip(): continue row, col, value = map(int, line.split()) assert value == 1 matrix[row - 1][col - 1] ^= 1 seen += 1 assert seen == nnz return matrix def gf2_rank(matrix: list[list[int]]) -> int: packed = [ sum((value & 1) << col for col, value in enumerate(row)) for row in matrix ] rank = 0 while packed: pivot = max(packed) if not pivot: break bit = 1 << (pivot.bit_length() - 1) rank += 1 packed = [ value ^ pivot if value & bit else value for value in packed if value != pivot ] return rank def content_sha256(matrix: list[list[int]]) -> str: shape = [len(matrix), len(matrix[0]) if matrix else 0] header = json.dumps( {"dtype": "uint8-gf2", "shape": shape}, sort_keys=True, separators=(",", ":"), ) raw = bytes(value & 1 for row in matrix for value in row) return hashlib.sha256(header.encode() + b"\n" + raw).hexdigest() def polynomial_matrix( ell: int, m: int, terms: list[list[int]], ) -> list[list[int]]: size = ell * m matrix = [[0] * size for _ in range(size)] for row in range(size): x, y = divmod(row, m) for dx, dy in terms: col = ((x + dx) % ell) * m + ((y + dy) % m) matrix[row][col] ^= 1 return matrix def transpose(matrix: list[list[int]]) -> list[list[int]]: return [list(column) for column in zip(*matrix)] def hstack( left: list[list[int]], right: list[list[int]], ) -> list[list[int]]: return [a + b for a, b in zip(left, right)] def parity(left: list[int], right: list[int]) -> int: return sum(a & b for a, b in zip(left, right)) & 1 def main(root: Path) -> None: root = root.resolve() data_path = root / "data/historical_exact_fom11.jsonl" evidence_path = root / "evidence/historical_exact_fom11.jsonl" manifest_path = root / "manifests/historical_exact_fom11.json" row = load_jsonl_one(data_path) evidence = load_jsonl_one(evidence_path) manifest = json.loads(manifest_path.read_text()) assert canonical_sha256(row, "record_sha256") == row["record_sha256"] assert canonical_sha256(evidence, "evidence_sha256") == evidence["evidence_sha256"] assert canonical_sha256(manifest, "manifest_sha256") == manifest["manifest_sha256"] for item in manifest["files"]: path = root / item["path"] assert path.stat().st_size == item["bytes"] assert sha256(path) == item["sha256"] assert row["canonical_digest"] == evidence["canonical_digest"] construction = row["construction"] construction_payload = { key: construction[key] for key in ("ell", "m", "A_terms", "B_terms") } encoded = json.dumps( construction_payload, sort_keys=True, separators=(",", ":"), ) assert hashlib.sha256(encoded.encode()).hexdigest() == row["identity"]["construction_sha256"] ell, m = construction["ell"], construction["m"] a_matrix = polynomial_matrix(ell, m, construction["A_terms"]) b_matrix = polynomial_matrix(ell, m, construction["B_terms"]) expected_hx = hstack(a_matrix, b_matrix) expected_hz = hstack(transpose(b_matrix), transpose(a_matrix)) matrices = row["matrices"] hx_path = root / matrices["hx_path"] hz_path = root / matrices["hz_path"] hx, hz = load_mtx(hx_path), load_mtx(hz_path) assert hx == expected_hx and hz == expected_hz assert [len(hx), len(hx[0])] == matrices["hx_shape"] == [144, 288] assert [len(hz), len(hz[0])] == matrices["hz_shape"] == [144, 288] assert sha256(hx_path) == matrices["hx_file_sha256"] assert sha256(hz_path) == matrices["hz_file_sha256"] assert content_sha256(hx) == matrices["hx_content_sha256"] assert content_sha256(hz) == matrices["hz_content_sha256"] rank_x, rank_z = gf2_rank(hx), gf2_rank(hz) assert rank_x == row["matrix_checks"]["rank_x"] == 119 assert rank_z == row["matrix_checks"]["rank_z"] == 119 n = row["parameters"]["n"] k = row["parameters"]["k"] distance = row["distance"]["exact"] max_check_weight = max(max(map(sum, hx)), max(map(sum, hz))) max_sector_qubit_degree = max( max(sum(check[j] for check in hx) for j in range(n)), max(sum(check[j] for check in hz) for j in range(n)), ) max_qubit_degree = max( sum(check[j] for check in hx) + sum(check[j] for check in hz) for j in range(n) ) assert row["matrix_checks"]["max_check_weight"] == max_check_weight == 8 assert row["matrix_checks"]["max_sector_qubit_degree"] == max_sector_qubit_degree == 4 assert row["matrix_checks"]["max_qubit_degree"] == max_qubit_degree == 8 assert n - rank_x - rank_z == k == 50 assert all(parity(x_row, z_row) == 0 for x_row in hx for z_row in hz) assert row["fom"]["exact"]["numerator"] == 100 assert row["fom"]["exact"]["denominator"] == 9 assert k * distance * distance * 9 == 100 * n assert k * distance * distance < 12 * n assert row["authority"]["modern_replayable_distance_certificate"] is False assert evidence["legacy_milp_summary"]["logicals_optimal"] == 100 assert evidence["legacy_milp_summary"]["total_logicals"] == 100 assert evidence["source"]["records"][1]["orientation"] == "equivalent_A_B_swapped_form" print("PASS: historical [[288,50,8]] identity, matrices, hashes, and FOM verified") if __name__ == "__main__": main(Path(sys.argv[1]) if len(sys.argv) > 1 else Path("."))