diff --git a/research/quadratic-affine-packed/BENCHMARK.json b/research/quadratic-affine-packed/BENCHMARK.json new file mode 100644 index 0000000000..f885136e2d --- /dev/null +++ b/research/quadratic-affine-packed/BENCHMARK.json @@ -0,0 +1,78 @@ +{ + "started_utc": "2026-10-13T04:57:42.530075+00:00", + "python": "3.14.2 (main, Dec 5 2025, 16:49:16) [Clang 17.0.0 (clang-1700.4.4.1)]", + "executable": "/opt/homebrew/opt/python@3.14/bin/python3.14", + "platform": "macOS-26.3-arm64-arm-64bit-Mach-O", + "machine": "arm64", + "pairs": 3, + "timing_scope": "Fresh serial child wall time including interpreter, imports, source checks, complete audit, certificate comparison and output", + "os_caches_flushed": false, + "samples": [ + { + "pair": 1, + "engine": "original", + "wall_seconds": 73.41088783298619, + "certificate_sha256": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=original compare_rows=false\n", + "stderr": "", + "returncode": 0 + }, + { + "pair": 1, + "engine": "packed", + "wall_seconds": 11.063003709015902, + "certificate_sha256": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=packed compare_rows=false\n", + "stderr": "", + "returncode": 0 + }, + { + "pair": 2, + "engine": "packed", + "wall_seconds": 10.959382041008212, + "certificate_sha256": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=packed compare_rows=false\n", + "stderr": "", + "returncode": 0 + }, + { + "pair": 2, + "engine": "original", + "wall_seconds": 74.10734995896928, + "certificate_sha256": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=original compare_rows=false\n", + "stderr": "", + "returncode": 0 + }, + { + "pair": 3, + "engine": "original", + "wall_seconds": 73.32691987499129, + "certificate_sha256": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=original compare_rows=false\n", + "stderr": "", + "returncode": 0 + }, + { + "pair": 3, + "engine": "packed", + "wall_seconds": 9.665873582998756, + "certificate_sha256": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=packed compare_rows=false\n", + "stderr": "", + "returncode": 0 + } + ], + "source_manifest_sha256": "5123e5ce87e5f34446460842fcdbcfedd823aa0a4f284f5af18e66deaaf3e034", + "implementation_sha256": { + "packed.py": "366a8046fc420eaa36ae397eec35928b91284b60f2d14f56565006ad6cd6f538", + "replay.py": "398ae9b8694103f4dcbd5cebd2786a079c62dccce1d4958a324da65f9dd70068", + "benchmark.py": "a9be1a9ab43fc6821ec4f2d81115b277c063734cfe084e31373c5f5c08449015" + }, + "median_seconds": { + "original": 73.41088783298619, + "packed": 10.959382041008212 + }, + "observed_speedup": 6.698451386975532, + "finished_utc": "2026-10-13T05:01:55.085331+00:00" +} diff --git a/research/quadratic-affine-packed/README.md b/research/quadratic-affine-packed/README.md new file mode 100644 index 0000000000..c2efb0bf99 --- /dev/null +++ b/research/quadratic-affine-packed/README.md @@ -0,0 +1,196 @@ +# Faster exact quadratic-affine witness replay + +**Verification tooling; no new kappa or new negative result.** This optional +adapter constructs all rows of Douglas Colkitt's twelve frozen quadratic-affine +rank witnesses using column-parallel integer arithmetic. It retains every +column, the diagonal, row order, binary elimination and source-hashed +certificate. It leaves the original checker and default commands unchanged. + +The base is `66849e1475c930fd7732b1a0067ffee7e1e95f60`. At that base the selected +conditional result is #626 with #543, kappa `0.001635527380375392`; the historical +certificate's `2^-31` retained-kappa field is not the current frontier. +The selected audited #626 source is `660de893d27bb7abd1d5b01b1640207528a5c851`, +with #543 at `2065fe4f72a914d907f1046603f5a36f17018535`; neither is a new code +dependency of this historical replay. + +## Reproduce + +Python 3.11+ and its standard library are sufficient. From the repository root: + +```sh +python3 -I -B research/quadratic-affine-packed/verify.py +``` + +Expected final line: + +```text +PASS_QUADRATIC_AFFINE_DIFFERENTIAL all_frozen_rows_equal=true cases=12 +``` + +This runs the new arithmetic and failure controls, all four unchanged original +test groups, then compares each of **14,662 complete rows** directly with the +original pairwise calculation. The declared minors contain **161,333,572 matrix +entries**, including their diagonals. It then performs the original elimination +and rational span checks and compares the entire serialized certificate with +the frozen source. The largest minor has 12,623 claimed independent rows and +12,687 columns. No row or column is sampled or removed. + +For ordinary optimized replay without the deliberately costly differential +oracle: + +```sh +python3 -I -B research/quadratic-affine-packed/replay.py +``` + +Expected: `PASS_QUADRATIC_AFFINE_PACKED cases=12`, with certificate SHA-256 +`b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f`, +`engine=packed`, and `compare_rows=false`. An optional `--out` must name a fresh +file. The default writes no certificate into the checkout. `--engine original` +runs the original audit through the same source and certificate guards. + +Reproduce the complete-audit A/B measurement with: + +```sh +python3 -I -B research/quadratic-affine-packed/benchmark.py \ + --pairs 3 --out /tmp/quadratic-affine-benchmark.json +``` + +The output path must not already exist. Each child uses identical frozen inputs +and starts a fresh interpreter. Order alternates original/packed, packed/original, +original/packed. Interpreter startup, imports, source checks, all twelve +witnesses, rational span checks, certificate comparison and output are included. +Every generated certificate must match the same bytes. Timing has no pass +threshold; OS caches are not flushed. This measures this historical audit, +not the selected construction or the whole repository suite. Raw observations +and environment details are retained in `BENCHMARK.json`. + +On CPython 3.14.2 / macOS 26.3 arm64, the three alternating pairs measured +**73.411 seconds original versus 10.959 seconds packed** by median, an observed +**6.70x speedup** (85.1% less elapsed time). The six individual observations are +preserved; the packed times range from 9.666 to 11.063 seconds. This is a local +measurement, not a hardware-independent performance guarantee. + +## Exact arithmetic argument + +Write a sampled vector's coordinate as `w[j,k] = a[j,k] + i*b[j,k]`, where the +coordinate is zero or a Gaussian unit. For `N` columns and `D <= 32`, set +`B = 256`, `U = sum(B**j for j in range(N))`, and construct the signed integers + +```text +R[k] = sum(a[j,k] * B**j for j in range(N)) +I[k] = sum(b[j,k] * B**j for j in range(N)). +``` + +For query `v[k] = x[k] + i*y[k]`, compute + +```text +P = D*U + sum(x[k]*R[k] + y[k]*I[k] for k in range(D)) +Q = D*U + sum(x[k]*I[k] - y[k]*R[k] for k in range(D)). +``` + +These are integer polynomial identities: each final digit is `D` plus the real +or imaginary part of `conj(v) dot w[j]`. Each component has absolute value at +most `D`, so every digit lies in `[0, 2D]`, a subset of `[0, 64]`. Thus the final +base-256 digits are exactly those sums, with no carry between columns. The +signed intermediate integers may be negative; intermediate borrowing does not +change the final polynomial identity. No floating point, modular approximation +or probabilistic identity test is used. + +Set `Z = (P xor D*U) or (Q xor D*U)`. Every digit of `Z` is below 128 and is zero +exactly for a zero Hermitian product. Adding `127*U` gives digits at most 254, +so again there is no carry. Its byte high bit is one exactly for a nonzero +digit. Therefore + +```text +((128*U) xor ((Z + 127*U) and (128*U))) >> 7 +``` + +has bit `8*j` set exactly for orthogonality. This uses carry-free addition; +the familiar subtract-ones zero-byte trick can misclassify adjacent lanes and +is an explicit negative control. + +Finally compress the spaced bits. At a step with `k` a power of two, each +block's `k` low bits sit at positions `8*k*g`. Shifting by `7*k` places an odd +block directly above the preceding even block; OR and a mask keeping `2*k` +bits at positions `16*k*g` merges each pair. Induction doubles the compact +block length until all columns occupy consecutive bits. The final `N`-bit mask +discards padding. Adding bit `i` makes row `i` of **I plus the full +orthogonality relation**, exactly as in the original checker. + +The original ordered GF(2) elimination and independence rejection are retained. +Its row digest is computed before elimination and includes the same byte +padding. The family construction, support counts, rational real span controls, +Gaussian coordinate basis checks, source verification, deficit inequalities and +receipt fields run unchanged in the original audit. `rank_witness` copies its +short validation/elimination/receipt routine with attribution; only row +construction changes. No successful rank result is cached. +`zero_lanes` is an internal decoder for the bounded digits returned by +`products`; arbitrary integers outside that contract are not supported. + +## Coverage, negative controls and limits + +The independent component oracle uses literal integer pairs, without the +original packed inner-product function. It covers all 125 vectors with three +coordinates in `{0, 1, i, -1, -i}` (15,625 products), all 60 Gaussian labels at +`t=2`, both signed extremes at `D=32`, and deterministic seed-92 samples for +every dimension 1 through 32. The fixed stopping rule is completion of these +tests and all twelve shipped witnesses; this is not a mathematical search. + +Additional controls cover all 4,225 allowed offset-digit pairs at `D=32`, +sample sizes around byte and power-of-two boundaries through 257, nonmonotone +sample/row order, and original rejection messages for invalid or dependent +witnesses. Deliberate errors in conjugation, imaginary-part checking, diagonal +insertion and zero-byte detection are detected. The diagonal control preserves +the small example's rank but changes the row digest, demonstrating why rank +alone is insufficient. Changed source, fixture, certificate or upstream +manifest, disabled assertions, and preexisting output are rejected. + +This preserves the existing negative result's exact scope: the twelve specified +full-orthogonality central matrices fail the positive-deficit condition. It +does not exclude other supported matrices, subfamilies, phase families or +compilers. It is not a formal proof of the full multiplication theorem. The +current construction's all-size/compiler/common-label/mutable-basis, hosting, +precision/tape/recovery and analytic assumptions remain untouched. + +## Provenance and concurrent work + +`SOURCES.json` pins all 37 predecessor files loaded or consumed by the replay, +including the complete imported local Python closure, frozen fixture, original +certificate and documentation, and the original upstream manifest and files. +The historical kernel first appears at +`d55bcd62ceb8f322bcf311a5a66e94c3ac7724f0`; upstream is +`adc7f1241b42e322a6451854ab7e4b4c146bf78a`. The adapter refuses source drift before +importing the audit. No predecessor file, `upstream/` file, selected record, +certificate or global Makefile is changed. + +OS-random assignment seed **92** selected verification engineering. Three +candidates, the selection-time claim, time budget and overlap checks are in +`SELECTION.json`. The live local sprint claim is retained beside the checkout +and updated with the final PR link after submission. +No pinned coordination issue existed. The initial check covered 544 open PRs. +PR154 by eumemic identified `quadratic_affine.inner` as an unchanged costly +kernel; this contribution follows that useful profiling lead. PR154's parallel +scheduling, PR367's frame algebra, PR452's E8 replay, PR662's Clifford helper +check, PR667/672's interval engines, PR707's Gram prefixes, PR709's R420 geometry +cache, and PR721's archive streaming concern different mechanisms. No pending +implementation is imported or required. +The final refresh covered 565 open PRs through #746. New tooling #727 uses +owner-footprint masks and #736 skips cyclic-inverse zero row updates; neither +changes the quadratic-affine rank-witness replay. +The immediate pre-submission arrival check through #748 also found no overlap. + +Local validation passed all 19 test groups, the complete direct row comparison, +all six timed complete audit replays, the selected source-inventory check and +whitespace validation. `VALIDATION.json` preserves commands and observations. +The repository-wide `make verify`, selected supplier cold replay, Lean and +remote Linux CI were not run for this isolated optional adapter. Their results +are not inferred from these local checks. + +Original quadratic-affine families, witnesses, finite obstruction and audit: +**Douglas R. Colkitt**, with the original OpenAI Codex assistance disclosure. +New packed arithmetic, adapter, evidence and tests: **Curtis Dransfield +(@Curtis-D), with substantial OpenAI Codex assistance**, including a separate +scoped AI arithmetic reviewer and test author. AI review is not independent +human review. Apache-2.0; see the repository `LICENSE` and `NOTICE` for retained +attribution. The original work motivates its families with Dehaene and De Moor; +that background is not a new claim of this optimization. diff --git a/research/quadratic-affine-packed/SELECTION.json b/research/quadratic-affine-packed/SELECTION.json new file mode 100644 index 0000000000..a35633e983 --- /dev/null +++ b/research/quadratic-affine-packed/SELECTION.json @@ -0,0 +1,53 @@ +{ + "track": "verification engineering", + "assignment_seed": 92, + "seed_source": "Python secrets.randbelow(100), operating-system randomness", + "base_commit": "66849e1475c930fd7732b1a0067ffee7e1e95f60", + "started_utc": "2026-10-13T04:50:44Z", + "claimed_utc": "2026-10-13T04:54:06.744099+00:00", + "expected_finish_utc": "2026-10-13T05:30:44Z", + "verification_reserve_starts_utc": "2026-10-13T05:20:44Z", + "coordination": "GitHub pinnedIssues query returned no nodes; local claim only", + "candidates": [ + { + "object": "selected R420 geometry reuse", + "decision": "declined: directly overlaps PR709" + }, + { + "object": "ternary-side common-pair DAG signature generation", + "decision": "declined: broader graph/invoice verification surface" + }, + { + "object": "quadratic-affine rank-witness row construction", + "decision": "selected: seed 92 modulo 3 is 2; historical negative-result replay, no matching optimization found" + } + ], + "scope": "Exact packed-integer construction of all orthogonality rows for the twelve frozen quadratic-affine rank witnesses. Preserve every column, row order, diagonal entry, digest, binary elimination, rejection, and certificate. Optional adapter only.", + "hypothesis": "Column-parallel bounded integer lanes can replace pairwise Hermitian inner-product calls without weakening exact row coverage.", + "method": "Carry-safe base-256 dot-product lanes and exact zero-lane extraction; compare complete certificates and same-input fresh-process timing.", + "deliverable": "Source-pinned optional verifier, exact differential and negative controls, benchmark evidence, proof of lane arithmetic, README and focused PR.", + "overlap": { + "open_pr_count": 544, + "max_pr_number": 725, + "relevant_prs": [ + 154, + 367, + 452, + 662, + 667, + 672, + 707, + 709, + 721 + ], + "notes": "PR154 reports quadratic_affine.inner as an unchanged 168-second hotspot; current kernel is already bit-packed per pair. This changes row construction across columns. Other performance PRs concern distinct kernels." + }, + "record_type": "selection-time snapshot; completed validation is in VALIDATION.json", + "verification_and_packaging_started_utc": "2026-10-13T05:01:15Z", + "final_overlap": { + "open_pr_count": 565, + "max_pr_number": 746, + "result": "No overlapping quadratic-affine packed-row implementation. PR154 retains its earlier profiling credit. New arrivals concern other scopes.", + "immediate_pre_submission_latest_pr": 748 + } +} diff --git a/research/quadratic-affine-packed/SOURCES.json b/research/quadratic-affine-packed/SOURCES.json new file mode 100644 index 0000000000..905d67cd64 --- /dev/null +++ b/research/quadratic-affine-packed/SOURCES.json @@ -0,0 +1,44 @@ +{ + "base_commit": "66849e1475c930fd7732b1a0067ffee7e1e95f60", + "historical_source_commit": "d55bcd62ceb8f322bcf311a5a66e94c3ac7724f0", + "upstream_commit": "adc7f1241b42e322a6451854ab7e4b4c146bf78a", + "files": { + "certificates/quadratic-affine-vectors.json": "b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f", + "docs/research/quadratic-affine-vectors.md": "2fefaa114b274944c7eaf26297c062d76a7a5e18c541e40479250ed6d1a8e084", + "scripts/audit_cancellation.py": "508934ee4d9514a5519813603b6192a0d53103b6e30d65b8d53a1a774de244b6", + "scripts/audit_fused_block.py": "2afb83ca8e5f193ca8a4c242becf267ad1d9dc2578ba238b820e98d5185f6288", + "scripts/audit_joint_frames.py": "3df8061392ce0ba42bbde8c02f2a30c8141145e70e9a48c18da73d3aafb14423", + "scripts/audit_quadratic_affine.py": "9b7b3a7b8bcf417d95f19e44ff488733e7a3f4757fd12bd98d417e1d01d70425", + "scripts/certify.py": "2348d95008276c5d5764f7e028c3918e484582cd589bd4d9b82919161745e53e", + "scripts/exclusion_circuit.py": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", + "scripts/experiments/quadratic_affine.py": "8f9252bd092eb14779997c6767e090377e91f4d57e6526e9840ae0dd7dcc8f9c", + "scripts/experiments/quadratic_affine_witnesses.json": "3efcebf41650681a8a21a901cd2f81dc07a0c4d28064b883af16b10c3e0036c3", + "scripts/experiments/single_intersection.py": "08ddae46e5621a5b4c525b504e67ae655abe9b49461d894feaa7abc89a51847d", + "scripts/paired_exclusion_circuit.py": "8f05f04cf06217befd6d0ee0f2d7d0c5c5eb32cfab8c9a8ca7917fd6d620b7d2", + "scripts/paired_network.py": "991223dc3c1374284c361ed017e42bf732dd971fa0bea2be2282cfb18257b3f7", + "scripts/prepare_layers.py": "a6e2adc612f264b1c0e51d18ce6315de9ae5e2ae2420aa22e99a22d8bf304947", + "scripts/search_network.py": "3a4faa5037d9b8e7ca5828afac193a96b6966a8b114f14937921795b24ec4f6a", + "scripts/shared_point_circuit.py": "c9bb7a5df92d260a4e3ad532b53e26936730d7c93f7facfb0784957c1e81c8ba", + "scripts/shared_point_network.py": "a85e7f0d8c42d6a27e3846308571ca324a1824ac5c2d59e4daa5c7673c33c697", + "upstream/LICENSE": "c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4", + "upstream/README.md": "8d1ceff898a4e0e681679f91c4def485820be9e99f04e4861f2ce601a364723e", + "upstream/build/figures/multiplication-flow.tex": "3654d2728b0c98919d61760e1802804ed616546a747f9167a93e17bb8697f320", + "upstream/build/figures/proof-map.tex": "76d4deacf07bea355412abcfd798c7cbe6dcccef29a4a420a9e2a7b158f23848", + "upstream/build/main.tex": "42f0f506976bc2084374a71e97edc1549689a7accf35dda4db2a18af4f13c1aa", + "upstream/build/references.bib": "33cc2df13ccf85330849e8fb34d4ac7613b23f625787457a5b7b9aa6d7369476", + "upstream/build/sections/00-introduction.tex": "98721f03682ccf1cc7d0850b07adabaa2d8f3afe552e3861753eba9d284d0da0", + "upstream/build/sections/01-history.tex": "798956c8150c31e3e2f5f29c47110d25ce619faad1374f4d3a0f15fa3ee33ead", + "upstream/build/sections/02-streams.tex": "606c80db61cad13aa0c3b6060dc4b88dbbe7ac60cdd831aa93f28d908fd09dab", + "upstream/build/sections/03-motifs.tex": "ed76761194988a01af77ead0ea6469cb5dcd29690ab985a0b5a4006b0f2b69bb", + "upstream/build/sections/04-swap.tex": "412b170ccaab38dba96e95e3cbfae09f4876a9818f8d278776a746b0f8044906", + "upstream/build/sections/05-layers.tex": "20cfc8d12099d4e4ed9e3e7eeb6162edd9b6e0e076f24693364cd24377252594", + "upstream/build/sections/06-transforms.tex": "924316e4bc905066281d8f9e8746a49d8829aeb393688608deb1e61c3c87853c", + "upstream/build/sections/07-resampling.tex": "8a6b91670f9163afb54892489fa2210c77fe1b058f0325ec7d18b44b94be6e2c", + "upstream/build/sections/08-assembly.tex": "763d7945b1e1ec4ebae9b799bcefcc45fa9bae6e2ffad799868a9c3d513dbd4a", + "upstream/build/sections/09-exact-arithmetic.tex": "5714e5d0656c78c6a1fde8d01fd11f1ba856a7ea94149b283bd9a50fdbf516c3", + "upstream/build/sections/10-transposition.tex": "c14ad8d56112f6578678a58211bc8d63c3a4b60e55078ac73bc38f7348e713cb", + "upstream/build/sections/11-grouped-rectangles.tex": "6525cd0a01980e56accfeaf06e9d875bc699197ba42c6e22b69435e352275174", + "upstream/formalization.yaml": "2dcbd0d6e6a22475f53d49bfdfcac9b6d2b470653c332a9f56522f5be166edb9", + "upstream/manifest.json": "16ab13971694cabab09440c47ebf2fc0496a9d35fc3af33950fda3498f8e2392" + } +} diff --git a/research/quadratic-affine-packed/VALIDATION.json b/research/quadratic-affine-packed/VALIDATION.json new file mode 100644 index 0000000000..cd42f9149f --- /dev/null +++ b/research/quadratic-affine-packed/VALIDATION.json @@ -0,0 +1,69 @@ +{ + "status": "PASS; optional verification tooling; no new kappa", + "base_commit": "66849e1475c930fd7732b1a0067ffee7e1e95f60", + "full_differential": { + "command": [ + "python3", + "-I", + "-B", + "research/quadratic-affine-packed/verify.py" + ], + "returncode": 0, + "seconds": 75.56934225000441, + "completed_utc": "2026-10-13T05:03:33.477448+00:00", + "log_sha256": "e0b26c26e3bccd55f542210b5f405b370e7945fa19e6756fe518e591adc1d028", + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=packed compare_rows=true\nPASS_QUADRATIC_AFFINE_DIFFERENTIAL all_frozen_rows_equal=true cases=12\n", + "stderr": "test_all_offset_digit_pairs (__main__.PackedArithmetic.test_all_offset_digit_pairs) ... ok\ntest_compaction_boundary_counts_and_positions (__main__.PackedArithmetic.test_compaction_boundary_counts_and_positions) ... ok\ntest_every_small_gaussian_label (__main__.PackedArithmetic.test_every_small_gaussian_label) ... ok\ntest_every_three_coordinate_phase_vector (__main__.PackedArithmetic.test_every_three_coordinate_phase_vector) ... ok\ntest_negative_control_cross_lane_borrow (__main__.PackedArithmetic.test_negative_control_cross_lane_borrow) ... ok\ntest_negative_controls_conjugation_imaginary_diagonal (__main__.PackedArithmetic.test_negative_controls_conjugation_imaginary_diagonal) ... ok\ntest_seed_92_all_dimensions (__main__.PackedArithmetic.test_seed_92_all_dimensions) ... ok\ntest_signed_extremes_at_maximum_dimension (__main__.PackedArithmetic.test_signed_extremes_at_maximum_dimension) ... ok\ntest_validation_and_input_snapshot (__main__.PackedArithmetic.test_validation_and_input_snapshot) ... ok\ntest_receipt_detects_diagonal_mutation (__main__.RankReplay.test_receipt_detects_diagonal_mutation) ... ok\ntest_receipts_and_nonmonotone_orders_match_original (__main__.RankReplay.test_receipts_and_nonmonotone_orders_match_original) ... ok\ntest_same_rejections_as_original (__main__.RankReplay.test_same_rejections_as_original) ... ok\n\n----------------------------------------------------------------------\nRan 12 tests in 0.225s\n\nOK\n...\n----------------------------------------------------------------------\nRan 3 tests in 0.236s\n\nOK\n....\n----------------------------------------------------------------------\nRan 4 tests in 0.053s\n\nOK\n" + }, + "portable_replay": { + "returncode": 0, + "stdout": "PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=b55506bcf2334eebc07ce31078edb33282ff7d9bb57c40a9e4d5209d749dcd7f engine=packed compare_rows=false\n", + "stderr": "", + "seconds": 9.747994582983665, + "isolated": true, + "path_with_spaces": true, + "unrelated_working_directory": true, + "all_copied_files_unchanged": true + }, + "benchmark_file": "BENCHMARK.json", + "benchmark_sha256": "2460444afb28500f685a90c92743e513bd3f456b99133b10f2edc9eebe4eb08d", + "source_manifest_sha256": "5123e5ce87e5f34446460842fcdbcfedd823aa0a4f284f5af18e66deaaf3e034", + "complete_rows": 14662, + "declared_matrix_entries": 161333572, + "test_groups": { + "packed": 12, + "source_and_cli": 3, + "unchanged_original": 4 + }, + "additional_checks": [ + { + "command": "python3 -B scripts/verify_selected_result.py", + "result": "PASS selected record, exact kappa, original source inventory and proof supplement: 51110230636731/31250000000000000" + }, + { + "command": "Verify BENCHMARK.json implementation and source-manifest SHA-256 fields against current files", + "result": "PASS" + }, + { + "command": "git diff --cached --check", + "result": "PASS; only research/quadratic-affine-packed files added; no upstream, selected, generator or existing test changes" + } + ], + "scoped_ai_review": { + "role": "independent arithmetic reasoning and component-oracle test author", + "result": "No blocker found; checked signed-lane bound, zero detection, compaction invariant and runner guards", + "human_review": false + }, + "not_run": [ + "repository-wide make verify", + "selected supplier cold replay", + "Lean", + "remote Linux CI" + ], + "preliminary_probe": { + "original_seconds": 73.86765500000911, + "packed_seconds": 11.014644916984253, + "certificate_equal": true, + "scope": "Initial hypothesis check with overlapping processes; excluded from benchmark and speedup claim" + } +} diff --git a/research/quadratic-affine-packed/benchmark.py b/research/quadratic-affine-packed/benchmark.py new file mode 100644 index 0000000000..35afff553e --- /dev/null +++ b/research/quadratic-affine-packed/benchmark.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""Alternating fresh-process A/B of the complete pinned historical audit. + +Copyright 2026 Curtis Dransfield, substantial OpenAI Codex assistance. +Apache-2.0. Timing is observational; certificate equality is mandatory. +""" +import argparse +from datetime import datetime, timezone +from hashlib import sha256 +import json +from pathlib import Path +import platform +import statistics +import subprocess +import sys +import tempfile +import time + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--pairs', type=int, default=3) + parser.add_argument('--out', type=Path, required=True, help='fresh JSON result file') + args = parser.parse_args() + if args.pairs < 1 or args.out.exists() or sys.flags.optimize: + parser.error('Use positive pairs, enabled assertions and a fresh output file') + expected = (ROOT / 'certificates/quadratic-affine-vectors.json').read_bytes() + result = dict(started_utc=datetime.now(timezone.utc).isoformat(), + python=sys.version, executable=sys.executable, platform=platform.platform(), + machine=platform.machine(), pairs=args.pairs, + timing_scope='Fresh serial child wall time including interpreter, imports, source checks, complete audit, certificate comparison and output', + os_caches_flushed=False, samples=[], + source_manifest_sha256=sha256((HERE / 'SOURCES.json').read_bytes()).hexdigest(), + implementation_sha256={p.name: sha256(p.read_bytes()).hexdigest() + for p in (HERE / 'packed.py', HERE / 'replay.py', Path(__file__))}) + with tempfile.TemporaryDirectory(prefix='quadratic-affine-benchmark-') as tmp: + for pair in range(args.pairs): + order = ('original', 'packed') if pair % 2 == 0 else ('packed', 'original') + for engine in order: + out = Path(tmp) / f'{pair}-{engine}.json' + command = [sys.executable, '-I', '-B', str(HERE / 'replay.py'), + '--engine', engine, '--out', str(out)] + print(f'RUN pair={pair + 1} engine={engine}', flush=True) + start = time.perf_counter() + completed = subprocess.run(command, cwd=ROOT, capture_output=True, text=True) + elapsed = time.perf_counter() - start + if completed.returncode: + raise RuntimeError(f'{engine} failed: {completed.stdout}\n{completed.stderr}') + if out.read_bytes() != expected: + raise ValueError('Benchmark certificate mismatch') + result['samples'].append(dict(pair=pair + 1, engine=engine, wall_seconds=elapsed, + certificate_sha256=sha256(out.read_bytes()).hexdigest(), + stdout=completed.stdout, stderr=completed.stderr, returncode=completed.returncode)) + print(f'PASS pair={pair + 1} engine={engine} seconds={elapsed:.6f}', flush=True) + medians = {engine: statistics.median(s['wall_seconds'] for s in result['samples'] + if s['engine'] == engine) for engine in ('original', 'packed')} + result.update(median_seconds=medians, + observed_speedup=medians['original'] / medians['packed'], + finished_utc=datetime.now(timezone.utc).isoformat()) + with args.out.open('x') as out: + out.write(json.dumps(result, indent=2) + '\n') + print('PASS_QUADRATIC_AFFINE_BENCHMARK certificates_equal=true ' + + f"observed_speedup={result['observed_speedup']:.3f}") + + +if __name__ == '__main__': + main() diff --git a/research/quadratic-affine-packed/packed.py b/research/quadratic-affine-packed/packed.py new file mode 100644 index 0000000000..75bebca04b --- /dev/null +++ b/research/quadratic-affine-packed/packed.py @@ -0,0 +1,141 @@ +"""Exact column-parallel Gaussian orthogonality rows, using Python integers. + +Copyright 2026 Curtis Dransfield. Apache-2.0. +Prepared with substantial OpenAI Codex assistance. rank_witness preserves +Douglas Colkitt's original validation, elimination and receipt fields. +""" +from fractions import Fraction +from hashlib import sha256 + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +class PackedRows: + """Immutable snapshot of a nonempty sequence of D-coordinate phase vectors. + + Coordinates are 0, 1, i, -1, -i; masks encode (support, low, high). + One byte per column is sufficient for D <= 32. See README for the + signed-lane and zero-byte proofs. This class does not assert rank. + """ + + def __init__(self, vectors, dimension): + require(type(dimension) is int and 1 <= dimension <= 32, + 'Packed dimension must be in 1..32') + self.dimension = dimension + self.vectors = tuple(tuple(v) for v in vectors) + require(bool(self.vectors), 'Empty packed sample') + for v in self.vectors: + self._validate(v) + self.count = len(self.vectors) + unit = int.from_bytes(b'\x01' * self.count, 'little') + self.offset = dimension * unit + self.low = 127 * unit + self.high = 128 * unit + self.real = [] + self.imag = [] + for k in range(dimension): + real_pos, real_neg, imag_pos, imag_neg = (bytearray(self.count) for _ in range(4)) + for j, (support, lo, hi) in enumerate(self.vectors): + if support >> k & 1: + phase = ((lo >> k) & 1) + 2 * ((hi >> k) & 1) + (real_pos, imag_pos, real_neg, imag_neg)[phase][j] = 1 + self.real.append(int.from_bytes(real_pos, 'little') - int.from_bytes(real_neg, 'little')) + self.imag.append(int.from_bytes(imag_pos, 'little') - int.from_bytes(imag_neg, 'little')) + # At step k, collect two k-bit blocks spaced 8*k bits apart. + self.compaction = [] + k = 1 + while k < self.count: + groups = (self.count + 2*k - 1) // (2*k) + stride = 16*k + repetitions = ((1 << (stride*groups)) - 1) // ((1 << stride) - 1) + self.compaction.append((7*k, ((1 << (2*k)) - 1) * repetitions)) + k *= 2 + self.row_mask = (1 << self.count) - 1 + + def _validate(self, v): + require(len(v) == 3 and all(type(x) is int and x >= 0 for x in v), + 'Invalid phase masks') + support, lo, hi = v + require(support < 1 << self.dimension and not ((lo | hi) & ~support), + 'Phase masks leave support or dimension') + + def products(self, v): + """Return offset packed real/imaginary parts of conj(v) dot each column.""" + self._validate(v) + support, lo, hi = v + real = imag = self.offset + while support: + bit = support & -support + support ^= bit + k = bit.bit_length() - 1 + phase = ((lo >> k) & 1) + 2 * ((hi >> k) & 1) + r, i = self.real[k], self.imag[k] + if phase == 0: + real += r + imag += i + elif phase == 1: + real += i + imag -= r + elif phase == 2: + real -= r + imag -= i + else: + real -= i + imag += r + return real, imag + + def zero_lanes(self, real, imag): + """Decode products() output; each offset digit must be in [0, 2D].""" + different = (real ^ self.offset) | (imag ^ self.offset) + nonzero = (different + self.low) & self.high + bits = (self.high ^ nonzero) >> 7 + for shift, mask in self.compaction: + bits = (bits | (bits >> shift)) & mask + return bits & self.row_mask + + def orthogonality_row(self, v): + return self.zero_lanes(*self.products(v)) + + def central_row(self, index): + require(type(index) is int and 0 <= index < self.count, 'Invalid packed row index') + return self.orthogonality_row(self.vectors[index]) | (1 << index) + + +def rank_witness(original, t, complex_phases, parity, sample, independent_rows, + *, compare_rows=False): + """Original finite rank obligation with only row construction replaced.""" + f = original.family(t, complex_phases, parity) + require(sample and len(set(sample)) == len(sample) and + all(type(i) is int and 0 <= i < f['n'] for i in sample), + 'Sample must contain distinct valid labels') + require(independent_rows and len(set(independent_rows)) == len(independent_rows) and + all(type(i) is int and 0 <= i < len(sample) for i in independent_rows), + 'Invalid independent row indices') + rows = PackedRows([original.vector(f, i) for i in sample], f['D']) + basis = {} + digest = sha256() + bytewidth = (len(sample) + 7) // 8 + for i in independent_rows: + row = rows.central_row(i) + if compare_rows: + expected = sum(1 << j for j, v in enumerate(rows.vectors) + if i == j or original.inner(rows.vectors[i], v) == (0, 0)) + require(row == expected, 'Packed row differs from original exact row') + digest.update(row.to_bytes(bytewidth, 'little')) + x = row + while x: + p = x.bit_length() - 1 + if p not in basis: + basis[p] = x + break + x ^= basis[p] + require(x != 0, 'Claimed rows are linearly dependent') + r, ell, d, n = len(basis), f['ell'], f['d'], f['n'] + return dict(t=t, complex_phases=complex_phases, parity=parity, n=n, d=d, ell=ell, + normalized_dimension=Fraction(d, ell), counts_by_support_dimension=f['counts'], + minor_columns=len(sample), binary_rank_lower=r, independent_row_sha256=digest.hexdigest(), + positive_deficit_excluded=n*ell <= 6*r*d, + scope='Only I plus the full orthogonality relation on this exact vector family; no rank upper bound asserted') diff --git a/research/quadratic-affine-packed/replay.py b/research/quadratic-affine-packed/replay.py new file mode 100644 index 0000000000..db9aac56cb --- /dev/null +++ b/research/quadratic-affine-packed/replay.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +"""Replay all twelve pinned quadratic-affine witnesses without overwriting them. + +Copyright 2026 Curtis Dransfield, substantial OpenAI Codex assistance. +Apache-2.0. Uses Douglas Colkitt's original finite audit and source checks. +""" +import argparse +from functools import partial +from hashlib import sha256 +import json +from pathlib import Path +import sys + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] + + +def check_sources(root=ROOT): + manifest = json.loads((HERE / 'SOURCES.json').read_text()) + for relative, digest in manifest['files'].items(): + path = root / relative + if sha256(path.read_bytes()).hexdigest() != digest: + raise ValueError('Pinned input changed: ' + relative) + return manifest + + +def run(engine, compare_rows=False): + if sys.flags.optimize: + raise ValueError('Assertions must remain enabled for the inherited audit') + check_sources() + sys.path.insert(0, str(ROOT / 'scripts')) + import audit_quadratic_affine as audit + import experiments.quadratic_affine as original + from prepare_layers import serializable + # The adapter is process-local; the original audit entry point stays intact. + original_rank = audit.rank_witness + try: + if engine == 'packed': + sys.path.insert(0, str(HERE)) + from packed import rank_witness + audit.rank_witness = partial(rank_witness, original, compare_rows=compare_rows) + elif engine != 'original' or compare_rows: + raise ValueError('Row comparison requires the packed engine') + certificate = json.dumps(serializable(audit.certificate()), indent=2, sort_keys=True) + '\n' + finally: + audit.rank_witness = original_rank + expected = (ROOT / 'certificates/quadratic-affine-vectors.json').read_text() + if certificate != expected: + raise ValueError('Complete certificate differs from the pinned finite witness') + return certificate + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--engine', choices=('original', 'packed'), default='packed') + parser.add_argument('--compare-rows', action='store_true', + help='compare every full packed row to the original pairwise calculation') + parser.add_argument('--out', type=Path, help='optional fresh certificate output file') + args = parser.parse_args() + if args.out is not None and args.out.exists(): + parser.error('Output must not already exist') + certificate = run(args.engine, args.compare_rows) + if args.out is not None: + with args.out.open('x') as out: + out.write(certificate) + print('PASS_QUADRATIC_AFFINE_PACKED cases=12 certificate_sha256=' + + sha256(certificate.encode()).hexdigest() + ' engine=' + args.engine + + ' compare_rows=' + str(args.compare_rows).lower()) + + +if __name__ == '__main__': + main() diff --git a/research/quadratic-affine-packed/test_packed.py b/research/quadratic-affine-packed/test_packed.py new file mode 100644 index 0000000000..01fbf507b4 --- /dev/null +++ b/research/quadratic-affine-packed/test_packed.py @@ -0,0 +1,233 @@ +"""Independent integer-component controls for the optional packed replay. + +Copyright 2026 Curtis Dransfield. Apache-2.0. +Prepared with substantial OpenAI Codex assistance. The original family and +rank witness are Douglas Colkitt's research, with its retained Codex disclosure. +""" +from itertools import product +from pathlib import Path +import random +import sys +import unittest +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT / 'scripts')) +sys.path.insert(0, str(Path(__file__).resolve().parent)) + +from experiments import quadratic_affine as original +from packed import PackedRows, rank_witness + + +PHASES = ((0, 0), (1, 0), (0, 1), (-1, 0), (0, -1)) + + +def encode(coordinates): + support = lo = hi = 0 + for k, coordinate in enumerate(coordinates): + if coordinate == (0, 0): + continue + phase = PHASES.index(coordinate) - 1 + support |= 1 << k + lo |= (phase % 2) << k + hi |= (phase // 2) << k + return support, lo, hi + + +def components(v, dimension): + support, lo, hi = v + return [PHASES[1 + ((lo >> k) & 1) + 2*((hi >> k) & 1)] + if (support >> k) & 1 else PHASES[0] for k in range(dimension)] + + +def scalar_product(v, w, dimension): + """Literal Hermitian product, with no packed arithmetic or original.inner.""" + real = imag = 0 + for (a, b), (c, d) in zip(components(v, dimension), components(w, dimension)): + real += a*c + b*d + imag += a*d - b*c + return real, imag + + +def scalar_row(vectors, dimension, index, diagonal=True): + return sum(1 << j for j, w in enumerate(vectors) + if (diagonal and j == index) or + scalar_product(vectors[index], w, dimension) == (0, 0)) + + +class PackedArithmetic(unittest.TestCase): + def check_vectors(self, vectors, dimension): + rows = PackedRows(vectors, dimension) + for index, v in enumerate(vectors): + real, imag = rows.products(v) + real_digits = real.to_bytes(len(vectors), 'little') + imag_digits = imag.to_bytes(len(vectors), 'little') + expected_row = 0 + for j, w in enumerate(vectors): + expected = scalar_product(v, w, dimension) + self.assertEqual((real_digits[j] - dimension, imag_digits[j] - dimension), expected) + if expected == (0, 0): + expected_row |= 1 << j + self.assertEqual(rows.orthogonality_row(v), expected_row) + self.assertEqual(rows.central_row(index), expected_row | (1 << index)) + + def test_every_three_coordinate_phase_vector(self): + # All 5^3 vectors, including zero and all support patterns: 15,625 + # independent exact products, a larger class than affine labels. + vectors = [encode(p) for p in product(PHASES, repeat=3)] + self.check_vectors(vectors, 3) + + def test_every_small_gaussian_label(self): + f = original.family(2, True) + self.check_vectors([original.vector(f, i) for i in range(f['n'])], f['D']) + + def test_signed_extremes_at_maximum_dimension(self): + vectors = [encode([phase] * 32) for phase in PHASES] + vectors += [encode([PHASES[1 + (k + shift) % 4] for k in range(32)]) + for shift in range(4)] + self.check_vectors(vectors, 32) + + def test_seed_92_all_dimensions(self): + rng = random.Random(92) + counts = (1, 7, 8, 9, 31, 32, 33, 63, 64, 65) + for dimension in range(1, 33): + count = counts[(dimension + 92) % len(counts)] + vectors = [encode([rng.choice(PHASES) for _ in range(dimension)]) + for _ in range(count)] + with self.subTest(dimension=dimension, count=count): + self.check_vectors(vectors, dimension) + + def test_compaction_boundary_counts_and_positions(self): + # Unit query versus either same/disjoint unit support realizes any + # chosen orthogonality pattern independently of byte compaction. + query = encode(((1, 0), (0, 0))) + orthogonal = encode(((0, 0), (1, 0))) + counts = (1, 2, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, + 63, 64, 65, 127, 128, 129, 255, 256, 257) + for count in counts: + for selected in ({0}, {count - 1}, set(range(count)), + set(range(0, count, 2)), set(range(1, count, 2)), set()): + columns = [orthogonal if j in selected else query for j in range(count)] + expected = sum(1 << j for j in selected) + with self.subTest(count=count, selected=len(selected)): + self.assertEqual(PackedRows(columns, 2).orthogonality_row(query), expected) + + def test_all_offset_digit_pairs(self): + # Independent direct inputs to zero_lanes cover every permitted pair + # of output digits at the largest D; consecutive lanes cross zero. + dimension = 32 + digit_pairs = list(product(range(2*dimension + 1), repeat=2)) + rows = PackedRows([(0, 0, 0)] * len(digit_pairs), dimension) + real = int.from_bytes(bytes(a for a, b in digit_pairs), 'little') + imag = int.from_bytes(bytes(b for a, b in digit_pairs), 'little') + expected = sum(1 << j for j, pair in enumerate(digit_pairs) + if pair == (dimension, dimension)) + self.assertEqual(rows.zero_lanes(real, imag), expected) + + def test_negative_controls_conjugation_imaginary_diagonal(self): + v = encode(((1, 0), (0, 1))) + w = encode(((1, 0), (0, -1))) + rows = PackedRows([v, w], 2) + self.assertEqual(scalar_product(v, w, 2), (0, 0)) + self.assertEqual(rows.orthogonality_row(v), 2) + # Conjugating the query before the Hermitian kernel intentionally + # computes a bilinear product and must fail the same oracle. + conjugate_v = (v[0], v[1], v[2] ^ v[1]) + self.assertNotEqual(rows.orthogonality_row(conjugate_v), 2) + real_unit, imaginary_unit = encode(((1, 0),)), encode(((0, 1),)) + imaginary_rows = PackedRows([imaginary_unit], 1) + self.assertEqual(imaginary_rows.orthogonality_row(real_unit), 0) + real, imag = imaginary_rows.products(real_unit) + self.assertEqual(imaginary_rows.zero_lanes(real, imaginary_rows.offset), 1) + # Omitting I changes the actual matrix, since norms are nonzero. + self.assertEqual(rows.central_row(0), 3) + self.assertNotEqual(rows.central_row(0), rows.orthogonality_row(v)) + + def test_negative_control_cross_lane_borrow(self): + rows = PackedRows([(0, 0, 0)] * 2, 1) + # Offset-real bytes (1,0), imaginary (1,1) give comparison bytes + # (0,1). Subtract-one zero-byte detection falsely marks BOTH lanes. + real, imag = 1, 257 + self.assertEqual(rows.zero_lanes(real, imag), 1) + different = (real ^ rows.offset) | (imag ^ rows.offset) + bad_flags = (different - 257) & ~different & (128*257) + bad_row = sum(((bad_flags >> (8*j + 7)) & 1) << j for j in range(2)) + self.assertEqual(bad_row, 3) + self.assertNotEqual(bad_row, rows.zero_lanes(real, imag)) + + def test_validation_and_input_snapshot(self): + for dimension in (0, 33, True, 1.0): + with self.assertRaises(ValueError): + PackedRows([(1, 0, 0)], dimension) + for vectors in ([], [(1, 0)], [(-1, 0, 0)], [(True, 0, 0)], + [(2, 0, 0)], [(1, 2, 0)], [(0, 0, 1)]): + with self.assertRaises(ValueError): + PackedRows(vectors, 1) + vectors = [[1, 0, 0], [2, 0, 0]] + rows = PackedRows(vectors, 2) + vectors[0][0] = 2 + self.assertEqual(rows.central_row(0), 3) + for index in (-1, 2, True, 0.0): + with self.assertRaises(ValueError): + rows.central_row(index) + + +class RankReplay(unittest.TestCase): + def test_receipts_and_nonmonotone_orders_match_original(self): + for phases in (False, True): + f = original.family(2, phases) + sample = list(range(f['n'])) + random.Random(92).shuffle(sample) + vectors = [original.vector(f, i) for i in sample] + basis, independent = {}, [] + order = list(range(len(sample))) + random.Random(93).shuffle(order) + for i in order: + x = scalar_row(vectors, f['D'], i) + while x: + pivot = x.bit_length() - 1 + if pivot not in basis: + basis[pivot] = x + independent.append(i) + break + x ^= basis[pivot] + args = dict(t=2, complex_phases=phases, parity=None, + sample=sample, independent_rows=independent) + expected = original.rank_witness(**args) + self.assertEqual(rank_witness(original, **args), expected) + args['independent_rows'] = independent[::-1] + reordered = original.rank_witness(**args) + self.assertNotEqual(reordered['independent_row_sha256'], expected['independent_row_sha256']) + self.assertEqual(rank_witness(original, **args), reordered) + + def test_same_rejections_as_original(self): + valid = dict(t=1, complex_phases=False, parity=None, + sample=[0, 1, 2, 3], independent_rows=[0, 2]) + mutations = [dict(sample=x) for x in ([], [0, 0], [-1], [4], [True], [1.0])] + mutations += [dict(independent_rows=x) for x in + ([], [0, 0], [-1], [4], [True], [0.0], [0, 1])] + mutations += [dict(t=0), dict(t=6), dict(complex_phases=True, parity=0)] + for mutation in mutations: + args = dict(valid, **mutation) + with self.subTest(mutation=mutation): + with self.assertRaises(ValueError) as old_error: + original.rank_witness(**args) + with self.assertRaises(ValueError) as new_error: + rank_witness(original, **args) + self.assertEqual(str(old_error.exception), str(new_error.exception)) + + def test_receipt_detects_diagonal_mutation(self): + args = dict(t=1, complex_phases=False, parity=None, + sample=[0, 1, 2, 3], independent_rows=[0, 2]) + expected = original.rank_witness(**args) + with patch.object(PackedRows, 'central_row', + lambda self, i: self.orthogonality_row(self.vectors[i])): + mutated = rank_witness(original, **args) + with self.assertRaisesRegex(ValueError, 'Packed row differs from original exact row'): + rank_witness(original, **args, compare_rows=True) + self.assertEqual(mutated['binary_rank_lower'], expected['binary_rank_lower']) + self.assertNotEqual(mutated['independent_row_sha256'], expected['independent_row_sha256']) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/research/quadratic-affine-packed/test_replay.py b/research/quadratic-affine-packed/test_replay.py new file mode 100644 index 0000000000..dce8c9d116 --- /dev/null +++ b/research/quadratic-affine-packed/test_replay.py @@ -0,0 +1,53 @@ +"""Fail-closed source and CLI controls for the optional audit adapter.""" +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +from replay import ROOT, check_sources + + +class ReplayGuards(unittest.TestCase): + def test_all_pins_and_source_mutations(self): + manifest = check_sources() + with tempfile.TemporaryDirectory() as temp: + root = Path(temp) + for relative in manifest['files']: + dest = root / relative + dest.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(ROOT / relative, dest) + self.assertEqual(check_sources(root), manifest) + for relative in ('scripts/experiments/quadratic_affine.py', + 'scripts/experiments/quadratic_affine_witnesses.json', + 'certificates/quadratic-affine-vectors.json', + 'upstream/manifest.json'): + path = root / relative + original = path.read_bytes() + path.write_bytes(original + b' ') + with self.assertRaisesRegex(ValueError, 'Pinned input changed'): + check_sources(root) + path.write_bytes(original) + + def test_optimized_python_is_refused(self): + p = subprocess.run([sys.executable, '-I', '-O', '-B', str(HERE / 'replay.py')], + capture_output=True, text=True) + self.assertNotEqual(p.returncode, 0) + self.assertIn('Assertions must remain enabled', p.stderr) + + def test_existing_output_is_preserved(self): + with tempfile.TemporaryDirectory() as temp: + out = Path(temp) / 'existing.json' + out.write_bytes(b'preserve this output') + for script in ('replay.py', 'benchmark.py'): + p = subprocess.run([sys.executable, '-I', '-B', str(HERE / script), '--out', str(out)], + capture_output=True, text=True) + self.assertNotEqual(p.returncode, 0) + self.assertEqual(out.read_bytes(), b'preserve this output') + + +if __name__ == '__main__': + unittest.main() diff --git a/research/quadratic-affine-packed/verify.py b/research/quadratic-affine-packed/verify.py new file mode 100644 index 0000000000..835fa6b7a9 --- /dev/null +++ b/research/quadratic-affine-packed/verify.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +"""One-command exact differential verification and failure controls (no timing).""" +from pathlib import Path +import subprocess +import sys + +HERE = Path(__file__).resolve().parent + + +def main(): + if sys.flags.optimize: + raise SystemExit('Assertions must remain enabled for the inherited audit') + for script in ('test_packed.py', 'test_replay.py'): + subprocess.run([sys.executable, '-I', '-B', str(HERE / script)], check=True) + # Retain all original rational-projector, count, norm and rejection tests. + subprocess.run([sys.executable, '-I', '-B', str(HERE.parents[1] / 'tests/test_quadratic_affine.py')], + check=True) + subprocess.run([sys.executable, '-I', '-B', str(HERE / 'replay.py'), '--compare-rows'], check=True) + print('PASS_QUADRATIC_AFFINE_DIFFERENTIAL all_frozen_rows_equal=true cases=12') + + +if __name__ == '__main__': + main()