From f6a192c2d22c0a3534ea7f01e31e45a8033e9191 Mon Sep 17 00:00:00 2001 From: Nick Petalas Date: Fri, 30 Oct 2026 13:54:28 +0000 Subject: [PATCH] perf(research): accelerate complete mapped-contact replay --- research/rank-contact-replay/PROOF.md | 107 +++++++++++++ research/rank-contact-replay/README.md | 92 +++++++++++ research/rank-contact-replay/SOURCES.json | 42 +++++ research/rank-contact-replay/TASK.json | 66 ++++++++ research/rank-contact-replay/VERIFY.json | 39 +++++ research/rank-contact-replay/benchmark.py | 75 +++++++++ research/rank-contact-replay/contact_rank.py | 75 +++++++++ research/rank-contact-replay/replay.py | 70 +++++++++ research/rank-contact-replay/verify.py | 157 +++++++++++++++++++ 9 files changed, 723 insertions(+) create mode 100644 research/rank-contact-replay/PROOF.md create mode 100644 research/rank-contact-replay/README.md create mode 100644 research/rank-contact-replay/SOURCES.json create mode 100644 research/rank-contact-replay/TASK.json create mode 100644 research/rank-contact-replay/VERIFY.json create mode 100644 research/rank-contact-replay/benchmark.py create mode 100644 research/rank-contact-replay/contact_rank.py create mode 100644 research/rank-contact-replay/replay.py create mode 100644 research/rank-contact-replay/verify.py diff --git a/research/rank-contact-replay/PROOF.md b/research/rank-contact-replay/PROOF.md new file mode 100644 index 0000000000..34ba070ad3 --- /dev/null +++ b/research/rank-contact-replay/PROOF.md @@ -0,0 +1,107 @@ +# Exact rank execution of mapped contacts + +This scoped implementation proof and the accompanying code were produced by +Nick Petalas. David Ganz / Dugongue's PR4841 supplies the retained geometric +formula, caller, graph and expected receipt. Rank-nullity and binary Gaussian +elimination are classical. No new geometric lower bound is claimed. + +## Equality of the two actual-change computations + +Let U,V be n-dimensional subspaces of F2^(2n), with bases B_U,B_V. For any +axis a, define T_a(x)=x+omega(x,a)a, where omega is the standard alternating +binary symplectic form. Since omega(a,a)=0, T_a is linear and T_a^2=I. +Consequently T_a(B_U) is a basis of T_a(U). + +The retained function computes + + dim(U intersect V) - dim(T_a(U) intersect V). + +Rank-nullity gives dim(U intersect V)=2n-rank(B_U union B_V), and the +corresponding identity holds for T_a(U). Subtraction therefore yields + + rank(T_a(B_U) union B_V) - rank(B_U union B_V). + +This is exactly `ContactRank.__call__`. Its elimination works over F2 by +XOR, so no floating-point or modular reconstruction premise enters. Isotropy +of U and V is unnecessary. The code accepts 1<=n<=9, matching and bounding +the source caller's domain. The same algebraic identity holds in every n. + +The difference lies in {-1,0,1}. Indeed T_a(B_U)-B_U has row space inside +the line spanned by a, so the two concatenated matrices differ by a +rank-at-most-one matrix. The runtime assertion checks this consequence. + +For an input set S of size 2^n, `checked_basis` validates ambient coordinates +and span dimension n. Every point of S belongs to its computed span; the +equal cardinalities imply that S is the entire subspace. This also rejects +incomplete or nonclosed sets. Booleans are rejected as coordinates and axes. + +## One-contact cache and complete caller + +The cache stores one pair of immutable frozensets, their bases, width and +original union rank. Object identity and width are tested on every call. +Repeated equal sets with different identities are recomputed. A new cache +entry is published only after both subspaces validate. Axis validation +precedes access. Failed preparation leaves the preceding cache usable. +The cache owns no state outside one replay and retains no historical contacts. + +The retained `small` and `actual_graph` functions execute without edits. +Only the `actual_change` function reference in one freshly loaded module is +replaced. The distinct retained `change_formula`, chosen edges, fixed seed, +sampled axes, actual maps, rank checks and all receipt fields stay literal. +The replacement never calls `change_formula`. The candidate still performs +79,360 actual-change computations and 1,240 contact preparations. Complete +receipt equality is checked against the authenticated retained JSON. + +For a contact queried q times, preparation uses at most 2^(n+1) input +vectors once. Each subsequent query moves n basis vectors and eliminates +2n rows, instead of moving and intersecting a set of 2^n vectors. Work is +O(2^n n^2 + q n^3) elementary binary steps per contact, compared with +O(q 2^n n) for a literal bit-cost implementation of the retained set route. +These are algorithmic envelopes, not measured machine costs or a new supplier +tariff. The physically relevant supplier program itself is unchanged. + +## Checks and scope + +`verify.py` compares the new engine directly with the retained literal +intersection on all Lagrangian pairs through n=3, all axes including zero, +and all 35 binary 2-planes, including nonisotropic ones. It adds 72 wider +pairs through n=9, invalid inputs, cache-failure and sign/constant mutants. +It then executes the complete retained caller with the replacement installed. +The benchmark runs that whole caller with each engine in fresh processes, +checks its receipt, and requires identical stdout hashes on every run. + +The written proof establishes replacement equivalence. The finite checks +exercise its implementation; they are not a new proof of the predecessor's +geometric claims. The full PR4841 five-command package, graph regeneration, +native supplier, compiler, physical caller, precision and outer admission are +outside this contribution. No selected assumption or kappa changes. + +## Code constraints + +Every new loop has a finite bound. Subspaces contain at most 512 points; +ambient width is at most 18; basis unions contain at most 18 vectors. The +conformance domains are explicit finite sets and the benchmark accepts five +through nine pairs, with a 120-second subprocess limit. All functions are +under 60 physical lines. Rejection checks remain active with optimization. + +Power of Ten rule 3 deviation: CPython allocates integer and collection +objects during elimination. The public n<=9 check bounds live data to one +contact's two at-most-512-point sets, two nine-vector bases and an 18-vector +elimination buffer, plus caller-owned source data. No growing contact cache +is used. A fixed-buffer implementation is a possible later port; it is not +required for this Python research replay. The tests exercise the n=9 bound +and invalid widths. Runtime/library allocator internals are not inspected. + +Power of Ten rules 8 and 9 deviation: the unchanged caller is loaded through +one explicit authenticated file path, and one module function reference is +replaced by one explicit callable object. This is necessary to run the real +frozen caller without modifying its files. There is no input-selected module +or callback registry. Exact hash checks precede loading, and complete caller +and invalid-source controls cover the boundary. No source implementation is +generated or evaluated from a string. A future upstream adoption could use +an ordinary explicit engine parameter instead of this isolated adapter. + +Warning-strict normal and optimized execution, source compilation and AST +function-length inspection pass locally. Ruff and mypy are not installed in +the available environment; no static-type-checker pass is claimed. Their +absence does not suppress a diagnostic or change shared repository checks. diff --git a/research/rank-contact-replay/README.md b/research/rank-contact-replay/README.md new file mode 100644 index 0000000000..088716189f --- /dev/null +++ b/research/rank-contact-replay/README.md @@ -0,0 +1,92 @@ +# Faster complete mapped-contact replay + +The complete PR4841 mapped-contact distance caller takes 2.252 s rather than +8.199 s at the median of five alternating fresh-process pairs on the recorded +local machine, a **72.54% time reduction**. All ten runs succeed with identical +output hashes. This is a scoped implementation-equivalence proof with finite +conformance and local performance evidence. No new kappa is claimed. + +The implementation, proof and experiments were produced by Nick Petalas. +David Ganz / Dugongue retains PR4841's compiler, geometric predicates, complete +small-domain checks, frozen graph and expected receipt. Cao Yifan's PR4610 and +Cinna Davis's PR4690/4672 retain the actual-word and contact-model lineage. +The full unchanged predecessor notice is an authenticated input. +Rank-nullity and binary elimination are classical. New work is Apache-2.0. + +## Change and evidence + +The original actual-change engine moves all 512 elements of each nine-dimensional +subspace for every axis. The replacement moves nine basis vectors and computes +an exact binary rank. A one-contact cache retains the original union rank and +the two immutable bases. [PROOF.md](PROOF.md) derives equality with literal +intersection without using the separate transvection-distance formula. + +The complete caller retains every small check, 4,429 frames, 145 maps, all +1,240 chosen mapped edges, the same seed and every one of 79,360 sampled axes. +Its output is checked against the original authenticated JSON. The new engine +prepares exactly 1,240 contacts and executes every actual-change query. +No frozen predecessor file is edited. + +The replacement conformance check covers 1,170,036 Lagrangian pair/axis cases +through width three, including the zero axis; 19,600 cases on all binary +two-planes; one equal-value cache replacement; and 4,608 wider cases through +width nine. Eleven invalid inputs, four changed-source controls and two +incorrect-output controls reject. Normal and optimized Python run the same +checks. This is verification of the replacement implementation, not a new +audit claim for the predecessor's construction. + +The untimed profile attributes 16.799 of 18.866 profiled seconds to the +retained actual-change engine. Its generator performs 40,711,680 iterations. +The timing experiment includes interpreter startup, source authentication, +both complete caller phases, expected-output validation and JSON output. +Literal samples range from 8.028 to 8.284 s; rank samples range from 2.196 to +2.302 s. The medians are 8.198592292 and 2.251555958 s. Every side runs the +same source, flags and checks. The host has 18 logical CPUs, with observed +load averages 3.66/2.51/1.80 before measurement. Other work may run on this +shared machine, so the local percentage is not a hardware-independent promise. +The profile is excluded from the timing samples. It explains why removing +the enumeration can affect the complete path by this amount. + +## Reproduce + +From the repository root, using the Python 3.12 environment: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/rank-contact-replay/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -O -W error -B research/rank-contact-replay/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/research_package.py --package research/rank-contact-replay +``` + +The direct commands print `PASS_RANK_CONTACT_REPLAY` in generated JSON. The +launcher must also print `PASS_REGISTERED_RESEARCH_PACKAGE`. Source pins are +in [SOURCES.json](SOURCES.json); all four live inputs are declared in +[VERIFY.json](VERIFY.json). Python's standard library suffices. + +Run `replay.py` for the complete faster caller or add `--engine literal` for +the retained engine. To remeasure, choose an existing scratch directory and run: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/rank-contact-replay/benchmark.py --output "$TMPDIR/contact-benchmark.json" +``` + +The benchmark emits actual timings, exit status, complete work counts, output +hashes, environment and package-file hashes. It refuses to write inside the +package. Generated logs and receipts belong outside the checkout. Set TMPDIR +to the chosen scratch directory to place verification's temporary corruption +fixtures there too. CI runs conformance, not a speed threshold. + +## Limits + +The measured path is the entire retained `decoder/testformula.py --graph ...` +caller. It is one command of PR4841's larger package, not its whole five-command +compiler suite. Graph regeneration, new chart search, native implementation, +physical hosting, supplier admission, precision and outer funding were not run. +The five framework assumptions and selected frontier remain unchanged. No +independent human or formal proof-kernel acceptance is claimed. + +## Open next questions + +Can a basis-only contact ABI remove complete-subspace materialization too? +That could remove preparation work, but changes the caller interface. Start +with a bounded validated basis contract and measure the whole source caller. +It remains outside this first-improvement package. diff --git a/research/rank-contact-replay/SOURCES.json b/research/rank-contact-replay/SOURCES.json new file mode 100644 index 0000000000..466ba62fd9 --- /dev/null +++ b/research/rank-contact-replay/SOURCES.json @@ -0,0 +1,42 @@ +{ + "schema_version": 1, + "base_commit": "2485805a6d47f36ea83ddde6327d453ac82d44dc", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "d59b5035e7b4cbaa9d688a1d32dd0d6eb79c466a", + "sha256": "9b8d0eea95b3bf0dfcb7938a732a6439b87a2685dec2a5ab9e525b3b34debbfa" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "3404fbdd5e7529a0674eef26114b5071e4586cec", + "sha256": "ead6cdc9d0af6d58e8a49744d8ec8c7c15e13d2ba93c276c434bcf9b51b93a81" + }, + { + "path": "research/native-clifford-gauge-compiler/NOTICE.md", + "usage": "input", + "git_blob": "5519958ecf6200f04b63a4783f805394c717b915", + "sha256": "ea459c3cae30546924cc4d3521eca3d982db19d34ae37cb04d0651909df1081e" + }, + { + "path": "research/native-clifford-gauge-compiler/decoder/TRANSVECTION-EXPECTED.json", + "usage": "input", + "git_blob": "6e929cc239937ccd9374d02856e0379fe23109a1", + "sha256": "18a092c2cd4cd9174488646782c86f1bcc3ef044bbb5d7feceb6c513f6fc7988" + }, + { + "path": "research/native-clifford-gauge-compiler/decoder/testformula.py", + "usage": "input", + "git_blob": "0533809634361ff9954df601a77184461c1abbb0", + "sha256": "a9f32161eb0222ae9279a96f74504b0af8863ac6251973d9fde32f205dd3ed8c" + }, + { + "path": "research/native-clifford-gauge-compiler/inputs/round3-contact-graph.txt", + "usage": "input", + "git_blob": "31e1bfc6c5ca64d1056bce4f35861d840412cd2a", + "sha256": "f802be993525ec7b944a2622c4fd47db76101d7a558ea449ece7254780f6efef" + } + ] +} diff --git a/research/rank-contact-replay/TASK.json b/research/rank-contact-replay/TASK.json new file mode 100644 index 0000000000..e4330ca6fc --- /dev/null +++ b/research/rank-contact-replay/TASK.json @@ -0,0 +1,66 @@ +{ + "schema_version": 1, + "task_id": "replay-runtime", + "priority_revision": 8, + "base_commit": "2485805a6d47f36ea83ddde6327d453ac82d44dc", + "original_card": { + "id": "replay-runtime", + "question": "Can one measured replay bottleneck be sped up without dropping coverage?", + "starting_points": [ + "scripts/run_isolated_tests.py", + "scripts/verify_replay_coverage.py", + "certificates/selected-result.json" + ], + "methods": [ + "Profile one isolated replay and optimize the measured bottleneck.", + "Design source-bound reuse with explicit dependency invalidation and negative controls." + ], + "acceptance_gate": "Report before/after wall time on the same fixture, retained checks and invalidation behavior.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_selected_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Resolve the current selected review, separate complex/ordinary BIT/admitted BIT/outer budgets, and compare the card against live handoffs before starting. Historical ceilings require exact source/model equivalence.", + "scope": "Source-binding orientation only; not a scientific replay or all-size proof. Materialize selected inputs if this checkout is sparse." + }, + "first_experiment": "Measure one unchanged fixture, preserve check coverage, and test one optimization with invalidation controls.", + "bottleneck_link": "Faster trustworthy replays increase review throughput and let mechanism candidates be checked sooner.", + "allowed_variations": [ + "Profile one stage or design one source-bound cache boundary." + ], + "scope_change_triggers": [ + "Skipping coverage or comparing different fixtures to claim speedup." + ], + "overlap_terms": [ + "replay runtime", + "cache invalidation", + "isolated tests" + ] + }, + "scope_status": "refined", + "actual_scope": "Exact basis-rank execution of the complete PR4841 mapped-contact caller, retaining all checks and receipts.", + "scope_change_reason": "The runtime card is refined to the complete retained PR4841 contact-distance caller and its measured literal intersection bottleneck.", + "bottleneck_link": "This verifier-engineering side result removes measured exponential subspace enumeration from a complete native-contact replay; it does not improve the paid supplier invoice.", + "budget_minutes": 60, + "stop_rule": "Ship the first verified complete-caller improvement; do not optimize other caller phases.", + "outcome": "completed", + "evidence": "PROOF.md; verify.py; benchmark.py emits timed complete-caller receipts and SHA256 bindings outside the checkout.", + "author": "Nick Petalas", + "ai_assistance": "Independent human and formal proof-kernel review are unclaimed.", + "open_questions": [ + { + "question": "Can a basis-only contact ABI eliminate materialization of complete subspaces at the caller boundary?", + "why_it_matters": "The current adapter still pays for span construction and validates full input sets.", + "first_step": "Define a separate bounded basis contract and compare the complete graph caller, including basis validation.", + "blocked_by": "This changes the source interface and is outside the first-improvement scope." + } + ], + "question_source": { + "pr": 4841, + "author": "Dugongue", + "role": "Retained implementation and geometric predicates; the performance question is newly posed." + } +} diff --git a/research/rank-contact-replay/VERIFY.json b/research/rank-contact-replay/VERIFY.json new file mode 100644 index 0000000000..22ce172a1f --- /dev/null +++ b/research/rank-contact-replay/VERIFY.json @@ -0,0 +1,39 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/native-clifford-gauge-compiler/NOTICE.md", + "research/native-clifford-gauge-compiler/decoder/TRANSVECTION-EXPECTED.json", + "research/native-clifford-gauge-compiler/decoder/testformula.py", + "research/native-clifford-gauge-compiler/inputs/round3-contact-graph.txt" + ], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 180, + "checks": "Replacement versus literal intersections, invalid inputs, one-contact cache and complete retained caller" + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 180, + "checks": "The same complete conformance and caller checks with optimization enabled" + } + ] +} diff --git a/research/rank-contact-replay/benchmark.py b/research/rank-contact-replay/benchmark.py new file mode 100644 index 0000000000..bb3cecfcc6 --- /dev/null +++ b/research/rank-contact-replay/benchmark.py @@ -0,0 +1,75 @@ +"""Alternate fresh complete callers and retain successful output hashes. + +Produced by Nick Petalas. The timing includes interpreter startup, source +authentication, all small checks, the entire graph caller and receipt checking. +""" + +import argparse +import hashlib +import json +import os +from pathlib import Path +import platform +import statistics +import subprocess +import sys +import time + +PACKAGE = Path(__file__).resolve().parent + + +def run_one(engine: str) -> dict: + argv = [sys.executable, "-I", "-W", "error", "-B", + str(PACKAGE / "replay.py"), "--engine", engine] + start = time.perf_counter_ns() + result = subprocess.run(argv, capture_output=True, timeout=120, check=True) + elapsed = time.perf_counter_ns() - start + if result.stderr: + raise ValueError("Unexpected diagnostic: " + result.stderr.decode()) + receipt = json.loads(result.stdout) + if receipt["actual"]["exact_axis_cases"] != 79360: + raise ValueError("Complete graph work was not performed") + return {"engine": engine, "argv": argv, "elapsed_ns": elapsed, + "stdout_sha256": hashlib.sha256(result.stdout).hexdigest(), + "actual_axis_cases": receipt["actual"]["exact_axis_cases"], + "returncode": result.returncode} + + +def measure(pairs: int) -> dict: + runs = [] + for _pair in range(pairs): + for engine in ("literal", "rank"): + runs.append(run_one(engine)) + if len({run["stdout_sha256"] for run in runs}) != 1: + raise ValueError("Complete caller outputs disagree") + samples = {engine: [r["elapsed_ns"] for r in runs if r["engine"] == engine] + for engine in ("literal", "rank")} + summaries = {engine: {"median_ns": statistics.median(values), + "min_ns": min(values), "max_ns": max(values)} + for engine, values in samples.items()} + return {"status": "PASS_COMPLETE_CONTACT_BENCHMARK", "pairs": pairs, + "errors": 0, "python": sys.version, "platform": platform.platform(), + "cpu_count": os.cpu_count(), "load_average": os.getloadavg(), + "runs": runs, "summaries": summaries, + "median_reduction_fraction": 1 - summaries["rank"]["median_ns"] + / summaries["literal"]["median_ns"], + "package_sha256": {p.name: hashlib.sha256(p.read_bytes()).hexdigest() + for p in sorted(PACKAGE.iterdir()) if p.is_file()}} + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--pairs", type=int, default=5, choices=range(5, 10)) + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + if PACKAGE in args.output.resolve().parents: + raise ValueError("Write benchmark logs outside the package") + out = measure(args.pairs) + args.output.write_text(json.dumps(out, indent=2, sort_keys=True) + "\n", + encoding="utf-8") + print(json.dumps({k: out[k] for k in ("status", "pairs", "errors", "summaries", + "median_reduction_fraction")}, sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/research/rank-contact-replay/contact_rank.py b/research/rank-contact-replay/contact_rank.py new file mode 100644 index 0000000000..ae4be457e7 --- /dev/null +++ b/research/rank-contact-replay/contact_rank.py @@ -0,0 +1,75 @@ +"""Exact bounded binary-subspace intersection engine. Apache-2.0. + +The implementation was produced by Nick Petalas. Rank-nullity and binary +Gaussian elimination are classical; this module changes no contact formula. +""" + +from collections.abc import Iterable + + +def binary_basis(rows: Iterable[int], width: int) -> tuple[int, ...]: + """Internal elimination: callers bound row counts and coordinate widths.""" + pivots = [0] * width + for row in rows: + value = row + for bit in range(width - 1, -1, -1): + if not value & (1 << bit): + continue + if pivots[bit]: + value ^= pivots[bit] + else: + pivots[bit] = value + break + return tuple(value for value in pivots if value) + + +def checked_basis(space: frozenset[int], n: int) -> tuple[int, ...]: + """Validate the complete subspace, including its promised dimension.""" + if not isinstance(space, frozenset) or len(space) != 1 << n: + raise ValueError("Expected a complete n-dimensional frozen subspace") + if any(type(x) is not int or not 0 <= x < 1 << (2 * n) for x in space): + raise ValueError("Subspace coordinate outside the binary ambient space") + result = binary_basis(space, 2 * n) + if len(result) != n: + raise ValueError("Subspace cardinality and span dimension disagree") + # Every input lies in this span. Equal cardinalities therefore imply equality. + assert len(space) == 1 << len(result) + return result + + +class ContactRank: + """One-contact cache, owned by one replay; no global retained subspaces.""" + + def __init__(self) -> None: + self.u: frozenset[int] | None = None + self.v: frozenset[int] | None = None + self.n = 0 + self.ub: tuple[int, ...] = () + self.vb: tuple[int, ...] = () + self.old_rank = 0 + self.calls = 0 + self.preparations = 0 + + def __call__(self, u: frozenset[int], v: frozenset[int], axis: int, + n: int) -> int: + if type(n) is not int or not 1 <= n <= 9: + raise ValueError("Supported contact widths are integers 1 through 9") + if type(axis) is not int or not 0 <= axis < 1 << (2 * n): + raise ValueError("Axis outside the binary ambient space") + if u is not self.u or v is not self.v or n != self.n: + # Prepare completely before publishing a new cache entry. + ub, vb = checked_basis(u, n), checked_basis(v, n) + old_rank = len(binary_basis(ub + vb, 2 * n)) + self.u, self.v, self.n = u, v, n + self.ub, self.vb, self.old_rank = ub, vb, old_rank + self.preparations += 1 + mask = (1 << n) - 1 + low, high = axis & mask, axis >> n + moved = tuple(x ^ (axis if (((x & mask) & high).bit_count() + + ((x >> n) & low).bit_count()) & 1 else 0) + for x in self.ub) + new_rank = len(binary_basis(self.vb + moved, 2 * n)) + self.calls += 1 + result = new_rank - self.old_rank + assert -1 <= result <= 1 + return result diff --git a/research/rank-contact-replay/replay.py b/research/rank-contact-replay/replay.py new file mode 100644 index 0000000000..0638f82e5b --- /dev/null +++ b/research/rank-contact-replay/replay.py @@ -0,0 +1,70 @@ +"""Run the entire retained contact-distance caller with either exact engine. + +Produced by Nick Petalas. The retained caller and its mathematical predicates +are David Ganz / Dugongue's PR4841, with the notices pinned in SOURCES.json. +""" + +import argparse +import hashlib +import importlib.util +import json +from pathlib import Path +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from contact_rank import ContactRank + +PACKAGE = Path(__file__).resolve().parent +ROOT = PACKAGE.parents[1] +SOURCE = "research/native-clifford-gauge-compiler" + + +def authenticate(root: Path = ROOT) -> None: + pins = json.loads((PACKAGE / "SOURCES.json").read_text(encoding="utf-8")) + for entry in pins["files"]: + if entry["usage"] == "input": + data = (root / entry["path"]).read_bytes() + if hashlib.sha256(data).hexdigest() != entry["sha256"]: + raise ValueError("Source hash mismatch: " + entry["path"]) + + +def load_source(root: Path = ROOT): + authenticate(root) + path = root / SOURCE / "decoder/testformula.py" + spec = importlib.util.spec_from_file_location("retained_contacts", path) + if spec is None or spec.loader is None: + raise ValueError("Cannot load authenticated contact caller") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def complete_replay(engine: str = "rank") -> tuple[dict, ContactRank | None]: + if engine not in ("rank", "literal"): + raise ValueError("Unknown contact engine") + source = load_source() + rank_engine = ContactRank() if engine == "rank" else None + if rank_engine is not None: + source.actual_change = rank_engine + # These are precisely the calls in the retained main with --graph supplied. + out = {"status": "PASS_EXACT_TRANSVECTION_DISTANCE", "small": source.small(), + "actual": source.actual_graph(ROOT / SOURCE / "inputs/round3-contact-graph.txt")} + expected = json.loads((ROOT / SOURCE / "decoder/TRANSVECTION-EXPECTED.json") + .read_text(encoding="utf-8")) + if out != expected: + raise ValueError("Complete retained contact receipt changed") + if rank_engine is not None and rank_engine.calls != out["actual"]["exact_axis_cases"]: + raise ValueError("Rank engine did not execute every actual contact check") + return out, rank_engine + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--engine", choices=("rank", "literal"), default="rank") + args = parser.parse_args() + out, _rank_engine = complete_replay(args.engine) + print(json.dumps(out, sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/research/rank-contact-replay/verify.py b/research/rank-contact-replay/verify.py new file mode 100644 index 0000000000..ea694a5f8f --- /dev/null +++ b/research/rank-contact-replay/verify.py @@ -0,0 +1,157 @@ +"""Check the replacement engine and the full retained caller. Apache-2.0. + +Produced by Nick Petalas. No source geometric result is claimed as new. +""" + +import argparse +import hashlib +import itertools +import json +from pathlib import Path +import random +import shutil +import sys +import tempfile + +PACKAGE = Path(__file__).resolve().parent +sys.path.insert(0, str(PACKAGE)) +from contact_rank import ContactRank +from replay import ROOT, authenticate, complete_replay, load_source + + +def need(condition: bool, message: str) -> None: + if not condition: + raise ValueError(message) + + +def kernel_checks(source) -> dict: + engine = ContactRank() + counts = [] + for n in (1, 2, 3): + spaces = source.all_lagrangians(n) + count = 0 + for u in spaces: + for v in spaces: + for axis in range(1 << (2 * n)): + need(engine(u, v, axis, n) == source.actual_change(u, v, axis, n), + "Rank engine differs from literal moved-space intersection") + count += 1 + counts.append({"n": n, "spaces": len(spaces), "axis_cases": count}) + # Nonisotropic complete subspaces are legal too. Enumerate every 2-plane. + spaces = sorted({source.span(pair) for pair in itertools.combinations(range(1, 16), 2)}, + key=lambda x: tuple(sorted(x))) + need(len(spaces) == 35, "Complete binary Grassmannian count") + general_cases = 0 + for u in spaces: + for v in spaces: + for axis in range(16): + need(engine(u, v, axis, 2) == source.actual_change(u, v, axis, 2), + "Nonisotropic intersection mismatch") + general_cases += 1 + # A fresh equal-value object must give the same answer after cache eviction. + u, v = spaces[0], spaces[-1] + expected = source.actual_change(u, v, 7, 2) + need(engine(frozenset(list(u)), frozenset(list(v)), 7, 2) == expected, + "Equal-value cache replacement") + return {"lagrangian_cases": counts, "general_plane_cases": general_cases, + "calls": engine.calls, "cache_preparations": engine.preparations} + + +def wider_checks(source) -> dict: + rng = random.Random(48414841) + engine = ContactRank() + count = 0 + for n in (4, 5, 9): + for _case in range(24): + u, v = source.span([1 << j for j in range(n)]), source.span([ + (1 << j) ^ (1 << (n + j)) for j in range(n)]) + # Arbitrary symplectic transvections preserve complete subspaces. + for _step in range(12): + axis = rng.randrange(1, 1 << (2 * n)) + u = frozenset(x ^ (axis if source.symp(x, axis, n) else 0) for x in u) + axis = rng.randrange(1, 1 << (2 * n)) + v = frozenset(x ^ (axis if source.symp(x, axis, n) else 0) for x in v) + for _axis in range(64): + axis = rng.randrange(1 << (2 * n)) + need(engine(u, v, axis, n) == source.actual_change(u, v, axis, n), + "Wide literal intersection mismatch") + count += 1 + return {"pairs": 72, "axis_cases": count} + + +def controls(source) -> dict: + engine = ContactRank() + u, v = frozenset((0, 1)), frozenset((0, 2)) + good = engine(u, v, 3, 1) + bad = [(u, v, -1, 1), (u, v, 4, 1), (u, v, True, 1), + (u, v, 0, 0), (u, v, 0, 10), (u, v, 0, True), + (set(u), v, 0, 1), (frozenset((0,)), v, 0, 1), + (frozenset((0, 4)), v, 0, 1), (frozenset((0, True)), v, 0, 1), + (frozenset((0, 1, 2, 4)), frozenset((0, 1, 2, 3)), 0, 2)] + for args in bad: + try: + engine(*args) + except ValueError: + pass + else: + raise ValueError("Invalid contact was accepted") + need(engine(u, v, 3, 1) == good and engine.preparations == 1, + "Rejected input corrupted the one-entry cache") + need(good == -1 and source.actual_change(u, v, 3, 1) == -1, + "Sign-reversal and constant-zero mutants must fail") + need(engine(u, v, 1, 1) == 0, "Fixed-subspace axis control") + return {"invalid_inputs_rejected": len(bad), "mutation_controls": 2, + "failed_preparation_preserves_cache": True} + + +def source_controls() -> int: + pins = json.loads((PACKAGE / "SOURCES.json").read_text(encoding="utf-8")) + entries = [e for e in pins["files"] if e["usage"] == "input"] + with tempfile.TemporaryDirectory(prefix="contact-source-control-") as directory: + root = Path(directory) + for entry in entries: + target = root / entry["path"] + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(ROOT / entry["path"], target) + authenticate(root) + for entry in entries: + target = root / entry["path"] + original = target.read_bytes() + target.write_bytes(original + b"\n# changed source bytes\n") + try: + load_source(root) + except ValueError as error: + need(str(error).startswith("Source hash mismatch:"), + "Mutated source was executed before authentication") + else: + raise ValueError("Changed source accepted") + target.write_bytes(original) + return len(entries) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path) + args = parser.parse_args() + authenticate() + source = load_source() + out = {"status": "PASS_RANK_CONTACT_REPLAY", "kernel": kernel_checks(source), + "wider": wider_checks(source), "controls": controls(source), + "source_mutations_rejected": source_controls()} + full, engine = complete_replay() + need(engine is not None and engine.preparations == 1240, + "Every consecutive actual contact must be prepared exactly once") + out["complete_retained_caller"] = full + out["rank_calls"] = engine.calls + out["rank_preparations"] = engine.preparations + out["package_sha256"] = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() + for p in sorted(PACKAGE.iterdir()) if p.is_file()} + if args.output: + need(PACKAGE not in args.output.resolve().parents, "Write receipts outside package") + args.output.write_text(json.dumps(out, indent=2, sort_keys=True) + "\n", + encoding="utf-8") + print(json.dumps(out, sort_keys=True)) + + +if __name__ == "__main__": + main()