Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/physics-mv/patches/5ee3e4e7842a0f3a # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/physics-mv#46 (code only) closed derived `5ee3e4e7842a0f3a` #46 Imported from GitHub aien-dev/physics-mv#46 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `d74d79965ccccfe8beb891e3d8da7bd93dbdf8a1` · Head `939f0a018a48f16366325d43936d92690e597b3d` ### Apply it ``` git checkout -b patch-5ee3e4e7842a0f3a d74d79965ccccfe8beb891e3d8da7bd93dbdf8a1 curl -fsSL https://interplane.io/drakestapleton/physics-mv/patches/5ee3e4e7842a0f3a.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/physics-mv 46 939f0a018a48f16366325d43936d92690e597b3d ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"physics-mv","number":46,"url":"https://github.com/aien-dev/physics-mv/pull/46","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:26Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index 1178348..55cff56 100644 --- a/.crumb +++ b/.crumb @@ -49,8 +49,8 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "e07cb55f9a5843cb4db673e521870da3e2e195153806de48704de7f776e90adc", - "last_commit": "d52759d", + "digest": "bf1be2f8b458c9343fdebbef9f8906cfe850fdd53e4a8b481737889dbacc0584", + "last_commit": "7aba0b9", "name": "forge", "purpose": "Directory forge. Purpose not yet described by a human." }, @@ -67,8 +67,8 @@ "purpose": "Directory m3. Purpose not yet described by a human." }, { - "digest": "c19ebd3b17c1ead4c0ed77e8945c8d765aa9de45074bc4e32d5e9d3a2b17f241", - "last_commit": "6d7cf0d", + "digest": "75efed90799746e089a7c60bf007dbbf0102d53299d4b1b21fe32f70c065aa93", + "last_commit": "7aba0b9", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -85,15 +85,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:9af971530fe6aab5a22b5d363fae19de11b117a314568cd3122211f72c49168b", - "compiled_at": "2026-10-04T14:06:48Z", + "children_root": "sha256:0670a6ccf8f2b6ce4293750fa1bf3129761bf741c60e86660105708a4ab621de", + "compiled_at": "2026-10-04T16:07:57Z", "compiler": "crumb-compile/1", - "digest": "sha256:4bf8817de8aa3c04566820e27ff09eb1481d19601859e5fc841683b1320c554a", - "evidence_files": 80, - "evidence_root": "sha256:9796c61d2acb13463f0c3e268a6b1566d3e55419168ef270cd4ab889dbce4ad7", - "files": 252, - "generated_at_commit": "4fa9e9c4a2765647f440cb67c3b1a9352a3b97ee", - "source_tree": "sha256:b409ca95dcebbc75268aa9ac9a7a792860dd4791212c84fbd47259286a729c5c" + "digest": "sha256:0110a225c5bbc08a19f383c390867f49dc2c83516edc08ef35d79c998e5c80f3", + "evidence_files": 81, + "evidence_root": "sha256:0a0459a4517679721bb2817540dd06995500a1c1ede079bc4665d9da5c39183d", + "files": 257, + "generated_at_commit": "7aba0b9597b9163a3890a266abb87c293cb396db", + "source_tree": "sha256:40493e2b4d3f4ab0376ab83dc26d59cfe223570c2ce4345d992d534fdf12545f" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/host-gates.yml b/.github/workflows/host-gates.yml index d98be2c..e224e97 100644 --- a/.github/workflows/host-gates.yml +++ b/.github/workflows/host-gates.yml @@ -11,6 +11,7 @@ name: Host gates # Coverage map (Lane 34, 2026-10-01): # forge-v2 tests/run_forge_v2_gates.sh FORGE V2 KAT + golden doc # analog-sim tests/run_forge_analog_sim_gates.sh AR2 simulated analog provider, +# form0 tests/run_forge_form_gates.sh FORM0 realization-form IR (ADR 0034), # receipt reproduced byte for byte; oracle gate compares against omega main # gate34-writer tests/test_forge_gates_receipt.sh Gate 3/4 receipt writer (no GPU), # digests cross-checked with omega tools/json_canon.c @@ -44,6 +45,8 @@ jobs: cmd: tests/run_forge_v2_gates.sh - gate: analog-sim cmd: tests/run_forge_analog_sim_gates.sh + - gate: form0 + cmd: tests/run_forge_form_gates.sh - gate: gate34-writer cmd: tests/test_forge_gates_receipt.sh "$GITHUB_WORKSPACE/omega" - gate: m2 diff --git a/evidence/FORM0/ca1f084750a00914e7d48a7c0d44a63400e11e04262b7fd9c2a1771bfa9ef7c4.json b/evidence/FORM0/ca1f084750a00914e7d48a7c0d44a63400e11e04262b7fd9c2a1771bfa9ef7c4.json new file mode 100644 index 0000000..ebfd721 --- /dev/null +++ b/evidence/FORM0/ca1f084750a00914e7d48a7c0d44a63400e11e04262b7fd9c2a1771bfa9ef7c4.json @@ -0,0 +1,28 @@ +{ + "gate": "FORM0_LINEAR_OPERATOR_HOST", + "workstream": "ARCH-0018 / ARCH-0019 extension; ADR 0034 Physical-Form Realization IR (PROPOSED)", + "provenance_class": "SIMULATED_DEVELOPMENT", + "note": "Host-only reference. No device acted. The digital reference ran on the host CPU as a development fixture; the analog lowering is the AR2 software model. Errors are simulated or host errors against the digital oracle.", + "status": "PASS", + "checks_passed": 163, + "checks_failed": 0, + "evidence_records": 12, + "evidence_records_simulated_development": 12, + "proof_two_realizations_same_contract": "PASS", + "proof": {"contract_digest": "2d4a744ccbcca37750e8fe8fb82b719386ef6028ccfa82e16803e1e81d58e441", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "digital_reference": {"realization_identity": "c000f918e3d6aca6fe7f84e7bbc2279955e2ad0e10d06bc1ee0b3ff530e1c8a8", "evidence_digest": "2e080ff06311d7921ed9ec647c69b544042e67413b41064c8c480f865b9b6309"}, "analog_sim": {"realization_identity": "7d2850b33169804749213865088dbf29d149bef3858bd72f94e0b27cf0cb0851", "evidence_digest": "1f9e524f4aa254ce82ef4ab62ef2d6f96ae5ed34331a1390723f580281dd3328"}}, + "cases": [ + {"name": "exact_digital_reference_published", "backend": "digital_reference", "contract_kind": "EXACT", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "2d4a744ccbcca37750e8fe8fb82b719386ef6028ccfa82e16803e1e81d58e441", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "d645bf5078049604ef379b2bfb4850ea02f68c08434200c0fec475e7d19e7ab0", "realization_identity": "c000f918e3d6aca6fe7f84e7bbc2279955e2ad0e10d06bc1ee0b3ff530e1c8a8", "evidence_digest": "2e080ff06311d7921ed9ec647c69b544042e67413b41064c8c480f865b9b6309", "record_digest": "e2a8bcf67d1850989e1ae267fbec6a663c164a19a1f8442f571ced22880df913", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_analog_sim_ideal_published", "backend": "analog_sim", "contract_kind": "EXACT", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "2d4a744ccbcca37750e8fe8fb82b719386ef6028ccfa82e16803e1e81d58e441", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "0295b32762979dc71c8db9110b4da30cd72a78706cac7f7fdaea1b2de4b53dee", "realization_identity": "7d2850b33169804749213865088dbf29d149bef3858bd72f94e0b27cf0cb0851", "evidence_digest": "1f9e524f4aa254ce82ef4ab62ef2d6f96ae5ed34331a1390723f580281dd3328", "record_digest": "fe2fb646fb37e3e2b26926e00d52a92839a6bd71f9b174f66c47f53582bb72e3", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "bounded_digital_reference_published", "backend": "digital_reference", "contract_kind": "BOUNDED_DETERMINISTIC", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "005d09beb5ec1c82add74a15437efc5acbf206730447478fbe73b33af53f8f6e", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "d645bf5078049604ef379b2bfb4850ea02f68c08434200c0fec475e7d19e7ab0", "realization_identity": "8845d3267404a3cf42ed9c3664d6a94b0ab7cd94bf53721d64137214f2c9c2d0", "evidence_digest": "3c563974b789230f6a59ed0bc4b687aeb12354b4541275c5a98db83f2988564b", "record_digest": "ee9ca84b562bbe1d8b0ba722a9655b90f8ae9ecf08558c0d53cf3ff57c3dae08", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "bounded_analog_sim_bias2_published", "backend": "analog_sim", "contract_kind": "BOUNDED_DETERMINISTIC", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 2, "contract_digest": "005d09beb5ec1c82add74a15437efc5acbf206730447478fbe73b33af53f8f6e", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "0295b32762979dc71c8db9110b4da30cd72a78706cac7f7fdaea1b2de4b53dee", "realization_identity": "018d30ae5f5e1badd9a7d2f508155c830dff9b77466ac073a8e3c8afb37986ee", "evidence_digest": "93ff0da63fa7087782b9e97800db1fdfdff44fff5d8d4359979484faf80d5467", "record_digest": "63f6998f1fb64f94a845561a7b486aaa33ad54061e9f69670a5cf93fa53641e5", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "bounded_analog_sim_bias5_refused", "backend": "analog_sim", "contract_kind": "BOUNDED_DETERMINISTIC", "outcome": "refused", "refusal": "error_over_bound", "ran": 1, "runs": 4, "runs_within_bound": 0, "error_vs_oracle_abs": 5, "contract_digest": "005d09beb5ec1c82add74a15437efc5acbf206730447478fbe73b33af53f8f6e", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "0295b32762979dc71c8db9110b4da30cd72a78706cac7f7fdaea1b2de4b53dee", "realization_identity": "018d30ae5f5e1badd9a7d2f508155c830dff9b77466ac073a8e3c8afb37986ee", "evidence_digest": "8c837375874d288a8c66d6d41ffea4f14b7ee066a8db0df965c604f1a6434670", "record_digest": "e61b8b10960b060e309531cffa7273ac254554fc74c84c7bf6a37e3401fa4a97", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "bounded_digital_fallback_published", "backend": "digital_reference", "contract_kind": "BOUNDED_DETERMINISTIC", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "005d09beb5ec1c82add74a15437efc5acbf206730447478fbe73b33af53f8f6e", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "d645bf5078049604ef379b2bfb4850ea02f68c08434200c0fec475e7d19e7ab0", "realization_identity": "8845d3267404a3cf42ed9c3664d6a94b0ab7cd94bf53721d64137214f2c9c2d0", "evidence_digest": "3c563974b789230f6a59ed0bc4b687aeb12354b4541275c5a98db83f2988564b", "record_digest": "ee9ca84b562bbe1d8b0ba722a9655b90f8ae9ecf08558c0d53cf3ff57c3dae08", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "exact_analog_sim_hidden_noise_refused", "backend": "analog_sim", "contract_kind": "EXACT", "outcome": "refused", "refusal": "exact_digest_mismatch", "ran": 1, "runs": 4, "runs_within_bound": 0, "error_vs_oracle_abs": 2, "contract_digest": "2d4a744ccbcca37750e8fe8fb82b719386ef6028ccfa82e16803e1e81d58e441", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "0295b32762979dc71c8db9110b4da30cd72a78706cac7f7fdaea1b2de4b53dee", "realization_identity": "7d2850b33169804749213865088dbf29d149bef3858bd72f94e0b27cf0cb0851", "evidence_digest": "fe1ac8b1317136895057a06e9459fe4296f8383a2d355b4a0d203b8007a9dbe5", "record_digest": "f9c32ddbee3648003a363f39476861ce98fa9a22eda2a0cfc43238338f7af67d", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "stochastic_analog_sim_noise2_published", "backend": "analog_sim", "contract_kind": "BOUNDED_STOCHASTIC", "outcome": "published", "refusal": "none", "ran": 1, "runs": 16, "runs_within_bound": 16, "error_vs_oracle_abs": 2, "contract_digest": "473c90cdb631caea37c1c2603525f0d1337a0382b00ec24ed21e203329075f43", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "0295b32762979dc71c8db9110b4da30cd72a78706cac7f7fdaea1b2de4b53dee", "realization_identity": "681167724e9026451877808ece6a7f7ae35f4f621c02b17865a4f0fb98c12bce", "evidence_digest": "58306eb636c1fa6a2239713bc6930b724af4d6f868793f1d6142c271979b4974", "record_digest": "4a02da4c573bf01756d0d4907529ea7417ef50c47dbea1a1e2c37b17e3e82c39", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "stochastic_digital_reference_published", "backend": "digital_reference", "contract_kind": "BOUNDED_STOCHASTIC", "outcome": "published", "refusal": "none", "ran": 1, "runs": 16, "runs_within_bound": 16, "error_vs_oracle_abs": 0, "contract_digest": "473c90cdb631caea37c1c2603525f0d1337a0382b00ec24ed21e203329075f43", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "d645bf5078049604ef379b2bfb4850ea02f68c08434200c0fec475e7d19e7ab0", "realization_identity": "b2291d9c5ff7ebe248c31f2cbb711f64e11bfea842b85cf1c7e9b5fba2f94f68", "evidence_digest": "03181a48dad394cc73a35075de3de3cc6ad53aaf6e0ba204c794cfb746bbf797", "record_digest": "a1f3f7e38d3e9d63f941199795559391663b896832b838f37aff5e578293cac9", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "stale_calibration_ineligible", "backend": "analog_sim", "contract_kind": "BOUNDED_DETERMINISTIC", "outcome": "refused", "refusal": "ineligible", "ran": 0, "runs": 0, "runs_within_bound": 0, "error_vs_oracle_abs": 0, "contract_digest": "005d09beb5ec1c82add74a15437efc5acbf206730447478fbe73b33af53f8f6e", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "0295b32762979dc71c8db9110b4da30cd72a78706cac7f7fdaea1b2de4b53dee", "realization_identity": "", "evidence_digest": "", "record_digest": "", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "digital_reference_unaffected_by_analog_calibration", "backend": "digital_reference", "contract_kind": "BOUNDED_DETERMINISTIC", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "005d09beb5ec1c82add74a15437efc5acbf206730447478fbe73b33af53f8f6e", "form_digest": "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8", "lowering_digest": "d645bf5078049604ef379b2bfb4850ea02f68c08434200c0fec475e7d19e7ab0", "realization_identity": "8845d3267404a3cf42ed9c3664d6a94b0ab7cd94bf53721d64137214f2c9c2d0", "evidence_digest": "3c563974b789230f6a59ed0bc4b687aeb12354b4541275c5a98db83f2988564b", "record_digest": "ee9ca84b562bbe1d8b0ba722a9655b90f8ae9ecf08558c0d53cf3ff57c3dae08", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "i32_digital_reference_published", "backend": "digital_reference", "contract_kind": "EXACT", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "36c1ab138b64d031f43dd76f410b84497c70903ee38f1f7385a3b0a76ebb3f11", "form_digest": "a8c860e752b20db3e14b124a70e6beca831da913bd7cc5f351a9d16f82f74177", "lowering_digest": "81118c4b0d0a587b68ba201f6b7fcf3a1da002d9ab01d1b5eaee29ee02cc49d7", "realization_identity": "c6240a7e8507ec2028ce6ea1fa75b687503a1f6d6105476a1466fb58983e74b0", "evidence_digest": "62aaad06a30f982013c98d6ddac5e5525f77504dcd4bd56c9566f2bbe537ea3f", "record_digest": "55e08f05669e69f160dcc69837eda7e05a3948ba475dcde1b869202fb6a4f211", "provenance_class": "SIMULATED_DEVELOPMENT"}, + {"name": "i32_analog_sim_ideal_published", "backend": "analog_sim", "contract_kind": "EXACT", "outcome": "published", "refusal": "none", "ran": 1, "runs": 4, "runs_within_bound": 4, "error_vs_oracle_abs": 0, "contract_digest": "36c1ab138b64d031f43dd76f410b84497c70903ee38f1f7385a3b0a76ebb3f11", "form_digest": "a8c860e752b20db3e14b124a70e6beca831da913bd7cc5f351a9d16f82f74177", "lowering_digest": "244471f8fd6eaa7bcd1ea5e62da6ee06596ad103f909b05c2b773428f189420c", "realization_identity": "d3ddc292abb31dd4e5bca6b5db27dbc88c9180a4a7325a2a3b472709b3d53d6c", "evidence_digest": "81ef2b6ddeaf7284a770bc34579f9f42426416f99d19fc35b8480ee481b49900", "record_digest": "45fff051146e442d9fb92f720305350f33cb53cbe2e78879995b4d7169edf778", "provenance_class": "SIMULATED_DEVELOPMENT"} + ] +} diff --git a/forge/.crumb b/forge/.crumb index 9ce66ed..6895367 100644 --- a/forge/.crumb +++ b/forge/.crumb @@ -22,6 +22,12 @@ "name": "analog-sim", "purpose": "Directory forge/analog-sim. Purpose not yet described by a human." }, + { + "digest": "843298f1f241ac4785662d45297410b3aaedb3dccb713c0ac02c490ac5c6751a", + "last_commit": "7aba0b9", + "name": "form", + "purpose": "FORGE REALIZATION FORM V1: substrate-neutral physical-form realization IR (LINEAR_OPERATOR), can" + }, { "digest": "ccbca195c2d8d5e78ab7326fc75a72c6ecd4c7c0e76afa98b7e99cbd5c1bc766", "last_commit": "5497705", @@ -29,15 +35,15 @@ "purpose": "Directory forge/v2. Purpose not yet described by a human." } ], - "children_root": "sha256:a871fb13190a3bef239931f8691ab00c609be051e78e968495f5e7dc2fcf2932", - "compiled_at": "2026-10-04T13:47:27Z", + "children_root": "sha256:aff3f6326f95820eac28aa51dd874024e5b82e29625cc4f8afe4d69672832ca6", + "compiled_at": "2026-10-04T16:07:57Z", "compiler": "crumb-compile/1", - "digest": "sha256:e07cb55f9a5843cb4db673e521870da3e2e195153806de48704de7f776e90adc", + "digest": "sha256:bf1be2f8b458c9343fdebbef9f8906cfe850fdd53e4a8b481737889dbacc0584", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 10, - "generated_at_commit": "e0be0e765af3353984b65f8c822a5bb9616272de", - "source_tree": "sha256:7c6b3c5d3bcad9526023913f54680b24254fe7eab9f121828cd46fdcd02e8fb4" + "files": 12, + "generated_at_commit": "7aba0b9597b9163a3890a266abb87c293cb396db", + "source_tree": "sha256:48b820b426c003ecfac780b72fbcdc965ce4c7680f13b037a4ef608483a2b57d" }, "owner_lane": "", "provenance": { diff --git a/forge/form/.crumb b/forge/form/.crumb new file mode 100644 index 0000000..368f9d9 --- /dev/null +++ b/forge/form/.crumb @@ -0,0 +1,19 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-04T16:07:57Z", + "compiler": "crumb-compile/1", + "digest": "sha256:843298f1f241ac4785662d45297410b3aaedb3dccb713c0ac02c490ac5c6751a", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 2, + "generated_at_commit": "7aba0b9597b9163a3890a266abb87c293cb396db", + "source_tree": "sha256:cbbdb9f49d20db4320ab7b4c36acbb6d3ed39a92bc08a3af41c6bc420fa48e9f" + } + }, + "name": "forge/form", + "purpose": "FORGE REALIZATION FORM V1: substrate-neutral physical-form realization IR (LINEAR_OPERATOR), canonical encoding, digests, contract mapping, digital-reference and simulated-analog lowerings with V2 evidence. Host-only, SIMULATED_DEVELOPMENT. ADR 0034 (PROPOSED), extends ARCH-0018/0019.", + "schema_version": "1.0.0" +} diff --git a/forge/form/forge_form.c b/forge/form/forge_form.c new file mode 100644 index 0000000..2c35da0 --- /dev/null +++ b/forge/form/forge_form.c @@ -0,0 +1,622 @@ +/* + * forge/form/forge_form.c -- FORGE REALIZATION FORM V1 reference: canonical + * encoding, digests, contract mapping, two lowerings (digital reference, + * simulated analog) and realization with V2 evidence. Host-only: no + * hardware, no heap, no I/O. See forge_form.h. + */ +#include "forge_form.h" + +#include + +/* ------------------------------------------------------------------ */ +/* primitives */ +/* ------------------------------------------------------------------ */ + +static void put_u16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } +static void put_u32(uint8_t *p, uint32_t v) +{ + p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); +} +static void put_u64(uint8_t *p, uint64_t v) { put_u32(p, (uint32_t)v); put_u32(p + 4, (uint32_t)(v >> 32)); } +static uint16_t get_u16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } +static uint32_t get_u32(const uint8_t *p) +{ + return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); +} +static uint64_t sat_add(uint64_t a, uint64_t b) { return a > UINT64_MAX - b ? UINT64_MAX : a + b; } +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 - 1) / d; + return q > UINT64_MAX ? UINT64_MAX : (uint64_t)q; +} +static void label_digest(const char *label, uint8_t out[32]) +{ + forge_v2_sha256((const uint8_t *)label, strlen(label), out); +} + +/* ------------------------------------------------------------------ */ +/* schema: every field required, fixed width, ascending tags */ +/* ------------------------------------------------------------------ */ + +enum { T_U32 = 1, T_D32 = 2 }; +typedef struct { uint32_t tag; uint8_t type; size_t off; } Field; + +#define FORM_NFIELDS 5u +static const Field FORM_FIELDS[FORM_NFIELDS] = { + { 1, T_U32, offsetof(ForgeRealizationForm, form_kind) }, + { 2, T_U32, offsetof(ForgeRealizationForm, scalar_domain) }, + { 3, T_U32, offsetof(ForgeRealizationForm, operator_rows) }, + { 4, T_U32, offsetof(ForgeRealizationForm, operator_cols) }, + { 5, T_U32, offsetof(ForgeRealizationForm, accumulation) }, +}; + +#define RECORD_NFIELDS 8u +static const Field RECORD_FIELDS[RECORD_NFIELDS] = { + { 1, T_D32, offsetof(ForgeFormRealizationRecord, form_digest) }, + { 2, T_D32, offsetof(ForgeFormRealizationRecord, contract_digest) }, + { 3, T_D32, offsetof(ForgeFormRealizationRecord, substrate_digest) }, + { 4, T_D32, offsetof(ForgeFormRealizationRecord, lowering_digest) }, + { 5, T_D32, offsetof(ForgeFormRealizationRecord, realization_identity) }, + { 6, T_D32, offsetof(ForgeFormRealizationRecord, evidence_digest) }, + { 7, T_U32, offsetof(ForgeFormRealizationRecord, provenance_class) }, + { 8, T_U32, offsetof(ForgeFormRealizationRecord, decision) }, +}; + +static size_t field_width(uint8_t type) { return type == T_U32 ? 4u : 32u; } + +static int is_zero32(const uint8_t *p) +{ + uint8_t acc = 0; + for (size_t i = 0; i < 32; i++) acc |= p[i]; + return acc == 0; +} + +/* ---- validation ---- */ + +int forge_form_validate(const ForgeRealizationForm *f) +{ + if (!f) return FORGE_FORM_ERR_ARG; + switch (f->form_kind) { + case FORGE_FORM_LINEAR_OPERATOR: break; + case FORGE_FORM_ENERGY_FUNCTIONAL: + case FORGE_FORM_STOCHASTIC_DISTRIBUTION: return FORGE_FORM_ERR_UNSUPPORTED_FORM; + default: return FORGE_FORM_ERR_VALUE; + } + if (f->scalar_domain != FORGE_FORM_DOMAIN_INT_WRAP_U64 && f->scalar_domain != FORGE_FORM_DOMAIN_INT_WRAP_U32) + return FORGE_FORM_ERR_VALUE; + if (f->operator_rows == 0 || f->operator_rows > FORGE_FORM_MAX_DIM) return FORGE_FORM_ERR_VALUE; + if (f->operator_cols == 0 || f->operator_cols > FORGE_FORM_MAX_DIM) return FORGE_FORM_ERR_VALUE; + if (f->accumulation != FORGE_FORM_ACCUM_EXACT_MODULAR) return FORGE_FORM_ERR_VALUE; + return FORGE_FORM_OK; +} + +static int record_validate(const ForgeFormRealizationRecord *r) +{ + if (!r) return FORGE_FORM_ERR_ARG; + if (is_zero32(r->form_digest) || is_zero32(r->contract_digest) || is_zero32(r->substrate_digest) || + is_zero32(r->lowering_digest) || is_zero32(r->realization_identity) || is_zero32(r->evidence_digest)) + return FORGE_FORM_ERR_VALUE; + if (r->provenance_class != FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT) return FORGE_FORM_ERR_VALUE; + if (r->decision != FORGE_FORM_DECISION_PUBLISHED && r->decision != FORGE_FORM_DECISION_REFUSED) + return FORGE_FORM_ERR_VALUE; + return FORGE_FORM_OK; +} + +/* ---- generic encode / decode ---- */ + +static int enc_obj(uint16_t kind, const Field *fields, size_t nf, const void *obj, uint8_t *buf, size_t cap, + size_t *out_len) +{ + size_t n = FORGE_FORM_HEADER_LEN; + const uint8_t *base = (const uint8_t *)obj; + if (!buf || !out_len) return FORGE_FORM_ERR_ARG; + for (size_t i = 0; i < nf; i++) { + size_t w = field_width(fields[i].type); + if (n + FORGE_FORM_TLV_HEAD_LEN + w > cap) return FORGE_FORM_ERR_SPACE; + put_u32(buf + n, fields[i].tag); + put_u32(buf + n + 4, (uint32_t)w); + if (fields[i].type == T_U32) { + uint32_t v; + memcpy(&v, base + fields[i].off, sizeof v); + put_u32(buf + n + 8, v); + } else { + memcpy(buf + n + 8, base + fields[i].off, 32); + } + n += FORGE_FORM_TLV_HEAD_LEN + w; + } + if (cap < FORGE_FORM_HEADER_LEN) return FORGE_FORM_ERR_SPACE; + buf[0] = FORGE_FORM_MAGIC0; buf[1] = FORGE_FORM_MAGIC1; buf[2] = FORGE_FORM_MAGIC2; buf[3] = FORGE_FORM_MAGIC3; + put_u16(buf + 4, kind); + put_u16(buf + 6, FORGE_FORM_WIRE_VERSION); + put_u32(buf + 8, (uint32_t)(n - FORGE_FORM_HEADER_LEN)); + *out_len = n; + return FORGE_FORM_OK; +} + +static int dec_obj(uint16_t kind, const Field *fields, size_t nf, const uint8_t *buf, size_t len, int strict, + void *obj, size_t obj_size) +{ + uint8_t *base = (uint8_t *)obj; + uint32_t prev_tag = 0; + uint8_t present[16] = { 0 }; + size_t n = FORGE_FORM_HEADER_LEN; + if (!buf || !obj) return FORGE_FORM_ERR_ARG; + memset(obj, 0, obj_size); + if (len < FORGE_FORM_HEADER_LEN) return FORGE_FORM_ERR_TRUNCATED; + if (len > FORGE_FORM_MAX_OBJECT_LEN) return FORGE_FORM_ERR_LENGTH; + if (buf[0] != FORGE_FORM_MAGIC0 || buf[1] != FORGE_FORM_MAGIC1 || buf[2] != FORGE_FORM_MAGIC2 || + buf[3] != FORGE_FORM_MAGIC3) + return FORGE_FORM_ERR_MAGIC; + if (get_u16(buf + 4) != kind) return FORGE_FORM_ERR_KIND; + if (get_u16(buf + 6) != FORGE_FORM_WIRE_VERSION) return FORGE_FORM_ERR_VERSION; + if ((size_t)get_u32(buf + 8) != len - FORGE_FORM_HEADER_LEN) return FORGE_FORM_ERR_LENGTH; + while (n < len) { + uint32_t tag, flen; + size_t i; + if (len - n < FORGE_FORM_TLV_HEAD_LEN) return FORGE_FORM_ERR_TRUNCATED; + tag = get_u32(buf + n); + flen = get_u32(buf + n + 4); + if (tag <= prev_tag) return FORGE_FORM_ERR_ORDER; /* duplicate or descending */ + prev_tag = tag; + n += FORGE_FORM_TLV_HEAD_LEN; + if (len - n < flen) return FORGE_FORM_ERR_TRUNCATED; + for (i = 0; i < nf; i++) if (fields[i].tag == tag) break; + if (i == nf) { + if (strict) return FORGE_FORM_ERR_UNKNOWN_TAG; + n += flen; + continue; + } + if (flen != field_width(fields[i].type)) return FORGE_FORM_ERR_WIDTH; + if (fields[i].type == T_U32) { + uint32_t v = get_u32(buf + n); + memcpy(base + fields[i].off, &v, sizeof v); + } else { + memcpy(base + fields[i].off, buf + n, 32); + } + present[i] = 1; + n += flen; + } + for (size_t i = 0; i < nf; i++) if (!present[i]) return FORGE_FORM_ERR_MISSING; + return FORGE_FORM_OK; +} + +static int dec_public(uint16_t kind, const Field *fields, size_t nf, const uint8_t *buf, size_t len, int strict, + void *obj, size_t obj_size, int (*validate)(const void *)) +{ + int rc = dec_obj(kind, fields, nf, buf, len, strict, obj, obj_size); + if (rc != FORGE_FORM_OK) { if (obj) memset(obj, 0, obj_size); return rc; } + rc = validate(obj); + if (rc != FORGE_FORM_OK) { memset(obj, 0, obj_size); return rc; } + if (strict) { + uint8_t again[FORGE_FORM_MAX_OBJECT_LEN]; + size_t m = 0; + rc = enc_obj(kind, fields, nf, obj, again, sizeof again, &m); + if (rc != FORGE_FORM_OK || m != len || memcmp(again, buf, len) != 0) { + memset(obj, 0, obj_size); + return FORGE_FORM_ERR_NONCANONICAL; + } + } + return FORGE_FORM_OK; +} + +static int validate_form_v(const void *o) { return forge_form_validate((const ForgeRealizationForm *)o); } +static int validate_record_v(const void *o) { return record_validate((const ForgeFormRealizationRecord *)o); } + +int forge_form_encode(const ForgeRealizationForm *f, uint8_t *buf, size_t cap, size_t *out_len) +{ + int rc = forge_form_validate(f); + if (rc != FORGE_FORM_OK) return rc; + return enc_obj(FORGE_FORM_KIND_FORM, FORM_FIELDS, FORM_NFIELDS, f, buf, cap, out_len); +} + +int forge_form_decode(const uint8_t *buf, size_t len, int strict, ForgeRealizationForm *f) +{ + return dec_public(FORGE_FORM_KIND_FORM, FORM_FIELDS, FORM_NFIELDS, buf, len, strict, f, sizeof *f, + validate_form_v); +} + +int forge_form_digest(const ForgeRealizationForm *f, uint8_t out[32]) +{ + uint8_t buf[256]; + size_t n = 0; + int rc = forge_form_encode(f, buf, sizeof buf, &n); + if (rc != FORGE_FORM_OK) return rc; + forge_v2_sha256(buf, n, out); + return FORGE_FORM_OK; +} + +int forge_form_encode_record(const ForgeFormRealizationRecord *r, uint8_t *buf, size_t cap, size_t *out_len) +{ + int rc = record_validate(r); + if (rc != FORGE_FORM_OK) return rc; + return enc_obj(FORGE_FORM_KIND_REALIZATION_RECORD, RECORD_FIELDS, RECORD_NFIELDS, r, buf, cap, out_len); +} + +int forge_form_decode_record(const uint8_t *buf, size_t len, int strict, ForgeFormRealizationRecord *r) +{ + return dec_public(FORGE_FORM_KIND_REALIZATION_RECORD, RECORD_FIELDS, RECORD_NFIELDS, buf, len, strict, r, + sizeof *r, validate_record_v); +} + +int forge_form_digest_record(const ForgeFormRealizationRecord *r, uint8_t out[32]) +{ + uint8_t buf[512]; + size_t n = 0; + int rc = forge_form_encode_record(r, buf, sizeof buf, &n); + if (rc != FORGE_FORM_OK) return rc; + forge_v2_sha256(buf, n, out); + return FORGE_FORM_OK; +} + +void forge_form_lowering_digest(const uint8_t form_digest[32], const uint8_t code_digest[32], uint8_t out[32]) +{ + static const char domain[19] = { 'F','O','R','G','E','-','F','O','R','M','-','L','O','W','E','R','-','V','1' }; + uint8_t in[19 + 64]; + memcpy(in, domain, 19); + memcpy(in + 19, form_digest, 32); + memcpy(in + 51, code_digest, 32); + forge_v2_sha256(in, sizeof in, out); +} + +/* ------------------------------------------------------------------ */ +/* contract mapping */ +/* ------------------------------------------------------------------ */ + +static uint32_t oracle_of(const uint8_t digest[32]) +{ + uint8_t d[32]; + if (forge_asim_oracle_digest(FORGE_ASIM_ORACLE_EXACT_U64, d) == FORGE_V2_OK && memcmp(d, digest, 32) == 0) + return FORGE_ASIM_ORACLE_EXACT_U64; + if (forge_asim_oracle_digest(FORGE_ASIM_ORACLE_I32, d) == FORGE_V2_OK && memcmp(d, digest, 32) == 0) + return FORGE_ASIM_ORACLE_I32; + return 0; +} + +int forge_form_from_contract(const ForgeSemanticResultContract *c, ForgeRealizationForm *out) +{ + uint32_t oracle; + if (!c || !out) return FORGE_FORM_ERR_ARG; + memset(out, 0, sizeof *out); + if (c->operation_family != FORGE_V2_OP_MATVEC) return FORGE_FORM_ERR_CONTRACT; + if (c->shape_rank != 2 || c->shape_dim[0] == 0 || c->shape_dim[1] == 0 || c->shape_dim[2] || c->shape_dim[3]) + return FORGE_FORM_ERR_CONTRACT; + oracle = oracle_of(c->reference_oracle_digest); + if (!oracle) return FORGE_FORM_ERR_CONTRACT; + out->form_kind = FORGE_FORM_LINEAR_OPERATOR; + out->scalar_domain = oracle == FORGE_ASIM_ORACLE_EXACT_U64 ? FORGE_FORM_DOMAIN_INT_WRAP_U64 + : FORGE_FORM_DOMAIN_INT_WRAP_U32; + out->operator_rows = c->shape_dim[0]; + out->operator_cols = c->shape_dim[1]; + out->accumulation = FORGE_FORM_ACCUM_EXACT_MODULAR; + return forge_form_validate(out); +} + +/* ------------------------------------------------------------------ */ +/* lowerings */ +/* ------------------------------------------------------------------ */ + +static const char *const DIGITAL_CODE_LABEL = "FORGE-FORM/lowering/linear-operator/digital-reference/v1"; +static const char *const ANALOG_CODE_LABEL = "FORGE-FORM/lowering/linear-operator/analog-sim/v1"; + +static int finish_lowering(const ForgeRealizationForm *f, ForgeFormLowering *out) +{ + if (forge_form_digest(f, out->form_digest) != FORGE_FORM_OK) return FORGE_FORM_ERR_ARG; + if (forge_v2_digest_substrate(&out->substrate, out->substrate_digest) != FORGE_V2_OK) + return FORGE_FORM_ERR_EVIDENCE; + forge_form_lowering_digest(out->form_digest, out->code_digest, out->lowering_digest); + return FORGE_FORM_OK; +} + +int forge_form_lower_digital(const ForgeRealizationForm *f, ForgeFormLowering *out) +{ + int rc; + if (!out) return FORGE_FORM_ERR_ARG; + memset(out, 0, sizeof *out); + if ((rc = forge_form_validate(f)) != FORGE_FORM_OK) return rc; + out->backend = FORGE_FORM_BACKEND_DIGITAL_REFERENCE; + forge_asim_digital_descriptor(&out->substrate); + label_digest(DIGITAL_CODE_LABEL, out->code_digest); + return finish_lowering(f, out); +} + +int forge_form_lower_analog_sim(const ForgeRealizationForm *f, const ForgeAnalogSimModel *declared, + const ForgeAnalogSimModel *actual, const ForgeCalibrationArtifact *cal, + ForgeFormLowering *out) +{ + int rc; + if (!out) return FORGE_FORM_ERR_ARG; + memset(out, 0, sizeof *out); + if (!declared || !actual || !cal) return FORGE_FORM_ERR_ARG; + if ((rc = forge_form_validate(f)) != FORGE_FORM_OK) return rc; + out->backend = FORGE_FORM_BACKEND_ANALOG_SIM; + out->declared = *declared; + out->actual = *actual; + out->cal = *cal; + out->has_calibration = 1; + if (forge_asim_describe(declared, cal, &out->substrate) != FORGE_V2_OK) return FORGE_FORM_ERR_ARG; + if (forge_v2_digest_calibration(cal, out->cal_digest) != FORGE_V2_OK) return FORGE_FORM_ERR_ARG; + label_digest(ANALOG_CODE_LABEL, out->code_digest); + return finish_lowering(f, out); +} + +/* ------------------------------------------------------------------ */ +/* admission and realization */ +/* ------------------------------------------------------------------ */ + +uint32_t forge_form_provenance(void) { return FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; } + +static int eligible(const ForgeFormLowering *lw, const ForgeSemanticResultContract *c, const ForgeV2Conditions *now) +{ + return forge_v2_substrate_eligible(&lw->substrate, c, lw->has_calibration ? &lw->cal : NULL, + FORGE_V2_FAULT_NONE, now); +} + +int forge_form_admit(const ForgeFormLowering *const *lw, size_t n, const ForgeSemanticResultContract *c, + const ForgeFormInput *in, size_t *n_eligible) +{ + ForgeV2Candidate set[8]; + uint8_t contract_digest[32]; + size_t nset = 0; + if (!lw || !c || !in || n > 8) return FORGE_FORM_ERR_ARG; + if (forge_v2_digest_contract(c, contract_digest) != FORGE_V2_OK) return FORGE_FORM_ERR_CONTRACT; + for (size_t i = 0; i < n; i++) { + if (!lw[i]) return FORGE_FORM_ERR_ARG; + if (eligible(lw[i], c, &in->now) != FORGE_V2_OK) continue; + memcpy(set[nset].contract_digest, contract_digest, 32); + memcpy(set[nset].machine_identity, in->machine_identity, 32); + memcpy(set[nset].substrate_digest, lw[i]->substrate_digest, 32); + set[nset].substrate_class = lw[i]->substrate.substrate_class; + nset++; + } + if (n_eligible) *n_eligible = nset; + return forge_v2_admit_plan(set, nset, contract_digest) == FORGE_V2_OK ? FORGE_FORM_OK + : FORGE_FORM_ERR_NO_FALLBACK; +} + +static size_t put_u64_at(uint8_t *p, uint64_t v) { put_u64(p, v); return 8; } + +static void vec_digest(const char *domain, const uint64_t *v, size_t n, uint8_t out[32]) +{ + /* SHA-256(domain | n (u64) | v[0..n) ) via a fixed stack buffer */ + uint8_t buf[64 + 8 + 8 * FORGE_ASIM_MAX_COLS]; + size_t k = strlen(domain); + memcpy(buf, domain, k); + k += put_u64_at(buf + k, (uint64_t)n); + for (size_t i = 0; i < n; i++) k += put_u64_at(buf + k, v[i]); + forge_v2_sha256(buf, k, out); +} + +static void input_digest(const uint64_t *A, const uint64_t *x, uint32_t M, uint32_t N, uint8_t out[32]) +{ + uint8_t da[32], dx[32], in[19 + 8 + 64]; + /* A is hashed row by row to stay inside the fixed buffer */ + uint8_t rows[32 * FORGE_ASIM_MAX_ROWS]; + for (uint32_t i = 0; i < M; i++) vec_digest("FORGE-FORM/input/row/v1", A + (size_t)i * N, N, rows + 32 * i); + forge_v2_sha256(rows, 32 * (size_t)M, da); + vec_digest("FORGE-FORM/input/x/v1", x, N, dx); + memcpy(in, "FORGE-FORM/input/v1", 19); + put_u32(in + 19, M); put_u32(in + 23, N); + memcpy(in + 27, da, 32); memcpy(in + 59, dx, 32); + forge_v2_sha256(in, 91, out); +} + +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; +} + +static int shape_ok(const ForgeFormInput *in) +{ + if (!in->A || !in->x || in->M == 0 || in->N == 0 || in->M > FORGE_ASIM_MAX_ROWS || in->N > FORGE_ASIM_MAX_COLS) + return 0; + for (size_t k = 0; k < (size_t)in->M * in->N; k++) if (in->A[k] >> FORGE_ASIM_INPUT_BITS) return 0; + for (uint32_t j = 0; j < in->N; j++) if (in->x[j] >> FORGE_ASIM_INPUT_BITS) return 0; + return 1; +} + +static void compute_ref(uint32_t oracle, const ForgeFormInput *in, uint64_t *ref) +{ + if (oracle == FORGE_ASIM_ORACLE_EXACT_U64) { + forge_asim_oracle_exact_u64(in->A, in->x, ref, in->M, in->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)in->M * in->N; k++) a32[k] = (uint32_t)in->A[k]; + for (uint32_t j = 0; j < in->N; j++) x32[j] = (uint32_t)in->x[j]; + forge_asim_oracle_i32(a32, x32, y32, in->M, in->N); + for (uint32_t i = 0; i < in->M; i++) ref[i] = y32[i]; + } +} + +/* One run of the lowering. Never consults the oracle. */ +static int run_once(const ForgeFormLowering *lw, const ForgeFormInput *in, uint32_t r, uint32_t oracle, + uint64_t *y, uint64_t *latency_ns) +{ + if (lw->backend == FORGE_FORM_BACKEND_DIGITAL_REFERENCE) { + compute_ref(oracle, in, y); /* the digital reference IS the oracle's arithmetic */ + *latency_ns = 0; + return FORGE_FORM_OK; + } + if (lw->backend == FORGE_FORM_BACKEND_ANALOG_SIM) { + uint64_t cal_age = in->now.execution_epoch_s - lw->cal.epoch_s; /* eligible => not negative */ + int32_t mid = (int32_t)(((int64_t)lw->cal.env_temp_min_mc + lw->cal.env_temp_max_mc) / 2); + int32_t dtemp = in->now.temperature_mc - mid; + int rc = forge_asim_realize(&lw->actual, lw->wrong_row, lw->wrong_delta, in->A, in->x, in->M, in->N, r, + cal_age, dtemp, y, latency_ns); + return rc == FORGE_ASIM_PUBLISHED_ANALOG ? FORGE_FORM_OK : FORGE_FORM_ERR_INPUT_RANGE; + } + return FORGE_FORM_ERR_ARG; +} + +static int seal(ForgeFormOutcome *out, const ForgeFormLowering *lw, const uint8_t contract_digest[32], + uint32_t decision) +{ + uint8_t buf[4096]; + size_t n; + ForgeExecutionEvidenceV2 *e = &out->evidence; + ForgeFormRealizationRecord *rec = &out->record; + e->provenance_class = forge_form_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_FORM_ERR_EVIDENCE; + forge_v2_sha256(buf, n, out->evidence_digest); + memset(rec, 0, sizeof *rec); + memcpy(rec->form_digest, lw->form_digest, 32); + memcpy(rec->contract_digest, contract_digest, 32); + memcpy(rec->substrate_digest, lw->substrate_digest, 32); + memcpy(rec->lowering_digest, lw->lowering_digest, 32); + memcpy(rec->realization_identity, e->realization_identity, 32); + memcpy(rec->evidence_digest, out->evidence_digest, 32); + rec->provenance_class = forge_form_provenance(); + rec->decision = decision; + if (forge_form_digest_record(rec, out->record_digest) != FORGE_FORM_OK) return FORGE_FORM_ERR_EVIDENCE; + return FORGE_FORM_OK; +} + +int forge_form_realize(const ForgeRealizationForm *f, const ForgeSemanticResultContract *c, + const ForgeFormLowering *lw, const ForgeFormInput *in, ForgeFormOutcome *out) +{ + ForgeRealizationForm derived; + uint8_t contract_digest[32], form_digest[32], input_id[32], ref_id[32]; + uint64_t ref[FORGE_ASIM_MAX_ROWS], first[FORGE_ASIM_MAX_ROWS], run_y[FORGE_ASIM_MAX_ROWS], + chosen[FORGE_ASIM_MAX_ROWS]; + uint64_t worst = 0, worst_rel = 0, lat = 0, lat_total = 0; + uint32_t oracle, runs, within_runs = 0, have_chosen = 0, differs = 0, exact_bad = 0; + int rc; + + if (!out) return FORGE_FORM_ERR_ARG; + memset(out, 0, sizeof *out); + out->status = FORGE_FORM_ERR_ARG; + if (!f || !c || !lw || !in) return out->status; + if (forge_form_validate(f) != FORGE_FORM_OK) return out->status; + /* the form must be exactly the one this contract maps to */ + if (forge_form_from_contract(c, &derived) != FORGE_FORM_OK || memcmp(&derived, f, sizeof derived) != 0) + return out->status = FORGE_FORM_ERR_CONTRACT; + if (forge_form_digest(f, form_digest) != FORGE_FORM_OK || memcmp(form_digest, lw->form_digest, 32) != 0) + return out->status = FORGE_FORM_ERR_CONTRACT; + if (in->M != f->operator_rows || in->N != f->operator_cols) return out->status = FORGE_FORM_ERR_CONTRACT; + oracle = oracle_of(c->reference_oracle_digest); + runs = c->min_samples; + if (!oracle || runs == 0 || runs > FORGE_ASIM_MAX_SAMPLES) return out->status; + if (!shape_ok(in)) return out->status = FORGE_FORM_ERR_INPUT_RANGE; + if (forge_v2_digest_contract(c, contract_digest) != FORGE_V2_OK) return out->status = FORGE_FORM_ERR_CONTRACT; + + out->eligibility_rc = eligible(lw, c, &in->now); + if (out->eligibility_rc != FORGE_V2_OK) { + out->refuse_reason = FORGE_ASIM_REFUSE_INELIGIBLE; + return out->status = FORGE_FORM_REFUSED; + } + if (c->error_norm != FORGE_V2_NORM_ELEMENTWISE_MAX || c->error_kind == FORGE_V2_ERROR_MEASURED_DISTRIBUTION) { + out->refuse_reason = FORGE_ASIM_REFUSE_UNSUPPORTED; + return out->status = FORGE_FORM_REFUSED; + } + + compute_ref(oracle, in, ref); + input_digest(in->A, in->x, in->M, in->N, input_id); + vec_digest("FORGE-FORM/output/linear-operator/v1", ref, in->M, ref_id); + + for (uint32_t r = 0; r < runs; r++) { + rc = run_once(lw, in, r, oracle, run_y, &lat); + if (rc != FORGE_FORM_OK) return out->status = rc; + lat_total = sat_add(lat_total, lat); + if (r == 0) memcpy(first, run_y, sizeof first); + else if (memcmp(first, run_y, (size_t)in->M * sizeof run_y[0]) != 0) differs = 1; + if (c->error_kind == FORGE_V2_ERROR_EXACT) { + uint8_t od[32]; + vec_digest("FORGE-FORM/output/linear-operator/v1", run_y, in->M, od); + if (memcmp(od, ref_id, 32) != 0) exact_bad = 1; + } + uint32_t ok = 1; + for (uint32_t i = 0; i < in->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->ran = 1; + out->runs = runs; + out->runs_within_bound = within_runs; + out->error_abs = worst; + + if (c->error_kind == FORGE_V2_ERROR_EXACT && + (exact_bad || (lw->backend == FORGE_FORM_BACKEND_ANALOG_SIM && (!model_ideal(&lw->actual) || lw->wrong_row)))) + 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, published or refused; the form digest enters only via the lowering digest */ + ForgeExecutionEvidenceV2 *e = &out->evidence; + forge_v2_realization_identity(contract_digest, in->machine_identity, lw->substrate_digest, lw->lowering_digest, + lw->has_calibration ? lw->cal_digest : NULL, e->realization_identity); + memcpy(e->machine_identity, in->machine_identity, 32); + memcpy(e->substrate_identity, lw->substrate.substrate_identity, 32); + memcpy(e->semantic_contract_digest, contract_digest, 32); + e->has_calibration = lw->has_calibration; + if (lw->has_calibration) memcpy(e->calibration_digest, lw->cal_digest, 32); + memcpy(e->input_identity, input_id, 32); + vec_digest("FORGE-FORM/output/linear-operator/v1", have_chosen ? chosen : first, in->M, e->output_identity); + e->execution_epoch_s = in->now.execution_epoch_s; + e->execution_duration_ns = lat_total; + e->conversion_duration_ns = lat; + e->env_temperature_mc = in->now.temperature_mc; + e->env_supply_uv = in->now.supply_uv; + e->measured_error_abs = worst; /* V2 field name; here a simulated / host 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 (out->refuse_reason == FORGE_ASIM_REFUSE_NONE) { + if ((rc = seal(out, lw, contract_digest, FORGE_FORM_DECISION_PUBLISHED)) != FORGE_FORM_OK) + return out->status = rc; + memcpy(out->y, chosen, (size_t)in->M * sizeof chosen[0]); + out->published = 1; + return out->status = FORGE_FORM_PUBLISHED; + } + if ((rc = seal(out, lw, contract_digest, FORGE_FORM_DECISION_REFUSED)) != FORGE_FORM_OK) + return out->status = rc; + return out->status = FORGE_FORM_REFUSED; +} + +const char *forge_form_strerror(int err) +{ + switch (err) { + case FORGE_FORM_OK: return "ok"; + case FORGE_FORM_REFUSED: return "refused"; + case FORGE_FORM_ERR_ARG: return "bad argument"; + case FORGE_FORM_ERR_SPACE: return "output buffer too small"; + case FORGE_FORM_ERR_TRUNCATED: return "truncated"; + case FORGE_FORM_ERR_MAGIC: return "bad magic"; + case FORGE_FORM_ERR_KIND: return "wrong object kind"; + case FORGE_FORM_ERR_VERSION: return "unsupported wire version"; + case FORGE_FORM_ERR_LENGTH: return "body length mismatch"; + case FORGE_FORM_ERR_ORDER: return "tags not strictly ascending"; + case FORGE_FORM_ERR_UNKNOWN_TAG: return "unknown tag (strict)"; + case FORGE_FORM_ERR_WIDTH: return "field width wrong"; + case FORGE_FORM_ERR_MISSING: return "required field missing"; + case FORGE_FORM_ERR_VALUE: return "value out of range"; + case FORGE_FORM_ERR_NONCANONICAL: return "non-canonical encoding"; + case FORGE_FORM_ERR_UNSUPPORTED_FORM: return "form kind specified but not realizable here"; + case FORGE_FORM_ERR_CONTRACT: return "contract does not map to this form"; + case FORGE_FORM_ERR_NO_FALLBACK: return "eligible set holds no digital realization"; + case FORGE_FORM_ERR_EVIDENCE: return "evidence or record failed validation"; + case FORGE_FORM_ERR_INPUT_RANGE: return "input outside lowering limits"; + default: return "unknown"; + } +} diff --git a/forge/form/forge_form.h b/forge/form/forge_form.h new file mode 100644 index 0000000..748c0ce --- /dev/null +++ b/forge/form/forge_form.h @@ -0,0 +1,235 @@ +/* + * forge/form/forge_form.h -- FORGE REALIZATION FORM V1 (host-only reference). + * + * Architecture: extends ARCH-0018 (substrate-neutral physical realization) + * and ARCH-0019 (mixed-algebra realization); proposed as ADR 0034 + * "Physical-Form Realization IR" (PROPOSED, not accepted). + * + * A realization form is a REALIZATION-SIDE artifact: the mathematical + * structure that one or more physical substrates can embody. It sits below + * an Omega semantic operation and its SemanticResultContract, and beside + * the substrate-specific lowering. It is not an Omega program, not a new + * layer, not an authority, and it carries no substrate, vendor, device, + * calibration, address, clock or temperature field. + * + * Omega semantic operation + SemanticResultContract (semantic identity) + * | + * v forge_form_from_contract + * ForgeRealizationForm (form digest) + * | + * +-------------+--------------+ + * v v forge_form_lower_* + * digital reference lowering simulated analog lowering (lowering digest + * | | = H(form, code)) + * v v forge_form_realize + * V2 realization identity V2 realization identity (realization identity + * V2 evidence + record V2 evidence + record and evidence identity) + * + * Identity rules (the point of this module): + * - The form digest NEVER enters the semantic contract digest. + * - The form digest enters realization identity only through the + * code_digest slot of forge_v2_realization_identity (FORGE V2 bytes and + * digests are untouched). + * - Every realization of one form under one contract carries the SAME + * semantic_contract_digest and DIFFERENT realization identities. + * + * Provenance: every evidence record and realization record produced here is + * SIMULATED_DEVELOPMENT (host fixture; no provisioned Machine measured + * anything). There is no API to label a result PHYSICAL. + * + * Wire form (same discipline as FORGE V2, separate magic so the two + * families never collide): + * header : magic "FGFM" | kind u16 | version u16 (=1) | body_len u32 + * body : TLV fields, tag u32 | len u32 | value[len], strictly ascending + * tags, every field required, fixed widths, little-endian. + * digest : SHA-256 over header+body. + * + * Integers only, no heap, no hardware, no I/O. + */ +#ifndef FORGE_FORM_H +#define FORGE_FORM_H + +#include +#include + +#include "forge_substrate_v2.h" +#include "forge_analog_sim.h" + +#define FORGE_FORM_MAGIC0 'F' +#define FORGE_FORM_MAGIC1 'G' +#define FORGE_FORM_MAGIC2 'F' +#define FORGE_FORM_MAGIC3 'M' +#define FORGE_FORM_HEADER_LEN 12u +#define FORGE_FORM_TLV_HEAD_LEN 8u +#define FORGE_FORM_WIRE_VERSION 1u +#define FORGE_FORM_MAX_OBJECT_LEN 4096u +#define FORGE_FORM_MAX_DIM 65536u + +/* ---- object kinds ---- */ +enum { + FORGE_FORM_KIND_FORM = 0x0001, + FORGE_FORM_KIND_REALIZATION_RECORD = 0x0002 +}; + +/* ---- form kinds (numeric ids frozen; only LINEAR_OPERATOR is realizable) ---- */ +enum { + FORGE_FORM_INVALID = 0, + FORGE_FORM_LINEAR_OPERATOR = 1, /* y = A x over a declared scalar domain */ + FORGE_FORM_ENERGY_FUNCTIONAL = 2, /* SPECIFIED in ADR 0034, NOT IMPLEMENTED: decode refuses */ + FORGE_FORM_STOCHASTIC_DISTRIBUTION = 3 /* RESERVED, NOT IMPLEMENTED: decode refuses */ +}; + +/* ---- scalar domains (ARCH-0019: the algebraic domain a realization computes in) ---- */ +enum { + FORGE_FORM_DOMAIN_INVALID = 0, + FORGE_FORM_DOMAIN_INT_WRAP_U64 = 1, /* integers mod 2^64, wrapping (omega_matvec_reference) */ + FORGE_FORM_DOMAIN_INT_WRAP_U32 = 2 /* integers mod 2^32, wrapping (omega_matmul_cpu_oracle_i32, n = 1) */ +}; + +/* ---- accumulation semantics ---- */ +enum { + FORGE_FORM_ACCUM_INVALID = 0, + FORGE_FORM_ACCUM_EXACT_MODULAR = 1 /* exact modular sum; order-free in a wrapping integer ring */ +}; + +/* ---- lowering backends (realization side; NOT a form field) ---- */ +enum { + FORGE_FORM_BACKEND_DIGITAL_REFERENCE = 1, + FORGE_FORM_BACKEND_ANALOG_SIM = 2 +}; + +/* ---- realization record decision ---- */ +enum { + FORGE_FORM_DECISION_PUBLISHED = 1, + FORGE_FORM_DECISION_REFUSED = 2 +}; + +/* ---- errors / statuses ---- */ +enum { + FORGE_FORM_OK = 0, + FORGE_FORM_PUBLISHED = 0, /* forge_form_realize: result satisfied the contract */ + FORGE_FORM_REFUSED = 1, /* forge_form_realize: nothing published, evidence kept */ + FORGE_FORM_ERR_ARG = -1, + FORGE_FORM_ERR_SPACE = -2, + FORGE_FORM_ERR_TRUNCATED = -3, + FORGE_FORM_ERR_MAGIC = -4, + FORGE_FORM_ERR_KIND = -5, + FORGE_FORM_ERR_VERSION = -6, + FORGE_FORM_ERR_LENGTH = -7, + FORGE_FORM_ERR_ORDER = -8, + FORGE_FORM_ERR_UNKNOWN_TAG = -9, + FORGE_FORM_ERR_WIDTH = -10, + FORGE_FORM_ERR_MISSING = -11, + FORGE_FORM_ERR_VALUE = -12, + FORGE_FORM_ERR_NONCANONICAL = -13, + FORGE_FORM_ERR_UNSUPPORTED_FORM = -14, /* specified or reserved form kind with no realization here */ + FORGE_FORM_ERR_CONTRACT = -15, /* contract cannot be mapped to / does not match the form */ + FORGE_FORM_ERR_NO_FALLBACK = -16, /* eligible set holds no digital realization */ + FORGE_FORM_ERR_EVIDENCE = -17, /* evidence or record failed validation */ + FORGE_FORM_ERR_INPUT_RANGE = -18 /* inputs outside what the lowering can take */ +}; + +/* ---- objects ---- */ + +/* The form: mathematical structure only. */ +typedef struct { + uint32_t form_kind; /* FORGE_FORM_LINEAR_OPERATOR */ + uint32_t scalar_domain; /* FORGE_FORM_DOMAIN_* */ + uint32_t operator_rows; /* M, 1..FORGE_FORM_MAX_DIM */ + uint32_t operator_cols; /* N, 1..FORGE_FORM_MAX_DIM */ + uint32_t accumulation; /* FORGE_FORM_ACCUM_* */ +} ForgeRealizationForm; + +/* Evidence-side binding of one realization of one form under one contract. */ +typedef struct { + uint8_t form_digest[32]; + uint8_t contract_digest[32]; + uint8_t substrate_digest[32]; + uint8_t lowering_digest[32]; + uint8_t realization_identity[32]; + uint8_t evidence_digest[32]; + uint32_t provenance_class; /* always SIMULATED_DEVELOPMENT here */ + uint32_t decision; /* FORGE_FORM_DECISION_* */ +} ForgeFormRealizationRecord; + +/* A lowering: one way to embody the form on one substrate. */ +typedef struct { + uint32_t backend; /* FORGE_FORM_BACKEND_* */ + ForgeSubstrateDescriptor substrate; + uint8_t substrate_digest[32]; + uint8_t code_digest[32]; /* identity of the lowering code (label digest) */ + uint8_t form_digest[32]; + uint8_t lowering_digest[32]; /* H("FORGE-FORM-LOWER-V1" | form_digest | code_digest) */ + uint8_t has_calibration; + ForgeCalibrationArtifact cal; + uint8_t cal_digest[32]; + /* analog-sim backend only */ + ForgeAnalogSimModel declared, actual; + uint32_t wrong_row; + int64_t wrong_delta; +} ForgeFormLowering; + +typedef struct { + const uint64_t *A; /* M x N row-major */ + const uint64_t *x; /* N */ + uint32_t M, N; /* must equal the form's rows / cols */ + uint8_t machine_identity[32]; + ForgeV2Conditions now; +} ForgeFormInput; + +typedef struct { + int status; /* FORGE_FORM_PUBLISHED / FORGE_FORM_REFUSED / < 0 */ + uint32_t refuse_reason; /* FORGE_ASIM_REFUSE_* vocabulary */ + int eligibility_rc; + uint32_t ran, runs, runs_within_bound; + uint64_t error_abs; /* worst element error vs the contract's oracle */ + uint32_t published; + uint64_t y[FORGE_ASIM_MAX_ROWS]; /* zeroed unless published */ + ForgeExecutionEvidenceV2 evidence; /* valid when ran */ + uint8_t evidence_digest[32]; + ForgeFormRealizationRecord record; /* valid when ran */ + uint8_t record_digest[32]; +} ForgeFormOutcome; + +/* ---- encoding / identity ---- */ +int forge_form_encode(const ForgeRealizationForm *f, uint8_t *buf, size_t cap, size_t *out_len); +int forge_form_decode(const uint8_t *buf, size_t len, int strict, ForgeRealizationForm *f); +int forge_form_digest(const ForgeRealizationForm *f, uint8_t out[32]); +int forge_form_validate(const ForgeRealizationForm *f); + +int forge_form_encode_record(const ForgeFormRealizationRecord *r, uint8_t *buf, size_t cap, size_t *out_len); +int forge_form_decode_record(const uint8_t *buf, size_t len, int strict, ForgeFormRealizationRecord *r); +int forge_form_digest_record(const ForgeFormRealizationRecord *r, uint8_t out[32]); + +/* Lowering digest: SHA-256("FORGE-FORM-LOWER-V1" | form_digest | code_digest). */ +void forge_form_lowering_digest(const uint8_t form_digest[32], const uint8_t code_digest[32], uint8_t out[32]); + +/* ---- contract mapping (Omega / V2 contract -> form) ---- */ +/* MATVEC, rank 2, known oracle -> LINEAR_OPERATOR; anything else FORGE_FORM_ERR_CONTRACT. */ +int forge_form_from_contract(const ForgeSemanticResultContract *c, ForgeRealizationForm *out); + +/* ---- lowerings ---- */ +int forge_form_lower_digital(const ForgeRealizationForm *f, ForgeFormLowering *out); +int forge_form_lower_analog_sim(const ForgeRealizationForm *f, const ForgeAnalogSimModel *declared, + const ForgeAnalogSimModel *actual, const ForgeCalibrationArtifact *cal, + ForgeFormLowering *out); + +/* ---- admission and realization ---- */ +/* Eligible set over the given lowerings; FORGE_FORM_OK when it holds a + * digital realization of this contract (forge_v2_admit_plan), otherwise + * FORGE_FORM_ERR_NO_FALLBACK. *n_eligible receives the eligible count. */ +int forge_form_admit(const ForgeFormLowering *const *lw, size_t n, const ForgeSemanticResultContract *c, + const ForgeFormInput *in, size_t *n_eligible); + +/* Realize the form under the contract through one lowering: eligibility, + * contract.min_samples runs, check against the oracle the contract names, + * V2 evidence + realization record sealed whether published or refused. */ +int forge_form_realize(const ForgeRealizationForm *f, const ForgeSemanticResultContract *c, + const ForgeFormLowering *lw, const ForgeFormInput *in, ForgeFormOutcome *out); + +/* Fixed provenance of everything this module seals. */ +uint32_t forge_form_provenance(void); + +const char *forge_form_strerror(int err); + +#endif diff --git a/tests/.crumb b/tests/.crumb index 964ddd5..2e2061a 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -20,14 +20,14 @@ } ], "children_root": "sha256:be29e4df8a1e9e426d7723fccf6e09f23eb6fb07a6b21490cd019c75d6fed6cd", - "compiled_at": "2026-10-04T13:47:27Z", + "compiled_at": "2026-10-04T16:07:57Z", "compiler": "crumb-compile/1", - "digest": "sha256:c19ebd3b17c1ead4c0ed77e8945c8d765aa9de45074bc4e32d5e9d3a2b17f241", + "digest": "sha256:75efed90799746e089a7c60bf007dbbf0102d53299d4b1b21fe32f70c065aa93", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 13, - "generated_at_commit": "e0be0e765af3353984b65f8c822a5bb9616272de", - "source_tree": "sha256:c965592c10514646573af182eb826a805b5b1733d89eb316271bdbed49f1d280" + "files": 15, + "generated_at_commit": "7aba0b9597b9163a3890a266abb87c293cb396db", + "source_tree": "sha256:cc4a7d117c8eba611f9cbf8f2c034f5ae7223d2e7835a08d57dfde648efca84a" }, "owner_lane": "", "provenance": { diff --git a/tests/run_forge_form_gates.sh b/tests/run_forge_form_gates.sh new file mode 100755 index 0000000..ccdbcb6 --- /dev/null +++ b/tests/run_forge_form_gates.sh @@ -0,0 +1,122 @@ +#!/bin/bash +# tests/run_forge_form_gates.sh -- FORM0 gates: FORGE REALIZATION FORM V1 +# (substrate-neutral physical-form realization IR, ADR 0034 PROPOSED; extends +# ARCH-0018 / ARCH-0019). Host-only: no hardware, no driver, no GPU lock. +# Every result is SIMULATED_DEVELOPMENT. +# +# tests/run_forge_form_gates.sh build, run every check, verify the +# committed receipt is reproduced byte for byte +# tests/run_forge_form_gates.sh --regen also (re)write evidence/FORM0/.json +# +# 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_form" +RECEIPT_DIR="evidence/FORM0" +SRC_C="forge/form/forge_form.c" +SRC_H="forge/form/forge_form.h" +TEST_C="tests/test_forge_form.c" +SRCS="forge/v2/forge_substrate_v2.c forge/analog-sim/forge_analog_sim.c $SRC_C sha256_clean.c" +INC="-I./forge/v2 -I./forge/analog-sim -I./forge/form" +FAILS=0 +PASSES=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 "=== FORM0 realization-form gates (host-only, SIMULATED_DEVELOPMENT) ===" +mkdir -p "$BUILD_DIR" + +# shellcheck disable=SC2086 +gcc -O2 -Wall -Wextra -Werror -std=c11 $INC $SRCS "$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|INFO|RESULT)' +"$BIN" > /dev/null +gate "form0_checks_all_pass" $? + +# Same checks under the undefined-behaviour sanitizer (any UB aborts the run). +# shellcheck disable=SC2086 +gcc -O1 -g -Wall -Wextra -Werror -std=c11 -fsanitize=undefined -fno-sanitize-recover=all \ + $INC $SRCS "$TEST_C" -o "$BIN-ubsan" \ + && "$BIN-ubsan" > /dev/null +gate "form0_checks_pass_under_ubsan" $? + +# Deterministic receipt, named by the SHA-256 of its bytes. +"$BIN" --receipt > "$BUILD_DIR/form0-receipt.json" +gate "receipt_emitted_by_code" $? +SUM=$(sha256sum "$BUILD_DIR/form0-receipt.json" | cut -d' ' -f1) +if [ "${1:-}" = "--regen" ]; then + mkdir -p "$RECEIPT_DIR" + rm -f "$RECEIPT_DIR"/*.json + cp "$BUILD_DIR/form0-receipt.json" "$RECEIPT_DIR/$SUM.json" + gate "receipt_regenerated" $? +fi +"$BIN" --receipt | cmp -s - "$BUILD_DIR/form0-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/form0-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/form0-receipt.json" \ + && grep -q '"status": "PASS"' "$BUILD_DIR/form0-receipt.json" \ + && ! grep -qiE 'physical_result|measured_on_device|PROVENANCE_PHYSICAL' "$BUILD_DIR/form0-receipt.json" +gate "receipt_simulated_development_no_physical_claim" $? +CASES=$(grep -c '"name": ' "$BUILD_DIR/form0-receipt.json") +SIMCASES=$(grep -c '"provenance_class": "SIMULATED_DEVELOPMENT"}' "$BUILD_DIR/form0-receipt.json") +[ "$CASES" -gt 0 ] && [ "$CASES" -eq "$SIMCASES" ] +gate "every_receipt_case_simulated_development" $? + +# The success criterion is in the receipt: one contract, two realizations, +# same contract digest, different realization identities. +grep -q '"proof_two_realizations_same_contract": "PASS"' "$BUILD_DIR/form0-receipt.json" +gate "receipt_proof_two_realizations_same_contract" $? + +# The provider source never names the PHYSICAL provenance (except to refuse it). +[ "$(grep -c 'PROVENANCE_PHYSICAL' "$SRC_C" "$SRC_H" | awk -F: '{s+=$2} END {print s}')" -eq 0 ] +gate "provider_source_cannot_emit_physical" $? + +# The form object itself carries no substrate, vendor, device or calibration coordinate. +! awk '/^typedef struct \{/{b=1; buf=""} b{buf=buf $0 "\n"} /\} ForgeRealizationForm;/{print buf; b=0}' "$SRC_H" \ + | grep -qiE 'substrate|vendor|device|calibrat|clock|temperature|address|bus|machine' +gate "form_struct_has_no_realization_coordinates" $? + +# 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*(/\*|\*)' > /dev/null +gate "no_vendor_names_in_identifiers" $? + +# No quantum vocabulary in classical semantics. +! grep -niE 'quantum|superposition|wavefunction|collapse|entangle' "$SRC_C" "$SRC_H" > /dev/null +gate "no_quantum_metaphors_in_provider" $? + +# 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 / AR2 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" $? + +# FORGE V2 and AR2 files are untouched by this gate: their committed receipts +# and golden vectors must still reproduce (AR2 script also runs the AR1 gates). +bash tests/run_forge_analog_sim_gates.sh > "$BUILD_DIR/ar2-gates-from-form0.log" 2>&1 +gate "ar1_ar2_gates_still_pass" $? + +echo "RESULT gates: $PASSES passed, $FAILS failed" +[ "$FAILS" -eq 0 ] diff --git a/tests/test_forge_form.c b/tests/test_forge_form.c new file mode 100644 index 0000000..60d37b7 --- /dev/null +++ b/tests/test_forge_form.c @@ -0,0 +1,602 @@ +/* + * tests/test_forge_form.c -- FORM0 gate: FORGE REALIZATION FORM V1 (host-only). + * + * test_forge_form run every check, print PASS/FAIL lines + RESULT + * test_forge_form --receipt same checks, then print the deterministic JSON receipt + * + * Proves, on the host with SIMULATED_DEVELOPMENT provenance only: + * 1. canonical encoding + frozen digest of the LINEAR_OPERATOR form; + * 2. strict decode refuses every malformed, reordered, unknown-tag, wrong-width, + * out-of-range, reserved-kind and single-byte-mutated input, or reproduces + * the input byte for byte (canonical uniqueness); + * 3. one MATVEC SemanticResultContract maps to one form; the form digest is + * never an input of the contract digest; + * 4. the same contract is realized by two different methods (digital + * reference, simulated analog) with the SAME contract identity and + * DIFFERENT form-bound realization identities and evidence identities; + * 5. digital fallback and refusal paths keep evidence. + */ +#include +#include + +#include "forge_form.h" + +static int g_pass, g_fail, g_quiet; +static int g_records, g_records_sim; + +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); +} + +#define MAX_CASES 64 +typedef struct { + const char *name; + uint32_t backend, contract_kind; + int status; + uint32_t refuse, ran, runs, within; + uint64_t error_abs; + uint8_t contract[32], form[32], lowering[32], realization[32], evidence[32], record[32]; +} CaseRow; +static CaseRow g_cases[MAX_CASES]; +static int g_ncases; + +#define EPOCH0 1790000000ull +#define WINDOW (24ull * 3600) + +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-FORM/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; + 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-FORM/test/fixtures", 24, a->fixtures_digest); + forge_v2_sha256((const uint8_t *)"FORGE-FORM/test/evidence-root", 29, a->evidence_root); +} +static void fixture_contract(ForgeSemanticResultContract *c, uint32_t kind, uint32_t oracle, uint64_t bound, + uint32_t M, uint32_t N) +{ + memset(c, 0, sizeof *c); + 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; + } +} +static void fixture_input(ForgeFormInput *in, uint32_t M, uint32_t N) +{ + memset(in, 0, sizeof *in); + fill_inputs(M, N, 0x5eed0001ull + M * 131 + N); + in->A = g_A; in->x = g_x; in->M = M; in->N = N; + forge_v2_sha256((const uint8_t *)"FORGE-FORM/test/machine", 23, in->machine_identity); + in->now.execution_epoch_s = EPOCH0 + 3600; + in->now.temperature_mc = 25000; + in->now.supply_uv = 1000000; +} + +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 "MEASURED_DISTRIBUTION"; + } +} +static const char *backend_name(uint32_t b) +{ + return b == FORGE_FORM_BACKEND_ANALOG_SIM ? "analog_sim" : "digital_reference"; +} +static const char *status_name(int s) +{ + if (s == FORGE_FORM_PUBLISHED) return "published"; + if (s == FORGE_FORM_REFUSED) return "refused"; + return forge_form_strerror(s); +} +static void hex(const uint8_t *d) { for (int i = 0; i < 32; i++) printf("%02x", d[i]); } + +/* Run one lowering, audit its evidence and record it as a receipt row. */ +static ForgeFormOutcome g_out[MAX_CASES]; +static const ForgeFormOutcome *run(const char *name, const ForgeRealizationForm *f, + const ForgeSemanticResultContract *c, const ForgeFormLowering *lw, + const ForgeFormInput *in) +{ + ForgeFormOutcome *op = &g_out[g_ncases < MAX_CASES ? g_ncases : MAX_CASES - 1]; + int rc = forge_form_realize(f, c, lw, in, op); + check(name, rc == op->status); + if (op->ran) { + uint8_t buf[4096], d[32]; + size_t n; + ForgeFormRealizationRecord back; + g_records++; + if (op->evidence.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT && + op->record.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT) + g_records_sim++; + check("evidence_strict_decodes", forge_v2_encode_evidence(&op->evidence, buf, sizeof buf, &n) == FORGE_V2_OK && + forge_v2_decode_evidence(buf, n, 1, &(ForgeExecutionEvidenceV2){ 0 }) == + FORGE_V2_OK); + forge_v2_sha256(buf, n, d); + check("evidence_digest_matches", memcmp(d, op->evidence_digest, 32) == 0); + check("record_strict_roundtrip", forge_form_encode_record(&op->record, buf, sizeof buf, &n) == FORGE_FORM_OK && + forge_form_decode_record(buf, n, 1, &back) == FORGE_FORM_OK && + memcmp(&back, &op->record, sizeof back) == 0); + check("record_binds_form_and_contract", memcmp(op->record.form_digest, lw->form_digest, 32) == 0 && + memcmp(op->record.contract_digest, op->evidence.semantic_contract_digest, + 32) == 0 && + memcmp(op->record.realization_identity, op->evidence.realization_identity, + 32) == 0 && + memcmp(op->record.evidence_digest, op->evidence_digest, 32) == 0); + check("record_decision_matches_status", + op->record.decision == (op->status == FORGE_FORM_PUBLISHED ? FORGE_FORM_DECISION_PUBLISHED + : FORGE_FORM_DECISION_REFUSED)); + { + /* the realization identity really is V2's, with the lowering digest in the code slot */ + uint8_t rid[32]; + forge_v2_realization_identity(op->evidence.semantic_contract_digest, in->machine_identity, + lw->substrate_digest, lw->lowering_digest, + lw->has_calibration ? lw->cal_digest : NULL, rid); + check("realization_identity_is_v2_over_lowering_digest", memcmp(rid, op->evidence.realization_identity, 32) == 0); + } + } + if (g_ncases < MAX_CASES) { + CaseRow *r = &g_cases[g_ncases++]; + memset(r, 0, sizeof *r); + r->name = name; r->backend = lw->backend; r->contract_kind = c->error_kind; + r->status = op->status; r->refuse = op->refuse_reason; r->ran = op->ran; r->runs = op->runs; + r->within = op->runs_within_bound; r->error_abs = op->error_abs; + forge_v2_digest_contract(c, r->contract); + memcpy(r->form, lw->form_digest, 32); + memcpy(r->lowering, lw->lowering_digest, 32); + if (op->ran) { + memcpy(r->realization, op->evidence.realization_identity, 32); + memcpy(r->evidence, op->evidence_digest, 32); + memcpy(r->record, op->record_digest, 32); + } + } + return op; +} + +/* ---------------------------------------------------------------- */ +/* 1. canonical encoding, frozen digest */ +/* ---------------------------------------------------------------- */ + +/* Frozen KAT: LINEAR_OPERATOR, INT_WRAP_U64, 8 x 8, EXACT_MODULAR. */ +static const char *const KAT_FORM_DIGEST = "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8"; + +static void canonical_form(void) +{ + ForgeRealizationForm f = { FORGE_FORM_LINEAR_OPERATOR, FORGE_FORM_DOMAIN_INT_WRAP_U64, 8, 8, + FORGE_FORM_ACCUM_EXACT_MODULAR }, back; + uint8_t buf[256], d[32], again[256]; + size_t n = 0, m = 0; + char hexd[65]; + check("form_encode_ok", forge_form_encode(&f, buf, sizeof buf, &n) == FORGE_FORM_OK); + check("form_wire_len_is_12_plus_5x12", n == 12 + 5 * 12); + check("form_header_magic_kind_version", buf[0] == 'F' && buf[1] == 'G' && buf[2] == 'F' && buf[3] == 'M' && + buf[4] == 1 && buf[5] == 0 && buf[6] == 1 && buf[7] == 0); + check("form_strict_decode_roundtrip", forge_form_decode(buf, n, 1, &back) == FORGE_FORM_OK && + memcmp(&back, &f, sizeof f) == 0); + check("form_reencode_identical", forge_form_encode(&back, again, sizeof again, &m) == FORGE_FORM_OK && m == n && + memcmp(again, buf, n) == 0); + check("form_digest_ok", forge_form_digest(&f, d) == FORGE_FORM_OK); + for (int i = 0; i < 32; i++) snprintf(hexd + 2 * i, 3, "%02x", d[i]); + if (!g_quiet) printf("INFO form_digest_kat %s\n", hexd); + check("form_digest_kat_frozen", strcmp(hexd, KAT_FORM_DIGEST) == 0); + check("form_encode_too_small_refused", forge_form_encode(&f, buf, 20, &n) == FORGE_FORM_ERR_SPACE); + check("form_encode_null_refused", forge_form_encode(NULL, buf, sizeof buf, &n) == FORGE_FORM_ERR_ARG); +} + +/* ---------------------------------------------------------------- */ +/* 2. negative canonicalization */ +/* ---------------------------------------------------------------- */ + +static int dec(const uint8_t *b, size_t n) { ForgeRealizationForm f; return forge_form_decode(b, n, 1, &f); } + +static void negative_decode(void) +{ + ForgeRealizationForm f = { FORGE_FORM_LINEAR_OPERATOR, FORGE_FORM_DOMAIN_INT_WRAP_U64, 8, 8, + FORGE_FORM_ACCUM_EXACT_MODULAR }; + uint8_t ok[256], m[300]; + size_t n = 0; + forge_form_encode(&f, ok, sizeof ok, &n); + + memcpy(m, ok, n); m[0] = 'X'; + check("neg_bad_magic", dec(m, n) == FORGE_FORM_ERR_MAGIC); + memcpy(m, ok, n); m[4] = 2; + check("neg_wrong_kind", dec(m, n) == FORGE_FORM_ERR_KIND); + memcpy(m, ok, n); m[6] = 0; + check("neg_version_0_refused", dec(m, n) == FORGE_FORM_ERR_VERSION); + memcpy(m, ok, n); m[6] = 2; + check("neg_future_version_refused", dec(m, n) == FORGE_FORM_ERR_VERSION); + memcpy(m, ok, n); m[8] = (uint8_t)(m[8] + 1); + check("neg_body_len_mismatch", dec(m, n) == FORGE_FORM_ERR_LENGTH); + check("neg_truncated_header", dec(ok, 11) == FORGE_FORM_ERR_TRUNCATED); + { uint8_t t[300]; memcpy(t, ok, n); t[8] = (uint8_t)(n - 12 + 1); t[n] = 0; + check("neg_trailing_byte_is_length_error", dec(t, n + 1) == FORGE_FORM_ERR_LENGTH || dec(t, n + 1) == FORGE_FORM_ERR_TRUNCATED); } + { /* tag order: swap fields 1 and 2 */ + uint8_t t[300]; memcpy(t, ok, n); memcpy(t + 12, ok + 24, 12); memcpy(t + 24, ok + 12, 12); + check("neg_tag_order_refused", dec(t, n) == FORGE_FORM_ERR_ORDER); } + { /* duplicate tag: field 5 repeated as tag 5 again */ + uint8_t t[300]; memcpy(t, ok, n); memcpy(t + n, ok + n - 12, 12); t[8] = (uint8_t)(n - 12 + 12); + check("neg_duplicate_tag_refused", dec(t, n + 12) == FORGE_FORM_ERR_ORDER); } + { /* unknown tag 6 appended: strict refuses, lenient skips */ + uint8_t t[300]; ForgeRealizationForm g; memcpy(t, ok, n); + t[n] = 6; t[n+1] = t[n+2] = t[n+3] = 0; t[n+4] = 4; t[n+5] = t[n+6] = t[n+7] = 0; memset(t + n + 8, 0x11, 4); + t[8] = (uint8_t)(n - 12 + 12); + check("neg_unknown_tag_strict_refused", dec(t, n + 12) == FORGE_FORM_ERR_UNKNOWN_TAG); + check("neg_unknown_tag_lenient_skipped", forge_form_decode(t, n + 12, 0, &g) == FORGE_FORM_OK && + memcmp(&g, &f, sizeof f) == 0); } + { /* wrong width: tag 3 with len 8 */ + uint8_t t[300]; memcpy(t, ok, n); t[12 + 24 + 4] = 8; + check("neg_wrong_width_refused", dec(t, n) == FORGE_FORM_ERR_WIDTH || dec(t, n) == FORGE_FORM_ERR_TRUNCATED); } + { /* missing tag: drop field 5 */ + uint8_t t[300]; memcpy(t, ok, n - 12); t[8] = (uint8_t)(n - 12 - 12); + check("neg_missing_field_refused", dec(t, n - 12) == FORGE_FORM_ERR_MISSING); } + { ForgeRealizationForm g = f; g.form_kind = FORGE_FORM_ENERGY_FUNCTIONAL; uint8_t t[256]; + check("neg_energy_functional_specified_not_realizable", + forge_form_encode(&g, t, sizeof t, &n) == FORGE_FORM_ERR_UNSUPPORTED_FORM); + g.form_kind = FORGE_FORM_STOCHASTIC_DISTRIBUTION; + check("neg_stochastic_distribution_reserved_refused", + forge_form_encode(&g, t, sizeof t, &n) == FORGE_FORM_ERR_UNSUPPORTED_FORM); + g.form_kind = 9; + check("neg_unknown_form_kind_refused", forge_form_encode(&g, t, sizeof t, &n) == FORGE_FORM_ERR_VALUE); + g = f; g.scalar_domain = 7; + check("neg_unknown_domain_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); + g = f; g.operator_rows = 0; + check("neg_zero_rows_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); + g = f; g.operator_cols = FORGE_FORM_MAX_DIM + 1; + check("neg_cols_over_max_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); + g = f; g.accumulation = 2; + check("neg_unknown_accumulation_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); } + { /* decoded wire with a reserved kind is refused too (not only at encode) */ + uint8_t t[300]; memcpy(t, ok, n); t[20] = FORGE_FORM_ENERGY_FUNCTIONAL; + check("neg_wire_energy_functional_refused", dec(t, n) == FORGE_FORM_ERR_UNSUPPORTED_FORM); } + forge_form_encode(&f, ok, sizeof ok, &n); + { + /* single-byte mutation sweep: every mutant is refused, or decodes to a + * form whose canonical re-encoding is byte-identical to the mutant and + * whose digest differs from the original (canonical uniqueness) */ + uint8_t d0[32]; + int bad = 0, accepted = 0; + forge_form_digest(&f, d0); + for (size_t i = 0; i < n; i++) { + for (int bit = 0; bit < 8; bit++) { + ForgeRealizationForm g; + uint8_t t[256], again[256], d1[32]; + size_t m2 = 0; + memcpy(t, ok, n); + t[i] ^= (uint8_t)(1u << bit); + if (forge_form_decode(t, n, 1, &g) != FORGE_FORM_OK) continue; + accepted++; + if (forge_form_encode(&g, again, sizeof again, &m2) != FORGE_FORM_OK || m2 != n || + memcmp(again, t, n) != 0) + bad++; + forge_form_digest(&g, d1); + if (memcmp(d0, d1, 32) == 0) bad++; + } + } + if (!g_quiet) printf("INFO mutation sweep: %zu bits, %d accepted (all canonical), %d bad\n", n * 8, accepted, bad); + check("neg_single_byte_mutants_canonical_or_refused", bad == 0); + check("neg_some_value_mutants_are_valid_other_forms", accepted > 0); + } + { /* a record with a zero digest or a non-simulated provenance is refused */ + ForgeFormRealizationRecord r; uint8_t t[512]; + memset(&r, 0x5a, sizeof r); + r.provenance_class = FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; r.decision = FORGE_FORM_DECISION_PUBLISHED; + check("record_encode_ok", forge_form_encode_record(&r, t, sizeof t, &n) == FORGE_FORM_OK); + r.provenance_class = FORGE_V2_PROVENANCE_PHYSICAL; + check("neg_record_physical_provenance_refused", forge_form_encode_record(&r, t, sizeof t, &n) == FORGE_FORM_ERR_VALUE); + r.provenance_class = FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; memset(r.form_digest, 0, 32); + check("neg_record_zero_form_digest_refused", forge_form_encode_record(&r, t, sizeof t, &n) == FORGE_FORM_ERR_VALUE); } +} + +/* ---------------------------------------------------------------- */ +/* 3. contract mapping and identity separation */ +/* ---------------------------------------------------------------- */ + +static void contract_mapping(void) +{ + ForgeSemanticResultContract c, c2; + ForgeRealizationForm f, f2; + uint8_t cd[32], cd2[32], fd[32], fd2[32], cbytes[1024]; + size_t n = 0; + fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); + check("map_matvec_u64_to_linear_operator", forge_form_from_contract(&c, &f) == FORGE_FORM_OK && + f.form_kind == FORGE_FORM_LINEAR_OPERATOR && + f.scalar_domain == FORGE_FORM_DOMAIN_INT_WRAP_U64 && + f.operator_rows == 8 && f.operator_cols == 8); + forge_v2_digest_contract(&c, cd); + forge_form_digest(&f, fd); + /* the form digest is not an input of the contract digest */ + forge_v2_encode_contract(&c, cbytes, sizeof cbytes, &n); + { int found = 0; for (size_t i = 0; i + 32 <= n; i++) if (memcmp(cbytes + i, fd, 32) == 0) found = 1; + check("form_digest_not_in_contract_bytes", !found); } + forge_v2_digest_contract(&c, cd2); + check("contract_digest_unchanged_by_form_derivation", memcmp(cd, cd2, 32) == 0); + /* a weaker contract (bounded) on the same operation maps to the SAME form */ + fixture_contract(&c2, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); + forge_v2_digest_contract(&c2, cd2); + check("bounded_contract_has_different_contract_digest", memcmp(cd, cd2, 32) != 0); + check("bounded_contract_maps_to_same_form", forge_form_from_contract(&c2, &f2) == FORGE_FORM_OK && + forge_form_digest(&f2, fd2) == FORGE_FORM_OK && + memcmp(fd, fd2, 32) == 0); + /* a different oracle domain maps to a different form */ + fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_I32, 0, 8, 8); + check("i32_oracle_maps_to_u32_wrap_form", forge_form_from_contract(&c2, &f2) == FORGE_FORM_OK && + f2.scalar_domain == FORGE_FORM_DOMAIN_INT_WRAP_U32 && + forge_form_digest(&f2, fd2) == FORGE_FORM_OK && + memcmp(fd, fd2, 32) != 0); + /* unmappable contracts */ + fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); + c2.operation_family = FORGE_V2_OP_MATMUL; + check("map_non_matvec_refused", forge_form_from_contract(&c2, &f2) == FORGE_FORM_ERR_CONTRACT); + fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); + c2.shape_rank = 1; c2.shape_dim[1] = 0; + check("map_rank1_refused", forge_form_from_contract(&c2, &f2) == FORGE_FORM_ERR_CONTRACT); + fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); + memset(c2.reference_oracle_digest, 0x77, 32); + check("map_unknown_oracle_refused", forge_form_from_contract(&c2, &f2) == FORGE_FORM_ERR_CONTRACT); + /* the form carries no substrate: two lowerings share one form digest */ + { + ForgeFormLowering dl, al; + ForgeAnalogSimModel ideal = { 0 }; + ForgeCalibrationArtifact cal; + fixture_cal(&cal); + check("lower_digital_ok", forge_form_lower_digital(&f, &dl) == FORGE_FORM_OK); + check("lower_analog_sim_ok", forge_form_lower_analog_sim(&f, &ideal, &ideal, &cal, &al) == FORGE_FORM_OK); + check("lowerings_share_form_digest", memcmp(dl.form_digest, al.form_digest, 32) == 0 && + memcmp(dl.form_digest, fd, 32) == 0); + check("lowerings_differ_in_substrate_and_lowering_digest", + memcmp(dl.substrate_digest, al.substrate_digest, 32) != 0 && + memcmp(dl.lowering_digest, al.lowering_digest, 32) != 0); + { uint8_t ld[32]; + forge_form_lowering_digest(fd, dl.code_digest, ld); + check("lowering_digest_is_H_form_code", memcmp(ld, dl.lowering_digest, 32) == 0); } + check("lowerings_substrate_classes_digital_vs_analog", + dl.substrate.substrate_class == FORGE_V2_SUBSTRATE_DIGITAL_CPU && + al.substrate.substrate_class == FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY); + } +} + +/* ---------------------------------------------------------------- */ +/* 4. two realizations of one contract */ +/* ---------------------------------------------------------------- */ + +static uint8_t g_proof_contract[32], g_proof_form[32], g_proof_rid_d[32], g_proof_rid_a[32], + g_proof_ev_d[32], g_proof_ev_a[32]; +static int g_proof_ok; + +static void two_realizations(void) +{ + ForgeSemanticResultContract c; + ForgeRealizationForm f; + ForgeFormLowering dl, al, bad; + ForgeFormInput in; + ForgeAnalogSimModel ideal = { 0 }, biased = { 0 }, noisy = { 0 }; + ForgeCalibrationArtifact cal; + const ForgeFormOutcome *od, *oa; + size_t nel = 0; + uint8_t cd[32]; + + fixture_cal(&cal); + biased.bias_lsb = 2; + noisy.noise_peak_lsb = 2; noisy.noise_seed = 42; + + /* EXACT contract, ideal simulated analog: both realizations publish */ + fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); + fixture_input(&in, 8, 8); + forge_form_from_contract(&c, &f); + forge_form_lower_digital(&f, &dl); + forge_form_lower_analog_sim(&f, &ideal, &ideal, &cal, &al); + { const ForgeFormLowering *set[2] = { &al, &dl }; + check("admit_two_lowerings_with_digital", forge_form_admit(set, 2, &c, &in, &nel) == FORGE_FORM_OK && nel == 2); + check("admit_analog_only_refused_no_fallback", forge_form_admit(set, 1, &c, &in, &nel) == FORGE_FORM_ERR_NO_FALLBACK && nel == 1); } + od = run("exact_digital_reference_published", &f, &c, &dl, &in); + check("exact_digital_published", od->status == FORGE_FORM_PUBLISHED && od->published && od->error_abs == 0); + memcpy(g_proof_rid_d, od->evidence.realization_identity, 32); + memcpy(g_proof_ev_d, od->evidence_digest, 32); + { uint64_t ref[64]; forge_asim_oracle_exact_u64(in.A, in.x, ref, 8, 8); + check("exact_digital_output_equals_oracle", memcmp(od->y, ref, 8 * sizeof ref[0]) == 0); } + { uint8_t out_d[32]; memcpy(out_d, od->evidence.output_identity, 32); + oa = run("exact_analog_sim_ideal_published", &f, &c, &al, &in); + check("exact_analog_published", oa->status == FORGE_FORM_PUBLISHED && oa->published && oa->error_abs == 0); + check("exact_same_output_identity", memcmp(out_d, oa->evidence.output_identity, 32) == 0); } + memcpy(g_proof_rid_a, oa->evidence.realization_identity, 32); + memcpy(g_proof_ev_a, oa->evidence_digest, 32); + forge_v2_digest_contract(&c, cd); + memcpy(g_proof_contract, cd, 32); + memcpy(g_proof_form, dl.form_digest, 32); + g_proof_ok = memcmp(g_proof_rid_d, g_proof_rid_a, 32) != 0 && memcmp(g_proof_ev_d, g_proof_ev_a, 32) != 0 && + memcmp(od->evidence.semantic_contract_digest, cd, 32) == 0 && + memcmp(oa->evidence.semantic_contract_digest, cd, 32) == 0 && + memcmp(od->record.form_digest, oa->record.form_digest, 32) == 0 && + memcmp(od->record.lowering_digest, oa->record.lowering_digest, 32) != 0 && + memcmp(od->record.substrate_digest, oa->record.substrate_digest, 32) != 0 && + memcmp(od->evidence.substrate_identity, oa->evidence.substrate_identity, 32) != 0 && + od->evidence.has_calibration == 0 && oa->evidence.has_calibration == 1; + check("proof_same_contract_digest_in_both_realizations", + memcmp(od->evidence.semantic_contract_digest, oa->evidence.semantic_contract_digest, 32) == 0); + check("proof_same_form_digest_in_both_records", memcmp(od->record.form_digest, oa->record.form_digest, 32) == 0); + check("proof_different_realization_identities", memcmp(g_proof_rid_d, g_proof_rid_a, 32) != 0); + check("proof_different_evidence_identities", memcmp(g_proof_ev_d, g_proof_ev_a, 32) != 0); + check("proof_different_record_digests", memcmp(od->record_digest, oa->record_digest, 32) != 0); + check("proof_two_realizations_same_contract", g_proof_ok); + check("proof_both_simulated_development", od->evidence.provenance_class == forge_form_provenance() && + oa->evidence.provenance_class == forge_form_provenance() && + forge_form_provenance() == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT); + + /* BOUNDED_DETERMINISTIC: a biased simulated analog publishes a DIFFERENT + * output that still satisfies the same contract as the digital reference */ + fixture_contract(&c, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); + forge_form_from_contract(&c, &f); + forge_form_lower_digital(&f, &dl); + forge_form_lower_analog_sim(&f, &biased, &biased, &cal, &al); + od = run("bounded_digital_reference_published", &f, &c, &dl, &in); + check("bounded_digital_zero_error", od->status == FORGE_FORM_PUBLISHED && od->error_abs == 0); + { uint8_t out_d[32]; memcpy(out_d, od->evidence.output_identity, 32); + oa = run("bounded_analog_sim_bias2_published", &f, &c, &al, &in); + check("bounded_analog_published_with_error_2", oa->status == FORGE_FORM_PUBLISHED && oa->error_abs == 2 && + oa->runs_within_bound == oa->runs); + check("bounded_outputs_differ_same_contract", memcmp(out_d, oa->evidence.output_identity, 32) != 0 && + memcmp(od->evidence.semantic_contract_digest, + oa->evidence.semantic_contract_digest, 32) == 0); } + + /* over the bound: declared bias 2 (eligible) but actual bias 5: analog + * refused with evidence, digital answers */ + { ForgeAnalogSimModel worse = biased; worse.bias_lsb = 5; + forge_form_lower_analog_sim(&f, &biased, &worse, &cal, &al); } + oa = run("bounded_analog_sim_bias5_refused", &f, &c, &al, &in); + check("over_bound_refused_error_bound", oa->status == FORGE_FORM_REFUSED && !oa->published && + oa->refuse_reason == FORGE_ASIM_REFUSE_ERROR_BOUND && oa->ran && + oa->record.decision == FORGE_FORM_DECISION_REFUSED); + { int zero = 1; for (int i = 0; i < 64; i++) if (oa->y[i]) zero = 0; check("refused_publishes_nothing", zero); } + od = run("bounded_digital_fallback_published", &f, &c, &dl, &in); + check("fallback_digital_published_same_contract", od->status == FORGE_FORM_PUBLISHED && + memcmp(od->evidence.semantic_contract_digest, + oa->evidence.semantic_contract_digest, 32) == 0); + + /* declared ideal, actually biased: EXACT refused even if outputs matched */ + fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); + forge_form_from_contract(&c, &f); + forge_form_lower_analog_sim(&f, &ideal, &noisy, &cal, &al); + oa = run("exact_analog_sim_hidden_noise_refused", &f, &c, &al, &in); + check("exact_hidden_noise_refused", oa->status == FORGE_FORM_REFUSED && + oa->refuse_reason == FORGE_ASIM_REFUSE_EXACT_MISMATCH); + + /* BOUNDED_STOCHASTIC with noise within bound publishes */ + fixture_contract(&c, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); + forge_form_from_contract(&c, &f); + forge_form_lower_analog_sim(&f, &noisy, &noisy, &cal, &al); + oa = run("stochastic_analog_sim_noise2_published", &f, &c, &al, &in); + check("stochastic_published_within_confidence", oa->status == FORGE_FORM_PUBLISHED && oa->runs == 16 && + oa->runs_within_bound == 16); + forge_form_lower_digital(&f, &dl); + od = run("stochastic_digital_reference_published", &f, &c, &dl, &in); + check("stochastic_digital_published", od->status == FORGE_FORM_PUBLISHED && od->runs == 16); + + /* stale calibration: ineligible, never runs, no evidence */ + fixture_contract(&c, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); + forge_form_from_contract(&c, &f); + biased.bias_lsb = 2; + forge_form_lower_analog_sim(&f, &biased, &biased, &cal, &al); + in.now.execution_epoch_s = EPOCH0 + WINDOW + 1; + oa = run("stale_calibration_ineligible", &f, &c, &al, &in); + check("stale_cal_not_run", oa->status == FORGE_FORM_REFUSED && oa->refuse_reason == FORGE_ASIM_REFUSE_INELIGIBLE && + !oa->ran && oa->eligibility_rc == FORGE_V2_ERR_CAL_STALE); + in.now.execution_epoch_s = EPOCH0 + 3600; + + /* the digital reference is also eligible with a stale analog calibration (fallback stays) */ + forge_form_lower_digital(&f, &dl); + od = run("digital_reference_unaffected_by_analog_calibration", &f, &c, &dl, &in); + check("digital_published_regardless", od->status == FORGE_FORM_PUBLISHED); + + /* form / contract mismatch is refused before anything runs */ + { ForgeRealizationForm wrong = f; ForgeFormOutcome o; wrong.operator_rows = 4; + forge_form_lower_digital(&wrong, &bad); + check("mismatched_form_refused_err_contract", + forge_form_realize(&wrong, &c, &bad, &in, &o) == FORGE_FORM_ERR_CONTRACT && !o.ran); + check("lowering_of_other_form_refused", + forge_form_realize(&f, &c, &bad, &in, &o) == FORGE_FORM_ERR_CONTRACT && !o.ran); } + /* input outside the lowering's declared range */ + { ForgeFormOutcome o; uint64_t saved = g_A[0]; g_A[0] = 0x1FF; + check("input_out_of_range_refused", forge_form_realize(&f, &c, &dl, &in, &o) == FORGE_FORM_ERR_INPUT_RANGE); + g_A[0] = saved; } + /* i32 oracle domain through both lowerings */ + fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_I32, 0, 5, 7); + fixture_input(&in, 5, 7); + forge_form_from_contract(&c, &f); + forge_form_lower_digital(&f, &dl); + forge_form_lower_analog_sim(&f, &ideal, &ideal, &cal, &al); + od = run("i32_digital_reference_published", &f, &c, &dl, &in); + oa = run("i32_analog_sim_ideal_published", &f, &c, &al, &in); + check("i32_both_published_same_contract_different_realizations", + od->status == FORGE_FORM_PUBLISHED && oa->status == FORGE_FORM_PUBLISHED && + memcmp(od->evidence.semantic_contract_digest, oa->evidence.semantic_contract_digest, 32) == 0 && + memcmp(od->evidence.realization_identity, oa->evidence.realization_identity, 32) != 0); +} + +/* ---------------------------------------------------------------- */ +/* receipt */ +/* ---------------------------------------------------------------- */ + +static void print_receipt(void) +{ + printf("{\n"); + printf(" \"gate\": \"FORM0_LINEAR_OPERATOR_HOST\",\n"); + printf(" \"workstream\": \"ARCH-0018 / ARCH-0019 extension; ADR 0034 Physical-Form Realization IR (PROPOSED)\",\n"); + printf(" \"provenance_class\": \"SIMULATED_DEVELOPMENT\",\n"); + printf(" \"note\": \"Host-only reference. No device acted. The digital reference ran on the host CPU as a " + "development fixture; the analog lowering is the AR2 software model. Errors are simulated or host " + "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_records); + printf(" \"evidence_records_simulated_development\": %d,\n", g_records_sim); + printf(" \"proof_two_realizations_same_contract\": \"%s\",\n", g_proof_ok ? "PASS" : "FAIL"); + printf(" \"proof\": {\"contract_digest\": \""); hex(g_proof_contract); + printf("\", \"form_digest\": \""); hex(g_proof_form); + printf("\", \"digital_reference\": {\"realization_identity\": \""); hex(g_proof_rid_d); + printf("\", \"evidence_digest\": \""); hex(g_proof_ev_d); + printf("\"}, \"analog_sim\": {\"realization_identity\": \""); hex(g_proof_rid_a); + printf("\", \"evidence_digest\": \""); hex(g_proof_ev_a); + printf("\"}},\n"); + printf(" \"cases\": [\n"); + for (int i = 0; i < g_ncases; i++) { + const CaseRow *r = &g_cases[i]; + printf(" {\"name\": \"%s\", \"backend\": \"%s\", \"contract_kind\": \"%s\", \"outcome\": \"%s\", " + "\"refusal\": \"%s\", \"ran\": %u, \"runs\": %u, \"runs_within_bound\": %u, " + "\"error_vs_oracle_abs\": %llu, ", + r->name, backend_name(r->backend), kind_name(r->contract_kind), status_name(r->status), + forge_asim_refuse_name(r->refuse), r->ran, r->runs, r->within, (unsigned long long)r->error_abs); + printf("\"contract_digest\": \""); hex(r->contract); + printf("\", \"form_digest\": \""); hex(r->form); + printf("\", \"lowering_digest\": \""); hex(r->lowering); + printf("\", \"realization_identity\": \""); if (r->ran) hex(r->realization); + printf("\", \"evidence_digest\": \""); if (r->ran) hex(r->evidence); + printf("\", \"record_digest\": \""); if (r->ran) hex(r->record); + 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; + canonical_form(); + negative_decode(); + contract_mapping(); + two_realizations(); + if (receipt) print_receipt(); + else printf("RESULT checks: %d passed, %d failed\n", g_pass, g_fail); + return g_fail == 0 ? 0 : 1; +} ```