interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / tests / test_forge_analog_sim.c

684 lines · 32684 bytes Raw Blame History
1/*
2 * tests/test_forge_analog_sim.c -- AR2 gate checks for the simulated analog
3 * provider (forge/analog-sim). Maps 1:1 onto the AR2 table of the analog
4 * realization test plan: exact oracle, tolerance oracle, injected
5 * non-idealities, contract strictness, fallback, provenance.
6 *
7 * test_forge_analog_sim run every check, print PASS/FAIL, exit 0 on all-pass
8 * test_forge_analog_sim --receipt same checks, then print the deterministic JSON receipt
9 *
10 * Every result in this file is SIMULATED_DEVELOPMENT. No device acts.
11 */
12#include <stdio.h>
13#include <string.h>
14
15#include "forge_analog_sim.h"
16
17static int g_pass, g_fail;
18static int g_quiet;
19
20static void check(const char *name, int ok)
21{
22 if (ok) g_pass++; else g_fail++;
23 if (!g_quiet || !ok) fprintf(g_quiet ? stderr : stdout, "%s %s\n", ok ? "PASS" : "FAIL", name);
24}
25
26/* ---- receipt table ---- */
27
28#define MAX_CASES 128
29typedef struct {
30 const char *name;
31 uint32_t contract_kind, oracle;
32 uint64_t contract_bound;
33 int status;
34 uint32_t refuse_reason;
35 int eligibility_rc;
36 uint32_t analog_ran, runs, runs_within;
37 uint64_t error_abs;
38 uint8_t analog_ev[32], final_ev[32];
39 uint32_t final_is_digital;
40} CaseRow;
41static CaseRow g_cases[MAX_CASES];
42static int g_ncases;
43
44/* ---- fixtures ---- */
45
46#define EPOCH0 1790000000ull /* fixed authority-clock time of the fixture calibration */
47#define WINDOW (24ull * 3600) /* calibration validity: one day */
48
49static uint64_t g_A[FORGE_ASIM_MAX_ROWS * FORGE_ASIM_MAX_COLS], g_x[FORGE_ASIM_MAX_COLS];
50
51static uint64_t lcg(uint64_t *s)
52{
53 *s = *s * 6364136223846793005ull + 1442695040888963407ull;
54 return *s >> 33;
55}
56
57static void fill_inputs(uint32_t M, uint32_t N, uint64_t seed)
58{
59 for (size_t k = 0; k < (size_t)M * N; k++) g_A[k] = lcg(&seed) & 0xFF;
60 for (uint32_t j = 0; j < N; j++) g_x[j] = lcg(&seed) & 0xFF;
61}
62
63static void fixture_cal(ForgeCalibrationArtifact *a)
64{
65 memset(a, 0, sizeof *a);
66 forge_asim_substrate_identity(a->substrate_id);
67 forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/calibration/1", 29, a->calibration_id);
68 a->method = FORGE_V2_CAL_SIMULATED_MODEL;
69 a->epoch_s = EPOCH0;
70 a->validity_window_s = WINDOW;
71 a->env_temp_min_mc = 20000;
72 a->env_temp_max_mc = 30000; /* calibration point = 25 C */
73 a->env_supply_min_uv = 900000;
74 a->env_supply_max_uv = 1100000;
75 a->noise_samples = 1024;
76 a->fixture_count = 8;
77 forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/fixtures", 24, a->fixtures_digest);
78 forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/evidence-root", 29, a->evidence_root);
79}
80
81static void fixture_setup(ForgeAsimSetup *st)
82{
83 memset(st, 0, sizeof *st);
84 fixture_cal(&st->cal);
85 forge_v2_sha256((const uint8_t *)"FORGE-ASIM/test/machine", 23, st->machine_identity);
86 st->include_digital = 1;
87}
88
89static void fixture_request(ForgeAsimRequest *rq, uint32_t kind, uint32_t oracle, uint64_t bound, uint32_t M,
90 uint32_t N)
91{
92 memset(rq, 0, sizeof *rq);
93 ForgeSemanticResultContract *c = &rq->contract;
94 c->operation_family = FORGE_V2_OP_MATVEC;
95 forge_asim_oracle_digest(oracle, c->reference_oracle_digest);
96 c->shape_rank = 2; c->shape_dim[0] = M; c->shape_dim[1] = N;
97 c->error_kind = kind;
98 c->error_abs = kind == FORGE_V2_ERROR_EXACT ? 0 : bound;
99 c->error_norm = FORGE_V2_NORM_ELEMENTWISE_MAX;
100 c->confidence_ppm = 1000000;
101 c->min_samples = 4;
102 c->allowed_nondeterminism = FORGE_V2_NONDET_NONE;
103 if (kind == FORGE_V2_ERROR_BOUNDED_DETERMINISTIC) c->allowed_nondeterminism = FORGE_V2_NONDET_ROUNDING;
104 if (kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC) {
105 c->allowed_nondeterminism = FORGE_V2_NONDET_ROUNDING | FORGE_V2_NONDET_STOCHASTIC_NOISE;
106 c->min_samples = 16;
107 }
108 rq->A = g_A; rq->x = g_x; rq->M = M; rq->N = N;
109 rq->now.execution_epoch_s = EPOCH0 + 3600; /* calibration one hour old */
110 rq->now.temperature_mc = 25000;
111 rq->now.supply_uv = 1000000;
112}
113
114static void oracle_u64(const ForgeAsimRequest *rq, uint64_t *ref)
115{
116 forge_asim_oracle_exact_u64(rq->A, rq->x, ref, rq->M, rq->N);
117}
118
119static int y_equals(const ForgeAsimOutcome *o, const uint64_t *ref, uint32_t M)
120{
121 return memcmp(o->y, ref, (size_t)M * sizeof ref[0]) == 0;
122}
123
124static int y_zero(const ForgeAsimOutcome *o)
125{
126 for (uint32_t i = 0; i < FORGE_ASIM_MAX_ROWS; i++) if (o->y[i]) return 0;
127 return 1;
128}
129
130/* Provenance of every evidence record any case produced; strict decode. */
131static int g_evidence_records, g_evidence_simulated, g_evidence_bad;
132
133static void audit_evidence(const ForgeExecutionEvidenceV2 *e)
134{
135 uint8_t buf[4096];
136 size_t n;
137 ForgeExecutionEvidenceV2 back;
138 g_evidence_records++;
139 if (forge_v2_encode_evidence(e, buf, sizeof buf, &n) != FORGE_V2_OK ||
140 forge_v2_decode_evidence(buf, n, 1, &back) != FORGE_V2_OK) {
141 g_evidence_bad++;
142 return;
143 }
144 if (back.provenance_class == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT) g_evidence_simulated++;
145 else g_evidence_bad++;
146}
147
148static ForgeAsimOutcome g_o;
149
150static const ForgeAsimOutcome *run(const char *name, const ForgeAsimSetup *st, const ForgeAsimRequest *rq)
151{
152 forge_asim_execute(st, rq, &g_o);
153 if (g_o.analog_ran) audit_evidence(&g_o.analog_evidence);
154 if (g_o.published) audit_evidence(&g_o.final_evidence);
155 if (g_ncases >= MAX_CASES) check("receipt_case_table_has_room", 0);
156 else {
157 CaseRow *r = &g_cases[g_ncases++];
158 memset(r, 0, sizeof *r);
159 r->name = name;
160 r->contract_kind = rq->contract.error_kind;
161 r->oracle = 0;
162 for (uint32_t o = FORGE_ASIM_ORACLE_EXACT_U64; o <= FORGE_ASIM_ORACLE_I32; o++) {
163 uint8_t d[32];
164 forge_asim_oracle_digest(o, d);
165 if (memcmp(d, rq->contract.reference_oracle_digest, 32) == 0) r->oracle = o;
166 }
167 r->contract_bound = rq->contract.error_abs;
168 r->status = g_o.status;
169 r->refuse_reason = g_o.refuse_reason;
170 r->eligibility_rc = g_o.eligibility_rc;
171 r->analog_ran = g_o.analog_ran;
172 r->runs = g_o.analog_runs;
173 r->runs_within = g_o.analog_runs_within_bound;
174 r->error_abs = g_o.analog_error_abs;
175 if (g_o.analog_ran) memcpy(r->analog_ev, g_o.analog_evidence_digest, 32);
176 if (g_o.published) memcpy(r->final_ev, g_o.final_evidence_digest, 32);
177 r->final_is_digital = g_o.status == FORGE_ASIM_PUBLISHED_FALLBACK;
178 }
179 return &g_o;
180}
181
182/* refused analog, digital answer published under the same contract */
183static int refused_with_fallback(const ForgeAsimOutcome *o, const ForgeAsimRequest *rq, uint32_t reason)
184{
185 uint64_t ref[FORGE_ASIM_MAX_ROWS];
186 uint8_t cd[32];
187 ForgeSubstrateDescriptor d;
188 oracle_u64(rq, ref);
189 forge_v2_digest_contract(&rq->contract, cd);
190 forge_asim_digital_descriptor(&d);
191 return o->status == FORGE_ASIM_PUBLISHED_FALLBACK && o->refuse_reason == reason && o->published == 1 &&
192 y_equals(o, ref, rq->M) && memcmp(o->final_evidence.semantic_contract_digest, cd, 32) == 0 &&
193 memcmp(o->final_evidence.substrate_identity, d.substrate_identity, 32) == 0;
194}
195
196static int published_analog(const ForgeAsimOutcome *o)
197{
198 return o->status == FORGE_ASIM_PUBLISHED_ANALOG && o->refuse_reason == FORGE_ASIM_REFUSE_NONE &&
199 o->published == 1 && o->analog_ran == 1;
200}
201
202/* ---- AR2 checks ---- */
203
204static void exact_oracle(void)
205{
206 static const uint32_t shapes[][2] = { {1, 1}, {7, 13}, {64, 64}, {64, 1}, {1, 64} };
207 int all_ok = 1;
208 for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) {
209 ForgeAsimSetup st; ForgeAsimRequest rq;
210 uint64_t ref[FORGE_ASIM_MAX_ROWS];
211 fixture_setup(&st);
212 fill_inputs(shapes[s][0], shapes[s][1], 100 + s);
213 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, shapes[s][0], shapes[s][1]);
214 const ForgeAsimOutcome *o = run("exact_ideal_sim_vs_u64_oracle", &st, &rq);
215 oracle_u64(&rq, ref);
216 if (!(published_analog(o) && y_equals(o, ref, rq.M) && o->analog_error_abs == 0)) all_ok = 0;
217 }
218 check("exact_ideal_sim_bit_equal_to_u64_oracle_all_shapes", all_ok);
219
220 ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d;
221 fixture_setup(&st);
222 forge_asim_describe(&st.declared, &st.cal, &d);
223 check("ideal_sim_declares_exact_error_kind", d.error_kind == FORGE_V2_ERROR_EXACT && d.error_abs == 0 &&
224 d.substrate_class == FORGE_V2_SUBSTRATE_ANALOG_IN_MEMORY);
225 fill_inputs(16, 16, 7);
226 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 16, 16);
227 const ForgeAsimOutcome *o = run("exact_output_identity_equals_oracle_digest", &st, &rq);
228 ForgeAsimOutcome o1 = *o;
229 const ForgeAsimOutcome *fb;
230 ForgeAsimSetup st2 = st; st2.actual.bias_lsb = 1; /* force a fallback to obtain the oracle digest */
231 st2.declared = st2.actual;
232 ForgeAsimRequest rq2 = rq; rq2.contract.error_kind = FORGE_V2_ERROR_EXACT;
233 fb = run("exact_oracle_digest_via_digital", &st2, &rq2);
234 check("exact_output_identity_equals_oracle_output_identity",
235 published_analog(&o1) && fb->status == FORGE_ASIM_PUBLISHED_FALLBACK &&
236 memcmp(o1.analog_evidence.output_identity, fb->final_evidence.output_identity, 32) == 0);
237
238 /* oracle restatements agree where they must, and wrap where omega wraps */
239 uint32_t a32[4] = { 0xFFFFFFFFu, 2, 3, 4 }, x32[2] = { 2, 1 }, y32[2];
240 forge_asim_oracle_i32(a32, x32, y32, 2, 2);
241 check("oracle_i32_wraps_u32_like_omega", y32[0] == 0u && y32[1] == 10u);
242 uint64_t A64[1] = { 1ull << 63 }, x64[1] = { 2 }, y64[1];
243 forge_asim_oracle_exact_u64(A64, x64, y64, 1, 1);
244 check("oracle_u64_wraps_u64_like_omega", y64[0] == 0);
245 uint8_t d1[32], d2[32];
246 check("oracle_digests_distinct_and_unknown_refused",
247 forge_asim_oracle_digest(FORGE_ASIM_ORACLE_EXACT_U64, d1) == 0 &&
248 forge_asim_oracle_digest(FORGE_ASIM_ORACLE_I32, d2) == 0 && memcmp(d1, d2, 32) != 0 &&
249 forge_asim_oracle_digest(99, d1) == FORGE_V2_ERR_ARG);
250}
251
252static void tolerance_oracle(void)
253{
254 ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d;
255 /* deterministic non-idealities against the i32 oracle */
256 fixture_setup(&st);
257 st.declared.quant_step_lsb = 8;
258 st.declared.bias_lsb = 3;
259 st.declared.drift_lsb_per_hour = 1;
260 st.declared.temp_lsb_per_k = 1;
261 st.actual = st.declared;
262 forge_asim_describe(&st.declared, &st.cal, &d);
263 fill_inputs(32, 32, 11);
264 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, d.error_abs, 32, 32);
265 rq.now.temperature_mc = 28000;
266 const ForgeAsimOutcome *o = run("tolerance_bounded_deterministic_i32", &st, &rq);
267 check("tolerance_bounded_deterministic_vs_i32_oracle_within_declared_bound",
268 d.error_kind == FORGE_V2_ERROR_BOUNDED_DETERMINISTIC && d.error_abs == 4 + 3 + 24 + 5 &&
269 published_analog(o) && o->analog_error_abs > 0 && o->analog_error_abs <= d.error_abs);
270
271 /* stochastic: noise, many runs, confidence counted */
272 fixture_setup(&st);
273 st.declared.noise_peak_lsb = 6;
274 st.declared.noise_seed = 0xA2A2;
275 st.declared.bias_lsb = -2;
276 st.actual = st.declared;
277 forge_asim_describe(&st.declared, &st.cal, &d);
278 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_I32, d.error_abs, 32, 32);
279 o = run("tolerance_bounded_stochastic_i32", &st, &rq);
280 check("tolerance_bounded_stochastic_vs_i32_oracle_all_runs_within_bound",
281 d.error_kind == FORGE_V2_ERROR_BOUNDED_STOCHASTIC && published_analog(o) && o->analog_runs == 16 &&
282 o->analog_runs_within_bound == 16 && o->analog_error_abs > 0 && o->analog_error_abs <= 8);
283
284 /* the bound is error_abs + error_rel_ppb * |ref| / 1e9 per element: an
285 * error above error_abs alone passes only because of the relative part,
286 * and one LSB above the smallest per-element allowance is refused */
287 fixture_setup(&st);
288 st.declared.bias_lsb = 40;
289 st.actual = st.declared;
290 fill_inputs(8, 64, 12);
291 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_EXACT_U64, 40, 8, 64);
292 rq.contract.error_rel_ppb = 10000; /* 10 ppm of the reference */
293 uint64_t ref[FORGE_ASIM_MAX_ROWS], allow_min = UINT64_MAX;
294 oracle_u64(&rq, ref);
295 for (uint32_t i = 0; i < rq.M; i++) {
296 uint64_t a = 40 + ref[i] * rq.contract.error_rel_ppb / 1000000000ull;
297 if (a < allow_min) allow_min = a;
298 }
299 st.actual.bias_lsb = (int64_t)allow_min;
300 o = run("tolerance_rel_part_admits_error_above_abs", &st, &rq);
301 check("tolerance_rel_part_admits_error_above_abs",
302 allow_min > 40 && published_analog(o) && o->analog_error_abs == allow_min);
303 st.actual.bias_lsb = (int64_t)allow_min + 1;
304 o = run("tolerance_one_lsb_over_bound_refused", &st, &rq);
305 check("tolerance_one_lsb_over_bound_refused", refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_ERROR_BOUND) &&
306 o->analog_error_abs == allow_min + 1);
307}
308
309/* One non-ideality: declared model -> contract bound = declared bound.
310 * Within: actual = declared, published. Out: actual worse, refused. */
311static void one_kind(const char *within_name, const char *out_name, const ForgeAnalogSimModel *declared,
312 const ForgeAnalogSimModel *worse, uint32_t kind, uint32_t expect_reason)
313{
314 ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d;
315 fixture_setup(&st);
316 st.declared = *declared;
317 st.actual = *declared;
318 forge_asim_describe(&st.declared, &st.cal, &d);
319 fill_inputs(24, 40, 21);
320 fixture_request(&rq, kind, FORGE_ASIM_ORACLE_I32, d.error_abs, 24, 40);
321 rq.now.temperature_mc = 27000;
322 rq.now.execution_epoch_s = EPOCH0 + 10 * 3600;
323 rq.latency_budget_ns = 1000;
324 const ForgeAsimOutcome *o = run(within_name, &st, &rq);
325 check(within_name, published_analog(o) && o->analog_error_abs <= d.error_abs);
326 st.actual = *worse;
327 o = run(out_name, &st, &rq);
328 check(out_name, refused_with_fallback(o, &rq, expect_reason) && o->analog_ran == 1);
329}
330
331static void injected_nonidealities(void)
332{
333 ForgeAnalogSimModel dec, bad;
334 const uint32_t BD = FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, BS = FORGE_V2_ERROR_BOUNDED_STOCHASTIC;
335
336 memset(&dec, 0, sizeof dec); dec.quant_step_lsb = 4;
337 bad = dec; bad.quant_step_lsb = 64;
338 one_kind("inject_quantization_within_bound_published", "inject_quantization_over_bound_refused", &dec, &bad,
339 BD, FORGE_ASIM_REFUSE_ERROR_BOUND);
340
341 memset(&dec, 0, sizeof dec); dec.noise_peak_lsb = 3; dec.noise_seed = 77;
342 bad = dec; bad.noise_peak_lsb = 300;
343 one_kind("inject_noise_within_bound_published", "inject_noise_over_bound_refused", &dec, &bad, BS,
344 FORGE_ASIM_REFUSE_CONFIDENCE);
345
346 memset(&dec, 0, sizeof dec); dec.bias_lsb = 5;
347 bad = dec; bad.bias_lsb = 9;
348 one_kind("inject_bias_within_bound_published", "inject_bias_over_bound_refused", &dec, &bad, BD,
349 FORGE_ASIM_REFUSE_ERROR_BOUND);
350
351 memset(&dec, 0, sizeof dec); dec.drift_lsb_per_hour = 1; /* window bound 24 LSB; at 10 h, 10 LSB */
352 bad = dec; bad.drift_lsb_per_hour = 10; /* at 10 h, 100 LSB */
353 one_kind("inject_drift_within_bound_published", "inject_drift_over_bound_refused", &dec, &bad, BD,
354 FORGE_ASIM_REFUSE_ERROR_BOUND);
355
356 memset(&dec, 0, sizeof dec); dec.saturation_max = (1ull << 24) - 1; /* never clips these inputs */
357 bad = dec; bad.saturation_max = 1000; /* clips most rows */
358 one_kind("inject_saturation_within_range_published", "inject_saturation_clipping_refused", &dec, &bad, BD,
359 FORGE_ASIM_REFUSE_ERROR_BOUND);
360
361 memset(&dec, 0, sizeof dec); dec.temp_lsb_per_k = 2; /* envelope half width 5 K: bound 10 */
362 bad = dec; bad.temp_lsb_per_k = 20; /* 2 K off: 40 LSB */
363 one_kind("inject_temperature_within_bound_published", "inject_temperature_over_bound_refused", &dec, &bad,
364 BD, FORGE_ASIM_REFUSE_ERROR_BOUND);
365
366 memset(&dec, 0, sizeof dec); dec.bias_lsb = 1; dec.latency_ns = 500;
367 bad = dec; bad.latency_ns = 5000;
368 one_kind("inject_latency_within_budget_published", "inject_latency_over_budget_refused", &dec, &bad, BD,
369 FORGE_ASIM_REFUSE_LATENCY_ACTUAL);
370
371 memset(&dec, 0, sizeof dec);
372 dec.quant_step_lsb = 4; dec.noise_peak_lsb = 2; dec.noise_seed = 5; dec.bias_lsb = -3;
373 dec.drift_lsb_per_hour = 1; dec.temp_lsb_per_k = 1; dec.saturation_max = (1ull << 24) - 1; dec.latency_ns = 400;
374 bad = dec; bad.bias_lsb = -60; bad.noise_peak_lsb = 40;
375 one_kind("inject_combined_within_bound_published", "inject_combined_over_bound_refused", &dec, &bad, BS,
376 FORGE_ASIM_REFUSE_CONFIDENCE);
377
378 /* declared-level refusals (before anything runs) */
379 ForgeAsimSetup st; ForgeAsimRequest rq; ForgeSubstrateDescriptor d;
380 const ForgeAsimOutcome *o;
381 fixture_setup(&st);
382 st.declared.bias_lsb = 2; st.declared.latency_ns = 400; st.actual = st.declared;
383 forge_asim_describe(&st.declared, &st.cal, &d);
384 fill_inputs(8, 8, 31);
385
386 fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs, 8, 8);
387 rq.now.execution_epoch_s = EPOCH0 + WINDOW - 1;
388 o = run("inject_calibration_age_inside_window_published", &st, &rq);
389 check("inject_calibration_age_inside_window_published", published_analog(o));
390 rq.now.execution_epoch_s = EPOCH0 + WINDOW;
391 o = run("inject_calibration_age_stale_refused", &st, &rq);
392 check("inject_calibration_age_stale_refused_ineligible",
393 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) &&
394 o->eligibility_rc == FORGE_V2_ERR_CAL_STALE && o->analog_ran == 0);
395 rq.now.execution_epoch_s = EPOCH0 - 1;
396 o = run("inject_calibration_from_future_refused", &st, &rq);
397 check("inject_calibration_from_future_refused_ineligible",
398 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && o->eligibility_rc == FORGE_V2_ERR_CAL_STALE);
399
400 fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs, 8, 8);
401 rq.now.temperature_mc = 31000;
402 o = run("inject_temperature_outside_envelope_refused", &st, &rq);
403 check("inject_temperature_outside_envelope_refused_ineligible",
404 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) &&
405 o->eligibility_rc == FORGE_V2_ERR_OUT_OF_ENVELOPE && o->analog_ran == 0);
406
407 fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs, 8, 8);
408 rq.latency_budget_ns = 300;
409 o = run("inject_latency_declared_over_budget_refused", &st, &rq);
410 check("inject_latency_declared_over_budget_refused",
411 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_LATENCY_DECLARED) && o->analog_ran == 0);
412
413 fixture_request(&rq, BD, FORGE_ASIM_ORACLE_I32, d.error_abs - 1, 8, 8);
414 o = run("declared_bound_over_contract_refused", &st, &rq);
415 check("declared_bound_over_contract_refused_ineligible",
416 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && o->eligibility_rc == FORGE_V2_ERR_VALUE &&
417 o->analog_ran == 0);
418}
419
420static void strictness(void)
421{
422 static const char *const names[] = {
423 "exact_refuses_declared_quantization", "exact_refuses_declared_noise", "exact_refuses_declared_bias",
424 "exact_refuses_declared_drift", "exact_refuses_declared_temperature", "exact_refuses_declared_saturation"
425 };
426 ForgeAsimSetup st; ForgeAsimRequest rq;
427 int all_ok = 1;
428 fill_inputs(12, 12, 41);
429 for (int k = 0; k < 6; k++) {
430 fixture_setup(&st);
431 switch (k) {
432 case 0: st.declared.quant_step_lsb = 2; break;
433 case 1: st.declared.noise_peak_lsb = 1; break;
434 case 2: st.declared.bias_lsb = 1; break;
435 case 3: st.declared.drift_lsb_per_hour = 1; break;
436 case 4: st.declared.temp_lsb_per_k = 1; break;
437 default: st.declared.saturation_max = (1ull << 24) - 1; break;
438 }
439 st.actual = st.declared;
440 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12);
441 const ForgeAsimOutcome *o = run(names[k], &st, &rq);
442 if (!(refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) &&
443 o->eligibility_rc == FORGE_V2_ERR_VALUE && o->analog_ran == 0))
444 all_ok = 0;
445 }
446 check("exact_contract_refuses_every_declared_nonideality_at_eligibility", all_ok);
447
448 /* a provider that declares itself ideal but is not: caught by the digest compare */
449 fixture_setup(&st);
450 st.actual.bias_lsb = 1;
451 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12);
452 const ForgeAsimOutcome *o = run("exact_false_ideal_claim_caught", &st, &rq);
453 check("exact_false_ideal_claim_caught_by_output_digest",
454 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && o->eligibility_rc == FORGE_V2_OK &&
455 o->analog_ran == 1);
456
457 /* injected quantization that happens to land on the oracle's values
458 * (all outputs even, step 2) still never satisfies EXACT */
459 fixture_setup(&st);
460 st.actual.quant_step_lsb = 2;
461 for (size_t k = 0; k < 12 * 12; k++) g_A[k] &= ~1ull;
462 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12);
463 o = run("exact_refuses_injected_nonideality_even_on_matching_output", &st, &rq);
464 check("exact_refuses_injected_nonideality_even_on_matching_output",
465 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && o->analog_ran == 1 &&
466 o->analog_error_abs == 0);
467 fill_inputs(12, 12, 41);
468
469 /* a stochastic substrate is never offered for a deterministic contract */
470 fixture_setup(&st);
471 st.declared.noise_peak_lsb = 1; st.actual = st.declared;
472 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, 1000, 12, 12);
473 o = run("deterministic_contract_refuses_stochastic_substrate", &st, &rq);
474 check("deterministic_contract_refuses_stochastic_substrate",
475 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_INELIGIBLE) && o->eligibility_rc == FORGE_V2_ERR_VALUE);
476
477 /* declared deterministic, actually noisy: runs disagree, refused */
478 fixture_setup(&st);
479 st.declared.bias_lsb = 1; st.actual = st.declared;
480 st.actual.noise_peak_lsb = 1; st.actual.noise_seed = 3;
481 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, 1, 12, 12);
482 rq.contract.error_abs = 1000;
483 o = run("deterministic_contract_catches_hidden_noise", &st, &rq);
484 check("deterministic_contract_catches_hidden_noise",
485 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_NONDETERMINISM) && o->analog_ran == 1);
486
487 /* deliberately wrong simulated answers are caught under every contract kind */
488 fixture_setup(&st);
489 st.wrong_answer_row = 5; st.wrong_answer_delta = 1;
490 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 12, 12);
491 o = run("wrong_answer_exact_caught", &st, &rq);
492 check("wrong_answer_off_by_one_caught_under_exact",
493 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) && o->analog_error_abs == 1);
494
495 fixture_setup(&st);
496 st.declared.bias_lsb = 2; st.actual = st.declared;
497 st.wrong_answer_row = 12; st.wrong_answer_delta = -500;
498 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_DETERMINISTIC, FORGE_ASIM_ORACLE_I32, 2, 12, 12);
499 o = run("wrong_answer_bounded_deterministic_caught", &st, &rq);
500 check("wrong_answer_caught_under_bounded_deterministic",
501 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_ERROR_BOUND) && o->analog_error_abs >= 498);
502
503 fixture_setup(&st);
504 st.declared.noise_peak_lsb = 2; st.declared.noise_seed = 9; st.actual = st.declared;
505 st.wrong_answer_row = 1; st.wrong_answer_delta = 50;
506 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_I32, 2, 12, 12);
507 rq.contract.confidence_ppm = 500000;
508 o = run("wrong_answer_bounded_stochastic_caught", &st, &rq);
509 check("wrong_answer_caught_under_bounded_stochastic",
510 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_CONFIDENCE) && o->analog_runs_within_bound == 0);
511}
512
513static void fallback(void)
514{
515 ForgeAsimSetup st; ForgeAsimRequest rq;
516 const ForgeAsimOutcome *o;
517 fill_inputs(10, 10, 51);
518
519 fixture_setup(&st);
520 st.include_digital = 0;
521 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 10, 10);
522 o = run("no_digital_fallback_plan_not_admitted", &st, &rq);
523 check("no_digital_fallback_plan_not_admitted_even_when_analog_fits",
524 o->status == FORGE_ASIM_ERR_NO_FALLBACK && o->published == 0 && o->analog_ran == 0 && y_zero(o));
525
526 st.actual.bias_lsb = 7;
527 o = run("no_digital_fallback_refused_nothing_published", &st, &rq);
528 check("no_digital_fallback_refused_result_publishes_nothing",
529 o->status == FORGE_ASIM_ERR_NO_FALLBACK && o->published == 0 && y_zero(o));
530
531 fixture_setup(&st);
532 st.actual.bias_lsb = 7;
533 o = run("fallback_digital_answers_same_contract", &st, &rq);
534 check("fallback_digital_answers_same_contract_with_evidence",
535 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_EXACT_MISMATCH) &&
536 o->final_evidence.has_calibration == 0 && o->final_evidence.measured_error_abs == 0 &&
537 memcmp(o->final_evidence_digest, o->analog_evidence_digest, 32) != 0);
538
539 /* the refused analog run still left evidence of what it did */
540 check("refused_analog_run_still_has_evidence",
541 o->analog_ran == 1 && o->analog_evidence.measured_error_abs == 7 && o->analog_evidence.repeat_passes == 0 &&
542 o->analog_evidence.has_calibration == 1);
543
544 /* the digital fallback meets the same contract's sample count */
545 fixture_setup(&st);
546 st.declared.noise_peak_lsb = 2; st.declared.noise_seed = 4; st.actual = st.declared;
547 st.actual.noise_peak_lsb = 500;
548 fixture_request(&rq, FORGE_V2_ERROR_BOUNDED_STOCHASTIC, FORGE_ASIM_ORACLE_I32, 2, 10, 10);
549 o = run("fallback_meets_stochastic_sample_count", &st, &rq);
550 check("fallback_meets_contract_sample_count",
551 refused_with_fallback(o, &rq, FORGE_ASIM_REFUSE_CONFIDENCE) &&
552 o->final_evidence.repeat_runs == rq.contract.min_samples &&
553 o->final_evidence.repeat_passes == rq.contract.min_samples);
554
555 /* extreme but accepted model magnitudes stay defined (the gate also runs this under UBSan) */
556 {
557 ForgeAnalogSimModel m;
558 uint64_t yy[FORGE_ASIM_MAX_ROWS], lat;
559 memset(&m, 0, sizeof m);
560 m.bias_lsb = (int64_t)1 << 32; m.drift_lsb_per_hour = (int64_t)1 << 32; m.temp_lsb_per_k = (int64_t)1 << 32;
561 m.noise_peak_lsb = UINT32_MAX; m.quant_step_lsb = UINT32_MAX;
562 int r1 = forge_asim_realize(&m, 1, (int64_t)1 << 32, g_A, g_x, 10, 10, 0, UINT64_MAX, INT32_MAX, yy, &lat);
563 m.bias_lsb = -m.bias_lsb; m.drift_lsb_per_hour = -m.drift_lsb_per_hour;
564 int r2 = forge_asim_realize(&m, 0, 0, g_A, g_x, 10, 10, 1, UINT64_MAX, INT32_MIN, yy, &lat);
565 m.bias_lsb = ((int64_t)1 << 32) + 1;
566 int r3 = forge_asim_realize(&m, 0, 0, g_A, g_x, 10, 10, 0, 0, 0, yy, &lat);
567 uint64_t b = forge_asim_model_bound(&m, UINT64_MAX, UINT32_MAX);
568 check("extreme_model_magnitudes_defined_and_over_limit_refused",
569 r1 == FORGE_ASIM_PUBLISHED_ANALOG && r2 == FORGE_ASIM_PUBLISHED_ANALOG && r3 == FORGE_ASIM_ERR_ARG &&
570 b == UINT64_MAX);
571 }
572
573 /* input out of the declared limits is refused outright */
574 g_A[0] = 256;
575 fixture_setup(&st);
576 o = run("input_out_of_declared_range_refused", &st, &rq);
577 check("input_out_of_declared_range_refused", o->status == FORGE_ASIM_ERR_INPUT_RANGE && o->published == 0);
578 g_A[0] = 1;
579}
580
581static void provenance_and_identity(void)
582{
583 ForgeAsimSetup st; ForgeAsimRequest rq;
584 ForgeAsimOutcome a, b;
585 check("provider_provenance_is_simulated_development",
586 forge_asim_provenance() == FORGE_V2_PROVENANCE_SIMULATED_DEVELOPMENT &&
587 forge_asim_provenance() != FORGE_V2_PROVENANCE_PHYSICAL);
588 check("every_evidence_record_simulated_development_strict_roundtrip",
589 g_evidence_records > 0 && g_evidence_bad == 0 && g_evidence_simulated == g_evidence_records);
590
591 /* a gate that requires PHYSICAL refuses every record of this provider */
592 fixture_setup(&st);
593 fill_inputs(9, 9, 61);
594 fixture_request(&rq, FORGE_V2_ERROR_EXACT, FORGE_ASIM_ORACLE_EXACT_U64, 0, 9, 9);
595 forge_asim_execute(&st, &rq, &a);
596 check("physical_requiring_gate_refuses_simulated_result",
597 a.published && a.final_evidence.provenance_class != FORGE_V2_PROVENANCE_PHYSICAL);
598
599 /* determinism: the same inputs give the same evidence digests */
600 forge_asim_execute(&st, &rq, &b);
601 check("same_inputs_same_evidence_digest", memcmp(a.final_evidence_digest, b.final_evidence_digest, 32) == 0);
602
603 /* the calibration is bound into realization identity */
604 st.cal.calibration_id[0] ^= 1;
605 forge_asim_execute(&st, &rq, &b);
606 check("calibration_change_moves_realization_identity",
607 b.published && memcmp(a.final_evidence.realization_identity, b.final_evidence.realization_identity, 32) != 0);
608}
609
610/* ---- receipt ---- */
611
612static void hex(const uint8_t *d)
613{
614 for (int i = 0; i < 32; i++) printf("%02x", d[i]);
615}
616
617static const char *kind_name(uint32_t k)
618{
619 switch (k) {
620 case FORGE_V2_ERROR_EXACT: return "EXACT";
621 case FORGE_V2_ERROR_BOUNDED_DETERMINISTIC: return "BOUNDED_DETERMINISTIC";
622 case FORGE_V2_ERROR_BOUNDED_STOCHASTIC: return "BOUNDED_STOCHASTIC";
623 default: return "OTHER";
624 }
625}
626
627static const char *status_name(int s)
628{
629 switch (s) {
630 case FORGE_ASIM_PUBLISHED_ANALOG: return "published_simulated_analog";
631 case FORGE_ASIM_PUBLISHED_FALLBACK: return "published_digital_fallback";
632 case FORGE_ASIM_ERR_NO_FALLBACK: return "not_admitted_no_digital_fallback";
633 case FORGE_ASIM_ERR_INPUT_RANGE: return "input_out_of_range";
634 default: return "error";
635 }
636}
637
638static void print_receipt(void)
639{
640 printf("{\n");
641 printf(" \"gate\": \"AR2_ANALOG_SIMULATION_PROVIDER\",\n");
642 printf(" \"workstream\": \"ARCH-0018 AR2\",\n");
643 printf(" \"provenance_class\": \"SIMULATED_DEVELOPMENT\",\n");
644 printf(" \"note\": \"Software model of an analog in-memory matvec substrate. No device acted. "
645 "Errors are simulated errors against the digital oracle.\",\n");
646 printf(" \"status\": \"%s\",\n", g_fail == 0 ? "PASS" : "FAIL");
647 printf(" \"checks_passed\": %d,\n", g_pass);
648 printf(" \"checks_failed\": %d,\n", g_fail);
649 printf(" \"evidence_records\": %d,\n", g_evidence_records);
650 printf(" \"evidence_records_simulated_development\": %d,\n", g_evidence_simulated);
651 printf(" \"cases\": [\n");
652 for (int i = 0; i < g_ncases; i++) {
653 const CaseRow *r = &g_cases[i];
654 printf(" {\"name\": \"%s\", \"contract_kind\": \"%s\", \"oracle\": \"%s\", \"contract_error_abs\": %llu, "
655 "\"outcome\": \"%s\", \"analog_refusal\": \"%s\", \"eligibility\": \"%s\", \"analog_ran\": %u, "
656 "\"runs\": %u, \"runs_within_bound\": %u, \"simulated_error_vs_oracle_abs\": %llu, ",
657 r->name, kind_name(r->contract_kind),
658 r->oracle == FORGE_ASIM_ORACLE_I32 ? "omega_matmul_cpu_oracle_i32" : "omega_matvec_reference",
659 (unsigned long long)r->contract_bound, status_name(r->status), forge_asim_refuse_name(r->refuse_reason),
660 forge_v2_strerror(r->eligibility_rc), r->analog_ran, r->runs, r->runs_within,
661 (unsigned long long)r->error_abs);
662 printf("\"analog_evidence_digest\": \"");
663 if (r->analog_ran) hex(r->analog_ev);
664 printf("\", \"published_evidence_digest\": \"");
665 if (r->status == FORGE_ASIM_PUBLISHED_ANALOG || r->status == FORGE_ASIM_PUBLISHED_FALLBACK) hex(r->final_ev);
666 printf("\", \"provenance_class\": \"SIMULATED_DEVELOPMENT\"}%s\n", i + 1 < g_ncases ? "," : "");
667 }
668 printf(" ]\n}\n");
669}
670
671int main(int argc, char **argv)
672{
673 int receipt = argc > 1 && strcmp(argv[1], "--receipt") == 0;
674 g_quiet = receipt;
675 exact_oracle();
676 tolerance_oracle();
677 injected_nonidealities();
678 strictness();
679 fallback();
680 provenance_and_identity();
681 if (receipt) print_receipt();
682 else printf("RESULT checks: %d passed, %d failed\n", g_pass, g_fail);
683 return g_fail == 0 ? 0 : 1;
684}