diff --git a/research/chirp-residue-loop/BENCHMARK.json b/research/chirp-residue-loop/BENCHMARK.json new file mode 100644 index 0000000000..7a164d1451 --- /dev/null +++ b/research/chirp-residue-loop/BENCHMARK.json @@ -0,0 +1,101 @@ +{ + "python": "3.12.15 (main, Oct 14 2026, 00:55:37) [Clang 23.1.3 ]", + "platform": "macOS-27.0.1-arm64-arm-64bit", + "logical_cpus": 18, + "load_average": [ + 4.34033203125, + 3.3828125, + 2.54541015625 + ], + "samples": [ + { + "trial": 0, + "mode": "baseline", + "seconds": 2.4566558750811964, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 0, + "mode": "fast", + "seconds": 1.9595325000118464, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 1, + "mode": "baseline", + "seconds": 2.44032737496309, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 1, + "mode": "fast", + "seconds": 1.9822481670416892, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 2, + "mode": "baseline", + "seconds": 2.456384208984673, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 2, + "mode": "fast", + "seconds": 2.0032798750326037, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 3, + "mode": "baseline", + "seconds": 2.4750329998787493, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 3, + "mode": "fast", + "seconds": 1.9908895001281053, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 4, + "mode": "baseline", + "seconds": 2.45319641684182, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + }, + { + "trial": 4, + "mode": "fast", + "seconds": 1.9722066670656204, + "stdout_sha256": "d2af94ba9138a742e51c11e0c3331ffc24e5ea64bb64353f0d654adf4a3db2b6", + "errors": 0 + } + ], + "summary": { + "baseline": { + "median": 2.456384208984673, + "minimum": 2.44032737496309, + "maximum": 2.4750329998787493 + }, + "fast": { + "median": 1.9822481670416892, + "minimum": 1.9595325000118464, + "maximum": 2.0032798750326037 + } + }, + "reduction": 0.19302193859118, + "errors": 0, + "source_hashes": { + "loop.py": "d81d6177971e9948fd82607e68691b2fbf45f1423c71fcdc01390e00e68d52f0", + "replay.py": "48af4465ee424f761ea9cee791a70e6c46a4cff4c05135b07e016967b5e06a7b", + "benchmark.py": "b4fa62123aa5c2561b9b6036bf1045f7661de397478c64269d7eb9712c94d109" + } +} diff --git a/research/chirp-residue-loop/PROFILE.json b/research/chirp-residue-loop/PROFILE.json new file mode 100644 index 0000000000..4ad4ab1b43 --- /dev/null +++ b/research/chirp-residue-loop/PROFILE.json @@ -0,0 +1,50 @@ +{ + "baseline": { + "total_profile_seconds": 5.156292709000001, + "functions": [ + { + "file": "research/chirp-residue-step/verify.py", + "line": 24, + "name": "require", + "primitive_calls": 42099208, + "total_calls": 42099208, + "self_seconds": 0.9858124140000001, + "cumulative_seconds": 0.9858124140000001 + }, + { + "file": "research/chirp-residue-step/verify.py", + "line": 38, + "name": "one_axis", + "primitive_calls": 6141, + "total_calls": 6141, + "self_seconds": 4.1104735660000005, + "cumulative_seconds": 5.093219727 + } + ], + "source_main_seconds": 5.151132625000001 + }, + "fast": { + "total_profile_seconds": 2.17950446, + "functions": [ + { + "file": "chirp-residue-step/verify.py", + "line": 24, + "name": "require", + "primitive_calls": 143896, + "total_calls": 143896, + "self_seconds": 0.003372458, + "cumulative_seconds": 0.003372458 + }, + { + "file": "chirp-residue-loop/loop.py", + "line": 4, + "name": "one_axis", + "primitive_calls": 6141, + "total_calls": 6141, + "self_seconds": 2.122359931, + "cumulative_seconds": 2.122359931 + } + ] + }, + "warning": "Instrumented profiles identify the limiting code and counted calls; their elapsed times are not speedup samples." +} diff --git a/research/chirp-residue-loop/PROOF.md b/research/chirp-residue-loop/PROOF.md new file mode 100644 index 0000000000..e850e97d0b --- /dev/null +++ b/research/chirp-residue-loop/PROOF.md @@ -0,0 +1,89 @@ +# Equivalence of the bounded one-axis loop + +This is an implementation-equivalence argument for PR3806's primary command. +It establishes no new chirp identity or multiplication theorem. James Reed's +mathematical result remains the credited predecessor. + +## Domain and state induction + +The ordinary interface has integer radius r=2^ell for 2<=ell<=12, +length t in {r/2,r}, and odd slope s with 0 64 for x in initial) + or not 0 <= initial[0] < length): + raise ValueError("invalid initial-state control") + index, value, difference, total = initial + steps = 0 + for _ in range(length): + if value != (factor * index * index) % modulus: + raise SystemExit("FAIL_CHIRP_RESIDUE_STEP value") + if difference != (factor * (2 * index + 1)) % modulus: + raise SystemExit("FAIL_CHIRP_RESIDUE_STEP difference") + if total != value: + raise SystemExit("FAIL_CHIRP_RESIDUE_STEP one-axis total") + steps += 1 + if index + 1 < length: + total = (total + difference) % modulus + value = (value + difference) % modulus + difference = (difference + 2 * factor) % modulus + index += 1 + continue + total = (total - value) % modulus + index, value, difference = 0, 0, factor + break + if total != 0 or index != 0 or value != 0: + raise SystemExit("FAIL_CHIRP_RESIDUE_STEP rollover") + if steps != length: + raise SystemExit("FAIL_CHIRP_RESIDUE_STEP step count") + return steps diff --git a/research/chirp-residue-loop/replay.py b/research/chirp-residue-loop/replay.py new file mode 100644 index 0000000000..fd7ed0f403 --- /dev/null +++ b/research/chirp-residue-loop/replay.py @@ -0,0 +1,54 @@ +"""Authenticate and run the complete retained command with one substitution.""" + +import argparse +import hashlib +import importlib.util +import json +from pathlib import Path +import sys + +PACKAGE = Path(__file__).resolve().parent +ROOT = PACKAGE.parents[1] +sys.path.insert(0, str(PACKAGE)) +from loop import one_axis + + +def load_source(root=ROOT): + manifest = json.loads((PACKAGE / "SOURCES.json").read_text()) + for row in manifest["files"]: + if row["usage"] == "input": + data = (root / row["path"]).read_bytes() + if hashlib.sha256(data).hexdigest() != row["sha256"]: + raise ValueError("source hash mismatch: " + row["path"]) + source = root / "research/chirp-residue-step/verify.py" + spec = importlib.util.spec_from_file_location("retained_chirp_residue", source) + if spec is None or spec.loader is None: + raise RuntimeError("retained source loader unavailable") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def execute(mode, *, census=None): + source = load_source() + implementation = source.one_axis if mode == "baseline" else one_axis + if census is None: + source.one_axis = implementation + else: + def record(radius, length, slope): + result = implementation(radius, length, slope) + census.append((radius, length, slope, result)) + return result + source.one_axis = record + source.main() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--mode", choices=("baseline", "fast"), default="fast") + args = parser.parse_args() + execute(args.mode) + + +if __name__ == "__main__": + main() diff --git a/research/chirp-residue-loop/verify.py b/research/chirp-residue-loop/verify.py new file mode 100644 index 0000000000..9d9e79bc6d --- /dev/null +++ b/research/chirp-residue-loop/verify.py @@ -0,0 +1,120 @@ +"""Complete source-bound conformance and new-loop rejection controls.""" + +from contextlib import redirect_stdout +import hashlib +from io import StringIO +from itertools import product +import json +from pathlib import Path +import subprocess +import sys +import tempfile + +PACKAGE = Path(__file__).resolve().parent +sys.path.insert(0, str(PACKAGE)) +from loop import one_axis +from replay import ROOT, execute, load_source + + +def need(condition, message): + if not condition: + raise AssertionError(message) + + +def complete_conformance(): + runs = [] + for mode in ("baseline", "fast"): + census, output = [], StringIO() + with redirect_stdout(output): + execute(mode, census=census) + runs.append((output.getvalue(), census)) + need(runs[0] == runs[1], "complete stdout and ordered rollout equality") + text, census = runs[1] + need(text == "PASS_CHIRP_RESIDUE_STEP rollouts=6141 no_new_kappa\n", "source marker") + need(len(census) == 6141, "every rollout") + steps = sum(row[3] for row in census) + need(steps == 13981010, "every state") + command = [sys.executable, "-I", "-W", "error", "-B", + str(ROOT / "research/chirp-residue-step/test_verify.py")] + result = subprocess.run(command, capture_output=True, timeout=10) + need(result.returncode == 0 and not result.stderr + and result.stdout == b"PASS_CHIRP_RESIDUE_STEP_TEST\n", "retained controls") + return {"rollouts": len(census), "one_axis_states": steps, + "eliminated_require_calls": 3 * steps + 2 * len(census), + "census_sha256": hashlib.sha256(json.dumps(census).encode()).hexdigest()} + + +def initial_state_controls(): + failures = 0 + for length, slope in ((2, 1), (4, 1), (4, 3)): + factor = (-slope * (4 // length)) % 8 + for value, difference, total in product(range(8), repeat=3): + expected = ("value" if value != 0 else "difference" if difference != factor + else "one-axis total" if total != value else None) + try: + result = one_axis(4, length, slope, initial=(0, value, difference, total)) + except SystemExit as error: + need(str(error) == "FAIL_CHIRP_RESIDUE_STEP " + str(expected), + "first failing source predicate") + failures += 1 + else: + need(expected is None and result == length, "valid initial state") + need(failures == 1533, "initial-state rejection census") + return failures + + +def boundary_controls(): + cases = ((True, 2, 1), (2, 1, 1), (8192, 4096, 1), (6, 3, 1), + (4, True, 1), (4, 3, 1), (4, 4, True), (4, 4, 2), + (4, 4, 0), (4, 4, 5)) + for args in cases: + try: + one_axis(*args) + except ValueError: + continue + raise AssertionError("invalid domain accepted: " + repr(args)) + initials = ((0,), (0, 0, True, 0), (-1, 0, 0, 0), (2, 0, 0, 0), + (0, 0, 1 << 65, 0)) + for initial in initials: + try: + one_axis(4, 2, 1, initial=initial) + except ValueError: + continue + raise AssertionError("invalid control state accepted") + return len(cases) + len(initials) + + +def source_invalidation(): + manifest = json.loads((PACKAGE / "SOURCES.json").read_text()) + inputs = [row for row in manifest["files"] if row["usage"] == "input"] + with tempfile.TemporaryDirectory(prefix="chirp-loop-control-") as folder: + root = Path(folder) + for row in inputs: + target = root / row["path"] + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes((ROOT / row["path"]).read_bytes()) + target = root / "research/chirp-residue-step/verify.py" + target.write_bytes(target.read_bytes() + b"\nraise RuntimeError('must not execute')\n") + try: + load_source(root) + except ValueError as error: + need(str(error) == "source hash mismatch: research/chirp-residue-step/verify.py", + "source drift must reject before execution") + else: + raise AssertionError("altered source accepted") + return 1 + + +def main(): + result = complete_conformance() + result["initial_state_rejections"] = initial_state_controls() + result["boundary_rejections"] = boundary_controls() + result["source_drift_rejections"] = source_invalidation() + result["status"] = "PASS_CHIRP_RESIDUE_LOOP" + result["files"] = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() + for p in sorted(PACKAGE.iterdir()) if p.is_file()} + print(json.dumps(result, sort_keys=True)) + + +if __name__ == "__main__": + main()