"""Check the complete published finite search and capacity package offline.""" import argparse import copy import hashlib import importlib.util import json from pathlib import Path ROOT = Path(__file__).resolve().parent def load_module(name, filename): spec = importlib.util.spec_from_file_location(name, ROOT/filename) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def read(filename): return json.loads((ROOT/filename).read_text()) def equal(actual, expected): if json.loads(json.dumps(actual)) != expected: raise ValueError("Recomputed evidence differs from the published target") def main(controls): observations = read("observations.json") plan = observations["preregistration"] for filename, key in (("inspect-tuples.py", "checker_sha256"), ("cert2624.py", "legacy_decoder_sha256")): if hashlib.sha256((ROOT/filename).read_bytes()).hexdigest() != plan[key]: raise ValueError("Pinned search dependency changed") capacity_plan = read("capacity-preregistration.json") if hashlib.sha256((ROOT/"check-capacity.py").read_bytes()).hexdigest() != capacity_plan["checker_sha256"]: raise ValueError("Pinned capacity checker changed") search = load_module("search", "summarize-search.py") verify = load_module("tuples", "inspect-tuples.py").verify reference = load_module("legacy", "cert2624.py").certificate actual_search = search.summarize(observations, verify, reference) expected_search = read("summary.json") equal(actual_search, expected_search) capacities = read("capacity.json") check_capacity = load_module("capacities", "check-capacity.py").check actual_capacity = check_capacity(capacities) equal(actual_capacity, read("capacity-check.json")) elementary = {41: 9, 43: 8, 47: 8, 53: 6, 59: 6, 61: 6, 67: 6, 71: 6, 73: 6} if any(value > elementary[q] for q, value in actual_capacity["maxima"].items()): raise ValueError("Computed capacities contradict the elementary prime-5 bound") best = actual_search["best"] if best: table = {(row["q"], row["r"]): row["capacity"] for row in capacities["phases"]} phases = {int(token.split("/")[1]): int(token.split("/")[0]) for token in best["tuple"].split()[1:20]} covered = set() for q in (41, 43, 47, 53, 59, 61, 67, 71, 73): step = 2*pow(6, -1, q) % q struck = {j for j in best["natal_indices"] if j % q == phases[q] or (j+step) % q == phases[q]} if len(struck) > table[q, phases[q]]: raise ValueError("Best witness contradicts a computed phase capacity") covered.update(struck) if covered != set(best["natal_indices"]) or best["count"] > actual_capacity["sum"]: raise ValueError("Best witness contradicts the capacity union bound") if controls: wrong_summary = copy.deepcopy(expected_search) wrong_summary["raw_tuples"] += 1 wrong_capacity = copy.deepcopy(capacities) wrong_capacity["phases"][0]["capacity"] += 1 missing_phase = copy.deepcopy(capacities) missing_phase["phases"].pop() wrong_tuples = copy.deepcopy(observations) chunk = next(row for row in wrong_tuples["chunks"] if row["stdout"].strip()) lines = chunk["stdout"].splitlines() words = lines[0].split() words[1] = "0/5" lines[0] = " ".join(words) chunk["stdout"] = "\n".join(lines)+"\n" for operation in ( lambda: equal(actual_search, wrong_summary), lambda: check_capacity(wrong_capacity), lambda: check_capacity(missing_phase), lambda: search.summarize(wrong_tuples, verify, reference), ): try: operation() except ValueError: pass else: raise AssertionError("A corrupted public target was accepted") print(json.dumps({ "all_search_tuples_and_summary_agree": True, "raw_tuples": actual_search["raw_tuples"], "distinct_tuples": actual_search["distinct_tuples"], "sample_maximum_interior_count": actual_search["best"]["count"] if actual_search["best"] else None, "all_capacity_phases_agree": True, "best_witness_respects_each_phase_capacity": True, "capacity_phases": actual_capacity["phases"], "upper_bound_for_gaps_at_most_1530": actual_capacity["sum"], "negative_controls": 4 if controls else 0, }, sort_keys=True)) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--selftest", action="store_true") main(parser.parse_args().selftest)