From dd5b27a8d0ad7261899d972d41b1c56f25c496a5 Mon Sep 17 00:00:00 2001 From: woklerx <338878655+woklerx@users.noreply.github.com> Date: Tue, 13 Oct 2026 08:19:46 -0700 Subject: [PATCH] Research consolidation: bind historical bit-compression replay --- research/bit-compression-provenance/CLAIM.md | 48 +++ research/bit-compression-provenance/README.md | 144 +++++++++ .../bit-compression-provenance/REPLAY.json | 62 ++++ .../bit-compression-provenance/SOURCES.json | 79 +++++ .../VALIDATION.json | 149 +++++++++ .../independent_arithmetic.py | 290 ++++++++++++++++++ .../bit-compression-provenance/test_verify.py | 287 +++++++++++++++++ research/bit-compression-provenance/verify.py | 226 ++++++++++++++ 8 files changed, 1285 insertions(+) create mode 100644 research/bit-compression-provenance/CLAIM.md create mode 100644 research/bit-compression-provenance/README.md create mode 100644 research/bit-compression-provenance/REPLAY.json create mode 100644 research/bit-compression-provenance/SOURCES.json create mode 100644 research/bit-compression-provenance/VALIDATION.json create mode 100644 research/bit-compression-provenance/independent_arithmetic.py create mode 100644 research/bit-compression-provenance/test_verify.py create mode 100644 research/bit-compression-provenance/verify.py diff --git a/research/bit-compression-provenance/CLAIM.md b/research/bit-compression-provenance/CLAIM.md new file mode 100644 index 0000000000..7ba3222c0c --- /dev/null +++ b/research/bit-compression-provenance/CLAIM.md @@ -0,0 +1,48 @@ +# Sprint claim + +- Track: research consolidation; OS-random assignment seed **99**. +- Exact base: `007664306fff84fbb1c9a1b4053b69f439764034`. +- Object: the historical `certificates/bit-compression-audit.json` scoped + paired-template obstruction and its incomplete two-file source inventory. +- Method: reconstruct and pin the repository-local import/input closure; + replay the unchanged producer and compare all mathematical fields; test + altered-driver/helper/certificate controls; independently check the exact + role-budget arithmetic. No expansion of the old search domain. +- Deliverable: one additive research consolidation package, reproducible + verifier, source and result receipts, and a scoped provenance explanation. +- Expected finish: 2026-10-13 08:40 America/Los_Angeles (15:40 UTC), reserving + at least the final quarter of the 45-minute budget for review and handoff. +- Candidates considered: (0) bit-compression provenance closure, (1) stage-pair + fusion evidence closure, (2) balanced-Fano completion lineage. Seed 99 mod 3 + selects candidate 0. The other historical packages have related pending + replay tooling (#824 and #756); no same-scope bit-compression closure was + found in the focused public search. +- Coordination: no pinned sprint issue was visible in the public issue list + (open issues #913 and #650). Claim recorded locally as requested. +- Overlap checked with Curiosity Bot's GitHub provider: latest 100 open PRs; + provenance and negative-result queries (each capped at 100); focused Fano, + stage-pair and bit-compression queries. These are bounded searches, not a + claim to have reviewed every open PR. Latest inspected arrival was #961. +- Attribution: contributed by woklerx with substantial OpenAI Codex assistance; + predecessor mathematical work remains credited to its original authors. + +## Completion update + +The full isolated replay reproduced every historical certificate field. The +new inventory binds 32 inputs, including all nine local Python sources. The +independent checker confirms all 73 local, 18 global, and 36 finite paired rows; +no arithmetic discrepancy or new kappa was found. The concrete deficit-formula +mutation changes an exact ratio while preserving both old source hashes, and +is rejected by the new input binding. All 23 regression tests pass normally +and under `-O`; all seven predecessor tests pass. Final receipts are retained +in `REPLAY.json` and `VALIDATION.json`. + +Final public overlap refresh on 2026-10-13 at 08:14 America/Los_Angeles +included the latest 100 updated open PRs through #966 and the focused +bit-compression query (only unrelated #289). No same-scope result was found. +PR867's selected-799 provenance map, PR756's Fano replay, PR824's stage-pair +replay, and PR946's broader cross-platform work concern distinct inputs or +obligations. There is still no pinned sprint issue; open issues are #913/#650. + +Contribution handoff: [source branch and complete package](https://github.com/woklerx/integer-mult-bounds/tree/research/bit-compression-provenance-seed99/research/bit-compression-provenance). +The single PR URL is also recorded in the local submission handoff. diff --git a/research/bit-compression-provenance/README.md b/research/bit-compression-provenance/README.md new file mode 100644 index 0000000000..c9626943ed --- /dev/null +++ b/research/bit-compression-provenance/README.md @@ -0,0 +1,144 @@ +# Historical bit-compression provenance closure (no new kappa) + +The historical [bit-compression certificate](../../certificates/bit-compression-audit.json) +has two `source_sha256` entries. Both still match at base +`007664306fff84fbb1c9a1b4053b69f439764034`, but they do not pin the full computation: +the driver and eight helper modules execute, and only one of those nine Python +files appears in that inventory. In particular, the omitted +`shared_point_network.counts` computes the deficit used in the saving bounds. +The upstream manifest that determines `upstream_commit` is also omitted. + +This package resolves that **provenance undercoverage**, not a mathematical +counterexample. It supplies a checked local source/input closure, a fresh replay +that compares every certificate field, an independent arithmetic checker, and +failure controls. The full replay reproduced the historical result. No existing +construction, certificate, selected frontier, or `upstream/` file is changed. + +A concrete control changes only the omitted helper's `D=N-2*L` to `D=N-2*L+1`. +At the recorded winning counts, the deficit ratio changes from `23/660995000` +to `1767136000001/50785567840000000000`, an exact difference of +`1/50785567840000000000`. Both historical source hashes still match; the new +input verifier rejects the changed helper before execution. This demonstrates +why those two hashes alone do not bind the arithmetic-producing code. + +## Reproduce + +From the repository root, with Python 3.11 or later and no third-party packages: + +```sh +python -I -S -B research/bit-compression-provenance/verify.py +``` + +Expected final line: + +```text +PASS: 32 pinned inputs; 9 local Python files; no new kappa. +``` + +This checks hashes before executing any predecessor code, checks the independent +arithmetic, and runs the unchanged historical producer in a fresh temporary +directory. The reference output is removed from that directory before execution, +so an omitted producer write cannot count as a successful replay. Only pinned +files are copied. An ambient `PYTHONPATH`, site package, or added package +initializer cannot enter this isolated source copy. A runtime module inventory +must agree with the static import closure. Every generated JSON field must match; +only JSON key order and whitespace are normalized. Integer `1` and Boolean `true` +are different. The original two source hashes are retained in the comparison. + +To write a receipt, add `--output /path/to/a/new/receipt.json`. An existing receipt +is refused. `--check-only` is a quick input/arithmetic check and explicitly reports +`INPUTS_AND_ARITHMETIC_ONLY`; it does **not** report a fresh producer replay. +The wrapper rejects `-O` because the predecessor's circuit checks use assertions. + +Run the failure controls and the original focused tests separately: + +```sh +python -I -S -B research/bit-compression-provenance/test_verify.py +python -B -m unittest discover -s tests -p test_bit_compression.py -v +``` + +The first command tests this package's source, arithmetic, output-comparison, +and isolated-replay failure modes. The second is the predecessor's seven-test +suite. The contribution does not change shared modules, so global artifact +regeneration and the selected-frontier cold replay are unnecessary here. + +## What the new inventory establishes + +[SOURCES.json](SOURCES.json) records exact SHA-256 values for 32 inputs: + +| Input class | Count | Purpose | +| --- | ---: | --- | +| Historical driver and repository-local imported Python modules | 9 | Actual producer implementation, including function-local imports | +| Historical proof note | 1 | Written model, counting argument, and infinite-tail hypotheses | +| Upstream manifest and its listed files | 20 | Bind the manifest itself and every file read by `verify_sources` | +| Historical output certificate | 1 | Exact expected result, independently retained for comparison | +| Historical focused test module | 1 | Reproducible predecessor test source | + +The upstream dependency remains OpenAI's `math` commit +`adc7f1241b42e322a6451854ab7e4b4c146bf78a`. The static graph has nine local Python +files; the runtime trace contains the eight imported helpers plus the separately +executed driver. Unknown non-standard-library imports are rejected, including a +helper omitted from both the files table and the graph. All inputs are verified +again against the copied bytes as applicable, and non-output files must remain +unchanged after execution. + +These hashes are integrity bindings relative to the reviewed Git source. They +are not signatures or proof that a run occurred. The Python interpreter and its +standard library are recorded as environment, not pinned binary dependencies. +The closure is for this producer, not a claim to discover arbitrary dynamic +imports or authenticate the repository's entire dependency graph. + +## Mathematical evidence and boundary + +The independent checker imports no predecessor implementation. It reconstructs +the rational logarithm enclosures, count formulas, strict integer role budgets, +candidate domains, and best/peak selection. It checks: + +- All 73 distinct local configurations at `h=50` and all 18 merged configurations. +- All 36 even ground sizes `40,42,...,110` for the existing paired template. +- At `h=50`, `U=4389`, `F=2304`, and the conditional role floor + `50 U + ceil(50 F/2) + 50 binom(49,2) = 335850`, versus the necessary target + budget `R <= 317035` for the historical `2^-30` target. +- The finite favorable-bound peak at `h=52`, the `h=112` tail endpoint, and the + recorded 48-role reduction from `509194` to `509146`. + +The independent arithmetic module treats circuit node classifications, merge +counts, and frame predicates as supplied inputs. The full producer replay +recomputes those finite circuit facts. Neither check proves the infinite family +by finite enumeration: the tail relies on the written argument that distinct +unshareable outputs imply `R >= 6v`, and on monotonicity of +`1 / ((14h^3 - 1) log(h^3))`. The historical obstruction retains the `c+q` compiler, +separate partial outputs, original central losses, and the old assembly inequality +`kappa < a_b/5`. It must not be transferred to the selected PR890 construction. + +This is a completed replay of the old domain, not an enlarged search. The search +stopping rule, finite coverage, and general counting argument are unchanged. +No general circuit lower bound, full multiplication theorem, or new exponent +saving is claimed. The selected result at the pinned base is still PR890's +conditional `1661957774647029/10^18`. + +## Attribution, coordination, and receipts + +The mathematical construction and original audit are Douglas R. Colkitt's +historical research, retained by commit +`d55bcd62ceb8f322bcf311a5a66e94c3ac7724f0`, with the repository's existing OpenAI +Codex assistance disclosure. This contribution is by **woklerx**, with substantial +OpenAI Codex assistance, including independently assigned dependency and arithmetic +reviews. It claims no priority for the obstruction or the historical circuit. +New files follow the repository's Apache-2.0 license; predecessor notices remain +in place. + +[CLAIM.md](CLAIM.md) records OS-random seed 99, the three candidates, the narrow +scope, and bounded overlap searches. The public issue list contained no pinned +sprint issue, so the claim was recorded locally. PR867's selected-799 provenance +map is related prior work, but concerns different source packages; PR756 and +PR824 address faster Fano and stage-pair replays. This contribution binds and +replays the older bit-compression obstruction without changing their work. + +[REPLAY.json](REPLAY.json) records the full comparison, environment, eight-helper +runtime inventory, and independent arithmetic result. +[VALIDATION.json](VALIDATION.json) pins the new implementation and records all +four successful commands: 23 controls normally, the same 23 under `-O`, the +predecessor's seven tests, and the final integrated full replay (about 51 seconds +on this Windows/Python 3.14 host). These are local execution receipts; hosted CI +and independent human mathematical review remain separate. diff --git a/research/bit-compression-provenance/REPLAY.json b/research/bit-compression-provenance/REPLAY.json new file mode 100644 index 0000000000..e91885a4de --- /dev/null +++ b/research/bit-compression-provenance/REPLAY.json @@ -0,0 +1,62 @@ +{ + "assignment_seed": 99, + "base_commit": "007664306fff84fbb1c9a1b4053b69f439764034", + "canonical_certificate_sha256": "5675a64088357f173aa241299ce00914b09ced14afef5d76b4d34d823f6ca1e5", + "historical_certificate_sha256": "53effc15424ca20be4e92e8d7b66599b513f208cce1863176fdd1347a46faf90", + "historical_source_pin_count": 2, + "independent_arithmetic": { + "baseline_roles": 509194, + "best_roles": 509146, + "global_rows_checked": 18, + "global_rows_with_matching_local_count_rows": 8, + "limitations": [ + "U/F, local additions, and merge counts require circuit replay or separate proof.", + "Recorded symbolic and frame flags are cross-checked, not proved by this module.", + "Infinite-tail circuit hypotheses are a written argument, not finite replay.", + "No all-size theorem, general optimality, or new multiplication witness is certified." + ], + "local_rows_checked": 73, + "log_method": "24-term rational atanh series with geometric remainder", + "paired_finite_range": [ + 40, + 110, + 2 + ], + "paired_finite_rows_checked": 36, + "paired_h50_frontier": 2304, + "paired_h50_role_floor": 335850, + "paired_h50_unshareable": 4389, + "paired_peak_h": 52, + "role_reduction": 48, + "scope": "Existing paired local template, arbitrary independent relabelings, equal-sum identification and pruning only; c+q compiler, separate partial outputs and original central losses retained.", + "status": "PASS: independent arithmetic; circuit counts are certificate inputs", + "tail_cutoff": 112, + "target_h50_integer_role_budget": 317035 + }, + "input_count": 32, + "local_python_files": 9, + "platform": "Windows-11-10.0.26300-SP0", + "producer_replay": { + "all_certificate_fields_equal": true, + "loaded_local_modules": { + "certify": "scripts/certify.py", + "exclusion_circuit": "scripts/exclusion_circuit.py", + "experiments.bit_compression": "scripts/experiments/bit_compression.py", + "paired_exclusion_circuit": "scripts/paired_exclusion_circuit.py", + "prepare_layers": "scripts/prepare_layers.py", + "search_network": "scripts/search_network.py", + "shared_point_circuit": "scripts/shared_point_circuit.py", + "shared_point_network": "scripts/shared_point_network.py" + }, + "seconds": 50.597238, + "status": "PASS", + "stderr": [], + "stdout": [ + "PASS bounded bit compression: 509194 -> 509146 roles; no new kappa.", + "Paired-template relabel/merge-only route excludes 2^-30 for every admissible even h." + ] + }, + "python": "3.14.5", + "source_manifest_sha256": "2700e579a70d01f315a8c3bb19b73686135d8b79f694055af3f535028e01734b", + "status": "PASS" +} diff --git a/research/bit-compression-provenance/SOURCES.json b/research/bit-compression-provenance/SOURCES.json new file mode 100644 index 0000000000..eea53471c8 --- /dev/null +++ b/research/bit-compression-provenance/SOURCES.json @@ -0,0 +1,79 @@ +{ + "assignment_seed": 99, + "base_commit": "007664306fff84fbb1c9a1b4053b69f439764034", + "certificate": "certificates/bit-compression-audit.json", + "entrypoint": "scripts/audit_bit_compression.py", + "files": { + "certificates/bit-compression-audit.json": "53effc15424ca20be4e92e8d7b66599b513f208cce1863176fdd1347a46faf90", + "docs/research/bit-compression-audit.md": "d97764832d3ea1fe813623431b42383e3ffd713d83863de275425853fd85469d", + "scripts/audit_bit_compression.py": "f0ed48592a885b54669d3f9086530fc84c467c39fd9281b357ba7550549aaa25", + "scripts/certify.py": "2348d95008276c5d5764f7e028c3918e484582cd589bd4d9b82919161745e53e", + "scripts/exclusion_circuit.py": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", + "scripts/experiments/bit_compression.py": "d1e6726341b3619262cc07246c3420584f46bdf57d44f1946d8dab18334cbca8", + "scripts/paired_exclusion_circuit.py": "8f05f04cf06217befd6d0ee0f2d7d0c5c5eb32cfab8c9a8ca7917fd6d620b7d2", + "scripts/prepare_layers.py": "a6e2adc612f264b1c0e51d18ce6315de9ae5e2ae2420aa22e99a22d8bf304947", + "scripts/search_network.py": "3a4faa5037d9b8e7ca5828afac193a96b6966a8b114f14937921795b24ec4f6a", + "scripts/shared_point_circuit.py": "c9bb7a5df92d260a4e3ad532b53e26936730d7c93f7facfb0784957c1e81c8ba", + "scripts/shared_point_network.py": "a85e7f0d8c42d6a27e3846308571ca324a1824ac5c2d59e4daa5c7673c33c697", + "tests/test_bit_compression.py": "865e6f32fbf36b9c1571e4654f87e5ce2116f568494383cf1b64954180ef4bc8", + "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" + }, + "local_import_graph": { + "scripts/audit_bit_compression.py": [ + "scripts/certify.py", + "scripts/experiments/bit_compression.py", + "scripts/paired_exclusion_circuit.py", + "scripts/prepare_layers.py", + "scripts/search_network.py", + "scripts/shared_point_circuit.py", + "scripts/shared_point_network.py" + ], + "scripts/certify.py": [ + "scripts/search_network.py" + ], + "scripts/exclusion_circuit.py": [], + "scripts/experiments/bit_compression.py": [ + "scripts/exclusion_circuit.py" + ], + "scripts/paired_exclusion_circuit.py": [ + "scripts/exclusion_circuit.py" + ], + "scripts/prepare_layers.py": [ + "scripts/certify.py", + "scripts/search_network.py" + ], + "scripts/search_network.py": [ + "scripts/certify.py" + ], + "scripts/shared_point_circuit.py": [ + "scripts/exclusion_circuit.py" + ], + "scripts/shared_point_network.py": [ + "scripts/certify.py", + "scripts/search_network.py", + "scripts/shared_point_circuit.py" + ] + }, + "schema": "historical-bit-compression-inputs-v1", + "scope": "Repository-local input closure for the historical producer and one focused test module. Interpreter and standard-library implementation are recorded as environment, not source-pinned. Full generator replay checks all certificate fields; old source_sha256 is preserved.", + "upstream_commit": "adc7f1241b42e322a6451854ab7e4b4c146bf78a" +} diff --git a/research/bit-compression-provenance/VALIDATION.json b/research/bit-compression-provenance/VALIDATION.json new file mode 100644 index 0000000000..77e1546e55 --- /dev/null +++ b/research/bit-compression-provenance/VALIDATION.json @@ -0,0 +1,149 @@ +{ + "base_commit": "007664306fff84fbb1c9a1b4053b69f439764034", + "commands": [ + { + "command": [ + "python", + "-I", + "-S", + "-B", + "research\\bit-compression-provenance\\test_verify.py" + ], + "exit_code": 0, + "seconds": 3.970758, + "stderr": [ + "test_all_historical_rows_pass_independent_arithmetic (__main__.IndependentArithmeticControls.test_all_historical_rows_pass_independent_arithmetic) ... ok", + "test_changed_best_global_count_is_rejected (__main__.IndependentArithmeticControls.test_changed_best_global_count_is_rejected) ... ok", + "test_changed_classification_and_role_counts_are_rejected (__main__.IndependentArithmeticControls.test_changed_classification_and_role_counts_are_rejected) ... ok", + "test_changed_exact_log_derived_fractions_are_rejected (__main__.IndependentArithmeticControls.test_changed_exact_log_derived_fractions_are_rejected) ... ok", + "test_changed_tail_endpoint_or_broadened_scope_is_rejected (__main__.IndependentArithmeticControls.test_changed_tail_endpoint_or_broadened_scope_is_rejected) ... ok", + "test_deleted_or_duplicated_coverage_is_rejected (__main__.IndependentArithmeticControls.test_deleted_or_duplicated_coverage_is_rejected) ... ok", + "test_false_and_integer_frame_flags_are_rejected (__main__.IndependentArithmeticControls.test_false_and_integer_frame_flags_are_rejected) ... ok", + "test_cli_rejects_optimized_python (__main__.IsolatedProducerControls.test_cli_rejects_optimized_python) ... ok", + "test_failed_producer_cannot_pass_from_staged_reference (__main__.IsolatedProducerControls.test_failed_producer_cannot_pass_from_staged_reference) ... ok", + "test_success_without_a_fresh_output_is_rejected (__main__.IsolatedProducerControls.test_success_without_a_fresh_output_is_rejected) ... ok", + "test_worker_ignores_environment_and_preserves_assertions (__main__.IsolatedProducerControls.test_worker_ignores_environment_and_preserves_assertions) ... ok", + "test_changed_deficit_changes_exact_result_but_not_old_pins (__main__.SourceBindingControls.test_changed_deficit_changes_exact_result_but_not_old_pins) ... ok", + "test_changed_fraction_coverage_and_types_are_rejected (__main__.SourceBindingControls.test_changed_fraction_coverage_and_types_are_rejected) ... ok", + "test_complete_reference_inputs_are_accepted (__main__.SourceBindingControls.test_complete_reference_inputs_are_accepted) ... ok", + "test_deleted_import_edge_is_rejected (__main__.SourceBindingControls.test_deleted_import_edge_is_rejected) ... ok", + "test_driver_helper_and_deep_helper_changes_rejected (__main__.SourceBindingControls.test_driver_helper_and_deep_helper_changes_rejected) ... ok", + "test_historical_source_cross_check_survives_outer_repin (__main__.SourceBindingControls.test_historical_source_cross_check_survives_outer_repin) ... ok", + "test_json_reordering_is_allowed_but_duplicate_keys_are_rejected (__main__.SourceBindingControls.test_json_reordering_is_allowed_but_duplicate_keys_are_rejected) ... ok", + "test_missing_transitive_pin_rejected_even_with_edited_graph (__main__.SourceBindingControls.test_missing_transitive_pin_rejected_even_with_edited_graph) ... ok", + "test_nonlocal_paths_are_rejected (__main__.SourceBindingControls.test_nonlocal_paths_are_rejected) ... ok", + "test_unlisted_namespace_initializer_is_not_staged (__main__.SourceBindingControls.test_unlisted_namespace_initializer_is_not_staged) ... ok", + "test_upstream_manifest_and_payload_bytes_are_bound (__main__.SourceBindingControls.test_upstream_manifest_and_payload_bytes_are_bound) ... ok", + "test_upstream_manifest_cross_check_survives_outer_repin (__main__.SourceBindingControls.test_upstream_manifest_cross_check_survives_outer_repin) ... ok", + "", + "----------------------------------------------------------------------", + "Ran 23 tests in 3.726s", + "", + "OK" + ], + "stdout": [] + }, + { + "command": [ + "python", + "-I", + "-S", + "-B", + "-O", + "research\\bit-compression-provenance\\test_verify.py" + ], + "exit_code": 0, + "seconds": 4.247818, + "stderr": [ + "test_all_historical_rows_pass_independent_arithmetic (__main__.IndependentArithmeticControls.test_all_historical_rows_pass_independent_arithmetic) ... ok", + "test_changed_best_global_count_is_rejected (__main__.IndependentArithmeticControls.test_changed_best_global_count_is_rejected) ... ok", + "test_changed_classification_and_role_counts_are_rejected (__main__.IndependentArithmeticControls.test_changed_classification_and_role_counts_are_rejected) ... ok", + "test_changed_exact_log_derived_fractions_are_rejected (__main__.IndependentArithmeticControls.test_changed_exact_log_derived_fractions_are_rejected) ... ok", + "test_changed_tail_endpoint_or_broadened_scope_is_rejected (__main__.IndependentArithmeticControls.test_changed_tail_endpoint_or_broadened_scope_is_rejected) ... ok", + "test_deleted_or_duplicated_coverage_is_rejected (__main__.IndependentArithmeticControls.test_deleted_or_duplicated_coverage_is_rejected) ... ok", + "test_false_and_integer_frame_flags_are_rejected (__main__.IndependentArithmeticControls.test_false_and_integer_frame_flags_are_rejected) ... ok", + "test_cli_rejects_optimized_python (__main__.IsolatedProducerControls.test_cli_rejects_optimized_python) ... ok", + "test_failed_producer_cannot_pass_from_staged_reference (__main__.IsolatedProducerControls.test_failed_producer_cannot_pass_from_staged_reference) ... ok", + "test_success_without_a_fresh_output_is_rejected (__main__.IsolatedProducerControls.test_success_without_a_fresh_output_is_rejected) ... ok", + "test_worker_ignores_environment_and_preserves_assertions (__main__.IsolatedProducerControls.test_worker_ignores_environment_and_preserves_assertions) ... ok", + "test_changed_deficit_changes_exact_result_but_not_old_pins (__main__.SourceBindingControls.test_changed_deficit_changes_exact_result_but_not_old_pins) ... ok", + "test_changed_fraction_coverage_and_types_are_rejected (__main__.SourceBindingControls.test_changed_fraction_coverage_and_types_are_rejected) ... ok", + "test_complete_reference_inputs_are_accepted (__main__.SourceBindingControls.test_complete_reference_inputs_are_accepted) ... ok", + "test_deleted_import_edge_is_rejected (__main__.SourceBindingControls.test_deleted_import_edge_is_rejected) ... ok", + "test_driver_helper_and_deep_helper_changes_rejected (__main__.SourceBindingControls.test_driver_helper_and_deep_helper_changes_rejected) ... ok", + "test_historical_source_cross_check_survives_outer_repin (__main__.SourceBindingControls.test_historical_source_cross_check_survives_outer_repin) ... ok", + "test_json_reordering_is_allowed_but_duplicate_keys_are_rejected (__main__.SourceBindingControls.test_json_reordering_is_allowed_but_duplicate_keys_are_rejected) ... ok", + "test_missing_transitive_pin_rejected_even_with_edited_graph (__main__.SourceBindingControls.test_missing_transitive_pin_rejected_even_with_edited_graph) ... ok", + "test_nonlocal_paths_are_rejected (__main__.SourceBindingControls.test_nonlocal_paths_are_rejected) ... ok", + "test_unlisted_namespace_initializer_is_not_staged (__main__.SourceBindingControls.test_unlisted_namespace_initializer_is_not_staged) ... ok", + "test_upstream_manifest_and_payload_bytes_are_bound (__main__.SourceBindingControls.test_upstream_manifest_and_payload_bytes_are_bound) ... ok", + "test_upstream_manifest_cross_check_survives_outer_repin (__main__.SourceBindingControls.test_upstream_manifest_cross_check_survives_outer_repin) ... ok", + "", + "----------------------------------------------------------------------", + "Ran 23 tests in 3.697s", + "", + "OK" + ], + "stdout": [] + }, + { + "command": [ + "python", + "-B", + "-m", + "unittest", + "discover", + "-s", + "tests", + "-p", + "test_bit_compression.py", + "-v" + ], + "exit_code": 0, + "seconds": 0.378056, + "stderr": [ + "test_changed_cutoff_retains_both_global_frame_directions (test_bit_compression.BitCompression.test_changed_cutoff_retains_both_global_frame_directions) ... ok", + "test_frontier_is_computed_from_actual_source_intersections (test_bit_compression.BitCompression.test_frontier_is_computed_from_actual_source_intersections) ... ok", + "test_lower_bound_survives_independent_relabeling_and_pruning (test_bit_compression.BitCompression.test_lower_bound_survives_independent_relabeling_and_pruning) ... ok", + "test_optimistic_role_budget_still_misses_target (test_bit_compression.BitCompression.test_optimistic_role_budget_still_misses_target) ... ok", + "test_reordered_and_reassociated_maps (test_bit_compression.BitCompression.test_reordered_and_reassociated_maps) ... ok", + "test_small_variant_maps_against_independent_pair_enumeration (test_bit_compression.BitCompression.test_small_variant_maps_against_independent_pair_enumeration) ... ok", + "test_tail_bound_has_room_and_improves_with_ground_size (test_bit_compression.BitCompression.test_tail_bound_has_room_and_improves_with_ground_size) ... ok", + "", + "----------------------------------------------------------------------", + "Ran 7 tests in 0.184s", + "", + "OK" + ], + "stdout": [] + }, + { + "command": [ + "python", + "-I", + "-S", + "-B", + "research\\bit-compression-provenance\\verify.py", + "--output", + "research\\bit-compression-provenance\\REPLAY.json" + ], + "exit_code": 0, + "seconds": 51.123143, + "stderr": [], + "stdout": [ + "Replaying unchanged historical producer in an isolated directory...", + "PASS: 32 pinned inputs; 9 local Python files; no new kappa." + ] + } + ], + "implementation_sha256": { + "SOURCES.json": "2700e579a70d01f315a8c3bb19b73686135d8b79f694055af3f535028e01734b", + "independent_arithmetic.py": "111948ed72e5146e5172dc2f314883d39358d4c9e007b9031db5e753450b9d27", + "test_verify.py": "d185ca6bc98eeb54e6e69b8c7dcb91eadb12a2a784f4013ee92e2a02b9cd80be", + "verify.py": "9f3e72116abfeff15433ffd269e49b6b739213f94f77960508c6957fae5d7ead" + }, + "platform": "Windows-11-10.0.26300-SP0", + "python": "3.14.5", + "scope": "Scoped local validation only; no hosted CI, full make verify, or selected-frontier cold replay was run.", + "status": "PASS" +} diff --git a/research/bit-compression-provenance/independent_arithmetic.py b/research/bit-compression-provenance/independent_arithmetic.py new file mode 100644 index 0000000000..ff393a06d4 --- /dev/null +++ b/research/bit-compression-provenance/independent_arithmetic.py @@ -0,0 +1,290 @@ +"""Independent exact arithmetic for the historical bit-compression screen. + +Only the Python standard library is imported. Circuit node classifications, +merge counts, and frame assertions are certificate inputs here, not independently +proved circuit facts. In particular, this checker cannot turn finite replay +into a proof of the paired-template obstruction for every ground size. + +The logarithm enclosure is derived from log(x) = 2*atanh((x-1)/(x+1)). +After writing n = 2**k * y with 1 <= y < 2, each series argument is in +[0, 1/3]. For N terms its nonnegative omitted tail is at most +2*z**(2*N+1)/((2*N+1)*(1-z*z)); this follows by replacing every omitted +denominator with 2*N+1 and summing a geometric series. N=24 reproduces the +historical rational precision without calling or importing its log routine. +""" + +from fractions import Fraction +from functools import lru_cache +from itertools import product +from math import comb + + +TARGET = Fraction(1, 2**30) +SCOPE = ( + "Existing paired local template, arbitrary independent relabelings, " + "equal-sum identification and pruning only; c+q compiler, separate " + "partial outputs and original central losses retained." +) + + +def _require(condition, message): + if not condition: + raise ValueError(message) + + +def _integer(record, key, context): + value = record[key] + _require(type(value) is int and value >= 0, + f"{context}.{key}: expected a nonnegative integer") + return value + + +def _rational(record, key, context): + value = record[key] + _require(type(value) is str, f"{context}.{key}: expected a rational string") + result = Fraction(value) + _require(str(result) == value, f"{context}.{key}: noncanonical rational") + return result + + +def _equal(actual, expected, context): + _require(actual == expected, f"{context}: does not match independent arithmetic") + + +@lru_cache(maxsize=None) +def _log_bounds(n): + """Return rational lower/upper enclosures without floating-point operations.""" + _require(type(n) is int and n > 1, "log argument must be an integer > 1") + exponent = n.bit_length() - 1 + reduced = Fraction(n, 2**exponent) + result = [Fraction(0), Fraction(0)] + for multiplier, argument in ( + (exponent, Fraction(1, 3)), + (1, (reduced - 1) / (reduced + 1)), + ): + lower = sum( + (2 * argument**power / power for power in range(1, 48, 2)), + Fraction(0), + ) + remainder = 2 * argument**49 / (49 * (1 - argument**2)) + result[0] += multiplier * lower + result[1] += multiplier * (lower + remainder) + _require(0 < result[0] < result[1], "invalid independent log enclosure") + return tuple(result) + + +def _network(h, roles): + vertices = comb(h, 3) + m = h**3 + width = 2 * vertices**2 * (vertices + roles + h) + deficit = vertices**2 * (vertices - 6 * h**2) + eta = Fraction(deficit, width * m) + _require(0 < eta < 1, f"h={h}: deficit ratio is outside (0, 1)") + log_lower, log_upper = _log_bounds(m) + return eta, log_lower, log_upper + + +def _strict_integer_budget(h, log_lower): + """Largest integer R satisfying the strict favorable saving requirement.""" + vertices = comb(h, 3) + qlog = 5 * TARGET * log_lower + # Cancel v**2 from D and W before solving the inequality for R. + threshold = ( + Fraction(vertices - 6 * h**2, 2 * h**3) * (1 + qlog) / qlog + - vertices - h + ) + return (threshold.numerator - 1) // threshold.denominator + + +def _bound_row(row, h, context): + _equal(_integer(row, "h", context), h, context + ".h") + additions = _integer(row, "local_additions", context) + unshareable = _integer(row, "unshareable", context) + stars = _integer(row, "stars", context) + frontier = _integer(row, "mandatory_frontier_stars", context) + _equal(additions, unshareable + stars, context + ".classification partition") + _require(frontier <= stars, context + ": frontier exceeds all star nodes") + outputs = comb(h - 1, 2) + _require(unshareable >= outputs, context + ": fewer unshareable nodes than outputs") + roles = h * unshareable + (h * frontier + 1) // 2 + h * outputs + _equal(_integer(row, "side_roles_lower", context), roles, context + ".side_roles_lower") + eta, log_lower, _ = _network(h, roles) + saving = eta / ((1 - eta) * log_lower) + for field, expected in ( + ("bit_saving_upper", saving), + ("kappa_upper", saving / 5), + ("fraction_of_target", saving / (5 * TARGET)), + ): + _equal(_rational(row, field, context), expected, context + "." + field) + _equal(_integer(row, "necessary_integer_role_budget", context), + _strict_integer_budget(h, log_lower), context + ".necessary_integer_role_budget") + _require(saving / 5 < TARGET, context + ": favorable bound reaches target") + return saving + + +def _config_key(config): + _require(type(config) is dict, "configuration must be an object") + _require(all(type(key) is str for key in config), "configuration keys must be strings") + _require(all(type(value) in (str, int) for value in config.values()), + "configuration values must be strings or integers") + return tuple(sorted(config.items())) + + +def _block(k=2, leaf=4, vec="prefix", order=0, assoc=0): + return _config_key(dict(family="block", k=k, leaf=leaf, vec=vec, + order=order, assoc=assoc)) + + +def _expected_local_configs(): + result = {_config_key(dict(family="filter", kind=kind)) + for kind in ("balanced", "vertex")} + result.update(_block(k=k, leaf=leaf, vec=vec) + for k, leaf, vec in product((2, 3, 4, 6, 8), (3, 4, 6, 8), + ("prefix", "tree", "paired"))) + result.update(_block(order=order, assoc=assoc) + for order, assoc in product(range(3), range(4))) + return result + + +def _indexed_rows(rows, expected_configs, context): + _require(type(rows) is list, context + ": expected a list") + indexed = {} + for row in rows: + key = _config_key(row["config"]) + _require(key not in indexed, context + ": duplicate configuration") + indexed[key] = row + _equal(set(indexed), expected_configs, context + ".configuration coverage") + return indexed + + +def _check(certificate): + _require(type(certificate) is dict, "certificate must be an object") + _equal(_rational(certificate, "target", "certificate"), TARGET, "target") + _equal(_rational(certificate, "current_bit_saving", "certificate"), + Fraction(296, 10**11), "current_bit_saving") + _equal(certificate["status"], + "BOUNDED NEGATIVE SCREEN; NO NEW MULTIPLICATION WITNESS", "status") + + local = _indexed_rows(certificate["local_candidates"], _expected_local_configs(), + "local_candidates") + _equal(len(local), 73, "local_candidates.count") + for number, row in enumerate(certificate["local_candidates"]): + context = f"local_candidates[{number}]" + bound = row["optimistic_merging_bound"] + _bound_row(bound, 50, context + ".optimistic_merging_bound") + _equal(_integer(row, "local_roles", context), + bound["local_additions"] + comb(49, 2), context + ".local_roles") + + global_expected = {_block(leaf=leaf, vec=vec, order=order) + for leaf, vec, order in product((3, 4), + ("prefix", "tree", "paired"), + range(3))} + global_rows = _indexed_rows(certificate["global_candidates"], global_expected, + "global_candidates") + _equal(len(global_rows), 18, "global_candidates.count") + globals_with_local_counts = 0 + for number, row in enumerate(certificate["global_candidates"]): + context = f"global_candidates[{number}]" + roles = _integer(row, "side_roles", context) + merged = _integer(row, "merged_additions", context) + _require(roles >= 6 * comb(50, 3), context + ": violates distinct-output role floor") + eta, log_lower, log_upper = _network(50, roles) + _equal(_rational(row, "bit_saving_lower", context), eta / log_upper, + context + ".bit_saving_lower") + upper = eta / ((1 - eta) * log_lower) + _equal(_rational(row, "bit_saving_upper", context), upper, + context + ".bit_saving_upper") + _require(upper / 5 < TARGET, context + ": favorable bound reaches target") + key = _config_key(row["config"]) + if key in local: + bound = local[key]["optimistic_merging_bound"] + # Interning precedes liveness pruning. Merges alone do not account + # for every reduction in the final active-addition count. + before_pruning = 50 * bound["local_additions"] - merged + 50 * comb(49, 2) + _require(roles <= before_pruning, context + ": roles exceed unpruned count") + _require(roles >= bound["side_roles_lower"], context + ": roles violate local bound") + globals_with_local_counts += 1 + + best = min(certificate["global_candidates"], key=lambda row: row["side_roles"]) + _equal(certificate["best_global"], best, "best_global") + _equal(best["side_roles"], 509146, "best_global.side_roles") + baseline = global_rows[_block()] + _equal(baseline["side_roles"], 509194, "paired baseline roles") + _equal(_rational(certificate, "role_improvement_ratio", "certificate"), + Fraction(baseline["side_roles"], best["side_roles"]), "role_improvement_ratio") + + symbolic = certificate["best_global_symbolic_check"] + expected_symbolic = dict(h=50, inputs=comb(50, 3), + partial_outputs=50 * comb(49, 2), + additions=best["side_roles"] - 50 * comb(49, 2), + roles=best["side_roles"], merged_additions=best["merged_additions"]) + for key, value in expected_symbolic.items(): + _equal(_integer(symbolic, key, "best_global_symbolic_check"), value, + "best_global_symbolic_check." + key) + for key in ("all_additions_disjoint", "all_partial_outputs_exact", "every_node_has_common_point"): + _require(symbolic[key] is True, "recorded symbolic flag is not true: " + key) + frames = certificate["best_global_frame_check"] + _equal(_integer(frames, "roles", "best_global_frame_check"), best["side_roles"], + "best_global_frame_check.roles") + for key in ("forward_frames_nested", "reverse_complement_frames_nested"): + _require(frames[key] is True, "recorded frame flag is not true: " + key) + + paired = certificate["paired_template_all_even_h"] + _equal(paired["scope"], SCOPE, "paired_template_all_even_h.scope") + finite = paired["finite"] + _require(type(finite) is list, "paired finite coverage must be a list") + _equal([row["h"] for row in finite], list(range(40, 112, 2)), "paired finite coverage") + savings = [_bound_row(row, h, f"paired.finite[{number}]") + for number, (h, row) in enumerate(zip(range(40, 112, 2), finite))] + peak = finite[max(range(len(finite)), key=lambda index: savings[index])] + _equal(paired["finite_peak"], peak, "paired.finite_peak") + _equal(peak["h"], 52, "paired peak ground size") + row50 = finite[5] + _equal({key: row50[key] for key in ( + "unshareable", "mandatory_frontier_stars", "side_roles_lower", "necessary_integer_role_budget")}, + dict(unshareable=4389, mandatory_frontier_stars=2304, + side_roles_lower=335850, necessary_integer_role_budget=317035), "paired h=50 checkpoint") + _equal(row50, local[_block()]["optimistic_merging_bound"], "paired h=50 local correspondence") + + _equal(_integer(paired, "nonpositive_deficit_below", "paired"), 40, + "paired.nonpositive_deficit_below") + for h in range(6, 40, 2): + _require(comb(h, 3) - 6 * h**2 <= 0, f"h={h}: positive small-ground deficit") + _equal(_integer(paired, "tail_cutoff", "paired"), 112, "paired.tail_cutoff") + log_lower, _ = _log_bounds(112**3) + tail = 1 / ((14 * 112**3 - 1) * log_lower) + _equal(_rational(paired, "tail_bit_saving_upper", "paired"), tail, + "paired.tail_bit_saving_upper") + _require(tail < 5 * TARGET, "paired tail endpoint does not exclude target") + # The infinite-tail step is conditional on the written circuit argument: + # distinct unshareable outputs give c >= 3v and output uses q = 3v, hence + # R >= 6v. Then W >= 14v**3 and eta < 1/(14*h**3). Both positive factors + # of (14*h**3-1)*log(h**3) increase for h >= 112, so its reciprocal + # decreases. The rational check above establishes its endpoint estimate; + # it does not establish distinctness/unshareability for all h by replay. + return dict( + status="PASS: independent arithmetic; circuit counts are certificate inputs", + local_rows_checked=len(local), global_rows_checked=len(global_rows), + global_rows_with_matching_local_count_rows=globals_with_local_counts, + paired_finite_rows_checked=len(finite), paired_finite_range=[40, 110, 2], + paired_peak_h=peak["h"], tail_cutoff=112, + paired_h50_unshareable=4389, paired_h50_frontier=2304, + paired_h50_role_floor=335850, target_h50_integer_role_budget=317035, + baseline_roles=509194, best_roles=509146, role_reduction=48, + log_method="24-term rational atanh series with geometric remainder", + scope=SCOPE, + limitations=[ + "U/F, local additions, and merge counts require circuit replay or separate proof.", + "Recorded symbolic and frame flags are cross-checked, not proved by this module.", + "Infinite-tail circuit hypotheses are a written argument, not finite replay.", + "No all-size theorem, general optimality, or new multiplication witness is certified.", + ], + ) + + +def check(certificate: dict) -> dict: + """Return a deterministic JSON-compatible summary, or raise ValueError.""" + try: + return _check(certificate) + except (KeyError, TypeError, IndexError, ZeroDivisionError, OverflowError) as error: + raise ValueError(f"malformed arithmetic certificate: {error}") from error diff --git a/research/bit-compression-provenance/test_verify.py b/research/bit-compression-provenance/test_verify.py new file mode 100644 index 0000000000..14e5575125 --- /dev/null +++ b/research/bit-compression-provenance/test_verify.py @@ -0,0 +1,287 @@ +#!/usr/bin/env python3 +"""Mutation controls for the historical source-bound replay. + +The full historical producer is deliberately not repeated here. Temporary +copies exercise input rejection; tiny producers test subprocess isolation. +""" +import copy +from fractions import Fraction +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest +from unittest.mock import patch + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import verify + + +class SourceBindingControls(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.manifest = verify.read_json(verify.MANIFEST.read_bytes()) + cls.blobs, cls.expected = verify.validate_inputs(verify.ROOT, cls.manifest) + + def setUp(self): + self.directory = tempfile.TemporaryDirectory(prefix="bit-compression-control-") + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + verify.stage_inputs(self.root, self.blobs) + + def old_pins_still_match(self): + for name, expected_hash in self.expected["source_sha256"].items(): + self.assertEqual(verify.digest((self.root / name).read_bytes()), expected_hash) + + def test_complete_reference_inputs_are_accepted(self): + blobs, certificate = verify.validate_inputs(self.root, self.manifest) + self.assertEqual(len(blobs), 32) + self.assertEqual(len(verify.import_graph(blobs)), 9) + self.assertEqual(len(certificate["source_sha256"]), 2) + + def test_driver_helper_and_deep_helper_changes_rejected(self): + for name in (verify.ENTRY, "scripts/shared_point_circuit.py", + "scripts/exclusion_circuit.py"): + with self.subTest(source=name): + target = self.root / name + target.write_bytes(self.blobs[name] + b"\n# mutation control\n") + self.old_pins_still_match() + with self.assertRaisesRegex(ValueError, "Source hash mismatch"): + verify.validate_inputs(self.root, self.manifest) + target.write_bytes(self.blobs[name]) + + def test_changed_deficit_changes_exact_result_but_not_old_pins(self): + code = """ +import json, pathlib, sys, types +sys.path.insert(0, str(pathlib.Path(sys.argv[1]) / 'scripts')) +from shared_point_network import counts +c = types.SimpleNamespace(h=50, additions=450346, outputs=range(58800)) +n = counts(50, c) +print(json.dumps({k: str(n[k]) for k in ('D', 'W', 'eta')})) +""" + + def evaluate(): + proc = subprocess.run([sys.executable, "-I", "-S", "-B", "-c", code, + str(self.root)], check=True, capture_output=True, + text=True, encoding="utf-8") + return json.loads(proc.stdout) + + baseline = evaluate() + name = "scripts/shared_point_network.py" + source = self.blobs[name] + self.assertEqual(source.count(b"D=N-2*L"), 1) + (self.root / name).write_bytes(source.replace(b"D=N-2*L", b"D=N-2*L+1")) + changed = evaluate() + self.assertEqual(int(changed["D"]), int(baseline["D"]) + 1) + self.assertEqual(changed["W"], baseline["W"]) + self.assertEqual(Fraction(changed["eta"]) - Fraction(baseline["eta"]), + Fraction(1, int(baseline["W"]) * 50**3)) + self.old_pins_still_match() + with self.assertRaisesRegex(ValueError, "Source hash mismatch"): + verify.validate_inputs(self.root, self.manifest) + + def test_upstream_manifest_and_payload_bytes_are_bound(self): + for name in ("upstream/manifest.json", "upstream/LICENSE"): + with self.subTest(source=name): + (self.root / name).write_bytes(self.blobs[name] + b"\n") + with self.assertRaisesRegex(ValueError, "Source hash mismatch"): + verify.validate_inputs(self.root, self.manifest) + (self.root / name).write_bytes(self.blobs[name]) + + def test_upstream_manifest_cross_check_survives_outer_repin(self): + manifest = copy.deepcopy(self.manifest) + name = "upstream/LICENSE" + changed = self.blobs[name] + b"\nmutation control\n" + (self.root / name).write_bytes(changed) + manifest["files"][name] = verify.digest(changed) + with self.assertRaisesRegex(ValueError, "Missing or changed upstream input"): + verify.validate_inputs(self.root, manifest) + + def test_historical_source_cross_check_survives_outer_repin(self): + manifest = copy.deepcopy(self.manifest) + name = "docs/research/bit-compression-audit.md" + changed = self.blobs[name] + b"\nmutation control\n" + (self.root / name).write_bytes(changed) + manifest["files"][name] = verify.digest(changed) + with self.assertRaisesRegex(ValueError, "Historical source pin mismatch"): + verify.validate_inputs(self.root, manifest) + + def test_missing_transitive_pin_rejected_even_with_edited_graph(self): + for edit_graph in (False, True): + with self.subTest(edit_graph=edit_graph): + manifest = copy.deepcopy(self.manifest) + missing = "scripts/exclusion_circuit.py" + del manifest["files"][missing] + if edit_graph: + del manifest["local_import_graph"][missing] + for dependencies in manifest["local_import_graph"].values(): + if missing in dependencies: + dependencies.remove(missing) + with self.assertRaises(ValueError): + verify.validate_inputs(self.root, manifest) + + def test_deleted_import_edge_is_rejected(self): + manifest = copy.deepcopy(self.manifest) + manifest["local_import_graph"]["scripts/paired_exclusion_circuit.py"].remove( + "scripts/exclusion_circuit.py") + with self.assertRaisesRegex(ValueError, "Static local import inventory changed"): + verify.validate_inputs(self.root, manifest) + + def test_unlisted_namespace_initializer_is_not_staged(self): + initializer = self.root / "scripts/experiments/__init__.py" + initializer.write_text("raise RuntimeError('unlisted initializer ran')\n", + encoding="utf-8") + blobs, _ = verify.validate_inputs(self.root, self.manifest) + with tempfile.TemporaryDirectory(prefix="bit-compression-clean-stage-") as directory: + staged = Path(directory) + verify.stage_inputs(staged, blobs) + self.assertFalse((staged / "scripts/experiments/__init__.py").exists()) + self.assertTrue((staged / "scripts/experiments/bit_compression.py").exists()) + + def test_changed_fraction_coverage_and_types_are_rejected(self): + mutations = [] + changed = copy.deepcopy(self.expected) + changed["target"] = "1/1073741825" + mutations.append(changed) + changed = copy.deepcopy(self.expected) + changed["local_candidates"].pop() + mutations.append(changed) + changed = copy.deepcopy(self.expected) + changed["best_global_frame_check"]["forward_frames_nested"] = 1 + mutations.append(changed) + for number, changed in enumerate(mutations): + with self.subTest(mutation=number): + with self.assertRaisesRegex(ValueError, "Historical certificate payload differs"): + verify.compare_certificate(changed, self.expected) + + def test_json_reordering_is_allowed_but_duplicate_keys_are_rejected(self): + equivalent = dict(reversed(list(self.expected.items()))) + verify.compare_certificate(equivalent, self.expected) + with self.assertRaisesRegex(ValueError, "Duplicate JSON key"): + verify.read_json(b'{"outer":{"x":1,"x":2}}') + + def test_nonlocal_paths_are_rejected(self): + for name in ("../outside", "/outside", "C:/outside", "upstream/../outside", + "upstream\\LICENSE", "./upstream/LICENSE"): + with self.subTest(path=name): + with self.assertRaisesRegex(ValueError, "Nonlocal input path"): + verify.checked_path(self.root, name) + + +class IsolatedProducerControls(unittest.TestCase): + @staticmethod + def inputs(source): + return {verify.ENTRY: source.encode("utf-8"), verify.CERTIFICATE: b'{"fresh":true}'} + + def test_worker_ignores_environment_and_preserves_assertions(self): + source = """ +import json, pathlib, sys +assert __debug__ +assert sys.flags.isolated == 1 and sys.flags.no_site == 1 +assert sys.flags.optimize == 0 +root = pathlib.Path(__file__).resolve().parents[1] +(root / 'certificates/bit-compression-audit.json').write_text( + json.dumps({'fresh': True}), encoding='utf-8') +""" + with tempfile.TemporaryDirectory(prefix="bit-compression-poison-") as directory: + poison = Path(directory) + marker = poison / "site-ran.txt" + (poison / "sitecustomize.py").write_text( + "from pathlib import Path\nPath(" + repr(str(marker)) + ").touch()\n", + encoding="utf-8") + with patch.dict(os.environ, {"PYTHONPATH": str(poison), "PYTHONOPTIMIZE": "2", + "PYTHONHOME": str(poison / "missing-home")}): + result = verify.replay(self.inputs(source), {"fresh": True}) + self.assertEqual(result["status"], "PASS") + self.assertFalse(marker.exists()) + + def test_success_without_a_fresh_output_is_rejected(self): + with self.assertRaises((ValueError, FileNotFoundError)): + verify.replay(self.inputs("pass\n"), {"fresh": True}) + + def test_failed_producer_cannot_pass_from_staged_reference(self): + with self.assertRaisesRegex(ValueError, "Historical producer failed"): + verify.replay(self.inputs("raise RuntimeError('control failure')\n"), {"fresh": True}) + + def test_cli_rejects_optimized_python(self): + proc = subprocess.run([sys.executable, "-I", "-S", "-B", "-O", + str(HERE / "verify.py"), "--check-only"], + capture_output=True, text=True, encoding="utf-8") + self.assertNotEqual(proc.returncode, 0) + self.assertIn("Historical assertions must remain enabled", proc.stderr) + + +class IndependentArithmeticControls(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.expected = verify.read_json((verify.ROOT / verify.CERTIFICATE).read_bytes()) + + def test_all_historical_rows_pass_independent_arithmetic(self): + result = verify.arithmetic_check(self.expected) + self.assertEqual(result["local_rows_checked"], 73) + self.assertEqual(result["global_rows_checked"], 18) + self.assertEqual(result["paired_finite_rows_checked"], 36) + + def test_deleted_or_duplicated_coverage_is_rejected(self): + for family, duplicate in (("local_candidates", False), ("local_candidates", True), + ("global_candidates", False), ("global_candidates", True), + ("paired_finite", False)): + with self.subTest(family=family, duplicate=duplicate): + changed = copy.deepcopy(self.expected) + rows = (changed["paired_template_all_even_h"]["finite"] + if family == "paired_finite" else changed[family]) + if duplicate: + rows.append(copy.deepcopy(rows[0])) + else: + rows.pop() + with self.assertRaises(ValueError): + verify.arithmetic_check(changed) + + def test_changed_classification_and_role_counts_are_rejected(self): + for field in ("unshareable", "mandatory_frontier_stars", "side_roles_lower", + "necessary_integer_role_budget"): + with self.subTest(field=field): + changed = copy.deepcopy(self.expected) + changed["local_candidates"][0]["optimistic_merging_bound"][field] += 1 + with self.assertRaises(ValueError): + verify.arithmetic_check(changed) + + def test_changed_exact_log_derived_fractions_are_rejected(self): + for field in ("bit_saving_upper", "kappa_upper", "fraction_of_target"): + with self.subTest(field=field): + changed = copy.deepcopy(self.expected) + row = changed["local_candidates"][0]["optimistic_merging_bound"] + row[field] = str(Fraction(row[field]) + Fraction(1, 10**30)) + with self.assertRaises(ValueError): + verify.arithmetic_check(changed) + + def test_changed_best_global_count_is_rejected(self): + changed = copy.deepcopy(self.expected) + changed["best_global"]["side_roles"] -= 1 + with self.assertRaises(ValueError): + verify.arithmetic_check(changed) + + def test_changed_tail_endpoint_or_broadened_scope_is_rejected(self): + for field, value in (("tail_cutoff", 114), + ("scope", "All pair-exclusion circuits at every ground size.")): + with self.subTest(field=field): + changed = copy.deepcopy(self.expected) + changed["paired_template_all_even_h"][field] = value + with self.assertRaises(ValueError): + verify.arithmetic_check(changed) + + def test_false_and_integer_frame_flags_are_rejected(self): + for field in ("forward_frames_nested", "reverse_complement_frames_nested"): + for value in (False, 1): + with self.subTest(field=field, value=value): + changed = copy.deepcopy(self.expected) + changed["best_global_frame_check"][field] = value + with self.assertRaises(ValueError): + verify.arithmetic_check(changed) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/research/bit-compression-provenance/verify.py b/research/bit-compression-provenance/verify.py new file mode 100644 index 0000000000..3c37dd89ea --- /dev/null +++ b/research/bit-compression-provenance/verify.py @@ -0,0 +1,226 @@ +#!/usr/bin/env python3 +"""Replay the historical bit-compression screen from its pinned local inputs. + +No predecessor file is regenerated in the checkout. This is a provenance and +finite-replay check, not a proof of the full multiplication theorem. +""" +import argparse +import ast +import hashlib +import json +from pathlib import Path, PurePosixPath +import platform +import subprocess +import sys +import tempfile +import time + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +ENTRY = "scripts/audit_bit_compression.py" +CERTIFICATE = "certificates/bit-compression-audit.json" +MANIFEST = HERE / "SOURCES.json" + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +def canonical(value): + return json.dumps(value, sort_keys=True, separators=(",", ":"), + ensure_ascii=True).encode("ascii") + + +def read_json(data): + def unique(pairs): + result = {} + for key, value in pairs: + require(key not in result, f"Duplicate JSON key: {key}") + result[key] = value + return result + return json.loads(data, object_pairs_hook=unique) + + +def checked_path(root, relative): + path = PurePosixPath(relative) + require(path.as_posix() == relative and not path.is_absolute() + and ".." not in path.parts and "\\" not in relative + and ":" not in relative, f"Nonlocal input path: {relative}") + target = root.joinpath(*path.parts) + require(target.resolve().is_relative_to(root.resolve()), + f"Input escapes root: {relative}") + cursor = root + for part in path.parts: + cursor = cursor / part + require(not cursor.is_symlink(), f"Symlink input: {relative}") + return target + + +def import_graph(blobs): + """Conservative static closure, including function-local imports. + + The pinned sources have no relative/dynamic local imports. Only known + source bytes are parsed; missing modules cannot be added by this scan. + """ + graph = {} + for path, data in sorted(blobs.items()): + if not path.startswith("scripts/") or not path.endswith(".py"): + continue + dependencies = set() + for node in ast.walk(ast.parse(data, filename=path)): + if isinstance(node, ast.Import): + names = [alias.name for alias in node.names] + elif isinstance(node, ast.ImportFrom): + require(node.level == 0, f"Unexpected relative import in {path}") + names = [node.module] if node.module else [] + else: + continue + for name in names: + candidate = "scripts/" + name.replace(".", "/") + ".py" + if candidate in blobs: + dependencies.add(candidate) + else: + require(name.split(".")[0] in sys.stdlib_module_names, + f"Unpinned local or third-party import: {name} in {path}") + graph[path] = sorted(dependencies) + return graph + + +def validate_inputs(root, manifest): + require(manifest["entrypoint"] == ENTRY, "Changed entrypoint") + require(manifest["certificate"] == CERTIFICATE, "Changed reference certificate") + blobs = {} + for path, expected in manifest["files"].items(): + data = checked_path(root, path).read_bytes() + require(digest(data) == expected, f"Source hash mismatch: {path}") + blobs[path] = data + require(import_graph(blobs) == manifest["local_import_graph"], + "Static local import inventory changed") + local = set(manifest["local_import_graph"]) + require(ENTRY in local, "Missing driver pin") + reachable, pending = set(), [ENTRY] + while pending: + path = pending.pop() + if path not in reachable: + reachable.add(path) + pending.extend(manifest["local_import_graph"][path]) + require(reachable == local, "Disconnected or missing local source") + upstream = read_json(blobs["upstream/manifest.json"]) + require(upstream["commit"] == manifest["upstream_commit"], "Upstream commit mismatch") + for relative, info in upstream["files"].items(): + path = "upstream/" + relative + require(path in blobs and digest(blobs[path]) == info["sha256"], + f"Missing or changed upstream input: {path}") + expected = read_json(blobs[CERTIFICATE]) + for path, expected_hash in expected["source_sha256"].items(): + require(path in blobs and digest(blobs[path]) == expected_hash, + f"Historical source pin mismatch: {path}") + return blobs, expected + + +def stage_inputs(destination, blobs): + for relative, data in blobs.items(): + target = checked_path(destination, relative) + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(data) + + +# -I ignores Python environment overrides; -S excludes installed site packages. +# The only added module search directory is this fresh, source-verified copy. +BOOTSTRAP = r''' +import json, pathlib, runpy, sys +root = pathlib.Path(sys.argv[1]).resolve() +sys.path.insert(0, str(root / "scripts")) +runpy.run_path(str(root / "scripts/audit_bit_compression.py"), run_name="__main__") +loaded = {} +for name, module in tuple(sys.modules.items()): + source = getattr(module, "__file__", None) + if source: + source = pathlib.Path(source).resolve() + if source.is_relative_to(root): + loaded[name] = source.relative_to(root).as_posix() +(root / "loaded-modules.json").write_text(json.dumps(loaded, sort_keys=True), encoding="utf-8") +''' + + +def compare_certificate(actual, expected): + # Preserve every field, including the historical two source hashes. JSON + # whitespace/key ordering is the only normalization; 1 and true differ. + require(canonical(actual) == canonical(expected), "Historical certificate payload differs") + + +def replay(blobs, expected): + with tempfile.TemporaryDirectory(prefix="bit-compression-provenance-") as directory: + root = Path(directory) + stage_inputs(root, blobs) + # The expected certificate is an input to our comparison, never a + # pre-existing output that could conceal an omitted producer write. + (root / CERTIFICATE).unlink() + started = time.monotonic() + proc = subprocess.run([sys.executable, "-I", "-S", "-B", "-c", BOOTSTRAP, str(root)], + cwd=root, capture_output=True, text=True, encoding="utf-8") + elapsed = time.monotonic() - started + require(proc.returncode == 0, f"Historical producer failed:\n{proc.stdout}\n{proc.stderr}") + require((root / CERTIFICATE).is_file(), "Producer omitted the fresh certificate") + actual = read_json((root / CERTIFICATE).read_bytes()) + compare_certificate(actual, expected) + loaded = read_json((root / "loaded-modules.json").read_bytes()) + require(set(loaded.values()) == set(import_graph(blobs)) - {ENTRY}, + "Runtime local imports differ from the static inventory") + for path, data in blobs.items(): + if path != CERTIFICATE: + require((root / path).read_bytes() == data, f"Replay modified input: {path}") + return {"status": "PASS", "seconds": round(elapsed, 6), + "stdout": proc.stdout.strip().splitlines(), + "stderr": proc.stderr.strip().splitlines(), + "loaded_local_modules": loaded, + "all_certificate_fields_equal": True} + + +def arithmetic_check(certificate): + # Import only this contribution's independent implementation, not the + # historical producer or its arithmetic helpers. + sys.path.insert(0, str(HERE)) + from independent_arithmetic import check + return check(certificate) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--check-only", action="store_true", + help="check inputs and independent arithmetic; do not claim a fresh producer replay") + parser.add_argument("--output", type=Path, help="write a new receipt; existing files are refused") + args = parser.parse_args() + require(not sys.flags.optimize, "Historical assertions must remain enabled") + if args.output: + require(not args.output.exists(), "Receipt path already exists") + manifest = read_json(MANIFEST.read_bytes()) + blobs, expected = validate_inputs(ROOT, manifest) + result = {"status": "INPUTS_AND_ARITHMETIC_ONLY" if args.check_only else "PASS", + "base_commit": manifest["base_commit"], "assignment_seed": 99, + "source_manifest_sha256": digest(MANIFEST.read_bytes()), + "python": platform.python_version(), "platform": platform.platform(), + "input_count": len(blobs), "local_python_files": len(import_graph(blobs)), + "historical_source_pin_count": len(expected["source_sha256"]), + "historical_certificate_sha256": digest(blobs[CERTIFICATE]), + "canonical_certificate_sha256": digest(canonical(expected)), + "independent_arithmetic": arithmetic_check(expected)} + if not args.check_only: + print("Replaying unchanged historical producer in an isolated directory...", flush=True) + result["producer_replay"] = replay(blobs, expected) + output = json.dumps(result, indent=2, sort_keys=True) + "\n" + if args.output: + args.output.parent.mkdir(parents=True, exist_ok=True) + with args.output.open("x", encoding="utf-8", newline="\n") as stream: + stream.write(output) + print(f"{result['status']}: {len(blobs)} pinned inputs; " + f"{len(import_graph(blobs))} local Python files; no new kappa.") + + +if __name__ == "__main__": + main()