From 5b1865bf1d7ad32de398d1cbfcb44ba87bfeb069 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Wed, 30 Sep 2026 21:35:06 -0500 Subject: [PATCH] AR2: simulated analog provider with digital oracle parity (new files) Software model of an analog in-memory matvec substrate (ARCH-0018 AR2). Declares its error model as a V2 substrate descriptor, passes V2 eligibility + digital-fallback admission, injects quantization, noise, bias, drift, saturation, temperature, latency and calibration-age non-idealities, and is checked against restated omega oracles (omega_matvec_reference, omega_matmul_cpu_oracle_i32). Refused results leave evidence and the digital realization answers under the same contract. Every evidence record is SIMULATED_DEVELOPMENT. New files only; omega untouched. Codex review (gpt-6-astra) fixes: overflow-safe offsets (128-bit, limit 2^32), mul/div saturates after dividing, EXACT refuses any injected non-ideality, digital fallback runs min_samples, relative-tolerance test isolates the rel term. Added UBSan gate. Receipt evidence/AR2/808e79f8...json. Co-Authored-By: Drake Stapleton Co-Authored-By: GPT-6 Astra (Codex) Co-Authored-By: Claude Opus 5.5 --- ...b8cfb9d7f6774dab6f7bef9c4861ebbe4b073.json | 64 ++ forge/analog-sim/forge_analog_sim.c | 518 +++++++++++++ forge/analog-sim/forge_analog_sim.h | 170 +++++ tests/run_forge_analog_sim_gates.sh | 166 +++++ tests/test_forge_analog_sim.c | 684 ++++++++++++++++++ 5 files changed, 1602 insertions(+) create mode 100644 evidence/AR2/808e79f84b01ef3acc5eb056d82b8cfb9d7f6774dab6f7bef9c4861ebbe4b073.json create mode 100644 forge/analog-sim/forge_analog_sim.c create mode 100644 forge/analog-sim/forge_analog_sim.h create mode 100755 tests/run_forge_analog_sim_gates.sh create mode 100644 tests/test_forge_analog_sim.c diff --git a/evidence/AR2/808e79f84b01ef3acc5eb056d82b8cfb9d7f6774dab6f7bef9c4861ebbe4b073.json b/evidence/AR2/808e79f84b01ef3acc5eb056d82b8cfb9d7f6774dab6f7bef9c4861ebbe4b073.json new file mode 100644 index 0000000..ebf1a07 --- /dev/null +++ b/evidence/AR2/808e79f84b01ef3acc5eb056d82b8cfb9d7f6774dab6f7bef9c4861ebbe4b073.json @@ -0,0 +1,64 @@ +{ + "gate": "AR2_ANALOG_SIMULATION_PROVIDER", + "workstream": "ARCH-0018 AR2", + "provenance_class": "SIMULATED_DEVELOPMENT", + "note": "Software model of an analog in-memory matvec substrate. No device acted. Errors are simulated errors against the digital oracle.", + "status": "PASS", + "checks_passed": 52, + "checks_failed": 0, + "evidence_records": 83, + "evidence_records_simulated_development": 83, + "cases": [ + {"name": "exact_ideal_sim_vs_u64_oracle", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "e9bbe72a489d6e03fedbc9d7e3b767a6402c8d721a2fffce6883d3abd541adb3", "published_evidence_digest": "e9bbe72a489d6e03fedbc9d7e3b767a6402c8d721a2fffce6883d3abd541adb3", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_ideal_sim_vs_u64_oracle", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "a885a8a70d90c707e5b9e29b6ff7578b2636c77aa98f5741291a51f57bd643b4", "published_evidence_digest": "a885a8a70d90c707e5b9e29b6ff7578b2636c77aa98f5741291a51f57bd643b4", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_ideal_sim_vs_u64_oracle", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "cc3f3ce48c9612d4c51938bf57a146ac65187ef2a4915813b0e72292db068765", "published_evidence_digest": "cc3f3ce48c9612d4c51938bf57a146ac65187ef2a4915813b0e72292db068765", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_ideal_sim_vs_u64_oracle", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "8e293606d1b503327d4b8329f56656db645dddbe47776be5aaea446c0b6a25df", "published_evidence_digest": "8e293606d1b503327d4b8329f56656db645dddbe47776be5aaea446c0b6a25df", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_ideal_sim_vs_u64_oracle", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "33179922e8f950fc77baca57cd9e64e550d34ac0fe7c984defa9e80353884a39", "published_evidence_digest": "33179922e8f950fc77baca57cd9e64e550d34ac0fe7c984defa9e80353884a39", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_output_identity_equals_oracle_digest", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "c55dcf5b54208ac3ea1c671136f13a7816dd3a78fe25c7b9a10a4b400508a944", "published_evidence_digest": "c55dcf5b54208ac3ea1c671136f13a7816dd3a78fe25c7b9a10a4b400508a944", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_oracle_digest_via_digital", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "1948e2bb76cda636c127e5e4d9c6d0c24d51b067c08dc7b4364e9e55faf0ce0a", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "tolerance_bounded_deterministic_i32", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 36, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 11, "analog_evidence_digest": "aa968cc316f3042627be6254ccaac03a5ebc2ff69a7f45eaa866dabb1abdfddd", "published_evidence_digest": "aa968cc316f3042627be6254ccaac03a5ebc2ff69a7f45eaa866dabb1abdfddd", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "tolerance_bounded_stochastic_i32", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 8, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 16, "simulated_error_vs_oracle_abs": 8, "analog_evidence_digest": "2ab0bd8ed5d396a9e9428b3010538f8ee705aa3e91161214582334b8fad54db6", "published_evidence_digest": "2ab0bd8ed5d396a9e9428b3010538f8ee705aa3e91161214582334b8fad54db6", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "tolerance_rel_part_admits_error_above_abs", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matvec_reference", "contract_error_abs": 40, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 48, "analog_evidence_digest": "5f14e8196085fd8ef683b6d39080c6f166cc9f6b4ec8fbee09456c8e1a05e847", "published_evidence_digest": "5f14e8196085fd8ef683b6d39080c6f166cc9f6b4ec8fbee09456c8e1a05e847", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "tolerance_one_lsb_over_bound_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matvec_reference", "contract_error_abs": 40, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 49, "analog_evidence_digest": "73ebddefa50f5835d560b4af1219427e3dad4c02316fd41c4e87f0d71b69aa7b", "published_evidence_digest": "951b789f6102844e2848d60a7161fbe54be698f762be55a5e3c96249308807dc", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_quantization_within_bound_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 2, "analog_evidence_digest": "d5d4e33805fa2d8fe1f0f9ea2d45df31deb4b810c41d70bf8152ac1e8875866a", "published_evidence_digest": "d5d4e33805fa2d8fe1f0f9ea2d45df31deb4b810c41d70bf8152ac1e8875866a", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_quantization_over_bound_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 32, "analog_evidence_digest": "7bd47975c6d35c632a73d9fb3ff166c8e4a803720a167f38d694a920beabda79", "published_evidence_digest": "a34bf77e2e4ae728e0139031dc407d4f49e138cef62a59331e5aac9852aa9983", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_noise_within_bound_published", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 3, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 16, "simulated_error_vs_oracle_abs": 3, "analog_evidence_digest": "3bed38556c4e3b6473363908eecb592cff07e2250ac4e9c0568e2a7e96a1629c", "published_evidence_digest": "3bed38556c4e3b6473363908eecb592cff07e2250ac4e9c0568e2a7e96a1629c", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_noise_over_bound_refused", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 3, "outcome": "published_digital_fallback", "analog_refusal": "confidence_not_met", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 298, "analog_evidence_digest": "cc2a0a26d7a87534903f3396b4ff5f90c3e9c21d88c6e66e938ad1823a102e16", "published_evidence_digest": "1de5c73d8f09d39a7e54ab3968494ddbde4ca5a876f802c310912033b2a36e5d", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_bias_within_bound_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 5, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 5, "analog_evidence_digest": "02ae780c3ffaf6a534f7c3fe2f18d646952f871ebdd2e8391be1f10353392d8a", "published_evidence_digest": "02ae780c3ffaf6a534f7c3fe2f18d646952f871ebdd2e8391be1f10353392d8a", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_bias_over_bound_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 5, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 9, "analog_evidence_digest": "2a323a5963d63c6d65d35a44841d74f372511c86f42560ccc423269901df03e6", "published_evidence_digest": "3991f71b680572c8ae55e4c3c0b66d6bc6bd7c16c4532d0f27c20b1fc2c4f67b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_drift_within_bound_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 24, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 10, "analog_evidence_digest": "b883f926a993c3871294de0e380302f3d8c5de817bb8e8fe00ff69eed70296fe", "published_evidence_digest": "b883f926a993c3871294de0e380302f3d8c5de817bb8e8fe00ff69eed70296fe", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_drift_over_bound_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 24, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 100, "analog_evidence_digest": "10aa33700f079719ee7d2a83bcadb352d841d6a764193eafb9cf586bb64d4759", "published_evidence_digest": "9197cc512c813a4dd29c1825c105db408c8bbcf68879be882c2d31bff296dd09", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_saturation_within_range_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "8ff64706b61bba3689836109225265e1e4b75c0f13e26ab0dc3ec597437e025a", "published_evidence_digest": "8ff64706b61bba3689836109225265e1e4b75c0f13e26ab0dc3ec597437e025a", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_saturation_clipping_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 856842, "analog_evidence_digest": "ce29c2e5d00e12e7bd2685949d3f990c5c318f997834f8c3bce9f9ef4ef2d90a", "published_evidence_digest": "9d355a4434ab59430e2c4dd55bd606f46812f1f5fe590ac1f68e1ccc8d603cb7", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_temperature_within_bound_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 10, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 4, "analog_evidence_digest": "106115ab6493927d0b610060c2508288fa9c481a608d15738fec06689297c558", "published_evidence_digest": "106115ab6493927d0b610060c2508288fa9c481a608d15738fec06689297c558", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_temperature_over_bound_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 10, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 40, "analog_evidence_digest": "19cedfc68f09f066d2fcff178326afd3c11c44ca56c22f35a4338315fadedb2a", "published_evidence_digest": "91d5d97f663829ac07acbb7f10dfdebd9ef8f1fbccc459f60da5fe1cdb72b98c", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_latency_within_budget_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 1, "analog_evidence_digest": "80817f80420c1282ab3d20efa2fc56d93402a975856b1a1e4449fece147047a4", "published_evidence_digest": "80817f80420c1282ab3d20efa2fc56d93402a975856b1a1e4449fece147047a4", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_latency_over_budget_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1, "outcome": "published_digital_fallback", "analog_refusal": "latency_actual_over_budget", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 1, "analog_evidence_digest": "d4dc245067d44abd3807ba9d5cb6c28126f6c29875d21166cd6aae46ad203bfd", "published_evidence_digest": "9d355a4434ab59430e2c4dd55bd606f46812f1f5fe590ac1f68e1ccc8d603cb7", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_combined_within_bound_published", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 36, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 16, "simulated_error_vs_oracle_abs": 13, "analog_evidence_digest": "fcd4e5ce123ec93a370a8050d9783391a44bddffb5f7597436916f7562fbbed5", "published_evidence_digest": "fcd4e5ce123ec93a370a8050d9783391a44bddffb5f7597436916f7562fbbed5", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_combined_over_bound_refused", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 36, "outcome": "published_digital_fallback", "analog_refusal": "confidence_not_met", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 87, "analog_evidence_digest": "1d41f0e38455ce954474fd33ac67da9b17f7c425ce12811f487494b710c1e7ae", "published_evidence_digest": "96932b23037f79d67c6369beadfb947f2dfffac26e139429af34011bb7f1c44d", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_calibration_age_inside_window_published", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_simulated_analog", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 2, "analog_evidence_digest": "f1c3a29f265b86a47af3128a05621e5820fbcd4544c1fd19bc37d5d81848f75a", "published_evidence_digest": "f1c3a29f265b86a47af3128a05621e5820fbcd4544c1fd19bc37d5d81848f75a", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_calibration_age_stale_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "calibration stale", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "68655c2707581e540bbc260a581b47addba69e772e238dc14f4716bff4f75d3a", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_calibration_from_future_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "calibration stale", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "710db297e6f4c32b7743cd0cbc3d3e4ffd98d3504eacb19cd792181cc55fbcf4", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_temperature_outside_envelope_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "conditions outside calibration envelope", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "c30ecf97c03fbce6f9e8ce9d4eaf3282523b60f39ef0ee27666c521e571fd92e", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "inject_latency_declared_over_budget_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "latency_declared_over_budget", "eligibility": "ok", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "714e972ad7c2a00d80c971969f86bdd2bd789d1a79741ddf368b77f9fbb4a21e", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "declared_bound_over_contract_refused", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "0148e904485797762ed3805cfd4bab6622a12d27ee49ac7b681936c14aadaf12", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_declared_quantization", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_declared_noise", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_declared_bias", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_declared_drift", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_declared_temperature", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_declared_saturation", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_false_ideal_claim_caught", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "exact_digest_mismatch", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 1, "analog_evidence_digest": "3d098c1c831abae2bbc4fe5e939671ab8b126109f859e0ea385a7c5e5bb8a79f", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_refuses_injected_nonideality_even_on_matching_output", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "exact_digest_mismatch", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "238f2e48bd11f2524b1d7665d423031270e45f1d2a00f3656ec890ec84a4fec2", "published_evidence_digest": "47777aff27bdd7245fa53ac82eb0c6ff5e1a6a36b6c28539b3ebd53b2248359f", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "deterministic_contract_refuses_stochastic_substrate", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1000, "outcome": "published_digital_fallback", "analog_refusal": "ineligible", "eligibility": "value rule violated", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "ab681630368c62db3e6c9df109da894e7d32f1493ab210634f83105ece9859b0", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "deterministic_contract_catches_hidden_noise", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 1000, "outcome": "published_digital_fallback", "analog_refusal": "nondeterministic_runs", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 4, "simulated_error_vs_oracle_abs": 2, "analog_evidence_digest": "abf712cdea367239ebfdf3e475d6aa92b7a8f2ea1aad2b6c989454479b75e274", "published_evidence_digest": "ab681630368c62db3e6c9df109da894e7d32f1493ab210634f83105ece9859b0", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "wrong_answer_exact_caught", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "exact_digest_mismatch", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 1, "analog_evidence_digest": "6090f16ff7258c2cbd70df3bfc7b6d62638c29bb9b2a6d2aa2ce0da55d094a4b", "published_evidence_digest": "301206845dc593f11e1c5580ef1f92a6ab2d518fe32fe9ab7d1caad136f5ef1b", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "wrong_answer_bounded_deterministic_caught", "contract_kind": "BOUNDED_DETERMINISTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "error_over_bound", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 498, "analog_evidence_digest": "3d263a9973bf4484a775435412932a8cb8cd92093bf2e4b8f29891c2a6f1cd9b", "published_evidence_digest": "fc658e7d1db4ad67cec3c46dc0cc8b3feee65278ded26e8b997699eb5e05c0c6", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "wrong_answer_bounded_stochastic_caught", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "confidence_not_met", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 52, "analog_evidence_digest": "1f91a24497dd21e1361b3904102b72d27351d5017c33ba8a3b3844e347f9c3b0", "published_evidence_digest": "3a9878ae3d2aaeeb0f228af96fa7a37b3ae9ba38fec13324d07f72ba181f5b35", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "no_digital_fallback_plan_not_admitted", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "not_admitted_no_digital_fallback", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "no_digital_fallback_refused_nothing_published", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "not_admitted_no_digital_fallback", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "fallback_digital_answers_same_contract", "contract_kind": "EXACT", "oracle": "omega_matvec_reference", "contract_error_abs": 0, "outcome": "published_digital_fallback", "analog_refusal": "exact_digest_mismatch", "eligibility": "ok", "analog_ran": 1, "runs": 4, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 7, "analog_evidence_digest": "62641ae6e39b69d436189ed1d0c95448c410cc168928870a7a8ddf9a20260ae1", "published_evidence_digest": "7f9baf8262fbec02ac14fd9e0c119130959fac7d0872a94b462e498a83da45c0", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "fallback_meets_stochastic_sample_count", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "published_digital_fallback", "analog_refusal": "confidence_not_met", "eligibility": "ok", "analog_ran": 1, "runs": 16, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 495, "analog_evidence_digest": "dbca837fb4264659a8a2159581b10a2e38744db960ec4e5f991fdc2a6020932e", "published_evidence_digest": "d4898067d439d7b94d99c1c20acec7810ff1b079c91d797f26eb92a8ea002f9d", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "input_out_of_declared_range_refused", "contract_kind": "BOUNDED_STOCHASTIC", "oracle": "omega_matmul_cpu_oracle_i32", "contract_error_abs": 2, "outcome": "input_out_of_range", "analog_refusal": "none", "eligibility": "ok", "analog_ran": 0, "runs": 0, "runs_within_bound": 0, "simulated_error_vs_oracle_abs": 0, "analog_evidence_digest": "", "published_evidence_digest": "", "provenance_class": "SIMULATED_DEVELOPMENT"} + ] +} diff --git a/forge/analog-sim/forge_analog_sim.c b/forge/analog-sim/forge_analog_sim.c new file mode 100644 index 0000000..b383aa9 --- /dev/null +++ b/forge/analog-sim/forge_analog_sim.c @@ -0,0 +1,518 @@ +/* + * forge/analog-sim/forge_analog_sim.c -- AR2 simulated analog provider. + * See forge_analog_sim.h. Every result here is SIMULATED_DEVELOPMENT. + */ +#include "forge_analog_sim.h" + +#include + +#define ASIM_MAGNITUDE_LIMIT (1ll << 32) /* model offsets beyond this are refused; keeps every sum inside int64 */ + +static const char *const ORACLE_LABEL_EXACT_U64 = + "FORGE-ASIM/oracle/omega_matvec_reference/src/omega_matvec.h:74/u64-wrapping"; +static const char *const ORACLE_LABEL_I32 = + "FORGE-ASIM/oracle/omega_matmul_cpu_oracle_i32/src/omega_blackwell_matmul.h:50/u32-wrapping/n=1"; +static const char *const SUBSTRATE_LABEL = "FORGE-ASIM/substrate/analog-in-memory/simulated/v1"; +static const char *const DIGITAL_LABEL = "FORGE-ASIM/substrate/digital-cpu/v1"; +static const char *const ANALOG_CODE_LABEL = "FORGE-ASIM/code/analog-matvec-simulation/v1"; +static const char *const DIGITAL_CODE_LABEL = "FORGE-ASIM/code/digital-matvec-oracle/v1"; + +static void label_digest(const char *label, uint8_t out[32]) +{ + forge_v2_sha256((const uint8_t *)label, strlen(label), out); +} + +/* ---- oracles ---- */ + +/* Restatement of omega_matvec_reference (omega src/omega_matvec.c:265). */ +void forge_asim_oracle_exact_u64(const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, uint32_t N) +{ + if (!A || !x || !y) return; + for (uint32_t i = 0; i < M; ++i) { + uint64_t acc = 0; + for (uint32_t j = 0; j < N; ++j) acc += A[(size_t)i * N + j] * x[j]; + y[i] = acc; + } +} + +/* Restatement of omega_matmul_cpu_oracle_i32 (omega src/omega_blackwell_matmul.c:41) + * with n = 1: c[i] = sum_p a[i*k + p] * b[p], wrapping uint32. */ +int forge_asim_oracle_i32(const uint32_t *a, const uint32_t *x, uint32_t *y, uint32_t M, uint32_t N) +{ + if (!a || !x || !y || M == 0 || N == 0) return -1; + for (uint32_t i = 0; i < M; i++) { + uint32_t acc = 0; + for (uint32_t p = 0; p < N; p++) acc += a[i * N + p] * x[p]; + y[i] = acc; + } + return 0; +} + +int forge_asim_oracle_digest(uint32_t oracle, uint8_t out[32]) +{ + if (!out) return FORGE_V2_ERR_ARG; + switch (oracle) { + case FORGE_ASIM_ORACLE_EXACT_U64: label_digest(ORACLE_LABEL_EXACT_U64, out); return FORGE_V2_OK; + case FORGE_ASIM_ORACLE_I32: label_digest(ORACLE_LABEL_I32, out); return FORGE_V2_OK; + default: return FORGE_V2_ERR_ARG; + } +} + +static uint32_t oracle_of(const uint8_t digest[32]) +{ + uint8_t d[32]; + forge_asim_oracle_digest(FORGE_ASIM_ORACLE_EXACT_U64, d); + if (memcmp(d, digest, 32) == 0) return FORGE_ASIM_ORACLE_EXACT_U64; + forge_asim_oracle_digest(FORGE_ASIM_ORACLE_I32, d); + if (memcmp(d, digest, 32) == 0) return FORGE_ASIM_ORACLE_I32; + return 0; +} + +/* ---- error model ---- */ + +static uint64_t mag(int64_t v) { return v < 0 ? (uint64_t)0 - (uint64_t)v : (uint64_t)v; } + +static uint64_t sat_add(uint64_t a, uint64_t b) { return a > UINT64_MAX - b ? UINT64_MAX : a + b; } + +/* ceil(a * b / d) with a 128-bit product, saturating at UINT64_MAX only + * after the division; d > 0 */ +static uint64_t mul_div_ceil(uint64_t a, uint64_t b, uint64_t d) +{ + unsigned __int128 p = (unsigned __int128)a * b; + unsigned __int128 q = p / d + (p % d != 0); + return q > UINT64_MAX ? UINT64_MAX : (uint64_t)q; +} + +/* signed a * b / d truncated toward zero, 128-bit product, saturating at + * +/-2^61 after the division (three such terms still sum inside int64) */ +static int64_t mul_div_trunc(int64_t a, int64_t b, int64_t d) +{ + __int128 q = ((__int128)a * b) / d; + const __int128 lim = (__int128)1 << 61; + return (int64_t)(q > lim ? lim : (q < -lim ? -lim : q)); +} + +static int model_ok(const ForgeAnalogSimModel *m) +{ + return mag(m->bias_lsb) <= (uint64_t)ASIM_MAGNITUDE_LIMIT && + mag(m->drift_lsb_per_hour) <= (uint64_t)ASIM_MAGNITUDE_LIMIT && + mag(m->temp_lsb_per_k) <= (uint64_t)ASIM_MAGNITUDE_LIMIT; +} + +static int model_ideal(const ForgeAnalogSimModel *m) +{ + return m->quant_step_lsb <= 1 && m->bias_lsb == 0 && m->noise_peak_lsb == 0 && m->drift_lsb_per_hour == 0 && + m->temp_lsb_per_k == 0 && m->saturation_max == 0; +} + +uint64_t forge_asim_model_bound(const ForgeAnalogSimModel *m, uint64_t cal_age_s, uint32_t temp_delta_mc) +{ + uint64_t b = 0; + if (!m) return UINT64_MAX; + if (m->quant_step_lsb > 1) b = m->quant_step_lsb / 2; + b = sat_add(b, mag(m->bias_lsb)); + b = sat_add(b, m->noise_peak_lsb); + b = sat_add(b, mul_div_ceil(mag(m->drift_lsb_per_hour), cal_age_s, 3600)); + b = sat_add(b, mul_div_ceil(mag(m->temp_lsb_per_k), temp_delta_mc, 1000)); + return b; +} + +/* ---- descriptors ---- */ + +void forge_asim_substrate_identity(uint8_t out[32]) { label_digest(SUBSTRATE_LABEL, out); } + +int forge_asim_describe(const ForgeAnalogSimModel *declared, const ForgeCalibrationArtifact *cal, + ForgeSubstrateDescriptor *s) +{ + if (!declared || !cal || !s || !model_ok(declared)) return FORGE_V2_ERR_ARG; + if (cal->env_temp_max_mc < cal->env_temp_min_mc || cal->validity_window_s == 0) return FORGE_V2_ERR_ARG; + memset(s, 0, sizeof *s); + forge_asim_substrate_identity(s->substrate_identity); + s->substrate_class = FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY; + s->operation_families = FORGE_V2_OP_MATVEC; + s->input_repr = FORGE_V2_REPR_ANALOG_VOLTAGE; s->input_bits = FORGE_ASIM_INPUT_BITS; + s->output_repr = FORGE_V2_REPR_ANALOG_CURRENT; s->output_bits = FORGE_ASIM_OUTPUT_BITS; + s->state_model = FORGE_V2_STATE_PERSISTENT_WEIGHTS; + s->sync_model = FORGE_V2_SYNC_BATCH_BARRIER; + s->memory_model = FORGE_V2_MEM_IN_MEMORY_COMPUTE; + s->memory_capacity_bytes = (uint64_t)FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS; + s->numerical.dynamic_range_mdb = 144000; /* 24 bits */ + s->numerical.resolution_bits = FORGE_ASIM_OUTPUT_BITS; + s->numerical.settling_ns = declared->latency_ns; + s->numerical.calibration_validity_s = cal->validity_window_s; + s->calibration_required = 1; + s->calibration_method_mask = 1u << FORGE_V2_CAL_SIMULATED_MODEL; + s->calibration_max_window_s = cal->validity_window_s; + s->calibration_min_fixtures = 4; + /* worst case over the whole calibration window and envelope */ + uint32_t half_mc = (uint32_t)(((int64_t)cal->env_temp_max_mc - cal->env_temp_min_mc + 1) / 2); + uint64_t bound = forge_asim_model_bound(declared, cal->validity_window_s, half_mc); + if (model_ideal(declared)) { + s->error_kind = FORGE_V2_ERROR_EXACT; + } else { + s->error_kind = declared->noise_peak_lsb ? FORGE_V2_ERROR_BOUNDED_STOCHASTIC + : FORGE_V2_ERROR_BOUNDED_DETERMINISTIC; + s->error_abs = bound ? bound : 1; /* a non-exact substrate never declares zero error */ + } + s->fault_detect_mask = 0; + s->timing_min_latency_ns = declared->latency_ns; + s->timing_conversion_ns = declared->latency_ns; + s->timing_deterministic = declared->noise_peak_lsb ? 0 : 1; + s->limit_max_rows = FORGE_ASIM_MAX_ROWS; + s->limit_max_cols = FORGE_ASIM_MAX_COLS; + s->limit_max_state_bytes = (uint64_t)FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS; + s->limit_max_concurrent = 1; + s->evidence_capabilities = FORGE_V2_EVCAP_DURATION | FORGE_V2_EVCAP_TEMPERATURE | + FORGE_V2_EVCAP_MEASURED_ERROR | FORGE_V2_EVCAP_REPEATS; + return FORGE_V2_OK; +} + +void forge_asim_digital_descriptor(ForgeSubstrateDescriptor *s) +{ + memset(s, 0, sizeof *s); + label_digest(DIGITAL_LABEL, s->substrate_identity); + s->substrate_class = FORGE_V2_SUBSTRATE_DIGITAL_CPU; + s->operation_families = FORGE_V2_OP_MATVEC | FORGE_V2_OP_MATMUL; + s->input_repr = FORGE_V2_REPR_DIGITAL_INT; s->input_bits = 64; + s->output_repr = FORGE_V2_REPR_DIGITAL_INT; s->output_bits = 64; + s->state_model = FORGE_V2_STATE_STATELESS; + s->sync_model = FORGE_V2_SYNC_CLOCKED; + s->memory_model = FORGE_V2_MEM_SHARED_COHERENT; + s->numerical.resolution_bits = 64; + s->error_kind = FORGE_V2_ERROR_EXACT; + s->timing_deterministic = 1; + s->limit_max_rows = FORGE_ASIM_MAX_ROWS; + s->limit_max_cols = FORGE_ASIM_MAX_COLS; + s->limit_max_concurrent = 1; + s->evidence_capabilities = FORGE_V2_EVCAP_REPEATS; +} + +/* ---- simulation ---- */ + +static uint64_t splitmix64(uint64_t z) +{ + z += 0x9E3779B97F4A7C15ull; + z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; + z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; + return z ^ (z >> 31); +} + +static int shape_ok(const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N) +{ + const uint64_t lim = 1ull << FORGE_ASIM_INPUT_BITS; + if (!A || !x || M == 0 || N == 0 || M > FORGE_ASIM_MAX_ROWS || N > FORGE_ASIM_MAX_COLS) return 0; + for (size_t k = 0; k < (size_t)M * N; k++) if (A[k] >= lim) return 0; + for (uint32_t j = 0; j < N; j++) if (x[j] >= lim) return 0; + return 1; +} + +int forge_asim_realize(const ForgeAnalogSimModel *m, uint32_t wrong_row, int64_t wrong_delta, + const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, + uint32_t run_index, uint64_t cal_age_s, int32_t temp_delta_mc, + uint64_t *y, uint64_t *latency_ns) +{ + if (!m || !y || !latency_ns || !model_ok(m) || mag(wrong_delta) > (uint64_t)ASIM_MAGNITUDE_LIMIT) + return FORGE_ASIM_ERR_ARG; + if (!shape_ok(A, x, M, N)) return FORGE_ASIM_ERR_INPUT_RANGE; + const uint64_t age_cap = 1ull << 40; + int64_t age = (int64_t)(cal_age_s > age_cap ? age_cap : cal_age_s); + int64_t offset = m->bias_lsb + mul_div_trunc(m->drift_lsb_per_hour, age, 3600) + + mul_div_trunc(m->temp_lsb_per_k, temp_delta_mc, 1000); + for (uint32_t i = 0; i < M; i++) { + /* the ideal crossbar column current: exact for inputs within limits */ + int64_t v = 0; + for (uint32_t j = 0; j < N; j++) v += (int64_t)(A[(size_t)i * N + j] * x[j]); + v += offset; + if (m->noise_peak_lsb) { + uint64_t r = splitmix64(m->noise_seed ^ splitmix64(((uint64_t)run_index << 32) | i)); + v += (int64_t)(r % (2ull * m->noise_peak_lsb + 1)) - (int64_t)m->noise_peak_lsb; + } + if (v < 0) v = 0; /* no negative output current */ + if (m->quant_step_lsb > 1) { + int64_t q = m->quant_step_lsb; + v = ((v + q / 2) / q) * q; + } + if (m->saturation_max && (uint64_t)v > m->saturation_max) v = (int64_t)m->saturation_max; + if (wrong_row && i == wrong_row - 1) { + v += wrong_delta; + if (v < 0) v = 0; + } + y[i] = (uint64_t)v; + } + *latency_ns = m->latency_ns; + return FORGE_ASIM_PUBLISHED_ANALOG; +} + +/* ---- evidence ---- */ + +uint32_t forge_asim_provenance(void) { return FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; } + +const char *forge_asim_refuse_name(uint32_t r) +{ + switch (r) { + case FORGE_ASIM_REFUSE_NONE: return "none"; + case FORGE_ASIM_REFUSE_INELIGIBLE: return "ineligible"; + case FORGE_ASIM_REFUSE_LATENCY_DECLARED: return "latency_declared_over_budget"; + case FORGE_ASIM_REFUSE_EXACT_MISMATCH: return "exact_digest_mismatch"; + case FORGE_ASIM_REFUSE_ERROR_BOUND: return "error_over_bound"; + case FORGE_ASIM_REFUSE_CONFIDENCE: return "confidence_not_met"; + case FORGE_ASIM_REFUSE_NONDETERMINISM: return "nondeterministic_runs"; + case FORGE_ASIM_REFUSE_LATENCY_ACTUAL: return "latency_actual_over_budget"; + case FORGE_ASIM_REFUSE_UNSUPPORTED: return "unsupported_contract"; + default: return "unknown"; + } +} + +static size_t put_u64(uint8_t *p, uint64_t v) +{ + for (int b = 0; b < 8; b++) p[b] = (uint8_t)(v >> (8 * b)); + return 8; +} + +static void vec_digest(const char *domain, const uint64_t *v, size_t n, uint8_t out[32]) +{ + uint8_t buf[64 + 8 * FORGE_ASIM_MAX_ROWS]; + size_t len = strlen(domain), off; + memcpy(buf, domain, len); + off = len; + off += put_u64(buf + off, n); + for (size_t k = 0; k < n; k++) off += put_u64(buf + off, v[k]); + forge_v2_sha256(buf, off, out); +} + +static void input_digest(const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, uint8_t out[32]) +{ + static const char dom[] = "FORGE-ASIM/input/matvec/v1"; + uint8_t buf[sizeof dom + 16 + 8 * (FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS + FORGE_ASIM_MAX_COLS)]; + size_t off = sizeof dom - 1; + memcpy(buf, dom, off); + off += put_u64(buf + off, M); + off += put_u64(buf + off, N); + for (size_t k = 0; k < (size_t)M * N; k++) off += put_u64(buf + off, A[k]); + for (uint32_t j = 0; j < N; j++) off += put_u64(buf + off, x[j]); + forge_v2_sha256(buf, off, out); +} + +/* Fill the fields every record of this provider shares, then validate and + * digest it through the V2 encoder. Provenance is fixed here and nowhere + * else. */ +static int seal_evidence(ForgeExecutionEvidenceV2 *e, uint8_t digest[32]) +{ + uint8_t buf[4096]; + size_t n; + e->provenance_class = forge_asim_provenance(); + e->energy_source = FORGE_V2_ENERGY_NOT_MEASURED; + e->energy_nj = 0; + if (forge_v2_encode_evidence(e, buf, sizeof buf, &n) != FORGE_V2_OK) return FORGE_ASIM_ERR_EVIDENCE; + forge_v2_sha256(buf, n, digest); + return 0; +} + +/* ---- the whole path ---- */ + +static void compute_ref(uint32_t oracle, const ForgeAsimRequest *rq, uint64_t *ref) +{ + if (oracle == FORGE_ASIM_ORACLE_EXACT_U64) { + forge_asim_oracle_exact_u64(rq->A, rq->x, ref, rq->M, rq->N); + } else { + uint32_t a32[FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS], x32[FORGE_ASIM_MAX_COLS], y32[FORGE_ASIM_MAX_ROWS]; + for (size_t k = 0; k < (size_t)rq->M * rq->N; k++) a32[k] = (uint32_t)rq->A[k]; + for (uint32_t j = 0; j < rq->N; j++) x32[j] = (uint32_t)rq->x[j]; + forge_asim_oracle_i32(a32, x32, y32, rq->M, rq->N); + for (uint32_t i = 0; i < rq->M; i++) ref[i] = y32[i]; + } +} + +/* The digital realization: the contract's oracle on the CPU, repeated for + * the contract's min_samples so its evidence meets the same contract. */ +static int run_digital(const ForgeAsimSetup *st, const ForgeAsimRequest *rq, uint32_t oracle, + const uint8_t contract_digest[32], const uint8_t input_id[32], const uint64_t *ref, + ForgeAsimOutcome *out) +{ + ForgeSubstrateDescriptor d; + uint8_t sd[32], code[32]; + uint64_t again[FORGE_ASIM_MAX_ROWS]; + uint32_t runs = rq->contract.min_samples, same = 0; + ForgeExecutionEvidenceV2 *e = &out->final_evidence; + for (uint32_t r = 0; r < runs; r++) { + compute_ref(oracle, rq, again); + if (memcmp(again, ref, (size_t)rq->M * sizeof ref[0]) == 0) same++; + } + if (same != runs) return FORGE_ASIM_ERR_EVIDENCE; /* a digital oracle that disagrees with itself */ + forge_asim_digital_descriptor(&d); + forge_v2_digest_substrate(&d, sd); + label_digest(DIGITAL_CODE_LABEL, code); + memset(e, 0, sizeof *e); + forge_v2_realization_identity(contract_digest, st->machine_identity, sd, code, NULL, e->realization_identity); + memcpy(e->machine_identity, st->machine_identity, 32); + memcpy(e->substrate_identity, d.substrate_identity, 32); + memcpy(e->semantic_contract_digest, contract_digest, 32); + memcpy(e->input_identity, input_id, 32); + vec_digest("FORGE-ASIM/output/matvec/v1", ref, rq->M, e->output_identity); + e->execution_epoch_s = rq->now.execution_epoch_s; + e->env_temperature_mc = rq->now.temperature_mc; + e->env_supply_uv = rq->now.supply_uv; + e->confidence_ppm = 1000000; + e->repeat_runs = runs; + e->repeat_passes = same; + e->fault_state = FORGE_V2_FAULT_NONE; + if (seal_evidence(e, out->final_evidence_digest) != 0) return FORGE_ASIM_ERR_EVIDENCE; + memcpy(out->y, ref, (size_t)rq->M * sizeof ref[0]); + out->published = 1; + return FORGE_ASIM_PUBLISHED_FALLBACK; +} + +int forge_asim_execute(const ForgeAsimSetup *st, const ForgeAsimRequest *rq, ForgeAsimOutcome *out) +{ + ForgeSubstrateDescriptor analog, digital; + ForgeV2Candidate set[2]; + size_t nset = 0; + uint8_t contract_digest[32], analog_digest[32], cal_digest[32], input_id[32], ref_id[32]; + uint64_t ref[FORGE_ASIM_MAX_ROWS]; + uint64_t first[FORGE_ASIM_MAX_ROWS], run_y[FORGE_ASIM_MAX_ROWS], chosen[FORGE_ASIM_MAX_ROWS]; + int rc; + + if (!out) return FORGE_ASIM_ERR_ARG; + memset(out, 0, sizeof *out); + out->status = FORGE_ASIM_ERR_ARG; + if (!st || !rq) return out->status; + const ForgeSemanticResultContract *c = &rq->contract; + if (c->operation_family != FORGE_V2_OP_MATVEC || c->shape_rank != 2 || c->shape_dim[0] != rq->M || + c->shape_dim[1] != rq->N) + return out->status; + uint32_t oracle = oracle_of(c->reference_oracle_digest); + if (!oracle || c->min_samples == 0 || c->min_samples > FORGE_ASIM_MAX_SAMPLES) return out->status; + if (!shape_ok(rq->A, rq->x, rq->M, rq->N)) return out->status = FORGE_ASIM_ERR_INPUT_RANGE; + if (forge_v2_digest_contract(c, contract_digest) != FORGE_V2_OK) return out->status; + if (forge_asim_describe(&st->declared, &st->cal, &analog) != FORGE_V2_OK) return out->status; + if (!model_ok(&st->actual)) return out->status; + forge_v2_digest_substrate(&analog, analog_digest); + memcpy(out->analog_substrate_digest, analog_digest, 32); + if (forge_v2_digest_calibration(&st->cal, cal_digest) != FORGE_V2_OK) return out->status; + + /* the reference answer, from the oracle the contract names */ + compute_ref(oracle, rq, ref); + input_digest(rq->A, rq->x, rq->M, rq->N, input_id); + vec_digest("FORGE-ASIM/output/matvec/v1", ref, rq->M, ref_id); + + /* eligibility of the simulated analog realization */ + out->eligibility_rc = forge_v2_substrate_eligible(&analog, c, &st->cal, FORGE_V2_FAULT_NONE, &rq->now); + if (out->eligibility_rc != FORGE_V2_OK) out->refuse_reason = FORGE_ASIM_REFUSE_INELIGIBLE; + else if (rq->latency_budget_ns && st->declared.latency_ns > rq->latency_budget_ns) + out->refuse_reason = FORGE_ASIM_REFUSE_LATENCY_DECLARED; + else if (c->error_norm != FORGE_V2_NORM_ELEMENTWISE_MAX || c->error_kind == FORGE_V2_ERROR_MEASURED_DISTRIBUTION) + out->refuse_reason = FORGE_ASIM_REFUSE_UNSUPPORTED; + if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { + memcpy(set[nset].contract_digest, contract_digest, 32); + memcpy(set[nset].machine_identity, st->machine_identity, 32); + memcpy(set[nset].substrate_digest, analog_digest, 32); + set[nset].substrate_class = analog.substrate_class; + nset++; + } + forge_asim_digital_descriptor(&digital); + if (st->include_digital && + forge_v2_substrate_eligible(&digital, c, NULL, FORGE_V2_FAULT_NONE, &rq->now) == FORGE_V2_OK) { + memcpy(set[nset].contract_digest, contract_digest, 32); + memcpy(set[nset].machine_identity, st->machine_identity, 32); + forge_v2_digest_substrate(&digital, set[nset].substrate_digest); + set[nset].substrate_class = digital.substrate_class; + nset++; + } + /* admission: the eligible set must hold a digital realization */ + if (forge_v2_admit_plan(set, nset, contract_digest) != FORGE_V2_OK) + return out->status = FORGE_ASIM_ERR_NO_FALLBACK; + + if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { + uint32_t runs = c->min_samples; + uint64_t cal_age = rq->now.execution_epoch_s - st->cal.epoch_s; /* eligible => not negative */ + int32_t mid = (int32_t)(((int64_t)st->cal.env_temp_min_mc + st->cal.env_temp_max_mc) / 2); + int32_t dtemp = rq->now.temperature_mc - mid; + uint64_t worst = 0, worst_rel = 0, lat = 0, lat_total = 0; + uint32_t within_runs = 0, have_chosen = 0, differs = 0, exact_bad = 0, late = 0; + for (uint32_t r = 0; r < runs; r++) { + rc = forge_asim_realize(&st->actual, st->wrong_answer_row, st->wrong_answer_delta, rq->A, rq->x, + rq->M, rq->N, r, cal_age, dtemp, run_y, &lat); + if (rc != FORGE_ASIM_PUBLISHED_ANALOG) return out->status = rc; + lat_total = sat_add(lat_total, lat); + if (rq->latency_budget_ns && lat > rq->latency_budget_ns) late = 1; + if (r == 0) memcpy(first, run_y, sizeof first); + else if (memcmp(first, run_y, (size_t)rq->M * sizeof run_y[0]) != 0) differs = 1; + if (c->error_kind == FORGE_V2_ERROR_EXACT) { + uint8_t od[32]; + vec_digest("FORGE-ASIM/output/matvec/v1", run_y, rq->M, od); + if (memcmp(od, ref_id, 32) != 0) exact_bad = 1; + } + uint32_t ok = 1; + for (uint32_t i = 0; i < rq->M; i++) { + uint64_t err = run_y[i] > ref[i] ? run_y[i] - ref[i] : ref[i] - run_y[i]; + uint64_t allow = sat_add(c->error_abs, (uint64_t)c->error_rel_ppb * ref[i] / 1000000000ull); + if (c->error_kind == FORGE_V2_ERROR_EXACT) allow = 0; + if (err > allow) ok = 0; + if (err > worst) worst = err; + uint64_t rel = ref[i] ? mul_div_ceil(err, 1000000000ull, ref[i]) : (err ? UINT32_MAX : 0); + if (rel > worst_rel) worst_rel = rel; + } + if (ok) { + within_runs++; + if (!have_chosen) { memcpy(chosen, run_y, sizeof chosen); have_chosen = 1; } + } + } + out->analog_ran = 1; + out->analog_error_abs = worst; + out->analog_runs = runs; + out->analog_runs_within_bound = within_runs; + + if (late) out->refuse_reason = FORGE_ASIM_REFUSE_LATENCY_ACTUAL; + /* EXACT is never satisfied once any non-ideality is injected, even + * when the outputs happen to land on the oracle's values */ + else if (c->error_kind == FORGE_V2_ERROR_EXACT && + (exact_bad || !model_ideal(&st->actual) || st->wrong_answer_row != 0)) + out->refuse_reason = FORGE_ASIM_REFUSE_EXACT_MISMATCH; + else if (c->error_kind != FORGE_V2_ERROR_BOUNDED_STOCHASTIC && differs) + out->refuse_reason = FORGE_ASIM_REFUSE_NONDETERMINISM; + else if (c->error_kind != FORGE_V2_ERROR_BOUNDED_STOCHASTIC && within_runs != runs) + out->refuse_reason = FORGE_ASIM_REFUSE_ERROR_BOUND; + else if (c->error_kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC && + (uint64_t)within_runs * 1000000ull < (uint64_t)c->confidence_ppm * runs) + out->refuse_reason = FORGE_ASIM_REFUSE_CONFIDENCE; + + /* evidence of the simulated analog realization, refused or not */ + ForgeExecutionEvidenceV2 *e = &out->analog_evidence; + uint8_t code[32]; + label_digest(ANALOG_CODE_LABEL, code); + forge_v2_realization_identity(contract_digest, st->machine_identity, analog_digest, code, cal_digest, + e->realization_identity); + memcpy(e->machine_identity, st->machine_identity, 32); + memcpy(e->substrate_identity, analog.substrate_identity, 32); + memcpy(e->semantic_contract_digest, contract_digest, 32); + e->has_calibration = 1; + memcpy(e->calibration_digest, cal_digest, 32); + memcpy(e->input_identity, input_id, 32); + vec_digest("FORGE-ASIM/output/matvec/v1", have_chosen ? chosen : first, rq->M, e->output_identity); + e->execution_epoch_s = rq->now.execution_epoch_s; + e->execution_duration_ns = lat_total; + e->conversion_duration_ns = lat; + e->env_temperature_mc = rq->now.temperature_mc; + e->env_supply_uv = rq->now.supply_uv; + e->measured_error_abs = worst; /* V2 field name; the value is a simulated error vs the oracle */ + e->measured_error_rel_ppb = worst_rel > UINT32_MAX ? UINT32_MAX : (uint32_t)worst_rel; + e->confidence_ppm = (uint32_t)((uint64_t)within_runs * 1000000ull / runs); + e->repeat_runs = runs; + e->repeat_passes = within_runs; + e->repeat_mean_ns = runs ? lat_total / runs : 0; + e->fault_state = FORGE_V2_FAULT_NONE; + if (seal_evidence(e, out->analog_evidence_digest) != 0) return out->status = FORGE_ASIM_ERR_EVIDENCE; + + if (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { + out->final_evidence = *e; + memcpy(out->final_evidence_digest, out->analog_evidence_digest, 32); + memcpy(out->y, chosen, (size_t)rq->M * sizeof chosen[0]); + out->published = 1; + return out->status = FORGE_ASIM_PUBLISHED_ANALOG; + } + } + /* refused: nothing of the analog run is published; the digital + * realization answers under the same contract */ + if (!st->include_digital) return out->status = FORGE_ASIM_ERR_NO_FALLBACK; + return out->status = run_digital(st, rq, oracle, contract_digest, input_id, ref, out); +} diff --git a/forge/analog-sim/forge_analog_sim.h b/forge/analog-sim/forge_analog_sim.h new file mode 100644 index 0000000..0aac77b --- /dev/null +++ b/forge/analog-sim/forge_analog_sim.h @@ -0,0 +1,170 @@ +/* + * forge/analog-sim/forge_analog_sim.h -- AR2: simulated analog realization + * provider with digital oracle parity (ARCH-0018 section 12, gate AR2). + * + * A software model of an analog in-memory matvec substrate. Every result it + * produces is a SIMULATED_DEVELOPMENT result: no device acts, nothing here is + * a physical result, and there is no API to label it otherwise. + * + * Shape of the path (one call, forge_asim_execute): + * 1. describe the provider states its error model as a V2 substrate + * descriptor (declared model -> error_kind + error_abs); + * 2. eligible forge_v2_substrate_eligible against the contract, the + * calibration artifact and the execution conditions, plus + * the request's latency budget; + * 3. admit forge_v2_admit_plan: the eligible set must hold a digital + * realization of the same contract, otherwise nothing runs; + * 4. realize the simulation injects the ACTUAL non-idealities (which a + * test may make worse than the declared ones) for + * contract.min_samples runs; it never sees the oracle; + * 5. check outputs are compared to the reference oracle named by the + * contract; EXACT needs bit equality, bounded kinds need + * |err| <= error_abs + error_rel_ppb * |ref| / 1e9 per element; + * 6. publish only a passing result is copied out; a refused result + * still yields evidence, and the digital realization then + * produces the answer under the same contract. + * + * Reference oracles (restated, not linked; the gate script cross-checks them + * against the omega sources when an omega checkout is present): + * EXACT_U64 omega_matvec_reference omega src/omega_matvec.h:74 + * y = A * x, wrapping uint64 arithmetic. + * I32 omega_matmul_cpu_oracle_i32 omega src/omega_blackwell_matmul.h:50 + * n = 1 column, wrapping uint32 arithmetic. + * The f16 / bf16 oracles are not covered here (deferred). + * + * Integers only: bounds are in LSB units of the output, as spec section 6.4 + * requires. No heap, no hardware, no I/O. + */ +#ifndef FORGE_ANALOG_SIM_H +#define FORGE_ANALOG_SIM_H + +#include +#include + +#include "forge_substrate_v2.h" + +#define FORGE_ASIM_MAX_ROWS 64u +#define FORGE_ASIM_MAX_COLS 64u +#define FORGE_ASIM_MAX_SAMPLES 64u +#define FORGE_ASIM_INPUT_BITS 8u /* every A and x element must fit */ +#define FORGE_ASIM_OUTPUT_BITS 24u /* 8 + 8 + log2(64) */ + +/* Reference oracles, identified by the contract's reference_oracle_digest. */ +enum { + FORGE_ASIM_ORACLE_EXACT_U64 = 1, + FORGE_ASIM_ORACLE_I32 = 2 +}; + +/* Outcome of forge_asim_execute. Values >= 0 are decisions; < 0 are errors. */ +enum { + FORGE_ASIM_PUBLISHED_ANALOG = 0, /* simulated analog result passed the contract */ + FORGE_ASIM_PUBLISHED_FALLBACK = 1, /* analog refused; digital result published */ + FORGE_ASIM_ERR_ARG = -1, + FORGE_ASIM_ERR_INPUT_RANGE = -2, /* shape or element outside the declared limits */ + FORGE_ASIM_ERR_NO_FALLBACK = -3, /* plan not admitted: no digital realization */ + FORGE_ASIM_ERR_EVIDENCE = -4 /* evidence record failed V2 validation */ +}; + +/* Why the simulated analog realization was refused (0 = not refused). */ +enum { + FORGE_ASIM_REFUSE_NONE = 0, + FORGE_ASIM_REFUSE_INELIGIBLE = 1, /* forge_v2_substrate_eligible said no */ + FORGE_ASIM_REFUSE_LATENCY_DECLARED = 2,/* declared latency exceeds the request budget */ + FORGE_ASIM_REFUSE_EXACT_MISMATCH = 3, /* EXACT contract, output digest != oracle digest */ + FORGE_ASIM_REFUSE_ERROR_BOUND = 4, /* deterministic kind, some element out of bound */ + FORGE_ASIM_REFUSE_CONFIDENCE = 5, /* stochastic kind, too few runs within bound */ + FORGE_ASIM_REFUSE_NONDETERMINISM = 6, /* non-stochastic kind, runs disagree */ + FORGE_ASIM_REFUSE_LATENCY_ACTUAL = 7, /* simulated latency exceeds the request budget */ + FORGE_ASIM_REFUSE_UNSUPPORTED = 8 /* oracle or norm this provider cannot check */ +}; + +/* One error model. All magnitudes in output LSB units unless stated. + * Zero everywhere means an ideal (exact) simulated substrate. */ +typedef struct { + uint32_t quant_step_lsb; /* output quantization step; 0 or 1 = off */ + int64_t bias_lsb; /* constant offset */ + uint32_t noise_peak_lsb; /* uniform noise in [-peak, +peak]; 0 = off */ + uint64_t noise_seed; /* deterministic noise stream */ + int64_t drift_lsb_per_hour; /* offset growth with calibration age */ + int64_t temp_lsb_per_k; /* offset per kelvin away from the calibration temperature */ + uint64_t saturation_max; /* outputs clamp to [0, saturation_max]; 0 = off */ + uint64_t latency_ns; /* settling + conversion per run; 0 = none */ +} ForgeAnalogSimModel; + +/* The simulated provider and its world. */ +typedef struct { + ForgeAnalogSimModel declared; /* what the provider claims (descriptor, eligibility) */ + ForgeAnalogSimModel actual; /* what the simulation injects */ + uint32_t wrong_answer_row; /* fault injection: row + 1 gets wrong_answer_delta; 0 = off */ + int64_t wrong_answer_delta; + ForgeCalibrationArtifact cal; /* SIMULATED_MODEL calibration for this substrate */ + uint8_t machine_identity[32]; + uint32_t include_digital; /* 1: a digital CPU realization is in the eligible set */ +} ForgeAsimSetup; + +typedef struct { + ForgeSemanticResultContract contract; /* operation MATVEC, shape {M, N} */ + const uint64_t *A; /* M x N, row-major */ + const uint64_t *x; /* N */ + uint32_t M, N; + ForgeV2Conditions now; + uint64_t latency_budget_ns; /* 0 = no budget */ +} ForgeAsimRequest; + +typedef struct { + int status; /* FORGE_ASIM_PUBLISHED_* or FORGE_ASIM_ERR_* */ + uint32_t refuse_reason; /* FORGE_ASIM_REFUSE_* for the analog realization */ + int eligibility_rc; /* forge_v2_substrate_eligible result */ + uint32_t published; /* 1 when y holds an accepted result */ + uint64_t y[FORGE_ASIM_MAX_ROWS];/* zeroed unless published */ + uint32_t analog_ran; /* 1 when the simulation executed */ + uint64_t analog_error_abs; /* worst element error vs oracle over all runs */ + uint32_t analog_runs, analog_runs_within_bound; + uint8_t analog_substrate_digest[32]; + ForgeExecutionEvidenceV2 analog_evidence; /* valid when analog_ran */ + uint8_t analog_evidence_digest[32]; + ForgeExecutionEvidenceV2 final_evidence; /* evidence of the published result */ + uint8_t final_evidence_digest[32]; +} ForgeAsimOutcome; + +/* Oracles (restatements of the omega anchors above). */ +void forge_asim_oracle_exact_u64(const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, uint32_t N); +int forge_asim_oracle_i32(const uint32_t *a, const uint32_t *x, uint32_t *y, uint32_t M, uint32_t N); +/* Stable identity of an oracle; returns FORGE_V2_OK or FORGE_V2_ERR_ARG. */ +int forge_asim_oracle_digest(uint32_t oracle, uint8_t out[32]); + +/* Error bound (LSB) that a model guarantees at a calibration age and a + * temperature distance from the calibration point. Saturation is not part + * of the bound: clipping is a range limit and is caught by the check. */ +uint64_t forge_asim_model_bound(const ForgeAnalogSimModel *m, uint64_t cal_age_s, uint32_t temp_delta_mc); + +/* The V2 substrate descriptor this provider declares for a declared model + * and calibration: worst-case bound over the full calibration window and + * envelope. error_kind is EXACT only when the declared model is ideal. */ +int forge_asim_describe(const ForgeAnalogSimModel *declared, const ForgeCalibrationArtifact *cal, + ForgeSubstrateDescriptor *out); + +/* Identity of the simulated substrate instance (calibrations must name it). */ +void forge_asim_substrate_identity(uint8_t out[32]); + +/* The digital CPU realization used as the fallback. */ +void forge_asim_digital_descriptor(ForgeSubstrateDescriptor *out); + +/* One simulated run. Never consults an oracle. Returns the simulated + * latency in *latency_ns. */ +int forge_asim_realize(const ForgeAnalogSimModel *m, uint32_t wrong_row, int64_t wrong_delta, + const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, + uint32_t run_index, uint64_t cal_age_s, int32_t temp_delta_mc, + uint64_t *y, uint64_t *latency_ns); + +/* The whole path: describe, eligibility, admission, realize, check, + * publish or fall back. Every evidence record it writes carries + * provenance_class = SIMULATED_DEVELOPMENT. */ +int forge_asim_execute(const ForgeAsimSetup *setup, const ForgeAsimRequest *req, ForgeAsimOutcome *out); + +/* Provenance used by every evidence record of this provider. Fixed. */ +uint32_t forge_asim_provenance(void); + +const char *forge_asim_refuse_name(uint32_t reason); + +#endif diff --git a/tests/run_forge_analog_sim_gates.sh b/tests/run_forge_analog_sim_gates.sh new file mode 100755 index 0000000..b6fdbf0 --- /dev/null +++ b/tests/run_forge_analog_sim_gates.sh @@ -0,0 +1,166 @@ +#!/bin/bash +# tests/run_forge_analog_sim_gates.sh -- AR2 gates (ARCH-0018): simulated analog +# realization provider with digital oracle parity. Host-only: no hardware, no +# driver, no GPU lock. Every result is SIMULATED_DEVELOPMENT. +# +# tests/run_forge_analog_sim_gates.sh build, run every check, verify the +# committed receipt is reproduced byte for byte +# tests/run_forge_analog_sim_gates.sh --regen also (re)write evidence/AR2/.json +# +# Optional: OMEGA_DIR (default ../omega next to this repo). When it holds +# src/omega_matvec.c and src/omega_blackwell_matmul.c, the two restated +# oracles are compared against the omega originals; otherwise that gate SKIPs. +# +# Exit code 0 only when every gate passes. +set -uo pipefail + +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P) +REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd -P) +cd "$REPO_ROOT" || exit 1 + +BUILD_DIR="$REPO_ROOT/build" +BIN="$BUILD_DIR/test_forge_analog_sim" +RECEIPT_DIR="evidence/AR2" +SRC_C="forge/analog-sim/forge_analog_sim.c" +SRC_H="forge/analog-sim/forge_analog_sim.h" +TEST_C="tests/test_forge_analog_sim.c" +OMEGA_DIR="${OMEGA_DIR:-$REPO_ROOT/../omega}" +FAILS=0 +PASSES=0 +SKIPS=0 + +gate() { # name, status(0 = pass) + if [ "$2" -eq 0 ]; then echo "PASS $1"; PASSES=$((PASSES + 1)); + else echo "FAIL $1"; FAILS=$((FAILS + 1)); fi +} + +echo "=== AR2 simulated analog provider gates (host-only, SIMULATED_DEVELOPMENT) ===" +mkdir -p "$BUILD_DIR" + +gcc -O2 -Wall -Wextra -Werror -std=c11 -I./forge/v2 -I./forge/analog-sim \ + forge/v2/forge_substrate_v2.c "$SRC_C" sha256_clean.c "$TEST_C" -o "$BIN" +gate "build_gcc_O2_Wall_Wextra_Werror_std_c11" $? +if [ ! -x "$BIN" ]; then + echo "RESULT gates: $PASSES passed, $FAILS failed (build failed)" + exit 1 +fi + +"$BIN" | grep -E '^(FAIL|RESULT)' +"$BIN" > /dev/null +gate "ar2_checks_all_pass" $? + +# Same checks under the undefined-behaviour sanitizer (any UB aborts the run). +gcc -O1 -g -Wall -Wextra -Werror -std=c11 -fsanitize=undefined -fno-sanitize-recover=all \ + -I./forge/v2 -I./forge/analog-sim \ + forge/v2/forge_substrate_v2.c "$SRC_C" sha256_clean.c "$TEST_C" -o "$BIN-ubsan" \ + && "$BIN-ubsan" > /dev/null +gate "ar2_checks_pass_under_ubsan" $? + +# Deterministic receipt, named by the SHA-256 of its bytes. +"$BIN" --receipt > "$BUILD_DIR/ar2-receipt.json" +gate "receipt_emitted_by_code" $? +SUM=$(sha256sum "$BUILD_DIR/ar2-receipt.json" | cut -d' ' -f1) +if [ "${1:-}" = "--regen" ]; then + mkdir -p "$RECEIPT_DIR" + rm -f "$RECEIPT_DIR"/*.json + cp "$BUILD_DIR/ar2-receipt.json" "$RECEIPT_DIR/$SUM.json" + gate "receipt_regenerated" $? +fi +"$BIN" --receipt | cmp -s - "$BUILD_DIR/ar2-receipt.json" +gate "receipt_deterministic_across_runs" $? +COMMITTED=$(ls "$RECEIPT_DIR"/*.json 2>/dev/null | wc -l) +[ "$COMMITTED" -eq 1 ] && [ -f "$RECEIPT_DIR/$SUM.json" ] && cmp -s "$RECEIPT_DIR/$SUM.json" "$BUILD_DIR/ar2-receipt.json" +gate "committed_receipt_reproduced_digest_named" $? + +# Provenance: the receipt says SIMULATED_DEVELOPMENT and never claims a physical result. +grep -q '"provenance_class": "SIMULATED_DEVELOPMENT"' "$BUILD_DIR/ar2-receipt.json" \ + && grep -q '"status": "PASS"' "$BUILD_DIR/ar2-receipt.json" \ + && ! grep -qiE 'physical|measured' "$BUILD_DIR/ar2-receipt.json" +gate "receipt_simulated_development_no_physical_claim" $? +CASES=$(grep -c '"name": ' "$BUILD_DIR/ar2-receipt.json") +SIMCASES=$(grep -c '"provenance_class": "SIMULATED_DEVELOPMENT"}' "$BUILD_DIR/ar2-receipt.json") +[ "$CASES" -gt 0 ] && [ "$CASES" -eq "$SIMCASES" ] +gate "every_receipt_case_simulated_development" $? + +# The provider source never names the PHYSICAL provenance. +! grep -n 'PROVENANCE_PHYSICAL' "$SRC_C" "$SRC_H" > /dev/null +gate "provider_source_cannot_emit_physical" $? + +# Vendor-neutral: no vendor or product word in any identifier of the new files. +! grep -nE '\b[A-Za-z_]*(NVIDIA|Nvidia|nvidia|GB10|gb10|BLACKWELL|Blackwell|blackwell|CUDA|cuda|NVRM|nvrm)[A-Za-z_0-9]*\b' \ + "$SRC_C" "$SRC_H" "$TEST_C" \ + | grep -vE '^\S+:[0-9]+:\s*(/\*|\*)' | grep -vE 'omega_blackwell_matmul\.[ch]' > /dev/null +gate "no_vendor_names_in_identifiers" $? + +# No hardware, driver, heap or I/O in the provider. +! grep -nE '#include\s*[<"](nvrm|nv|cuda|sys/ioctl|sys/mman|fcntl|stdio)' "$SRC_C" "$SRC_H" > /dev/null \ + && ! grep -nE '\b(malloc|calloc|realloc|free|ioctl|mmap|open|printf|fopen)\s*\(' "$SRC_C" > /dev/null +gate "no_hardware_access_no_heap_no_io_in_provider" $? + +# R16 loop-inventory heuristics (same patterns as the V2 gates). +! grep -nE 'while\s*\(\s*(1|true)\s*\)|for\s*\(\s*;\s*;\s*\)' "$SRC_C" "$TEST_C" > /dev/null \ + && ! grep -niE '(^|[^a-z])(u?sleep|nanosleep|msleep|poll|recv|heartbeat|tick|dispatch|schedule|scheduler|orchestrate|orchestrator|turns?|pulse|yield|epoll|run_until_complete|max_steps|max_turns)([^a-z]|$)' \ + "$SRC_C" "$SRC_H" "$TEST_C" > /dev/null +gate "r16_loop_inventory_heuristics_clean" $? + +# Oracle parity with the omega originals (read-only; omega is never modified). +MV="$OMEGA_DIR/src/omega_matvec.c" +MM="$OMEGA_DIR/src/omega_blackwell_matmul.c" +if [ -f "$MV" ] && [ -f "$MM" ]; then + X="$BUILD_DIR/ar2-omega-oracle" + mkdir -p "$X" + { + echo '#include ' + echo '#include ' + awk '/^void omega_matvec_reference\(/,/^}/' "$MV" + awk '/^int omega_matmul_cpu_oracle_i32\(/,/^}/' "$MM" + } > "$X/omega_oracles.c" + cat > "$X/cross.c" <<'EOF' +#include +#include +#include "forge_analog_sim.h" +void omega_matvec_reference(const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, uint32_t N); +int omega_matmul_cpu_oracle_i32(const uint32_t *a, const uint32_t *b, uint32_t *c, uint32_t m, uint32_t k, uint32_t n); +int main(void) +{ + static uint64_t A[64 * 64], x[64], y1[64], y2[64]; + static uint32_t a32[64 * 64], x32[64], z1[64], z2[64]; + uint64_t s = 0x5eed; + int bad = 0; + for (int c = 0; c < 64; c++) { + uint32_t M = 1 + (uint32_t)(c % 64), N = 1 + (uint32_t)((c * 37) % 64); + for (uint32_t k = 0; k < M * N; k++) { + s = s * 6364136223846793005ull + 1442695040888963407ull; + A[k] = (c & 1) ? s : (s >> 56); + a32[k] = (uint32_t)(s >> 7); + } + for (uint32_t j = 0; j < N; j++) { + s = s * 6364136223846793005ull + 1442695040888963407ull; + x[j] = (c & 1) ? s : (s >> 56); + x32[j] = (uint32_t)(s >> 9); + } + omega_matvec_reference(A, x, y1, M, N); + forge_asim_oracle_exact_u64(A, x, y2, M, N); + omega_matmul_cpu_oracle_i32(a32, x32, z1, M, N, 1); + forge_asim_oracle_i32(a32, x32, z2, M, N); + if (memcmp(y1, y2, M * sizeof y1[0]) || memcmp(z1, z2, M * sizeof z1[0])) bad++; + } + printf("omega oracle cross-check: %d mismatching cases of 64\n", bad); + return bad != 0; +} +EOF + gcc -O2 -std=c11 -Wall -Werror -I./forge/v2 -I./forge/analog-sim \ + "$X/omega_oracles.c" "$X/cross.c" "$SRC_C" forge/v2/forge_substrate_v2.c sha256_clean.c -o "$X/cross" \ + && "$X/cross" + gate "restated_oracles_match_omega_originals" $? +else + echo "SKIP restated_oracles_match_omega_originals (no omega checkout at $OMEGA_DIR)" + SKIPS=$((SKIPS + 1)) +fi + +# The AR1 (V2 descriptor contract) gates are untouched and still pass. +bash tests/run_forge_v2_gates.sh > "$BUILD_DIR/ar1-gates.log" 2>&1 +gate "ar1_v2_gates_still_pass" $? + +echo "RESULT gates: $PASSES passed, $FAILS failed, $SKIPS skipped" +[ "$FAILS" -eq 0 ] diff --git a/tests/test_forge_analog_sim.c b/tests/test_forge_analog_sim.c new file mode 100644 index 0000000..591c4a0 --- /dev/null +++ b/tests/test_forge_analog_sim.c @@ -0,0 +1,684 @@ +/* + * tests/test_forge_analog_sim.c -- AR2 gate checks for the simulated analog + * provider (forge/analog-sim). Maps 1:1 onto the AR2 table of the analog + * realization test plan: exact oracle, tolerance oracle, injected + * non-idealities, contract strictness, fallback, provenance. + * + * test_forge_analog_sim run every check, print PASS/FAIL, exit 0 on all-pass + * test_forge_analog_sim --receipt same checks, then print the deterministic JSON receipt + * + * Every result in this file is SIMULATED_DEVELOPMENT. No device acts. + */ +#include +#include + +#include "forge_analog_sim.h" + +static int g_pass, g_fail; +static int g_quiet; + +static void check(const char *name, int ok) +{ + if (ok) g_pass++; else g_fail++; + if (!g_quiet || !ok) fprintf(g_quiet ? stderr : stdout, "%s %s\n", ok ? "PASS" : "FAIL", name); +} + +/* ---- receipt table ---- */ + +#define MAX_CASES 128 +typedef struct { + const char *name; + uint32_t contract_kind, oracle; + uint64_t contract_bound; + int status; + uint32_t refuse_reason; + int eligibility_rc; + uint32_t analog_ran, runs, runs_within; + uint64_t error_abs; + uint8_t analog_ev[32], final_ev[32]; + uint32_t final_is_digital; +} CaseRow; +static CaseRow g_cases[MAX_CASES]; +static int g_ncases; + +/* ---- fixtures ---- */ + +#define EPOCH0 1790000000ull /* fixed authority-clock time of the fixture calibration */ +#define WINDOW (24ull * 3600) /* calibration validity: one day */ + +static uint64_t g_A[FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS], g_x[FORGE_ASIM_MAX_COLS]; + +static uint64_t lcg(uint64_t *s) +{ + *s = *s * 6364136223846793005ull + 1442695040888963407ull; + return *s >> 33; +} + +static void fill_inputs(uint32_t M, uint32_t N, uint64_t seed) +{ + for (size_t k = 0; k < (size_t)M * N; k++) g_A[k] = lcg(&seed) & 0xFF; + for (uint32_t j = 0; j < N; j++) g_x[j] = lcg(&seed) & 0xFF; +} + +static void fixture_cal(ForgeCalibrationArtifact *a) +{ + memset(a, 0, sizeof *a); + forge_asim_substrate_identity(a->substrate_id); + forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/calibration/1", 29, a->calibration_id); + a->method = FORGE_V2_CAL_SIMULATED_MODEL; + a->epoch_s = EPOCH0; + a->validity_window_s = WINDOW; + a->env_temp_min_mc = 20000; + a->env_temp_max_mc = 30000; /* calibration point = 25 C */ + a->env_supply_min_uv = 900000; + a->env_supply_max_uv = 1100000; + a->noise_samples = 1024; + a->fixture_count = 8; + forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/fixtures", 24, a->fixtures_digest); + forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/evidence-root", 29, a->evidence_root); +} + +static void fixture_setup(ForgeAsimSetup *st) +{ + memset(st, 0, sizeof *st); + fixture_cal(&st->cal); + forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/machine", 23, st->machine_identity); + st->include_digital = 1; +} + +static void fixture_request(ForgeAsimRequest *rq, uint32_t kind, uint32_t oracle, uint64_t bound, uint32_t M, + uint32_t N) +{ + memset(rq, 0, sizeof *rq); + ForgeSemanticResultContract *c = &rq->contract; + c->operation_family = FORGE_V2_OP_MATVEC; + forge_asim_oracle_digest(oracle, c->reference_oracle_digest); + c->shape_rank = 2; c->shape_dim[0] = M; c->shape_dim[1] = N; + c->error_kind = kind; + c->error_abs = kind == FORGE_V2_ERROR_EXACT ? 0 : bound; + c->error_norm = FORGE_V2_NORM_ELEMENTWISE_MAX; + c->confidence_ppm = 1000000; + c->min_samples = 4; + c->allowed_nondeterminism = FORGE_V2_NONDET_NONE; + if (kind == FORGE_V2_ERROR_BOUNDED_DETERMINISTIC) c->allowed_nondeterminism = FORGE_V2_NONDET_ROUNDING; + if (kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC) { + c->allowed_nondeterminism = FORGE_V2_NONDET_ROUNDING | FORGE_V2_NONDET_STOCHASTIC_NOISE; + c->min_samples = 16; + } + rq->A = g_A; rq->x = g_x; rq->M = M; rq->N = N; + rq->now.execution_epoch_s = EPOCH0 + 3600; /* calibration one hour old */ + rq->now.temperature_mc = 25000; + rq->now.supply_uv = 1000000; +} + +static void oracle_u64(const ForgeAsimRequest *rq, uint64_t *ref) +{ + forge_asim_oracle_exact_u64(rq->A, rq->x, ref, rq->M, rq->N); +} + +static int y_equals(const ForgeAsimOutcome *o, const uint64_t *ref, uint32_t M) +{ + return memcmp(o->y, ref, (size_t)M * sizeof ref[0]) == 0; +} + +static int y_zero(const ForgeAsimOutcome *o) +{ + for (uint32_t i = 0; i < FORGE_ASIM_MAX_ROWS; i++) if (o->y[i]) return 0; + return 1; +} + +/* Provenance of every evidence record any case produced; strict decode. */ +static int g_evidence_records, g_evidence_simulated, g_evidence_bad; + +static void audit_evidence(const ForgeExecutionEvidenceV2 *e) +{ + uint8_t buf[4096]; + size_t n; + ForgeExecutionEvidenceV2 back; + g_evidence_records++; + if (forge_v2_encode_evidence(e, buf, sizeof buf, &n) != FORGE_V2_OK || + forge_v2_decode_evidence(buf, n, 1, &back) != FORGE_V2_OK) { + g_evidence_bad++; + return; + } + if (back.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT) g_evidence_simulated++; + else g_evidence_bad++; +} + +static ForgeAsimOutcome g_o; + +static const ForgeAsimOutcome *run(const char *name, const ForgeAsimSetup *st, const ForgeAsimRequest *rq) +{ + forge_asim_execute(st, rq, &g_o); + if (g_o.analog_ran) audit_evidence(&g_o.analog_evidence); + if (g_o.published) audit_evidence(&g_o.final_evidence); + if (g_ncases >= MAX_CASES) check("receipt_case_table_has_room", 0); + else { + CaseRow *r = &g_cases[g_ncases++]; + memset(r, 0, sizeof *r); + r->name = name; + r->contract_kind = rq->contract.error_kind; + r->oracle = 0; + for (uint32_t o = FORGE_ASIM_ORACLE_EXACT_U64; o <= FORGE_ASIM_ORACLE_I32; o++) { + uint8_t d[32]; + forge_asim_oracle_digest(o, d); + if (memcmp(d, rq->contract.reference_oracle_digest, 32) == 0) r->oracle = o; + } + r->contract_bound = rq->contract.error_abs; + r->status = g_o.status; + r->refuse_reason = g_o.refuse_reason; + r->eligibility_rc = g_o.eligibility_rc; + r->analog_ran = g_o.analog_ran; + r->runs = g_o.analog_runs; + r->runs_within = g_o.analog_runs_within_bound; + r->error_abs = g_o.analog_error_abs; + if (g_o.analog_ran) memcpy(r->analog_ev, g_o.analog_evidence_digest, 32); + if (g_o.published) memcpy(r->final_ev, g_o.final_evidence_digest, 32); + r->final_is_digital = g_o.status == FORGE_ASIM_PUBLISHED_FALLBACK; + } + return &g_o; +} + +/* refused analog, digital answer published under the same contract */ +static int refused_with_fallback(const ForgeAsimOutcome *o, const ForgeAsimRequest *rq, uint32_t reason) +{ + uint64_t ref[FORGE_ASIM_MAX_ROWS]; + uint8_t cd[32]; + ForgeSubstrateDescriptor d; + oracle_u64(rq, ref); + forge_v2_digest_contract(&rq->contract, cd); + forge_asim_digital_descriptor(&d); + return o->status == FORGE_ASIM_PUBLISHED_FALLBACK && o->refuse_reason == reason && o->published == 1 && + y_equals(o, ref, rq->M) && memcmp(o->final_evidence.semantic_contract_digest, cd, 32) == 0 && + memcmp(o->final_evidence.substrate_identity, d.substrate_identity, 32) == 0; +} + +static int published_analog(const ForgeAsimOutcome *o) +{ + return o->status == FORGE_ASIM_PUBLISHED_ANALOG && o->refuse_reason == FORGE_ASIM_REFUSE_NONE && + o->published == 1 && o->analog_ran == 1; +} + +/* ---- AR2 checks ---- */ + +static void exact_oracle(void) +{ + static const uint32_t shapes[][2] = { {1, 1}, {7, 13}, {64, 64}, {64, 1}, {1, 64} }; + int all_ok = 1; + for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) { + ForgeAsimSetup st; ForgeAsimRequest rq; + uint64_t ref[FORGE_ASIM_MAX_ROWS]; + fixture_setup(&st); + fill_inputs(shapes[s][0], shapes[s][1], 100 + s); + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, shapes[s][0], shapes[s][1]); + const ForgeAsimOutcome *o = run("exact_ideal_sim_vs_u64_oracle", &st, &rq); + oracle_u64(&rq, ref); + if (!(published_analog(o) && y_equals(o, ref, rq.M) && o->analog_error_abs == 0)) all_ok = 0; + } + check("exact_ideal_sim_bit_equal_to_u64_oracle_all_shapes", all_ok); + + ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d; + fixture_setup(&st); + forge_asim_describe(&st.declared, &st.cal, &d); + check("ideal_sim_declares_exact_error_kind", d.error_kind == FORGE_V2_ERROR_EXACT && d.error_abs == 0 && + d.substrate_class == FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY); + fill_inputs(16, 16, 7); + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 16, 16); + const ForgeAsimOutcome *o = run("exact_output_identity_equals_oracle_digest", &st, &rq); + ForgeAsimOutcome o1 = *o; + const ForgeAsimOutcome *fb; + ForgeAsimSetup st2 = st; st2.actual.bias_lsb = 1; /* force a fallback to obtain the oracle digest */ + st2.declared = st2.actual; + ForgeAsimRequest rq2 = rq; rq2.contract.error_kind = FORGE_V2_ERROR_EXACT; + fb = run("exact_oracle_digest_via_digital", &st2, &rq2); + check("exact_output_identity_equals_oracle_output_identity", + published_analog(&o1) && fb->status == FORGE_ASIM_PUBLISHED_FALLBACK && + memcmp(o1.analog_evidence.output_identity, fb->final_evidence.output_identity, 32) == 0); + + /* oracle restatements agree where they must, and wrap where omega wraps */ + uint32_t a32[4] = { 0xFFFFFFFFu, 2, 3, 4 }, x32[2] = { 2, 1 }, y32[2]; + forge_asim_oracle_i32(a32, x32, y32, 2, 2); + check("oracle_i32_wraps_u32_like_omega", y32[0] == 0u && y32[1] == 10u); + uint64_t A64[1] = { 1ull << 63 }, x64[1] = { 2 }, y64[1]; + forge_asim_oracle_exact_u64(A64, x64, y64, 1, 1); + check("oracle_u64_wraps_u64_like_omega", y64[0] == 0); + uint8_t d1[32], d2[32]; + check("oracle_digests_distinct_and_unknown_refused", + forge_asim_oracle_digest(FORGE_ASIM_ORACLE_EXACT_U64, d1) == 0 && + forge_asim_oracle_digest(FORGE_ASIM_ORACLE_I32, d2) == 0 && memcmp(d1, d2, 32) != 0 && + forge_asim_oracle_digest(99, d1) == FORGE_V2_ERR_ARG); +} + +static void tolerance_oracle(void) +{ + ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d; + /* deterministic non-idealities against the i32 oracle */ + fixture_setup(&st); + st.declared.quant_step_lsb = 8; + st.declared.bias_lsb = 3; + st.declared.drift_lsb_per_hour = 1; + st.declared.temp_lsb_per_k = 1; + st.actual = st.declared; + forge_asim_describe(&st.declared, &st.cal, &d); + fill_inputs(32, 32, 11); + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, d.error_abs, 32, 32); + rq.now.temperature_mc = 28000; + const ForgeAsimOutcome *o = run("tolerance_bounded_deterministic_i32", &st, &rq); + check("tolerance_bounded_deterministic_vs_i32_oracle_within_declared_bound", + d.error_kind == FORGE_V2_ERROR_BOUNDED_DETERMINISTIC && d.error_abs == 4 + 3 + 24 + 5 && + published_analog(o) && o->analog_error_abs > 0 && o->analog_error_abs <= d.error_abs); + + /* stochastic: noise, many runs, confidence counted */ + fixture_setup(&st); + st.declared.noise_peak_lsb = 6; + st.declared.noise_seed = 0xA2A2; + st.declared.bias_lsb = -2; + st.actual = st.declared; + forge_asim_describe(&st.declared, &st.cal, &d); + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_I32, d.error_abs, 32, 32); + o = run("tolerance_bounded_stochastic_i32", &st, &rq); + check("tolerance_bounded_stochastic_vs_i32_oracle_all_runs_within_bound", + d.error_kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC && published_analog(o) && o->analog_runs == 16 && + o->analog_runs_within_bound == 16 && o->analog_error_abs > 0 && o->analog_error_abs <= 8); + + /* the bound is error_abs + error_rel_ppb * |ref| / 1e9 per element: an + * error above error_abs alone passes only because of the relative part, + * and one LSB above the smallest per-element allowance is refused */ + fixture_setup(&st); + st.declared.bias_lsb = 40; + st.actual = st.declared; + fill_inputs(8, 64, 12); + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 40, 8, 64); + rq.contract.error_rel_ppb = 10000; /* 10 ppm of the reference */ + uint64_t ref[FORGE_ASIM_MAX_ROWS], allow_min = UINT64_MAX; + oracle_u64(&rq, ref); + for (uint32_t i = 0; i < rq.M; i++) { + uint64_t a = 40 + ref[i] * rq.contract.error_rel_ppb / 1000000000ull; + if (a < allow_min) allow_min = a; + } + st.actual.bias_lsb = (int64_t)allow_min; + o = run("tolerance_rel_part_admits_error_above_abs", &st, &rq); + check("tolerance_rel_part_admits_error_above_abs", + allow_min > 40 && published_analog(o) && o->analog_error_abs == allow_min); + st.actual.bias_lsb = (int64_t)allow_min + 1; + o = run("tolerance_one_lsb_over_bound_refused", &st, &rq); + check("tolerance_one_lsb_over_bound_refused", refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_ERROR_BOUND) && + o->analog_error_abs == allow_min + 1); +} + +/* One non-ideality: declared model -> contract bound = declared bound. + * Within: actual = declared, published. Out: actual worse, refused. */ +static void one_kind(const char *within_name, const char *out_name, const ForgeAnalogSimModel *declared, + const ForgeAnalogSimModel *worse, uint32_t kind, uint32_t expect_reason) +{ + ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d; + fixture_setup(&st); + st.declared = *declared; + st.actual = *declared; + forge_asim_describe(&st.declared, &st.cal, &d); + fill_inputs(24, 40, 21); + fixture_request(&rq, kind, FORGE_ASIM_ORACLE_I32, d.error_abs, 24, 40); + rq.now.temperature_mc = 27000; + rq.now.execution_epoch_s = EPOCH0 + 10 * 3600; + rq.latency_budget_ns = 1000; + const ForgeAsimOutcome *o = run(within_name, &st, &rq); + check(within_name, published_analog(o) && o->analog_error_abs <= d.error_abs); + st.actual = *worse; + o = run(out_name, &st, &rq); + check(out_name, refused_with_fallback(o, &rq, expect_reason) && o->analog_ran == 1); +} + +static void injected_nonidealities(void) +{ + ForgeAnalogSimModel dec, bad; + const uint32_t BD = FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, BS = FORGE_V2_ERROR_BOUNDED_STOCHASTIC; + + memset(&dec, 0, sizeof dec); dec.quant_step_lsb = 4; + bad = dec; bad.quant_step_lsb = 64; + one_kind("inject_quantization_within_bound_published", "inject_quantization_over_bound_refused", &dec, &bad, + BD, FORGE_ASIM_REFUSE_ERROR_BOUND); + + memset(&dec, 0, sizeof dec); dec.noise_peak_lsb = 3; dec.noise_seed = 77; + bad = dec; bad.noise_peak_lsb = 300; + one_kind("inject_noise_within_bound_published", "inject_noise_over_bound_refused", &dec, &bad, BS, + FORGE_ASIM_REFUSE_CONFIDENCE); + + memset(&dec, 0, sizeof dec); dec.bias_lsb = 5; + bad = dec; bad.bias_lsb = 9; + one_kind("inject_bias_within_bound_published", "inject_bias_over_bound_refused", &dec, &bad, BD, + FORGE_ASIM_REFUSE_ERROR_BOUND); + + memset(&dec, 0, sizeof dec); dec.drift_lsb_per_hour = 1; /* window bound 24 LSB; at 10 h, 10 LSB */ + bad = dec; bad.drift_lsb_per_hour = 10; /* at 10 h, 100 LSB */ + one_kind("inject_drift_within_bound_published", "inject_drift_over_bound_refused", &dec, &bad, BD, + FORGE_ASIM_REFUSE_ERROR_BOUND); + + memset(&dec, 0, sizeof dec); dec.saturation_max = (1ull << 24) - 1; /* never clips these inputs */ + bad = dec; bad.saturation_max = 1000; /* clips most rows */ + one_kind("inject_saturation_within_range_published", "inject_saturation_clipping_refused", &dec, &bad, BD, + FORGE_ASIM_REFUSE_ERROR_BOUND); + + memset(&dec, 0, sizeof dec); dec.temp_lsb_per_k = 2; /* envelope half width 5 K: bound 10 */ + bad = dec; bad.temp_lsb_per_k = 20; /* 2 K off: 40 LSB */ + one_kind("inject_temperature_within_bound_published", "inject_temperature_over_bound_refused", &dec, &bad, + BD, FORGE_ASIM_REFUSE_ERROR_BOUND); + + memset(&dec, 0, sizeof dec); dec.bias_lsb = 1; dec.latency_ns = 500; + bad = dec; bad.latency_ns = 5000; + one_kind("inject_latency_within_budget_published", "inject_latency_over_budget_refused", &dec, &bad, BD, + FORGE_ASIM_REFUSE_LATENCY_ACTUAL); + + memset(&dec, 0, sizeof dec); + dec.quant_step_lsb = 4; dec.noise_peak_lsb = 2; dec.noise_seed = 5; dec.bias_lsb = -3; + dec.drift_lsb_per_hour = 1; dec.temp_lsb_per_k = 1; dec.saturation_max = (1ull << 24) - 1; dec.latency_ns = 400; + bad = dec; bad.bias_lsb = -60; bad.noise_peak_lsb = 40; + one_kind("inject_combined_within_bound_published", "inject_combined_over_bound_refused", &dec, &bad, BS, + FORGE_ASIM_REFUSE_CONFIDENCE); + + /* declared-level refusals (before anything runs) */ + ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d; + const ForgeAsimOutcome *o; + fixture_setup(&st); + st.declared.bias_lsb = 2; st.declared.latency_ns = 400; st.actual = st.declared; + forge_asim_describe(&st.declared, &st.cal, &d); + fill_inputs(8, 8, 31); + + fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs, 8, 8); + rq.now.execution_epoch_s = EPOCH0 + WINDOW - 1; + o = run("inject_calibration_age_inside_window_published", &st, &rq); + check("inject_calibration_age_inside_window_published", published_analog(o)); + rq.now.execution_epoch_s = EPOCH0 + WINDOW; + o = run("inject_calibration_age_stale_refused", &st, &rq); + check("inject_calibration_age_stale_refused_ineligible", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && + o->eligibility_rc == FORGE_V2_ERR_CAL_STALE && o->analog_ran == 0); + rq.now.execution_epoch_s = EPOCH0 - 1; + o = run("inject_calibration_from_future_refused", &st, &rq); + check("inject_calibration_from_future_refused_ineligible", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && o->eligibility_rc == FORGE_V2_ERR_CAL_STALE); + + fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs, 8, 8); + rq.now.temperature_mc = 31000; + o = run("inject_temperature_outside_envelope_refused", &st, &rq); + check("inject_temperature_outside_envelope_refused_ineligible", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && + o->eligibility_rc == FORGE_V2_ERR_OUT_OF_ENVELOPE && o->analog_ran == 0); + + fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs, 8, 8); + rq.latency_budget_ns = 300; + o = run("inject_latency_declared_over_budget_refused", &st, &rq); + check("inject_latency_declared_over_budget_refused", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_LATENCY_DECLARED) && o->analog_ran == 0); + + fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs - 1, 8, 8); + o = run("declared_bound_over_contract_refused", &st, &rq); + check("declared_bound_over_contract_refused_ineligible", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && o->eligibility_rc == FORGE_V2_ERR_VALUE && + o->analog_ran == 0); +} + +static void strictness(void) +{ + static const char *const names[] = { + "exact_refuses_declared_quantization", "exact_refuses_declared_noise", "exact_refuses_declared_bias", + "exact_refuses_declared_drift", "exact_refuses_declared_temperature", "exact_refuses_declared_saturation" + }; + ForgeAsimSetup st; ForgeAsimRequest rq; + int all_ok = 1; + fill_inputs(12, 12, 41); + for (int k = 0; k < 6; k++) { + fixture_setup(&st); + switch (k) { + case 0: st.declared.quant_step_lsb = 2; break; + case 1: st.declared.noise_peak_lsb = 1; break; + case 2: st.declared.bias_lsb = 1; break; + case 3: st.declared.drift_lsb_per_hour = 1; break; + case 4: st.declared.temp_lsb_per_k = 1; break; + default: st.declared.saturation_max = (1ull << 24) - 1; break; + } + st.actual = st.declared; + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12); + const ForgeAsimOutcome *o = run(names[k], &st, &rq); + if (!(refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && + o->eligibility_rc == FORGE_V2_ERR_VALUE && o->analog_ran == 0)) + all_ok = 0; + } + check("exact_contract_refuses_every_declared_nonideality_at_eligibility", all_ok); + + /* a provider that declares itself ideal but is not: caught by the digest compare */ + fixture_setup(&st); + st.actual.bias_lsb = 1; + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12); + const ForgeAsimOutcome *o = run("exact_false_ideal_claim_caught", &st, &rq); + check("exact_false_ideal_claim_caught_by_output_digest", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && o->eligibility_rc == FORGE_V2_OK && + o->analog_ran == 1); + + /* injected quantization that happens to land on the oracle's values + * (all outputs even, step 2) still never satisfies EXACT */ + fixture_setup(&st); + st.actual.quant_step_lsb = 2; + for (size_t k = 0; k < 12 * 12; k++) g_A[k] &= ~1ull; + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12); + o = run("exact_refuses_injected_nonideality_even_on_matching_output", &st, &rq); + check("exact_refuses_injected_nonideality_even_on_matching_output", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && o->analog_ran == 1 && + o->analog_error_abs == 0); + fill_inputs(12, 12, 41); + + /* a stochastic substrate is never offered for a deterministic contract */ + fixture_setup(&st); + st.declared.noise_peak_lsb = 1; st.actual = st.declared; + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, 1000, 12, 12); + o = run("deterministic_contract_refuses_stochastic_substrate", &st, &rq); + check("deterministic_contract_refuses_stochastic_substrate", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && o->eligibility_rc == FORGE_V2_ERR_VALUE); + + /* declared deterministic, actually noisy: runs disagree, refused */ + fixture_setup(&st); + st.declared.bias_lsb = 1; st.actual = st.declared; + st.actual.noise_peak_lsb = 1; st.actual.noise_seed = 3; + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, 1, 12, 12); + rq.contract.error_abs = 1000; + o = run("deterministic_contract_catches_hidden_noise", &st, &rq); + check("deterministic_contract_catches_hidden_noise", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_NONDETERMINISM) && o->analog_ran == 1); + + /* deliberately wrong simulated answers are caught under every contract kind */ + fixture_setup(&st); + st.wrong_answer_row = 5; st.wrong_answer_delta = 1; + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12); + o = run("wrong_answer_exact_caught", &st, &rq); + check("wrong_answer_off_by_one_caught_under_exact", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && o->analog_error_abs == 1); + + fixture_setup(&st); + st.declared.bias_lsb = 2; st.actual = st.declared; + st.wrong_answer_row = 12; st.wrong_answer_delta = -500; + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, 2, 12, 12); + o = run("wrong_answer_bounded_deterministic_caught", &st, &rq); + check("wrong_answer_caught_under_bounded_deterministic", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_ERROR_BOUND) && o->analog_error_abs >= 498); + + fixture_setup(&st); + st.declared.noise_peak_lsb = 2; st.declared.noise_seed = 9; st.actual = st.declared; + st.wrong_answer_row = 1; st.wrong_answer_delta = 50; + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_I32, 2, 12, 12); + rq.contract.confidence_ppm = 500000; + o = run("wrong_answer_bounded_stochastic_caught", &st, &rq); + check("wrong_answer_caught_under_bounded_stochastic", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_CONFIDENCE) && o->analog_runs_within_bound == 0); +} + +static void fallback(void) +{ + ForgeAsimSetup st; ForgeAsimRequest rq; + const ForgeAsimOutcome *o; + fill_inputs(10, 10, 51); + + fixture_setup(&st); + st.include_digital = 0; + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 10, 10); + o = run("no_digital_fallback_plan_not_admitted", &st, &rq); + check("no_digital_fallback_plan_not_admitted_even_when_analog_fits", + o->status == FORGE_ASIM_ERR_NO_FALLBACK && o->published == 0 && o->analog_ran == 0 && y_zero(o)); + + st.actual.bias_lsb = 7; + o = run("no_digital_fallback_refused_nothing_published", &st, &rq); + check("no_digital_fallback_refused_result_publishes_nothing", + o->status == FORGE_ASIM_ERR_NO_FALLBACK && o->published == 0 && y_zero(o)); + + fixture_setup(&st); + st.actual.bias_lsb = 7; + o = run("fallback_digital_answers_same_contract", &st, &rq); + check("fallback_digital_answers_same_contract_with_evidence", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && + o->final_evidence.has_calibration == 0 && o->final_evidence.measured_error_abs == 0 && + memcmp(o->final_evidence_digest, o->analog_evidence_digest, 32) != 0); + + /* the refused analog run still left evidence of what it did */ + check("refused_analog_run_still_has_evidence", + o->analog_ran == 1 && o->analog_evidence.measured_error_abs == 7 && o->analog_evidence.repeat_passes == 0 && + o->analog_evidence.has_calibration == 1); + + /* the digital fallback meets the same contract's sample count */ + fixture_setup(&st); + st.declared.noise_peak_lsb = 2; st.declared.noise_seed = 4; st.actual = st.declared; + st.actual.noise_peak_lsb = 500; + fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_I32, 2, 10, 10); + o = run("fallback_meets_stochastic_sample_count", &st, &rq); + check("fallback_meets_contract_sample_count", + refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_CONFIDENCE) && + o->final_evidence.repeat_runs == rq.contract.min_samples && + o->final_evidence.repeat_passes == rq.contract.min_samples); + + /* extreme but accepted model magnitudes stay defined (the gate also runs this under UBSan) */ + { + ForgeAnalogSimModel m; + uint64_t yy[FORGE_ASIM_MAX_ROWS], lat; + memset(&m, 0, sizeof m); + m.bias_lsb = (int64_t)1 << 32; m.drift_lsb_per_hour = (int64_t)1 << 32; m.temp_lsb_per_k = (int64_t)1 << 32; + m.noise_peak_lsb = UINT32_MAX; m.quant_step_lsb = UINT32_MAX; + int r1 = forge_asim_realize(&m, 1, (int64_t)1 << 32, g_A, g_x, 10, 10, 0, UINT64_MAX, INT32_MAX, yy, &lat); + m.bias_lsb = -m.bias_lsb; m.drift_lsb_per_hour = -m.drift_lsb_per_hour; + int r2 = forge_asim_realize(&m, 0, 0, g_A, g_x, 10, 10, 1, UINT64_MAX, INT32_MIN, yy, &lat); + m.bias_lsb = ((int64_t)1 << 32) + 1; + int r3 = forge_asim_realize(&m, 0, 0, g_A, g_x, 10, 10, 0, 0, 0, yy, &lat); + uint64_t b = forge_asim_model_bound(&m, UINT64_MAX, UINT32_MAX); + check("extreme_model_magnitudes_defined_and_over_limit_refused", + r1 == FORGE_ASIM_PUBLISHED_ANALOG && r2 == FORGE_ASIM_PUBLISHED_ANALOG && r3 == FORGE_ASIM_ERR_ARG && + b == UINT64_MAX); + } + + /* input out of the declared limits is refused outright */ + g_A[0] = 256; + fixture_setup(&st); + o = run("input_out_of_declared_range_refused", &st, &rq); + check("input_out_of_declared_range_refused", o->status == FORGE_ASIM_ERR_INPUT_RANGE && o->published == 0); + g_A[0] = 1; +} + +static void provenance_and_identity(void) +{ + ForgeAsimSetup st; ForgeAsimRequest rq; + ForgeAsimOutcome a, b; + check("provider_provenance_is_simulated_development", + forge_asim_provenance() == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT && + forge_asim_provenance() != FORGE_V2_PROVENANCE_PHYSICAL); + check("every_evidence_record_simulated_development_strict_roundtrip", + g_evidence_records > 0 && g_evidence_bad == 0 && g_evidence_simulated == g_evidence_records); + + /* a gate that requires PHYSICAL refuses every record of this provider */ + fixture_setup(&st); + fill_inputs(9, 9, 61); + fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 9, 9); + forge_asim_execute(&st, &rq, &a); + check("physical_requiring_gate_refuses_simulated_result", + a.published && a.final_evidence.provenance_class != FORGE_V2_PROVENANCE_PHYSICAL); + + /* determinism: the same inputs give the same evidence digests */ + forge_asim_execute(&st, &rq, &b); + check("same_inputs_same_evidence_digest", memcmp(a.final_evidence_digest, b.final_evidence_digest, 32) == 0); + + /* the calibration is bound into realization identity */ + st.cal.calibration_id[0] ^= 1; + forge_asim_execute(&st, &rq, &b); + check("calibration_change_moves_realization_identity", + b.published && memcmp(a.final_evidence.realization_identity, b.final_evidence.realization_identity, 32) != 0); +} + +/* ---- receipt ---- */ + +static void hex(const uint8_t *d) +{ + for (int i = 0; i < 32; i++) printf("%02x", d[i]); +} + +static const char *kind_name(uint32_t k) +{ + switch (k) { + case FORGE_V2_ERROR_EXACT: return "EXACT"; + case FORGE_V2_ERROR_BOUNDED_DETERMINISTIC: return "BOUNDED_DETERMINISTIC"; + case FORGE_V2_ERROR_BOUNDED_STOCHASTIC: return "BOUNDED_STOCHASTIC"; + default: return "OTHER"; + } +} + +static const char *status_name(int s) +{ + switch (s) { + case FORGE_ASIM_PUBLISHED_ANALOG: return "published_simulated_analog"; + case FORGE_ASIM_PUBLISHED_FALLBACK: return "published_digital_fallback"; + case FORGE_ASIM_ERR_NO_FALLBACK: return "not_admitted_no_digital_fallback"; + case FORGE_ASIM_ERR_INPUT_RANGE: return "input_out_of_range"; + default: return "error"; + } +} + +static void print_receipt(void) +{ + printf("{\n"); + printf(" \"gate\": \"AR2_ANALOG_SIMULATION_PROVIDER\",\n"); + printf(" \"workstream\": \"ARCH-0018 AR2\",\n"); + printf(" \"provenance_class\": \"SIMULATED_DEVELOPMENT\",\n"); + printf(" \"note\": \"Software model of an analog in-memory matvec substrate. No device acted. " + "Errors are simulated errors against the digital oracle.\",\n"); + printf(" \"status\": \"%s\",\n", g_fail == 0 ? "PASS" : "FAIL"); + printf(" \"checks_passed\": %d,\n", g_pass); + printf(" \"checks_failed\": %d,\n", g_fail); + printf(" \"evidence_records\": %d,\n", g_evidence_records); + printf(" \"evidence_records_simulated_development\": %d,\n", g_evidence_simulated); + printf(" \"cases\": [\n"); + for (int i = 0; i < g_ncases; i++) { + const CaseRow *r = &g_cases[i]; + printf(" {\"name\": \"%s\", \"contract_kind\": \"%s\", \"oracle\": \"%s\", \"contract_error_abs\": %llu, " + "\"outcome\": \"%s\", \"analog_refusal\": \"%s\", \"eligibility\": \"%s\", \"analog_ran\": %u, " + "\"runs\": %u, \"runs_within_bound\": %u, \"simulated_error_vs_oracle_abs\": %llu, ", + r->name, kind_name(r->contract_kind), + r->oracle == FORGE_ASIM_ORACLE_I32 ? "omega_matmul_cpu_oracle_i32" : "omega_matvec_reference", + (unsigned long long)r->contract_bound, status_name(r->status), forge_asim_refuse_name(r->refuse_reason), + forge_v2_strerror(r->eligibility_rc), r->analog_ran, r->runs, r->runs_within, + (unsigned long long)r->error_abs); + printf("\"analog_evidence_digest\": \""); + if (r->analog_ran) hex(r->analog_ev); + printf("\", \"published_evidence_digest\": \""); + if (r->status == FORGE_ASIM_PUBLISHED_ANALOG || r->status == FORGE_ASIM_PUBLISHED_FALLBACK) hex(r->final_ev); + printf("\", \"provenance_class\": \"SIMULATED_DEVELOPMENT\"}%s\n", i + 1 < g_ncases ? "," : ""); + } + printf(" ]\n}\n"); +} + +int main(int argc, char **argv) +{ + int receipt = argc > 1 && strcmp(argv[1], "--receipt") == 0; + g_quiet = receipt; + exact_oracle(); + tolerance_oracle(); + injected_nonidealities(); + strictness(); + fallback(); + provenance_and_identity(); + if (receipt) print_receipt(); + else printf("RESULT checks: %d passed, %d failed\n", g_pass, g_fail); + return g_fail == 0 ? 0 : 1; +}