From dfc135cdad7c5e1e3c3ac2665216763e514d915b Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 17 Oct 2026 15:40:09 -0700 Subject: [PATCH] Compose integer labels with checked XOR scatter export --- .../integer-xor-scatter-boundary/README.md | 142 ++++++++++++++++ .../integer-xor-scatter-boundary/RESULT.json | 23 +++ .../integer-xor-scatter-boundary/SOURCES.json | 54 ++++++ .../integer-xor-scatter-boundary/TASK.json | 96 +++++++++++ .../integer-xor-scatter-boundary/VERIFY.json | 38 +++++ .../integer-xor-scatter-boundary/boundary.py | 109 ++++++++++++ .../test_boundary.py | 158 ++++++++++++++++++ .../integer-xor-scatter-boundary/verify.py | 147 ++++++++++++++++ 8 files changed, 767 insertions(+) create mode 100644 research/integer-xor-scatter-boundary/README.md create mode 100644 research/integer-xor-scatter-boundary/RESULT.json create mode 100644 research/integer-xor-scatter-boundary/SOURCES.json create mode 100644 research/integer-xor-scatter-boundary/TASK.json create mode 100644 research/integer-xor-scatter-boundary/VERIFY.json create mode 100644 research/integer-xor-scatter-boundary/boundary.py create mode 100644 research/integer-xor-scatter-boundary/test_boundary.py create mode 100644 research/integer-xor-scatter-boundary/verify.py diff --git a/research/integer-xor-scatter-boundary/README.md b/research/integer-xor-scatter-boundary/README.md new file mode 100644 index 0000000000..c5c7e757fa --- /dev/null +++ b/research/integer-xor-scatter-boundary/README.md @@ -0,0 +1,142 @@ +# Integer labels and XOR scatter in one checked export boundary + +`integer-xor-scatter-v1` composes PR3100's exact-integer label contract with +PR3273's XOR-normalized scatter contract. It retains PR3273's full symbolic +check before export. The result is an opt-in compiler-boundary repair; it does +not change the selected supplier, frozen inputs, or kappa. + +## Why this composition is needed + +The two underlying defects and their individual repairs are already known: + +- Nick Petalas's PR2977 found that the checker counts scatter occurrences + modulo two while the exporter tests membership. PR3273 supplies the + `xor-scatter-v1` normalizer and checks its complete exported action. +- `josecarlosgonzalezroque-design`'s PR3100 found that the checker applies + `int(a)` to label coordinates while export uses `Fraction(a)`. Its + `integer-labels-v1` guard is composed with PR2992's simple-support wrapper. + +PR3273's actual public boundary still admits PR3100's fractional-label witness: +changing `lines[0][1]` from `0` to `0.5`. Its symbolic checker sees the original +integer geometry, while export retains the fractional coordinate. Correct +scatter counts do not detect the resulting geometry discrepancy. + +This package supplies the missing composition. It accepts occurrence-based +scatter lists, including cancelling pairs, while rejecting noninteger label +encodings before delegation. The contribution is the combined executable +boundary and its integration controls, not discovery of either underlying bug +or a new normalization theorem. + +## Contract and implementation + +The schema is `integer-xor-scatter-v1`. It first applies PR3100's authenticated +`validate_labels`: `v` is a positive exact integer, `h` is the integer 8, +`lines` has exactly `v` rows of length `h`, and every coordinate has Python type +`int` after JSON parsing. Booleans, strings, fractional numbers, and integral +float spellings such as `0.0` are rejected. Integer typing does not establish +E8 geometry; the full symbolic check is still required. + +It then delegates to PR3273's authenticated XOR boundary on a private snapshot. +That boundary reduces each row's slot occurrences modulo two, runs the full +symbolic checker on the normalized word, and exports only after that check +succeeds. COPY/ERASE pairs remain charged per retained slot; aliases and unused +slots are not coalesced. Its emitted invoice checks are retained. + +The input is limited to 5 MiB both compressed and decompressed. The inherited +XOR limits remain: at most 256 targets, 4096 helpers, 256 retained slots, +4096 occurrences per row and 65536 occurrences overall. These are implementation +limits, not mathematical bounds. The API is +`execute(mode, input_path, output=None, root=ROOT, scratch=None)`. + +The wrapper authenticates and privately snapshots the predecessor validator, +XOR implementation, its source manifest, and the delegated checker/exporter. +The original input is parsed and label-validated before its snapshot is passed +to XOR validation. Thus replacing the original input after validation does not +substitute unchecked label data. Publication inherits the XOR wrapper's +existing-output protection; copying the output files is not a filesystem +transaction. A copy failure can leave a partial new output directory. + +For exact integer coordinates, `int(a)` and `Fraction(a)` represent the same +number. For each retained slot with occurrence multiplicity `m`, the scatter +normal form preserves its characteristic-two coefficient `m mod 2`. These two +predecessor arguments concern different fields and apply to the same private +word snapshot. The full symbolic checker remains responsible for the other +word, frame, endpoint and interface conditions. No claim of complete JSON +schema validation follows from these two contracts. + +## Reproduction and evidence + +From the repository root: + +```sh +python3 -B scripts/research_package.py --package research/integer-xor-scatter-boundary +python3 -I -O -Werror -B research/integer-xor-scatter-boundary/verify.py +``` + +Use the new entry point with a candidate word: + +```sh +python3 -I -B research/integer-xor-scatter-boundary/boundary.py check WORD.json.gz +python3 -I -B research/integer-xor-scatter-boundary/boundary.py export WORD.json.gz OUTDIR --scratch SESSION_SCRATCH_DIRECTORY +``` + +`OUTDIR` must be new. Check mode takes no output path. The receipt reports +`schema`, `input_occurrences`, `normalized_scatter_terms`, and, in export mode, +the inherited exported invoice. + +[RESULT.json](RESULT.json) records the finite regression results. The controls +exercise the public wrapper, exact selected export preservation, cancelling +scatter pairs, the fractional-label counterexample, and their combination. +An integer-coordinate mutation that violates the original geometry must still +be rejected by the full checker, demonstrating that the type guard does not +replace semantic validation. The registered commands and their bounded input +closure are in [VERIFY.json](VERIFY.json); exact predecessor pins are in +[SOURCES.json](SOURCES.json). + +The scientific base is canonical main +`6f100d4bb5083f8c35d96fd95a539811c62f245c`, priority revision 6. This package +performs no fresh native C++/Boost replay, nonempty kernel/owner/donor admission, +complete selected cold replay, physical hosting or routing, precision/tape +transfer, all-size multiplication proof, or supplier-moment improvement. +PR3326's native results belong to that predecessor and are not silently +extended to this successor. Hosted CI and independent human review are separate +from local automated verification. + +## Overlap and attribution + +The initial `find_overlap.py` refresh had no usable remote snapshot because +`gh` was unavailable. Public GitHub API searches subsequently covered all +results for XOR scatter (11), scatter/nonempty (6), scatter/donor (23), and +PR3100 references (4), supplemented by local sources and visible portions of +Discussions 2976 and 1166. No matching combined integer-label/XOR repair was +identified. Further API searches hit the unauthenticated rate limit. This +coverage excludes arbitrary diffs, hidden discussion comments and unpublished +work; absence of a match is not proof of novelty. [TASK.json](TASK.json) +records the scope refinement and coverage. + +Credit Nick Petalas for PR2977's mismatch and PR3273's XOR boundary; +`josecarlosgonzalezroque-design` for PR3100's label witness and validator; +Arham Tahir for PR2992's simple-scatter boundary; and kumagi/Hiroki Kumazaki for +PR3004's parity/count analysis. Retained-alias qualifications and native +controls belong to Sricharan Vagvala/PR3128, James Reed/PR3099, and +commdata2338/PR3210/PR3222. PR3326 supplies the separate genuine-Boost native +composition audit. eumemic and DreamingOfClouds retain the selected word/checker +lineage, Dylan Crow the exporter/native lineage, and Douglas Colkitt the +selected bindings and review framework. All source notices remain unchanged. + +Prepared for Pierre with substantial OpenAI Codex assistance in research, +implementation, automated adversarial review, verification and writing. +Apache-2.0; no independent human or proof-assistant review claimed. + +## Open next questions + +1. Should a named successor supplier adopt this combined boundary? First bind + its producer and consumer to the schema and source manifest, then replay its + complete symbolic/export/native chain. Maintainer adoption and the complete + supplier obligations remain open; this opt-in wrapper changes no production + caller. +2. Does it compose with a named nonempty kernel, owner and donor construction? + PR3326's native fixture uses empty ownership inputs. First pin a concrete + fixture and enumerate live-source, ownership, bank and complete invoice + obligations. Fresh native and physical accounting are required before + claiming that stronger result. diff --git a/research/integer-xor-scatter-boundary/RESULT.json b/research/integer-xor-scatter-boundary/RESULT.json new file mode 100644 index 0000000000..d5c78710d4 --- /dev/null +++ b/research/integer-xor-scatter-boundary/RESULT.json @@ -0,0 +1,23 @@ +{ + "accepted": { + "baseline": { + "249-records.bin": "ea44aa047a2a9cf034de8b56b2166c49cf2dbdf9cd9ed6a20299a4f2b05ef09a", + "249-states.json": "701ca802279b23ba23ffd502935ae9f34a33fc81fdfd266042f3208a2cb16265", + "frames.json": "d006b3bfe0c04afecfea7aecfa59dfe4298b99501c70da3938e40289bc2f5010" + }, + "paired": { + "249-records.bin": "ea44aa047a2a9cf034de8b56b2166c49cf2dbdf9cd9ed6a20299a4f2b05ef09a", + "249-states.json": "701ca802279b23ba23ffd502935ae9f34a33fc81fdfd266042f3208a2cb16265", + "frames.json": "d006b3bfe0c04afecfea7aecfa59dfe4298b99501c70da3938e40289bc2f5010" + } + }, + "predecessor_fractional_exports": 2, + "rejected": { + "fractional": true, + "fractional_paired": true, + "integer_wrong_geometry": true, + "wrong_endpoint": true + }, + "selected_artifacts_unchanged": true, + "status": "PASS_INTEGER_XOR_SCATTER_BOUNDARY" +} diff --git a/research/integer-xor-scatter-boundary/SOURCES.json b/research/integer-xor-scatter-boundary/SOURCES.json new file mode 100644 index 0000000000..3c7b779738 --- /dev/null +++ b/research/integer-xor-scatter-boundary/SOURCES.json @@ -0,0 +1,54 @@ +{ + "schema_version": 1, + "base_commit": "6f100d4bb5083f8c35d96fd95a539811c62f245c", + "files": [ + { + "path": "research/label-coordinate-boundary-audit/boundary.py", + "usage": "input", + "git_blob": "bf0c5558144efe436c213bb058a7b0b5a2cf5481", + "sha256": "d6079e0495c7f6f4badb13676035dc127b0b230548b54658e37b05a9a33804e2" + }, + { + "path": "research/xor-scatter-boundary/boundary.py", + "usage": "input", + "git_blob": "5972753580aab6314c4c9cadd2da746029f1e5a2", + "sha256": "65438fa2ac81094127165883fa131e9604939ca7503143b00f993d433dadd8cc" + }, + { + "path": "research/xor-scatter-boundary/SOURCES.json", + "usage": "input", + "git_blob": "57cd2b64b810e335f8803bc2bbb4743bc6f057bc", + "sha256": "0a92c54d977d18d5f6bc35068b3584d58c0598b63a627155faebd23bcf6da570" + }, + { + "path": "research/e8-bit-p111-tuned/code/xcheck_p.py", + "usage": "input", + "git_blob": "184f5dcf94a486cc7db91ca4db5865916bf26009", + "sha256": "af3decc877e27c00001cf56f50c6c09566e8e101297ab34c7860aed96a06d80e" + }, + { + "path": "research/e8-bit-carrier-skeleton/code/bit/export_records.py", + "usage": "input", + "git_blob": "3632d370486a02f5456f7643ced03f8c925b2350", + "sha256": "8a5999601f917dbb492bb9ae9a344611f18cc7cedc53dc3c76496a369e530eb3" + }, + { + "path": "research/e8-bit-p111-tuned/tune/bitE8mut-p111-tuned.json.gz", + "usage": "input", + "git_blob": "85a4499bf9e4911a705d50567a2c2ef6cf23739a", + "sha256": "e3e301e62bc2b41dbf1ceac0acf3d35824ebeab1848c5a2142d4577b109da52f" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "67f085acd86acd7d4e03922d9cf4b26bca66fed5", + "sha256": "47b42433f5a85809a3aadd29c2c339da2c6ed728e4f7762df6b88bccb7874cf0" + }, + { + "path": "certificates/selected-result.json", + "usage": "provenance", + "git_blob": "b1fea0c94436b236cc2b7d41a7be032811b94c81", + "sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + } + ] +} diff --git a/research/integer-xor-scatter-boundary/TASK.json b/research/integer-xor-scatter-boundary/TASK.json new file mode 100644 index 0000000000..b1d05bbfac --- /dev/null +++ b/research/integer-xor-scatter-boundary/TASK.json @@ -0,0 +1,96 @@ +{ + "schema_version": 1, + "task_id": "supplier-boundary-audit", + "priority_revision": 6, + "base_commit": "6f100d4bb5083f8c35d96fd95a539811c62f245c", + "original_card": { + "id": "supplier-boundary-audit", + "question": "Does one selected supplier obligation follow from its actual source-bound checks?", + "starting_points": [ + "certificates/selected-result.json", + "research/owner-boundary-audit/README.md", + "research/scatter-export-parity-audit/README.md" + ], + "methods": [ + "Trace one hypothesis through the source, checker and complete caller.", + "Construct one semantically invalid but superficially well-formed fixture and test rejection." + ], + "acceptance_gate": "Pin the current sources and one obligation; the historical owner audit is an example, not evidence of a current bug.", + "first_step": { + "command": [ + "python3", + "-B", + "scripts/research_baseline.py" + ], + "expected": "PASS_RESEARCH_BASELINE_BINDINGS_ONLY", + "inspect": "Read #2977 reported duplicate-scatter counterexample and its opt-in repair. The archive is not a production fix or an independently validated selected-result failure; the selected generator emits unique rows.", + "scope": "Source-binding orientation only; does not replay or validate the reported bug." + }, + "first_experiment": "First check whether the scatter repair has been adopted. If still open, independently reproduce #2977 symbolic-versus-exported parity on the tiny duplicate fixture, compare the selected unique-row positive control, and define either enforced uniqueness or consistent multiplicity semantics. A bounded guard-adoption patch must preserve valid inputs and test the actual exported action. Stop after this obligation; no full historical replay or frontier change follows from the mutant alone.", + "bottleneck_link": "Closing a current acceptance gap or proof obligation strengthens reliability even without changing kappa.", + "allowed_variations": [ + "Audit one named current condition using a new semantic counterexample." + ], + "scope_change_triggers": [ + "Reporting the historical timing-only diagnostic as a new current acceptance bug.", + "Calling a malformed mutant a failure of the selected construction, or importing an opt-in repair without testing the exporter boundary." + ], + "overlap_terms": [ + "owner boundary", + "bridge omission", + "hosting", + "2977", + "scatter export", + "duplicate index", + "parity semantics" + ] + }, + "scope_status": "refined", + "actual_scope": "Compose PR3100 integer-labels-v1 with PR3273 XOR occurrence normalization and full symbolic checking before export, retaining exact selected artifacts and rejecting mixed fractional-label/duplicate-scatter inputs.", + "scope_change_reason": "PR2977 duplicate semantics and PR3100 fractional-label coercion are already known, and PR3273 plus PR3326 already supply normalization and native replay. The missing composition allows the XOR boundary to accept the known fractional-label fixture; this separately versioned wrapper combines both existing contracts instead of rediscovering either defect.", + "bottleneck_link": "A checker/exporter boundary must agree on geometry as well as scalar action. Composing the existing guards closes a concrete reliability gap without a supplier or kappa claim.", + "budget_minutes": 60, + "stop_rule": "Stop after a source-bound compositional wrapper, public-entry regression controls, exact export comparisons, and a focused contribution. No native ownership or construction search expansion.", + "outcome": "completed", + "evidence": "README.md, RESULT.json and the bounded verifier and behavioral controls registered in VERIFY.json. Validation claims are limited to the retained receipt and executed commands.", + "author": "Pierre, with OpenAI Codex assistance", + "ai_assistance": "Substantial OpenAI Codex assistance in source inspection, implementation, automated adversarial review, verification and writing. No independent human or proof-assistant review claimed.", + "selection": { + "method": "User-assigned exporter repair, refined after checking existing work; no random reroll.", + "nearest_predecessors": [ + 2977, + 2992, + 3100, + 3273, + 3326 + ], + "overlap": "find_overlap.py --task supplier-boundary-audit --refresh --include-closed had no usable remote snapshot because gh was unavailable. Public GitHub API searches then covered all results for is:pr \"xor-scatter\" (11), scatter/nonempty (6), scatter/donor (23), and 3100 (4), with local README/source inspection and visible Discussion2976 and Discussion1166 content. No matching integer-label/XOR composition repair was identified. Further searches hit the unauthenticated API rate limit. Search metadata and visible discussion portions do not cover all diffs, hidden comments or unpublished work; this is not a novelty certificate.", + "public_claim": "None; bounded contribution, no advisory reservation." + }, + "open_questions": [ + { + "question": "Should a named successor supplier adopt the composed integer-label/XOR boundary?", + "why_it_matters": "This opt-in entry point fixes the combined representation gap, but production callers and their manifests are not changed.", + "first_step": "Bind a named producer and consumer to this contract, review the source pins, and replay the complete symbolic/export/native path.", + "blocked_by": "Maintainer adoption and complete supplier/native obligations; no adoption follows from this finite audit." + }, + { + "question": "Does the composed boundary preserve one named nonempty kernel, owner and donor construction?", + "why_it_matters": "PR3326 native composition uses empty ownership inputs and does not supply physical admission for this successor.", + "first_step": "Pin the concrete consumer fixture, enumerate ownership and live-source requirements, and run native controls with its complete invoice.", + "blocked_by": "A concrete nonempty fixture, separate bank and physical accounting, and fresh native replay." + } + ], + "validation": { + "registered_replay": "PASS under CPython 3.12.1 on macOS", + "direct_optimized": "PASS under CPython 3.12.1; matching receipt produced with CPython 3.11.6", + "behavioral_test_groups": 5, + "hosted_ci": "Not yet run; reported checks local only" + }, + "freshness": { + "upstream_commit": "6f100d4bb5083f8c35d96fd95a539811c62f245c", + "checked_at_utc": "2026-10-17T22:40:09.618856+00:00", + "priority_revision": 6, + "relevant_changes": "Canonical main unchanged on pre-submission fetch; live overlap coverage limitations recorded above." + } +} diff --git a/research/integer-xor-scatter-boundary/VERIFY.json b/research/integer-xor-scatter-boundary/VERIFY.json new file mode 100644 index 0000000000..b30692f30b --- /dev/null +++ b/research/integer-xor-scatter-boundary/VERIFY.json @@ -0,0 +1,38 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "research/label-coordinate-boundary-audit/boundary.py", + "research/xor-scatter-boundary/boundary.py", + "research/xor-scatter-boundary/SOURCES.json", + "research/e8-bit-p111-tuned/code/xcheck_p.py", + "research/e8-bit-carrier-skeleton/code/bit/export_records.py", + "research/e8-bit-p111-tuned/tune/bitE8mut-p111-tuned.json.gz" + ], + "commands": [ + { + "argv": [ + "{python}", + "-I", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 90, + "checks": "Actual predecessor discrepancy, composed acceptance and rejection, exact selected export artifact equality." + }, + { + "argv": [ + "{python}", + "-I", + "-O", + "-B", + "{package}/test_boundary.py", + "-v" + ], + "timeout_seconds": 120, + "checks": "Malformed schema, delegate corruption, input snapshot replacement, output preservation and optimized CLI checks." + } + ] +} diff --git a/research/integer-xor-scatter-boundary/boundary.py b/research/integer-xor-scatter-boundary/boundary.py new file mode 100644 index 0000000000..cd4407ba34 --- /dev/null +++ b/research/integer-xor-scatter-boundary/boundary.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Compose integer-labels-v1 and xor-scatter-v1 before checking and export. + +Apache-2.0. Prepared with substantial OpenAI Codex assistance. Label validation +is delegated unchanged to PR3100; XOR normalization and checked export retain +Nick Petalas's PR3273 implementation and its predecessor attribution. +""" + +import argparse +import gzip +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import tempfile + +PACKAGE = Path(__file__).resolve().parent +ROOT = PACKAGE.parents[1] +SCHEMA = "integer-xor-scatter-v1" +MAX_BYTES = 5 * 1024 * 1024 +LABELS = "research/label-coordinate-boundary-audit/boundary.py" +XOR = "research/xor-scatter-boundary/boundary.py" +XOR_PINS = "research/xor-scatter-boundary/SOURCES.json" +CHECKER = "research/e8-bit-p111-tuned/code/xcheck_p.py" +EXPORTER = "research/e8-bit-carrier-skeleton/code/bit/export_records.py" +DEPENDENCIES = (LABELS, XOR, XOR_PINS, CHECKER, EXPORTER) + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def read_bounded(path, compressed=False): + require(path.stat().st_size <= MAX_BYTES, "input exceeds 5 MiB") + opener = gzip.open if compressed else open + with opener(path, "rb") as stream: + body = stream.read(MAX_BYTES + 1) + require(len(body) <= MAX_BYTES, "input exceeds 5 MiB") + return body + + +def sources(root): + """Read and authenticate all delegate bytes before executing any of them.""" + pins = json.loads(read_bounded(PACKAGE / "SOURCES.json"))["files"] + require(type(pins) is list and len(pins) <= 16, "bounded source inventory") + result = {} + for path in DEPENDENCIES: + rows = [row for row in pins if row["path"] == path and row["usage"] == "input"] + require(len(rows) == 1, "unique source pin: " + path) + body = read_bounded(root / path) + require( + hashlib.sha256(body).hexdigest() == rows[0]["sha256"], + "source pin mismatch: " + path, + ) + result[path] = body + return result + + +def module(body, path): + scope = {"__name__": "authenticated_boundary", "__file__": str(path)} + exec(compile(body, str(path), "exec", optimize=0), scope) # noqa: S102 + return scope + + +def execute(mode, input_path, output=None, root=ROOT, scratch=None): + require(mode in ("check", "export"), "unknown mode") + require((mode == "export") == (output is not None), "export needs output; check takes none") + if output is not None: + require(not output.exists() and not output.is_symlink(), "output already exists") + raw = read_bounded(input_path, input_path.suffix == ".gz") + word = json.loads(raw) + bodies = sources(root) + labels = module(bodies[LABELS], root / LABELS) + labels["validate_labels"](word) + with tempfile.TemporaryDirectory(prefix="integer-xor-", dir=scratch) as temporary: + directory = Path(temporary) + private_root = directory / "sources" + for path, body in bodies.items(): + target = private_root / path + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(body) + snapshot = directory / "word.json.gz" + snapshot.write_bytes(gzip.compress(json.dumps(word).encode(), mtime=0)) + xor = module(bodies[XOR], private_root / XOR) + result = xor["execute"](mode, snapshot, output, root=private_root, scratch=directory) + require(result["schema"] == "xor-scatter-v1", "delegate schema") + return {**result, "schema": SCHEMA} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("mode", choices=("check", "export")) + parser.add_argument("word", type=Path) + parser.add_argument("output", type=Path, nargs="?") + parser.add_argument("--scratch", type=Path) + args = parser.parse_args() + try: + result = execute(args.mode, args.word, args.output, scratch=args.scratch) + except (ValueError, OSError, EOFError, RecursionError, subprocess.TimeoutExpired) as error: + print(f"REJECT {SCHEMA}: {error}", file=sys.stderr) + return 2 + print(json.dumps(result, sort_keys=True)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/research/integer-xor-scatter-boundary/test_boundary.py b/research/integer-xor-scatter-boundary/test_boundary.py new file mode 100644 index 0000000000..36bb0aa96d --- /dev/null +++ b/research/integer-xor-scatter-boundary/test_boundary.py @@ -0,0 +1,158 @@ +"""Public-entry controls for the composed boundary. Apache-2.0.""" + +import copy +import json +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import boundary # noqa: E402 +import verify # noqa: E402 + + +class BoundaryTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.word, _ = verify.sources(boundary.ROOT) + + def test_label_types_and_shapes_reject_in_both_modes(self): + variants = [] + for value in (0.5, 0.0, True, "0", None): + word = verify.fixture(self.word, paired=True) + word["lines"][0][1] = value + variants.append(word) + for field, value in (("h", 8.0), ("v", True), ("lines", [])): + word = copy.deepcopy(self.word) + word[field] = value + variants.append(word) + word = copy.deepcopy(self.word) + word["lines"][0].append(0) + variants.append(word) + with tempfile.TemporaryDirectory() as temporary: + directory = Path(temporary) + path = directory / "word.json" + for index, word in enumerate(variants): + path.write_text(json.dumps(word), encoding="utf-8") + for mode in ("check", "export"): + output = ( + directory / (str(index) + mode) if mode == "export" else None + ) + with self.subTest(index=index, mode=mode), self.assertRaises( + ValueError + ): + boundary.execute(mode, path, output, scratch=directory) + if output is not None: + self.assertFalse(output.exists()) + + def test_existing_output_and_input_unchanged(self): + with tempfile.TemporaryDirectory() as temporary: + directory = Path(temporary) + path, output = directory / "word.json", directory / "existing" + path.write_text(json.dumps(self.word), encoding="utf-8") + before = path.read_bytes() + output.mkdir() + (output / "sentinel").write_bytes(b"preserve") + with self.assertRaises(ValueError): + boundary.execute("export", path, output, scratch=directory) + self.assertEqual(path.read_bytes(), before) + self.assertEqual(list(output.iterdir()), [output / "sentinel"]) + self.assertEqual((output / "sentinel").read_bytes(), b"preserve") + + def test_public_cli_optimized_check_and_rejection(self): + with tempfile.TemporaryDirectory() as temporary: + directory = Path(temporary) + for bad in (False, True): + word = verify.fixture(self.word, fractional=bad, paired=True) + path = directory / "word.json" + path.write_text(json.dumps(word), encoding="utf-8") + run = subprocess.run( + [ + sys.executable, + "-I", + "-O", + "-Werror", + "-B", + str(verify.PACKAGE / "boundary.py"), + "check", + str(path), + ], + check=False, + capture_output=True, + text=True, + timeout=60, + ) + with self.subTest(fractional=bad): + self.assertEqual(run.returncode == 0, not bad, run.stderr) + if not bad: + result = json.loads(run.stdout) + self.assertEqual(result["schema"], "integer-xor-scatter-v1") + self.assertEqual(result["input_occurrences"], 529) + self.assertEqual(result["normalized_scatter_terms"], 527) + + def test_each_delegate_corruption_rejects_before_publication(self): + bodies = boundary.sources(boundary.ROOT) + with tempfile.TemporaryDirectory() as temporary: + directory = Path(temporary) + path = directory / "word.json" + path.write_text(json.dumps(self.word), encoding="utf-8") + for name, body in bodies.items(): + target = directory / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(body) + for name, body in bodies.items(): + target = directory / name + target.write_bytes(body + b" ") + with self.subTest(source=name), self.assertRaisesRegex( + ValueError, "source pin" + ): + boundary.execute( + "export", path, directory / "output", directory, directory + ) + self.assertFalse((directory / "output").exists()) + target.write_bytes(body) + + def test_input_replacement_after_validation_uses_same_snapshot(self): + real_module = boundary.module + with tempfile.TemporaryDirectory() as temporary: + directory = Path(temporary) + path = directory / "word.json" + path.write_text( + json.dumps(verify.fixture(self.word, paired=True)), encoding="utf-8" + ) + + def replace_after_validation(body, module_path): + scope = real_module(body, module_path) + if str(module_path).endswith(boundary.LABELS): + validate = scope["validate_labels"] + + def validate_and_replace(word): + validate(word) + path.write_text( + json.dumps(verify.fixture(self.word, fractional=True)), + encoding="utf-8", + ) + + scope["validate_labels"] = validate_and_replace + return scope + + with mock.patch.object( + boundary, "module", side_effect=replace_after_validation + ): + result = boundary.execute( + "export", path, directory / "output", scratch=directory + ) + self.assertEqual(result["input_occurrences"], 529) + self.assertEqual(result["normalized_scatter_terms"], 527) + expected = json.loads((verify.PACKAGE / "RESULT.json").read_bytes()) + self.assertEqual( + verify.artifacts(directory / "output"), expected["accepted"]["baseline"] + ) + self.assertEqual(json.loads(path.read_bytes())["lines"][0][1], 0.5) + + +if __name__ == "__main__": + unittest.main() diff --git a/research/integer-xor-scatter-boundary/verify.py b/research/integer-xor-scatter-boundary/verify.py new file mode 100644 index 0000000000..de7e3a40ba --- /dev/null +++ b/research/integer-xor-scatter-boundary/verify.py @@ -0,0 +1,147 @@ +"""Composed integer-label and XOR boundary evidence. Apache-2.0.""" + +import argparse +import copy +import gzip +import hashlib +import json +import sys +import tempfile +import types +from pathlib import Path + +PACKAGE = Path(__file__).resolve().parent +sys.path.insert(0, str(PACKAGE)) +import boundary # noqa: E402 + +WORD = "research/e8-bit-p111-tuned/tune/bitE8mut-p111-tuned.json.gz" +XOR = "research/xor-scatter-boundary/boundary.py" +ARTIFACTS = ("249-records.bin", "249-states.json", "frames.json") + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def sources(root): + pins = json.loads((PACKAGE / "SOURCES.json").read_bytes())["files"] + for row in pins: + if row["usage"] == "input": + body = (root / row["path"]).read_bytes() + require(hashlib.sha256(body).hexdigest() == row["sha256"], "input pin") + word = json.loads(gzip.decompress((root / WORD).read_bytes())) + module = types.ModuleType("authenticated_xor_predecessor") + module.__file__ = str(root / XOR) + exec(compile((root / XOR).read_bytes(), XOR, "exec", optimize=0), module.__dict__) + return word, module + + +def fixture(word, fractional=False, paired=False): + result = copy.deepcopy(word) + if fractional: + result["lines"][0][1] = 0.5 + if paired: + result["scatter"][0].extend([0, 0]) + return result + + +def artifacts(path): + return { + name: hashlib.sha256((path / name).read_bytes()).hexdigest() + for name in ARTIFACTS + } + + +def reject(word, name, directory, root): + path, output = directory / (name + ".json"), directory / name + path.write_text(json.dumps(word), encoding="utf-8") + before = path.read_bytes() + try: + boundary.execute("export", path, output, root, directory) + except ValueError: + pass + else: + raise ValueError("invalid word accepted: " + name) + require(not output.exists(), "rejection published output") + require(path.read_bytes() == before, "rejection changed input") + return True + + +def audit(root, directory): + word, old = sources(root) + require(word["lines"][0] == [2, 0, 0, 0, 0, 0, 0, 0], "label fixture") + require(word["scatter"][0] == [4, 5, 6, 7, 8], "scatter fixture") + accepted, rejected = {}, {} + for name, paired in (("baseline", False), ("paired", True)): + value = fixture(word, paired=paired) + path, output = directory / (name + ".json"), directory / name + path.write_text(json.dumps(value), encoding="utf-8") + before = path.read_bytes() + boundary.execute("export", path, output, root, directory) + require(path.read_bytes() == before, "export changed input") + accepted[name] = artifacts(output) + require(accepted["baseline"] == accepted["paired"], "paired repair byte identity") + baseline_path = directory / "baseline.json" + old.execute("export", baseline_path, directory / "old-baseline", root, directory) + require( + artifacts(directory / "old-baseline") == accepted["baseline"], + "predecessor baseline byte identity", + ) + old.delegate( + old.scripts(root)[old.EXPORTER], + baseline_path, + directory, + "raw-exporter.py", + directory / "raw-baseline", + ) + require( + artifacts(directory / "raw-baseline") == accepted["baseline"], + "raw selected artifact byte identity", + ) + for paired in (False, True): + name = "fractional_paired" if paired else "fractional" + value = fixture(word, fractional=True, paired=paired) + rejected[name] = reject(value, name, directory, root) + old_output = directory / ("old-" + name) + old.execute("export", directory / (name + ".json"), old_output, root, directory) + require( + artifacts(old_output)["frames.json"] != accepted["baseline"]["frames.json"], + "known predecessor geometry discrepancy not reproduced", + ) + bad = fixture(word, paired=True) + bad["lines"][0][1] = 1 + rejected["integer_wrong_geometry"] = reject( + bad, "integer_wrong_geometry", directory, root + ) + bad = fixture(word) + bad["scatter"][0].append(0) + rejected["wrong_endpoint"] = reject(bad, "wrong_endpoint", directory, root) + return { + "status": "PASS_INTEGER_XOR_SCATTER_BOUNDARY", + "accepted": accepted, + "rejected": rejected, + "predecessor_fractional_exports": 2, + "selected_artifacts_unchanged": True, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--emit", action="store_true") + args = parser.parse_args() + with tempfile.TemporaryDirectory(prefix="integer-xor-audit-") as temporary: + result = audit(boundary.ROOT, Path(temporary)) + if args.emit: + print(json.dumps(result, indent=2, sort_keys=True)) + else: + expected = json.loads((PACKAGE / "RESULT.json").read_bytes()) + require( + json.dumps(result, sort_keys=True) == json.dumps(expected, sort_keys=True), + "typed deterministic evidence differs", + ) + print(result["status"], "no_new_kappa") + + +if __name__ == "__main__": + main()