From 362d2f91f30b96bc2596ff431c9baf7a78503683 Mon Sep 17 00:00:00 2001 From: Sagar Thakrar Date: Wed, 14 Oct 2026 05:08:58 +0530 Subject: [PATCH] Research tooling: checked cpp_int stand-in runs bit589's native step without Boost (no new kappa) The selected cold replay's only native step (bit589 tune/verify.sh step 4) needs Boost cpp_int, the one dependency pip cannot install. Add an opt-in, exact-or-refuse __int128 stand-in for exactly the subset the three cohort checkers use, and a registered package that runs the pinned step-4 block verbatim with BOOST_INCLUDE at the stand-in and requires the committed Boost-built receipts, checker-side rejections, an over-range refusal and a seeded differential test. No pinned file changes. Seed 92, portable-replay (refined). Prepared with substantial Anthropic Claude assistance. --- .../EXPECTED.json | 35 ++ .../bit589-checked-cpp-int-seed92/README.md | 218 ++++++++++++ .../SOURCES.json | 168 +++++++++ .../bit589-checked-cpp-int-seed92/TASK.json | 66 ++++ .../bit589-checked-cpp-int-seed92/VERIFY.json | 44 +++ .../include/boost/multiprecision/cpp_int.hpp | 109 ++++++ .../bit589-checked-cpp-int-seed92/mutants.py | 94 +++++ .../bit589-checked-cpp-int-seed92/verify.py | 325 ++++++++++++++++++ 8 files changed, 1059 insertions(+) create mode 100644 research/bit589-checked-cpp-int-seed92/EXPECTED.json create mode 100644 research/bit589-checked-cpp-int-seed92/README.md create mode 100644 research/bit589-checked-cpp-int-seed92/SOURCES.json create mode 100644 research/bit589-checked-cpp-int-seed92/TASK.json create mode 100644 research/bit589-checked-cpp-int-seed92/VERIFY.json create mode 100644 research/bit589-checked-cpp-int-seed92/include/boost/multiprecision/cpp_int.hpp create mode 100644 research/bit589-checked-cpp-int-seed92/mutants.py create mode 100644 research/bit589-checked-cpp-int-seed92/verify.py diff --git a/research/bit589-checked-cpp-int-seed92/EXPECTED.json b/research/bit589-checked-cpp-int-seed92/EXPECTED.json new file mode 100644 index 0000000000..db363fda46 --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/EXPECTED.json @@ -0,0 +1,35 @@ +{ + "composed_step4_sha256": "3ae51d121452af525f037c24f7933dd192f0e9ef28425b6ccbbc9f625f02bc27", + "step4_log_lines": 10, + "nondeterministic_fields": [ + "seconds", + "local_scalar_stream" + ], + "mutation_suite_last_lines": [ + "PASS all 6 record-level negative controls rejected", + "PASS all 3 record-level phase-B source mutants rejected by every column checker (legality unchanged)" + ], + "differential": { + "seed": 20261014, + "operations": 100000, + "refusals_per_operation": 40, + "counts": [ + 62870, + 244, + 558 + ] + }, + "refusing_operations": [ + "add8", + "sub8", + "neg8", + "mul", + "imul", + "shl", + "msb", + "shleq", + "shlbig", + "shlneg", + "hex" + ] +} diff --git a/research/bit589-checked-cpp-int-seed92/README.md b/research/bit589-checked-cpp-int-seed92/README.md new file mode 100644 index 0000000000..594c58ad91 --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/README.md @@ -0,0 +1,218 @@ +# Checked cpp_int stand-in for bit589's native step + +**Portability tooling; no new κ.** Seed 92 (verification engineering), card +`portable-replay`, refined scope recorded in [TASK.json](TASK.json). Base +`bce54fb3`. Nothing pinned, selected or under `upstream/` is changed. + +## Problem + +The selected record's `make frontier1094-verify` reaches bit589 through +`scripts/verify_frontier741.py`, which runs +`research/e8-bit-p111-tuned/tune/verify.sh`. Its non-standard requirements +split two ways: + +- `numpy`, `sympy` and `scipy` (`../requirements.txt`) install with pip. +- Boost headers are a system package. Step 4 compiles the three official + cohort checkers `legality_h.cpp`, `columns_h.cpp` and `columns_k.cpp` in + `research/e8-bit-carrier-skeleton/code/bit/checkers/`. These are the only + native code in the selected chain, and their only Boost use is + `boost::multiprecision::cpp_int`. + +On a host without Boost, step 4 stops with +`fatal error: 'boost/multiprecision/cpp_int.hpp' file not found`, so the +selected reproduce command cannot complete. `verify.sh` already reads +`BOOST_INCLUDE`, and `verify_frontier1094.py`, `1042`, `799` and `741` pass +the environment through unchanged. + +## The stand-in + +[include/boost/multiprecision/cpp_int.hpp](include/boost/multiprecision/cpp_int.hpp) +is about 100 lines of standard C++17 plus the GCC/Clang `__int128` type. + +- **Opt in** for the whole selected chain: + `BOOST_INCLUDE=$PWD/research/bit589-checked-cpp-int-seed92/include make frontier1094-verify`. + Real Boost stays the default and the reference. +- **API covered:** exactly the subset the three checkers use. That is + construction from built-in integers, `+ - *`, compound assignment, + non-negative left shift, unary minus, the six comparisons, explicit `bool`, + `str()`, `convert_to()`, default-format decimal stream output + and `msb()`. +- **Anything else fails to compile.** That includes division, `gcd`, other + conversions and floating constructors. +- **Exact or refuse.** Every arithmetic operation is overflow-checked with + `__builtin_{add,sub,mul}_overflow`. Shift counts are checked in their own + type before any narrowing, so negative or huge counts are refused. A zero + value shifts to zero, as in Boost, and a negative value is refused. A value + outside [−2¹²⁷, 2¹²⁷) aborts with + `checked cpp_int stand-in: 128-bit range exceeded`. Nothing ever wraps. + Streaming with `hex`, `oct` or `showpos` is refused rather than printed in + decimal. + +So a checker run that completes has computed the same integers Boost's +unbounded `cpp_int` would. Each implemented operation is ordinary integer +arithmetic on in-range values, and every out-of-range intermediate is a +refusal, never a different value. On a passing run, the checkers themselves +assert `payload < 2^104`. The payload is a product of factors ≥ 1, so every +intermediate is below 2¹⁰⁴. The selected receipts therefore stay well inside +the stand-in's range. + +## Verification + +```sh +python3 scripts/research_package.py --package research/bit589-checked-cpp-int-seed92 --check-only +python3.12 scripts/research_package.py --package research/bit589-checked-cpp-int-seed92 +``` + +`verify.py` uses only the standard library plus `bash` and a C++17 compiler +(`CXX`, default `c++`). GitHub Ubuntu runners ship both. + +**Registration note.** The package compiles C++ inside the generic package +hook, although the +[package guide](../../docs/research/research-package-guide.md) routes native +builds to a dedicated workflow. That is a deliberate departure, for three +reasons: + +- the native build is the subject of the check; +- it uses only the runner's preinstalled `g++` and `bash`, and installs no apt + or pip package and no Boost; +- it takes about 7 s, far inside the hook's limits. + +Maintainers who prefer the rule can move the command into its own workflow, +like `e8-bit-p111-tuned.yml`, and keep `VERIFY.json` for metadata. + +It checks the following: + +1. **Pins and include order.** Every declared input matches its pin. The 25 + inputs include the nine parent files `adapt.py` edits by exact anchors, the + `relax.py` it copies, and the `prime_witnesses.py` that `gram_frames.py` + loads. With + `verify.sh`'s own `-I` order, a probe compiles only if the stand-in is the + `cpp_int` header actually used. +2. **The real step 4, verbatim.** It extracts three parts of the pinned + `verify.sh`: the preamble, the step-3 `adapt.py` command that step 4's + mutation suite needs, and the whole step-4 block. The composed text is + pinned in [EXPECTED.json](EXPECTED.json), and it runs under `bash` with + `BOOST_INCLUDE` at the stand-in. Every original step-4 assertion therefore + runs unchanged: + - six binaries are built with the original flags; + - legality, the five-, nine- and eleven-stage formal columns, the + omitted-bridge controls, the KPAIRS = 2 regression, both mutation suites + and the Gram witnesses all pass. +3. **Equality with the Boost-built receipts.** + - The 10 step-4 output lines equal the step-4 section of the committed + `research/community-frontier741-audit/bit589.log`, timing removed. Those + lines include record and ADD counts, rank mass, column counts, bridge + additions, payload bits, and the 6/6 and 3/3 negative controls. + - `columns_k4.json` equals the committed Boost receipt + `research/community-frontier741-audit/bit/cout/columns_k4.json` field for + field, except `seconds` and the `local_scalar_stream` path. It includes + the exact payload bound `9528119495320518471870892800`. +4. **Rejections come from the checkers.** Both mutation suites are rerun with + full output. No rejection may come from the stand-in. +5. **Over-range control.** All 24,305 ADD coefficients of the record stream + are set to ±2³⁰. The five-stage checker must then refuse with the + stand-in's message instead of wrapping. +6. **Differential test.** A seeded test runs 100,000 operations against + Python integers, plus fixed rows. The fixed rows cover: + - values at −2¹²⁷ and 2¹²⁷ − 1; + - shifts ending exactly at or just below 2¹²⁷; + - zero shifted far; + - multiples of 2⁶⁴ with a zero low word; + - comparisons that share a high word. + + 62,870 in-range results must be equal. 558 refusals are required: every one + of the 244 whose exact result lies within a factor of 2 of the bound, plus a + per-operation sample covering each refusal path: + - multiply, and scaled add/subtract/negate at ±2¹²⁷; + - left shift, by `<<` and `<<=`; + - 64-bit and negative shift counts; + - `msb(≤ 0)`; + - non-decimal stream flags. + + These coverage counts are pinned in `EXPECTED.json`. + +With `--boost-include DIR`, step 4 also runs against real Boost and every +receipt must match the stand-in's. I could not run that mode here, because this +host has no Boost. + +**Results.** `PASS_BIT589_STEP4_CHECKED_CPP_INT_STANDIN` in about 7 s with: + +- Apple clang 21 and Python 3.12.15; +- Homebrew GCC 16.2 and Python 3.14.8; +- clang with `-fsanitize=undefined -fno-sanitize-recover=undefined` on every + compiled binary: no runtime report from the six checkers, the mutation + suites, the over-range control or the differential harness. A positive + control confirms the sanitizer reports a 128-bit over-shift. Reproduce with + `python3 research/bit589-checked-cpp-int-seed92/mutants.py --ubsan`. + +**Header mutation test.** Run `python3 research/bit589-checked-cpp-int-seed92/mutants.py` +(opt-in, about two minutes). It reruns `verify.py` against 17 mutants of the +stand-in. 16 are rejected: + +- wrapping `*`, `+`, `-` or unary `-`; +- an unchecked shift range, a shift that wraps into the sign bit, a narrowed + shift count, or an accepted negative count; +- a zero-value shift that falls through; +- ignored stream flags; +- `msb` of zero, or an off-by-one `msb`; +- a dropped sign in `str()`; +- `<` implemented as `<=`; +- `bool` truncated to 64 bits; +- `>` wrong for a shared high word. + +The one accepted mutant removes the negative-value shift guard. It is +equivalent: negative counts are refused first, and the range check refuses +every negative value. + +## Scope and limits + +- **Not removed:** the pip dependencies (`numpy`, `sympy`, `scipy`). This + package removes only the one non-pip blocker. +- **Toolchain:** GCC or Clang is required. MSVC has no `__int128`. +- **Other repository `cpp_int` users are out of scope.** Examples are the + optional `audit_coset_paths.cpp` and the enlarged-positive-frame profilers, + which use division. They fail to compile against the stand-in rather than + run with different semantics. +- **Not run:** the complete `make frontier1094-verify` with the stand-in. This + host lacks `sympy` and `scipy`, which steps 5–7 need. Step 4, the only Boost + step, is fully covered above. +- **Path limitation:** the pinned `verify.sh` expands paths unquoted, so it and + this verifier need a checkout path without spaces. That is a pre-existing + limit, and `verify.py` checks for it explicitly. +- **Claim:** this certifies equal finite checker results, not the + multiplication theorem. + +## Open next questions + +The structured entries are in [TASK.json](TASK.json). + +1. **Does the complete chain pass without Boost?** + - *The question:* does `make frontier1094-verify` pass with the stand-in on + a host that has the pip dependencies but no Boost? + - *Why it matters:* it would show the whole selected cold replay needs no + non-pip system package. + - *First step:* run it in a fresh Ubuntu job without `libboost-dev` and + compare receipts with a Boost run. + - *Blocker:* this host lacks `sympy` and `scipy`, and the job needs a + dedicated long-running workflow. +2. **Should `tune/verify.sh` quote its path expansions?** + - *Why it matters:* it fails from checkout paths that contain spaces. + - *First step:* a quoted successor replayed byte for byte. + - *Blocker:* it is in a selected source root, so edits need a maintainer + rehash. + +## Credit + +- **The checkers:** Dylan Crow's #370 official checkers, generalised; DaysSky's + #381 `columns_k`. +- **The carrier skeleton and its `portable-include/bits/stdc++.h` precedent:** + eumemic, who also wrote the #555 supplier. +- **The #589 bit transport and tuned word:** DreamingOfClouds. +- **The #741 audit's committed Boost receipts:** Douglas Colkitt. +- **The broader lineage:** credited in the + [selected review](../../docs/research/community-batch1099-review.md). + +Prepared by Sagar Thakrar with substantial Anthropic Claude assistance. That +included explorer, judge and skeptic subagents, which mapped the selected +chain's dependencies, prototyped the stand-in and reviewed it. New work is +Apache-2.0. diff --git a/research/bit589-checked-cpp-int-seed92/SOURCES.json b/research/bit589-checked-cpp-int-seed92/SOURCES.json new file mode 100644 index 0000000000..1e6289f49c --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/SOURCES.json @@ -0,0 +1,168 @@ +{ + "schema_version": 1, + "base_commit": "bce54fb3f895cbe5cd4c0eb477c3afeed100190f", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "ae6a99f28aa468649eb66a361e764d769363bf11", + "sha256": "10b9e340fd8e354a37a0c4813c90e5de1dd42044b5f6aca4a3fc5a42b685fa32" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "c05fafaa25ba95b2d5af86b1800427198e3d20eb", + "sha256": "89b5928bc92c81e2647b6d702a7ad85a6795b8f44e2eb267b2ff3145d8f2b2ce" + }, + { + "path": "research/community-frontier741-audit/bit/cout/columns_k4.json", + "usage": "input", + "git_blob": "d461ac2c4a65a766923bfcd6cccc035e5c57d17d", + "sha256": "1156441a9f77f8fc895b978cb8bac53126c5a958e6ea9518519e5f77af4cea97" + }, + { + "path": "research/community-frontier741-audit/bit589.log", + "usage": "input", + "git_blob": "4a24e0d7de11f8b168ea532325f42c517e064c32", + "sha256": "83d2e8ccd6924f2138350da693f8cecedaa8c113e758ad22f5f29725e9cccc7e" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/checkers/columns_h.cpp", + "usage": "input", + "git_blob": "00c5a3a5d81d083582f60f33dd2e7bea5049c37f", + "sha256": "d1297f9052be4f2496e1ff2a77e4db2c0cdbd7d460d489b4c03e50bf317238ce" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/checkers/columns_k.cpp", + "usage": "input", + "git_blob": "42bb232fbec4de703e70cc13dc2f39a3b43a3a01", + "sha256": "1c344d18fcd494eeca93e259f10cafae7705780e6592f6dcb35b34752dab9bc2" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/checkers/gram_frames.py", + "usage": "input", + "git_blob": "b740d47295c02692260d7cc01874a49fe48f78ab", + "sha256": "223a5a07731fc315a90a3e48177796babc155c1d9985c7ae5017eff6af2a0608" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/checkers/json.hpp", + "usage": "input", + "git_blob": "82d69f7c5d044c9887c96b90c97f5639083ecd14", + "sha256": "aaf127c04cb31c406e5b04a63f1ae89369fccde6d8fa7cdda1ed4f32dfc5de63" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/checkers/legality_h.cpp", + "usage": "input", + "git_blob": "cb1233c7e96b1aa8bbf4d2e4165d75ab0dca64b9", + "sha256": "6c1ef2b24e57781d6951c172a1024fc4300e6fd392c9d6672079a7806ab663b3" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/export_records.py", + "usage": "input", + "git_blob": "3632d370486a02f5456f7643ced03f8c925b2350", + "sha256": "8a5999601f917dbb492bb9ae9a344611f18cc7cedc53dc3c76496a369e530eb3" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/independent/checkword.py", + "usage": "input", + "git_blob": "da4e0fe05067184d4dab1e130ae1384c1fa983f5", + "sha256": "11bf9620ca9db16d52d9fe806b814fc9ec9a89c4d7645354d1861d96f6e805ad" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/independent/relax.py", + "usage": "input", + "git_blob": "a37e8b8176080d4a977bb48bd25f92f4a62b156b", + "sha256": "d6e9f58de3fa41f538516b6227c249d61fb30f0f6843027cc8a07a1a372fa6b6" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/mutate_records.py", + "usage": "input", + "git_blob": "6794516f665d36eac189894e350a5ad01113cb4c", + "sha256": "b0e75d6550412a7be4017695e6b237392b611bb852ad8ff309d6682c9f166a4e" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/regression/stage_cohort.py", + "usage": "input", + "git_blob": "f6d7afe3e623208c4207b6a683a1e3e49249e31c", + "sha256": "6c288f6f234b528c1a7c0a4adfb724e91e0fe6b18c8dffad96af3f973de1829a" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/eleven/assemble.py", + "usage": "input", + "git_blob": "87cc45bc831fa7003906e8b026ac1480a69da1de", + "sha256": "c47889a606701ae592374dd82a13cee8c5a4e8449b7facfdad2d90563a30a18c" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/eleven/audit.py", + "usage": "input", + "git_blob": "10e47d32e3b4769ae5ce5bad9f76a7b3e5d797fc", + "sha256": "5ad832830c19ec6c904d1b64480f6b1e71c5673fdd38d487d17e179b1d197c09" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/eleven/emit.py", + "usage": "input", + "git_blob": "640cdb4d1220950e198f2fd9994d76d69ecbf764", + "sha256": "b0b25955875625ed63f82690f83aef976e3e9a978ae6620b23a23fc1207acd46" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/eleven/local_audit.py", + "usage": "input", + "git_blob": "2233c5f35f2fcb58809f7a1d8f91e9b36ad74080", + "sha256": "95275473d7f9811011bda760dd0f04de5169c2a6922456dc9a5725a5d671f941" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/hub/r740-hosted-eleven/code/geometry.py", + "usage": "input", + "git_blob": "b4ad5074014dc00a01143910eb1cd27fafe0e15c", + "sha256": "9554182d67e434ea84b4430f909a2c400abc755d8487b51454a8e6968e62859d" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/nine/geometry.py", + "usage": "input", + "git_blob": "91594e0223e8fac49162b5aef834ffaa8e94a254", + "sha256": "36eeda3184bd009e89462b65c8725d10860c444f8da8f1f87e5f6e50120011ca" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/pricing/prime_witnesses.py", + "usage": "input", + "git_blob": "7f16d930302ae879c562dd58e085d9747fcac077", + "sha256": "98cd03bc9582e7bd0ab194c191ea8eea940539e510051443ab9fd3e572a364ab" + }, + { + "path": "research/e8-bit-carrier-skeleton/discovery/host_select.py", + "usage": "input", + "git_blob": "5b50de453ddbd90d433f9ea8ed584308e7ab2d0f", + "sha256": "2aaaac18e083d289d4341e734c32571c89c51fefba04b388d407c7372b906dc7" + }, + { + "path": "research/e8-bit-carrier-skeleton/portable-include/bits/stdc++.h", + "usage": "input", + "git_blob": "fc5664e929d9a81d9b4926bee049a93330be86ec", + "sha256": "420094be2af295eed03a983dd66debbbe9cfb78d8252859f4498a615a0074b99" + }, + { + "path": "research/e8-bit-p111-tuned/code/mutrec_b.py", + "usage": "input", + "git_blob": "7b644a4a03423404348633680a6c503d3a2c7060", + "sha256": "231cbf26bfe5e6824347e4556959941d8b4dea1efa06d7f88d7a73cca8b03158" + }, + { + "path": "research/e8-bit-p111-tuned/tune/bitE8mut-p111-tuned.json.gz", + "usage": "input", + "git_blob": "85a4499bf9e4911a705d50567a2c2ef6cf23739a", + "sha256": "e3e301e62bc2b41dbf1ceac0acf3d35824ebeab1848c5a2142d4577b109da52f" + }, + { + "path": "research/e8-bit-p111-tuned/tune/verify.sh", + "usage": "input", + "git_blob": "92310cbc220ece46c72f2131ddcc8d802bd35c56", + "sha256": "efb1051d3e2c6119574a29a8e4efbe316307b5901d05079d029e12fae9ff1fb1" + }, + { + "path": "research/e8-bit-p111-tuned/vendor/pr545/code/adapt.py", + "usage": "input", + "git_blob": "bd1552af18ae259c01039627aa06006b9d5e760f", + "sha256": "b9a50303147a3003ad94fc4b1a822d4fca7f9b9e917d165e3c45152575302a0e" + } + ] +} diff --git a/research/bit589-checked-cpp-int-seed92/TASK.json b/research/bit589-checked-cpp-int-seed92/TASK.json new file mode 100644 index 0000000000..c4e42128a9 --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/TASK.json @@ -0,0 +1,66 @@ +{ + "schema_version": 1, + "task_id": "portable-replay", + "priority_revision": 2, + "base_commit": "bce54fb3f895cbe5cd4c0eb477c3afeed100190f", + "original_card": { + "id": "portable-replay", + "question": "Can one reproducible environment or artifact-comparison failure be repaired?", + "starting_points": [ + "research/gzip-container-reproducibility/README.md", + "docs/ci-verification.md" + ], + "methods": [ + "Reproduce one portability failure and add a minimal cross-environment fix.", + "Audit one canonical-payload comparison for omitted proof-relevant fields." + ], + "acceptance_gate": "Preserve immutable source hashes and proof-relevant content; a different compressed byte stream alone is not a mathematical mismatch.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Read the gzip-container reproducibility examples and distinguish frozen source bytes from regenerated payloads.", + "scope": "Positive control / source-bound orientation only; this is not a new construction or full cold replay." + }, + "first_experiment": "Reproduce one specific mismatch or audit one canonical comparison for omitted proof-relevant fields.", + "bottleneck_link": "Portable exact checks prevent environment failures from blocking valid contributions.", + "allowed_variations": [ + "One interpreter/version mismatch or one payload field comparison." + ], + "scope_change_triggers": [ + "Removing immutable-source pins because generated compressed bytes differ." + ], + "overlap_terms": [ + "gzip container", + "reproducibility", + "canonical payload" + ] + }, + "scope_status": "refined", + "actual_scope": "Can the one native dependency of the selected cold replay that pip cannot install (Boost cpp_int, used only by bit589 step 4's three cohort checkers) be replaced, opt-in, by a checked stand-in whose completed runs are exactly Boost's, without editing any pinned file?", + "scope_change_reason": "The card asks to repair one reproducible environment failure. The failure repaired is an environment dependency rather than a canonical-payload comparison: without Boost headers, tune/verify.sh stops at step 4 and the selected reproduce command cannot complete. A five-explorer sweep with impact/rigor/novelty judges ranked it as the strongest uncovered portability blocker on the selected chain.", + "bottleneck_link": "Side result for verification portability: no kappa change. It lets agents and donors without system Boost run the selected record's only native step, and its exact-or-refuse argument plus receipt equality keep the check as strong as real Boost.", + "budget_minutes": 120, + "stop_rule": "Stop once the verbatim step-4 block passes with the stand-in and reproduces the committed Boost receipts on two compilers, every negative control is rejected by the checkers rather than the stand-in, and header mutants are killed; do not edit pinned files or attempt numpy/sympy/scipy replacements.", + "outcome": "completed", + "evidence": "verify.py prints PASS_BIT589_STEP4_CHECKED_CPP_INT_STANDIN with Apple clang 21 / Python 3.12.15, GCC 16.2 / Python 3.14.8 and an UBSan-instrumented clang build in about 7 s: the pinned verify.sh step-4 block runs verbatim and its 10 output lines equal the committed Boost-built bit589.log section; columns_k4.json equals the committed Boost receipt except seconds and a path; 6/6 and 3/3 mutation controls are rejected by the checkers; an over-range stream is refused; 62,870 seeded and boundary operations equal Python integers and 558 refusals hold, including all 244 within 2x of the bound. mutants.py rejects 16 of 17 header mutants; the accepted one is equivalent.", + "author": "Sagar Thakrar", + "ai_assistance": "Substantial Anthropic Claude (Claude Code, Opus) assistance, including explorer, judge and skeptic subagents that mapped dependencies, prototyped the stand-in and reviewed it; the author reviewed, ran and is responsible for the result.", + "open_questions": [ + { + "question": "Does the complete make frontier1094-verify pass with BOOST_INCLUDE pointing at the stand-in on a host that has numpy, sympy and scipy but no Boost?", + "why_it_matters": "It would show the whole selected cold chain reproduces without any non-pip system dependency.", + "first_step": "Run BOOST_INCLUDE=$PWD/research/bit589-checked-cpp-int-seed92/include make frontier1094-verify in a fresh Ubuntu job without libboost-dev and compare its receipts with a Boost run.", + "blocked_by": "This host lacks sympy and scipy, and the cold chain needs a dedicated long-running job rather than the generic package hook." + }, + { + "question": "Should the pinned tune/verify.sh quote its paths so it runs from checkouts whose path contains spaces?", + "why_it_matters": "It currently splits unquoted $C/$B/$WD expansions, so it fails from paths such as macOS Application Support folders.", + "first_step": "Quote the expansions in a successor script and replay bit589 byte-for-byte.", + "blocked_by": "verify.sh is part of a selected source root, so edits need a maintainer rehash." + } + ] +} diff --git a/research/bit589-checked-cpp-int-seed92/VERIFY.json b/research/bit589-checked-cpp-int-seed92/VERIFY.json new file mode 100644 index 0000000000..f7c50b92eb --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/VERIFY.json @@ -0,0 +1,44 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/community-frontier741-audit/bit/cout/columns_k4.json", + "research/community-frontier741-audit/bit589.log", + "research/e8-bit-carrier-skeleton/code/bit/checkers/columns_h.cpp", + "research/e8-bit-carrier-skeleton/code/bit/checkers/columns_k.cpp", + "research/e8-bit-carrier-skeleton/code/bit/checkers/gram_frames.py", + "research/e8-bit-carrier-skeleton/code/bit/checkers/json.hpp", + "research/e8-bit-carrier-skeleton/code/bit/checkers/legality_h.cpp", + "research/e8-bit-carrier-skeleton/code/bit/export_records.py", + "research/e8-bit-carrier-skeleton/code/bit/independent/checkword.py", + "research/e8-bit-carrier-skeleton/code/bit/independent/relax.py", + "research/e8-bit-carrier-skeleton/code/bit/mutate_records.py", + "research/e8-bit-carrier-skeleton/code/bit/regression/stage_cohort.py", + "research/e8-bit-carrier-skeleton/code/eleven/assemble.py", + "research/e8-bit-carrier-skeleton/code/eleven/audit.py", + "research/e8-bit-carrier-skeleton/code/eleven/emit.py", + "research/e8-bit-carrier-skeleton/code/eleven/local_audit.py", + "research/e8-bit-carrier-skeleton/code/hub/r740-hosted-eleven/code/geometry.py", + "research/e8-bit-carrier-skeleton/code/nine/geometry.py", + "research/e8-bit-carrier-skeleton/code/pricing/prime_witnesses.py", + "research/e8-bit-carrier-skeleton/discovery/host_select.py", + "research/e8-bit-carrier-skeleton/portable-include/bits/stdc++.h", + "research/e8-bit-p111-tuned/code/mutrec_b.py", + "research/e8-bit-p111-tuned/tune/bitE8mut-p111-tuned.json.gz", + "research/e8-bit-p111-tuned/tune/verify.sh", + "research/e8-bit-p111-tuned/vendor/pr545/code/adapt.py" + ], + "commands": [ + { + "argv": [ + "{python}", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 600, + "checks": "Registration note: the native build is the subject of this check. It uses only the runner's preinstalled bash and C++17 compiler (g++), installs no apt or pip packages and needs no Boost; it runs in about 7 s. This departs from the guide's dedicated-workflow rule for native builds, which maintainers can restore by moving the command to its own workflow. Runs the pinned verify.sh preamble, step-3 adapt.py command and step-4 block verbatim with BOOST_INCLUDE at the stand-in; requires the stand-in to be the compiled header, the 10 step-4 log lines and columns_k4.json to equal the committed Boost receipts, all 9 negative controls to be rejected by the checkers, an over-range stream to be refused, and a seeded differential test against Python integers with pinned in-range, near-bound and per-operation refusal coverage." + } + ] +} diff --git a/research/bit589-checked-cpp-int-seed92/include/boost/multiprecision/cpp_int.hpp b/research/bit589-checked-cpp-int-seed92/include/boost/multiprecision/cpp_int.hpp new file mode 100644 index 0000000000..bc9634fd90 --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/include/boost/multiprecision/cpp_int.hpp @@ -0,0 +1,109 @@ +// Checked 128-bit stand-in for the subset of boost::multiprecision::cpp_int used by +// research/e8-bit-carrier-skeleton/code/bit/checkers/{legality_h,columns_h,columns_k}.cpp, +// the only native code in the selected record's cold replay (bit589 step 4). +// +// Exact or refuse: every arithmetic operation is overflow-checked on __int128. A value +// outside [-2^127, 2^127) aborts with a message instead of wrapping, so a run that +// completes computed exactly the integers Boost's unbounded cpp_int would. Operations +// outside the subset do not compile. Opt in with BOOST_INCLUDE=; +// real Boost remains the default and the reference. Requires GCC or Clang (__int128). +// +// Sagar Thakrar with substantial Anthropic Claude assistance. Apache-2.0. +#ifndef INTEGER_MULT_CHECKED_CPP_INT_HPP +#define INTEGER_MULT_CHECKED_CPP_INT_HPP +#define INTEGER_MULT_CHECKED_CPP_INT_STANDIN 1 +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { namespace multiprecision { + +class cpp_int { + __int128 v_; + [[noreturn]] static void refuse(const char* op) { + std::fprintf(stderr, "checked cpp_int stand-in: 128-bit range exceeded in %s; use real Boost headers\n", op); + std::abort(); + } + template + using if_int = typename std::enable_if::value && !std::is_same::value, int>::type; + + public: + cpp_int() : v_(0) {} + template = 0> + cpp_int(T x) : v_(static_cast<__int128>(x)) { + static_assert(sizeof(T) <= 8, "checked cpp_int stand-in: built-in integers up to 64 bits only"); + } + explicit operator bool() const { return v_ != 0; } + + cpp_int& operator+=(const cpp_int& o) { if (__builtin_add_overflow(v_, o.v_, &v_)) refuse("+"); return *this; } + cpp_int& operator-=(const cpp_int& o) { if (__builtin_sub_overflow(v_, o.v_, &v_)) refuse("-"); return *this; } + cpp_int& operator*=(const cpp_int& o) { if (__builtin_mul_overflow(v_, o.v_, &v_)) refuse("*"); return *this; } + // The count is range-checked in its own type before any narrowing. + template = 0> + cpp_int& operator<<=(T s) { + if (s < static_cast(0)) refuse("<< by a negative count"); + if (v_ < 0) refuse("<< of a negative value"); + if (v_ == 0) return *this; + if (static_cast(s) >= 127 || (v_ >> (127 - static_cast(s))) != 0) refuse("<<"); + v_ <<= static_cast(s); + return *this; + } + cpp_int operator-() const { + cpp_int r; + if (__builtin_sub_overflow(static_cast<__int128>(0), v_, &r.v_)) refuse("unary -"); + return r; + } + friend cpp_int operator+(cpp_int a, const cpp_int& b) { return a += b; } + friend cpp_int operator-(cpp_int a, const cpp_int& b) { return a -= b; } + friend cpp_int operator*(cpp_int a, const cpp_int& b) { return a *= b; } + template = 0> + friend cpp_int operator<<(cpp_int a, T s) { return a <<= s; } + friend bool operator==(const cpp_int& a, const cpp_int& b) { return a.v_ == b.v_; } + friend bool operator!=(const cpp_int& a, const cpp_int& b) { return a.v_ != b.v_; } + friend bool operator<(const cpp_int& a, const cpp_int& b) { return a.v_ < b.v_; } + friend bool operator>(const cpp_int& a, const cpp_int& b) { return a.v_ > b.v_; } + friend bool operator<=(const cpp_int& a, const cpp_int& b) { return a.v_ <= b.v_; } + friend bool operator>=(const cpp_int& a, const cpp_int& b) { return a.v_ >= b.v_; } + + int sign() const { return (v_ > 0) - (v_ < 0); } + unsigned __int128 magnitude() const { + return v_ < 0 ? static_cast(0) - static_cast(v_) + : static_cast(v_); + } + std::string str() const { + unsigned __int128 m = magnitude(); + std::string s; + do { s.insert(s.begin(), static_cast('0' + static_cast(m % 10))); m /= 10; } while (m); + if (v_ < 0) s.insert(s.begin(), '-'); + return s; + } + template + T convert_to() const { + static_assert(std::is_same::value, "checked cpp_int stand-in: only convert_to"); + return str(); + } + // Default decimal formatting only; Boost would honour hex, oct and showpos. + friend std::ostream& operator<<(std::ostream& os, const cpp_int& x) { + const std::ios_base::fmtflags base = os.flags() & std::ios_base::basefield; + if ((base != std::ios_base::dec && base != std::ios_base::fmtflags(0)) || (os.flags() & std::ios_base::showpos)) + refuse("stream output with non-default format flags"); + return os << x.str(); + } +}; + +// Index of the most significant set bit. Like Boost, refuses operands <= 0. +inline std::size_t msb(const cpp_int& x) { + if (x.sign() <= 0) throw std::domain_error("No bits were set in the operand."); + unsigned __int128 m = x.magnitude(); + std::size_t i = 0; + while (m >>= 1) ++i; + return i; +} + +}} // namespace boost::multiprecision +#endif diff --git a/research/bit589-checked-cpp-int-seed92/mutants.py b/research/bit589-checked-cpp-int-seed92/mutants.py new file mode 100644 index 0000000000..365a366bfb --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/mutants.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 +"""Opt-in checks of verify.py itself (not registered; about two minutes). + +Default: apply each listed mutation to a temporary copy of the stand-in header +and run verify.py against it. Every mutant must be rejected except the one +recorded as equivalent. --ubsan: run verify.py once with every binary built +with -fsanitize=undefined -fno-sanitize-recover=undefined, after a positive +control shows that the sanitizer is active. + +Sagar Thakrar with substantial Anthropic Claude assistance. Apache-2.0. +""" +import argparse +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +PACKAGE = Path(__file__).resolve().parent +HEADER = PACKAGE/'include/boost/multiprecision/cpp_int.hpp' +VERIFY = PACKAGE/'verify.py' +# (name, original text, replacement, rejected by verify.py) +MUTANTS = [ + ('mul wraps', 'if (__builtin_mul_overflow(v_, o.v_, &v_)) refuse("*");', '(void)__builtin_mul_overflow(v_, o.v_, &v_);', True), + ('add wraps', 'if (__builtin_add_overflow(v_, o.v_, &v_)) refuse("+");', '(void)__builtin_add_overflow(v_, o.v_, &v_);', True), + ('sub wraps', 'if (__builtin_sub_overflow(v_, o.v_, &v_)) refuse("-");', '(void)__builtin_sub_overflow(v_, o.v_, &v_);', True), + ('unary minus wraps', 'if (__builtin_sub_overflow(static_cast<__int128>(0), v_, &r.v_)) refuse("unary -");', + '(void)__builtin_sub_overflow(static_cast<__int128>(0), v_, &r.v_);', True), + ('shift range unchecked', ' if (static_cast(s) >= 127 || (v_ >> (127 - static_cast(s))) != 0) refuse("<<");\n', '', True), + ('shift wraps into the sign bit', '>= 127 || (v_ >> (127 - static_cast(s)))', '>= 128 || (s > 0 && (v_ >> (128 - static_cast(s))))', True), + ('shift count narrowed', 'static_cast(s) >= 127', 'static_cast(s) >= 127', True), + ('negative count accepted', ' if (s < static_cast(0)) refuse("<< by a negative count");\n', '', True), + ('zero shift falls through', ' if (v_ == 0) return *this;\n', '', True), + # Equivalent: negative counts are refused first and the range check refuses every negative value. + ('negative value guard removed', ' if (v_ < 0) refuse("<< of a negative value");\n', '', False), + ('stream flags ignored', ' refuse("stream output with non-default format flags");', ' (void)0;', True), + ('msb off by one', ' std::size_t i = 0;\n', ' std::size_t i = 1;\n', True), + ('msb of zero accepted', ' if (x.sign() <= 0) throw', ' if (false) throw', True), + ('str drops the sign', " if (v_ < 0) s.insert(s.begin(), '-');\n", '', True), + ('< implemented as <=', 'return a.v_ < b.v_; }', 'return a.v_ <= b.v_; }', True), + ('bool truncated to 64 bits', 'explicit operator bool() const { return v_ != 0; }', + 'explicit operator bool() const { return static_cast(v_) != 0; }', True), + ('> ignores a shared high word', 'return a.v_ > b.v_; }', + 'return (a.v_ >> 64) == (b.v_ >> 64) && (a.v_ >> 64) > 0 ? false : a.v_ > b.v_; }', True), +] + + +def verify(include=None, env=None): + argv = [sys.executable, '-B', str(VERIFY)] + (['--include', str(include)] if include else []) + p = subprocess.run(argv, capture_output=True, text=True, env=env, timeout=1200) + return p.returncode, (p.stdout + p.stderr).strip().splitlines()[-1] + + +def mutants(): + source = HEADER.read_text() + wrong = [] + with tempfile.TemporaryDirectory(prefix='standin-mutants-') as tmp: + target = Path(tmp)/'include/boost/multiprecision/cpp_int.hpp' + target.parent.mkdir(parents=True) + for name, old, new, rejected in MUTANTS: + if source.count(old) != 1: + raise SystemExit('FAIL: mutation anchor not unique: ' + name) + target.write_text(source.replace(old, new)) + code, last = verify(Path(tmp)/'include') + print(('rejected ' if code else 'accepted ') + name + ' | ' + last[:100], flush=True) + if bool(code) != rejected: + wrong.append(name) + if wrong: + raise SystemExit('FAIL: unexpected mutant outcomes: ' + ', '.join(wrong)) + print('PASS_STANDIN_MUTANTS', sum(m[3] for m in MUTANTS), 'rejected,', sum(not m[3] for m in MUTANTS), 'equivalent accepted') + + +def ubsan(): + cxx = os.environ.get('CXX', 'c++') + with tempfile.TemporaryDirectory(prefix='standin-ubsan-') as tmp: + wrapper = Path(tmp)/'cxx' + wrapper.write_text('#!/bin/sh\nexec %s -fsanitize=undefined -fno-sanitize-recover=undefined "$@"\n' % cxx) + wrapper.chmod(0o755) + probe = Path(tmp)/'probe.cpp' + probe.write_text('int main(int c, char**) { __int128 x = 1; x <<= 190 + c; return (int)x; }\n') + subprocess.run([str(wrapper), '-o', str(Path(tmp)/'probe'), str(probe)], check=True) + p = subprocess.run([str(Path(tmp)/'probe')], capture_output=True, text=True) + if p.returncode == 0 or 'runtime error' not in p.stderr: + raise SystemExit('FAIL: the sanitizer positive control was not reported') + code, last = verify(env=dict(os.environ, CXX=str(wrapper))) + if code: + raise SystemExit('FAIL: verify.py under UBSan: ' + last) + print('PASS_STANDIN_UBSAN positive control reported; verify.py passed with every binary sanitized') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--ubsan', action='store_true') + ubsan() if parser.parse_args().ubsan else mutants() diff --git a/research/bit589-checked-cpp-int-seed92/verify.py b/research/bit589-checked-cpp-int-seed92/verify.py new file mode 100644 index 0000000000..791b15737a --- /dev/null +++ b/research/bit589-checked-cpp-int-seed92/verify.py @@ -0,0 +1,325 @@ +#!/usr/bin/env python3 +"""Run bit589's native step (tune/verify.sh step 4) without Boost, and check it. + +The pinned verify.sh preamble, its step-3 adapt.py command and its step-4 block +run verbatim under bash with BOOST_INCLUDE set to this package's checked cpp_int +stand-in. The step-4 output must equal the committed Boost-built bit589.log and +columns_k4.json receipts. Further checks: the stand-in is the header actually +compiled, every negative control is rejected by the checkers rather than the +stand-in, an over-range control is refused, and a seeded differential test +against Python integers passes. With --boost-include, step 4 also runs against +real Boost and every receipt must match. + +Sagar Thakrar with substantial Anthropic Claude assistance. Apache-2.0. +""" +import argparse +from hashlib import sha256 +import json +import os +from pathlib import Path +import random +import re +import shutil +import struct +import subprocess +import sys +import tempfile + +PACKAGE = Path(__file__).resolve().parent +ROOT = PACKAGE.parents[1] +INCLUDE = PACKAGE/'include' +TUNE = ROOT/'research/e8-bit-p111-tuned/tune' +SKELETON = ROOT/'research/e8-bit-carrier-skeleton' +CHECKERS = SKELETON/'code/bit/checkers' +BOOST_K4 = ROOT/'research/community-frontier741-audit/bit/cout/columns_k4.json' +BOOST_LOG = ROOT/'research/community-frontier741-audit/bit589.log' +EXPECTED = json.loads((PACKAGE/'EXPECTED.json').read_bytes()) +REFUSAL = 'checked cpp_int stand-in: 128-bit range exceeded' +OPS = ['add', 'sub', 'mul', 'imul', 'neg', 'shl', 'cmp', 'msb', 'add8', 'sub8', 'neg8', + 'shleq', 'shlbig', 'shlneg', 'hex'] +PROBE = '''#include +#ifndef INTEGER_MULT_CHECKED_CPP_INT_STANDIN +#error "the checked stand-in is not the cpp_int header being compiled" +#endif +int main() { return 0; } +''' +HARNESS = '''#include +#include +#include +using boost::multiprecision::cpp_int; +static cpp_int mk(long long h, long long l) { return cpp_int(h) * (cpp_int(1) << 62) + cpp_int(l); } +int main() { + std::string op; long long ah, al, bh, bl; + while (std::cin >> op >> ah >> al >> bh >> bl) { + cpp_int a = mk(ah, al), b = mk(bh, bl); + if (op == "add") { cpp_int c = a; c += b; std::cout << c.convert_to() << "\\n"; } + else if (op == "sub") std::cout << (a - b).convert_to() << "\\n"; + else if (op == "mul") std::cout << (a * b).convert_to() << "\\n"; + else if (op == "imul") std::cout << (bl * a).convert_to() << "\\n"; + else if (op == "neg") std::cout << (-a).str() << "\\n"; + else if (op == "add8") { cpp_int c = a * 8; c += b * 8; std::cout << c << "\\n"; } + else if (op == "sub8") std::cout << (a * 8 - b * 8) << "\\n"; + else if (op == "neg8") std::cout << -(a * 8) << "\\n"; + else if (op == "shl") std::cout << (a << (int)bl).convert_to() << "\\n"; + else if (op == "shleq") { cpp_int c = a; c <<= bl; std::cout << c.str() << "\\n"; } + else if (op == "shlbig") std::cout << (a << ((1LL << 32) + bl)).str() << "\\n"; + else if (op == "shlneg") std::cout << (a << (-1 - bl)).str() << "\\n"; + else if (op == "hex") std::cout << std::hex << a << "\\n"; + else if (op == "cmp") std::cout << (a < b) << (a > b) << (a == b) << (a <= b) << (a >= b) << (a != b) << bool(a) << "\\n"; + else if (op == "msb") std::cout << boost::multiprecision::msb(a) << "\\n"; + std::cout.flush(); + } +} +''' + + +def require(condition, message): + if not condition: + raise SystemExit('FAIL: ' + message) + + +def digest(data): + return sha256(data).hexdigest() + + +def run(argv, **kw): + return subprocess.run([str(a) for a in argv], capture_output=True, text=True, **kw) + + +def check_pins(): + rows = [r for r in json.loads((PACKAGE/'SOURCES.json').read_bytes())['files'] if r['usage'] == 'input'] + for row in rows: + require(digest((ROOT/row['path']).read_bytes()) == row['sha256'], 'Pinned input: ' + row['path']) + return len(rows) + + +def step4_script(): + """The pinned preamble, step-3 adapt.py command and step-4 block of tune/verify.sh.""" + lines = (TUNE/'verify.sh').read_text().splitlines() + def index(predicate, what): + found = [i for i, line in enumerate(lines) if predicate(line)] + require(len(found) == 1, 'verify.sh marker not unique: ' + what) + return found[0] + start = index(lambda s: s == 'set -euo pipefail', 'set') + helper = index(lambda s: s.startswith('step() {'), 'step()') + adapt = index(lambda s: s.startswith('$PY $A545/adapt.py $P $WORD $WD/ad545'), 'adapt') + four = index(lambda s: s.startswith('step "4.'), 'step 4') + five = index(lambda s: s.startswith('step "5.'), 'step 5') + require(start < helper < adapt < four < five, 'verify.sh step order') + text = '\n'.join(lines[start:helper+1] + [lines[adapt]] + lines[four:five]) + '\n' + require(digest(text.encode()) == EXPECTED['composed_step4_sha256'], 'Composed step-4 script differs from pin') + return text + + +def section(text, header): + """Lines from the `==
` line to the next `== ` line, timing removed.""" + out, inside = [], False + for line in text.splitlines(): + if line.startswith('== '): + if inside: + break + inside = line.startswith('== ' + header) + if inside: + out.append(re.sub(r' \[\d+s\]$', '', line)) + return out + + +def receipt(path): + value = json.loads(Path(path).read_bytes()) + for name in EXPECTED['nondeterministic_fields']: + value.pop(name) + return value + + +def compile_probe(cxx, includes, work): + source = work/'probe.cpp' + source.write_text(PROBE) + flags = [f for d in includes for f in ('-I', d)] + return run([cxx, '-std=c++17', *flags, '-fsyntax-only', source]) + + +def run_step4(script, wd, cxx, boost): + env = {k: v for k, v in os.environ.items() if k not in ('PYTHONOPTIMIZE', 'PYTHONPATH', 'PYTHONSAFEPATH')} + env.update(BOOST_INCLUDE=str(boost), PYTHON=sys.executable, CXX=cxx) + p = run(['bash', '-c', script, TUNE/'verify.sh', wd], env=env, timeout=600) + require(p.returncode == 0, 'Step 4 failed:\n' + p.stdout[-2000:] + p.stderr[-2000:]) + return p.stdout + + +def controls_from_checkers(wd): + """Rerun both mutation suites with full output; rejections must not come from the stand-in.""" + word, bins = TUNE/'bitE8mut-p111-tuned.json.gz', wd/'bin' + v, r = (json.loads(__import__('gzip').decompress(word.read_bytes()))[k] for k in ('v', 'R')) + py = [sys.executable, '-I', '-B'] + suites = [ + [*py, wd/'ad545/mutate_records.py', wd/'records', wd/'cin', wd/'cout', bins/'legality', + bins/'columns_k5', bins/'legality_ncopy_h'], + [*py, ROOT/'research/e8-bit-p111-tuned/code/mutrec_b.py', word, wd/'records', wd/'cin', wd/'cout', + bins/'legality', bins/f'columns_h_{v}_{r}', bins/'columns_k4', bins/'columns_k5'], + ] + lasts = [] + for argv in suites: + p = run(argv, timeout=600) + require(p.returncode == 0, 'Mutation suite failed: ' + p.stderr[-1000:]) + require(REFUSAL not in p.stdout + p.stderr, 'A negative control was rejected by the stand-in, not the checker') + lasts.append(p.stdout.strip().splitlines()[-1]) + return v, r, lasts + + +def overflow_control(wd, v, r): + """Every ADD coefficient set to +-2^30: the stand-in must refuse, never wrap.""" + raw = (wd/'cout/COHORT249-RECORDS.bin').read_bytes() + width = struct.calcsize('=6i') + require(len(raw) % width == 0, 'record stream alignment') + out, adds = bytearray(), 0 + for i in range(0, len(raw), width): + rec = list(struct.unpack('=6i', raw[i:i+width])) + if rec[0] == 1: + rec[3] = (1 << 30) if adds % 2 == 0 else -(1 << 30) + adds += 1 + out += struct.pack('=6i', *rec) + require(adds > 0, 'no ADD records found') + mutated = wd/'overflow-RECORDS.bin' + mutated.write_bytes(bytes(out)) + p = run([wd/'bin'/f'columns_h_{v}_{r}', mutated, wd/'overflow.json'], timeout=120) + require(p.returncode != 0 and REFUSAL in p.stderr and not (wd/'overflow.json').exists(), + 'Over-range control was not refused by the stand-in') + return adds + + +def differential(cxx, work, include): + source = work/'harness.cpp' + source.write_text(HARNESS) + binary = work/'harness' + p = run([cxx, '-O2', '-std=c++17', '-I', include, '-o', binary, source], timeout=300) + require(p.returncode == 0, 'Harness build failed: ' + p.stderr[-1000:]) + spec = EXPECTED['differential'] + rng, limit = random.Random(spec['seed']), 1 << 127 + def operand(): + bits = rng.choice([1, 8, 30, 62, 64, 100, 124]) # high word stays within long long + return divmod(rng.randrange(-(1 << bits), 1 << bits), 1 << 62) + def expect(op, a, b, bl): + """(expected output, or None for a required refusal; whether a refusal is within 2x of the bound).""" + if op == 'cmp': + return ''.join(str(int(x)) for x in (a < b, a > b, a == b, a <= b, a >= b, a != b, a != 0)), False + if op == 'msb': + return (str(a.bit_length() - 1) if a > 0 else None), False + if op in ('hex', 'shlneg'): + return None, False + if op == 'shlbig': + return ('0' if a == 0 else None), False + if op == 'shleq': + op = 'shl' + if op == 'shl' and a < 0: + return None, False + if op.endswith('8'): + a, b, op = 8*a, 8*b, op[:-1] + if not (-limit <= a < limit and (op == 'neg' or -limit <= b < limit)): + return None, max(abs(a), abs(b) if op != 'neg' else 0) < 2*limit + value = dict(add=a+b, sub=a-b, mul=a*b, imul=bl*a, neg=-a, shl=a << bl if op == 'shl' else 0)[op] + return (str(value), False) if -limit <= value < limit else (None, abs(value) < 2*limit) + inside, outside, near = [], {op: [] for op in OPS}, [] + def add(op, ah, al, bh, bl): + out, close = expect(op, ah*(1 << 62)+al, bh*(1 << 62)+bl, bl) + row = (op, ah, al, bh, bl, out) + if out is not None: + inside.append(row) + else: + outside[op].append(row) + if close: + near.append(row) + for _ in range(spec['operations']): + op = rng.choice(OPS) + (ah, al), (bh, bl) = operand(), operand() + if op in ('shl', 'shleq'): + bh, bl = 0, rng.randrange(0, 130) + if op == 'shlbig': + bh, bl = 0, rng.randrange(0, 64) + if op in ('imul', 'shlneg'): + bh, bl = 0, rng.randrange(0, 1 << 62) + add(op, ah, al, bh, bl) + # Fixed rows: values at -2^127 and 2^127 - 1, shifts that end exactly at or below 2^127, + # zero shifted far, multiples of 2^64 (zero low word) and pairs sharing a high word. + low, high = divmod(-(1 << 124), 1 << 62), divmod((1 << 124) - 1, 1 << 62) + w = (4, 0) # 2^64 + for op, (ah, al), (bh, bl) in [ + ('neg8', low, (0, 0)), ('add8', low, (0, 0)), ('add8', high, high), ('sub8', low, (0, 1)), + ('sub8', high, low), ('add8', low, low), ('sub8', (0, 0), low), ('msb', high, (0, 0)), + ('shl', (0, 0), (0, 129)), ('shleq', (0, 0), (0, 200)), ('shlbig', (0, 0), (0, 5)), + ('shlneg', (0, 0), (0, 0)), ('shl', (0, 1), (0, 126)), ('shleq', (0, 1), (0, 126)), + ('shl', (0, 1), (0, 127)), ('shleq', (0, 1), (0, 127)), ('shl', high, (0, 3)), ('shl', high, (0, 4)), + ('shleq', high, (0, 4)), ('shl', (1, 0), (0, 64)), ('shl', (1, 0), (0, 65)), + ('cmp', w, (0, 0)), ('cmp', (-4, 0), w), ('cmp', (4, 5), (4, 3)), ('cmp', (4, 3), (4, 5)), + ('cmp', (-4, 5), (-4, 3)), ('cmp', (1 << 42, 7), (1 << 42, 9)), ('cmp', (1 << 42, 9), (1 << 42, 7)), + ('add', w, (-4, 0)), ('sub', w, (8, 0)), ('add', (-8, 0), w), ('neg', w, (0, 0)), + ('msb', w, (0, 0)), ('mul', w, (1, 0)), ('mul', w, (2, 0)), ('mul', (-4, 0), (2, 0)), + ('imul', w, (0, 1 << 61))]: + add(op, ah, al, bh, bl) + feed = ''.join('%s %d %d %d %d\n' % row[:5] for row in inside) + p = run([binary], input=feed, timeout=300) + got = p.stdout.split('\n')[:len(inside)] + require(p.returncode == 0 and got == [row[5] for row in inside], 'Stand-in differs from Python integers') + # Every near-bound refusal runs, plus a per-operation sample so that every refusal path is exercised. + sample = list(dict.fromkeys(near + [row for op in OPS for row in outside[op][:spec['refusals_per_operation']]])) + require({row[0] for row in sample} == set(EXPECTED['refusing_operations']), 'Refusal coverage by operation') + counts = [len(inside), len(near), len(sample)] + require(counts == spec['counts'], 'Differential coverage changed: ' + str(counts)) + for row in sample: + q = run([binary], input='%s %d %d %d %d\n' % row[:5], timeout=30) + require(q.returncode != 0 and q.stdout == '', 'Out-of-range operation was not refused: ' + str(row[:5])) + return len(inside), len(near), len(sample) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--boost-include', type=Path, help='also compare against real Boost headers') + parser.add_argument('--include', type=Path, default=INCLUDE, help=argparse.SUPPRESS) # mutants.py only + args = parser.parse_args() + include = args.include.resolve() + require(sys.version_info >= (3, 11) and not sys.flags.optimize, 'Python 3.11+ with assertions') + require(' ' not in str(ROOT), 'The pinned verify.sh expands paths unquoted; use a checkout path without spaces') + pins = check_pins() + cxx = os.environ.get('CXX', 'c++') + require(shutil.which(cxx) and shutil.which('bash'), 'Needs bash and a C++17 compiler (CXX, default c++)') + version = run([cxx, '--version']).stdout.splitlines()[0] + script = step4_script() + expected_lines = section(BOOST_LOG.read_text(), '4.') + require(len(expected_lines) == EXPECTED['step4_log_lines'], 'Committed Boost log section') + with tempfile.TemporaryDirectory(prefix='bit589-standin-') as tmp: + work = Path(tmp) + require(' ' not in str(work), 'Temporary directory path contains spaces; set TMPDIR') + includes = [SKELETON/'portable-include', CHECKERS, include] + p = compile_probe(cxx, includes, work) + require(p.returncode == 0, 'Include order does not select the stand-in: ' + p.stderr[-500:]) + wd = work/'standin' + output = run_step4(script, wd, cxx, include) + require(section(output, '4.') == expected_lines, 'Step-4 output differs from the committed Boost log') + require(receipt(wd/'cout/columns_k4.json') == receipt(BOOST_K4), + 'columns_k4.json differs from the committed Boost receipt') + v, r, lasts = controls_from_checkers(wd) + require(lasts == EXPECTED['mutation_suite_last_lines'], 'Mutation suite summaries') + adds = overflow_control(wd, v, r) + checked, near, refused = differential(cxx, work, include) + compared = 'committed Boost receipts' + if args.boost_include: + p = compile_probe(cxx, [SKELETON/'portable-include', CHECKERS, args.boost_include], work) + require(p.returncode != 0, '--boost-include resolves to the stand-in, not real Boost') + boost = run_step4(script, work/'boost', cxx, args.boost_include.resolve()) + require(section(boost, '4.') == section(output, '4.'), 'Step-4 output differs from real Boost') + for name in sorted(p.name for p in (wd/'cout').glob('*.json')): + require(receipt(work/'boost/cout'/name) == receipt(wd/'cout'/name) + if name.startswith('columns') else + json.loads((work/'boost/cout'/name).read_bytes()) == json.loads((wd/'cout'/name).read_bytes()), + 'Receipt differs from real Boost: ' + name) + compared += ' and a real-Boost rerun' + print('compiler:', version) + print('pinned inputs:', pins, '| step-4 log lines equal:', len(expected_lines), '| columns_k4.json equal') + print('mutation suites:', ' / '.join(lasts)) + print('over-range control refused with', adds, 'ADD coefficients at +-2^30') + print('differential: %d in-range operations equal to Python integers, %d refusals (%d within 2x of the bound)' + % (checked, refused, near)) + print('PASS_BIT589_STEP4_CHECKED_CPP_INT_STANDIN', 'matches', compared) + + +if __name__ == '__main__': + main()