"""Recount both endpoints of the v1 preprint using only the standard library.""" import argparse from fractions import Fraction import hashlib import json from pathlib import Path import platform import shutil import subprocess import sys import tempfile import time ROOT = Path(__file__).resolve().parent LOWER = Path("stage3/lower") UPPER = Path("stage4/alternative/u63345") def verify(): started = time.perf_counter() manifest = json.loads((ROOT / "manifest.json").read_bytes()) for name, digest in manifest.items(): if hashlib.sha256((ROOT / name).read_bytes()).hexdigest() != digest: raise ValueError(f"File digest differs for {name}") checks = [ [LOWER / "certificate.py", "--refined"], [LOWER / "audit.py", "--refined"], ["stage4/alternative/check.py", UPPER], ["audit_upper_exact.py", UPPER], ] runs = [] with tempfile.TemporaryDirectory() as scratch: work = Path(scratch) for name in manifest: if name.startswith(("stage1/upper_check/", "stage3/lower/", "stage4/alternative/u63345/")) or name in { "stage4/alternative/check.py", "audit_upper_exact.py" }: destination = work / name destination.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, destination) for check in checks: args = list(map(str, check)) before = time.perf_counter() result = subprocess.run([sys.executable, "-B", *args], cwd=work, check=True, stdout=subprocess.PIPE, text=True) print(result.stdout.strip(), flush=True) runs.append(dict(command=["python", "-B", *args], seconds=time.perf_counter()-before, output=result.stdout.strip())) lower = json.loads((work / LOWER / "refined-receipt.json").read_bytes()) upper = json.loads((work / UPPER / "verification.json").read_bytes()) q = Fraction(json.loads((work / LOWER / "refined-certificate.json").read_bytes())["q"]) u = Fraction(upper["upper"]) if (q != Fraction(11171, 20000) or u != Fraction(12669, 20000) or upper["source"] != "50000001/100000000" or any(step["gap"] for step in upper["steps"]) or len(upper["steps"]) != 2 or lower["events_compared"] != 112 or lower["likelihood_identities_tested"] != 3936 or Fraction(lower["minimum_exact"]) <= 0): raise ValueError("The checked claims differ from the published theorem") return dict(status="pass", finite_lower=str(q), transport_target=str(u), interval_width=str(u-q), python=sys.version, platform=platform.platform(), seconds=time.perf_counter()-started, manifest_files=len(manifest), runs=runs, lower_receipt=lower, upper_receipt=upper) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", type=Path, help="save a detailed JSON receipt") options = parser.parse_args() receipt = verify() if options.output: options.output.write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf8") print("PASS. Finite certificates for 0.55855 <= p_c <= 0.63345.")