omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#271 (code only)
Apply it
git checkout -b patch-2af34b61b8e217a0 3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/2af34b61b8e217a0.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
271
dd4054918bba67cb83f2c4d0acb4978994349131
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":271,"url":"https://github.com/aien-dev/omega-mv/pull/271","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:04Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 182fcb709..bdf2bded7 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "e555caa4954d4dde3e499f37df8c9af2efc41cf349dc8096273b0e062dd07a01", - "last_commit": "00f54b6", + "digest": "28840d48abe117c5e29a9fc7f352475deee0c546157edfcb09ebe61a1d364da1", + "last_commit": "cff342b", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "566d430880c69205dd89c06d645f5030442c526a97ce3e2c386276c282792e9f", - "last_commit": "1fb209a", + "digest": "25dc918988768c936cdd41410a68c18df3c9acf7cd129812bc030c6a070cc67c", + "last_commit": "cff342b", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "8e9dd6f8117c615273c291427db9ad14abaa907bc05741145bb9892706f68efa", - "last_commit": "1fb209a", + "digest": "839e2dd4f0aa249d7b552e9d3eafdf3ae94f13a4a4c17c3bed1ce603a07659d8", + "last_commit": "cff342b", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "e96dec62800dc6dae755ebe10eacfedcb0959e29e00a3540284cb585f8f9716f", - "last_commit": "1fb209a", + "digest": "7f8951ba438340bc36223d5722c1055efd8fdd01efe3a43bcf8f2e61ce68dea7", + "last_commit": "cff342b", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:0d2e5faff3d6519826a4922d03e6258a03c42792caee91a6a4bc0d6cba17dfa2", - "compiled_at": "2026-10-04T18:45:19Z", + "children_root": "sha256:f5cd48815a1e17f58c9a56231edbc7e6d4cb19045afc1f03cba9a5c92b20d6f4", + "compiled_at": "2026-10-04T18:54:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:b0b3f7ca6dda9afa4b09fa49f49e397ecfce7737dfd34ea426bcc2b905364232", - "evidence_files": 1288, - "evidence_root": "sha256:5e3386545c3742835ee2e90f3c06e6bfeeb9725fb51664e6c25cb59822356642", - "files": 3043, - "generated_at_commit": "393fa208a1a662de5ccb6c877d8d7c8b22fc1bf7", - "source_tree": "sha256:499774e2016130a9904f3fda0dbe4c7c852fa5b6109b60f44bde780b49cb1eb1" + "digest": "sha256:16e57dc9bcda740ae51c4709c0fd61944bf2690f670206423904038ad95a7bb7", + "evidence_files": 1289, + "evidence_root": "sha256:748eb7ce0bd377815585505582eb76663350e35af3d876a488897ceea120ba3f", + "files": 3049, + "generated_at_commit": "cff342b400ec0bc6b2b68e1004212872d490bb07", + "source_tree": "sha256:63e49ae9974a0c96e66aef44ec14d08e19354be518c670f04e2b6d1dab924b6f" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 7d2524369..1b27715f9 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -69,8 +69,8 @@ "purpose": "Directory docs/crumbs. Purpose not yet described by a human." }, { - "digest": "854ab7030882cb50548bdff57507ba8998c474383487f8a0ec7c500388942c39", - "last_commit": "3bebcc6", + "digest": "e9baaaeec04be41a57c4a2d6a540b01a682c70d4db96cb4af4e1a9912a133aa9", + "last_commit": "00f54b6", "name": "dual", "purpose": "DUAL (ARCH-0031) documents in omega: applicability experiment method and findings (APPLICABILITY" }, @@ -117,15 +117,15 @@ "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:8c5aa863d287effc2ff5df57b7f029cac75a1b0e7b5c71c9fd59c45e308a1e10", - "compiled_at": "2026-10-04T18:38:56Z", + "children_root": "sha256:7010ef7e3e982e3828bfeb2ff1a1fe5c8cfeceb6aef820c34c175fe1d0208734", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:e555caa4954d4dde3e499f37df8c9af2efc41cf349dc8096273b0e062dd07a01", + "digest": "sha256:28840d48abe117c5e29a9fc7f352475deee0c546157edfcb09ebe61a1d364da1", "evidence_files": 26, "evidence_root": "sha256:f58236184eadc5f8c8f95499127a9f43c2f4dc702ddca267853d8c3dee0690f1", "files": 100, - "generated_at_commit": "3bebcc68321fcc876b88df129f41d6190411a926", - "source_tree": "sha256:cdc21061ff898378059abf06f18c234da5dee85662b05f4c811fd79ac6db7fc0" + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:769aaad946a229b703b2def73085c781284fb13666223c34d2fdb16020a3f11b" }, "owner_lane": "", "provenance": { diff --git a/docs/dual/.crumb b/docs/dual/.crumb index dfd9f2a01..42ec6c5ce 100644 --- a/docs/dual/.crumb +++ b/docs/dual/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T18:38:56Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:854ab7030882cb50548bdff57507ba8998c474383487f8a0ec7c500388942c39", + "digest": "sha256:e9baaaeec04be41a57c4a2d6a540b01a682c70d4db96cb4af4e1a9912a133aa9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 2, - "generated_at_commit": "3bebcc68321fcc876b88df129f41d6190411a926", - "source_tree": "sha256:b4f60491f7fce8800fc6c21c9ed83cc4bc6b98ed72bfd5536325f2d2245f0df0" + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:4920e1c6a40a59b4a821be52bcfffd40a7b179228517de9afa28f78126089da7" } }, "name": "dual", diff --git a/docs/dual/DUAL_RECORDS.md b/docs/dual/DUAL_RECORDS.md index 06dd4ad92..7bd813d2b 100644 --- a/docs/dual/DUAL_RECORDS.md +++ b/docs/dual/DUAL_RECORDS.md @@ -13,6 +13,7 @@ nothing under `src/runtime/` includes it). DUAL is scarcity information. It neve | recommendation | 5 | `omega.dual.recommendation.v1` | dig decision_site, dig price_vector, u64 generation, u32 n_candidates, n x dig, u32 actual, u32 recommended, u32 n_consequences, n x (u32 resource_id, u32 unit, f64 predicted, f64 predicted_sd, u32 has_measured, f64 measured, f64 error), u32 authority (= 0) | | outcome | 6 | `omega.dual.outcome.v1` | dig recommendation, dig before_vector, dig after_vector, u32 target_resource_id, u64 generation_before, u64 generation_after, f64 lambda_before, f64 lambda_after | | site | 7 | `omega.dual.site.v1` | u32 site_id, 32 bytes name (printable ASCII, NUL padded), dig owner, u32 max_alternatives | +| trace | 8 | `omega.dual.trace.v1` | u32 trace_kind, u64 seed, u32 n_ticks, n x (u64 generation, f64 estimate, f64 uncertainty, u32 calibrated, u32 evidence_verified, u32 regime_change) (DUAL-1b; replay digest domain `omega.dual.replay.v1` over controller_id, trace digest, u32 n, n state digests) | Every encoding starts with `u8 kind, u8 version`. Doubles are IEEE-754 binary64 bits; -0.0 is written as +0.0; NaN and Inf are refused before encoding. Digest = SHA-256(domain || 0x00 || encoding). Decoders refuse a wrong kind byte, a wrong version, @@ -29,4 +30,4 @@ truncation and trailing bytes, then run the same validation as the constructor. - Recommendation: `authority` must be `NONE`; indices in range; consequences in ascending resource order; `error` must equal the recomputed `(measured - predicted) / predicted_sd`; an unmeasured row carries zero. - Controller: eta >= 0, rho in [0,1], k_sigma >= 0, max_age >= 1, cadence >= 1, per-resource lambda_max > 0, ascending ids. Any parameter change changes `controller_id`. -Status: DUAL-0a, DUAL-0b (rx_dual_bind.h: binding to est_* records, evidence verification, FRESH/STALE/FROZEN/UNCALIBRATED/REFUSED) and DUAL-1a (rx_dual_update.h: reference update) IMPLEMENTED; `DUAL_CONSTRAINT_CONTRACT = PASS` on host (evidence/DUAL/0b-1a-binding-controller); `DUAL_PRICE_REFERENCE` waits for DUAL-1b replay. +Status: DUAL-0a, DUAL-0b (rx_dual_bind.h: binding to est_* records, evidence verification, FRESH/STALE/FROZEN/UNCALIBRATED/REFUSED), DUAL-1a (rx_dual_update.h: reference update) and DUAL-1b (rx_dual_replay.h: digest-bound traces, kind byte 8 `omega.dual.trace.v1`, deterministic replay with replay digest `omega.dual.replay.v1`, perf-CSV import; target `test-dual-replay`) IMPLEMENTED; `DUAL_CONSTRAINT_CONTRACT = PASS` on host (evidence/DUAL/0b-1a-binding-controller); `DUAL_PRICE_REFERENCE = PASS` on host over 12 synthetic scenarios and 6 recorded R15 counter traces with a pre-registered negative control (evidence/DUAL/1b-replay). Every replayed price is UNCALIBRATED (EST-3 has not passed for any signal): no price may influence production. Next: DUAL-2 (Pareto integration) needs EST-5 and EST-7. diff --git a/evidence/DUAL/1b-replay/receipt.md b/evidence/DUAL/1b-replay/receipt.md new file mode 100644 index 000000000..ab12367ec --- /dev/null +++ b/evidence/DUAL/1b-replay/receipt.md @@ -0,0 +1,212 @@ +# DUAL-1b receipt: deterministic replay of the reference controller over digest-bound traces (ADR 0031 s5, s8 DUAL-1) + +Gate: `DUAL_PRICE_REFERENCE`. Host-only, no GPU, no production influence. + +EVERY input and EVERY replayed price in this receipt is UNCALIBRATED: EST-3 has not passed for any signal on main, no +calibration receipt exists, every synthetic trace carries calibrated = 0, and every recorded trace carries calibrated = 0. +No price computed here may influence any production decision. The recorded traces are perf cycle counters, which are +exact counts but are not calibrated estimates of any resource pressure; they are used only as input streams. + +| Field | Value | +|---|---| +| repository | aien-dev/omega | +| base commit | 0899c412c33eb37a0012170de7604e4dbd4bf963 (branch dual/0b-1a-binding-controller, PR #267, on dual/0a-records PR #266, on main after the AGPL relicense 559f6e5; the pre-registration was first committed on the pre-rebase base 0b3dcc0efe4210e533fd5cfcc675aada371c9fc9 and rebased unchanged) | +| pre-registration commit | the commit that adds this file with the sections "Pre-registered" filled and "Results" empty | +| candidate commit | filled in the Results section | +| compiler | cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 | +| flags | EST_CFLAGS (-std=c11 -Wall -Wextra -Werror -pedantic -O2 -ffp-contract=off -fno-fast-math) and EST_ASAN | +| host | Spark aarch64 Linux, host-only | + +## Pre-registered: trace format +Kind byte 8, version 1, digest domain `omega.dual.trace.v1`: u32 trace_kind, u64 seed, u32 n_ticks, then per tick +u64 generation, f64 estimate, f64 uncertainty, u32 calibrated, u32 evidence_verified, u32 regime_change. Little-endian, +-0.0 written as +0.0, NaN/Inf refused. Trace digest = SHA-256(domain || 0x00 || encoding). Generators use xorshift64* +from the declared seed. Replay log = the RxDualConstraintState digest of every tick, in order; replay digest = +SHA-256("omega.dual.replay.v1" || 0x00 || controller_id || trace_digest || u32 n || the n state digests). +Recorded traces: trace_kind RECORDED, seed 0, estimate = the perf cycle count of one (CPU, event) series, uncertainty 0, +calibrated 0, evidence_verified 1, regime_change 0, generation = row index + 1; the raw CSV bytes are SHA-256 digested +and listed below. The R15 machine-perf.csv and preflight-perf.csv files contain ONLY perf-stat cycle counters +(armv8_pmuv3_*/cycles/ per CPU and smmuv3_pmcg_*/cycles/); they contain no thermal, power or clock-frequency column, +so the per-CPU cycles-per-second series is the only stream available and is used as a clock-activity proxy. + +## Pre-registered: common parameters +- Resource: unit BYTES (synthetic) or DIMENSIONLESS (recorded; the unit registry has no cycles unit), scale 1000 (synthetic), 1e9 (recorded). +- Budget: 5000 (synthetic, except where stated); 1e9 cycles per second (recorded). +- Class SOFT. budget_contract = fixed stand-in digest 0x22 repeated (0x23 repeated for the second chain of scenario 7). +- Reference controller: eta 0.1, rho 0.05, k_sigma 2.0, max_age 4, cadence 1, lambda_max 10 per resource; n = 1 except + scenario 5/6 (n = 2, ids 1, 2) and scenario 10 (n = 32, ids 1..32). +- Hostile controller (negative control): eta 5.0, rho 0.0, k_sigma 2.0, max_age 4, cadence 1, lambda_max 10, n = 1. +- now_generation = tick generation (no staleness is injected in these scenarios). n_ticks = 200 unless stated. +- K (recovery ticks) = ceil(ln(100) / (-ln(1 - rho))) = ceil(89.78) = 90 for rho 0.05. + +## Pre-registered: scenarios (seeds are part of the trace identity) +| # | Scenario | Trace kind | Seed | Definition | +|---|---|---|---|---| +| 1 | constant low load | 1 | 0x1001 | estimate 4000, sd 100, all ticks | +| 2 | single binding constraint | 2 | 0x1002 | estimate 5500, sd 100, all ticks | +| 3 | abrupt step load | 3 | 0x1003 | estimate 4000 for t < 100, 6000 for t >= 100, sd 100 | +| 4 | square-wave congestion | 4 | 0x1004 | period 50: 25 ticks at 6000 then 25 at 4000, 4 periods, sd 100 | +| 5 | competing: memory | 5 | 0x1005 | resource 1: estimate 6000, sd 50 (one controller n = 2 prices both chains) | +| 6 | competing: latency | 6 | 0x1005 | resource 2: estimate 4900, sd 50 (inside the deadband: pressure -0.1, band 0.1) | +| 7 | sudden capacity reduction | 7 | 0x1007 | estimate 4500, sd 100; budget 5000 for t < 100 (chain A), 3000 for t >= 100 (chain B, new budget_contract, new chain at tick 0). A budget is declared by a contract (budget_contract), so a budget change is a new contract and a new constraint chain; the engine does not mutate a budget inside a chain | +| 8 | workload regime change | 8 | 0x1008 | estimate 5500, sd 100; regime_change = 1 for 80 <= t < 120 | +| 9 | bursty queue arrivals | 9 | 0x1009 | estimate 4200 + burst; each tick with probability 1/8 (xorshift) a burst uniform in [800, 2400]; sd 100 | +| 10 | high branch fan-out | 10 | 0x100A + r | 32 resources (r = 0..31), each a constant estimate 5500 + offset_r, offset_r uniform in [-50, 50] from seed 0x100A + r; sd 100; one controller n = 32 | +| 11 | KV saturation approach | 11 | 0x100B | estimate 3000 + 4000 * t / 199 (crosses budget + band = 5200 near t = 109.5), sd 100 | +| 12 | recovery after pressure | 12 | 0x100C | estimate 6000 for t < 100, 5000 for t >= 100 (inside the band: leak only), sd 100 | +| 13 | recorded R15 | 13 | 0 | machine-perf.csv CPU0 armv8_pmuv3_0/cycles/ and CPU7 armv8_pmuv3_1/cycles/; preflight-perf.csv CPU7 armv8_pmuv3_1/cycles/; both R15 runs (6 traces) | + +## Pre-registered: measures and thresholds (frozen before the first replay run) +Reversal count R(x) of a series x: number of sign changes between consecutive nonzero first differences. +| Id | Measure | Threshold | Applies to | +|---|---|---|---| +| M1 | hard bounds: lambda finite, >= 0, <= lambda_max at every tick | every tick | all scenarios, both controllers | +| M2 | constant-load tail: R(lambda) over the final 50 ticks | = 0 | 1, 2, 3, 5, 6, 7 (chain B), 8, 10 (each chain), 12 | +| M3a | square wave reversals: R(lambda) over the whole trace | <= 2 * periods = 8 | 4 | +| M3b | square wave amplitude: max(lambda) - min(lambda) over the final full period (t 150..199) | <= 0.25 * lambda_max = 2.5 | 4 | +| M4 | step response: lambda(t+1) >= lambda(t) - 1e-12 * (1 + lambda(t)) for every t at or after the step | all post-step ticks | 3 (t >= 100), 7 chain B (all), 11 (t >= first tick with estimate > 5200), 2 (all) | +| M5 | recovery: lambda[t_off - 1 + K] <= 0.01 * lambda[t_off - 1], t_off = 100, K = 90 | holds | 12 | +| M6 | regime: every tick with regime_change = 1 is FROZEN with lambda, estimate and generation equal to the previous tick | every such tick | 8 | +| M7 | uncalibrated: every tick with calibrated = 0 has state UNCALIBRATED (never FRESH) | every tick | all scenarios (all ticks are calibrated = 0), recorded | +| M8 | no input-free oscillation: R(lambda) <= R(estimate) + 1 per chain | holds | all scenarios incl. recorded, both controllers | +| M9 | competing: final lambda(memory) > 0 and final lambda(latency) = 0 | holds | 5, 6 | +| M10 | fan-out spread: max - min of the 32 final lambdas | <= 0.25 (eta * max band-shifted gradient spread / rho = 0.1 * 0.1 / 0.05 = 0.2, plus margin) | 10 | +| M11 | KV: lambda = 0 for every tick with estimate <= 5200 | holds | 11 | +| D1 | determinism: two replays of the same trace with the same controller give identical replay digests | identical | all | +| D2 | golden: trace digest and replay digest of scenarios 2 and 4 equal hex constants committed in the test (cross-process) | identical | 2, 4 | +| D3 | tamper: flipping one bit of the encoded estimate at tick 17 changes the trace digest and the replay digest | both differ | 2 | +| D4 | calibrated variant: scenario 2 with calibrated = 1 (stand-in digest, a FORMAT exercise, not a calibration claim) gives identical lambdas, FRESH states, and a different replay digest | holds | 2 | +| H1 | hard constraint through the replay entry point (class INVARIANT, class UNDECLARED) | refused, no record | 2 | + +Expected analytic values for the reference controller (from the closed form in tests/dual/dual_ref.c): scenario 2 fixed +point eta*g/rho = 0.1*0.3/0.05 = 0.6; scenario 3 post-step fixed point 1.6; scenario 4 peak per period +1.6 * (1 - 0.95^25) = 1.156 (M3b margin 2.5 - 1.16); scenario 7 chain B fixed point 2.6; scenario 12 peak 1.6 * (1 - 0.95^100) = 1.59. + +Note on M3a/M8 (stated before running): for the update lambda' = clip((1 - rho) lambda + eta g) the first difference +d(t) = eta g(t) - rho lambda(t) obeys d(t+1) = (1 - rho) d(t) + eta (g(t+1) - g(t)) while unclipped, so within a run of +same-sign changes of g the sign of d changes at most once, and clipping cannot add a reversal under monotone g. Hence +R(lambda) <= R(g) + 1 <= R(estimate) + 1 for EVERY valid controller, including the hostile one. Reversal counts therefore +cannot distinguish a good controller from a bad one on a noise-free wave; the discriminating oscillation measure is the +amplitude bound M3b, which the hostile controller is expected to fail. + +## Pre-registered: negative control (expected outcomes) +| Controller | Scenario | Expected | +|---|---|---| +| hostile (eta 5.0, rho 0.0) | 4 square wave | M1 PASS, M3a PASS (2 per period), M3b FAIL: lambda slams to lambda_max within 3 ticks of each rising edge and to 0 within 3 ticks of each falling edge, amplitude 10 > 2.5. Gate verdict for this configuration: FAIL | +| reference | 4 square wave | M1, M3a, M3b, M8 PASS | + +Verdict rule: `DUAL_PRICE_REFERENCE = PASS` only if every applicable measure passes for the reference controller on every +scenario AND the negative control fails M3b as declared. Otherwise FAIL with the numbers. A threshold is never changed +after this commit; a surprise is recorded as FAIL. + +## Results +(filled after the run; nothing above this line changes) + +| Field | Value | +|---|---| +| pre-registration commit | d51d418577d14f5f8d1e922051597bfb089461d4 (receipt sections above, no replay code; b0626246f3b6a03326420fafba502c3b5398c4c8 before the base rebase, same content) | +| candidate commit | this commit (branch dual/1b-replay) | +| compiler | cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, aarch64 | +| reference controller_id | bcf4285a4830663a1c9f0548a0bd9fe1c96ca9852a8c90d7e45b13f56d820ec4 (eta 0.1, rho 0.05, k_sigma 2, max_age 4, cadence 1, n 1, lambda_max 10) | +| competing controller_id (n = 2) | e4a8317c4e867e8cb49f28aa8275d2b2da8abf910575d88f5bb89edaffc41b11 | +| fan-out controller_id (n = 32) | b1af2f7e5df739fdb93534ac1ace836ade213f21e03bb20d1b28abad4bcddb27 | +| hostile controller_id | 1d08ce016bbbc47a506145813cd59d5f57bd687929f0a89ad3e1dec4d4d6a1c1 (eta 5.0, rho 0.0, k_sigma 2, max_age 4, cadence 1, n 1, lambda_max 10) | + +### Commands and results (run from the repository root) +| Command | Result | +|---|---| +| make dual-purity | PASS: rx_dual.o rx_dual_bind.o rx_dual_update.o rx_dual_replay.o reference no authority, promotion or process symbol (nm -u of rx_dual_replay.o: memcmp memcpy memset strlen, rx_dual_*, sha256_*, stack protector only; no stdio: the CSV importer takes bytes the test read) | +| make test-dual-replay | plain: 456 checks, 0 failures, PASS; ASan/UBSan: 456 checks, 0 failures, PASS | +| make test-dual | test-dual-types 14107/1667, test-dual-bind 85/85, test-dual-update 355348/35837, test-dual-replay 456/456 checks, 0 failures | +| make all / make test | unchanged: DUAL is not part of them; nothing under src/runtime/ changed (`git diff --quiet HEAD -- src/runtime` clean) | + +### Trace digests and replay digests (reference controller unless marked HOST) +| Chain | n | trace digest (omega.dual.trace.v1) | replay digest (omega.dual.replay.v1) | final lambda | +|---|---|---|---|---| +| 1 constant low | 200 | 0e0c2f9f833a47158fa885af34d2fc5cbea250124f891e56af31c7cf0d7a4ab1 | e3c9fbfc4e73e1054d8739f47c7ad56a5b97ad6efe57b2ef12ece689e67e32d2 | 0 | +| 2 binding (golden) | 200 | f9c3944ab952dd5e2beefebbb2499f98ec7be28157b571a47e406ca40e4d93fb | 46345146d1213fdd2888fd0ace3f7ab31287565fd42adf950ad58edeb11bf5ae | 0.599978968 | +| 3 step | 200 | 966e602e16db9f3a61187f0135c45657dd7b3f22b22d910e3eda9ede19eed175 | 3539d9d349ee9f789a833d95a011351b99807d3f85896996e35e6defa255f489 | 1.59052715 | +| 4 square (golden) | 200 | 0a463faef33ab2a59da6c854eabfa2ec9e189f3e5e6b00f07c773e490e52edd5 | 64a30ce77dc7decfedae15c6c8e8e66fa75693df049916deab7ca2abbfaaa888 | 0 | +| 4 square HOST | 200 | 0a463faef33ab2a59da6c854eabfa2ec9e189f3e5e6b00f07c773e490e52edd5 | 34ac4fde2f4f92a676c619201e0cea590a06522bc1a3d5e40040c4d4bd6b46bb | 0 | +| 5 competing memory | 200 | c988bffd193292402cd853f8453454f128bbc7c5a0b6c89e50fad8df5b0a4c6a | 46afcfa907a0393ccbff84ec62d844fda2af0aa7739f7fbbccdf70b0cde60064 | 1.79993691 | +| 6 competing latency | 200 | 92298884577ea85d0a133da92a2ae73b49f78e189df585af03bd8dbf6016abe4 | 65379174831235554c91a3194dd32a43a3285f5046b4f2096e29a1c22f5a287a | 0 | +| 7A capacity (budget 5000) | 100 | e3f2c5e796ede019b78fcc1b3ff584a2a8028236332d361b356fe0883c3da337 | 714c2438d01a68bf180af03906c50dbac6168757ea25ca41de9c4de85e580325 | 0 | +| 7B capacity (budget 3000, new contract) | 100 | 7f7d9ccdddf47321a545c49340eb48d32d0d595b695e82cdca0995be2a86a28c | 5a041ac1ff7390cd63428b87bcfd0380dec105ecbd48563f94965bf4a81aed62 | 2.58460662 | +| 8 regime change | 200 | 0b17c31469fd478c24ec6acf2318caadab124045431c986af6e985fa50c1245b | 8a4eabfde2d1d51e2457fba6c5f9174c06b320c5c6e22f85aebd0ba2317d0be7 | 0.599836345 | +| 9 bursty (26 bursts) | 200 | 30162f09ee84b7b1bd56a88f9f72426e3c9a476d90201f921c92e63cee1271bd | 723b8d1eb96e1647414cd376f79d87c4f7c035048388a9e41d2d46222caf3b3d | 0.120999564 | +| 10 fan-out r = 0 (of 32; all 32 digests in the test output) | 200 | df3ebf9e65ea2c9bb6e7df3e372edaf43f876f6c2d85916e38d84c21f7ee0d68 | 4746925bb0c1c76b8b2b87ae155644b4b13c009622df3f06f71837bb7ed99e07 | 0.538262214 | +| 11 KV saturation | 200 | f0308e3fa8e549f4a18a3fb57535ade611587f5720582ac057cd60698ae5794a | e49c22a48ec0d7e6dff654017a39e138db6aeff8dd426d2ad3730973ed39510d | 2.84391271 | +| 12 recovery | 200 | dca6d3cb19658bb4b2f9b33386c3c77a70d765e7bc81c994f4488565d1204826 | 356cd1f1f68d3ac3763fec9d9c6efa27e1e90b3ff48a9cab7dfb357d1b7bc5f8 | 0.00941676249 | +| 13 R15 020536Z machine CPU0 | 1976 | 8f5f9b1f92064ba93ce22685a55d6403645a53e9a0397f04db6dc868f5bd9345 | 175bd2cc5950145e7f0b371a8a0994d2fc22bc1c489eca470e942ad15af64bea | 0 | +| 13 R15 020536Z machine CPU7 | 1976 | fe2ca66182ae4f8891165d5b44a635e467efb5d30bd4e77d418dc76cd7869d94 | 908ed8bdef115365fb74392dfe160c7668cf80e31ddc271838fbd16f43333e3a | 0 | +| 13 R15 020536Z preflight CPU7 | 20 | 3a1cf368c4b0f376ecfea21864ce7f39a3cce12ffa96c457212d9e48a4af13c8 | 0e37c660a3e004755517657f85012b11fe5ec8306bb9acd8f6fd1a913cc30373 | 3.70425459 | +| 13 R15 025735Z machine CPU0 | 2033 | 83e92c15ae0cd3a2cc7b747559fbcd3786619180697d1721c910f435b098777b | 2f5387d79988db0bfdbf8272ba28bba19dcad766c79252d7af817e312a168725 | 0 | +| 13 R15 025735Z machine CPU7 | 2033 | 12ee87a420de376a852dbebda58a4ac8a4639e7523545dcc5ddda8c537763b7e | fbbc9e42ee76495260f91495efbcd27620de7a8d1264aeb5bbe744b717ab1e96 | 0 | +| 13 R15 025735Z preflight CPU7 | 20 | 5a262e15b8d85ade33f4d028a052292ed8e26946b837e196cf5e1b30a605c586 | 2b3f0da6e2c063a6594adba2b24b2474e9d3fe2fe04628f82889e4f402c6fa1d | 3.70285023 | + +Recorded raw files (SHA-256 of the bytes as read; `SUM` = equals the entry in that run's own SHA256SUMS): +| File | bytes | sha256 | SUM | +|---|---|---|---| +| evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon/machine-perf.csv | 5254065 | 22663a037a7f557634c1786e0427a669925c99d0e6c436309139a362e94a43af | PASS | +| evidence/R15/raw/20260929T020536Z-ad8e1f2ea4e4-silicon/preflight-perf.csv | 1469 | 2078a6412767ca5bbae118fe22486d0846e5e25b83cbd73231468e7736e70943 | not listed in SHA256SUMS; recorded here only | +| evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon/machine-perf.csv | 5400978 | 36e0cd936e783a054c82a47444219f1266ddb47a1ae33a14fadaaaac23adb3b2 | PASS | +| evidence/R15/raw/20260929T025735Z-3e9e53be3358-silicon/preflight-perf.csv | 1469 | ff14887fd894d5902f948942eecfb1f936b598a5f9986f5beb2c544244b8cf51 | not listed in SHA256SUMS; recorded here only | + +Recorded-trace facts: machine CPU0 cycles exceed the 1e9 budget on 53 / 60 of 1976 / 2033 ticks (bursts), CPU7 on 669 / 694 +ticks; the preflight CPU7 series is above budget on all 20 ticks. These are counter streams, not calibrated estimates. + +### Measures (reference controller; thresholds frozen above) +| Chain | M1 | M2 | M3a | M3b | M4 | M5 | M6 | M7 | M8 (R lambda / R est / bound) | other | +|---|---|---|---|---|---|---|---|---|---|---| +| 1 constant low | PASS | PASS (0) | | | | | | PASS | PASS 0/0/1 | | +| 2 binding | PASS | PASS (0) | | | PASS | | | PASS | PASS 0/0/1 | A PASS final 0.599978968 vs 0.6; D2 PASS; D3 PASS; D4 PASS; H1 PASS | +| 3 step | PASS | PASS (0) | | | PASS (from 100) | | | PASS | PASS 0/0/1 | lambda[99] = 0, lambda[100] > 0 | +| 4 square | PASS | | PASS 7 <= 8 | PASS 1.15617668 <= 2.5 | | | | PASS | PASS 7/6/7 | D2 PASS | +| 5 memory | PASS | PASS (0) | | | | | | PASS | PASS 0/0/1 | M9 PASS: memory 1.79993691 > 0 | +| 6 latency | PASS | PASS (0) | | | | | | PASS | PASS 0/0/1 | M9 PASS: latency 0 | +| 7A capacity | PASS | | | | | | | PASS | PASS 0/0/1 | lambda 0 throughout | +| 7B capacity | PASS | PASS (0) | | | PASS (from 0) | | | PASS | PASS 0/0/1 | generation 101..200, tick 0..99 | +| 8 regime | PASS | PASS (0) | | | | | PASS | PASS | PASS 0/0/1 | ticks 80..119 FROZEN, lambda/estimate/generation held; UNCALIBRATED resumes at 120 | +| 9 bursty | PASS | | | | | | | PASS | PASS 32/38/39 | | +| 10 fan-out (32 chains) | PASS x32 | PASS (0) x32 | | | | | | PASS x32 | PASS 0/0/1 x32 | M10 PASS: finals in [0.501392531, 0.697240739], spread 0.195848208 <= 0.25 | +| 11 KV | PASS | | | | PASS (from 110) | | | PASS | PASS 0/0/1 | M11 PASS: lambda 0 while estimate <= 5200; first crossing tick 110 | +| 12 recovery | PASS | PASS (0) | | | | PASS K=90: lambda[189] = 0.0157277126 <= 0.0159052715 | | PASS | PASS 1/0/1 | peak 1.59052715 | +| 13 020536Z machine CPU0 | PASS | | | | | | | PASS | PASS 81/1238/1239 | D1 PASS | +| 13 020536Z machine CPU7 | PASS | | | | | | | PASS | PASS 245/1175/1176 | D1 PASS | +| 13 020536Z preflight CPU7 | PASS | | | | | | | PASS | PASS 0/9/10 | D1 PASS | +| 13 025735Z machine CPU0 | PASS | | | | | | | PASS | PASS 91/1269/1270 | D1 PASS | +| 13 025735Z machine CPU7 | PASS | | | | | | | PASS | PASS 197/1228/1229 | D1 PASS | +| 13 025735Z preflight CPU7 | PASS | | | | | | | PASS | PASS 0/13/14 | D1 PASS | + +D1 (two replays, identical replay digest) PASS on every chain above. D2: golden hex constants for scenarios 2 and 4 are +committed in tests/dual/test_dual_replay.c and matched in both the plain and the ASan process. Trace codec: exact round +trip, kind byte 2 / version 2 / truncation / trailing byte refused, NaN / negative uncertainty / flag > 1 / kind 0 / n 0 / +n > 2048 refused, -0.0 digests as +0.0. Importer: "<not counted>" refused (RX_DUAL_ERR_ENCODING), no matching row +refused, non-matching event rows skipped, generation = row index + 1. + +### Negative control +| Controller | Scenario | M1 | M3a | M3b | M8 | Verdict for this configuration | +|---|---|---|---|---|---|---| +| hostile eta 5.0, rho 0.0 | 4 square | PASS (max 10 = clip) | PASS 7 <= 8 | FAIL peak-to-peak 10 > 2.5 | PASS 7/6/7 | FAIL, exactly as declared | +| reference | 4 square | PASS | PASS 7 | PASS 1.156 | PASS 7/6/7 | PASS | + +Every declared expectation for the negative control held: the hostile controller is bounded (clip works), reverses no more +often than the reference (the pre-stated theorem: reversal counts cannot exceed R(estimate) + 1 for any valid controller), +and fails the amplitude bound by slamming between 0 and lambda_max. + +### Verdict +`DUAL_PRICE_REFERENCE = PASS` on host: every pre-registered measure passed for the reference controller on every scenario and +on every recorded trace, and the negative control failed M3b as declared. No threshold was changed after the +pre-registration commit. + +Scope and limits (stated, not hidden): +- ALL inputs are UNCALIBRATED. Every replayed state is UNCALIBRATED (or FROZEN on regime ticks); none is FRESH except in the + D4 format exercise, which uses a stand-in digest and makes no calibration claim. No price from this gate, and no price from + this module, may influence any production decision until EST-3 passes for the signal and the later DUAL gates pass. +- The recorded traces are perf cycle counters (the R15 CSVs contain no thermal, power or clock-frequency column); the budget + 1e9 cycles per second is a pre-registered stand-in, not a declared resource budget of any contract. +- The regime-change scenario exercises the FROZEN path with an injected flag; it does not establish that EST-9 innovation is a + usable regime detector (ADR 0031 s5.3 leaves that open). +- Capacity reduction is modelled as a new constraint chain under a new budget_contract because a budget is declared by a + contract; the price history of the old chain is not carried into the new one. +- Host-only. No GPU, no runtime linkage, no src/runtime change. diff --git a/mk/.crumb b/mk/.crumb index c9b3fb4ad..3f9d87990 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T18:45:18Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:566d430880c69205dd89c06d645f5030442c526a97ce3e2c386276c282792e9f", + "digest": "sha256:25dc918988768c936cdd41410a68c18df3c9acf7cd129812bc030c6a070cc67c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 52, - "generated_at_commit": "00f54b6b19e5e64e8947aea2890e688511d897a8", - "source_tree": "sha256:e29b35006332b572af186fdd10bdef7ca6a8531766b44d075485e7ded46be9df" + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:33ab9148ac46504fa4ff3e51dd4472de84552d8ab5f7e8b8535fb560b85c5058" }, "owner_lane": "", "provenance": { diff --git a/mk/dual.mk b/mk/dual.mk index 7f73d0c2c..e325e3f82 100644 --- a/mk/dual.mk +++ b/mk/dual.mk @@ -71,3 +71,24 @@ test-dual-update: $(DUAL_DIR)/test_dual_update $(DUAL_DIR)/test_dual_update_asan ./$(DUAL_DIR)/test_dual_update_asan @echo "test-dual-update: DUAL-1a reference update passes analytic, property and refusal checks in plain and ASan/UBSan builds" endif + +# DUAL-1b replay (appended; same fragment) +ifndef DUAL_MK_3 +DUAL_MK_3 := 1 +.PHONY: test-dual-replay +DUAL_REPLAY_SRCS = $(DUAL_SRCS) src/dual/rx_dual_replay.c tests/dual/dual_traces.c +DUAL_REPLAY_HDRS = $(DUAL_HDRS) src/dual/rx_dual_replay.h tests/dual/dual_traces.h tests/dual/dual_fixtures.h +$(DUAL_DIR)/test_dual_replay: tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) $(DUAL_REPLAY_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) -o $@ tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) -lm +$(DUAL_DIR)/test_dual_replay_asan: tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) $(DUAL_REPLAY_HDRS) + @mkdir -p $(DUAL_DIR) + $(CC) $(DUAL_CFLAGS) $(EST_ASAN) -o $@ tests/dual/test_dual_replay.c $(DUAL_REPLAY_SRCS) -lm +# Run from the repository root: the recorded traces are evidence/R15/raw/*/{machine,preflight}-perf.csv. +test-dual-replay: $(DUAL_DIR)/test_dual_replay $(DUAL_DIR)/test_dual_replay_asan + ./$(DUAL_DIR)/test_dual_replay + ./$(DUAL_DIR)/test_dual_replay_asan > /dev/null + @echo "test-dual-replay: DUAL-1b replay, pre-registered measures and negative control pass in plain and ASan/UBSan builds (all inputs UNCALIBRATED)" +dual-purity: $(DUAL_DIR)/rx_dual_replay.o +test-dual: test-dual-replay +endif diff --git a/src/.crumb b/src/.crumb index 2f84e2beb..541f6f71a 100644 --- a/src/.crumb +++ b/src/.crumb @@ -134,7 +134,7 @@ }, { "digest": "779e416daad95fc928a9217a06a2897d29930df83b61423d2496fb71f7c42355", - "last_commit": "f523edd", + "last_commit": "3a3144d", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -170,14 +170,14 @@ } ], "children_root": "sha256:607467f8ea6e425bf33f7a0df65a411055a569e302b8127ab84822a0b4559109", - "compiled_at": "2026-10-04T18:45:18Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:8e9dd6f8117c615273c291427db9ad14abaa907bc05741145bb9892706f68efa", + "digest": "sha256:839e2dd4f0aa249d7b552e9d3eafdf3ae94f13a4a4c17c3bed1ce603a07659d8", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 401, - "generated_at_commit": "00f54b6b19e5e64e8947aea2890e688511d897a8", - "source_tree": "sha256:5613ea31519d6c5e879b868b3b0beee43e6afa8199bd6e4d9fe48b2ecc18a7ff" + "files": 403, + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:e98bb9c0ddc0aa8281597e44376154c18782109c91224cc49e83aa7a59cc1834" }, "owner_lane": "", "provenance": { diff --git a/src/dual/rx_dual_replay.c b/src/dual/rx_dual_replay.c new file mode 100644 index 000000000..b500906c8 --- /dev/null +++ b/src/dual/rx_dual_replay.c @@ -0,0 +1,257 @@ +/* DUAL-1b: digest-bound traces and deterministic replay. */ +#include "rx_dual_replay.h" +#include "sha256.h" + +#include <math.h> +#include <string.h> + +/* ---- generator ---- */ +uint64_t rx_dual_xorshift64s(uint64_t *s) +{ + uint64_t x = *s; + x ^= x >> 12; x ^= x << 25; x ^= x >> 27; + *s = x; + return x * 0x2545F4914F6CDD1Dull; +} +double rx_dual_xorshift_unit(uint64_t *s) { return (double)(rx_dual_xorshift64s(s) >> 11) / 9007199254740992.0; } + +/* ---- little-endian encoding (same conventions as rx_dual.c) ---- */ +static void p_u32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } +static void p_u64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } +static void p_f64(uint8_t *p, double d) +{ + uint64_t bits; + if (d == 0.0) d = 0.0; /* -0.0 -> +0.0 */ + memcpy(&bits, &d, 8); + p_u64(p, bits); +} +static uint32_t g_u32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = (v << 8) | p[i]; return v; } +static uint64_t g_u64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; } +static double g_f64(const uint8_t *p) { uint64_t bits = g_u64(p); double d; memcpy(&d, &bits, 8); return d; } + +static RxDualStatus check_tick(const RxDualTraceTick *t) +{ + if (!isfinite(t->estimate) || !isfinite(t->uncertainty)) return RX_DUAL_ERR_NONFINITE; + if (t->uncertainty < 0.0) return RX_DUAL_ERR_RANGE; + if (t->calibrated > 1u || t->evidence_verified > 1u || t->regime_change > 1u) return RX_DUAL_ERR_RANGE; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_check_trace(const RxDualTrace *tr) +{ + RxDualStatus st; + if (!tr) return RX_DUAL_ERR_NULL; + if (tr->trace_kind < 1u || tr->trace_kind >= (uint32_t)RX_DUAL_TRACE_MAX_) return RX_DUAL_ERR_KIND; + if (tr->n_ticks < 1u || tr->n_ticks > RX_DUAL_TRACE_MAX_TICKS) return RX_DUAL_ERR_RANGE; + for (uint32_t i = 0; i < tr->n_ticks; i++) + if ((st = check_tick(&tr->t[i])) != RX_DUAL_OK) return st; + return RX_DUAL_OK; +} + +static void enc_header(const RxDualTrace *tr, uint8_t out[RX_DUAL_TRACE_HEADER_ENCODED]) +{ + out[0] = (uint8_t)RX_DUAL_KIND_TRACE; out[1] = (uint8_t)RX_DUAL_FORMAT_VERSION; + p_u32(out + 2, tr->trace_kind); p_u64(out + 6, tr->seed); p_u32(out + 14, tr->n_ticks); +} +static void enc_tick(const RxDualTraceTick *t, uint8_t out[RX_DUAL_TRACE_TICK_ENCODED]) +{ + p_u64(out, t->generation); p_f64(out + 8, t->estimate); p_f64(out + 16, t->uncertainty); + p_u32(out + 24, t->calibrated); p_u32(out + 28, t->evidence_verified); p_u32(out + 32, t->regime_change); +} + +RxDualStatus rx_dual_encode_trace(const RxDualTrace *tr, uint8_t *buf, size_t cap, size_t *len) +{ + RxDualStatus st; + size_t need; + if (!buf || !len) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_trace(tr)) != RX_DUAL_OK) return st; + need = RX_DUAL_TRACE_HEADER_ENCODED + (size_t)tr->n_ticks * RX_DUAL_TRACE_TICK_ENCODED; + if (cap < need) return RX_DUAL_ERR_ENCODING; + enc_header(tr, buf); + for (uint32_t i = 0; i < tr->n_ticks; i++) + enc_tick(&tr->t[i], buf + RX_DUAL_TRACE_HEADER_ENCODED + (size_t)i * RX_DUAL_TRACE_TICK_ENCODED); + *len = need; + return RX_DUAL_OK; +} + +RxDualStatus rx_dual_decode_trace(const uint8_t *buf, size_t len, RxDualTrace *out) +{ + uint32_t n; + if (!buf || !out) return RX_DUAL_ERR_NULL; + if (len < RX_DUAL_TRACE_HEADER_ENCODED) return RX_DUAL_ERR_ENCODING; + if (buf[0] != (uint8_t)RX_DUAL_KIND_TRACE) return RX_DUAL_ERR_KIND; + if (buf[1] != (uint8_t)RX_DUAL_FORMAT_VERSION) return RX_DUAL_ERR_ENCODING; + memset(out, 0, sizeof *out); + out->trace_kind = g_u32(buf + 2); out->seed = g_u64(buf + 6); n = g_u32(buf + 14); + if (n < 1u || n > RX_DUAL_TRACE_MAX_TICKS) return RX_DUAL_ERR_RANGE; + if (len != RX_DUAL_TRACE_HEADER_ENCODED + (size_t)n * RX_DUAL_TRACE_TICK_ENCODED) return RX_DUAL_ERR_ENCODING; + out->n_ticks = n; + for (uint32_t i = 0; i < n; i++) { + const uint8_t *p = buf + RX_DUAL_TRACE_HEADER_ENCODED + (size_t)i * RX_DUAL_TRACE_TICK_ENCODED; + RxDualTraceTick *t = &out->t[i]; + t->generation = g_u64(p); t->estimate = g_f64(p + 8); t->uncertainty = g_f64(p + 16); + t->calibrated = g_u32(p + 24); t->evidence_verified = g_u32(p + 28); t->regime_change = g_u32(p + 32); + } + return rx_dual_check_trace(out); +} + +RxDualStatus rx_dual_digest_trace(const RxDualTrace *tr, RxDualDigest *out) +{ + sha256_ctx ctx; + uint8_t zero = 0, hdr[RX_DUAL_TRACE_HEADER_ENCODED], tick[RX_DUAL_TRACE_TICK_ENCODED]; + RxDualStatus st; + if (!out) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_trace(tr)) != RX_DUAL_OK) return st; + sha256_init(&ctx); + sha256_update(&ctx, (const uint8_t *)RX_DUAL_DOMAIN_TRACE, strlen(RX_DUAL_DOMAIN_TRACE)); + sha256_update(&ctx, &zero, 1); + enc_header(tr, hdr); + sha256_update(&ctx, hdr, sizeof hdr); + for (uint32_t i = 0; i < tr->n_ticks; i++) { enc_tick(&tr->t[i], tick); sha256_update(&ctx, tick, sizeof tick); } + sha256_final(&ctx, out->b); + return RX_DUAL_OK; +} + +/* ---- perf-stat CSV import (bytes in, no locale, no strtod) ---- */ +static int field_eq(const uint8_t *p, size_t n, const char *s) { return strlen(s) == n && memcmp(p, s, n) == 0; } +static int parse_udec(const uint8_t *p, size_t n, double *out) +{ + double v = 0.0; + if (n == 0 || n > 19) return 0; + for (size_t i = 0; i < n; i++) { + if (p[i] < '0' || p[i] > '9') return 0; + v = v * 10.0 + (double)(p[i] - '0'); + } + *out = v; + return 1; +} + +RxDualStatus rx_dual_trace_from_perf_csv(const uint8_t *bytes, size_t len, const char *cpu, const char *event, + RxDualTrace *out) +{ + size_t i = 0; + uint32_t n = 0; + if (!bytes || !cpu || !event || !out) return RX_DUAL_ERR_NULL; + memset(out, 0, sizeof *out); + while (i < len) { + size_t eol = i, f[8], fn = 0, k; + while (eol < len && bytes[eol] != '\n') eol++; + if (eol > i && bytes[i] != '#') { + /* field starts: f[0..fn), terminated by ',' or end of line; need at least 5 fields */ + f[fn++] = i; + for (k = i; k < eol && fn < 8; k++) if (bytes[k] == ',') f[fn++] = k + 1; + if (fn >= 5) { + size_t c0 = f[1], c1 = f[2] - 1, v0 = f[2], v1 = f[3] - 1, e0 = f[4], e1 = (fn > 5) ? f[5] - 1 : eol; + if (field_eq(bytes + c0, c1 - c0, cpu) && field_eq(bytes + e0, e1 - e0, event)) { + double v; + if (n >= RX_DUAL_TRACE_MAX_TICKS) return RX_DUAL_ERR_FULL; + if (!parse_udec(bytes + v0, v1 - v0, &v)) return RX_DUAL_ERR_ENCODING; /* "<not counted>" and friends */ + out->t[n].generation = (uint64_t)n + 1u; + out->t[n].estimate = v; + out->t[n].uncertainty = 0.0; + out->t[n].calibrated = 0; /* a counter is not a calibrated estimate */ + out->t[n].evidence_verified = 1; + out->t[n].regime_change = 0; + n++; + } + } + } + i = eol + 1; + } + if (n == 0) return RX_DUAL_ERR_RANGE; + out->trace_kind = RX_DUAL_TRACE_RECORDED; + out->seed = 0; + out->n_ticks = n; + return rx_dual_check_trace(out); +} + +/* ---- replay ---- */ +static void derive(const RxDualDigest *trace, const char *tag, uint32_t i, RxDualDigest *out) +{ + sha256_ctx ctx; uint8_t ib[4]; + p_u32(ib, i); + sha256_init(&ctx); + sha256_update(&ctx, (const uint8_t *)tag, strlen(tag)); + sha256_update(&ctx, trace->b, RX_DUAL_DIGEST_SIZE); + sha256_update(&ctx, ib, 4); + sha256_final(&ctx, out->b); +} + +RxDualStatus rx_dual_replay_run(const RxDualTrace *tr, const RxDualController *ctl, const RxDualResource *res, + RxDualClass cls, double budget, const RxDualDigest *budget_contract, + const RxDualDigest *calibration_ref, RxDualReplay *out) +{ + RxDualStatus st; + RxDualConstraintState cur, nx; + RxDualInput in; + sha256_ctx ctx; + uint8_t zero = 0, nb[4]; + if (!tr || !ctl || !res || !budget_contract || !out) return RX_DUAL_ERR_NULL; + if ((st = rx_dual_check_trace(tr)) != RX_DUAL_OK) return st; + memset(out, 0, sizeof *out); + if ((st = rx_dual_digest_trace(tr, &out->trace)) != RX_DUAL_OK) return st; + if ((st = rx_dual_digest_controller(ctl, &out->controller_id)) != RX_DUAL_OK) return st; + for (uint32_t i = 0; i < tr->n_ticks; i++) { + const RxDualTraceTick *t = &tr->t[i]; + memset(&in, 0, sizeof in); + derive(&out->trace, "omega.dual.trace.estimate_ref", i, &in.estimate_ref); + derive(&out->trace, "omega.dual.trace.evidence_root", 0, &in.evidence_root); + in.estimate_kind = RX_DUAL_EST_ESTIMATED; + in.unit = res->unit; + in.estimate = t->estimate; + in.uncertainty = t->uncertainty; + in.generation = t->generation; + in.evidence_verified = t->evidence_verified; + in.regime_change = t->regime_change; + if (t->calibrated) { + if (!calibration_ref || rx_dual_digest_is_zero(calibration_ref)) return RX_DUAL_ERR_CALIBRATION; + in.calibration_ref = *calibration_ref; + } + if (i == 0) st = rx_dual_init_state(res, cls, budget, budget_contract, &in, ctl, t->generation, &nx); + else st = rx_dual_update(&cur, res, &in, ctl, t->generation, &nx); + if (st != RX_DUAL_OK) return st; + if ((st = rx_dual_digest_constraint(&nx, &out->record[i])) != RX_DUAL_OK) return st; + out->lambda[i] = nx.lambda; + out->estimate[i] = nx.estimate; + out->generation[i] = nx.generation; + out->state[i] = nx.lambda_state; + cur = nx; + } + out->n = tr->n_ticks; + out->last = cur; + sha256_init(&ctx); + sha256_update(&ctx, (const uint8_t *)RX_DUAL_DOMAIN_REPLAY, strlen(RX_DUAL_DOMAIN_REPLAY)); + sha256_update(&ctx, &zero, 1); + sha256_update(&ctx, out->controller_id.b, RX_DUAL_DIGEST_SIZE); + sha256_update(&ctx, out->trace.b, RX_DUAL_DIGEST_SIZE); + p_u32(nb, out->n); + sha256_update(&ctx, nb, 4); + for (uint32_t i = 0; i < out->n; i++) sha256_update(&ctx, out->record[i].b, RX_DUAL_DIGEST_SIZE); + sha256_final(&ctx, out->replay.b); + return RX_DUAL_OK; +} + +/* ---- measures ---- */ +uint32_t rx_dual_reversals(const double *x, uint32_t lo, uint32_t hi) +{ + uint32_t r = 0; + int last = 0; + if (!x || hi <= lo) return 0; + for (uint32_t i = lo + 1; i < hi; i++) { + double d = x[i] - x[i - 1]; + int s = (d > 0.0) ? 1 : (d < 0.0) ? -1 : 0; + if (s == 0) continue; + if (last != 0 && s != last) r++; + last = s; + } + return r; +} + +double rx_dual_peak_to_peak(const double *x, uint32_t lo, uint32_t hi) +{ + double mn, mx; + if (!x || hi <= lo) return 0.0; + mn = mx = x[lo]; + for (uint32_t i = lo + 1; i < hi; i++) { if (x[i] < mn) mn = x[i]; if (x[i] > mx) mx = x[i]; } + return mx - mn; +} diff --git a/src/dual/rx_dual_replay.h b/src/dual/rx_dual_replay.h new file mode 100644 index 000000000..659d75f14 --- /dev/null +++ b/src/dual/rx_dual_replay.h @@ -0,0 +1,117 @@ +/* DUAL-1b: deterministic replay of the reference controller over digest-bound + * traces (ADR 0031 section 5 and section 8, gate DUAL_PRICE_REFERENCE). + * + * A trace is a bounded, canonically encoded sequence of controller inputs + * (kind byte 8, domain omega.dual.trace.v1). Its digest binds the inputs a + * replay consumed. The replay engine feeds the trace through + * rx_dual_init_state / rx_dual_update and records the digest of every + * resulting RxDualConstraintState; the digest of that log, bound to the + * controller_id and the trace digest, is the replay digest. Same trace, same + * controller, same resource and budget => identical replay digest. + * + * Bounded, no allocation, no globals, no I/O: the recorded-trace importer + * takes bytes the caller already read. Every price produced by a replay is as + * calibrated as its inputs: with calibrated = 0 on every tick (the only honest + * value while EST-3 has not passed) every state is UNCALIBRATED and may never + * influence production. Replay is measurement of the controller, nothing else. */ +#ifndef OMEGA_RX_DUAL_REPLAY_H +#define OMEGA_RX_DUAL_REPLAY_H + +#include "rx_dual_update.h" + +#define RX_DUAL_KIND_TRACE 8 +#define RX_DUAL_DOMAIN_TRACE "omega.dual.trace.v1" +#define RX_DUAL_DOMAIN_REPLAY "omega.dual.replay.v1" +#define RX_DUAL_TRACE_MAX_TICKS 2048u +#define RX_DUAL_TRACE_TICK_ENCODED 36u /* u64 f64 f64 u32 u32 u32 */ +#define RX_DUAL_TRACE_HEADER_ENCODED 18u /* kind ver u32 u64 u32 */ +#define RX_DUAL_TRACE_ENCODED_MAX (RX_DUAL_TRACE_HEADER_ENCODED + RX_DUAL_TRACE_MAX_TICKS * RX_DUAL_TRACE_TICK_ENCODED) + +typedef enum { + RX_DUAL_TRACE_CONSTANT_LOW = 1, + RX_DUAL_TRACE_BINDING = 2, + RX_DUAL_TRACE_STEP = 3, + RX_DUAL_TRACE_SQUARE = 4, + RX_DUAL_TRACE_COMPETING_MEMORY = 5, + RX_DUAL_TRACE_COMPETING_LATENCY = 6, + RX_DUAL_TRACE_CAPACITY_REDUCTION = 7, + RX_DUAL_TRACE_REGIME_CHANGE = 8, + RX_DUAL_TRACE_BURSTY = 9, + RX_DUAL_TRACE_FANOUT = 10, + RX_DUAL_TRACE_KV_SATURATION = 11, + RX_DUAL_TRACE_RECOVERY = 12, + RX_DUAL_TRACE_RECORDED = 13, + RX_DUAL_TRACE_MAX_ = 14 +} RxDualTraceKind; + +typedef struct { + uint64_t generation; /* World generation the input claims; also used as now_generation */ + double estimate; /* finite */ + double uncertainty; /* finite, >= 0 */ + uint32_t calibrated; /* 0 or 1; 0 = no EST-3 receipt: the state can only be UNCALIBRATED */ + uint32_t evidence_verified; /* 0 or 1 */ + uint32_t regime_change; /* 0 or 1 */ +} RxDualTraceTick; + +typedef struct { + uint32_t trace_kind; /* RxDualTraceKind */ + uint64_t seed; /* generator seed (0 for recorded) */ + uint32_t n_ticks; /* 1..RX_DUAL_TRACE_MAX_TICKS */ + RxDualTraceTick t[RX_DUAL_TRACE_MAX_TICKS]; +} RxDualTrace; + +/* xorshift64*: the one generator every synthetic trace uses. State must be nonzero. */ +uint64_t rx_dual_xorshift64s(uint64_t *state); +/* Uniform double in [0, 1) from one generator step (53 bits). */ +double rx_dual_xorshift_unit(uint64_t *state); + +RxDualStatus rx_dual_check_trace(const RxDualTrace *tr); +RxDualStatus rx_dual_encode_trace(const RxDualTrace *tr, uint8_t *buf, size_t cap, size_t *len); +RxDualStatus rx_dual_decode_trace(const uint8_t *buf, size_t len, RxDualTrace *out); +/* SHA-256(domain || 0x00 || encoding), streamed; no buffer needed. */ +RxDualStatus rx_dual_digest_trace(const RxDualTrace *tr, RxDualDigest *out); + +/* Import one (cpu, event) series from perf-stat CSV bytes: + * <time>,<cpu>,<count>,<unit>,<event>,<run ns>,<pct>,, + * Lines starting with '#' and empty lines are skipped. A row whose cpu and + * event fields equal `cpu` and `event` yields one tick with estimate = count + * (parsed as a decimal unsigned integer, no locale, no strtod), uncertainty 0, + * calibrated 0, evidence_verified 1, regime_change 0, generation = index + 1. + * A matching row whose count is not a plain decimal integer (for example + * "<not counted>") is refused (RX_DUAL_ERR_ENCODING), never skipped. Zero + * matching rows or more than RX_DUAL_TRACE_MAX_TICKS is refused. */ +RxDualStatus rx_dual_trace_from_perf_csv(const uint8_t *bytes, size_t len, const char *cpu, const char *event, + RxDualTrace *out); + +/* Replay of one constraint chain. */ +typedef struct { + RxDualDigest trace; /* rx_dual_digest_trace of the input */ + RxDualDigest controller_id; + uint32_t n; /* ticks replayed (= trace n_ticks) */ + double lambda[RX_DUAL_TRACE_MAX_TICKS]; + double estimate[RX_DUAL_TRACE_MAX_TICKS]; /* estimate held in the state (held under STALE/FROZEN/REFUSED) */ + uint64_t generation[RX_DUAL_TRACE_MAX_TICKS]; /* likewise */ + RxDualLambdaState state[RX_DUAL_TRACE_MAX_TICKS]; + RxDualDigest record[RX_DUAL_TRACE_MAX_TICKS]; /* constraint state digest per tick */ + RxDualConstraintState last; + RxDualDigest replay; /* SHA-256(REPLAY domain || 0 || controller_id || trace || u32 n || records) */ +} RxDualReplay; + +/* Feed every tick of `tr` through the controller. Tick 0 goes through + * rx_dual_init_state (lambda starts at 0), the rest through rx_dual_update. + * now_generation for tick i is tr->t[i].generation. calibration_ref is the + * digest written into inputs whose tick has calibrated = 1 (may be NULL when + * no tick is calibrated; a calibrated tick with a NULL or zero digest is + * refused). Any refusal from the controller aborts the replay with that + * status and leaves `out` unspecified: a refusal is a finding, never patched. */ +RxDualStatus rx_dual_replay_run(const RxDualTrace *tr, const RxDualController *ctl, const RxDualResource *res, + RxDualClass cls, double budget, const RxDualDigest *budget_contract, + const RxDualDigest *calibration_ref, RxDualReplay *out); + +/* Measures used by the DUAL-1 gate (pure functions of a series). */ +/* Sign changes between consecutive nonzero first differences of x[lo..hi). */ +uint32_t rx_dual_reversals(const double *x, uint32_t lo, uint32_t hi); +/* max(x) - min(x) over x[lo..hi); 0 for an empty range. */ +double rx_dual_peak_to_peak(const double *x, uint32_t lo, uint32_t hi); + +#endif /* OMEGA_RX_DUAL_REPLAY_H */ diff --git a/tests/.crumb b/tests/.crumb index 7d6c3ffef..5a2e5c5c1 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -254,7 +254,7 @@ }, { "digest": "f49db08f5e2f8ad20c9e10f7eaaa27e7f264b6bfc17e4d4fad16e60eb1495f1d", - "last_commit": "f523edd", + "last_commit": "3a3144d", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -284,14 +284,14 @@ } ], "children_root": "sha256:0a4e348cf355e1fa5924f9c76a460d1d49bc285687b134365ef0fb9dab4f0855", - "compiled_at": "2026-10-04T18:45:18Z", + "compiled_at": "2026-10-04T18:54:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:e96dec62800dc6dae755ebe10eacfedcb0959e29e00a3540284cb585f8f9716f", + "digest": "sha256:7f8951ba438340bc36223d5722c1055efd8fdd01efe3a43bcf8f2e61ce68dea7", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 787, - "generated_at_commit": "00f54b6b19e5e64e8947aea2890e688511d897a8", - "source_tree": "sha256:1a35905c6c7254bc66bf0519cac4d23e5b1c2d7722356a0e06401faec581448f" + "files": 790, + "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", + "source_tree": "sha256:9e8f9ed5339ccf473907a174516a6af9de8ed850a96934abd3c7474c4bf136a6" }, "owner_lane": "", "provenance": { diff --git a/tests/dual/dual_traces.c b/tests/dual/dual_traces.c new file mode 100644 index 000000000..bf4466572 --- /dev/null +++ b/tests/dual/dual_traces.c @@ -0,0 +1,76 @@ +#include "dual_traces.h" +#include <string.h> + +static void tick(RxDualTrace *t, uint32_t i, uint64_t gen, double est, double sd) +{ + t->t[i].generation = gen; t->t[i].estimate = est; t->t[i].uncertainty = sd; + t->t[i].calibrated = 0; t->t[i].evidence_verified = 1; t->t[i].regime_change = 0; +} +static void head(RxDualTrace *t, RxDualTraceKind kind, uint64_t seed, uint32_t n) +{ + memset(t, 0, sizeof *t); + t->trace_kind = (uint32_t)kind; t->seed = seed; t->n_ticks = n; +} + +RxDualStatus dual_trace_constant(RxDualTraceKind kind, uint64_t seed, uint32_t n, uint64_t gen0, double est, double sd, RxDualTrace *out) +{ + head(out, kind, seed, n); + for (uint32_t i = 0; i < n; i++) tick(out, i, gen0 + i, est, sd); + return rx_dual_check_trace(out); +} + +RxDualStatus dual_trace_scenario(int scenario, char part, uint32_t r, RxDualTrace *out) +{ + const uint32_t N = DUAL_TRACE_N; + uint64_t s; + switch (scenario) { + case 1: return dual_trace_constant(RX_DUAL_TRACE_CONSTANT_LOW, 0x1001, N, 1, 4000.0, DUAL_SD, out); + case 2: return dual_trace_constant(RX_DUAL_TRACE_BINDING, 0x1002, N, 1, 5500.0, DUAL_SD, out); + case 3: + head(out, RX_DUAL_TRACE_STEP, 0x1003, N); + for (uint32_t i = 0; i < N; i++) tick(out, i, i + 1, (i < 100) ? 4000.0 : 6000.0, DUAL_SD); + break; + case 4: + head(out, RX_DUAL_TRACE_SQUARE, 0x1004, N); + for (uint32_t i = 0; i < N; i++) tick(out, i, i + 1, ((i % 50) < 25) ? 6000.0 : 4000.0, DUAL_SD); + break; + case 5: return dual_trace_constant(RX_DUAL_TRACE_COMPETING_MEMORY, 0x1005, N, 1, 6000.0, 50.0, out); + case 6: return dual_trace_constant(RX_DUAL_TRACE_COMPETING_LATENCY, 0x1005, N, 1, 4900.0, 50.0, out); + case 7: /* two chains: A = ticks 0..99 (generation 1..100), B = ticks 100..199 (generation 101..200) */ + if (part == 'A') return dual_trace_constant(RX_DUAL_TRACE_CAPACITY_REDUCTION, 0x1007, 100, 1, 4500.0, DUAL_SD, out); + if (part == 'B') return dual_trace_constant(RX_DUAL_TRACE_CAPACITY_REDUCTION, 0x1007, 100, 101, 4500.0, DUAL_SD, out); + return RX_DUAL_ERR_RANGE; + case 8: + head(out, RX_DUAL_TRACE_REGIME_CHANGE, 0x1008, N); + for (uint32_t i = 0; i < N; i++) { tick(out, i, i + 1, 5500.0, DUAL_SD); out->t[i].regime_change = (i >= 80 && i < 120) ? 1u : 0u; } + break; + case 9: + head(out, RX_DUAL_TRACE_BURSTY, 0x1009, N); + s = 0x1009; + for (uint32_t i = 0; i < N; i++) { + double est = 4200.0; + int burst = (rx_dual_xorshift64s(&s) & 7u) == 0u; /* probability 1/8 */ + double amp = 800.0 + 1600.0 * rx_dual_xorshift_unit(&s); /* uniform [800, 2400) */ + if (burst) est += amp; + tick(out, i, i + 1, est, DUAL_SD); + } + break; + case 10: { + double off; + if (r >= RX_DUAL_MAX_RESOURCES) return RX_DUAL_ERR_RANGE; + s = 0x100Au + r; + off = -50.0 + 100.0 * rx_dual_xorshift_unit(&s); /* uniform [-50, 50) */ + return dual_trace_constant(RX_DUAL_TRACE_FANOUT, 0x100Au + r, N, 1, 5500.0 + off, DUAL_SD, out); + } + case 11: + head(out, RX_DUAL_TRACE_KV_SATURATION, 0x100B, N); + for (uint32_t i = 0; i < N; i++) tick(out, i, i + 1, 3000.0 + 4000.0 * (double)i / 199.0, DUAL_SD); + break; + case 12: + head(out, RX_DUAL_TRACE_RECOVERY, 0x100C, N); + for (uint32_t i = 0; i < N; i++) tick(out, i, i + 1, (i < 100) ? 6000.0 : 5000.0, DUAL_SD); + break; + default: return RX_DUAL_ERR_RANGE; + } + return rx_dual_check_trace(out); +} diff --git a/tests/dual/dual_traces.h b/tests/dual/dual_traces.h new file mode 100644 index 000000000..62862ad52 --- /dev/null +++ b/tests/dual/dual_traces.h @@ -0,0 +1,16 @@ +/* DUAL-1b pre-registered scenario traces (evidence/DUAL/1b-replay/receipt.md, + * section "Pre-registered: scenarios"). Every generator is a pure function of + * its declared parameters and seed; the trace digest is the scenario identity. */ +#ifndef DUAL_TRACES_H +#define DUAL_TRACES_H +#include "rx_dual_replay.h" + +#define DUAL_TRACE_N 200u +#define DUAL_SD 100.0 +#define DUAL_BUDGET 5000.0 +#define DUAL_SCALE 1000.0 + +/* Scenario 1..6, 8, 9, 11, 12 by number; 7 by part ('A' or 'B'); 10 by resource index r (0..31). */ +RxDualStatus dual_trace_constant(RxDualTraceKind kind, uint64_t seed, uint32_t n, uint64_t gen0, double est, double sd, RxDualTrace *out); +RxDualStatus dual_trace_scenario(int scenario, char part, uint32_t r, RxDualTrace *out); +#endif diff --git a/tests/dual/test_dual_replay.c b/tests/dual/test_dual_replay.c new file mode 100644 index 000000000..1aeb02fd2 --- /dev/null +++ b/tests/dual/test_dual_replay.c @@ -0,0 +1,306 @@ +/* DUAL-1b tests: digest-bound traces, deterministic replay, pre-registered + * measures (evidence/DUAL/1b-replay/receipt.md) and the negative control. + * Every input is UNCALIBRATED (calibrated = 0): every replayed state must be + * UNCALIBRATED and no price here may influence production. */ +#include "dual_fixtures.h" +#include "dual_traces.h" +#include "sha256.h" +#include <math.h> +#include <stdio.h> +#include <string.h> + +static int g_checks, g_fail; +#define CHECK(c) do { g_checks++; if (!(c)) { g_fail++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) +#define CHECK_ST(expr, want) do { RxDualStatus s_ = (expr); g_checks++; if (s_ != (want)) { g_fail++; \ + fprintf(stderr, "FAIL %s:%d: %s -> %d, want %d\n", __FILE__, __LINE__, #expr, (int)s_, (int)(want)); } } while (0) + +/* Golden digests (D2): committed after the first run, checked across processes ever after. */ +static const char *GOLDEN_TRACE_2 = "f9c3944ab952dd5e2beefebbb2499f98ec7be28157b571a47e406ca40e4d93fb"; +static const char *GOLDEN_REPLAY_2 = "46345146d1213fdd2888fd0ace3f7ab31287565fd42adf950ad58edeb11bf5ae"; +static const char *GOLDEN_TRACE_4 = "0a463faef33ab2a59da6c854eabfa2ec9e189f3e5e6b00f07c773e490e52edd5"; +static const char *GOLDEN_REPLAY_4 = "64a30ce77dc7decfedae15c6c8e8e66fa75693df049916deab7ca2abbfaaa888"; + +static RxDualTrace T, T2; +static RxDualReplay R, R2; +static RxDualDigest BC, BC2; +static int g_ref_fail; /* any pre-registered measure failed for the reference controller */ +static int g_neg_as_declared = 1; + +static void hex(const uint8_t *b, size_t n, char *out) { static const char *h = "0123456789abcdef"; for (size_t i = 0; i < n; i++) { out[2*i] = h[b[i] >> 4]; out[2*i+1] = h[b[i] & 15]; } out[2*n] = 0; } +static void dhex(const RxDualDigest *d, char *out) { hex(d->b, 32, out); } + +/* A measure result line for the receipt. */ +static void measure(const char *scenario, const char *id, int pass, const char *detail, int reference) +{ + printf("MEASURE %-14s %-4s %s %s\n", scenario, id, pass ? "PASS" : "FAIL", detail); + if (reference) { CHECK(pass); if (!pass) g_ref_fail = 1; } +} + +static const RxDualController *ref_ctl(void) { static RxDualController c; static int init; if (!init) { c = fx_ctl(1, 0.1, 0.05, 2.0, 10.0, 4); init = 1; } return &c; } +static const RxDualController *hostile_ctl(void) { static RxDualController c; static int init; if (!init) { c = fx_ctl(1, 5.0, 0.0, 2.0, 10.0, 4); init = 1; } return &c; } + +/* Run a chain and emit the digest line; returns status. */ +static RxDualStatus run(const char *name, const RxDualTrace *tr, const RxDualController *ctl, const RxDualResource *res, + double budget, const RxDualDigest *bc, RxDualReplay *out) +{ + char th[65], ch[65], rh[65]; + RxDualStatus st = rx_dual_replay_run(tr, ctl, res, RX_DUAL_CLASS_SOFT, budget, bc, NULL, out); + if (st != RX_DUAL_OK) { printf("REPLAY %-14s REFUSED status %d\n", name, (int)st); return st; } + dhex(&out->trace, th); dhex(&out->controller_id, ch); dhex(&out->replay, rh); + printf("REPLAY %-14s n=%u trace=%s controller=%s replay=%s final_lambda=%.9g\n", name, out->n, th, ch, rh, out->lambda[out->n - 1]); + return st; +} + +/* M1, M7, M8 on any chain; D1 determinism. */ +static void common_measures(const char *name, const RxDualTrace *tr, const RxDualController *ctl, const RxDualResource *res, + double budget, const RxDualDigest *bc, RxDualReplay *rp, int reference) +{ + char d[160]; int ok; uint32_t rl, re; double lmax = 0; + CHECK_ST(rx_dual_controller_lambda_max(ctl, res->resource_id, &lmax), RX_DUAL_OK); + ok = 1; + for (uint32_t i = 0; i < rp->n; i++) if (!isfinite(rp->lambda[i]) || rp->lambda[i] < 0.0 || rp->lambda[i] > lmax) ok = 0; + { double mx = 0; for (uint32_t i = 0; i < rp->n; i++) if (rp->lambda[i] > mx) mx = rp->lambda[i]; snprintf(d, sizeof d, "lambda_max=%g max_lambda=%.9g", lmax, mx); } + measure(name, "M1", ok, d, 1); + ok = 1; + for (uint32_t i = 0; i < rp->n; i++) if (tr->t[i].calibrated == 0 && rp->state[i] != (tr->t[i].regime_change ? RX_DUAL_LAMBDA_FROZEN : RX_DUAL_LAMBDA_UNCALIBRATED)) ok = 0; + for (uint32_t i = 0; i < rp->n; i++) if (rp->state[i] == RX_DUAL_LAMBDA_FRESH) ok = 0; + measure(name, "M7", ok, "every calibrated=0 tick is UNCALIBRATED (FROZEN exactly where regime_change = 1); none FRESH", 1); + rl = rx_dual_reversals(rp->lambda, 0, rp->n); + { static double est[RX_DUAL_TRACE_MAX_TICKS]; for (uint32_t i = 0; i < tr->n_ticks; i++) est[i] = tr->t[i].estimate; re = rx_dual_reversals(est, 0, tr->n_ticks); } + snprintf(d, sizeof d, "R(lambda)=%u R(estimate)=%u bound=%u", rl, re, re + 1); + measure(name, "M8", rl <= re + 1, d, 1); + { RxDualStatus st = rx_dual_replay_run(tr, ctl, res, RX_DUAL_CLASS_SOFT, budget, bc, NULL, &R2); + measure(name, "D1", st == RX_DUAL_OK && rx_dual_digest_eq(&rp->replay, &R2.replay), "second replay gives identical replay digest", 1); } + (void)reference; +} + +static void tail_zero(const char *name, const RxDualReplay *rp) +{ + char d[96]; uint32_t r = rx_dual_reversals(rp->lambda, rp->n - 50, rp->n); + snprintf(d, sizeof d, "R(lambda) over final 50 ticks = %u", r); + measure(name, "M2", r == 0, d, 1); +} +static void monotone_from(const char *name, const RxDualReplay *rp, uint32_t from) +{ + char d[96]; int ok = 1; uint32_t bad = 0; + for (uint32_t i = from + 1; i < rp->n; i++) if (rp->lambda[i] < rp->lambda[i - 1] - 1e-12 * (1.0 + rp->lambda[i - 1])) { ok = 0; if (!bad) bad = i; } + if (ok) snprintf(d, sizeof d, "non-decreasing from tick %u; no violation", from); + else snprintf(d, sizeof d, "non-decreasing from tick %u; first violation at tick %u", from, bad); + measure(name, "M4", ok, d, 1); +} + +static void scenarios(void) +{ + const RxDualController *ctl = ref_ctl(); + RxDualResource res = fx_res(1, RX_DUAL_UNIT_BYTES, DUAL_SCALE); + char d[200]; + /* 1 constant low */ + CHECK_ST(dual_trace_scenario(1, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("1-constant-low", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("1-constant-low", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); tail_zero("1-constant-low", &R); + CHECK(R.lambda[R.n - 1] == 0.0); + /* 2 binding (+ golden, tamper, calibrated variant, hostile class) */ + CHECK_ST(dual_trace_scenario(2, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("2-binding", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("2-binding", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); tail_zero("2-binding", &R); monotone_from("2-binding", &R, 0); + snprintf(d, sizeof d, "final lambda %.9g vs fixed point 0.6", R.lambda[R.n - 1]); + measure("2-binding", "A", fabs(R.lambda[R.n - 1] - 0.6) < 1e-3, d, 1); + { char th[65], rh[65]; dhex(&R.trace, th); dhex(&R.replay, rh); + measure("2-binding", "D2", strcmp(th, GOLDEN_TRACE_2) == 0 && strcmp(rh, GOLDEN_REPLAY_2) == 0, "trace and replay digests equal the committed golden constants", 1); } + { static uint8_t enc[RX_DUAL_TRACE_ENCODED_MAX]; size_t len; RxDualDigest d0 = R.trace, r0 = R.replay; + CHECK_ST(rx_dual_encode_trace(&T, enc, sizeof enc, &len), RX_DUAL_OK); + CHECK(len == RX_DUAL_TRACE_HEADER_ENCODED + 200u * RX_DUAL_TRACE_TICK_ENCODED); + CHECK_ST(rx_dual_decode_trace(enc, len, &T2), RX_DUAL_OK); + CHECK(memcmp(&T2, &T, sizeof T) == 0); + enc[RX_DUAL_TRACE_HEADER_ENCODED + 17u * RX_DUAL_TRACE_TICK_ENCODED + 8u] ^= 0x01; /* lowest mantissa bit of estimate, tick 17 */ + CHECK_ST(rx_dual_decode_trace(enc, len, &T2), RX_DUAL_OK); + CHECK(T2.t[17].estimate != T.t[17].estimate && T2.t[16].estimate == T.t[16].estimate); + CHECK_ST(rx_dual_replay_run(&T2, ctl, &res, RX_DUAL_CLASS_SOFT, DUAL_BUDGET, &BC, NULL, &R2), RX_DUAL_OK); + measure("2-binding", "D3", !rx_dual_digest_eq(&R2.trace, &d0) && !rx_dual_digest_eq(&R2.replay, &r0), "one flipped bit changes trace digest and replay digest", 1); + /* decoder refusals */ + enc[0] = 2; CHECK_ST(rx_dual_decode_trace(enc, len, &T2), RX_DUAL_ERR_KIND); enc[0] = 8; + enc[1] = 2; CHECK_ST(rx_dual_decode_trace(enc, len, &T2), RX_DUAL_ERR_ENCODING); enc[1] = 1; + CHECK_ST(rx_dual_decode_trace(enc, len - 1, &T2), RX_DUAL_ERR_ENCODING); + CHECK_ST(rx_dual_decode_trace(enc, len + 1, &T2), RX_DUAL_ERR_ENCODING); } + { RxDualDigest cal = fx_dig(0x44); int same = 1, fresh = 1; + T2 = T; for (uint32_t i = 0; i < T2.n_ticks; i++) T2.t[i].calibrated = 1; + CHECK_ST(rx_dual_replay_run(&T2, ctl, &res, RX_DUAL_CLASS_SOFT, DUAL_BUDGET, &BC, &cal, &R2), RX_DUAL_OK); + for (uint32_t i = 0; i < R2.n; i++) { if (R2.lambda[i] != R.lambda[i]) same = 0; if (R2.state[i] != RX_DUAL_LAMBDA_FRESH) fresh = 0; } + measure("2-binding", "D4", same && fresh && !rx_dual_digest_eq(&R2.replay, &R.replay), "calibrated=1 (stand-in digest, format exercise): same lambdas, FRESH, different replay digest", 1); + CHECK_ST(rx_dual_replay_run(&T2, ctl, &res, RX_DUAL_CLASS_SOFT, DUAL_BUDGET, &BC, NULL, &R2), RX_DUAL_ERR_CALIBRATION); } + { RxDualStatus a = rx_dual_replay_run(&T, ctl, &res, RX_DUAL_CLASS_INVARIANT, DUAL_BUDGET, &BC, NULL, &R2); + RxDualStatus b = rx_dual_replay_run(&T, ctl, &res, RX_DUAL_CLASS_UNDECLARED, DUAL_BUDGET, &BC, NULL, &R2); + measure("2-binding", "H1", a == RX_DUAL_ERR_CLASS && b == RX_DUAL_ERR_CLASS, "INVARIANT and UNDECLARED class refused by the replay entry point", 1); } + { RxDualController m = *ctl; m.eta = 0.2; RxDualDigest c1, c2; rx_dual_digest_controller(ctl, &c1); rx_dual_digest_controller(&m, &c2); + CHECK(!rx_dual_digest_eq(&c1, &c2)); } + /* 3 step */ + CHECK_ST(dual_trace_scenario(3, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("3-step", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("3-step", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); tail_zero("3-step", &R); monotone_from("3-step", &R, 100); + CHECK(R.lambda[99] == 0.0 && R.lambda[100] > 0.0); + /* 4 square: reference and hostile */ + CHECK_ST(dual_trace_scenario(4, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("4-square", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("4-square", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); + { uint32_t r = rx_dual_reversals(R.lambda, 0, R.n); double pp = rx_dual_peak_to_peak(R.lambda, 150, 200); + snprintf(d, sizeof d, "R(lambda)=%u bound 8", r); measure("4-square", "M3a", r <= 8, d, 1); + snprintf(d, sizeof d, "peak-to-peak over t 150..199 = %.9g bound 2.5 (analytic 1.156)", pp); measure("4-square", "M3b", pp <= 2.5, d, 1); + { char th[65], rh[65]; dhex(&R.trace, th); dhex(&R.replay, rh); + measure("4-square", "D2", strcmp(th, GOLDEN_TRACE_4) == 0 && strcmp(rh, GOLDEN_REPLAY_4) == 0, "trace and replay digests equal the committed golden constants", 1); } } + { const RxDualController *h = hostile_ctl(); uint32_t r, rl, re; double pp, mx = 0; int ok; + static double est[RX_DUAL_TRACE_MAX_TICKS]; + CHECK_ST(run("4-square-HOST", &T, h, &res, DUAL_BUDGET, &BC, &R2), RX_DUAL_OK); + ok = 1; for (uint32_t i = 0; i < R2.n; i++) { if (!isfinite(R2.lambda[i]) || R2.lambda[i] < 0.0 || R2.lambda[i] > 10.0) ok = 0; if (R2.lambda[i] > mx) mx = R2.lambda[i]; } + snprintf(d, sizeof d, "lambda_max=10 max_lambda=%.9g (hostile: declared PASS)", mx); measure("4-square-HOST", "M1", ok, d, 0); if (!ok) g_neg_as_declared = 0; + r = rx_dual_reversals(R2.lambda, 0, R2.n); snprintf(d, sizeof d, "R(lambda)=%u bound 8 (hostile: declared PASS)", r); measure("4-square-HOST", "M3a", r <= 8, d, 0); if (r > 8) g_neg_as_declared = 0; + pp = rx_dual_peak_to_peak(R2.lambda, 150, 200); snprintf(d, sizeof d, "peak-to-peak over t 150..199 = %.9g bound 2.5 (hostile: declared FAIL)", pp); + measure("4-square-HOST", "M3b", pp <= 2.5, d, 0); if (pp <= 2.5) g_neg_as_declared = 0; + for (uint32_t i = 0; i < T.n_ticks; i++) est[i] = T.t[i].estimate; + rl = rx_dual_reversals(R2.lambda, 0, R2.n); re = rx_dual_reversals(est, 0, T.n_ticks); + snprintf(d, sizeof d, "R(lambda)=%u R(estimate)=%u bound=%u (hostile: declared PASS)", rl, re, re + 1); measure("4-square-HOST", "M8", rl <= re + 1, d, 0); if (rl > re + 1) g_neg_as_declared = 0; + printf("NEGATIVE-CONTROL hostile eta=5 rho=0 on square wave: gate verdict for this configuration = %s (expected FAIL)\n", pp <= 2.5 ? "PASS (SURPRISE)" : "FAIL"); } + /* 5/6 competing: one controller n = 2 */ + { RxDualController c2; RxDualResource rm = fx_res(1, RX_DUAL_UNIT_BYTES, DUAL_SCALE), rl = fx_res(2, RX_DUAL_UNIT_BYTES, DUAL_SCALE); double lm, ll; + memset(&c2, 0, sizeof c2); c2.eta = 0.1; c2.rho = 0.05; c2.k_sigma = 2.0; c2.max_age = 4; c2.cadence = 1; c2.n = 2; + c2.resource_id[0] = 1; c2.resource_id[1] = 2; c2.lambda_max[0] = 10.0; c2.lambda_max[1] = 10.0; + CHECK_ST(dual_trace_scenario(5, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("5-memory", &T, &c2, &rm, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("5-memory", &T, &c2, &rm, DUAL_BUDGET, &BC, &R, 1); tail_zero("5-memory", &R); lm = R.lambda[R.n - 1]; + CHECK_ST(dual_trace_scenario(6, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("6-latency", &T, &c2, &rl, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("6-latency", &T, &c2, &rl, DUAL_BUDGET, &BC, &R, 1); tail_zero("6-latency", &R); ll = R.lambda[R.n - 1]; + snprintf(d, sizeof d, "final lambda memory %.9g (> 0), latency %.9g (= 0)", lm, ll); measure("5/6-competing", "M9", lm > 0.0 && ll == 0.0, d, 1); } + /* 7 capacity reduction: chain A budget 5000, chain B budget 3000 (new contract) */ + CHECK_ST(dual_trace_scenario(7, 'A', 0, &T), RX_DUAL_OK); + CHECK_ST(run("7A-capacity", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("7A-capacity", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); CHECK(R.lambda[R.n - 1] == 0.0); + CHECK_ST(dual_trace_scenario(7, 'B', 0, &T), RX_DUAL_OK); + CHECK_ST(run("7B-capacity", &T, ctl, &res, 3000.0, &BC2, &R), RX_DUAL_OK); + common_measures("7B-capacity", &T, ctl, &res, 3000.0, &BC2, &R, 1); tail_zero("7B-capacity", &R); monotone_from("7B-capacity", &R, 0); + CHECK(R.last.generation == 200 && R.last.tick == 99 && R.lambda[0] > 0.0); + /* 8 regime change */ + CHECK_ST(dual_trace_scenario(8, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("8-regime", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("8-regime", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); tail_zero("8-regime", &R); + { int ok = 1, after = 1; + for (uint32_t i = 80; i < 120; i++) if (R.state[i] != RX_DUAL_LAMBDA_FROZEN || R.lambda[i] != R.lambda[i - 1] || R.estimate[i] != R.estimate[i - 1] || R.generation[i] != R.generation[i - 1]) ok = 0; + for (uint32_t i = 120; i < R.n; i++) if (R.state[i] != RX_DUAL_LAMBDA_UNCALIBRATED) after = 0; + snprintf(d, sizeof d, "ticks 80..119 FROZEN with lambda/estimate/generation held: %s; UNCALIBRATED resumes at 120: %s", ok ? "yes" : "no", after ? "yes" : "no"); + measure("8-regime", "M6", ok && after, d, 1); CHECK(R.generation[119] == 80 && R.lambda[120] > R.lambda[119]); } + /* 9 bursty */ + CHECK_ST(dual_trace_scenario(9, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("9-bursty", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("9-bursty", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); + { uint32_t bursts = 0; for (uint32_t i = 0; i < T.n_ticks; i++) if (T.t[i].estimate > 4200.0) bursts++; printf("INFO 9-bursty bursts=%u of %u ticks\n", bursts, T.n_ticks); CHECK(bursts > 5 && bursts < 60); } + /* 10 fan-out: one controller n = 32, 32 chains */ + { RxDualController c32; double fin[RX_DUAL_MAX_RESOURCES], mn = 1e300, mx = -1e300; char name[24]; + memset(&c32, 0, sizeof c32); c32.eta = 0.1; c32.rho = 0.05; c32.k_sigma = 2.0; c32.max_age = 4; c32.cadence = 1; c32.n = RX_DUAL_MAX_RESOURCES; + for (uint32_t r = 0; r < RX_DUAL_MAX_RESOURCES; r++) { c32.resource_id[r] = r + 1; c32.lambda_max[r] = 10.0; } + for (uint32_t r = 0; r < RX_DUAL_MAX_RESOURCES; r++) { + RxDualResource rr = fx_res(r + 1, RX_DUAL_UNIT_BYTES, DUAL_SCALE); + snprintf(name, sizeof name, "10-fanout-%02u", r); + CHECK_ST(dual_trace_scenario(10, 0, r, &T), RX_DUAL_OK); + CHECK_ST(run(name, &T, &c32, &rr, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures(name, &T, &c32, &rr, DUAL_BUDGET, &BC, &R, 1); tail_zero(name, &R); + fin[r] = R.lambda[R.n - 1]; if (fin[r] < mn) mn = fin[r]; if (fin[r] > mx) mx = fin[r]; } + snprintf(d, sizeof d, "final lambdas in [%.9g, %.9g], spread %.9g bound 0.25", mn, mx, mx - mn); measure("10-fanout", "M10", mx - mn <= 0.25, d, 1); } + /* 11 KV saturation */ + CHECK_ST(dual_trace_scenario(11, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("11-kv", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("11-kv", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); + { uint32_t cross = 0; int ok = 1; + for (uint32_t i = 0; i < T.n_ticks; i++) { if (T.t[i].estimate <= 5200.0) { if (R.lambda[i] != 0.0) ok = 0; } else if (!cross) cross = i; } + snprintf(d, sizeof d, "lambda = 0 while estimate <= 5200 (first crossing at tick %u)", cross); measure("11-kv", "M11", ok, d, 1); + monotone_from("11-kv", &R, cross); CHECK(cross == 110); } + /* 12 recovery */ + CHECK_ST(dual_trace_scenario(12, 0, 0, &T), RX_DUAL_OK); + CHECK_ST(run("12-recovery", &T, ctl, &res, DUAL_BUDGET, &BC, &R), RX_DUAL_OK); + common_measures("12-recovery", &T, ctl, &res, DUAL_BUDGET, &BC, &R, 1); tail_zero("12-recovery", &R); + { uint32_t K = (uint32_t)ceil(log(100.0) / -log(1.0 - 0.05)); double peak = R.lambda[99], at = R.lambda[99 + K]; + snprintf(d, sizeof d, "K=%u peak=%.9g lambda[%u]=%.9g <= 0.01*peak=%.9g", K, peak, 99 + K, at, 0.01 * peak); + measure("12-recovery", "M5", K == 90 && at <= 0.01 * peak, d, 1); } +} + +/* ---- recorded R15 traces ---- */ +static uint8_t g_file[16u << 20]; +static size_t read_file(const char *path) +{ + FILE *f = fopen(path, "rb"); size_t n; + if (!f) { fprintf(stderr, "FAIL cannot open %s\n", path); g_fail++; return 0; } + n = fread(g_file, 1, sizeof g_file, f); + if (!feof(f)) { fprintf(stderr, "FAIL %s larger than buffer\n", path); g_fail++; n = 0; } + fclose(f); + return n; +} + +static void recorded_one(const char *dir, const char *file, const char *cpu, const char *event) +{ + char path[512], sums[512], name[64], raw[65], d[200]; size_t n; uint8_t dig[32]; + RxDualResource res = fx_res(1, RX_DUAL_UNIT_DIMENSIONLESS, 1e9); + snprintf(path, sizeof path, "evidence/R15/raw/%s/%s", dir, file); + snprintf(sums, sizeof sums, "evidence/R15/raw/%s/SHA256SUMS", dir); + snprintf(name, sizeof name, "13-%.12s-%s-%s", dir, file[0] == 'm' ? "machine" : "preflight", cpu); + n = read_file(path); if (!n) return; + sha256_hash(g_file, n, dig); hex(dig, 32, raw); + printf("RECORDED %s bytes=%zu sha256=%s\n", path, n, raw); + CHECK_ST(rx_dual_trace_from_perf_csv(g_file, n, cpu, event, &T), RX_DUAL_OK); + printf("RECORDED %s series cpu=%s event=%s ticks=%u (UNCALIBRATED counter stream)\n", path, cpu, event, T.n_ticks); + CHECK(T.trace_kind == RX_DUAL_TRACE_RECORDED && T.n_ticks > 10); + /* the R15 run's own SHA256SUMS must list this digest (binds the trace to the sealed R15 evidence) */ + { size_t m = read_file(sums); int found = 0, listed = 0; + if (m) { g_file[m < sizeof g_file ? m : sizeof g_file - 1] = 0; listed = strstr((char *)g_file, file) != NULL; found = strstr((char *)g_file, raw) != NULL; } + if (listed) { snprintf(d, sizeof d, "raw sha256 equals the entry in the run directory SHA256SUMS: %s", found ? "yes" : "no"); measure(name, "SUM", found, d, 1); } + else printf("INFO %s %s is not listed in the run directory SHA256SUMS; its raw sha256 is recorded in this receipt only\n", name, file); } + CHECK_ST(run(name, &T, ref_ctl(), &res, 1e9, &BC, &R), RX_DUAL_OK); + common_measures(name, &T, ref_ctl(), &res, 1e9, &BC, &R, 1); + { uint32_t above = 0; for (uint32_t i = 0; i < T.n_ticks; i++) if (T.t[i].estimate > 1e9) above++; + printf("INFO %s ticks above budget 1e9: %u of %u; final lambda %.9g\n", name, above, T.n_ticks, R.lambda[R.n - 1]); } +} + +static void recorded(void) +{ + static const char *dirs[2] = { "20260929T020536Z-ad8e1f2ea4e4-silicon", "20260929T025735Z-3e9e53be3358-silicon" }; + static const uint8_t bad[] = "# c\n1.0,CPU0,<not counted>,,armv8_pmuv3_0/cycles/,1,100.00,,\n"; + static const uint8_t none[] = "# c\n1.0,CPU1,5,,armv8_pmuv3_0/cycles/,1,100.00,,\n"; + static const uint8_t two[] = "# c\n\n1.0,CPU0,5,,armv8_pmuv3_0/cycles/,1,100.00,,\n1.0,CPU0,7,,x/cycles/,1,100.00,,\n2.0,CPU0,9,,armv8_pmuv3_0/cycles/,1,100.00,,"; + for (int k = 0; k < 2; k++) { + recorded_one(dirs[k], "machine-perf.csv", "CPU0", "armv8_pmuv3_0/cycles/"); + recorded_one(dirs[k], "machine-perf.csv", "CPU7", "armv8_pmuv3_1/cycles/"); + recorded_one(dirs[k], "preflight-perf.csv", "CPU7", "armv8_pmuv3_1/cycles/"); + } + /* importer refusals and exactness */ + CHECK_ST(rx_dual_trace_from_perf_csv(bad, sizeof bad - 1, "CPU0", "armv8_pmuv3_0/cycles/", &T), RX_DUAL_ERR_ENCODING); + CHECK_ST(rx_dual_trace_from_perf_csv(none, sizeof none - 1, "CPU0", "armv8_pmuv3_0/cycles/", &T), RX_DUAL_ERR_RANGE); + CHECK_ST(rx_dual_trace_from_perf_csv(two, sizeof two - 1, "CPU0", "armv8_pmuv3_0/cycles/", &T), RX_DUAL_OK); + CHECK(T.n_ticks == 2 && T.t[0].estimate == 5.0 && T.t[1].estimate == 9.0 && T.t[1].generation == 2 && T.t[0].calibrated == 0); +} + +static void trace_refusals(void) +{ + CHECK_ST(dual_trace_scenario(2, 0, 0, &T), RX_DUAL_OK); + { RxDualTrace *t = &T; RxDualDigest d; double keep = t->t[3].estimate; + t->t[3].estimate = NAN; CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_ERR_NONFINITE); t->t[3].estimate = keep; + t->t[3].uncertainty = -1; CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_ERR_RANGE); t->t[3].uncertainty = 100; + t->t[3].regime_change = 2; CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_ERR_RANGE); t->t[3].regime_change = 0; + t->trace_kind = 0; CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_ERR_KIND); t->trace_kind = 2; + t->n_ticks = 0; CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_ERR_RANGE); t->n_ticks = RX_DUAL_TRACE_MAX_TICKS + 1; + CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_ERR_RANGE); t->n_ticks = 200; + CHECK_ST(rx_dual_digest_trace(t, &d), RX_DUAL_OK); + /* -0.0 canonicalized: same digest as +0.0 */ + { RxDualDigest a, b; t->t[5].estimate = 0.0; rx_dual_digest_trace(t, &a); t->t[5].estimate = -0.0; rx_dual_digest_trace(t, &b); CHECK(rx_dual_digest_eq(&a, &b)); t->t[5].estimate = keep; } } + { static const double x[] = { 0, 1, 2, 2, 1, 1, 3, 3 }; CHECK(rx_dual_reversals(x, 0, 8) == 2); CHECK(rx_dual_reversals(x, 0, 3) == 0); CHECK(rx_dual_peak_to_peak(x, 0, 8) == 3.0); CHECK(rx_dual_reversals(x, 4, 4) == 0); } +} + +int main(void) +{ + BC = fx_dig(0x22); BC2 = fx_dig(0x23); + trace_refusals(); + scenarios(); + recorded(); + printf("NOTE every replayed price above is UNCALIBRATED (EST-3 has not passed for any signal); none may influence production.\n"); + printf("GATE DUAL_PRICE_REFERENCE = %s (reference measures %s, negative control %s)\n", + (!g_ref_fail && g_neg_as_declared && !g_fail) ? "PASS" : "FAIL", + g_ref_fail ? "FAILED" : "all pass", g_neg_as_declared ? "failed as declared" : "DID NOT behave as declared"); + printf("test_dual_replay: %d checks, %d failures: %s\n", g_checks, g_fail, g_fail ? "FAIL" : "PASS"); + return g_fail ? 1 : 0; +}