From 4fdb25a3759fcb86cc92642d97a2a206771a8dfb Mon Sep 17 00:00:00 2001 From: Nick Petalas Date: Thu, 29 Oct 2026 16:36:02 +0000 Subject: [PATCH] perf(research): cache exact H4 projective normals --- research/h4-projective-normal-cache/PROOF.md | 77 +++++++++ research/h4-projective-normal-cache/README.md | 162 ++++++++++++++++++ .../h4-projective-normal-cache/SOURCES.json | 24 +++ research/h4-projective-normal-cache/TASK.json | 67 ++++++++ .../h4-projective-normal-cache/VERIFY.json | 36 ++++ .../h4-projective-normal-cache/accelerator.py | 97 +++++++++++ .../h4-projective-normal-cache/benchmark.py | 66 +++++++ research/h4-projective-normal-cache/replay.py | 12 ++ research/h4-projective-normal-cache/verify.py | 136 +++++++++++++++ 9 files changed, 677 insertions(+) create mode 100644 research/h4-projective-normal-cache/PROOF.md create mode 100644 research/h4-projective-normal-cache/README.md create mode 100644 research/h4-projective-normal-cache/SOURCES.json create mode 100644 research/h4-projective-normal-cache/TASK.json create mode 100644 research/h4-projective-normal-cache/VERIFY.json create mode 100644 research/h4-projective-normal-cache/accelerator.py create mode 100644 research/h4-projective-normal-cache/benchmark.py create mode 100644 research/h4-projective-normal-cache/replay.py create mode 100644 research/h4-projective-normal-cache/verify.py diff --git a/research/h4-projective-normal-cache/PROOF.md b/research/h4-projective-normal-cache/PROOF.md new file mode 100644 index 0000000000..315b089065 --- /dev/null +++ b/research/h4-projective-normal-cache/PROOF.md @@ -0,0 +1,77 @@ +# Exact projective-normal reuse + +This contribution was produced by Nick Petalas. Apache-2.0. + +The input is Jose Manuel / jmbarrancoml's unchanged H4 verifier from +[PR1867](https://github.com/CrocSwap/integer-mult-bounds/pull/1867). +Its mathematical results and complete check domain remain predecessor work. +The claim here is equivalence of one implementation change. + +Write phi for a root of phi^2=phi+1. The field Q(phi) has conjugation +phi'=1-phi. A normal n has four entries a_i+b_i phi in Z[phi]. +Let p=c+d phi be its first nonzero entry. Its field norm is + + D = p conjugate(p) = c^2+cd-d^2 != 0. + +Multiplication of n by conjugate(p) gives the eight integer coefficients + + u_(2i) = a_i(c+d)-b_i d, + u_(2i+1) = b_i c-a_i d. + +The first nonzero field entry of u is the integer D. Divide all eight +coefficients by their positive integer gcd, then negate them if D<0. +Call the resulting primitive integer tuple k(n). + +## The key is exact + +The tuple u/D is exactly the coefficient representation of n/p. Therefore +two normals are proportional over Q(phi) if and only if their tuples u are +proportional over Q. Primitive integer normalization with the same positive +pivot gives + + k(n)=k(m) if and only if Q(phi)n = Q(phi)m. + +This covers arbitrary nonzero field scalars, including nonunits of Z[phi]. +Normalization by the gcd of the original eight coefficients alone would +only remove rational scalars and would miss some reusable normals. + +Nonzero proportional normals have the same annihilated roots. Thus every +cache hit replaces a repeated root scan by the already established fact +that the same projective normal defines the same flat. No approximate key, +modular comparison, hash collision assumption or rounded division is used. + +## First occurrences and complete output + +Both versions enumerate the same 34,220 root triples in the same order. +Both compute each cofactor normal, skip precisely the zero normals and +check cofactor annihilation against all three generating roots. On the +first occurrence of a projective class, the accelerator performs the same +sixty root products and inserts the same flat mask and original normal +with the same `setdefault` operation. + +On later occurrences the original code computes that same flat and leaves +its first normal unchanged. The accelerator leaves it unchanged directly. +Induction over the triple order proves equality of the flat dictionaries, +their insertion order and the exact retained normal coefficients. + +The complete remaining source `main` is unchanged. It receives the same +dictionary, so reflection orbits, chosen witnesses and their order, +support-receipt digest, budget predicates and all eight rejection controls +remain identical. No mathematical theorem is newly certified by the timing +comparison. The complete historical source runs in both conformance sides; +only its `hyperplanes` function is replaced on the accelerated side. + +## Scope and bounds + +The cache belongs to one `hyperplanes` invocation. It contains 1,320 keys +on this fixed sixty-line domain and holds no persisted result or source +state. The normal integer input contract is four pairs with absolute +coefficients at most 2^32. The key is mathematically valid beyond that +bound, but the implementation deliberately rejects larger inputs. + +The universal equivalence argument is written mathematics. The finite +checks compare a separate rational-division key with the integer key, +exercise golden-field scalings, and compare complete flat dictionaries +under four root order/sign variants. Complete original and accelerated +stdout are compared byte for byte. Independent human and formal +proof-kernel review are unclaimed. diff --git a/research/h4-projective-normal-cache/README.md b/research/h4-projective-normal-cache/README.md new file mode 100644 index 0000000000..c16d97b45b --- /dev/null +++ b/research/h4-projective-normal-cache/README.md @@ -0,0 +1,162 @@ +# Complete H4 replay takes 45.2% less wall time + +This contribution was produced by Nick Petalas. Apache-2.0. + +Exact projective-normal reuse reduces the complete H4 certificate replay's +local median wall time from **2.462 s to 1.350 s**, a **45.2% reduction**. +Five alternating fresh-process pairs emit identical complete stdout, with +zero errors. This is a measured verifier improvement with a scoped +implementation-equivalence proof and finite conformance evidence. +No new mathematical bound or kappa is claimed. + +Jose Manuel / jmbarrancoml's [PR1867](https://github.com/CrocSwap/integer-mult-bounds/pull/1867) +retains the H4 theorem, geometry, witness construction and complete +scientific checks. Its source remains unchanged. This package supplies an +opt-in accelerated command. It does not replace the predecessor's default +replay or provide a new scientific review of its theorem. + +## Change and evidence + +The complete-command profile puts 4.805 of 6.493 profiled seconds in +hyperplane construction. Different spanning triples repeatedly produce +normals for the same hyperplane. The accelerator computes an exact +primitive integer key for each normal's Q(sqrt(5)) projective class. +It scans the sixty roots only on the first occurrence of a class. + +All 34,220 triples still undergo cofactor construction. The 33,300 +nonzero normals still undergo the original annihilation checks against +their three generating roots. There are 1,320 distinct projective classes, +so the change avoids 31,980 repeated root scans. It retains the first +original normal, every flat mask and dictionary insertion order. +[PROOF.md](PROOF.md) proves exactness of the key and complete output. + +Conformance checks cover: + +- 624 nonzero test vectors, 272 projective classes and 6,864 nonzero + golden-field scalings against a separate exact rational-division key. +- Four complete original/accelerated flat dictionaries, using the original + order, reverse order, a cyclic order and alternating root signs. Every + mask, first normal and insertion position agrees. +- Byte-identical complete historical replay, including its 5,280 checked + representative support witnesses, reflection orbits, budget checks and + eight inherited corruption rejections. +- Eight new rejections for malformed normals, capacity overflow, a + deliberately colliding key and changed source bytes. The exact capacity + boundary is also accepted. + +The preserved support-witness digest is +`8b0a00c1e8bc012e7f9b620e0ed2c23384f08ea51f7a023f03279e34e9704ab6`. +The complete stdout digest is +`54a65418396d5d8e4989ae02c13e0abe0b34760e6cc43ee3d0e7382ef4482853`. +These are inherited output identities, not new theorem certificates. +The new verifier generates its own source-hash-bound receipt on stdout. + +## Measurement + +CPython 3.12.15, macOS 27.0.1 arm64, eighteen logical CPUs. The opening +load averages were 2.55, 2.73 and 2.60. Other research processes may share +the host. Timed commands run serially, with pair order alternating to +limit drift. Profiling runs are excluded. A preliminary timing run that +overlapped conformance was discarded. + +Each sample includes interpreter startup and the complete replay. +Both sides use `-I -W error -B`, the same interpreter and the same source +domain. The accelerated side also pays for source authentication and +loading. Each subprocess must exit zero, emit no stderr and produce the +same complete stdout. The cache starts empty in each process. + +| Pair | Order | Baseline seconds | Accelerated seconds | +| --- | --- | ---: | ---: | +| 1 | baseline, accelerated | 2.444386166 | 1.366814083 | +| 2 | accelerated, baseline | 2.438297166 | 1.409753166 | +| 3 | baseline, accelerated | 2.462136750 | 1.349535167 | +| 4 | accelerated, baseline | 2.559169750 | 1.342320875 | +| 5 | baseline, accelerated | 2.469625000 | 1.329154208 | + +Baseline range is 2.438 to 2.559 seconds; accelerated range is 1.329 to +1.410 seconds. All five pairs improve. The measured reduction is below +the share spent in the changed component. This is a single-host result, +not a portable performance guarantee. No timing threshold runs in CI. + +## Reproduce + +From the repository root: + +```sh +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/h4-projective-normal-cache/replay.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/h4-projective-normal-cache/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -O -W error -B research/h4-projective-normal-cache/verify.py +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/research_package.py --package research/h4-projective-normal-cache +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -B scripts/check_research_commit.py --package research/h4-projective-normal-cache --check-source-history +/Users/nick/git/integer-mult-bounds/.venv/bin/python3.12 -I -W error -B research/h4-projective-normal-cache/benchmark.py --pairs 5 +``` + +Another CPython 3.12 installation may replace the displayed interpreter. +The registered normal and optimized conformance commands each have a +120-second limit. Direct replay and verification write only stdout. +The benchmark also emits its generated timing receipt only to stdout. +The committed-byte check requires a committed package and full Git history. + +## Scope, provenance and constraints + +Base is canonical main `a5bff3bf133e482541c09eba4e773884bdb2b1b7`, priority +revision 8. The source-binding orientation passed. Selected kappa remains +`0.003125804`. The [advance CLAIM](https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19272415) +and [TASK.json](TASK.json) record this original replay-runtime question. +Live handoffs, open and merged PRs, recent account work, reservations and +distinctive main README searches were checked. Sparse local-note coverage, +unpublished work and arbitrary historical diffs remain limitations. + +[SOURCES.json](SOURCES.json) authenticates the one executed predecessor +file by SHA256 and Git blob. The loader reads those exact bytes and +compiles them with optimization disabled, including when this driver uses +`-O`. The cache is invocation-local and cannot outlive its source instance. +Source changes reject before execution. It does not infer dependencies of +a changed future predecessor. + +New functions are at most 42 physical lines. The fixed sixty-line domain +bounds triple loops by 34,220 iterations and the production cache by 1,320 +classes. Integer input coefficients are bounded by 2^32. The standalone +key tests have 624 vectors and eleven scalars. Benchmarking permits five +through fifteen pairs, with a sixty-second timeout for each subprocess. + +Power of Ten deviations are explicit. CPython and its libraries allocate +dynamically; finite input and collection bounds replace a fixed-allocation +guarantee. The authenticated predecessor retains its existing recursive +clique search, bounded here by clique size thirteen. Source execution and +the one function replacement use dynamic dispatch over exact, pinned +source; the conformance and source-corruption controls exercise that +boundary. These exceptions do not expand the inherited search domain. +Warning-strict normal and optimized runs, syntax/function-size inspection +and the repository package checks supply the diagnostics used here. + +Native implementation, ownership, physical caller, precision, compiler, +outer admission and a complete multiplication theorem are outside this +tooling change. The selected cold replay, Lean and Docker were not run. +Independent human and formal proof-kernel review are unclaimed. + +## Related open work + +No open PR found at the pre-claim checkpoint studies H4 projective-normal +reuse or this complete H4 replay. Other replay-engineering work uses +different sources and bottlenecks. This statement is refreshed in the PR +body before publication. + +## Open next questions + +Can exact cofactor construction be shortened after scan reuse? First +profile the complete accelerated command, then compare an exact algebraic +replacement while retaining all normal and witness outputs. No replacement +or performance result is supplied here. This is a separate future task; +the present contribution stops after its first measured improvement. + +## Credit + +Jose Manuel / jmbarrancoml retains PR1867's mathematical results, +coordinates, complete support checks, witness search and implementation. +Its credited Denney, Bao, Thiago Goulart / Tuafo, Joel Pulikkan, +DreamingOfClouds and retained decoder/accounting predecessors keep their +attribution. Primitive integer normalization, quadratic-field conjugation +and projective equality are classical. The new contribution is this exact +reuse implementation, its equivalence proof, conformance checks and +complete-command measurements. No predecessor credit line was edited. diff --git a/research/h4-projective-normal-cache/SOURCES.json b/research/h4-projective-normal-cache/SOURCES.json new file mode 100644 index 0000000000..7ebfa7aed3 --- /dev/null +++ b/research/h4-projective-normal-cache/SOURCES.json @@ -0,0 +1,24 @@ +{ + "schema_version": 1, + "base_commit": "a5bff3bf133e482541c09eba4e773884bdb2b1b7", + "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/h4-root-linear-copy-floor/verify.py", + "usage": "input", + "git_blob": "7a40c1cc10b54e79313957da61c740c0a3ef2d5b", + "sha256": "431fd177a9a2cef705442e730335e2e457c84440b21a024fce7d6c139ce0d031" + } + ] +} diff --git a/research/h4-projective-normal-cache/TASK.json b/research/h4-projective-normal-cache/TASK.json new file mode 100644 index 0000000000..4301352318 --- /dev/null +++ b/research/h4-projective-normal-cache/TASK.json @@ -0,0 +1,67 @@ +{ + "schema_version": 1, + "task_id": "replay-runtime", + "priority_revision": 8, + "base_commit": "a5bff3bf133e482541c09eba4e773884bdb2b1b7", + "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": "Invocation-local exact projective-normal reuse in the complete PR1867 H4 support-certificate replay, with unchanged complete stdout and every inherited check.", + "scope_change_reason": "A measured hyperplane-construction bottleneck gives one independent implementation question; no predecessor mathematical audit or altered domain is the deliverable.", + "bottleneck_link": "Reducing complete certificate replay time preserves coverage while shortening the wait for source-bound research evidence.", + "budget_minutes": 60, + "stop_rule": "Ship the first source-bound measurable complete-command improvement after conformance and publication checks; do not extend the mechanism.", + "outcome": "completed", + "evidence": "Integer projective-normal key proof, exact rational-key and field-scaling controls, four complete flat-dictionary comparisons and byte-identical complete historical output. Alternating complete-command timing is recorded in README.md.", + "author": "Nick Petalas", + "ai_assistance": "Independent human and formal proof-kernel review are unclaimed.", + "open_questions": [ + { + "question": "Can a complete H4 replay use a shorter exact cofactor computation after projective scan reuse?", + "why_it_matters": "Cofactor construction remains paid for every spanning triple; this is a separate possible replay cost.", + "first_step": "Profile the complete accelerated command and compare its cofactor routine with an exact algebraic replacement, preserving all normal and witness outputs.", + "blocked_by": "No replacement or performance result is provided; the present stop rule ends after one mechanism." + } + ], + "coordination": "https://github.com/CrocSwap/integer-mult-bounds/discussions/1166#discussioncomment-19272415", + "upstream_checkpoint": { + "commit": "a5bff3bf133e482541c09eba4e773884bdb2b1b7", + "priority_revision": 8, + "checked_at_utc": "2026-10-29T16:14:25Z" + } +} diff --git a/research/h4-projective-normal-cache/VERIFY.json b/research/h4-projective-normal-cache/VERIFY.json new file mode 100644 index 0000000000..deaf62c08a --- /dev/null +++ b/research/h4-projective-normal-cache/VERIFY.json @@ -0,0 +1,36 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/h4-root-linear-copy-floor/verify.py" + ], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Exact projective keys, golden-field scaling, four complete original/accelerated hyperplane dictionaries including first normals and order, byte-identical complete historical replay, all eight inherited controls and eight new rejection controls." + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-W", + "error", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "The same complete conformance and rejection domains with the driver optimized; retained source is compiled with optimize=0." + } + ] +} diff --git a/research/h4-projective-normal-cache/accelerator.py b/research/h4-projective-normal-cache/accelerator.py new file mode 100644 index 0000000000..7317b39b3b --- /dev/null +++ b/research/h4-projective-normal-cache/accelerator.py @@ -0,0 +1,97 @@ +"""Exact, invocation-local reuse of H4 projective normal classes. + +This contribution was produced by Nick Petalas. Apache-2.0. +""" + +from collections import Counter +from hashlib import sha256 +from itertools import combinations +import json +from math import gcd +from pathlib import Path +import types + +HERE = Path(__file__).resolve().parent +SOURCE = "research/h4-root-linear-copy-floor/verify.py" +LIMIT = 1 << 32 + + +def need(condition, message): + if not condition: + raise ValueError(message) + + +def read_bounded(path, capacity): + with path.open("rb") as stream: + raw = stream.read(capacity + 1) + need(len(raw) <= capacity, "file capacity: " + str(path)) + return raw + + +def load_source(): + manifest = json.loads(read_bounded(HERE / "SOURCES.json", 65536)) + pins = [row for row in manifest["files"] if row["path"] == SOURCE] + need(len(pins) == 1 and pins[0]["usage"] == "input", "one live source pin") + path = HERE.parents[1] / SOURCE + need(path.is_file() and not path.is_symlink(), "regular retained source") + raw = read_bounded(path, 131072) + need(sha256(raw).hexdigest() == pins[0]["sha256"], "source SHA256") + source = types.ModuleType("authenticated_h4_source") + source.__file__ = str(path) + exec(compile(raw, str(path), "exec", optimize=0), source.__dict__) + return source + + +def projective_key(vector): + """Primitive integer representative after conjugating the first pivot.""" + need(isinstance(vector, tuple) and len(vector) == 4, "normal arity") + need(all(isinstance(x, tuple) and len(x) == 2 for x in vector), "field pairs") + need(all(type(a) is int and abs(a) <= LIMIT for x in vector for a in x), + "integer normal capacity") + pivot = next((x for x in vector if x != (0, 0)), None) + need(pivot is not None, "nonzero normal") + c, d = pivot + norm = c * c + c * d - d * d + need(norm != 0, "nonzero field norm") + # phi' = 1-phi; (a+b phi)(c+d-d phi). + values = tuple(z for a, b in vector for z in + (a * (c + d) - b * d, b * c - a * d)) + divisor = 0 + for value in values: + divisor = gcd(divisor, value) + need(divisor > 0, "nonzero primitive divisor") + signed_divisor = divisor if norm > 0 else -divisor + return tuple(value // signed_divisor for value in values) + + +def hyperplanes(roots, source, stats=None): + need(len(roots) == 60, "H4 root-line capacity") + flats, known = {}, {} + nonzero = 0 + for indices in combinations(range(60), 3): + rows = [roots[i] for i in indices] + normal = source.normal(rows) + if all(x == source.ZERO for x in normal): + continue + nonzero += 1 + source.need(all(source.dot(normal, row) == source.ZERO for row in rows), + "cofactor annihilation") + key = projective_key(normal) + if key not in known: + flat = source.mask(i for i, root in enumerate(roots) + if source.dot(normal, root) == source.ZERO) + known[key] = flat + flats.setdefault(flat, normal) + source.need(Counter(x.bit_count() for x in flats) == {4: 600, 6: 660, 15: 60}, + "flat census") + need(len(known) == len(flats), "one projective class per flat") + if stats is not None: + stats.update(triples=34220, nonzero_normals=nonzero, classes=len(known), + root_scans=len(known), avoided_root_scans=nonzero-len(known)) + return flats + + +def accelerated_source(): + source = load_source() + source.hyperplanes = lambda roots: hyperplanes(roots, source) + return source diff --git a/research/h4-projective-normal-cache/benchmark.py b/research/h4-projective-normal-cache/benchmark.py new file mode 100644 index 0000000000..a9305af727 --- /dev/null +++ b/research/h4-projective-normal-cache/benchmark.py @@ -0,0 +1,66 @@ +"""Alternate complete H4 replay commands; write the timing receipt to stdout. + +This contribution was produced by Nick Petalas. Apache-2.0. +""" + +import argparse +from hashlib import sha256 +import json +import os +from pathlib import Path +import platform +from statistics import median +import subprocess +import sys +from time import perf_counter + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--pairs", type=int, choices=range(5, 16), default=5) + args = parser.parse_args() + scripts = {"baseline": ROOT / "research/h4-root-linear-copy-floor/verify.py", + "accelerated": HERE / "replay.py"} + commands = {name: [sys.executable, "-I", "-W", "error", "-B", str(script)] + for name, script in scripts.items()} + samples, expected = [], None + for pair in range(args.pairs): + order = ("baseline", "accelerated") if pair % 2 == 0 else ("accelerated", "baseline") + for name in order: + start = perf_counter() + run = subprocess.run(commands[name], cwd=ROOT, capture_output=True, + timeout=60, check=False) + elapsed = perf_counter() - start + if run.returncode or run.stderr or len(run.stdout) > 8192: + raise ValueError("complete benchmark command failed: " + repr(run)) + result = json.loads(run.stdout) + if result["status"] != "PASS_H4_ROOT_LINEAR_COPY_FLOOR": + raise ValueError("benchmark work did not complete") + if expected is None: + expected = run.stdout + if run.stdout != expected: + raise ValueError("complete output changed") + samples.append({"pair": pair, "side": name, "seconds": elapsed, + "exit_code": run.returncode, + "stdout_sha256": sha256(run.stdout).hexdigest()}) + summary = {} + for name in scripts: + times = [x["seconds"] for x in samples if x["side"] == name] + summary[name] = {"median_seconds": median(times), "range_seconds": [min(times), max(times)]} + gain = 1 - summary["accelerated"]["median_seconds"] / summary["baseline"]["median_seconds"] + print(json.dumps({"status": "PASS_COMPLETE_H4_BENCHMARK", "errors": 0, + "python": sys.version, "platform": platform.platform(), + "cpu_count": os.cpu_count(), "load_average": os.getloadavg(), + "commands": commands, "samples": samples, "summary": summary, + "wall_time_reduction": gain, + "implementation_sha256": {name: sha256((HERE / name).read_bytes()).hexdigest() + for name in ("accelerator.py", "replay.py", "benchmark.py", "SOURCES.json")}, + "source_sha256": {n: sha256(p.read_bytes()).hexdigest() for n, p in scripts.items()}}, + indent=2, sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/research/h4-projective-normal-cache/replay.py b/research/h4-projective-normal-cache/replay.py new file mode 100644 index 0000000000..95006f79fb --- /dev/null +++ b/research/h4-projective-normal-cache/replay.py @@ -0,0 +1,12 @@ +"""Run the complete H4 verifier with exact projective-normal reuse. + +This contribution was produced by Nick Petalas. Apache-2.0. +""" + +from pathlib import Path +import runpy + + +if __name__ == "__main__": + api = runpy.run_path(str(Path(__file__).resolve().with_name("accelerator.py"))) + api["accelerated_source"]().main() diff --git a/research/h4-projective-normal-cache/verify.py b/research/h4-projective-normal-cache/verify.py new file mode 100644 index 0000000000..2258b01ed3 --- /dev/null +++ b/research/h4-projective-normal-cache/verify.py @@ -0,0 +1,136 @@ +"""Conformance and failure controls for projective-normal reuse. + +This contribution was produced by Nick Petalas. Apache-2.0. +""" + +from contextlib import redirect_stdout +from fractions import Fraction +from hashlib import sha256 +from io import StringIO +from itertools import product +import json +from pathlib import Path +import runpy +from unittest.mock import patch + +HERE = Path(__file__).resolve().parent +API = runpy.run_path(str(HERE / "accelerator.py")) +need = API["need"] + + +def rational_key(vector): + c, d = next(x for x in vector if x != (0, 0)) + denominator = c * c + c * d - d * d + return tuple((Fraction(a * (c + d) - b * d, denominator), + Fraction(b * c - a * d, denominator)) for a, b in vector) + + +def check_keys(source): + alphabet = ((0, 0), (1, 0), (-1, 0), (0, 1), (0, -1)) + scalars = tuple(x for x in product(range(-1, 2), repeat=2) if x != (0, 0)) + scalars += ((2, 0), (0, 2), (-1, 2)) + forward, backward = {}, {} + vectors = scaled = 0 + for vector in product(alphabet, repeat=4): + if not any(x != (0, 0) for x in vector): + continue + key, exact = API["projective_key"](vector), rational_key(vector) + need(forward.setdefault(key, exact) == exact, "no false key collision") + need(backward.setdefault(exact, key) == key, "no split projective class") + vectors += 1 + for scalar in scalars: + other = tuple(source.mul(x, scalar) for x in vector) + need(API["projective_key"](other) == key, "field-scale invariance") + need(rational_key(other) == exact, "independent scale comparison") + scaled += 1 + return {"vectors": vectors, "projective_classes": len(forward), + "nonzero_field_scalings": scaled} + + +def check_flats(source): + roots, _ = source.geometry() + permutations = (roots, roots[::-1], roots[17:] + roots[:17], + [source.negate(r) if i % 2 else r for i, r in enumerate(roots)]) + counts = [] + for variant in permutations: + stats = {} + expected = source.hyperplanes(variant) + actual = API["hyperplanes"](variant, source, stats) + need(list(expected.items()) == list(actual.items()), + "complete flat masks, first normals and insertion order") + counts.append(stats) + return counts + + +def complete_output(source): + output = StringIO() + with redirect_stdout(output): + source.main() + raw = output.getvalue() + need(len(raw) <= 8192, "complete output capacity") + result = json.loads(raw) + need(result["status"] == "PASS_H4_ROOT_LINEAR_COPY_FLOOR", "complete status") + return raw, result + + +def check_controls(source): + zero = ((0, 0),) * 4 + bad = (zero, zero[:3], ((1, 0),) * 3 + ([1, 0],), + ((True, 0),) + zero[1:], ((1.0, 0),) + zero[1:], + ((API["LIMIT"] + 1, 0),) + zero[1:]) + rejected = 0 + for vector in bad: + try: + API["projective_key"](vector) + except ValueError: + rejected += 1 + else: + raise ValueError("invalid normal accepted") + API["projective_key"](((API["LIMIT"], 0),) + zero[1:]) + roots, _ = source.geometry() + namespace = API["hyperplanes"].__globals__ + with patch.dict(namespace, {"projective_key": lambda vector: (1,)}): + try: + API["hyperplanes"](roots, source) + except ValueError: + rejected += 1 + else: + raise ValueError("colliding key mutation accepted") + return rejected + + +def check_source_control(): + read = API["read_bounded"] + def changed(path, capacity): + raw = read(path, capacity) + return raw + b"\n" if path.as_posix().endswith(API["SOURCE"]) else raw + with patch.dict(API["load_source"].__globals__, {"read_bounded": changed}): + try: + API["load_source"]() + except ValueError as error: + need(str(error) == "source SHA256", "source corruption rejection phase") + else: + raise ValueError("changed source accepted") + + +def main(): + source = API["load_source"]() + keys = check_keys(source) + flats = check_flats(source) + baseline, result = complete_output(source) + accelerated, _ = complete_output(API["accelerated_source"]()) + need(baseline == accelerated, "byte-identical complete historical stdout") + controls = check_controls(source) + check_source_control() + files = sorted(p for p in HERE.iterdir() if p.is_file()) + need(len(files) <= 16, "package inventory capacity") + hashes = {p.name: sha256(API["read_bounded"](p, 1048576)).hexdigest() for p in files} + print(json.dumps({"status": "PASS_H4_PROJECTIVE_NORMAL_CACHE", "keys": keys, + "flat_variants": flats, "new_rejections": controls + 1, + "complete_original_result": result, + "complete_stdout_sha256": sha256(baseline.encode()).hexdigest(), + "package_sha256": hashes, "no_new_kappa": True}, sort_keys=True)) + + +if __name__ == "__main__": + main()