physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / tests / test_forge_form.c
| 1 | /* |
| 2 | * tests/test_forge_form.c -- FORM0 gate: FORGE REALIZATION FORM V1 (host-only). |
| 3 | * |
| 4 | * test_forge_form run every check, print PASS/FAIL lines + RESULT |
| 5 | * test_forge_form --receipt same checks, then print the deterministic JSON receipt |
| 6 | * |
| 7 | * Proves, on the host with SIMULATED_DEVELOPMENT provenance only: |
| 8 | * 1. canonical encoding + frozen digest of the LINEAR_OPERATOR form; |
| 9 | * 2. strict decode refuses every malformed, reordered, unknown-tag, wrong-width, |
| 10 | * out-of-range, reserved-kind and single-byte-mutated input, or reproduces |
| 11 | * the input byte for byte (canonical uniqueness); |
| 12 | * 3. one MATVEC SemanticResultContract maps to one form; the form digest is |
| 13 | * never an input of the contract digest; |
| 14 | * 4. the same contract is realized by two different methods (digital |
| 15 | * reference, simulated analog) with the SAME contract identity and |
| 16 | * DIFFERENT form-bound realization identities and evidence identities; |
| 17 | * 5. digital fallback and refusal paths keep evidence. |
| 18 | */ |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "forge_form.h" |
| 23 | |
| 24 | static int g_pass, g_fail, g_quiet; |
| 25 | static int g_records, g_records_sim; |
| 26 | |
| 27 | static void check(const char *name, int ok) |
| 28 | { |
| 29 | if (ok) g_pass++; else g_fail++; |
| 30 | if (!g_quiet || !ok) fprintf(g_quiet ? stderr : stdout, "%s %s\n", ok ? "PASS" : "FAIL", name); |
| 31 | } |
| 32 | |
| 33 | #define MAX_CASES 64 |
| 34 | typedef struct { |
| 35 | const char *name; |
| 36 | uint32_t backend, contract_kind; |
| 37 | int status; |
| 38 | uint32_t refuse, ran, runs, within; |
| 39 | uint64_t error_abs; |
| 40 | uint8_t contract[32], form[32], lowering[32], realization[32], evidence[32], record[32]; |
| 41 | } CaseRow; |
| 42 | static CaseRow g_cases[MAX_CASES]; |
| 43 | static int g_ncases; |
| 44 | |
| 45 | #define EPOCH0 1790000000ull |
| 46 | #define WINDOW (24ull * 3600) |
| 47 | |
| 48 | static uint64_t g_A[FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS], g_x[FORGE_ASIM_MAX_COLS]; |
| 49 | |
| 50 | static uint64_t lcg(uint64_t *s) |
| 51 | { |
| 52 | *s = *s * 6364136223846793005ull + 1442695040888963407ull; |
| 53 | return *s >> 33; |
| 54 | } |
| 55 | static void fill_inputs(uint32_t M, uint32_t N, uint64_t seed) |
| 56 | { |
| 57 | for (size_t k = 0; k < (size_t)M * N; k++) g_A[k] = lcg(&seed) & 0xFF; |
| 58 | for (uint32_t j = 0; j < N; j++) g_x[j] = lcg(&seed) & 0xFF; |
| 59 | } |
| 60 | static void fixture_cal(ForgeCalibrationArtifact *a) |
| 61 | { |
| 62 | memset(a, 0, sizeof *a); |
| 63 | forge_asim_substrate_identity(a->substrate_id); |
| 64 | forge_v2_sha256((const uint8_t *)"FORGE-FORM/test/calibration/1", 29, a->calibration_id); |
| 65 | a->method = FORGE_V2_CAL_SIMULATED_MODEL; |
| 66 | a->epoch_s = EPOCH0; |
| 67 | a->validity_window_s = WINDOW; |
| 68 | a->env_temp_min_mc = 20000; a->env_temp_max_mc = 30000; |
| 69 | a->env_supply_min_uv = 900000; a->env_supply_max_uv = 1100000; |
| 70 | a->noise_samples = 1024; |
| 71 | a->fixture_count = 8; |
| 72 | forge_v2_sha256((const uint8_t *)"FORGE-FORM/test/fixtures", 24, a->fixtures_digest); |
| 73 | forge_v2_sha256((const uint8_t *)"FORGE-FORM/test/evidence-root", 29, a->evidence_root); |
| 74 | } |
| 75 | static void fixture_contract(ForgeSemanticResultContract *c, uint32_t kind, uint32_t oracle, uint64_t bound, |
| 76 | uint32_t M, uint32_t N) |
| 77 | { |
| 78 | memset(c, 0, sizeof *c); |
| 79 | c->operation_family = FORGE_V2_OP_MATVEC; |
| 80 | forge_asim_oracle_digest(oracle, c->reference_oracle_digest); |
| 81 | c->shape_rank = 2; c->shape_dim[0] = M; c->shape_dim[1] = N; |
| 82 | c->error_kind = kind; |
| 83 | c->error_abs = kind == FORGE_V2_ERROR_EXACT ? 0 : bound; |
| 84 | c->error_norm = FORGE_V2_NORM_ELEMENTWISE_MAX; |
| 85 | c->confidence_ppm = 1000000; |
| 86 | c->min_samples = 4; |
| 87 | c->allowed_nondeterminism = FORGE_V2_NONDET_NONE; |
| 88 | if (kind == FORGE_V2_ERROR_BOUNDED_DETERMINISTIC) c->allowed_nondeterminism = FORGE_V2_NONDET_ROUNDING; |
| 89 | if (kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC) { |
| 90 | c->allowed_nondeterminism = FORGE_V2_NONDET_ROUNDING | FORGE_V2_NONDET_STOCHASTIC_NOISE; |
| 91 | c->min_samples = 16; |
| 92 | } |
| 93 | } |
| 94 | static void fixture_input(ForgeFormInput *in, uint32_t M, uint32_t N) |
| 95 | { |
| 96 | memset(in, 0, sizeof *in); |
| 97 | fill_inputs(M, N, 0x5eed0001ull + M * 131 + N); |
| 98 | in->A = g_A; in->x = g_x; in->M = M; in->N = N; |
| 99 | forge_v2_sha256((const uint8_t *)"FORGE-FORM/test/machine", 23, in->machine_identity); |
| 100 | in->now.execution_epoch_s = EPOCH0 + 3600; |
| 101 | in->now.temperature_mc = 25000; |
| 102 | in->now.supply_uv = 1000000; |
| 103 | } |
| 104 | |
| 105 | static const char *kind_name(uint32_t k) |
| 106 | { |
| 107 | switch (k) { |
| 108 | case FORGE_V2_ERROR_EXACT: return "EXACT"; |
| 109 | case FORGE_V2_ERROR_BOUNDED_DETERMINISTIC: return "BOUNDED_DETERMINISTIC"; |
| 110 | case FORGE_V2_ERROR_BOUNDED_STOCHASTIC: return "BOUNDED_STOCHASTIC"; |
| 111 | default: return "MEASURED_DISTRIBUTION"; |
| 112 | } |
| 113 | } |
| 114 | static const char *backend_name(uint32_t b) |
| 115 | { |
| 116 | return b == FORGE_FORM_BACKEND_ANALOG_SIM ? "analog_sim" : "digital_reference"; |
| 117 | } |
| 118 | static const char *status_name(int s) |
| 119 | { |
| 120 | if (s == FORGE_FORM_PUBLISHED) return "published"; |
| 121 | if (s == FORGE_FORM_REFUSED) return "refused"; |
| 122 | return forge_form_strerror(s); |
| 123 | } |
| 124 | static void hex(const uint8_t *d) { for (int i = 0; i < 32; i++) printf("%02x", d[i]); } |
| 125 | |
| 126 | /* Run one lowering, audit its evidence and record it as a receipt row. */ |
| 127 | static ForgeFormOutcome g_out[MAX_CASES]; |
| 128 | static const ForgeFormOutcome *run(const char *name, const ForgeRealizationForm *f, |
| 129 | const ForgeSemanticResultContract *c, const ForgeFormLowering *lw, |
| 130 | const ForgeFormInput *in) |
| 131 | { |
| 132 | ForgeFormOutcome *op = &g_out[g_ncases < MAX_CASES ? g_ncases : MAX_CASES - 1]; |
| 133 | int rc = forge_form_realize(f, c, lw, in, op); |
| 134 | check(name, rc == op->status); |
| 135 | if (op->ran) { |
| 136 | uint8_t buf[4096], d[32]; |
| 137 | size_t n; |
| 138 | ForgeFormRealizationRecord back; |
| 139 | g_records++; |
| 140 | if (op->evidence.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT && |
| 141 | op->record.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT) |
| 142 | g_records_sim++; |
| 143 | check("evidence_strict_decodes", forge_v2_encode_evidence(&op->evidence, buf, sizeof buf, &n) == FORGE_V2_OK && |
| 144 | forge_v2_decode_evidence(buf, n, 1, &(ForgeExecutionEvidenceV2){ 0 }) == |
| 145 | FORGE_V2_OK); |
| 146 | forge_v2_sha256(buf, n, d); |
| 147 | check("evidence_digest_matches", memcmp(d, op->evidence_digest, 32) == 0); |
| 148 | check("record_strict_roundtrip", forge_form_encode_record(&op->record, buf, sizeof buf, &n) == FORGE_FORM_OK && |
| 149 | forge_form_decode_record(buf, n, 1, &back) == FORGE_FORM_OK && |
| 150 | memcmp(&back, &op->record, sizeof back) == 0); |
| 151 | check("record_binds_form_and_contract", memcmp(op->record.form_digest, lw->form_digest, 32) == 0 && |
| 152 | memcmp(op->record.contract_digest, op->evidence.semantic_contract_digest, |
| 153 | 32) == 0 && |
| 154 | memcmp(op->record.realization_identity, op->evidence.realization_identity, |
| 155 | 32) == 0 && |
| 156 | memcmp(op->record.evidence_digest, op->evidence_digest, 32) == 0); |
| 157 | check("record_decision_matches_status", |
| 158 | op->record.decision == (op->status == FORGE_FORM_PUBLISHED ? FORGE_FORM_DECISION_PUBLISHED |
| 159 | : FORGE_FORM_DECISION_REFUSED)); |
| 160 | { |
| 161 | /* the realization identity really is V2's, with the lowering digest in the code slot */ |
| 162 | uint8_t rid[32]; |
| 163 | forge_v2_realization_identity(op->evidence.semantic_contract_digest, in->machine_identity, |
| 164 | lw->substrate_digest, lw->lowering_digest, |
| 165 | lw->has_calibration ? lw->cal_digest : NULL, rid); |
| 166 | check("realization_identity_is_v2_over_lowering_digest", memcmp(rid, op->evidence.realization_identity, 32) == 0); |
| 167 | } |
| 168 | } |
| 169 | if (g_ncases < MAX_CASES) { |
| 170 | CaseRow *r = &g_cases[g_ncases++]; |
| 171 | memset(r, 0, sizeof *r); |
| 172 | r->name = name; r->backend = lw->backend; r->contract_kind = c->error_kind; |
| 173 | r->status = op->status; r->refuse = op->refuse_reason; r->ran = op->ran; r->runs = op->runs; |
| 174 | r->within = op->runs_within_bound; r->error_abs = op->error_abs; |
| 175 | forge_v2_digest_contract(c, r->contract); |
| 176 | memcpy(r->form, lw->form_digest, 32); |
| 177 | memcpy(r->lowering, lw->lowering_digest, 32); |
| 178 | if (op->ran) { |
| 179 | memcpy(r->realization, op->evidence.realization_identity, 32); |
| 180 | memcpy(r->evidence, op->evidence_digest, 32); |
| 181 | memcpy(r->record, op->record_digest, 32); |
| 182 | } |
| 183 | } |
| 184 | return op; |
| 185 | } |
| 186 | |
| 187 | /* ---------------------------------------------------------------- */ |
| 188 | /* 1. canonical encoding, frozen digest */ |
| 189 | /* ---------------------------------------------------------------- */ |
| 190 | |
| 191 | /* Frozen KAT: LINEAR_OPERATOR, INT_WRAP_U64, 8 x 8, EXACT_MODULAR. */ |
| 192 | static const char *const KAT_FORM_DIGEST = "6e1769e21b1c1df854f4832618e276f3c556a45500af62636a51e1e862cf71d8"; |
| 193 | |
| 194 | static void canonical_form(void) |
| 195 | { |
| 196 | ForgeRealizationForm f = { FORGE_FORM_LINEAR_OPERATOR, FORGE_FORM_DOMAIN_INT_WRAP_U64, 8, 8, |
| 197 | FORGE_FORM_ACCUM_EXACT_MODULAR }, back; |
| 198 | uint8_t buf[256], d[32], again[256]; |
| 199 | size_t n = 0, m = 0; |
| 200 | char hexd[65]; |
| 201 | check("form_encode_ok", forge_form_encode(&f, buf, sizeof buf, &n) == FORGE_FORM_OK); |
| 202 | check("form_wire_len_is_12_plus_5x12", n == 12 + 5 * 12); |
| 203 | check("form_header_magic_kind_version", buf[0] == 'F' && buf[1] == 'G' && buf[2] == 'F' && buf[3] == 'M' && |
| 204 | buf[4] == 1 && buf[5] == 0 && buf[6] == 1 && buf[7] == 0); |
| 205 | check("form_strict_decode_roundtrip", forge_form_decode(buf, n, 1, &back) == FORGE_FORM_OK && |
| 206 | memcmp(&back, &f, sizeof f) == 0); |
| 207 | check("form_reencode_identical", forge_form_encode(&back, again, sizeof again, &m) == FORGE_FORM_OK && m == n && |
| 208 | memcmp(again, buf, n) == 0); |
| 209 | check("form_digest_ok", forge_form_digest(&f, d) == FORGE_FORM_OK); |
| 210 | for (int i = 0; i < 32; i++) snprintf(hexd + 2 * i, 3, "%02x", d[i]); |
| 211 | if (!g_quiet) printf("INFO form_digest_kat %s\n", hexd); |
| 212 | check("form_digest_kat_frozen", strcmp(hexd, KAT_FORM_DIGEST) == 0); |
| 213 | check("form_encode_too_small_refused", forge_form_encode(&f, buf, 20, &n) == FORGE_FORM_ERR_SPACE); |
| 214 | check("form_encode_null_refused", forge_form_encode(NULL, buf, sizeof buf, &n) == FORGE_FORM_ERR_ARG); |
| 215 | } |
| 216 | |
| 217 | /* ---------------------------------------------------------------- */ |
| 218 | /* 2. negative canonicalization */ |
| 219 | /* ---------------------------------------------------------------- */ |
| 220 | |
| 221 | static int dec(const uint8_t *b, size_t n) { ForgeRealizationForm f; return forge_form_decode(b, n, 1, &f); } |
| 222 | |
| 223 | static void negative_decode(void) |
| 224 | { |
| 225 | ForgeRealizationForm f = { FORGE_FORM_LINEAR_OPERATOR, FORGE_FORM_DOMAIN_INT_WRAP_U64, 8, 8, |
| 226 | FORGE_FORM_ACCUM_EXACT_MODULAR }; |
| 227 | uint8_t ok[256], m[300]; |
| 228 | size_t n = 0; |
| 229 | forge_form_encode(&f, ok, sizeof ok, &n); |
| 230 | |
| 231 | memcpy(m, ok, n); m[0] = 'X'; |
| 232 | check("neg_bad_magic", dec(m, n) == FORGE_FORM_ERR_MAGIC); |
| 233 | memcpy(m, ok, n); m[4] = 2; |
| 234 | check("neg_wrong_kind", dec(m, n) == FORGE_FORM_ERR_KIND); |
| 235 | memcpy(m, ok, n); m[6] = 0; |
| 236 | check("neg_version_0_refused", dec(m, n) == FORGE_FORM_ERR_VERSION); |
| 237 | memcpy(m, ok, n); m[6] = 2; |
| 238 | check("neg_future_version_refused", dec(m, n) == FORGE_FORM_ERR_VERSION); |
| 239 | memcpy(m, ok, n); m[8] = (uint8_t)(m[8] + 1); |
| 240 | check("neg_body_len_mismatch", dec(m, n) == FORGE_FORM_ERR_LENGTH); |
| 241 | check("neg_truncated_header", dec(ok, 11) == FORGE_FORM_ERR_TRUNCATED); |
| 242 | { uint8_t t[300]; memcpy(t, ok, n); t[8] = (uint8_t)(n - 12 + 1); t[n] = 0; |
| 243 | check("neg_trailing_byte_is_length_error", dec(t, n + 1) == FORGE_FORM_ERR_LENGTH || dec(t, n + 1) == FORGE_FORM_ERR_TRUNCATED); } |
| 244 | { /* tag order: swap fields 1 and 2 */ |
| 245 | uint8_t t[300]; memcpy(t, ok, n); memcpy(t + 12, ok + 24, 12); memcpy(t + 24, ok + 12, 12); |
| 246 | check("neg_tag_order_refused", dec(t, n) == FORGE_FORM_ERR_ORDER); } |
| 247 | { /* duplicate tag: field 5 repeated as tag 5 again */ |
| 248 | uint8_t t[300]; memcpy(t, ok, n); memcpy(t + n, ok + n - 12, 12); t[8] = (uint8_t)(n - 12 + 12); |
| 249 | check("neg_duplicate_tag_refused", dec(t, n + 12) == FORGE_FORM_ERR_ORDER); } |
| 250 | { /* unknown tag 6 appended: strict refuses, lenient skips */ |
| 251 | uint8_t t[300]; ForgeRealizationForm g; memcpy(t, ok, n); |
| 252 | 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); |
| 253 | t[8] = (uint8_t)(n - 12 + 12); |
| 254 | check("neg_unknown_tag_strict_refused", dec(t, n + 12) == FORGE_FORM_ERR_UNKNOWN_TAG); |
| 255 | check("neg_unknown_tag_lenient_skipped", forge_form_decode(t, n + 12, 0, &g) == FORGE_FORM_OK && |
| 256 | memcmp(&g, &f, sizeof f) == 0); } |
| 257 | { /* wrong width: tag 3 with len 8 */ |
| 258 | uint8_t t[300]; memcpy(t, ok, n); t[12 + 24 + 4] = 8; |
| 259 | check("neg_wrong_width_refused", dec(t, n) == FORGE_FORM_ERR_WIDTH || dec(t, n) == FORGE_FORM_ERR_TRUNCATED); } |
| 260 | { /* missing tag: drop field 5 */ |
| 261 | uint8_t t[300]; memcpy(t, ok, n - 12); t[8] = (uint8_t)(n - 12 - 12); |
| 262 | check("neg_missing_field_refused", dec(t, n - 12) == FORGE_FORM_ERR_MISSING); } |
| 263 | { ForgeRealizationForm g = f; g.form_kind = FORGE_FORM_ENERGY_FUNCTIONAL; uint8_t t[256]; |
| 264 | check("neg_energy_functional_specified_not_realizable", |
| 265 | forge_form_encode(&g, t, sizeof t, &n) == FORGE_FORM_ERR_UNSUPPORTED_FORM); |
| 266 | g.form_kind = FORGE_FORM_STOCHASTIC_DISTRIBUTION; |
| 267 | check("neg_stochastic_distribution_reserved_refused", |
| 268 | forge_form_encode(&g, t, sizeof t, &n) == FORGE_FORM_ERR_UNSUPPORTED_FORM); |
| 269 | g.form_kind = 9; |
| 270 | check("neg_unknown_form_kind_refused", forge_form_encode(&g, t, sizeof t, &n) == FORGE_FORM_ERR_VALUE); |
| 271 | g = f; g.scalar_domain = 7; |
| 272 | check("neg_unknown_domain_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); |
| 273 | g = f; g.operator_rows = 0; |
| 274 | check("neg_zero_rows_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); |
| 275 | g = f; g.operator_cols = FORGE_FORM_MAX_DIM + 1; |
| 276 | check("neg_cols_over_max_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); |
| 277 | g = f; g.accumulation = 2; |
| 278 | check("neg_unknown_accumulation_refused", forge_form_validate(&g) == FORGE_FORM_ERR_VALUE); } |
| 279 | { /* decoded wire with a reserved kind is refused too (not only at encode) */ |
| 280 | uint8_t t[300]; memcpy(t, ok, n); t[20] = FORGE_FORM_ENERGY_FUNCTIONAL; |
| 281 | check("neg_wire_energy_functional_refused", dec(t, n) == FORGE_FORM_ERR_UNSUPPORTED_FORM); } |
| 282 | forge_form_encode(&f, ok, sizeof ok, &n); |
| 283 | { |
| 284 | /* single-byte mutation sweep: every mutant is refused, or decodes to a |
| 285 | * form whose canonical re-encoding is byte-identical to the mutant and |
| 286 | * whose digest differs from the original (canonical uniqueness) */ |
| 287 | uint8_t d0[32]; |
| 288 | int bad = 0, accepted = 0; |
| 289 | forge_form_digest(&f, d0); |
| 290 | for (size_t i = 0; i < n; i++) { |
| 291 | for (int bit = 0; bit < 8; bit++) { |
| 292 | ForgeRealizationForm g; |
| 293 | uint8_t t[256], again[256], d1[32]; |
| 294 | size_t m2 = 0; |
| 295 | memcpy(t, ok, n); |
| 296 | t[i] ^= (uint8_t)(1u << bit); |
| 297 | if (forge_form_decode(t, n, 1, &g) != FORGE_FORM_OK) continue; |
| 298 | accepted++; |
| 299 | if (forge_form_encode(&g, again, sizeof again, &m2) != FORGE_FORM_OK || m2 != n || |
| 300 | memcmp(again, t, n) != 0) |
| 301 | bad++; |
| 302 | forge_form_digest(&g, d1); |
| 303 | if (memcmp(d0, d1, 32) == 0) bad++; |
| 304 | } |
| 305 | } |
| 306 | if (!g_quiet) printf("INFO mutation sweep: %zu bits, %d accepted (all canonical), %d bad\n", n * 8, accepted, bad); |
| 307 | check("neg_single_byte_mutants_canonical_or_refused", bad == 0); |
| 308 | check("neg_some_value_mutants_are_valid_other_forms", accepted > 0); |
| 309 | } |
| 310 | { /* a record with a zero digest or a non-simulated provenance is refused */ |
| 311 | ForgeFormRealizationRecord r; uint8_t t[512]; |
| 312 | memset(&r, 0x5a, sizeof r); |
| 313 | r.provenance_class = FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; r.decision = FORGE_FORM_DECISION_PUBLISHED; |
| 314 | check("record_encode_ok", forge_form_encode_record(&r, t, sizeof t, &n) == FORGE_FORM_OK); |
| 315 | r.provenance_class = FORGE_V2_PROVENANCE_PHYSICAL; |
| 316 | check("neg_record_physical_provenance_refused", forge_form_encode_record(&r, t, sizeof t, &n) == FORGE_FORM_ERR_VALUE); |
| 317 | r.provenance_class = FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT; memset(r.form_digest, 0, 32); |
| 318 | check("neg_record_zero_form_digest_refused", forge_form_encode_record(&r, t, sizeof t, &n) == FORGE_FORM_ERR_VALUE); } |
| 319 | } |
| 320 | |
| 321 | /* ---------------------------------------------------------------- */ |
| 322 | /* 3. contract mapping and identity separation */ |
| 323 | /* ---------------------------------------------------------------- */ |
| 324 | |
| 325 | static void contract_mapping(void) |
| 326 | { |
| 327 | ForgeSemanticResultContract c, c2; |
| 328 | ForgeRealizationForm f, f2; |
| 329 | uint8_t cd[32], cd2[32], fd[32], fd2[32], cbytes[1024]; |
| 330 | size_t n = 0; |
| 331 | fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); |
| 332 | check("map_matvec_u64_to_linear_operator", forge_form_from_contract(&c, &f) == FORGE_FORM_OK && |
| 333 | f.form_kind == FORGE_FORM_LINEAR_OPERATOR && |
| 334 | f.scalar_domain == FORGE_FORM_DOMAIN_INT_WRAP_U64 && |
| 335 | f.operator_rows == 8 && f.operator_cols == 8); |
| 336 | forge_v2_digest_contract(&c, cd); |
| 337 | forge_form_digest(&f, fd); |
| 338 | /* the form digest is not an input of the contract digest */ |
| 339 | forge_v2_encode_contract(&c, cbytes, sizeof cbytes, &n); |
| 340 | { int found = 0; for (size_t i = 0; i + 32 <= n; i++) if (memcmp(cbytes + i, fd, 32) == 0) found = 1; |
| 341 | check("form_digest_not_in_contract_bytes", !found); } |
| 342 | forge_v2_digest_contract(&c, cd2); |
| 343 | check("contract_digest_unchanged_by_form_derivation", memcmp(cd, cd2, 32) == 0); |
| 344 | /* a weaker contract (bounded) on the same operation maps to the SAME form */ |
| 345 | fixture_contract(&c2, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); |
| 346 | forge_v2_digest_contract(&c2, cd2); |
| 347 | check("bounded_contract_has_different_contract_digest", memcmp(cd, cd2, 32) != 0); |
| 348 | check("bounded_contract_maps_to_same_form", forge_form_from_contract(&c2, &f2) == FORGE_FORM_OK && |
| 349 | forge_form_digest(&f2, fd2) == FORGE_FORM_OK && |
| 350 | memcmp(fd, fd2, 32) == 0); |
| 351 | /* a different oracle domain maps to a different form */ |
| 352 | fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_I32, 0, 8, 8); |
| 353 | check("i32_oracle_maps_to_u32_wrap_form", forge_form_from_contract(&c2, &f2) == FORGE_FORM_OK && |
| 354 | f2.scalar_domain == FORGE_FORM_DOMAIN_INT_WRAP_U32 && |
| 355 | forge_form_digest(&f2, fd2) == FORGE_FORM_OK && |
| 356 | memcmp(fd, fd2, 32) != 0); |
| 357 | /* unmappable contracts */ |
| 358 | fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); |
| 359 | c2.operation_family = FORGE_V2_OP_MATMUL; |
| 360 | check("map_non_matvec_refused", forge_form_from_contract(&c2, &f2) == FORGE_FORM_ERR_CONTRACT); |
| 361 | fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); |
| 362 | c2.shape_rank = 1; c2.shape_dim[1] = 0; |
| 363 | check("map_rank1_refused", forge_form_from_contract(&c2, &f2) == FORGE_FORM_ERR_CONTRACT); |
| 364 | fixture_contract(&c2, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); |
| 365 | memset(c2.reference_oracle_digest, 0x77, 32); |
| 366 | check("map_unknown_oracle_refused", forge_form_from_contract(&c2, &f2) == FORGE_FORM_ERR_CONTRACT); |
| 367 | /* the form carries no substrate: two lowerings share one form digest */ |
| 368 | { |
| 369 | ForgeFormLowering dl, al; |
| 370 | ForgeAnalogSimModel ideal = { 0 }; |
| 371 | ForgeCalibrationArtifact cal; |
| 372 | fixture_cal(&cal); |
| 373 | check("lower_digital_ok", forge_form_lower_digital(&f, &dl) == FORGE_FORM_OK); |
| 374 | check("lower_analog_sim_ok", forge_form_lower_analog_sim(&f, &ideal, &ideal, &cal, &al) == FORGE_FORM_OK); |
| 375 | check("lowerings_share_form_digest", memcmp(dl.form_digest, al.form_digest, 32) == 0 && |
| 376 | memcmp(dl.form_digest, fd, 32) == 0); |
| 377 | check("lowerings_differ_in_substrate_and_lowering_digest", |
| 378 | memcmp(dl.substrate_digest, al.substrate_digest, 32) != 0 && |
| 379 | memcmp(dl.lowering_digest, al.lowering_digest, 32) != 0); |
| 380 | { uint8_t ld[32]; |
| 381 | forge_form_lowering_digest(fd, dl.code_digest, ld); |
| 382 | check("lowering_digest_is_H_form_code", memcmp(ld, dl.lowering_digest, 32) == 0); } |
| 383 | check("lowerings_substrate_classes_digital_vs_analog", |
| 384 | dl.substrate.substrate_class == FORGE_V2_SUBSTRATE_DIGITAL_CPU && |
| 385 | al.substrate.substrate_class == FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* ---------------------------------------------------------------- */ |
| 390 | /* 4. two realizations of one contract */ |
| 391 | /* ---------------------------------------------------------------- */ |
| 392 | |
| 393 | static uint8_t g_proof_contract[32], g_proof_form[32], g_proof_rid_d[32], g_proof_rid_a[32], |
| 394 | g_proof_ev_d[32], g_proof_ev_a[32]; |
| 395 | static int g_proof_ok; |
| 396 | |
| 397 | static void two_realizations(void) |
| 398 | { |
| 399 | ForgeSemanticResultContract c; |
| 400 | ForgeRealizationForm f; |
| 401 | ForgeFormLowering dl, al, bad; |
| 402 | ForgeFormInput in; |
| 403 | ForgeAnalogSimModel ideal = { 0 }, biased = { 0 }, noisy = { 0 }; |
| 404 | ForgeCalibrationArtifact cal; |
| 405 | const ForgeFormOutcome *od, *oa; |
| 406 | size_t nel = 0; |
| 407 | uint8_t cd[32]; |
| 408 | |
| 409 | fixture_cal(&cal); |
| 410 | biased.bias_lsb = 2; |
| 411 | noisy.noise_peak_lsb = 2; noisy.noise_seed = 42; |
| 412 | |
| 413 | /* EXACT contract, ideal simulated analog: both realizations publish */ |
| 414 | fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); |
| 415 | fixture_input(&in, 8, 8); |
| 416 | forge_form_from_contract(&c, &f); |
| 417 | forge_form_lower_digital(&f, &dl); |
| 418 | forge_form_lower_analog_sim(&f, &ideal, &ideal, &cal, &al); |
| 419 | { const ForgeFormLowering *set[2] = { &al, &dl }; |
| 420 | check("admit_two_lowerings_with_digital", forge_form_admit(set, 2, &c, &in, &nel) == FORGE_FORM_OK && nel == 2); |
| 421 | check("admit_analog_only_refused_no_fallback", forge_form_admit(set, 1, &c, &in, &nel) == FORGE_FORM_ERR_NO_FALLBACK && nel == 1); } |
| 422 | od = run("exact_digital_reference_published", &f, &c, &dl, &in); |
| 423 | check("exact_digital_published", od->status == FORGE_FORM_PUBLISHED && od->published && od->error_abs == 0); |
| 424 | memcpy(g_proof_rid_d, od->evidence.realization_identity, 32); |
| 425 | memcpy(g_proof_ev_d, od->evidence_digest, 32); |
| 426 | { uint64_t ref[64]; forge_asim_oracle_exact_u64(in.A, in.x, ref, 8, 8); |
| 427 | check("exact_digital_output_equals_oracle", memcmp(od->y, ref, 8 * sizeof ref[0]) == 0); } |
| 428 | { uint8_t out_d[32]; memcpy(out_d, od->evidence.output_identity, 32); |
| 429 | oa = run("exact_analog_sim_ideal_published", &f, &c, &al, &in); |
| 430 | check("exact_analog_published", oa->status == FORGE_FORM_PUBLISHED && oa->published && oa->error_abs == 0); |
| 431 | check("exact_same_output_identity", memcmp(out_d, oa->evidence.output_identity, 32) == 0); } |
| 432 | memcpy(g_proof_rid_a, oa->evidence.realization_identity, 32); |
| 433 | memcpy(g_proof_ev_a, oa->evidence_digest, 32); |
| 434 | forge_v2_digest_contract(&c, cd); |
| 435 | memcpy(g_proof_contract, cd, 32); |
| 436 | memcpy(g_proof_form, dl.form_digest, 32); |
| 437 | 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 && |
| 438 | memcmp(od->evidence.semantic_contract_digest, cd, 32) == 0 && |
| 439 | memcmp(oa->evidence.semantic_contract_digest, cd, 32) == 0 && |
| 440 | memcmp(od->record.form_digest, oa->record.form_digest, 32) == 0 && |
| 441 | memcmp(od->record.lowering_digest, oa->record.lowering_digest, 32) != 0 && |
| 442 | memcmp(od->record.substrate_digest, oa->record.substrate_digest, 32) != 0 && |
| 443 | memcmp(od->evidence.substrate_identity, oa->evidence.substrate_identity, 32) != 0 && |
| 444 | od->evidence.has_calibration == 0 && oa->evidence.has_calibration == 1; |
| 445 | check("proof_same_contract_digest_in_both_realizations", |
| 446 | memcmp(od->evidence.semantic_contract_digest, oa->evidence.semantic_contract_digest, 32) == 0); |
| 447 | check("proof_same_form_digest_in_both_records", memcmp(od->record.form_digest, oa->record.form_digest, 32) == 0); |
| 448 | check("proof_different_realization_identities", memcmp(g_proof_rid_d, g_proof_rid_a, 32) != 0); |
| 449 | check("proof_different_evidence_identities", memcmp(g_proof_ev_d, g_proof_ev_a, 32) != 0); |
| 450 | check("proof_different_record_digests", memcmp(od->record_digest, oa->record_digest, 32) != 0); |
| 451 | check("proof_two_realizations_same_contract", g_proof_ok); |
| 452 | check("proof_both_simulated_development", od->evidence.provenance_class == forge_form_provenance() && |
| 453 | oa->evidence.provenance_class == forge_form_provenance() && |
| 454 | forge_form_provenance() == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT); |
| 455 | |
| 456 | /* BOUNDED_DETERMINISTIC: a biased simulated analog publishes a DIFFERENT |
| 457 | * output that still satisfies the same contract as the digital reference */ |
| 458 | fixture_contract(&c, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); |
| 459 | forge_form_from_contract(&c, &f); |
| 460 | forge_form_lower_digital(&f, &dl); |
| 461 | forge_form_lower_analog_sim(&f, &biased, &biased, &cal, &al); |
| 462 | od = run("bounded_digital_reference_published", &f, &c, &dl, &in); |
| 463 | check("bounded_digital_zero_error", od->status == FORGE_FORM_PUBLISHED && od->error_abs == 0); |
| 464 | { uint8_t out_d[32]; memcpy(out_d, od->evidence.output_identity, 32); |
| 465 | oa = run("bounded_analog_sim_bias2_published", &f, &c, &al, &in); |
| 466 | check("bounded_analog_published_with_error_2", oa->status == FORGE_FORM_PUBLISHED && oa->error_abs == 2 && |
| 467 | oa->runs_within_bound == oa->runs); |
| 468 | check("bounded_outputs_differ_same_contract", memcmp(out_d, oa->evidence.output_identity, 32) != 0 && |
| 469 | memcmp(od->evidence.semantic_contract_digest, |
| 470 | oa->evidence.semantic_contract_digest, 32) == 0); } |
| 471 | |
| 472 | /* over the bound: declared bias 2 (eligible) but actual bias 5: analog |
| 473 | * refused with evidence, digital answers */ |
| 474 | { ForgeAnalogSimModel worse = biased; worse.bias_lsb = 5; |
| 475 | forge_form_lower_analog_sim(&f, &biased, &worse, &cal, &al); } |
| 476 | oa = run("bounded_analog_sim_bias5_refused", &f, &c, &al, &in); |
| 477 | check("over_bound_refused_error_bound", oa->status == FORGE_FORM_REFUSED && !oa->published && |
| 478 | oa->refuse_reason == FORGE_ASIM_REFUSE_ERROR_BOUND && oa->ran && |
| 479 | oa->record.decision == FORGE_FORM_DECISION_REFUSED); |
| 480 | { int zero = 1; for (int i = 0; i < 64; i++) if (oa->y[i]) zero = 0; check("refused_publishes_nothing", zero); } |
| 481 | od = run("bounded_digital_fallback_published", &f, &c, &dl, &in); |
| 482 | check("fallback_digital_published_same_contract", od->status == FORGE_FORM_PUBLISHED && |
| 483 | memcmp(od->evidence.semantic_contract_digest, |
| 484 | oa->evidence.semantic_contract_digest, 32) == 0); |
| 485 | |
| 486 | /* declared ideal, actually biased: EXACT refused even if outputs matched */ |
| 487 | fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 8, 8); |
| 488 | forge_form_from_contract(&c, &f); |
| 489 | forge_form_lower_analog_sim(&f, &ideal, &noisy, &cal, &al); |
| 490 | oa = run("exact_analog_sim_hidden_noise_refused", &f, &c, &al, &in); |
| 491 | check("exact_hidden_noise_refused", oa->status == FORGE_FORM_REFUSED && |
| 492 | oa->refuse_reason == FORGE_ASIM_REFUSE_EXACT_MISMATCH); |
| 493 | |
| 494 | /* BOUNDED_STOCHASTIC with noise within bound publishes */ |
| 495 | fixture_contract(&c, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); |
| 496 | forge_form_from_contract(&c, &f); |
| 497 | forge_form_lower_analog_sim(&f, &noisy, &noisy, &cal, &al); |
| 498 | oa = run("stochastic_analog_sim_noise2_published", &f, &c, &al, &in); |
| 499 | check("stochastic_published_within_confidence", oa->status == FORGE_FORM_PUBLISHED && oa->runs == 16 && |
| 500 | oa->runs_within_bound == 16); |
| 501 | forge_form_lower_digital(&f, &dl); |
| 502 | od = run("stochastic_digital_reference_published", &f, &c, &dl, &in); |
| 503 | check("stochastic_digital_published", od->status == FORGE_FORM_PUBLISHED && od->runs == 16); |
| 504 | |
| 505 | /* stale calibration: ineligible, never runs, no evidence */ |
| 506 | fixture_contract(&c, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 3, 8, 8); |
| 507 | forge_form_from_contract(&c, &f); |
| 508 | biased.bias_lsb = 2; |
| 509 | forge_form_lower_analog_sim(&f, &biased, &biased, &cal, &al); |
| 510 | in.now.execution_epoch_s = EPOCH0 + WINDOW + 1; |
| 511 | oa = run("stale_calibration_ineligible", &f, &c, &al, &in); |
| 512 | check("stale_cal_not_run", oa->status == FORGE_FORM_REFUSED && oa->refuse_reason == FORGE_ASIM_REFUSE_INELIGIBLE && |
| 513 | !oa->ran && oa->eligibility_rc == FORGE_V2_ERR_CAL_STALE); |
| 514 | in.now.execution_epoch_s = EPOCH0 + 3600; |
| 515 | |
| 516 | /* the digital reference is also eligible with a stale analog calibration (fallback stays) */ |
| 517 | forge_form_lower_digital(&f, &dl); |
| 518 | od = run("digital_reference_unaffected_by_analog_calibration", &f, &c, &dl, &in); |
| 519 | check("digital_published_regardless", od->status == FORGE_FORM_PUBLISHED); |
| 520 | |
| 521 | /* form / contract mismatch is refused before anything runs */ |
| 522 | { ForgeRealizationForm wrong = f; ForgeFormOutcome o; wrong.operator_rows = 4; |
| 523 | forge_form_lower_digital(&wrong, &bad); |
| 524 | check("mismatched_form_refused_err_contract", |
| 525 | forge_form_realize(&wrong, &c, &bad, &in, &o) == FORGE_FORM_ERR_CONTRACT && !o.ran); |
| 526 | check("lowering_of_other_form_refused", |
| 527 | forge_form_realize(&f, &c, &bad, &in, &o) == FORGE_FORM_ERR_CONTRACT && !o.ran); } |
| 528 | /* input outside the lowering's declared range */ |
| 529 | { ForgeFormOutcome o; uint64_t saved = g_A[0]; g_A[0] = 0x1FF; |
| 530 | check("input_out_of_range_refused", forge_form_realize(&f, &c, &dl, &in, &o) == FORGE_FORM_ERR_INPUT_RANGE); |
| 531 | g_A[0] = saved; } |
| 532 | /* i32 oracle domain through both lowerings */ |
| 533 | fixture_contract(&c, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_I32, 0, 5, 7); |
| 534 | fixture_input(&in, 5, 7); |
| 535 | forge_form_from_contract(&c, &f); |
| 536 | forge_form_lower_digital(&f, &dl); |
| 537 | forge_form_lower_analog_sim(&f, &ideal, &ideal, &cal, &al); |
| 538 | od = run("i32_digital_reference_published", &f, &c, &dl, &in); |
| 539 | oa = run("i32_analog_sim_ideal_published", &f, &c, &al, &in); |
| 540 | check("i32_both_published_same_contract_different_realizations", |
| 541 | od->status == FORGE_FORM_PUBLISHED && oa->status == FORGE_FORM_PUBLISHED && |
| 542 | memcmp(od->evidence.semantic_contract_digest, oa->evidence.semantic_contract_digest, 32) == 0 && |
| 543 | memcmp(od->evidence.realization_identity, oa->evidence.realization_identity, 32) != 0); |
| 544 | } |
| 545 | |
| 546 | /* ---------------------------------------------------------------- */ |
| 547 | /* receipt */ |
| 548 | /* ---------------------------------------------------------------- */ |
| 549 | |
| 550 | static void print_receipt(void) |
| 551 | { |
| 552 | printf("{\n"); |
| 553 | printf(" \"gate\": \"FORM0_LINEAR_OPERATOR_HOST\",\n"); |
| 554 | printf(" \"workstream\": \"ARCH-0018 / ARCH-0019 extension; ADR 0034 Physical-Form Realization IR (PROPOSED)\",\n"); |
| 555 | printf(" \"provenance_class\": \"SIMULATED_DEVELOPMENT\",\n"); |
| 556 | printf(" \"note\": \"Host-only reference. No device acted. The digital reference ran on the host CPU as a " |
| 557 | "development fixture; the analog lowering is the AR2 software model. Errors are simulated or host " |
| 558 | "errors against the digital oracle.\",\n"); |
| 559 | printf(" \"status\": \"%s\",\n", g_fail == 0 ? "PASS" : "FAIL"); |
| 560 | printf(" \"checks_passed\": %d,\n", g_pass); |
| 561 | printf(" \"checks_failed\": %d,\n", g_fail); |
| 562 | printf(" \"evidence_records\": %d,\n", g_records); |
| 563 | printf(" \"evidence_records_simulated_development\": %d,\n", g_records_sim); |
| 564 | printf(" \"proof_two_realizations_same_contract\": \"%s\",\n", g_proof_ok ? "PASS" : "FAIL"); |
| 565 | printf(" \"proof\": {\"contract_digest\": \""); hex(g_proof_contract); |
| 566 | printf("\", \"form_digest\": \""); hex(g_proof_form); |
| 567 | printf("\", \"digital_reference\": {\"realization_identity\": \""); hex(g_proof_rid_d); |
| 568 | printf("\", \"evidence_digest\": \""); hex(g_proof_ev_d); |
| 569 | printf("\"}, \"analog_sim\": {\"realization_identity\": \""); hex(g_proof_rid_a); |
| 570 | printf("\", \"evidence_digest\": \""); hex(g_proof_ev_a); |
| 571 | printf("\"}},\n"); |
| 572 | printf(" \"cases\": [\n"); |
| 573 | for (int i = 0; i < g_ncases; i++) { |
| 574 | const CaseRow *r = &g_cases[i]; |
| 575 | printf(" {\"name\": \"%s\", \"backend\": \"%s\", \"contract_kind\": \"%s\", \"outcome\": \"%s\", " |
| 576 | "\"refusal\": \"%s\", \"ran\": %u, \"runs\": %u, \"runs_within_bound\": %u, " |
| 577 | "\"error_vs_oracle_abs\": %llu, ", |
| 578 | r->name, backend_name(r->backend), kind_name(r->contract_kind), status_name(r->status), |
| 579 | forge_asim_refuse_name(r->refuse), r->ran, r->runs, r->within, (unsigned long long)r->error_abs); |
| 580 | printf("\"contract_digest\": \""); hex(r->contract); |
| 581 | printf("\", \"form_digest\": \""); hex(r->form); |
| 582 | printf("\", \"lowering_digest\": \""); hex(r->lowering); |
| 583 | printf("\", \"realization_identity\": \""); if (r->ran) hex(r->realization); |
| 584 | printf("\", \"evidence_digest\": \""); if (r->ran) hex(r->evidence); |
| 585 | printf("\", \"record_digest\": \""); if (r->ran) hex(r->record); |
| 586 | printf("\", \"provenance_class\": \"SIMULATED_DEVELOPMENT\"}%s\n", i + 1 < g_ncases ? "," : ""); |
| 587 | } |
| 588 | printf(" ]\n}\n"); |
| 589 | } |
| 590 | |
| 591 | int main(int argc, char **argv) |
| 592 | { |
| 593 | int receipt = argc > 1 && strcmp(argv[1], "--receipt") == 0; |
| 594 | g_quiet = receipt; |
| 595 | canonical_form(); |
| 596 | negative_decode(); |
| 597 | contract_mapping(); |
| 598 | two_realizations(); |
| 599 | if (receipt) print_receipt(); |
| 600 | else printf("RESULT checks: %d passed, %d failed\n", g_pass, g_fail); |
| 601 | return g_fail == 0 ? 0 : 1; |
| 602 | } |