From d8029ee05dc8ff5b367f1299a5d63d2d7d82edda Mon Sep 17 00:00:00 2001 From: Wentao Li Date: Wed, 14 Oct 2026 16:39:35 +0000 Subject: [PATCH 1/3] Tooling: faster ExclusionCircuit.verify leave-two-out supports Replace the historical expected-support genexpr with an equivalent closed-form pair mask so n=45 verify receipts and circuit_sha256 stay identical. Update only the scripts/exclusion_circuit.py source pin on the four binding certificates. Prepared by Wentao Li with substantial AI assistance. --- certificates/disjoint-tensor-compression.json | 2 +- certificates/dyadic-central-factor.json | 2 +- certificates/positive-side-core.json | 2 +- certificates/ternary-side.json | 2 +- research/exclusion-support-masks/NOTICE.md | 19 ++ research/exclusion-support-masks/README.md | 104 +++++++++ research/exclusion-support-masks/RESULT.json | 50 +++++ research/exclusion-support-masks/SOURCES.json | 72 ++++++ research/exclusion-support-masks/TASK.json | 70 ++++++ research/exclusion-support-masks/VERIFY.json | 57 +++++ research/exclusion-support-masks/baseline.py | 184 ++++++++++++++++ research/exclusion-support-masks/benchmark.py | 137 ++++++++++++ research/exclusion-support-masks/verify.py | 207 ++++++++++++++++++ scripts/exclusion_circuit.py | 10 +- tests/test_exclusion_support_masks.py | 124 +++++++++++ 15 files changed, 1036 insertions(+), 6 deletions(-) create mode 100644 research/exclusion-support-masks/NOTICE.md create mode 100644 research/exclusion-support-masks/README.md create mode 100644 research/exclusion-support-masks/RESULT.json create mode 100644 research/exclusion-support-masks/SOURCES.json create mode 100644 research/exclusion-support-masks/TASK.json create mode 100644 research/exclusion-support-masks/VERIFY.json create mode 100644 research/exclusion-support-masks/baseline.py create mode 100644 research/exclusion-support-masks/benchmark.py create mode 100644 research/exclusion-support-masks/verify.py create mode 100644 tests/test_exclusion_support_masks.py diff --git a/certificates/disjoint-tensor-compression.json b/certificates/disjoint-tensor-compression.json index 323a46ad8f..ee54bca6ef 100644 --- a/certificates/disjoint-tensor-compression.json +++ b/certificates/disjoint-tensor-compression.json @@ -341,7 +341,7 @@ "scripts/audit_disjoint_tensor.py": "477db540e972b82751fac6a3a6d6e38fb4141e967261b1f9c260e2c686479a78", "scripts/binary_phase_frames.py": "4148cfa313cf050ed25296e8db36d35871520b8318fd086c7d1facf0c6af67d7", "scripts/complex_compression.py": "9b6f758a5985ba5d9fdd726fe6556b3d3b4bd9d98aaf8ba06a7bb58093c4c504", - "scripts/exclusion_circuit.py": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", + "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8" }, "status": "IMPROVED FINITE COMPLEX NETWORK; NO NEW MULTIPLICATION KAPPA", diff --git a/certificates/dyadic-central-factor.json b/certificates/dyadic-central-factor.json index a85cb8a395..e3cdb778c9 100644 --- a/certificates/dyadic-central-factor.json +++ b/certificates/dyadic-central-factor.json @@ -101,7 +101,7 @@ "scripts/audit_complex_centers.py": "166b18f560cd0d520d23fa29ee8147e0ea056358c4268a466ec458195a97a960", "scripts/binary_phase_frames.py": "4148cfa313cf050ed25296e8db36d35871520b8318fd086c7d1facf0c6af67d7", "scripts/complex_compression.py": "9b6f758a5985ba5d9fdd726fe6556b3d3b4bd9d98aaf8ba06a7bb58093c4c504", - "scripts/exclusion_circuit.py": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", + "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", "scripts/experiments/complex_centers.py": "3744396587d729253d80980f6310b56e7bf0ea21b165bd178d0d6d4c49faf2e2", "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", "scripts/general_network_guard.py": "790a2064449afdbffd6dbd4adbcf4a28eaf41931ce7134e4a4e8766f3764270c" diff --git a/certificates/positive-side-core.json b/certificates/positive-side-core.json index b3698be4e8..f3a14a500c 100644 --- a/certificates/positive-side-core.json +++ b/certificates/positive-side-core.json @@ -23101,7 +23101,7 @@ "source_sha256": { "docs/research/positive-side-core.md": "fab9ff0ed8de56cb9c4c1587b65f956f706ab8f3b8afcb9212366218e227f49d", "scripts/audit_positive_side.py": "b021aad944d8a6c023e57b1b48937854803bbb370240c93e0fe359d831c838c9", - "scripts/exclusion_circuit.py": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", + "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", "scripts/experiments/positive_side_core.py": "0d453ab7e869d2cf6a43d6865de82583edb390d3c75038271f0fc09e0f3828ba", "scripts/experiments/rank_product_core.py": "7a91c1188c5d42e864a5d2e42397b09c435b526ae4f4210c44bf615de488708e", "scripts/experiments/shared_core.py": "d3c59b61229956cb54ae1ef9145ed213abdf1ae743929de145a77c43656f01a8", diff --git a/certificates/ternary-side.json b/certificates/ternary-side.json index 2c5c9f6ea8..8449e7b08c 100644 --- a/certificates/ternary-side.json +++ b/certificates/ternary-side.json @@ -1563,7 +1563,7 @@ "notes/ternary-five-subsets.tex": "f8553a7ad7da9fcd85def67360f7c070ec6c15911c3310fca360d821e1ed1ccd", "scripts/audit_ternary_side.py": "5e86566eee32031e1829d26ea17a7372e4802e2f7d76a2caa3ae16de3ab072c1", "scripts/certify.py": "2348d95008276c5d5764f7e028c3918e484582cd589bd4d9b82919161745e53e", - "scripts/exclusion_circuit.py": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", + "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", "scripts/experiments/prime_core.py": "080a89a2c91505b509068698542843c9d7af3cdec01b5c88972107dc9c688c62", "scripts/experiments/rank_product_core.py": "7a91c1188c5d42e864a5d2e42397b09c435b526ae4f4210c44bf615de488708e", diff --git a/research/exclusion-support-masks/NOTICE.md b/research/exclusion-support-masks/NOTICE.md new file mode 100644 index 0000000000..0747fbcec1 --- /dev/null +++ b/research/exclusion-support-masks/NOTICE.md @@ -0,0 +1,19 @@ +# Notice + +This package accelerates the historical leave-two-out expected-support check in +`scripts/exclusion_circuit.py:ExclusionCircuit.verify` while preserving support +assertions and `circuit_sha256`. + +Predecessor credit: + +- Douglas R. Colkitt — exclusion circuits, shared-point circuits, bit-compression + screens and the historical certificates. +- Closest tooling priors on *different* objects: CrocSwap/integer-mult-bounds + PRs #756 (balanced-Fano packed replay), #1793 (inplace-side prefix), #1861 + (parity-side basis/contains), #1922 (joint-frame cleared product), #1984 + (mixed-point scalar digests), #1994 (shared-point verify digests; explicit + open question this package answers), and #1874 (hosted-audit digest path). + +Prepared by Wentao Li with substantial AI assistance under Apache-2.0. +This is not independent human peer review and is not a formal proof of the +multiplication theorem. diff --git a/research/exclusion-support-masks/README.md b/research/exclusion-support-masks/README.md new file mode 100644 index 0000000000..e8e686a73d --- /dev/null +++ b/research/exclusion-support-masks/README.md @@ -0,0 +1,104 @@ +# Faster exact exclusion leave-two-out supports with identical receipts + +**Tooling improvement; no new kappa.** At base +`0b1cccf86e2f5132ed286a26ef0a681a495740ea`, `ExclusionCircuit.verify` checks each +leave-two-out output support with + +`sum(1<=4 + self.n=n + self.inputs=list(combinations(range(n),2)) + self.support=[0]+[1< dict: + env = dict(os.environ) + env.pop("PYTHONOPTIMIZE", None) + env["PYTHONDONTWRITEBYTECODE"] = "1" + proc = subprocess.run( + [sys.executable, "-I", "-B", "-c", WORKER, str(ROOT), mode], + cwd=ROOT, + env=env, + capture_output=True, + text=True, + check=False, + ) + require(proc.returncode == 0, f"worker {mode} failed: {proc.stderr[-2000:]}") + line = proc.stdout.strip().splitlines()[-1] + return json.loads(line) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--rounds", type=int, default=5) + parser.add_argument("--out", type=Path, default=PACKAGE / "RESULT.json") + args = parser.parse_args() + require(args.rounds >= 1, "rounds") + + samples = [] + for i in range(args.rounds): + order = ("optimized", "baseline") if i % 2 == 0 else ("baseline", "optimized") + pair = {} + for mode in order: + pair[mode] = run_worker(mode) + require( + pair["baseline"]["record"] == pair["optimized"]["record"], + f"receipt mismatch on round {i}", + ) + samples.append( + dict( + round=i, + baseline_seconds=pair["baseline"]["seconds"], + optimized_seconds=pair["optimized"]["seconds"], + ratio=pair["baseline"]["seconds"] / pair["optimized"]["seconds"], + ) + ) + print( + f"round {i}: baseline={pair['baseline']['seconds']:.3f}s " + f"optimized={pair['optimized']['seconds']:.3f}s " + f"ratio={samples[-1]['ratio']:.3f}", + flush=True, + ) + + base_med = statistics.median(s["baseline_seconds"] for s in samples) + opt_med = statistics.median(s["optimized_seconds"] for s in samples) + result = dict( + status="PASS_IDENTICAL_EXCLUSION_SUPPORT_MASK_RECEIPTS", + fixture="ExclusionCircuit(45).verify only (construction excluded)", + rounds=args.rounds, + median_baseline_seconds=base_med, + median_optimized_seconds=opt_med, + median_ratio=base_med / opt_med, + samples=samples, + environment=dict( + python=sys.version, + platform=platform.platform(), + executable=sys.executable, + ), + source_sha256=dict( + baseline=sha256((PACKAGE / "baseline.py").read_bytes()).hexdigest(), + production=sha256((ROOT / "scripts/exclusion_circuit.py").read_bytes()).hexdigest(), + ), + no_new_kappa=True, + ) + args.out.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n") + print( + "PASS_IDENTICAL_EXCLUSION_SUPPORT_MASK_RECEIPTS", + f"median {base_med:.3f} -> {opt_med:.3f} s ({base_med / opt_med:.2f}x)", + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/research/exclusion-support-masks/verify.py b/research/exclusion-support-masks/verify.py new file mode 100644 index 0000000000..0d6c385408 --- /dev/null +++ b/research/exclusion-support-masks/verify.py @@ -0,0 +1,207 @@ +#!/usr/bin/env python3 +"""Equivalence, pin and mutation controls for exclusion leave-two-out supports. + +Read-only by default. Regenerates no certificate. Timing is not a pass gate; +see benchmark.py / RESULT.json. +""" +from __future__ import annotations + +import argparse +from copy import deepcopy +from hashlib import sha256 +import importlib.util +import json +from pathlib import Path +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +SCRIPTS = ROOT / "scripts" +sys.path.insert(0, str(SCRIPTS)) + +from certify import require # noqa: E402 +import exclusion_circuit as production # noqa: E402 + +BASE_COMMIT = "0b1cccf86e2f5132ed286a26ef0a681a495740ea" +ORIGINAL_BLOB = "aaa99986c4b81c5d4fbe016fd216a4818940bb95" +ORIGINAL_SHA256 = "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0" +OPTIMIZED_SHA256 = "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63" + +PINNED_CERTIFICATES = ( + "certificates/positive-side-core.json", + "certificates/dyadic-central-factor.json", + "certificates/ternary-side.json", + "certificates/disjoint-tensor-compression.json", +) + + +def load_module(path: Path, name: str): + spec = importlib.util.spec_from_file_location(name, path) + require(spec is not None and spec.loader is not None, f"missing module {path}") + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + +def digest_file(path: Path) -> str: + return sha256(path.read_bytes()).hexdigest() + + +def git_show(path: str) -> bytes: + return subprocess.check_output(["git", "show", f"{BASE_COMMIT}:{path}"], cwd=ROOT) + + +def check_baseline_bytes() -> None: + baseline = PACKAGE / "baseline.py" + original = git_show("scripts/exclusion_circuit.py") + require(sha256(original).hexdigest() == ORIGINAL_SHA256, "base exclusion_circuit.py sha256 drift") + blob = ( + subprocess.check_output( + ["git", "rev-parse", f"{BASE_COMMIT}:scripts/exclusion_circuit.py"], cwd=ROOT + ) + .decode() + .strip() + ) + require(blob == ORIGINAL_BLOB, "base exclusion_circuit.py blob drift") + require(baseline.read_bytes() == original, "baseline.py must be an exact copy of the base commit source") + require( + digest_file(SCRIPTS / "exclusion_circuit.py") == OPTIMIZED_SHA256, + "production exclusion_circuit.py does not match the claimed optimized digest", + ) + + +def historical_expected(inputs, a, b) -> int: + return sum(1 << i for i, p in enumerate(inputs) if a not in p and b not in p) + + +def closed_form_expected(full: int, vertex: list[int], a: int, b: int) -> int: + return full ^ (vertex[a] | vertex[b]) + + +def check_mask_equivalence() -> dict: + """Closed-form leave-two-out masks must match the historical genexpr.""" + cases = 0 + for n in (4, 6, 8, 12, 16, 24, 32, 45): + circuit = production.ExclusionCircuit(n) + full = (1 << len(circuit.inputs)) - 1 + vertex = [0] * circuit.n + for i, (p, q) in enumerate(circuit.inputs): + bit = 1 << i + vertex[p] |= bit + vertex[q] |= bit + for (a, b), node in circuit.outputs.items(): + old = historical_expected(circuit.inputs, a, b) + new = closed_form_expected(full, vertex, a, b) + require(old == new, f"mask mismatch n={n} exclude=({a},{b})") + require(circuit.support[node] == new, f"live support mismatch n={n} exclude=({a},{b})") + cases += 1 + # Mutant foil: XOR without the double-count fix keeps the (a,b) bit. + a, b = next(iter(circuit.outputs)) + mutant = full ^ vertex[a] ^ vertex[b] + correct = closed_form_expected(full, vertex, a, b) + require(mutant != correct, f"xor-mutant must differ on n={n}") + return {"mask_cases": cases, "xor_mutant": "rejected"} + + +def check_verify_equivalence(baseline, optimized) -> dict: + records = [] + for n in (4, 6, 8, 12, 16, 24, 32, 45): + circuit = optimized.ExclusionCircuit(n) + old = baseline.ExclusionCircuit.verify(circuit) + new = circuit.verify() + require(old == new, f"verify mismatch on n={n}") + records.append( + dict( + n=n, + inputs=new["inputs"], + outputs=new["outputs"], + additions=new["additions"], + scratch_roles=new["scratch_roles"], + nonzero_coefficients=new["nonzero_coefficients"], + circuit_sha256=new["circuit_sha256"], + ) + ) + require(records[-1]["additions"] == 11566, "unexpected n=45 additions") + require(records[-1]["outputs"] == 990, "unexpected n=45 outputs") + exact = "3b1a4d7731db2950040552672fbc56ebd172e36e3412129030ff2aec472d5f6c" + require(records[-1]["circuit_sha256"] == exact, "unexpected n=45 circuit_sha256") + return {"verify_cases": len(records), "records": records, "n45_circuit_sha256": exact} + + +def check_support_rejection(baseline, optimized) -> dict: + circuit = optimized.ExclusionCircuit(8) + target, node = next(iter(circuit.outputs.items())) + # Flip one expected support bit; both engines must reject. + circuit.support[node] ^= 1 + with _expect_assertion(): + optimized.ExclusionCircuit.verify(circuit) + with _expect_assertion(): + baseline.ExclusionCircuit.verify(circuit) + return {"support_rejection": "preserved", "corrupted_output": list(target)} + + +class _expect_assertion: + def __enter__(self): + return self + + def __exit__(self, exc_type, exc, tb): + require(exc_type is AssertionError, "expected AssertionError from corrupted support") + return True + + +def check_certificate_pin_discipline() -> dict: + """Working certificates may change only the exclusion_circuit.py source pin.""" + reports = {} + for rel in PINNED_CERTIFICATES: + base = json.loads(git_show(rel).decode()) + live = json.loads((ROOT / rel).read_text(encoding="utf-8")) + base_pin = base["source_sha256"]["scripts/exclusion_circuit.py"] + live_pin = live["source_sha256"]["scripts/exclusion_circuit.py"] + require(base_pin == ORIGINAL_SHA256, f"{rel}: unexpected base pin") + require(live_pin == OPTIMIZED_SHA256, f"{rel}: optimized pin missing") + restored = deepcopy(live) + restored["source_sha256"]["scripts/exclusion_circuit.py"] = ORIGINAL_SHA256 + require(restored == base, f"{rel}: changed fields beyond the exclusion_circuit pin") + reports[rel] = { + "base_pin": base_pin, + "live_pin": live_pin, + "only_pin_changed": True, + } + return reports + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--write-certificate", action="store_true", help="unused; kept for API parity") + args = parser.parse_args() + require(not args.write_certificate, "this package does not rewrite certificate payloads") + + check_baseline_bytes() + baseline = load_module(PACKAGE / "baseline.py", "exclusion_support_masks_baseline") + masks = check_mask_equivalence() + verifies = check_verify_equivalence(baseline, production) + if __debug__: + rejection = check_support_rejection(baseline, production) + else: + # Historical ExclusionCircuit.verify uses assert, stripped under -O. + rejection = {"support_rejection": "skipped_under_-O"} + pins = check_certificate_pin_discipline() + print( + "PASS_EXCLUSION_SUPPORT_MASKS", + f"verify_cases={verifies['verify_cases']}", + f"mask_cases={masks['mask_cases']}", + f"certs={len(pins)}", + "no_new_kappa", + ) + print( + "controls", + rejection["support_rejection"], + f"n45_sha={verifies['n45_circuit_sha256'][:16]}", + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/scripts/exclusion_circuit.py b/scripts/exclusion_circuit.py index aaa99986c4..5b0b4ce69b 100644 --- a/scripts/exclusion_circuit.py +++ b/scripts/exclusion_circuit.py @@ -98,9 +98,15 @@ def verify(self): assert a Date: Wed, 14 Oct 2026 17:01:30 +0000 Subject: [PATCH 2/3] Tooling: fold ExclusionCircuit.verify_embedding reverse masks into #2001 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the reverse-frame closed form beside the leave-two-out verify masks in the same production file, re-pin the four binding certificates once, and register research/exclusion-embedding-masks/ with identical receipts (5.46×). Prepared by Wentao Li with substantial AI assistance. --- certificates/disjoint-tensor-compression.json | 2 +- certificates/dyadic-central-factor.json | 2 +- certificates/positive-side-core.json | 2 +- certificates/ternary-side.json | 2 +- research/exclusion-embedding-masks/NOTICE.md | 21 ++ research/exclusion-embedding-masks/README.md | 117 ++++++++ .../exclusion-embedding-masks/RESULT.json | 50 ++++ .../exclusion-embedding-masks/SOURCES.json | 72 +++++ research/exclusion-embedding-masks/TASK.json | 70 +++++ .../exclusion-embedding-masks/VERIFY.json | 57 ++++ .../exclusion-embedding-masks/baseline.py | 184 ++++++++++++ .../exclusion-embedding-masks/benchmark.py | 138 +++++++++ research/exclusion-embedding-masks/verify.py | 277 ++++++++++++++++++ research/exclusion-support-masks/README.md | 14 +- research/exclusion-support-masks/RESULT.json | 2 +- research/exclusion-support-masks/verify.py | 2 +- scripts/exclusion_circuit.py | 14 +- tests/test_exclusion_embedding_masks.py | 136 +++++++++ tests/test_exclusion_support_masks.py | 2 +- 19 files changed, 1147 insertions(+), 17 deletions(-) create mode 100644 research/exclusion-embedding-masks/NOTICE.md create mode 100644 research/exclusion-embedding-masks/README.md create mode 100644 research/exclusion-embedding-masks/RESULT.json create mode 100644 research/exclusion-embedding-masks/SOURCES.json create mode 100644 research/exclusion-embedding-masks/TASK.json create mode 100644 research/exclusion-embedding-masks/VERIFY.json create mode 100644 research/exclusion-embedding-masks/baseline.py create mode 100644 research/exclusion-embedding-masks/benchmark.py create mode 100644 research/exclusion-embedding-masks/verify.py create mode 100644 tests/test_exclusion_embedding_masks.py diff --git a/certificates/disjoint-tensor-compression.json b/certificates/disjoint-tensor-compression.json index ee54bca6ef..b70256ae2d 100644 --- a/certificates/disjoint-tensor-compression.json +++ b/certificates/disjoint-tensor-compression.json @@ -341,7 +341,7 @@ "scripts/audit_disjoint_tensor.py": "477db540e972b82751fac6a3a6d6e38fb4141e967261b1f9c260e2c686479a78", "scripts/binary_phase_frames.py": "4148cfa313cf050ed25296e8db36d35871520b8318fd086c7d1facf0c6af67d7", "scripts/complex_compression.py": "9b6f758a5985ba5d9fdd726fe6556b3d3b4bd9d98aaf8ba06a7bb58093c4c504", - "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", + "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8" }, "status": "IMPROVED FINITE COMPLEX NETWORK; NO NEW MULTIPLICATION KAPPA", diff --git a/certificates/dyadic-central-factor.json b/certificates/dyadic-central-factor.json index e3cdb778c9..f0244556ce 100644 --- a/certificates/dyadic-central-factor.json +++ b/certificates/dyadic-central-factor.json @@ -101,7 +101,7 @@ "scripts/audit_complex_centers.py": "166b18f560cd0d520d23fa29ee8147e0ea056358c4268a466ec458195a97a960", "scripts/binary_phase_frames.py": "4148cfa313cf050ed25296e8db36d35871520b8318fd086c7d1facf0c6af67d7", "scripts/complex_compression.py": "9b6f758a5985ba5d9fdd726fe6556b3d3b4bd9d98aaf8ba06a7bb58093c4c504", - "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", + "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", "scripts/experiments/complex_centers.py": "3744396587d729253d80980f6310b56e7bf0ea21b165bd178d0d6d4c49faf2e2", "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", "scripts/general_network_guard.py": "790a2064449afdbffd6dbd4adbcf4a28eaf41931ce7134e4a4e8766f3764270c" diff --git a/certificates/positive-side-core.json b/certificates/positive-side-core.json index f3a14a500c..843dd248eb 100644 --- a/certificates/positive-side-core.json +++ b/certificates/positive-side-core.json @@ -23101,7 +23101,7 @@ "source_sha256": { "docs/research/positive-side-core.md": "fab9ff0ed8de56cb9c4c1587b65f956f706ab8f3b8afcb9212366218e227f49d", "scripts/audit_positive_side.py": "b021aad944d8a6c023e57b1b48937854803bbb370240c93e0fe359d831c838c9", - "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", + "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", "scripts/experiments/positive_side_core.py": "0d453ab7e869d2cf6a43d6865de82583edb390d3c75038271f0fc09e0f3828ba", "scripts/experiments/rank_product_core.py": "7a91c1188c5d42e864a5d2e42397b09c435b526ae4f4210c44bf615de488708e", "scripts/experiments/shared_core.py": "d3c59b61229956cb54ae1ef9145ed213abdf1ae743929de145a77c43656f01a8", diff --git a/certificates/ternary-side.json b/certificates/ternary-side.json index 8449e7b08c..b582f4a141 100644 --- a/certificates/ternary-side.json +++ b/certificates/ternary-side.json @@ -1563,7 +1563,7 @@ "notes/ternary-five-subsets.tex": "f8553a7ad7da9fcd85def67360f7c070ec6c15911c3310fca360d821e1ed1ccd", "scripts/audit_ternary_side.py": "5e86566eee32031e1829d26ea17a7372e4802e2f7d76a2caa3ae16de3ab072c1", "scripts/certify.py": "2348d95008276c5d5764f7e028c3918e484582cd589bd4d9b82919161745e53e", - "scripts/exclusion_circuit.py": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63", + "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", "scripts/experiments/prime_core.py": "080a89a2c91505b509068698542843c9d7af3cdec01b5c88972107dc9c688c62", "scripts/experiments/rank_product_core.py": "7a91c1188c5d42e864a5d2e42397b09c435b526ae4f4210c44bf615de488708e", diff --git a/research/exclusion-embedding-masks/NOTICE.md b/research/exclusion-embedding-masks/NOTICE.md new file mode 100644 index 0000000000..6058855a3d --- /dev/null +++ b/research/exclusion-embedding-masks/NOTICE.md @@ -0,0 +1,21 @@ +# Notice + +This package accelerates the historical reverse-frame expected-support check in +`scripts/exclusion_circuit.py:ExclusionCircuit.verify_embedding` while preserving +forward/reverse nesting assertions and embedding receipts. It deliberately leaves +Landed together with the leave-two-out closed form from open CrocSwap/integer-mult-bounds#2001 so both rewrite the same production file once. + +Predecessor credit: + +- Douglas R. Colkitt — exclusion circuits, shared-point circuits, bit-compression + screens and the historical certificates. +- Closest tooling priors on *different* objects: CrocSwap/integer-mult-bounds + PRs #756 (balanced-Fano packed replay), #1793 (inplace-side prefix), #1861 + (parity-side basis/contains), #1922 (joint-frame cleared product), #1984 + (mixed-point scalar digests), #1994 (shared-point verify digests), #2001 + (ExclusionCircuit.verify leave-two-out masks; explicit open question this + package answers), and #1874 (hosted-audit digest path). + +Prepared by Wentao Li with substantial AI assistance under Apache-2.0. +This is not independent human peer review and is not a formal proof of the +multiplication theorem. diff --git a/research/exclusion-embedding-masks/README.md b/research/exclusion-embedding-masks/README.md new file mode 100644 index 0000000000..d076387569 --- /dev/null +++ b/research/exclusion-embedding-masks/README.md @@ -0,0 +1,117 @@ +# Faster exact exclusion embedding reverse masks with identical receipts + +**Tooling improvement; no new kappa.** At base +`0b1cccf86e2f5132ed286a26ef0a681a495740ea`, `ExclusionCircuit.verify_embedding` +checks each reverse-frame source expected support with + +`sum(1<=4 + self.n=n + self.inputs=list(combinations(range(n),2)) + self.support=[0]+[1< dict: + env = dict(os.environ) + env.pop("PYTHONOPTIMIZE", None) + env["PYTHONDONTWRITEBYTECODE"] = "1" + proc = subprocess.run( + [sys.executable, "-I", "-B", "-c", WORKER, str(ROOT), mode], + cwd=ROOT, + env=env, + capture_output=True, + text=True, + check=False, + ) + require(proc.returncode == 0, f"worker {mode} failed: {proc.stderr[-2000:]}") + line = proc.stdout.strip().splitlines()[-1] + return json.loads(line) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--rounds", type=int, default=5) + parser.add_argument("--out", type=Path, default=PACKAGE / "RESULT.json") + args = parser.parse_args() + require(args.rounds >= 1, "rounds") + + samples = [] + for i in range(args.rounds): + order = ("optimized", "baseline") if i % 2 == 0 else ("baseline", "optimized") + pair = {} + for mode in order: + pair[mode] = run_worker(mode) + require( + pair["baseline"]["record"] == pair["optimized"]["record"], + f"receipt mismatch on round {i}", + ) + samples.append( + dict( + round=i, + baseline_seconds=pair["baseline"]["seconds"], + optimized_seconds=pair["optimized"]["seconds"], + ratio=pair["baseline"]["seconds"] / pair["optimized"]["seconds"], + ) + ) + print( + f"round {i}: baseline={pair['baseline']['seconds']:.3f}s " + f"optimized={pair['optimized']['seconds']:.3f}s " + f"ratio={samples[-1]['ratio']:.3f}", + flush=True, + ) + + base_med = statistics.median(s["baseline_seconds"] for s in samples) + opt_med = statistics.median(s["optimized_seconds"] for s in samples) + result = dict( + status="PASS_IDENTICAL_EXCLUSION_EMBEDDING_MASK_RECEIPTS", + fixture="ExclusionCircuit(45).verify_embedding only (construction excluded)", + rounds=args.rounds, + median_baseline_seconds=base_med, + median_optimized_seconds=opt_med, + median_ratio=base_med / opt_med, + samples=samples, + environment=dict( + python=sys.version, + platform=platform.platform(), + executable=sys.executable, + ), + source_sha256=dict( + baseline=sha256((PACKAGE / "baseline.py").read_bytes()).hexdigest(), + production=sha256((ROOT / "scripts/exclusion_circuit.py").read_bytes()).hexdigest(), + ), + no_new_kappa=True, + ) + args.out.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n") + print( + "PASS_IDENTICAL_EXCLUSION_EMBEDDING_MASK_RECEIPTS", + f"median {base_med:.3f} -> {opt_med:.3f} s ({base_med / opt_med:.2f}x)", + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/research/exclusion-embedding-masks/verify.py b/research/exclusion-embedding-masks/verify.py new file mode 100644 index 0000000000..4bfb3f5ef1 --- /dev/null +++ b/research/exclusion-embedding-masks/verify.py @@ -0,0 +1,277 @@ +#!/usr/bin/env python3 +"""Equivalence, pin and mutation controls for exclusion embedding reverse masks. + +Read-only by default. Regenerates no certificate. Timing is not a pass gate; +see benchmark.py / RESULT.json. +""" +from __future__ import annotations + +import argparse +from copy import deepcopy +from hashlib import sha256 +import importlib.util +import json +from pathlib import Path +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +SCRIPTS = ROOT / "scripts" +sys.path.insert(0, str(SCRIPTS)) + +from certify import require # noqa: E402 +import exclusion_circuit as production # noqa: E402 + +BASE_COMMIT = "0b1cccf86e2f5132ed286a26ef0a681a495740ea" +ORIGINAL_BLOB = "aaa99986c4b81c5d4fbe016fd216a4818940bb95" +ORIGINAL_SHA256 = "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0" +OPTIMIZED_SHA256 = "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3" +N45_VERIFY_SHA = "3b1a4d7731db2950040552672fbc56ebd172e36e3412129030ff2aec472d5f6c" + +PINNED_CERTIFICATES = ( + "certificates/positive-side-core.json", + "certificates/dyadic-central-factor.json", + "certificates/ternary-side.json", + "certificates/disjoint-tensor-compression.json", +) + + +def load_module(path: Path, name: str): + spec = importlib.util.spec_from_file_location(name, path) + require(spec is not None and spec.loader is not None, f"missing module {path}") + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + +def digest_file(path: Path) -> str: + return sha256(path.read_bytes()).hexdigest() + + +def git_show(path: str) -> bytes: + return subprocess.check_output(["git", "show", f"{BASE_COMMIT}:{path}"], cwd=ROOT) + + +def check_baseline_bytes() -> None: + baseline = PACKAGE / "baseline.py" + original = git_show("scripts/exclusion_circuit.py") + require(sha256(original).hexdigest() == ORIGINAL_SHA256, "base exclusion_circuit.py sha256 drift") + blob = ( + subprocess.check_output( + ["git", "rev-parse", f"{BASE_COMMIT}:scripts/exclusion_circuit.py"], cwd=ROOT + ) + .decode() + .strip() + ) + require(blob == ORIGINAL_BLOB, "base exclusion_circuit.py blob drift") + require(baseline.read_bytes() == original, "baseline.py must be an exact copy of the base commit source") + require( + digest_file(SCRIPTS / "exclusion_circuit.py") == OPTIMIZED_SHA256, + "production exclusion_circuit.py does not match the claimed optimized digest", + ) + + +def historical_expected(target_index, source) -> int: + return sum( + 1 << i + for target, i in target_index.items() + if not set(source) & set(target) + ) + + +def closed_form_expected(full: int, vertex: list[int], a: int, b: int) -> int: + return full ^ (vertex[a] | vertex[b]) + + +def reverse_masks(circuit) -> tuple[int, list[int], dict]: + target_index = {pair: i for i, pair in enumerate(sorted(circuit.outputs))} + full = (1 << len(target_index)) - 1 + vertex = [0] * circuit.n + for target, i in target_index.items(): + bit = 1 << i + vertex[target[0]] |= bit + vertex[target[1]] |= bit + return full, vertex, target_index + + +def check_mask_equivalence() -> dict: + """Closed-form reverse-frame masks must match the historical genexpr.""" + cases = 0 + for n in (4, 6, 8, 12, 16, 24, 32, 45): + circuit = production.ExclusionCircuit(n) + code = circuit.compile() + full, vertex, target_index = reverse_masks(circuit) + for source, _slot in code["sources"].items(): + old = historical_expected(target_index, source) + new = closed_form_expected(full, vertex, source[0], source[1]) + require(old == new, f"mask mismatch n={n} source={source}") + cases += 1 + # Mutant foil: XOR without the double-count fix keeps the (a,b) bit. + source = next(iter(code["sources"])) + a, b = source + mutant = full ^ vertex[a] ^ vertex[b] + correct = closed_form_expected(full, vertex, a, b) + require(mutant != correct, f"xor-mutant must differ on n={n}") + return {"mask_cases": cases, "xor_mutant": "rejected"} + + +def check_embedding_equivalence(baseline, optimized) -> dict: + records = [] + for n in (4, 6, 8, 12, 16, 24, 32, 45): + circuit = optimized.ExclusionCircuit(n) + old = baseline.ExclusionCircuit.verify_embedding(circuit) + new = circuit.verify_embedding() + require(old == new, f"verify_embedding mismatch on n={n}") + records.append( + dict( + n=n, + reversible_roles=new["reversible_roles"], + forward_coefficient_map_exact=new["forward_coefficient_map_exact"], + forward_support_frames_nested=new["forward_support_frames_nested"], + reverse_support_frames_nested=new["reverse_support_frames_nested"], + no_forbidden_source_target_path=new["no_forbidden_source_target_path"], + ) + ) + require(records[-1]["reversible_roles"] == 12556, "unexpected n=45 reversible_roles") + return {"embedding_cases": len(records), "records": records} + + +def check_verify_untouched(baseline, optimized) -> dict: + """Leave-two-out verify receipts stay identical; both closed forms co-land.""" + circuit = optimized.ExclusionCircuit(45) + old = baseline.ExclusionCircuit.verify(circuit) + new = circuit.verify() + require(old == new, "verify receipt drifted") + require(new["circuit_sha256"] == N45_VERIFY_SHA, "unexpected n=45 circuit_sha256") + # Folded production with #2001: both leave-two-out and reverse-frame closed + # forms live in scripts/exclusion_circuit.py with one source pin. + text = (SCRIPTS / "exclusion_circuit.py").read_text(encoding="utf-8") + require( + "assert self.support[node]==full^(vertex[a]|vertex[b])" in text, + "verify leave-two-out must use the #2001 closed-form pair mask", + ) + require( + "assert reverse_frames[slot]==full^(vertex[a]|vertex[b])" in text, + "verify_embedding must use the closed-form reverse mask", + ) + require( + "sum(1< dict: + circuit = optimized.ExclusionCircuit(8) + code = circuit.compile() + source, slot = next(iter(code["sources"].items())) + # Corrupt one reverse-frame bit after a normal run's expected value by + # monkey-patching compile to flip the source slot after construction. + # Simpler: flip a descendant bit used by reverse frames by corrupting an + # output support index used only inside verify_embedding's reverse phase. + # Direct approach: wrap verify_embedding body via corrupted outputs map. + original_outputs = dict(circuit.outputs) + # Flip one output node id mapping so reverse expected bits disagree. + target = next(iter(circuit.outputs)) + circuit.outputs[target] = circuit.outputs[target] # keep structure + # Corrupt reverse_frames by mutating support of an output node after compile + # path: change target_index ordering indirectly by renaming is hard. + # Instead corrupt the compiled source slot's reverse expectation by + # temporarily replacing compile to yield a flipped output bit assignment. + real_compile = optimized.ExclusionCircuit.compile + + def compile_flip(self): + code = real_compile(self) + # Flip which output bit is written into one reverse_frames seed. + pairs = list(code["outputs"].items()) + require(len(pairs) >= 2, "need two outputs to flip") + (p0, s0), (p1, s1) = pairs[0], pairs[1] + code = dict(code) + outs = dict(code["outputs"]) + outs[p0], outs[p1] = s1, s0 + code["outputs"] = outs + return code + + optimized.ExclusionCircuit.compile = compile_flip + try: + with _expect_assertion(): + optimized.ExclusionCircuit.verify_embedding(circuit) + with _expect_assertion(): + baseline.ExclusionCircuit.verify_embedding(circuit) + finally: + optimized.ExclusionCircuit.compile = real_compile + circuit.outputs = original_outputs + return {"reverse_rejection": "preserved", "corrupted_source": list(source), "slot": slot} + + +class _expect_assertion: + def __enter__(self): + return self + + def __exit__(self, exc_type, exc, tb): + require(exc_type is AssertionError, "expected AssertionError from corrupted reverse frame") + return True + + +def check_certificate_pin_discipline() -> dict: + """Working certificates may change only the exclusion_circuit.py source pin.""" + reports = {} + for rel in PINNED_CERTIFICATES: + base = json.loads(git_show(rel).decode()) + live = json.loads((ROOT / rel).read_text(encoding="utf-8")) + base_pin = base["source_sha256"]["scripts/exclusion_circuit.py"] + live_pin = live["source_sha256"]["scripts/exclusion_circuit.py"] + require(base_pin == ORIGINAL_SHA256, f"{rel}: unexpected base pin") + require(live_pin == OPTIMIZED_SHA256, f"{rel}: optimized pin missing") + restored = deepcopy(live) + restored["source_sha256"]["scripts/exclusion_circuit.py"] = ORIGINAL_SHA256 + require(restored == base, f"{rel}: changed fields beyond the exclusion_circuit pin") + reports[rel] = { + "base_pin": base_pin, + "live_pin": live_pin, + "only_pin_changed": True, + } + return reports + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--write-certificate", action="store_true", help="unused; kept for API parity") + args = parser.parse_args() + require(not args.write_certificate, "this package does not rewrite certificate payloads") + + check_baseline_bytes() + baseline = load_module(PACKAGE / "baseline.py", "exclusion_embedding_masks_baseline") + masks = check_mask_equivalence() + embeddings = check_embedding_equivalence(baseline, production) + verify_guard = check_verify_untouched(baseline, production) + if __debug__: + rejection = check_reverse_rejection(baseline, production) + else: + # Historical ExclusionCircuit.verify_embedding uses assert, stripped under -O. + rejection = {"reverse_rejection": "skipped_under_-O"} + pins = check_certificate_pin_discipline() + print( + "PASS_EXCLUSION_EMBEDDING_MASKS", + f"embedding_cases={embeddings['embedding_cases']}", + f"mask_cases={masks['mask_cases']}", + f"certs={len(pins)}", + "no_new_kappa", + ) + print( + "controls", + rejection["reverse_rejection"], + f"verify_companion={verify_guard['verify_companion']}", + f"n45_roles={embeddings['records'][-1]['reversible_roles']}", + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/research/exclusion-support-masks/README.md b/research/exclusion-support-masks/README.md index e8e686a73d..f5a4a0e28d 100644 --- a/research/exclusion-support-masks/README.md +++ b/research/exclusion-support-masks/README.md @@ -63,6 +63,12 @@ clears exactly those incident pairs and equals the genexpr. A mutant that uses rejected by the package controls. Support and topology checks remain the historical `assert` gates. +## Companion rewrite + +The sibling reverse-frame genexpr in `ExclusionCircuit.verify_embedding` is +rewritten in the same production file and packaged as +`research/exclusion-embedding-masks/` (same four certificate pins, one re-hash). + ## Open next questions 1. **Question:** Should `research/bit-compression-provenance` and other packages @@ -75,13 +81,7 @@ historical `assert` gates. **Blocked by:** Pin repair outside the four binding certificates is outside this tooling change. -2. **Question:** Is `ExclusionCircuit.verify_embedding`'s reverse-frame - expected-support sum worth a separate identical-receipt rewrite? - **Why it matters:** It is a different object from the `verify` leave-two-out - path measured here. - **First step:** Profile `ExclusionCircuit(45).verify_embedding` alone and - prototype a closed-form reverse mask if one matches the historical bits. - **Blocked by:** Out of this session’s stop rule. +2. **Answered in-tree:** `ExclusionCircuit.verify_embedding` reverse-frame closed form lands in the same production file as `research/exclusion-embedding-masks/` (identical receipts; one re-pin). ## Overlap and credit diff --git a/research/exclusion-support-masks/RESULT.json b/research/exclusion-support-masks/RESULT.json index c7ed2d607b..49cb1a377f 100644 --- a/research/exclusion-support-masks/RESULT.json +++ b/research/exclusion-support-masks/RESULT.json @@ -44,7 +44,7 @@ ], "source_sha256": { "baseline": "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0", - "production": "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63" + "production": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3" }, "status": "PASS_IDENTICAL_EXCLUSION_SUPPORT_MASK_RECEIPTS" } diff --git a/research/exclusion-support-masks/verify.py b/research/exclusion-support-masks/verify.py index 0d6c385408..478ab69f47 100644 --- a/research/exclusion-support-masks/verify.py +++ b/research/exclusion-support-masks/verify.py @@ -26,7 +26,7 @@ BASE_COMMIT = "0b1cccf86e2f5132ed286a26ef0a681a495740ea" ORIGINAL_BLOB = "aaa99986c4b81c5d4fbe016fd216a4818940bb95" ORIGINAL_SHA256 = "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0" -OPTIMIZED_SHA256 = "40bdb64670615a73be61e8810d1f5b142b02c04834cecc5020ecb7e3a7230f63" +OPTIMIZED_SHA256 = "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3" PINNED_CERTIFICATES = ( "certificates/positive-side-core.json", diff --git a/scripts/exclusion_circuit.py b/scripts/exclusion_circuit.py index 5b0b4ce69b..b0e2f3e03d 100644 --- a/scripts/exclusion_circuit.py +++ b/scripts/exclusion_circuit.py @@ -175,10 +175,18 @@ def verify_embedding(self): for slot in set(ins+outs): assert reverse_frames[slot]&~frame==0 reverse_frames[slot]=frame + # Reverse-frame expected support equals the full sorted-output mask with + # every output target incident to either source endpoint cleared. + # Equivalent to the historical genexpr + # sum(1< Date: Wed, 14 Oct 2026 17:16:50 +0000 Subject: [PATCH 3/3] Tooling: fold DisjointTensor.verify support masks into #2001 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add closed-form point-incidence masks for DisjointTensor.verify with identical h=26 receipts (1.82×), update the four disjoint_tensor.py certificate pins alongside the exclusion_circuit.py pins, and relax companion pin-discipline checks across the co-landing packages. Prepared by Wentao Li with substantial AI assistance. --- certificates/disjoint-tensor-compression.json | 2 +- certificates/dyadic-central-factor.json | 2 +- certificates/parity-side.json | 2 +- certificates/ternary-side.json | 2 +- .../disjoint-tensor-support-masks/NOTICE.md | 11 + .../disjoint-tensor-support-masks/README.md | 107 ++++++++ .../disjoint-tensor-support-masks/RESULT.json | 50 ++++ .../SOURCES.json | 78 ++++++ .../disjoint-tensor-support-masks/TASK.json | 70 +++++ .../disjoint-tensor-support-masks/VERIFY.json | 59 +++++ .../disjoint-tensor-support-masks/baseline.py | 239 ++++++++++++++++++ .../benchmark.py | 139 ++++++++++ .../disjoint-tensor-support-masks/verify.py | 233 +++++++++++++++++ research/exclusion-embedding-masks/verify.py | 13 +- research/exclusion-support-masks/verify.py | 13 +- scripts/experiments/disjoint_tensor.py | 21 +- tests/test_disjoint_tensor_support_masks.py | 142 +++++++++++ tests/test_exclusion_embedding_masks.py | 4 + tests/test_exclusion_support_masks.py | 4 + 19 files changed, 1181 insertions(+), 10 deletions(-) create mode 100644 research/disjoint-tensor-support-masks/NOTICE.md create mode 100644 research/disjoint-tensor-support-masks/README.md create mode 100644 research/disjoint-tensor-support-masks/RESULT.json create mode 100644 research/disjoint-tensor-support-masks/SOURCES.json create mode 100644 research/disjoint-tensor-support-masks/TASK.json create mode 100644 research/disjoint-tensor-support-masks/VERIFY.json create mode 100644 research/disjoint-tensor-support-masks/baseline.py create mode 100644 research/disjoint-tensor-support-masks/benchmark.py create mode 100644 research/disjoint-tensor-support-masks/verify.py create mode 100644 tests/test_disjoint_tensor_support_masks.py diff --git a/certificates/disjoint-tensor-compression.json b/certificates/disjoint-tensor-compression.json index b70256ae2d..a24b0c9af4 100644 --- a/certificates/disjoint-tensor-compression.json +++ b/certificates/disjoint-tensor-compression.json @@ -342,7 +342,7 @@ "scripts/binary_phase_frames.py": "4148cfa313cf050ed25296e8db36d35871520b8318fd086c7d1facf0c6af67d7", "scripts/complex_compression.py": "9b6f758a5985ba5d9fdd726fe6556b3d3b4bd9d98aaf8ba06a7bb58093c4c504", "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", - "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8" + "scripts/experiments/disjoint_tensor.py": "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1" }, "status": "IMPROVED FINITE COMPLEX NETWORK; NO NEW MULTIPLICATION KAPPA", "upstream_commit": "adc7f1241b42e322a6451854ab7e4b4c146bf78a" diff --git a/certificates/dyadic-central-factor.json b/certificates/dyadic-central-factor.json index f0244556ce..5bd4e583cd 100644 --- a/certificates/dyadic-central-factor.json +++ b/certificates/dyadic-central-factor.json @@ -103,7 +103,7 @@ "scripts/complex_compression.py": "9b6f758a5985ba5d9fdd726fe6556b3d3b4bd9d98aaf8ba06a7bb58093c4c504", "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", "scripts/experiments/complex_centers.py": "3744396587d729253d80980f6310b56e7bf0ea21b165bd178d0d6d4c49faf2e2", - "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", + "scripts/experiments/disjoint_tensor.py": "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1", "scripts/general_network_guard.py": "790a2064449afdbffd6dbd4adbcf4a28eaf41931ce7134e4a4e8766f3764270c" }, "status": "COMPLEX INTERFACE HEADROOM; RETAINED KAPPA 2^-31", diff --git a/certificates/parity-side.json b/certificates/parity-side.json index afbbf2d3b7..bcc1a78412 100644 --- a/certificates/parity-side.json +++ b/certificates/parity-side.json @@ -120,7 +120,7 @@ "docs/research/parity-side.md": "75de20f8dcd3d1a2e2d19bc087eda50a6cc77027651f48198868426a80cd12f2", "scripts/audit_parity_side.py": "7cbdd405ce11fa6f7ffcbef3811174f8685ad4640e9330176601d3b3d3e95390", "scripts/binary_phase_frames.py": "4148cfa313cf050ed25296e8db36d35871520b8318fd086c7d1facf0c6af67d7", - "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", + "scripts/experiments/disjoint_tensor.py": "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1", "scripts/experiments/mixed_point_circuit.py": "f509362b5031976a22af351fd158ae0b4520fa4f286a5176ec37023d672a7ddc", "scripts/experiments/parity_phase_repair.py": "1f76f3b96ed556ed901835bd101d0e2588c997d2ed39bdfca097f0187aa86d40", "scripts/experiments/parity_side.py": "1363ebf82d4c39e458e7bede8253688271008374db70ed4284df84253c8d092b" diff --git a/certificates/ternary-side.json b/certificates/ternary-side.json index b582f4a141..b2c1b793e7 100644 --- a/certificates/ternary-side.json +++ b/certificates/ternary-side.json @@ -1564,7 +1564,7 @@ "scripts/audit_ternary_side.py": "5e86566eee32031e1829d26ea17a7372e4802e2f7d76a2caa3ae16de3ab072c1", "scripts/certify.py": "2348d95008276c5d5764f7e028c3918e484582cd589bd4d9b82919161745e53e", "scripts/exclusion_circuit.py": "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3", - "scripts/experiments/disjoint_tensor.py": "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8", + "scripts/experiments/disjoint_tensor.py": "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1", "scripts/experiments/prime_core.py": "080a89a2c91505b509068698542843c9d7af3cdec01b5c88972107dc9c688c62", "scripts/experiments/rank_product_core.py": "7a91c1188c5d42e864a5d2e42397b09c435b526ae4f4210c44bf615de488708e", "scripts/experiments/shared_core.py": "d3c59b61229956cb54ae1ef9145ed213abdf1ae743929de145a77c43656f01a8", diff --git a/research/disjoint-tensor-support-masks/NOTICE.md b/research/disjoint-tensor-support-masks/NOTICE.md new file mode 100644 index 0000000000..3c1f0007bb --- /dev/null +++ b/research/disjoint-tensor-support-masks/NOTICE.md @@ -0,0 +1,11 @@ +# Notice — disjoint-tensor support masks + +Douglas R. Colkitt authored the disjoint-tensor compression screen, the +`DisjointTensor` recurrence and embedding, and the original expected-support +coefficient checks consumed by this tooling package. + +This package adds an equivalence-preserving closed-form rewrite of the +historical leave-intersecting-inputs expected-support genexpr inside +`DisjointTensor.verify`, with identical receipts and updated source pins only. + +Prepared by Wentao Li with substantial AI assistance, under Apache-2.0. diff --git a/research/disjoint-tensor-support-masks/README.md b/research/disjoint-tensor-support-masks/README.md new file mode 100644 index 0000000000..935634dbb2 --- /dev/null +++ b/research/disjoint-tensor-support-masks/README.md @@ -0,0 +1,107 @@ +# Faster DisjointTensor.verify output supports with identical receipts + +**Tooling improvement; no new kappa.** +Landed with open [CrocSwap/integer-mult-bounds#2001](https://github.com/CrocSwap/integer-mult-bounds/pull/2001) so shared certificate JSON files carry both the `exclusion_circuit.py` and `disjoint_tensor.py` source pins without a merge conflict. + At base +`0b1cccf86e2f5132ed286a26ef0a681a495740ea`, the historical disjoint-tensor +checker (`scripts/experiments/disjoint_tensor.py:DisjointTensor.verify`) spends +most of its wall time on the leave-intersecting-inputs expected-support genexpr + +```text +sum(1< n: return (0, 'empty') + if not a: return (0, 'copy') + if not b: return (comb(n, a)-1, 'sum') + best = (comb(n, b)*(comb(n-b, a)-1), 'rows') + for left in range(1, n//2+1): + right = n-left; total = 0 + for j in range(max(0, b-right), min(left, b)+1): + parts = 0 + for i in range(max(0, a-right), min(left, a)+1): + if i+j > left or a-i+b-j > right: continue + A, B = plan(left, i, j)[0], plan(right, a-i, b-j)[0] + total += min(comb(right, a-i)*A+comb(left, j)*B, + comb(left, i)*B+comb(right, b-j)*A) + parts += 1 + if parts: total += comb(left, j)*comb(right, b-j)*(parts-1) + if total < best[0]: best = total, left + return best + + +@cache +def subsets(points, size): + return tuple(sum(1 << p for p in S) for S in combinations(points, size)) + + +class DisjointTensor(ExclusionCircuit): + def __init__(self, h, k=3, maximum_inputs=10000): + require(type(h) is int and type(k) is int and h >= 2*k and k >= 1, + 'Need a nonempty disjointness relation') + require(comb(h, k) <= maximum_inputs, 'Expansion budget exceeded') + self.h, self.k = h, k + self.inputs = list(subsets(tuple(range(h)), k)) + self.support = [0]+[1 << i for i in range(len(self.inputs))] + self.args = [None]*len(self.support) + self.lookup = {s:i for i, s in enumerate(self.support)} + self.variables = {S:i+1 for i, S in enumerate(self.inputs)} + self.outputs = self.transform(tuple(range(h)), k, k, self.variables) + self.active = set(); pending = list(self.outputs.values()) + while pending: + node = pending.pop() + if not node or node in self.active: continue + self.active.add(node) + if self.args[node]: pending.extend(self.args[node]) + self.additions = sum(self.args[node] is not None for node in self.active) + + def transform(self, points, a, b, values): + n = len(points); _, method = plan(n, a, b) + outputs = subsets(points, b) + if method == 'empty': return {T:0 for T in outputs} + if method == 'copy': return {T:values[0] for T in outputs} + if method == 'sum': return {0:self.total(list(values.values()))} + if method == 'rows': + return {T:self.total([values[S] for S in subsets(points, a) if not S&T]) + for T in outputs} + left, right = points[:method], points[method:] + l, r = len(left), len(right) + terms = {T:[] for T in outputs} + for j in range(max(0, b-r), min(l, b)+1): + for i in range(max(0, a-r), min(l, a)+1): + if i+j > l or a-i+b-j > r: continue + A, B = plan(l, i, j)[0], plan(r, a-i, b-j)[0] + if comb(r, a-i)*A+comb(l, j)*B <= comb(l, i)*B+comb(r, b-j)*A: + middle = {S:self.transform(left, i, j, + {R:values[R|S] for R in subsets(left, i)}) + for S in subsets(right, a-i)} + for T in subsets(left, j): + row = self.transform(right, a-i, b-j, {S:M[T] for S,M in middle.items()}) + for U, node in row.items(): terms[T|U].append(node) + else: + middle = {S:self.transform(right, a-i, b-j, + {R:values[R|S] for R in subsets(right, a-i)}) + for S in subsets(left, i)} + for T in subsets(right, b-j): + row = self.transform(left, i, j, {S:M[T] for S,M in middle.items()}) + for U, node in row.items(): terms[T|U].append(node) + return {T:self.total(nodes) for T,nodes in terms.items()} + + def verify(self): + for node in sorted(self.active): + if self.args[node]: + a, b = self.args[node] + require(a < node and b < node and not self.support[a]&self.support[b], + 'An addition uses cancellation or is not acyclic') + require(self.support[node] == self.support[a]|self.support[b], 'Wrong sum') + for T, node in self.outputs.items(): + expected = sum(1 << i for i, S in enumerate(self.inputs) if not S&T) + require(self.support[node] == expected, 'Wrong integer output coefficient') + require(self.additions <= plan(self.h,self.k,self.k)[0], 'Interning exceeded recurrence') + code = self.compile(); values = [0]*code['roles'] + for S, slot in code['sources'].items(): values[slot] = self.support[self.variables[S]] + for node, ins, outs in code['gates']: + for slot in ins[1:]: + require(not values[ins[0]]&values[slot], 'Embedding adds overlapping supports') + values[ins[0]] |= values[slot] + for slot in outs[1:]: + require(values[slot] == 0, 'Fanout destination is not fresh') + values[slot] = values[ins[0]] + for T, slot in code['outputs'].items(): + require(values[slot] == self.support[self.outputs[T]], 'Wrong embedded output') + encoded = json.dumps([[i,self.args[i],str(self.support[i])] for i in sorted(self.active)], + separators=(',',':')).encode() + return dict(h=self.h,k=self.k,inputs=len(self.inputs),outputs=len(self.outputs), + additions=self.additions,unshared_recurrence_additions=plan(self.h,self.k,self.k)[0], + roles=code['roles'],integer_coefficient_map_verified=True, + disjoint_additions_verified=True,embedding_verified=True, + coefficients=len(self.inputs)*comb(self.h-self.k,self.k), + dag_sha256=sha256(encoded).hexdigest()) + + def frame_data(self): + """Ancestor/reachable-target families and their ground-point unions.""" + descendants = {node:0 for node in self.active} + targets = sorted(self.outputs) + for i,T in enumerate(targets): descendants[self.outputs[T]] |= 1 << i + for node in sorted(self.active, reverse=True): + if self.args[node]: + for parent in self.args[node]: descendants[parent] |= descendants[node] + source_union = {} + target_union = {} + for node in sorted(self.active): + if self.args[node]: + a,b = self.args[node]; source_union[node] = source_union[a]|source_union[b] + else: source_union[node] = self.inputs[node-1] + bits = descendants[node]; union = 0 + while bits: + bit = bits&-bits; bits ^= bit; union |= targets[bit.bit_length()-1] + target_union[node] = union + require(union and not union&source_union[node], 'Node is not a disjoint rectangle') + return descendants, source_union, target_union + + def verify_frames(self, exact_bases=False): + """Every physical transition, both orientations, including idle endpoints. + + General acceptance uses the proved three-case enclosure lemma. Optional + exact mode constructs every distinct local residual's binary basis. + """ + require(self.k == 3 and self.h > 6, 'The phase lemma here is for triples') + descendants, source_union, target_union = self.frame_data() + code = self.compile(); h = self.h; full = ('coordinate',(1 << h)-1) + empty = ('coordinate',0); checked = set(); types = {} + def space(frame): + kind, mask = frame + if kind == 'line': return (mask,) + if kind == 'complement': return nullspace((mask,),h) + return tuple(1 << i for i in range(h) if mask >> i & 1) + def transition(lower, upper): + if lower == upper: return + pair = lower, upper + if pair in checked: return + a,A = lower; b,B = upper + if a == 'coordinate' and b == 'coordinate': require(A&~B == 0,'Not nested coordinates') + elif a == 'line' and b == 'coordinate': require(A&~B == 0 and B&~A,'Missing residual unit') + elif a == 'coordinate' and b == 'line': require(A == 0,'Invalid line entry') + elif a == 'coordinate' and b == 'complement': + require(not A&B and ((1 << h)-1)&~(A|B),'Missing complement residual unit') + elif a == 'line' and b == 'complement': + require(not A&B and ((1 << h)-1)&~(A|B),'Missing two-line residual unit') + elif a == 'complement' and b == 'coordinate': require(B == (1 << h)-1,'Invalid complement exit') + else: require(False,'Unsupported phase transition') + if exact_bases: certify_chain((space(lower),space(upper))) + checked.add(pair) + for reverse in (False,True): + labels = {} + for node in sorted(self.active): + ns,nt = self.support[node].bit_count(),descendants[node].bit_count() + A,B = source_union[node],target_union[node] + if reverse: ns,nt,A,B = nt,ns,B,A + label = ('line',A) if ns == 1 else ('complement',B) if nt == 1 else ('coordinate',A) + labels[node] = label; types[label[0]] = types.get(label[0],0)+1 + physical = [empty]*code['roles'] + inputs = code['outputs'] if reverse else code['sources'] + outputs = code['sources'] if reverse else code['outputs'] + for S,slot in inputs.items(): + physical[slot] = ('line',S); transition(empty,physical[slot]) + gates = reversed(code['gates']) if reverse else code['gates'] + for node,ins,outs in gates: + label = labels[node] + for slot in set(ins+outs): transition(physical[slot],label); physical[slot] = label + for T,slot in outputs.items(): + target = ('complement',T); transition(physical[slot],target); physical[slot] = target + for old in physical: transition(old,full) + return dict(h=h,orientations=2,all_physical_transitions_checked=True, + distinct_nontrivial_transitions=len(checked),frame_types=types, + exact_residual_bases_constructed=exact_bases) + + +def complex_counts(h, disjoint_roles): + from fractions import Fraction as Q + require(h >= 8 and h%2 == 0 and disjoint_roles >= 0, 'Invalid complex ledger') + n = comb(h,3); N = n**3; m = h**3 + pair_roles = comb(h,2)*(4*(h-2)-6) + side = disjoint_roles+pair_roles; centers = h+1 + W = 2*N+2*n*n*(side+centers); L = 3*n*n*h*centers + delta = 2*N-2*L + return dict(h=h,v=n,N=N,m=m,disjoint_roles=disjoint_roles, + intersection_two_roles=pair_roles,side_roles=side,central_roles=centers, + W=W,L=L,s=W*m-delta,deficit=delta,eta=Q(delta,W*m)) + + +def scalar_program(circuit): + """Reuse the signed invocation interface with the new +1/2 side DAG.""" + from fractions import Fraction as Q + from complex_compression import LeaveOneCircuit + h = circuit.h + require(circuit.k == 3 and h >= 8 and h%2 == 0, 'Complex triple interface requires even h') + triples = list(combinations(range(h),3)) + index = {sum(1 << x for x in T):i for i,T in enumerate(triples)} + code = circuit.compile() + gates = [(ins,outs) for _,ins,outs in code['gates']] + sources = [(index[T],slot) for T,slot in code['sources'].items()] + outputs = [(index[T],slot,Q(1,2)) for T,slot in code['outputs'].items()] + offset = code['roles']; pair_code = LeaveOneCircuit(h-2).compile() + for pair in combinations(range(h),2): + rest = [i for i in range(h) if i not in pair] + def target(i): return index[sum(1 << x for x in (*pair,rest[i]))] + for _,ins,outs in pair_code['gates']: + gates.append((tuple(offset+i for i in ins),tuple(offset+i for i in outs))) + sources.extend((target(i),offset+slot) for i,slot in pair_code['sources'].items()) + outputs.extend((target(i),offset+slot,Q(-1,2)) for i,slot in pair_code['outputs'].items()) + offset += pair_code['roles'] + require(offset == complex_counts(h,code['roles'])['side_roles'], 'Wrong scalar role ledger') + return dict(h=h,triples=triples,gates=gates,sources=sources,outputs=outputs,roles=offset) diff --git a/research/disjoint-tensor-support-masks/benchmark.py b/research/disjoint-tensor-support-masks/benchmark.py new file mode 100644 index 0000000000..45334dea5f --- /dev/null +++ b/research/disjoint-tensor-support-masks/benchmark.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +"""Alternating fresh-process before/after timing for DisjointTensor.verify. + +Timing is descriptive and never a correctness gate. Receipts must match +fieldwise between baseline and optimized workers. Circuit construction is +shared and excluded from the timed interval so the measurement isolates the +disjoint-output expected-support path inside verify. +""" +from __future__ import annotations + +import argparse +from hashlib import sha256 +import json +import os +from pathlib import Path +import platform +import statistics +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +sys.path.insert(0, str(ROOT / "scripts")) +from certify import require # noqa: E402 + +WORKER = r''' +import importlib.util +import json +import sys +import time +from pathlib import Path + +root = Path(sys.argv[1]) +mode = sys.argv[2] +sys.path.insert(0, str(root / "scripts")) + +if mode == "baseline": + spec = importlib.util.spec_from_file_location( + "baseline_dt", root / "research/disjoint-tensor-support-masks/baseline.py" + ) + mod = importlib.util.module_from_spec(spec) + sys.modules["baseline_dt"] = mod + spec.loader.exec_module(mod) + DisjointTensor = mod.DisjointTensor +else: + from experiments.disjoint_tensor import DisjointTensor + +# Build the fixed historical full-size circuit outside the timed window. +circuit = DisjointTensor(26) + +t0 = time.perf_counter() +record = circuit.verify() +elapsed = time.perf_counter() - t0 +print(json.dumps(dict(mode=mode, seconds=elapsed, record=record), sort_keys=True)) +''' + + +def run_worker(mode: str) -> dict: + env = dict(os.environ) + env.pop("PYTHONOPTIMIZE", None) + env["PYTHONDONTWRITEBYTECODE"] = "1" + proc = subprocess.run( + [sys.executable, "-I", "-B", "-c", WORKER, str(ROOT), mode], + cwd=ROOT, + env=env, + capture_output=True, + text=True, + check=False, + ) + require(proc.returncode == 0, f"worker {mode} failed: {proc.stderr[-2000:]}") + line = proc.stdout.strip().splitlines()[-1] + return json.loads(line) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--rounds", type=int, default=5) + parser.add_argument("--out", type=Path, default=PACKAGE / "RESULT.json") + args = parser.parse_args() + require(args.rounds >= 1, "rounds") + + samples = [] + for i in range(args.rounds): + order = ("optimized", "baseline") if i % 2 == 0 else ("baseline", "optimized") + pair = {} + for mode in order: + pair[mode] = run_worker(mode) + require( + pair["baseline"]["record"] == pair["optimized"]["record"], + f"receipt mismatch on round {i}", + ) + samples.append( + dict( + round=i, + baseline_seconds=pair["baseline"]["seconds"], + optimized_seconds=pair["optimized"]["seconds"], + ratio=pair["baseline"]["seconds"] / pair["optimized"]["seconds"], + ) + ) + print( + f"round {i}: baseline={pair['baseline']['seconds']:.3f}s " + f"optimized={pair['optimized']['seconds']:.3f}s " + f"ratio={samples[-1]['ratio']:.3f}", + flush=True, + ) + + base_med = statistics.median(s["baseline_seconds"] for s in samples) + opt_med = statistics.median(s["optimized_seconds"] for s in samples) + result = dict( + status="PASS_IDENTICAL_DISJOINT_TENSOR_SUPPORT_MASK_RECEIPTS", + fixture="DisjointTensor(26).verify only (construction excluded)", + rounds=args.rounds, + median_baseline_seconds=base_med, + median_optimized_seconds=opt_med, + median_ratio=base_med / opt_med, + samples=samples, + environment=dict( + python=sys.version, + platform=platform.platform(), + executable=sys.executable, + ), + source_sha256=dict( + baseline=sha256((PACKAGE / "baseline.py").read_bytes()).hexdigest(), + production=sha256( + (ROOT / "scripts/experiments/disjoint_tensor.py").read_bytes() + ).hexdigest(), + ), + no_new_kappa=True, + ) + args.out.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n") + print( + "PASS_IDENTICAL_DISJOINT_TENSOR_SUPPORT_MASK_RECEIPTS", + f"median {base_med:.3f} -> {opt_med:.3f} s ({base_med / opt_med:.2f}x)", + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/research/disjoint-tensor-support-masks/verify.py b/research/disjoint-tensor-support-masks/verify.py new file mode 100644 index 0000000000..f5281b6822 --- /dev/null +++ b/research/disjoint-tensor-support-masks/verify.py @@ -0,0 +1,233 @@ +#!/usr/bin/env python3 +"""Equivalence, pin and mutation controls for DisjointTensor output supports. + +Read-only by default. Regenerates no certificate. Timing is not a pass gate; +see benchmark.py / RESULT.json. +""" +from __future__ import annotations + +import argparse +from copy import deepcopy +from hashlib import sha256 +import importlib.util +import json +from pathlib import Path +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[2] +PACKAGE = Path(__file__).resolve().parent +SCRIPTS = ROOT / "scripts" +sys.path.insert(0, str(SCRIPTS)) + +from certify import require # noqa: E402 +from experiments import disjoint_tensor as production # noqa: E402 + +BASE_COMMIT = "0b1cccf86e2f5132ed286a26ef0a681a495740ea" +ORIGINAL_BLOB = "dd196e67162c349d9b6f7aa27edbed2b1b0b601f" +ORIGINAL_SHA256 = "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8" +OPTIMIZED_SHA256 = "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1" +H26_DAG_SHA256 = "5da8011dadf51d6121d343416fa38b6b4bf524fd689e5b1b2def91324660f19b" + +PINNED_CERTIFICATES = ( + "certificates/disjoint-tensor-compression.json", + "certificates/ternary-side.json", + "certificates/dyadic-central-factor.json", + "certificates/parity-side.json", +) + + +def load_module(path: Path, name: str): + spec = importlib.util.spec_from_file_location(name, path) + require(spec is not None and spec.loader is not None, f"missing module {path}") + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + +def digest_file(path: Path) -> str: + return sha256(path.read_bytes()).hexdigest() + + +def git_show(path: str) -> bytes: + return subprocess.check_output(["git", "show", f"{BASE_COMMIT}:{path}"], cwd=ROOT) + + +def check_baseline_bytes() -> None: + baseline = PACKAGE / "baseline.py" + original = git_show("scripts/experiments/disjoint_tensor.py") + require(sha256(original).hexdigest() == ORIGINAL_SHA256, "base disjoint_tensor.py sha256 drift") + blob = ( + subprocess.check_output( + ["git", "rev-parse", f"{BASE_COMMIT}:scripts/experiments/disjoint_tensor.py"], + cwd=ROOT, + ) + .decode() + .strip() + ) + require(blob == ORIGINAL_BLOB, "base disjoint_tensor.py blob drift") + require(baseline.read_bytes() == original, "baseline.py must be an exact copy of the base commit source") + require( + digest_file(SCRIPTS / "experiments" / "disjoint_tensor.py") == OPTIMIZED_SHA256, + "production disjoint_tensor.py does not match the claimed optimized digest", + ) + + +def historical_expected(inputs, T: int) -> int: + return sum(1 << i for i, S in enumerate(inputs) if not S & T) + + +def closed_form_expected(full: int, vertex: list[int], T: int) -> int: + hit = 0 + bits = T + while bits: + bit = bits & -bits + bits ^= bit + hit |= vertex[bit.bit_length() - 1] + return full ^ hit + + +def build_vertex(inputs, h: int) -> tuple[int, list[int]]: + full = (1 << len(inputs)) - 1 + vertex = [0] * h + for i, S in enumerate(inputs): + bits = S + while bits: + bit = bits & -bits + bits ^= bit + vertex[bit.bit_length() - 1] |= 1 << i + return full, vertex + + +def check_mask_equivalence() -> dict: + """Closed-form disjoint-output masks must match the historical genexpr.""" + cases = 0 + for h in (8, 10, 12, 14, 16, 18, 20, 22, 26): + circuit = production.DisjointTensor(h) + full, vertex = build_vertex(circuit.inputs, circuit.h) + for T, node in circuit.outputs.items(): + old = historical_expected(circuit.inputs, T) + new = closed_form_expected(full, vertex, T) + require(old == new, f"mask mismatch h={h} T={T}") + require(circuit.support[node] == new, f"live support mismatch h={h} T={T}") + cases += 1 + # Mutant foil: XOR of per-point masks cancels inputs that meet T twice. + T = next(iter(circuit.outputs)) + xor_hit = 0 + bits = T + while bits: + bit = bits & -bits + bits ^= bit + xor_hit ^= vertex[bit.bit_length() - 1] + mutant = full ^ xor_hit + correct = closed_form_expected(full, vertex, T) + require(mutant != correct, f"xor-mutant must differ on h={h}") + return {"mask_cases": cases, "xor_mutant": "rejected"} + + +def check_verify_equivalence(baseline, optimized) -> dict: + records = [] + for h in (8, 10, 12, 16, 20, 26): + circuit = optimized.DisjointTensor(h) + old = baseline.DisjointTensor.verify(circuit) + new = circuit.verify() + require(old == new, f"verify mismatch on h={h}") + records.append( + dict( + h=h, + inputs=new["inputs"], + outputs=new["outputs"], + additions=new["additions"], + roles=new["roles"], + coefficients=new["coefficients"], + dag_sha256=new["dag_sha256"], + ) + ) + require(records[-1]["additions"] == 57772, "unexpected h=26 additions") + require(records[-1]["roles"] == 60372, "unexpected h=26 roles") + require(records[-1]["dag_sha256"] == H26_DAG_SHA256, "unexpected h=26 dag_sha256") + return {"verify_cases": len(records), "records": records, "h26_dag_sha256": H26_DAG_SHA256} + + +def check_support_rejection(baseline, optimized) -> dict: + circuit = optimized.DisjointTensor(10) + _target, node = next(iter(circuit.outputs.items())) + # Flip one expected support bit; both engines must reject. + circuit.support[node] ^= 1 + with _expect_value_error(): + optimized.DisjointTensor.verify(circuit) + with _expect_value_error(): + baseline.DisjointTensor.verify(circuit) + return {"support_rejection": "preserved", "corrupted_h": 10} + + +class _expect_value_error: + def __enter__(self): + return self + + def __exit__(self, exc_type, exc, tb): + require(exc_type is ValueError, "expected ValueError from corrupted support") + return True + + +def check_certificate_pin_discipline() -> dict: + """Certificates may update disjoint_tensor.py and companion exclusion_circuit.py pins.""" + companion_key = "scripts/exclusion_circuit.py" + companion_orig = "b253f9d7318becda1c1f3b24e2287ed44a9c27fcea00cf9d531afb950d0457a0" + companion_opt = "f9fbcd6e85e442cbd0926ac0887414e20735b2dc923d2d5822c803ceb46726d3" + reports = {} + for rel in PINNED_CERTIFICATES: + base = json.loads(git_show(rel).decode()) + live = json.loads((ROOT / rel).read_text(encoding="utf-8")) + key = "scripts/experiments/disjoint_tensor.py" + base_pin = base["source_sha256"][key] + live_pin = live["source_sha256"][key] + require(base_pin == ORIGINAL_SHA256, f"{rel}: unexpected base pin") + require(live_pin == OPTIMIZED_SHA256, f"{rel}: optimized pin missing") + restored = deepcopy(live) + restored["source_sha256"][key] = ORIGINAL_SHA256 + if companion_key in live["source_sha256"]: + require( + live["source_sha256"][companion_key] in {companion_orig, companion_opt}, + f"{rel}: unexpected companion exclusion_circuit pin", + ) + restored["source_sha256"][companion_key] = companion_orig + require(restored == base, f"{rel}: changed fields beyond allowed source pins") + reports[rel] = { + "base_pin": base_pin, + "live_pin": live_pin, + "only_pin_changed": True, + } + return reports + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--write-certificate", action="store_true", help="unused; kept for API parity") + args = parser.parse_args() + require(not args.write_certificate, "this package does not rewrite certificate payloads") + + check_baseline_bytes() + baseline = load_module(PACKAGE / "baseline.py", "disjoint_tensor_support_masks_baseline") + masks = check_mask_equivalence() + verifies = check_verify_equivalence(baseline, production) + rejection = check_support_rejection(baseline, production) + pins = check_certificate_pin_discipline() + print( + "PASS_DISJOINT_TENSOR_SUPPORT_MASKS", + f"verify_cases={verifies['verify_cases']}", + f"mask_cases={masks['mask_cases']}", + f"certs={len(pins)}", + "no_new_kappa", + ) + print( + "controls", + rejection["support_rejection"], + f"h26_sha={verifies['h26_dag_sha256'][:16]}", + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/research/exclusion-embedding-masks/verify.py b/research/exclusion-embedding-masks/verify.py index 4bfb3f5ef1..d280945bc5 100644 --- a/research/exclusion-embedding-masks/verify.py +++ b/research/exclusion-embedding-masks/verify.py @@ -220,7 +220,10 @@ def __exit__(self, exc_type, exc, tb): def check_certificate_pin_discipline() -> dict: - """Working certificates may change only the exclusion_circuit.py source pin.""" + """Certificates may update exclusion_circuit.py and companion disjoint_tensor.py pins.""" + companion_key = "scripts/experiments/disjoint_tensor.py" + companion_orig = "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8" + companion_opt = "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1" reports = {} for rel in PINNED_CERTIFICATES: base = json.loads(git_show(rel).decode()) @@ -231,7 +234,13 @@ def check_certificate_pin_discipline() -> dict: require(live_pin == OPTIMIZED_SHA256, f"{rel}: optimized pin missing") restored = deepcopy(live) restored["source_sha256"]["scripts/exclusion_circuit.py"] = ORIGINAL_SHA256 - require(restored == base, f"{rel}: changed fields beyond the exclusion_circuit pin") + if companion_key in live["source_sha256"]: + require( + live["source_sha256"][companion_key] in {companion_orig, companion_opt}, + f"{rel}: unexpected companion disjoint_tensor pin", + ) + restored["source_sha256"][companion_key] = companion_orig + require(restored == base, f"{rel}: changed fields beyond allowed source pins") reports[rel] = { "base_pin": base_pin, "live_pin": live_pin, diff --git a/research/exclusion-support-masks/verify.py b/research/exclusion-support-masks/verify.py index 478ab69f47..43b8396e11 100644 --- a/research/exclusion-support-masks/verify.py +++ b/research/exclusion-support-masks/verify.py @@ -152,7 +152,10 @@ def __exit__(self, exc_type, exc, tb): def check_certificate_pin_discipline() -> dict: - """Working certificates may change only the exclusion_circuit.py source pin.""" + """Certificates may update exclusion_circuit.py and companion disjoint_tensor.py pins.""" + companion_key = "scripts/experiments/disjoint_tensor.py" + companion_orig = "102dd59bf96033f96ba0dab4c2aba77b3a7a73b67b0f96929ff8b20a98b91cd8" + companion_opt = "085600fc922ae167b5981505e1a5caba4d1c1df15482f61d83d2c086fa89eac1" reports = {} for rel in PINNED_CERTIFICATES: base = json.loads(git_show(rel).decode()) @@ -163,7 +166,13 @@ def check_certificate_pin_discipline() -> dict: require(live_pin == OPTIMIZED_SHA256, f"{rel}: optimized pin missing") restored = deepcopy(live) restored["source_sha256"]["scripts/exclusion_circuit.py"] = ORIGINAL_SHA256 - require(restored == base, f"{rel}: changed fields beyond the exclusion_circuit pin") + if companion_key in live["source_sha256"]: + require( + live["source_sha256"][companion_key] in {companion_orig, companion_opt}, + f"{rel}: unexpected companion disjoint_tensor pin", + ) + restored["source_sha256"][companion_key] = companion_orig + require(restored == base, f"{rel}: changed fields beyond allowed source pins") reports[rel] = { "base_pin": base_pin, "live_pin": live_pin, diff --git a/scripts/experiments/disjoint_tensor.py b/scripts/experiments/disjoint_tensor.py index dd196e6716..b842d0a8d4 100644 --- a/scripts/experiments/disjoint_tensor.py +++ b/scripts/experiments/disjoint_tensor.py @@ -100,9 +100,26 @@ def verify(self): require(a < node and b < node and not self.support[a]&self.support[b], 'An addition uses cancellation or is not acyclic') require(self.support[node] == self.support[a]|self.support[b], 'Wrong sum') + # Expected output support equals the full input mask with every input + # that meets T cleared. Equivalent to the historical genexpr + # sum(1<