From 8599ce3b2c8327e66ed3d775bf9f87f301df8334 Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Mon, 11 May 2026 08:02:25 -0400 Subject: [PATCH] ticket #000036: add KAT-regen tooling + close MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "One more iteration then close" (fox): added committed KAT-regeneration scripts for both the T3 calculator and φ_PRG — the regen step was a throwaway temp script before; now it's reproducible and the phi_prg test's skipif reason ("run scripts/generate_phi_prg_kat.py") points at a file that exists. Then closed #000036. New scripts: - scripts/generate_t3_bound_kat.py — regenerates bench/fixtures/t3-bound/known-answer-tests.jsonl from a fixed 12-config list (the §7 worked examples under max_envelope + non-default-C_B* + g=0 edge + explicit-b1_model pins for the other three models). - scripts/generate_phi_prg_kat.py — regenerates bench/fixtures/phi-prg/known-answer-tests.jsonl from a fixed 10-entry list (placeholder/random seeds, one-bit-flip variants, block-boundary dim_h=16/17, 4096 counter-rollover stress). - Both verified to reproduce the committed fixture data lines byte- for-byte (only the header comments changed, to reference the script). Each docstring states: run after any algorithm change, then bump the module version (CALCULATOR_VERSION / PHI_PRG_VERSION) so the fixture's version field changes too. Doc/test: - test_t3_bound_calculator.py skipif reason now references the regen script (matches the phi_prg test pattern). - #000035 §3.3 + t3-bound.md §10.1 reference the regen scripts. Closure (#000036): - Status → closed · 2026-05-11 in the ticket file + TICKETS.md row. Phase 1 + dav1d Tier-1/Tier-2 (Option B in v1) + KAT-regen tooling all landed; all §5 acceptance criteria met; both dav1d closure blockers cleared. Continuation: empirical C_B1/C_B2/C_B3 tightening under #000043 (parks on v7 deployment data); landing the bound's framing into a v7 plastic-training spec parks on that spec gaining a deployment target; R2's architectural integrations (Merkle audit- event commitment, SQD canonicalization, CTI clause-lattice, 5F trigger, ForkScore security-risk) are separate tickets if wanted. - t3-bound.md header flipped to "closed 2026-05-11". Full suite: 2312 passed, 28 skipped. --- .../fixtures/phi-prg/known-answer-tests.jsonl | 10 +- .../t3-bound/known-answer-tests.jsonl | 6 +- docs/TICKETS.md | 6 +- docs/soft-hash-channel-t3-bound.md | 33 ++-- .../ticket-000035-prg-choice-phi-prg.md | 8 +- .../ticket-000036-t3-per-window-bound.md | 2 +- scripts/generate_phi_prg_kat.py | 115 ++++++++++++++ scripts/generate_t3_bound_kat.py | 145 ++++++++++++++++++ tests/test_t3_bound_calculator.py | 2 +- 9 files changed, 298 insertions(+), 29 deletions(-) create mode 100644 scripts/generate_phi_prg_kat.py create mode 100644 scripts/generate_t3_bound_kat.py diff --git a/bench/fixtures/phi-prg/known-answer-tests.jsonl b/bench/fixtures/phi-prg/known-answer-tests.jsonl index a579035..f7aa587 100644 --- a/bench/fixtures/phi-prg/known-answer-tests.jsonl +++ b/bench/fixtures/phi-prg/known-answer-tests.jsonl @@ -1,10 +1,8 @@ # arborist v7 phi_prg known-answer tests — version phi-prg-v1-hmac-sha512-le -# Pinned (seed, hard_hash, dim_h) → SHA-256 of raw byte output -# (HMAC-SHA-512 little-endian counter-mode expansion, before float -# conversion). Regenerated 2026-05-11 after the big-endian → little- -# endian counter flip (dav1d review; PHI_PRG_VERSION carries -le). -# Algorithm change MUST bump PHI_PRG_VERSION and create a new fixture -# file; do not overwrite — old runs replay against old data. +# Regenerated by scripts/generate_phi_prg_kat.py. Records SHA-256 of +# the raw HMAC-SHA-512 (little-endian counter-mode) expansion output, +# before float conversion. Algorithm change MUST bump PHI_PRG_VERSION +# and re-run this script; old runs replay against the old fixture. {"label": "placeholder-seed/zero-hash/dim_h=1", "version": "phi-prg-v1-hmac-sha512-le", "seed_hex": "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", "hard_hash_hex": "0000000000000000000000000000000000000000000000000000000000000000", "dim_h": 1, "output_sha256": "93618e085f1afae3368cabb57b328f2f01a81cb45c768e02264b13d5ec52732a", "output_bytes": 4} {"label": "placeholder-seed/zero-hash/dim_h=8", "version": "phi-prg-v1-hmac-sha512-le", "seed_hex": "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", "hard_hash_hex": "0000000000000000000000000000000000000000000000000000000000000000", "dim_h": 8, "output_sha256": "b7e7af7180105100e94fcb4361799e2820d78f9cf2636643dea2a367ea16beb8", "output_bytes": 32} {"label": "placeholder-seed/zero-hash/dim_h=32", "version": "phi-prg-v1-hmac-sha512-le", "seed_hex": "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", "hard_hash_hex": "0000000000000000000000000000000000000000000000000000000000000000", "dim_h": 32, "output_sha256": "1b5e89f20777000d0051daffb79aaf93e1e8bba7d3a77d5f6b4354561800d365", "output_bytes": 128} diff --git a/bench/fixtures/t3-bound/known-answer-tests.jsonl b/bench/fixtures/t3-bound/known-answer-tests.jsonl index 0fcd64e..45b2e40 100644 --- a/bench/fixtures/t3-bound/known-answer-tests.jsonl +++ b/bench/fixtures/t3-bound/known-answer-tests.jsonl @@ -1,7 +1,7 @@ # T3 bound calculator known-answer tests — version t3-bound-v1-bottou-refinement -# Regenerated 2026-05-11 after the b1_model=max_envelope default change -# (dav1d review Option B applied in v1). Algorithm change MUST bump -# CALCULATOR_VERSION + emit a new fixture file. +# Regenerated by scripts/generate_t3_bound_kat.py. Algorithm change +# MUST bump CALCULATOR_VERSION and re-run this script; old runs +# replay against the old fixture (do not delete history). {"label": "small-deployment-§7.1", "calculator_version": "t3-bound-v1-bottou-refinement", "b1_model": "max_envelope", "inputs": {"gradient_fraction": 0.05, "gradient_norm_max": 1.0, "gradient_noise_stddev": 0.1, "lr_decision_interval": 100, "lr_grid_size": 8, "window_length": 10000, "batches_per_epoch": 1024, "steps_per_epoch": 1024, "c_b1": 1.0, "c_b2": 1.0, "c_b3": 1.0, "b1_model": "max_envelope"}, "expected_total": 6183.0154, "expected_b1": 5849.625, "expected_b1_selected": "aggregate_bias", "expected_b1_fraction_channels": 1729.7158, "expected_b1_aggregate_bias": 5849.625, "expected_b1_effective_control_v1": 292.4813, "expected_b2": 300.0, "expected_b3": 33.3904, "expected_snr_grad": 0.5, "expected_snr_per_channel": 10.0, "expected_certification_status": "NOT_CERTIFIED_BY_BOUND"} {"label": "medium-deployment-§7.2", "calculator_version": "t3-bound-v1-bottou-refinement", "b1_model": "max_envelope", "inputs": {"gradient_fraction": 0.05, "gradient_norm_max": 1.0, "gradient_noise_stddev": 0.1, "lr_decision_interval": 1000, "lr_grid_size": 16, "window_length": 100000, "batches_per_epoch": 8192, "steps_per_epoch": 8192, "c_b1": 1.0, "c_b2": 1.0, "c_b3": 1.0, "b1_model": "max_envelope"}, "expected_total": 58959.1575, "expected_b1": 58496.2501, "expected_b1_selected": "aggregate_bias", "expected_b1_fraction_channels": 17297.1581, "expected_b1_aggregate_bias": 58496.2501, "expected_b1_effective_control_v1": 2924.8125, "expected_b2": 400.0, "expected_b3": 62.9075, "expected_snr_grad": 0.5, "expected_snr_per_channel": 10.0, "expected_certification_status": "NOT_CERTIFIED_BY_BOUND"} {"label": "hardened-deployment-§7.3", "calculator_version": "t3-bound-v1-bottou-refinement", "b1_model": "max_envelope", "inputs": {"gradient_fraction": 0.01, "gradient_norm_max": 1.0, "gradient_noise_stddev": 0.1, "lr_decision_interval": 100, "lr_grid_size": 4, "window_length": 10000, "batches_per_epoch": 1024, "steps_per_epoch": 1024, "c_b1": 1.0, "c_b2": 1.0, "c_b3": 1.0, "b1_model": "max_envelope"}, "expected_total": 1608.4256, "expected_b1": 1375.0352, "expected_b1_selected": "aggregate_bias", "expected_b1_fraction_channels": 345.9432, "expected_b1_aggregate_bias": 1375.0352, "expected_b1_effective_control_v1": 13.7504, "expected_b2": 200.0, "expected_b3": 33.3904, "expected_snr_grad": 0.1, "expected_snr_per_channel": 10.0, "expected_certification_status": "NOT_CERTIFIED_BY_BOUND"} diff --git a/docs/TICKETS.md b/docs/TICKETS.md index fac4f8d..d7c81d9 100644 --- a/docs/TICKETS.md +++ b/docs/TICKETS.md @@ -92,6 +92,8 @@ Newest first. Update on every open/close. | ID | Title | Status | Opened | Directive | |----------|------------------------------------------------|-----------------------|------------|-----------| +| #000047 | ForkScore `_delta_*` aggregator (mean vs max vs sum) | open · awaiting go/no-go · doc-only; parks until #000046 produces a below-ceiling baseline to bench against. Recommends: parameterize `delta_aggregator` (default `mean`), don't change the default without #000046 data. #000012-revision / #000025 §10.14 follow-up | 2026-05-11 | — | +| #000046 | Harder 5S/5T/5F fixture tier (below-ceiling baselines) | open · awaiting go/no-go · doc-only; the bench Δ-rate terms in `fork_score` are inert while every pack runs at rate 1.0. Recommends a narrow live-path hard tier starting with 5F Falsification (`verify_quotes` near-misses). #000025 §10.14 follow-up; gates #000047 | 2026-05-11 | — | | #000045 | Prometheus-Σ Phase 3 sleep-sweep scheduler (gating ticket) | open · doc-only scaffold 2026-05-10; pins 8 governance parameters + 4 retrigger gates; opens implementation only after one retrigger fires | 2026-05-10 | — | | #000044 | AUTOCOUNT doc-drift discipline | closed · landed across `fc5ba50` / `03c0f6a` / `6c6defb` / `f5dbfab` / `3b30126` 2026-05-10 (mechanism + 4 metrics + 54 tags across 7 doc files; harness catches drift at test time, refresh is 60-second turnaround) | 2026-05-10 | — | | #000043 | Empirical tightening of T3 bound constants (#000036 Phase 2) | open · awaiting go/no-go (parks until v7 deployment surfaces measurement data) | 2026-05-10 | — | @@ -101,7 +103,7 @@ Newest first. Update on every open/close. | #000039 | Optional `sqlite-vec` retrieval backend (A/B vs FTS5, hybrid not replacement) | open · awaiting go/no-go (doc-only Phase 0) | 2026-05-09 | — | | #000038 | Phase 4 content acquisition — proprietary textbook license decisions for warrant coverage | closed · obviated 2026-05-10 by alias-substitution sprint under #000031 (74 rows in #000041 + 13 rows in #000042); 92/92 records now resolve. Residue (multilingual PD, Hilbert-Ackermann OCR, Knuth permission, personal-copy path B) preserved as design log §8 | 2026-05-09 | — | | #000037 | Prometheus-Σ recursive falsification controller (bicameral substrate) | in progress · Phases 0 + 1 + 1.b + 1.c + 2 landed 2026-05-10; **§12 Trigger 2 fired** (divergence variance 0.575 / N=37); §22 Findings 2 + 3 RESOLVED (kernel/llm cost split + sweep_weights §15.4 + per-mode τ_qa); `controller_events` carries 4 event kinds (decision · difficulty · budget_allocation · falsification_proposal) feeding `arborist controller-events` inspector + live-harvest third bucket in `bench/scripts/harvest_falsification_proposals.py`; §12 Trigger 1 probe wired 2026-05-11 (`trigger_1_branch_density` reads `fork_score_branches` — measurable, not yet fired); Phase 3 sleep-sweep scheduler tracked under #000045 (gating ticket) | 2026-05-09 | — | -| #000036 | T3 per-window covert-channel budget bound | in progress · Phase 1 + dav1d review 2026-05-11 → Tier-1 + Tier-2 (Option B = `b1_model=max_envelope` default, applied *in v1*, no v2 fork) both landed 2026-05-11; KAT regenerated (12 entries, active); baseline 625.87 → 6183.02 (max_envelope); 53 → 83 tests; both prior closure blockers cleared — remaining = fox final close-or-iterate call | 2026-05-09 | — | +| #000036 | T3 per-window covert-channel budget bound | **closed · 2026-05-11** · Phase 1 + dav1d review → Tier-1 + Tier-2 (Option B = `b1_model=max_envelope` default, in v1, no v2 fork) + KAT-regen tooling (`scripts/generate_t3_bound_kat.py`) all landed 2026-05-11; baseline 625.87 → 6183.02 (max_envelope), `NOT_CERTIFIED_BY_BOUND` at W=10000; 53 → 83 tests; 12-entry active KAT; both dav1d closure blockers cleared, all §5 acceptance criteria met. Continuation: empirical C_B* tightening under #000043 (parks on v7) | 2026-05-09 | — | | #000035 | PRG choice for φ_PRG (HMAC-SHA-512 expansion) | in progress · Phase 1 landed 2026-05-10; §9.10 amendment text **dav1d-reviewed final + endianness resolved 2026-05-11** (manifest field → `anchor_prg_seed`; v7 TLV confirmed little-endian → counter + uint32-word reads flipped big→little, `PHI_PRG_VERSION → -le`, 10 KATs regenerated; float-map prose corrected to "negligible mean −2^−32" not "unbiased"; `dim_h ≤ 16·2^32` guard + bool-reject added; seed-independence + M1-policy-separation added; HMAC-SHA-512 / 32-byte / SHALL all LOCKED; 31 tests). Remaining: land §9.10 into v7 spec when plastic-training has a deployment target (exogenous) | 2026-05-09 | — | | #000034 | Hessian alignment under φ_linear | in progress · Phase 1a landed 2026-05-10 (synthetic-ablation probe + KAT fixture); Phase 1b parks for v7 ramp-up | 2026-05-09 | — | | #000033 | Claim-pack pillar VII (combinatorics) | closed · landed 2026-05-09 (live in shard 000.db; lift verified) | 2026-05-09 | — | @@ -140,4 +142,4 @@ Newest first. Update on every open/close. ## Next ID -`000046` +`000048` diff --git a/docs/soft-hash-channel-t3-bound.md b/docs/soft-hash-channel-t3-bound.md index b1b52fb..569bb8c 100644 --- a/docs/soft-hash-channel-t3-bound.md +++ b/docs/soft-hash-channel-t3-bound.md @@ -1,19 +1,21 @@ # T3 per-window covert-channel budget bound -**Ticket**: #000036 +**Ticket**: #000036 — **closed 2026-05-11**. **Source analysis**: `docs/soft-hash-channel-analysis.md` -**Date**: 2026-05-10 (dav1d review pass + Option-B applied 2026-05-11) +**Date**: 2026-05-10 (dav1d review pass + Option-B + KAT-regen tooling 2026-05-11) **Status**: formal derivation + calculator landed; **dav1d review -returned 2026-05-11** → **Tier-1 polish + Tier-2 (Option B: the +returned 2026-05-11** → Tier-1 polish + Tier-2 (Option B: the conservative `max_envelope` B1 model, applied in v1 — no v2 fork) -both landed 2026-05-11**. KAT fixture regenerated (12 entries, -active — `test_t3_bound_known_answer_tests` no longer skips). The -default B1 model is now `max_envelope` (§3.1); `effective_control_v1` -is reachable as a backward-comparison mode. Remaining before -closure: fox's final close-or-iterate call (optionally a dav1d -re-review of the envelope formula). The named C_B* constants are -conservative starting estimates that empirical tightening -(#000043) can replace without changing call sites. ++ KAT-regen tooling (`scripts/generate_t3_bound_kat.py`) all landed +2026-05-11; **#000036 closed**. KAT fixture is 12 entries, active +(`test_t3_bound_known_answer_tests` runs, not skips). The default +B1 model is `max_envelope` (§3.1); `effective_control_v1` / +`fraction_channels` / `aggregate_bias` reachable via `--b1-model`. +The named C_B* constants are conservative starting estimates that +empirical tightening — **continued under #000043** — can replace +without changing call sites. (#000043 parks on v7 deployment +data.) Landing the bound's framing into a v7 plastic-training spec +parks on that spec gaining a deployment target. --- @@ -604,9 +606,12 @@ in the follow-up commit): v1 default rather than forking a v2 (per fox's direction): `CALCULATOR_VERSION` stays `t3-bound-v1-bottou-refinement` (the "bottou-refinement" descriptor names the unchanged B3 term). -- **KAT fixture regenerated** (`bench/fixtures/t3-bound/known-answer-tests.jsonl`, - 12 entries: §7 worked examples under `max_envelope`, explicit-mode - pins for the other three models, a `g=0` edge case). +- **KAT fixture regenerated + regen tooling.** + `bench/fixtures/t3-bound/known-answer-tests.jsonl` (12 entries: + §7 worked examples under `max_envelope`, explicit-mode pins for + the other three models, a `g=0` edge case) is now produced by + `scripts/generate_t3_bound_kat.py` — run it after any algorithm + change, then bump `CALCULATOR_VERSION`. `test_t3_bound_known_answer_tests` now runs (no longer skips); it pins `b1_model`, `b1_selected`, `certification_status`, and the per-contribution numbers. diff --git a/docs/tickets/ticket-000035-prg-choice-phi-prg.md b/docs/tickets/ticket-000035-prg-choice-phi-prg.md index 3754f92..6daafdd 100644 --- a/docs/tickets/ticket-000035-prg-choice-phi-prg.md +++ b/docs/tickets/ticket-000035-prg-choice-phi-prg.md @@ -187,8 +187,12 @@ def phi_prg(hard_hash_32: bytes, dim_h: int) -> list[float]: ### 3.3 Bench fixture `bench/fixtures/phi-prg/known-answer-tests.jsonl` — fixed -(seed, hard_hash, dim_h) → expected_output_sha256 triples for -regression coverage. Generated once, pinned. +(seed, hard_hash, dim_h) → expected_output_sha256 triples (10 +entries) for regression coverage. Regenerated by +`scripts/generate_phi_prg_kat.py` (run it after any algorithm +change, then bump `PHI_PRG_VERSION` so the fixture's `version` +field changes too). `tests/test_anchor_prg.py::test_phi_prg_known_answer_tests` +pins these values and asserts the version matches the module. ### 3.4 Spec amendment diff --git a/docs/tickets/ticket-000036-t3-per-window-bound.md b/docs/tickets/ticket-000036-t3-per-window-bound.md index 06a36ff..d1b599e 100644 --- a/docs/tickets/ticket-000036-t3-per-window-bound.md +++ b/docs/tickets/ticket-000036-t3-per-window-bound.md @@ -1,6 +1,6 @@ # Ticket #000036 — T3 per-window covert-channel budget bound -**Status:** in progress · Phase 1 (formal derivation + calculator) landed 2026-05-10; **dav1d review returned 2026-05-11** (`RESPONSE_1` + `RESPONSE_2`) → **Tier-1 + Tier-2 both applied 2026-05-11**. Tier-1: recommendation wording → "CANNOT CERTIFY", structured `certification_status` / `b1_model` / `b1_selected` / `model_assumptions` output fields, bool/NaN/inf validation hardening, `g=0` accepted, hard-coded test cwd removed. **Tier-2 = dav1d Option B applied *in v1* (per fox — no v2 fork)**: default `b1_model` is now `max_envelope` = `max(fraction_channels, aggregate_bias)` (genuinely upper-bounding across both interpretations of `g`); `effective_control_v1` / `fraction_channels` / `aggregate_bias` reachable via `--b1-model`; every output reports all three B1 variants + both SNR readings; `CALCULATOR_VERSION` unchanged (`t3-bound-v1-bottou-refinement` — the descriptor names the unchanged B3 term, and `b1_model` echoed in `inputs` disambiguates KAT replays). **KAT fixture regenerated 2026-05-11** (12 entries; `test_t3_bound_known_answer_tests` now runs, not skips). Baseline I_window: 625.87 (effective_control_v1) → **6183.02 (max_envelope default)**. Test suite 53 → 83. **Both prior closure blockers cleared**; remaining = fox's final close-or-iterate call (optionally a dav1d re-review of the envelope formula). Empirical constant-tightening tracked separately under #000043. R2's architectural integrations (Merkle audit-event commitment, SQD canonicalization, CTI clause-lattice, 5F trigger, ForkScore security-risk) out-of-scope here — separate tickets if wanted. +**Status:** **closed · 2026-05-11.** Phase 1 (formal derivation + calculator) landed 2026-05-10; dav1d review (`RESPONSE_1` + `RESPONSE_2`) returned 2026-05-11 → Tier-1 polish (recommendation wording → "CANNOT CERTIFY"; structured `certification_status` / `b1_model` / `b1_selected` / `model_assumptions` output fields; bool/NaN/inf validation hardening; `g=0` accepted; hard-coded test cwd removed) + Tier-2 (dav1d Option B applied *in v1* per fox — no v2 fork: default `b1_model = max_envelope = max(fraction_channels, aggregate_bias)`, genuinely upper-bounding across both interpretations of `g`; `effective_control_v1` / `fraction_channels` / `aggregate_bias` reachable via `--b1-model`; every output reports all three B1 variants + both SNR readings; `CALCULATOR_VERSION` stays `t3-bound-v1-bottou-refinement`, `b1_model` echoed in `inputs` disambiguates KAT replays) + KAT-regen tooling (`scripts/generate_t3_bound_kat.py`, 12-entry fixture, `test_t3_bound_known_answer_tests` runs not skips) all landed 2026-05-11. Baseline I_window: 625.87 (effective_control_v1) → 6183.02 (max_envelope default), `NOT_CERTIFIED_BY_BOUND` at W=10000. Test suite 53 → 83. **Both dav1d closure blockers cleared** (B1 worst-case model; active KAT fixture). All §5 acceptance criteria met. **Continuation:** empirical C_B1/C_B2/C_B3 tightening tracked under **#000043** (parks on v7 deployment data); R2's architectural integrations (Merkle audit-event commitment, SQD canonicalization, CTI clause-lattice, 5F trigger, ForkScore security-risk) are separate tickets if wanted; landing the bound's framing into a v7 plastic-training spec parks on that spec gaining a deployment target. Reviewer artifacts: `~/Downloads/RESPONSE_1_-_ticket-000036-...txt`, `RESPONSE_2_-_...txt`. **Opened:** 2026-05-09 **Scope:** Derive an explicit closed-form upper bound on the covert- channel capacity under threat model T3 (hyperparameter adversary), diff --git a/scripts/generate_phi_prg_kat.py b/scripts/generate_phi_prg_kat.py new file mode 100644 index 0000000..1398667 --- /dev/null +++ b/scripts/generate_phi_prg_kat.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 +"""Regenerate the φ_PRG anchor-map known-answer-test fixture. + +Writes ``bench/fixtures/phi-prg/known-answer-tests.jsonl`` from the +fixed (seed, hard_hash, dim_h) list below. Each entry records the +SHA-256 of the raw HMAC-SHA-512 expansion output (before float +conversion) — the durable contract; the float layout can change +without invalidating the fixture (the byte stream doesn't). + +Run this whenever the φ_PRG algorithm changes — and then bump +``PHI_PRG_VERSION`` in ``arborist/substrate/anchor_prg.py`` so the +fixture's ``version`` field changes too. The KAT regression test +(``tests/test_anchor_prg.py::test_phi_prg_known_answer_tests``) +pins these values and asserts the version matches the module. + +Usage:: + + python -m scripts.generate_phi_prg_kat # from repo root + # or: python scripts/generate_phi_prg_kat.py + +Config list (10 entries): the placeholder seed against zero / all- +ones hashes at several dim_h; a random (seed, hash) pair with two +one-bit-flip variants (avalanche cross-check); block-boundary cases +(dim_h=16 = exactly one HMAC block; dim_h=17 = two blocks with +truncation); a 4096-element counter-rollover stress sample. Note: +at counter=0 the bytes are identical regardless of endianness, so +the single-block (dim_h ≤ 16) entries are endianness-invariant; the +multi-block entries pin the little-endian counter encoding. +""" + +from __future__ import annotations + +import hashlib +import json +import pathlib +import sys + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from arborist.substrate.anchor_prg import _expand, PHI_PRG_VERSION # noqa: E402 + +_FIXTURE = ( + pathlib.Path(__file__).resolve().parents[1] + / "bench" / "fixtures" / "phi-prg" / "known-answer-tests.jsonl" +) + +# (label, seed_hex, hard_hash_hex, dim_h) +_ENTRIES: list[tuple[str, str, str, int]] = [ + ("placeholder-seed/zero-hash/dim_h=1", + "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", + "0000000000000000000000000000000000000000000000000000000000000000", 1), + ("placeholder-seed/zero-hash/dim_h=8", + "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", + "0000000000000000000000000000000000000000000000000000000000000000", 8), + ("placeholder-seed/zero-hash/dim_h=32", + "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", + "0000000000000000000000000000000000000000000000000000000000000000", 32), + ("placeholder-seed/all-ones-hash/dim_h=16", + "ef532720a49159beb6816d98e13a162bac63c531b631bd1adb0fcca96b467ff3", + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16), + ("seed=A/hash=B/dim_h=64", + "14ab2dab0d3cddeaa58ec70632d3ff4f5de2c514004a92144e260dfe384d912e", + "a23cb10b94660f062b467f313a9dc9d84f2dc2748c3627c661082dda7f55e3bb", 64), + ("seed=A/hash=B'/dim_h=64 (one-bit-flip from prior)", + "14ab2dab0d3cddeaa58ec70632d3ff4f5de2c514004a92144e260dfe384d912e", + "223cb10b94660f062b467f313a9dc9d84f2dc2748c3627c661082dda7f55e3bb", 64), + ("seed=A'/hash=B/dim_h=64 (one-bit-flip seed)", + "94ab2dab0d3cddeaa58ec70632d3ff4f5de2c514004a92144e260dfe384d912e", + "a23cb10b94660f062b467f313a9dc9d84f2dc2748c3627c661082dda7f55e3bb", 64), + ("block-boundary/dim_h=16", + "cfd60c2bda64ebcefbb23a5b28d98269c9c4f8b8ac77f6f9ca7a0f4865b10f58", + "724cd966a7bfe78ba802877510ffb90c67f385a1d3135e4e1b8a1b38f744c6da", 16), + ("block-boundary/dim_h=17", + "cfd60c2bda64ebcefbb23a5b28d98269c9c4f8b8ac77f6f9ca7a0f4865b10f58", + "724cd966a7bfe78ba802877510ffb90c67f385a1d3135e4e1b8a1b38f744c6da", 17), + ("stress/dim_h=4096", + "0ddd62c311f88ebe2d4f6cd5d9d1374474dfd645e012043648dd966a71785c95", + "e605ede3d9d0d13c6d7d32c5c424b998677eef0689a0d9f0fa4ebd1bb4307cb9", 4096), +] + + +def build_lines() -> list[str]: + lines = [ + f"# arborist v7 phi_prg known-answer tests — version {PHI_PRG_VERSION}", + "# Regenerated by scripts/generate_phi_prg_kat.py. Records SHA-256 of", + "# the raw HMAC-SHA-512 (little-endian counter-mode) expansion output,", + "# before float conversion. Algorithm change MUST bump PHI_PRG_VERSION", + "# and re-run this script; old runs replay against the old fixture.", + ] + for label, seed_hex, hh_hex, dim_h in _ENTRIES: + seed = bytes.fromhex(seed_hex) + hh = bytes.fromhex(hh_hex) + raw = _expand(seed, hh, dim_h * 4) + lines.append(json.dumps({ + "label": label, + "version": PHI_PRG_VERSION, + "seed_hex": seed_hex, + "hard_hash_hex": hh_hex, + "dim_h": dim_h, + "output_sha256": hashlib.sha256(raw).hexdigest(), + "output_bytes": dim_h * 4, + }, ensure_ascii=False)) + return lines + + +def main() -> int: + lines = build_lines() + _FIXTURE.parent.mkdir(parents=True, exist_ok=True) + _FIXTURE.write_text("\n".join(lines) + "\n", encoding="utf-8") + print(f"wrote {len(_ENTRIES)} KAT entries to {_FIXTURE} (version {PHI_PRG_VERSION})") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/generate_t3_bound_kat.py b/scripts/generate_t3_bound_kat.py new file mode 100644 index 0000000..a9da51e --- /dev/null +++ b/scripts/generate_t3_bound_kat.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python3 +"""Regenerate the T3-bound calculator known-answer-test fixture. + +Writes ``bench/fixtures/t3-bound/known-answer-tests.jsonl`` from the +fixed config list below by running ``t3_bound_bits`` on each and +recording the full output. Run this whenever the calculator's +algorithm changes — and then bump ``CALCULATOR_VERSION`` in +``bench/scripts/t3_bound_calculator.py`` so the fixture's +``calculator_version`` field changes too. The KAT regression test +(``tests/test_t3_bound_calculator.py::test_t3_bound_known_answer_tests``) +pins these values. + +Usage:: + + python -m scripts.generate_t3_bound_kat # from repo root + # or: python scripts/generate_t3_bound_kat.py + +Config list rationale (12 entries): + +- ``small-deployment-§7.1`` / ``medium-deployment-§7.2`` / + ``hardened-deployment-§7.3`` — the worked examples in + ``docs/soft-hash-channel-t3-bound.md`` §7, under the default + ``b1_model=max_envelope``. +- ``extreme-low-g`` / ``tight-window-W=100`` — corner regimes. +- ``tightened-c-b1`` / ``all-constants-tight`` — non-default + C_B* overrides (replay-completeness for the constants path). +- ``b3-floor-regime`` — exercises the B3 floor-at-zero branch. +- ``gradient-fraction-zero`` — the g=0 component-isolation case + (B1=0; B2+B3 still fire). +- ``b1-effective-control-v1-mode`` / ``b1-fraction-channels-mode`` + / ``b1-aggregate-bias-mode`` — explicit ``b1_model`` pins so the + non-default models are regression-guarded. +""" + +from __future__ import annotations + +import json +import pathlib +import sys + +# Allow ``python scripts/generate_t3_bound_kat.py`` from repo root. +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from bench.scripts.t3_bound_calculator import ( # noqa: E402 + CALCULATOR_VERSION, + t3_bound_bits, +) + +_FIXTURE = ( + pathlib.Path(__file__).resolve().parents[1] + / "bench" / "fixtures" / "t3-bound" / "known-answer-tests.jsonl" +) + +# (label, inputs-dict). inputs may include c_b1/c_b2/c_b3/b1_model. +_CONFIGS: list[tuple[str, dict]] = [ + ("small-deployment-§7.1", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024)), + ("medium-deployment-§7.2", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=1000, lr_grid_size=16, window_length=100000, + batches_per_epoch=8192, steps_per_epoch=8192)), + ("hardened-deployment-§7.3", dict( + gradient_fraction=0.01, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=4, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024)), + ("extreme-low-g", dict( + gradient_fraction=0.001, gradient_norm_max=1.0, gradient_noise_stddev=0.5, + lr_decision_interval=500, lr_grid_size=2, window_length=5000, + batches_per_epoch=256, steps_per_epoch=256)), + ("tight-window-W=100", dict( + gradient_fraction=0.05, gradient_norm_max=2.0, gradient_noise_stddev=0.1, + lr_decision_interval=50, lr_grid_size=4, window_length=100, + batches_per_epoch=64, steps_per_epoch=64)), + ("tightened-c-b1", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024, c_b1=0.5)), + ("all-constants-tight", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024, c_b1=0.1, c_b2=0.1, c_b3=0.1)), + ("b3-floor-regime", dict( + gradient_fraction=0.05, gradient_norm_max=100.0, gradient_noise_stddev=0.001, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=4, steps_per_epoch=4)), + ("gradient-fraction-zero", dict( + gradient_fraction=0.0, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024)), + ("b1-effective-control-v1-mode", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024, b1_model="effective_control_v1")), + ("b1-fraction-channels-mode", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024, b1_model="fraction_channels")), + ("b1-aggregate-bias-mode", dict( + gradient_fraction=0.05, gradient_norm_max=1.0, gradient_noise_stddev=0.1, + lr_decision_interval=100, lr_grid_size=8, window_length=10000, + batches_per_epoch=1024, steps_per_epoch=1024, b1_model="aggregate_bias")), +] + + +def build_lines() -> list[str]: + lines = [ + f"# T3 bound calculator known-answer tests — version {CALCULATOR_VERSION}", + "# Regenerated by scripts/generate_t3_bound_kat.py. Algorithm change", + "# MUST bump CALCULATOR_VERSION and re-run this script; old runs", + "# replay against the old fixture (do not delete history).", + ] + for label, inp in _CONFIGS: + r = t3_bound_bits(**inp) + lines.append(json.dumps({ + "label": label, + "calculator_version": r["calculator_version"], + "b1_model": r["b1_model"], + "inputs": r["inputs"], + "expected_total": r["I_window_bits_upper_bound"], + "expected_b1": r["B1_contribution"], + "expected_b1_selected": r["b1_selected"], + "expected_b1_fraction_channels": r["B1_fraction_channels"], + "expected_b1_aggregate_bias": r["B1_aggregate_bias"], + "expected_b1_effective_control_v1": r["B1_effective_control_v1"], + "expected_b2": r["B2_contribution"], + "expected_b3": r["B3_contribution"], + "expected_snr_grad": r["snr_grad"], + "expected_snr_per_channel": r["snr_per_channel"], + "expected_certification_status": r["certification_status"], + }, ensure_ascii=False)) + return lines + + +def main() -> int: + lines = build_lines() + _FIXTURE.parent.mkdir(parents=True, exist_ok=True) + _FIXTURE.write_text("\n".join(lines) + "\n", encoding="utf-8") + print(f"wrote {len(_CONFIGS)} KAT entries to {_FIXTURE} (version {CALCULATOR_VERSION})") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/test_t3_bound_calculator.py b/tests/test_t3_bound_calculator.py index 730cfbf..e8a2326 100644 --- a/tests/test_t3_bound_calculator.py +++ b/tests/test_t3_bound_calculator.py @@ -572,7 +572,7 @@ KAT_FIXTURE = ( @pytest.mark.skipif( not KAT_FIXTURE.exists(), - reason="KAT fixture not yet generated", + reason="KAT fixture not yet generated; run scripts/generate_t3_bound_kat.py", ) def test_t3_bound_known_answer_tests(): """Replay the pinned t3-bound/known-answer-tests.jsonl fixture;