interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / research / atemporal / at1 / model / at1_result.c

689 lines · 34643 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* AT1_RESULT_V1 writer: splices the oracle's four reference families (charter reading c),
18 * judges the twelve checks of AT1_RESULT_V1 section 4 in exact rational arithmetic on the
19 * written tokens (AT0_RESULT_V2 section 4 comparison forms), and writes the complete file with
20 * verdict_id and evidence_digest (AT1_RESULT_V1 section 6).
21 *
22 * Exactness: every f64 token and every scaled decimal is an exact rational (at1_bn.c); the exact
23 * rational w/N is the ideal clock marginal of check 10. No comparison here uses floating point.
24 * Aggregation inside a check: FAIL if any item fails, else INDETERMINATE if any item is, else
25 * PASS; NOT_EVALUATED when no input remains (per-input NOT_EVALUATED reading). Check 9 is FAIL
26 * exactly when some engine label is UNDEFINED (as the qualified AT-0 judge). */
27#include "at1_model.h"
28
29#include <math.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34/* ---- growable text buffer ----------------------------------------------------------------- */
35typedef struct { char *p; size_t n, cap; } buf_t;
36static void put(buf_t *b, const char *s)
37{
38 size_t l = strlen(s);
39 if (b->n + l + 1 > b->cap) {
40 size_t cap = b->cap ? b->cap : 4096;
41 while (b->n + l + 1 > cap) cap *= 2;
42 b->p = at1_xrealloc(b->p, cap); b->cap = cap;
43 }
44 memcpy(b->p + b->n, s, l); b->n += l; b->p[b->n] = 0;
45}
46static void putn(buf_t *b, const char *s, size_t l)
47{
48 char *t = at1_xmalloc(l + 1); memcpy(t, s, l); t[l] = 0; put(b, t); free(t);
49}
50
51/* ---- judged value ---------------------------------------------------------------------------- */
52enum { JV_VALUE = 0, JV_NONFINITE = 1, JV_UNDEFINED = 2 };
53typedef struct { int kind; double v; bn bN; unsigned bk; char btxt[400]; } jv;
54
55static void jv_init(jv *x) { x->kind = JV_UNDEFINED; x->v = 0; bn_init(&x->bN); x->bk = 0; strcpy(x->btxt, "0@0"); }
56static void jv_free(jv *x) { bn_free(&x->bN); }
57static void jv_from_val(jv *x, const at1_val *v)
58{
59 char t[400];
60 if (v->undefined) { x->kind = JV_UNDEFINED; bn_set_u64(&x->bN, 0); x->bk = 0; return; }
61 /* a value whose bound overflowed is written nonfinite (it forces NONFINITE_VALUE) */
62 x->kind = isfinite(v->value) && isfinite(v->bound) ? JV_VALUE : JV_NONFINITE;
63 x->v = v->value;
64 /* the bound exactly as it is written */
65 at1_bound_format(x->kind == JV_VALUE ? v->bound : 0.0, x->btxt, sizeof x->btxt);
66 strcpy(t, x->btxt);
67 char *at = strchr(t, '@');
68 bn_from_digits(&x->bN, t, (size_t)(at - t));
69 x->bk = (unsigned)atoi(at + 1);
70}
71static void jv_text(const jv *x, char *out, size_t cap)
72{
73 if (x->kind == JV_UNDEFINED) { snprintf(out, cap, "undefined 0@0"); return; }
74 char v[64];
75 if (x->kind == JV_NONFINITE) snprintf(v, sizeof v, "nonfinite"); else at1_f64_format(x->v, v, sizeof v);
76 snprintf(out, cap, "%s %s", v, x->btxt); /* the bound exactly as at1_bound_format wrote it */
77}
78static void jv_exact(const jv *x, bq *val, bq *bound)
79{
80 bq_from_double(val, x->v);
81 bq_from_scaled(bound, &x->bN, x->bk);
82}
83
84/* ---- oracle reference lines ----------------------------------------------------------------- */
85static const char *AXES[3] = { "X", "Y", "Z" };
86static const char *SIGNS[2] = { "PLUS", "MINUS" };
87static const char *STATUS_NAME[3] = { "DEFINED", "UNDEFINED", "INDETERMINATE" };
88
89typedef struct {
90 int M;
91 char **lines; int nlines; /* the spliced lines, verbatim */
92 at1_label_status *status;
93 jv *clock; /* M */
94 jv (*ideal)[6], (*inter)[6]; /* M x 6 */
95} refs_t;
96
97static void refs_free(refs_t *R)
98{
99 for (int i = 0; i < R->nlines; i++) free(R->lines[i]);
100 free(R->lines);
101 if (R->clock) for (int k = 0; k < R->M; k++) jv_free(&R->clock[k]);
102 if (R->ideal) for (int k = 0; k < R->M; k++) for (int s = 0; s < 6; s++) { jv_free(&R->ideal[k][s]); jv_free(&R->inter[k][s]); }
103 free(R->status); free(R->clock); free(R->ideal); free(R->inter);
104}
105
106static int is_hex_lower(const char *s, size_t n)
107{
108 for (size_t i = 0; i < n; i++) if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return 0;
109 return 1;
110}
111static int is_digits(const char *s, size_t n)
112{
113 if (n == 0) return 0;
114 for (size_t i = 0; i < n; i++) if (s[i] < '0' || s[i] > '9') return 0;
115 return 1;
116}
117
118/* "<value> <bound>" -> jv; 0 on a grammar failure. undefined only with 0@0. */
119static int parse_value_bound(const char *v, const char *b, jv *x)
120{
121 size_t lb = strlen(b);
122 const char *at = strchr(b, '@');
123 if (!at) return 0;
124 size_t ln = (size_t)(at - b), lk = lb - ln - 1;
125 if (!is_digits(b, ln) || !is_digits(at + 1, lk) || lk > 2) return 0;
126 if ((ln > 1 && b[0] == '0') || (lk > 1 && at[1] == '0')) return 0;
127 unsigned k = (unsigned)atoi(at + 1);
128 if (k > AT1_SCALED_K_MAX) return 0;
129 if (ln == 1 && b[0] == '0' && k != 0) return 0;
130 if (k != 0 && b[ln - 1] == '0') return 0;
131 if (!bn_from_digits(&x->bN, b, ln)) return 0;
132 x->bk = k;
133 if (strcmp(v, "undefined") == 0) { x->kind = JV_UNDEFINED; return bn_is_zero(&x->bN) && k == 0; }
134 if (strcmp(v, "nonfinite") == 0) { x->kind = JV_NONFINITE; return 1; }
135 if (strlen(v) != 20 || strncmp(v, "f64:", 4) != 0 || !is_hex_lower(v + 4, 16)) return 0;
136 uint64_t bits = 0;
137 for (int i = 4; i < 20; i++) bits = (bits << 4) | (uint64_t)(v[i] <= '9' ? v[i] - '0' : v[i] - 'a' + 10);
138 double d; memcpy(&d, &bits, sizeof d);
139 if (!isfinite(d) || bits == 0x8000000000000000ull) return 0; /* -0.0 is never written */
140 x->kind = JV_VALUE; x->v = d;
141 return 1;
142}
143
144/* split a line into at most cap space-separated tokens (in a scratch copy) */
145static int tokens(char *s, char **t, int cap)
146{
147 int n = 0;
148 for (char *p = s; ; ) {
149 if (n == cap) return cap + 1;
150 t[n++] = p;
151 char *sp = strchr(p, ' ');
152 if (!sp) break;
153 *sp = 0; p = sp + 1;
154 }
155 return n;
156}
157
158static int key_is(const char *line, size_t len, const char *key)
159{
160 size_t k = strlen(key);
161 return len > k && strncmp(line, key, k) == 0 && line[k] == ' ';
162}
163
164
165/* the status rule of section 3, exact, on a written value and its written bound */
166static at1_label_status jv_status(const jv *x, const at1_scaled *tol)
167{
168 bq P, B, T, s; bq_init(&P); bq_init(&B); bq_init(&T); bq_init(&s);
169 bq_from_double(&P, x->v);
170 bq_from_scaled(&B, &x->bN, x->bk);
171 bq_from_scaled(&T, &tol->n, tol->k);
172 at1_label_status st;
173 bq_add(&s, &P, &B);
174 if (bq_cmp(&s, &T) <= 0) st = AT1_LABEL_UNDEFINED;
175 else { bq_sub(&s, &P, &B); st = bq_cmp(&s, &T) > 0 ? AT1_LABEL_DEFINED : AT1_LABEL_INDETERMINATE; }
176 bq_free(&P); bq_free(&B); bq_free(&T); bq_free(&s);
177 return st;
178}
179static at1_status refs_parse(const at1_case *c, const char *text, size_t len, refs_t *R, char *err, size_t cap)
180{
181 const int M = c->label_count;
182 memset(R, 0, sizeof *R);
183 R->M = M;
184 R->status = at1_xcalloc((size_t)M, sizeof *R->status);
185 R->clock = at1_xmalloc((size_t)M * sizeof(jv));
186 R->ideal = at1_xmalloc((size_t)M * sizeof *R->ideal);
187 R->inter = at1_xmalloc((size_t)M * sizeof *R->inter);
188 for (int k = 0; k < M; k++) { jv_init(&R->clock[k]); for (int s = 0; s < 6; s++) { jv_init(&R->ideal[k][s]); jv_init(&R->inter[k][s]); } }
189 R->lines = at1_xmalloc((size_t)(14 * M) * sizeof(char *));
190
191 if (len == 0 || text[len - 1] != '\n') { snprintf(err, cap, "oracle record: not LF-terminated"); return AT1_ERR_ARGUMENT; }
192 /* is there a values block? then take reference lines only from inside it */
193 int has_values = 0;
194 for (size_t i = 0; i < len; ) {
195 const char *e = memchr(text + i, '\n', len - i);
196 size_t l = (size_t)(e - (text + i));
197 if (l == 12 && strncmp(text + i, "begin values", 12) == 0) has_values = 1;
198 i += l + 1;
199 }
200 static const char *fam[4] = { "reference_label", "reference_clock_probability", "reference_ideal", "reference_interacting" };
201 int expect_total = 14 * M, got = 0, inside = !has_values;
202 for (size_t i = 0; i < len; ) {
203 const char *ln = text + i;
204 const char *e = memchr(ln, '\n', len - i);
205 size_t l = (size_t)(e - ln);
206 i += l + 1;
207 if (l == 12 && strncmp(ln, "begin values", 12) == 0) { inside = 1; continue; }
208 if (l == 10 && strncmp(ln, "end values", 10) == 0) { inside = 0; continue; }
209 if (key_is(ln, l, "case_id") && (l != 8 + 64 || strncmp(ln + 8, c->case_id, 64) != 0)) {
210 snprintf(err, cap, "oracle record: case_id differs from the case"); return AT1_ERR_ARGUMENT;
211 }
212 if (key_is(ln, l, "acceptance_id") && (l != 14 + 64 || strncmp(ln + 14, c->acceptance_id, 64) != 0)) {
213 snprintf(err, cap, "oracle record: acceptance_id differs from the case"); return AT1_ERR_ARGUMENT;
214 }
215 if (!inside) continue;
216 int f = -1;
217 for (int q = 0; q < 4; q++) if (key_is(ln, l, fam[q])) f = q;
218 if (f < 0) continue;
219 if (got >= expect_total) { snprintf(err, cap, "oracle record: too many reference lines"); return AT1_ERR_ARGUMENT; }
220 /* expected position */
221 int fam_expected, k, s;
222 if (got < M) { fam_expected = 0; k = got; s = 0; }
223 else if (got < 2 * M) { fam_expected = 1; k = got - M; s = 0; }
224 else if (got < 8 * M) { fam_expected = 2; k = (got - 2 * M) / 6; s = (got - 2 * M) % 6; }
225 else { fam_expected = 3; k = (got - 8 * M) / 6; s = (got - 8 * M) % 6; }
226 char *copy = at1_xmalloc(l + 1); memcpy(copy, ln, l); copy[l] = 0;
227 R->lines[R->nlines++] = copy;
228 char *scratch = at1_xmalloc(l + 1); memcpy(scratch, ln, l); scratch[l] = 0;
229 char *t[8]; int nt = tokens(scratch, t, 7);
230 char kbuf[16]; snprintf(kbuf, sizeof kbuf, "%d", k);
231 int ok = f == fam_expected && nt >= 2 && strcmp(t[1], kbuf) == 0;
232 if (ok && f == 0) {
233 ok = nt == 4 && strcmp(t[2], c->labels[k]) == 0;
234 int st = -1;
235 for (int q = 0; ok && q < 3; q++) if (strcmp(t[3], STATUS_NAME[q]) == 0) st = q;
236 ok = ok && st >= 0;
237 if (ok) R->status[k] = (at1_label_status)st;
238 } else if (ok && f == 1) {
239 ok = nt == 4 && parse_value_bound(t[2], t[3], &R->clock[k]);
240 } else if (ok) {
241 jv *dst = f == 2 ? &R->ideal[k][s] : &R->inter[k][s];
242 ok = nt == 6 && strcmp(t[2], AXES[s / 2]) == 0 && strcmp(t[3], SIGNS[s % 2]) == 0 && parse_value_bound(t[4], t[5], dst);
243 }
244 free(scratch);
245 if (!ok) { snprintf(err, cap, "oracle record: malformed or out-of-order reference line %d", got + 1); return AT1_ERR_ARGUMENT; }
246 got++;
247 }
248 if (got != expect_total) { snprintf(err, cap, "oracle record: %d reference lines, expected %d", got, expect_total); return AT1_ERR_ARGUMENT; }
249 /* undefined placement (section 1): reference_ideal is always a value; reference_interacting
250 * is undefined exactly when that reference_label is not DEFINED; reference_clock_probability
251 * is undefined only in the trivial shape (all M undefined, every reference_label UNDEFINED);
252 * a written clock value must reproduce its reference_label under the status rule. */
253 int n_undef_clock = 0;
254 for (int k = 0; k < M; k++) n_undef_clock += R->clock[k].kind == JV_UNDEFINED;
255 for (int k = 0; k < M; k++) {
256 const char *why = NULL;
257 for (int s = 0; s < 6 && !why; s++) {
258 if (R->ideal[k][s].kind == JV_UNDEFINED) why = "reference_ideal undefined";
259 else if ((R->inter[k][s].kind == JV_UNDEFINED) != (R->status[k] != AT1_LABEL_DEFINED))
260 why = "reference_interacting undefined does not match reference_label";
261 }
262 if (!why && n_undef_clock != 0 && (n_undef_clock != M || R->status[k] != AT1_LABEL_UNDEFINED))
263 why = "reference_clock_probability undefined outside the trivial shape";
264 if (!why && R->clock[k].kind == JV_VALUE && jv_status(&R->clock[k], &c->tol_zero) != R->status[k])
265 why = "reference_label does not follow the status rule on reference_clock_probability";
266 if (why) { snprintf(err, cap, "oracle record: label %d: %s", k, why); return AT1_ERR_ARGUMENT; }
267 }
268 return AT1_OK;
269}
270
271/* ---- provenance ------------------------------------------------------------------------------ */
272static const char *PROV_KEYS[13] = { "source_repo", "source_commit", "source_tree_clean", "contract_commit",
273 "engine_sha256", "oracle_repo", "oracle_commit", "oracle_sha256", "build_cc", "build_flags", "host",
274 "run_started_utc", "run_finished_utc" };
275
276static int valid_repo(const char *s)
277{
278 const char *sl = strchr(s, '/');
279 if (!sl || sl == s || !sl[1] || strchr(sl + 1, '/')) return 0;
280 for (const char *p = s; *p; p++) {
281 if (p == sl) continue;
282 char ch = *p;
283 if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '.' || ch == '-')) return 0;
284 }
285 return 1;
286}
287static int valid_text(const char *s)
288{
289 size_t n = strlen(s);
290 if (n < 1 || n > 200 || s[0] == ' ' || s[n - 1] == ' ') return 0;
291 for (size_t i = 0; i < n; i++) if (s[i] < 0x20 || s[i] > 0x7E) return 0;
292 return 1;
293}
294static int valid_utc(const char *s)
295{
296 if (strlen(s) != 20) return 0;
297 static const char pat[] = "dddd-dd-ddTdd:dd:ddZ";
298 for (int i = 0; i < 20; i++) {
299 if (pat[i] == 'd') { if (s[i] < '0' || s[i] > '9') return 0; }
300 else if (s[i] != pat[i]) return 0;
301 }
302 int mo = (s[5] - '0') * 10 + s[6] - '0', d = (s[8] - '0') * 10 + s[9] - '0';
303 int h = (s[11] - '0') * 10 + s[12] - '0', mi = (s[14] - '0') * 10 + s[15] - '0', se = (s[17] - '0') * 10 + s[18] - '0';
304 return mo >= 1 && mo <= 12 && d >= 1 && d <= 31 && h <= 23 && mi <= 59 && se <= 59;
305}
306static int valid_path(const char *s, size_t n)
307{
308 if (n < 1 || n > 200) return 0;
309 for (size_t i = 0; i < n; i++) if (s[i] < 0x21 || s[i] > 0x7E) return 0;
310 return 1;
311}
312
313/* Validates the provenance lines and returns them normalized in *out (each LF-terminated).
314 * oracle_none: the oracle fields are written as "none" (ERROR results). */
315static at1_status prov_parse(const char *text, size_t len, int oracle_none, buf_t *out, char *err, size_t cap)
316{
317 if (len == 0 || text[len - 1] != '\n') { snprintf(err, cap, "provenance: not LF-terminated"); return AT1_ERR_ARGUMENT; }
318 for (size_t i = 0; i < len; i++) if (text[i] != '\n' && (text[i] < 0x20 || text[i] > 0x7E)) { snprintf(err, cap, "provenance: byte outside 0x20..0x7E"); return AT1_ERR_ARGUMENT; }
319 char *copy = at1_xmalloc(len + 1); memcpy(copy, text, len); copy[len] = 0;
320 char *line = copy; int idx = 0; at1_status st = AT1_OK;
321 char engine_sha[65] = "", oracle_sha[65] = "", prev_path[256] = ""; int have_prev = 0;
322 while (*line && st == AT1_OK) {
323 char *nl = strchr(line, '\n'); *nl = 0;
324 if (idx < 13) {
325 const char *key = PROV_KEYS[idx];
326 size_t kl = strlen(key);
327 if (strncmp(line, key, kl) != 0 || line[kl] != ' ') { snprintf(err, cap, "provenance: line %d must be %s", idx + 1, key); st = AT1_ERR_ARGUMENT; break; }
328 const char *v = line + kl + 1;
329 int ok;
330 int is_oracle_field = idx >= 5 && idx <= 7;
331 if (oracle_none && is_oracle_field) { v = "none"; ok = 1; }
332 else switch (idx) {
333 case 0: case 5: ok = valid_repo(v); break;
334 case 1: case 3: case 6: ok = strlen(v) == 40 && is_hex_lower(v, 40); break;
335 case 2: ok = strcmp(v, "YES") == 0 || strcmp(v, "NO") == 0; break;
336 case 4: case 7: ok = strlen(v) == 64 && is_hex_lower(v, 64); break;
337 case 8: case 9: case 10: ok = valid_text(v); break;
338 default: ok = valid_utc(v); break;
339 }
340 if (!ok) { snprintf(err, cap, "provenance: bad value for %s", key); st = AT1_ERR_ARGUMENT; break; }
341 if (idx == 4) strcpy(engine_sha, v);
342 if (idx == 7 && strcmp(v, "none") != 0) strcpy(oracle_sha, v);
343 if (idx == 8 && strncmp(v, "oracle ", 7) == 0) { snprintf(err, cap, "provenance: build_cc marks an oracle-written record; a candidate result is refused"); st = AT1_ERR_ARGUMENT; break; }
344 put(out, key); put(out, " "); put(out, v); put(out, "\n");
345 } else {
346 /* artifact <path> <digest>, sorted strictly by path */
347 char *sp1 = strncmp(line, "artifact ", 9) == 0 ? line + 9 : NULL;
348 char *sp2 = sp1 ? strchr(sp1, ' ') : NULL;
349 if (!sp2 || !valid_path(sp1, (size_t)(sp2 - sp1)) || strlen(sp2 + 1) != 64 || !is_hex_lower(sp2 + 1, 64) || strchr(sp2 + 1, ' ')) {
350 snprintf(err, cap, "provenance: bad artifact line"); st = AT1_ERR_ARGUMENT; break;
351 }
352 *sp2 = 0;
353 if (have_prev && strcmp(prev_path, sp1) >= 0) { snprintf(err, cap, "provenance: artifact paths not strictly increasing"); st = AT1_ERR_ARGUMENT; break; }
354 strcpy(prev_path, sp1); have_prev = 1;
355 *sp2 = ' ';
356 put(out, line); put(out, "\n");
357 }
358 idx++;
359 line = nl + 1;
360 }
361 if (st == AT1_OK && idx < 13) { snprintf(err, cap, "provenance: %d lines, expected at least 13", idx); st = AT1_ERR_ARGUMENT; }
362 if (st == AT1_OK && oracle_sha[0] && strcmp(engine_sha, oracle_sha) == 0) {
363 snprintf(err, cap, "provenance: engine_sha256 equals oracle_sha256 (oracle-written record); refused"); st = AT1_ERR_ARGUMENT;
364 }
365 free(copy);
366 return st;
367}
368
369/* ---- exact checks ---------------------------------------------------------------------------- */
370enum { C_PASS = 0, C_FAIL = 1, C_INDET = 2, C_NE = 3 };
371static const char *CHECK_STATE[4] = { "PASS", "FAIL", "INDETERMINATE", "NOT_EVALUATED" };
372static const char *CHECK_NAME[12] = { "bound_kind_sufficient", "values_finite", "physical_state_nontrivial",
373 "constraint_residual", "povm_normalization", "clock_probability_sum", "probability_range", "pauli_pair_sum",
374 "conditional_defined", "target_agreement", "oracle_cross_check", "label_status_agreement" };
375/* failure code index (at1_failure_codes order) of each check; check 10 depends on the target */
376enum { K_BOUND = 0, K_COND = 1, K_CON = 2, K_INTER = 3, K_NONFIN = 4, K_ORACLE = 5, K_POVM = 6, K_PREC = 7,
377 K_RANGE = 8, K_SUM = 9, K_SCHRO = 10, K_TRIV = 11 };
378
379typedef struct { int seen, fail, indet; } agg_t;
380static void agg_add(agg_t *g, int r) { g->seen = 1; if (r == C_FAIL) g->fail = 1; else if (r == C_INDET) g->indet = 1; }
381static int agg_res(const agg_t *g) { return !g->seen ? C_NE : g->fail ? C_FAIL : g->indet ? C_INDET : C_PASS; }
382
383/* absolute form: |v| + b <= tol PASS; |v| - b > tol FAIL; else INDETERMINATE */
384static int absform(const bq *v, const bq *b, const bq *tol)
385{
386 bq a, s; bq_init(&a); bq_init(&s);
387 bq_abs(&a, v);
388 bq_add(&s, &a, b);
389 int r;
390 if (bq_cmp(&s, tol) <= 0) r = C_PASS;
391 else { bq_sub(&s, &a, b); r = bq_cmp(&s, tol) > 0 ? C_FAIL : C_INDET; }
392 bq_free(&a); bq_free(&s);
393 return r;
394}
395static int signedform(const bq *v, const bq *b, const bq *tol)
396{
397 bq s; bq_init(&s);
398 bq_add(&s, v, b);
399 int r;
400 if (bq_cmp(&s, tol) <= 0) r = C_PASS;
401 else { bq_sub(&s, v, b); r = bq_cmp(&s, tol) > 0 ? C_FAIL : C_INDET; }
402 bq_free(&s);
403 return r;
404}
405/* compare a - b (bounds summed) */
406static int absdiff(const jv *a, const jv *b, const bq *tol)
407{
408 bq va, ba, vb, bb, d, s; bq_init(&va); bq_init(&ba); bq_init(&vb); bq_init(&bb); bq_init(&d); bq_init(&s);
409 jv_exact(a, &va, &ba); jv_exact(b, &vb, &bb);
410 bq_sub(&d, &va, &vb); bq_add(&s, &ba, &bb);
411 int r = absform(&d, &s, tol);
412 bq_free(&va); bq_free(&ba); bq_free(&vb); bq_free(&bb); bq_free(&d); bq_free(&s);
413 return r;
414}
415
416typedef struct {
417 int M;
418 int trivial, kernel_dim;
419 jv constraint, povm;
420 at1_label_status *status;
421 jv *clock;
422 jv (*pauli)[6];
423} eng_t;
424
425static void judge(const at1_case *c, const eng_t *E, const refs_t *R, int res[12], unsigned *codes)
426{
427 const int M = E->M, triv = E->trivial;
428 bq tol_con, tol_povm, tol_p, tol_s, one, wn, v, b, s, t;
429 bq_init(&tol_con); bq_init(&tol_povm); bq_init(&tol_p); bq_init(&tol_s); bq_init(&one); bq_init(&wn);
430 bq_init(&v); bq_init(&b); bq_init(&s); bq_init(&t);
431 bq_from_scaled(&tol_con, &c->tol_constraint.n, c->tol_constraint.k);
432 bq_from_scaled(&tol_povm, &c->tol_povm.n, c->tol_povm.k);
433 bq_from_scaled(&tol_p, &c->tol_probability.n, c->tol_probability.k);
434 bq_from_scaled(&tol_s, &c->tol_schrodinger.n, c->tol_schrodinger.k);
435 bq_set_i64(&one, 1, 1);
436 { bq n; bq_init(&n); bq_set_i64(&n, c->clock_dim, 1); bq_div(&wn, &c->weight, &n); bq_free(&n); }
437 agg_t g;
438
439 /* 1 */
440 res[0] = AT1_BOUND_ESTIMATED >= c->min_bound_kind ? C_PASS : C_FAIL;
441 /* 2: no nonfinite token anywhere in the values block */
442 int nonfin = E->constraint.kind == JV_NONFINITE || E->povm.kind == JV_NONFINITE;
443 for (int k = 0; k < M; k++) {
444 if (E->clock[k].kind == JV_NONFINITE || R->clock[k].kind == JV_NONFINITE) nonfin = 1;
445 for (int q = 0; q < 6; q++)
446 if (E->pauli[k][q].kind == JV_NONFINITE || R->ideal[k][q].kind == JV_NONFINITE || R->inter[k][q].kind == JV_NONFINITE) nonfin = 1;
447 }
448 res[1] = nonfin ? C_FAIL : C_PASS;
449 /* 3 */
450 res[2] = triv ? C_FAIL : C_PASS;
451 /* 4 */
452 memset(&g, 0, sizeof g);
453 if (!triv && E->constraint.kind == JV_VALUE) { jv_exact(&E->constraint, &v, &b); agg_add(&g, absform(&v, &b, &tol_con)); }
454 res[3] = triv ? C_NE : agg_res(&g);
455 /* 5 */
456 memset(&g, 0, sizeof g);
457 if (E->povm.kind == JV_VALUE) { jv_exact(&E->povm, &v, &b); agg_add(&g, absform(&v, &b, &tol_povm)); }
458 res[4] = agg_res(&g);
459 /* 6 */
460 memset(&g, 0, sizeof g);
461 if (!triv) {
462 int all = 1;
463 bq sv, sb; bq_init(&sv); bq_init(&sb);
464 for (int k = 0; k < M; k++) {
465 if (E->clock[k].kind != JV_VALUE) { all = 0; break; }
466 jv_exact(&E->clock[k], &v, &b); bq_add(&sv, &sv, &v); bq_add(&sb, &sb, &b);
467 }
468 if (all) { bq_sub(&sv, &sv, &one); agg_add(&g, absform(&sv, &sb, &tol_p)); }
469 bq_free(&sv); bq_free(&sb);
470 }
471 res[5] = triv ? C_NE : agg_res(&g);
472 /* 7: every clock_probability and pauli value, reference lines excluded */
473 memset(&g, 0, sizeof g);
474 if (!triv) {
475 for (int k = 0; k < M; k++) for (int q = -1; q < 6; q++) {
476 const jv *x = q < 0 ? &E->clock[k] : &E->pauli[k][q];
477 if (x->kind != JV_VALUE) continue;
478 jv_exact(x, &v, &b);
479 bq_neg(&t, &v); agg_add(&g, signedform(&t, &b, &tol_p));
480 bq_sub(&t, &v, &one); agg_add(&g, signedform(&t, &b, &tol_p));
481 }
482 }
483 res[6] = triv ? C_NE : agg_res(&g);
484 /* 8 */
485 memset(&g, 0, sizeof g);
486 if (!triv) for (int k = 0; k < M; k++) {
487 if (E->status[k] != AT1_LABEL_DEFINED) continue;
488 for (int a = 0; a < 3; a++) {
489 const jv *pl = &E->pauli[k][2 * a], *mi = &E->pauli[k][2 * a + 1];
490 if (pl->kind != JV_VALUE || mi->kind != JV_VALUE) continue;
491 bq v2, b2; bq_init(&v2); bq_init(&b2);
492 jv_exact(pl, &v, &b); jv_exact(mi, &v2, &b2);
493 bq_add(&s, &v, &v2); bq_sub(&s, &s, &one); bq_add(&b, &b, &b2);
494 agg_add(&g, absform(&s, &b, &tol_p));
495 bq_free(&v2); bq_free(&b2);
496 }
497 }
498 res[7] = triv ? C_NE : agg_res(&g);
499 /* 9 */
500 if (triv) res[8] = C_NE;
501 else { res[8] = C_PASS; for (int k = 0; k < M; k++) if (E->status[k] == AT1_LABEL_UNDEFINED) res[8] = C_FAIL; }
502 /* 10 and 11 */
503 for (int chk = 0; chk < 2; chk++) {
504 memset(&g, 0, sizeof g);
505 int use_ideal = chk == 0 && c->target == AT1_TARGET_IDEAL;
506 if (!triv && !(chk == 1 && c->target == AT1_TARGET_INTERACTING)) {
507 for (int k = 0; k < M; k++) {
508 if (E->status[k] != AT1_LABEL_DEFINED || R->status[k] != AT1_LABEL_DEFINED) continue;
509 for (int q = 0; q < 6; q++) {
510 const jv *ref = use_ideal ? &R->ideal[k][q] : &R->inter[k][q];
511 if (E->pauli[k][q].kind != JV_VALUE || ref->kind != JV_VALUE) continue;
512 agg_add(&g, absdiff(&E->pauli[k][q], ref, &tol_s));
513 }
514 if (E->clock[k].kind != JV_VALUE) continue;
515 if (use_ideal) {
516 jv_exact(&E->clock[k], &v, &b); bq_sub(&s, &v, &wn);
517 agg_add(&g, absform(&s, &b, &tol_p));
518 } else if (R->clock[k].kind == JV_VALUE) {
519 agg_add(&g, absdiff(&E->clock[k], &R->clock[k], &tol_p));
520 }
521 }
522 }
523 res[9 + chk] = triv ? C_NE : agg_res(&g);
524 }
525 /* 12 */
526 memset(&g, 0, sizeof g);
527 if (!triv) for (int k = 0; k < M; k++) {
528 at1_label_status a = E->status[k], r = R->status[k];
529 if (a == AT1_LABEL_INDETERMINATE || r == AT1_LABEL_INDETERMINATE) agg_add(&g, C_INDET);
530 else agg_add(&g, a != r ? C_FAIL : C_PASS);
531 }
532 res[11] = triv ? C_NE : agg_res(&g);
533
534 const int code_of[12] = { K_BOUND, K_NONFIN, K_TRIV, K_CON, K_POVM, K_SUM, K_RANGE, K_SUM, K_COND,
535 c->target == AT1_TARGET_IDEAL ? K_SCHRO : K_INTER, K_ORACLE, K_ORACLE };
536 int indet = 0;
537 for (int k = 0; k < M; k++) if (E->status[k] == AT1_LABEL_INDETERMINATE || R->status[k] == AT1_LABEL_INDETERMINATE) indet = 1;
538 *codes = 0;
539 for (int i = 0; i < 12; i++) { if (res[i] == C_FAIL) *codes |= 1u << code_of[i]; if (res[i] == C_INDET) indet = 1; }
540 if (indet) *codes |= 1u << K_PREC;
541 bq_free(&tol_con); bq_free(&tol_povm); bq_free(&tol_p); bq_free(&tol_s); bq_free(&one); bq_free(&wn);
542 bq_free(&v); bq_free(&b); bq_free(&s); bq_free(&t);
543}
544
545/* ---- writer ------------------------------------------------------------------------------------- */
546static void eng_from_values(eng_t *E, const at1_values *V)
547{
548 const int M = V->M;
549 E->M = M; E->trivial = V->trivial; E->kernel_dim = V->kernel_dim;
550 jv_init(&E->constraint); jv_init(&E->povm);
551 jv_from_val(&E->constraint, &V->constraint); jv_from_val(&E->povm, &V->povm);
552 E->status = at1_xmalloc((size_t)M * sizeof *E->status);
553 E->clock = at1_xmalloc((size_t)M * sizeof(jv));
554 E->pauli = at1_xmalloc((size_t)M * sizeof *E->pauli);
555 for (int k = 0; k < M; k++) {
556 E->status[k] = V->status[k];
557 jv_init(&E->clock[k]); jv_from_val(&E->clock[k], &V->clock[k]);
558 for (int q = 0; q < 6; q++) { jv_init(&E->pauli[k][q]); jv_from_val(&E->pauli[k][q], &V->pauli[k][q]); }
559 }
560}
561static void eng_free(eng_t *E)
562{
563 jv_free(&E->constraint); jv_free(&E->povm);
564 for (int k = 0; k < E->M; k++) { jv_free(&E->clock[k]); for (int q = 0; q < 6; q++) jv_free(&E->pauli[k][q]); }
565 free(E->status); free(E->clock); free(E->pauli);
566}
567
568static void write_engine_values(buf_t *o, const at1_case *c, const eng_t *E)
569{
570 char t[640], vb[480];
571 snprintf(t, sizeof t, "physical_state_kernel_dim %d\n", E->kernel_dim); put(o, t);
572 jv_text(&E->constraint, vb, sizeof vb); snprintf(t, sizeof t, "constraint_residual %s\n", vb); put(o, t);
573 jv_text(&E->povm, vb, sizeof vb); snprintf(t, sizeof t, "povm_residual %s\n", vb); put(o, t);
574 for (int k = 0; k < E->M; k++) { snprintf(t, sizeof t, "label %d %s %s\n", k, c->labels[k], STATUS_NAME[E->status[k]]); put(o, t); }
575 for (int k = 0; k < E->M; k++) { jv_text(&E->clock[k], vb, sizeof vb); snprintf(t, sizeof t, "clock_probability %d %s\n", k, vb); put(o, t); }
576 for (int k = 0; k < E->M; k++) for (int q = 0; q < 6; q++) {
577 jv_text(&E->pauli[k][q], vb, sizeof vb);
578 snprintf(t, sizeof t, "pauli %d %s %s %s\n", k, AXES[q / 2], SIGNS[q % 2], vb); put(o, t);
579 }
580}
581
582static at1_status write_common_head(buf_t *o, const at1_case *c)
583{
584 char cid[65], aid[65], t[256];
585 /* a writer refuses blocks that do not reproduce the copied identities */
586 at1_tagged_sha256("omega.at1.case.v1", c->semantic_text, c->semantic_len, cid);
587 at1_tagged_sha256("omega.at1.acceptance.v1", c->acceptance_text, c->acceptance_len, aid);
588 if (strcmp(cid, c->case_id) != 0 || strcmp(aid, c->acceptance_id) != 0) return AT1_ERR_INTERNAL;
589 put(o, "OMEGA-AT1-RESULT v1\ndomain omega.at1.result.v1\ncontract AT1_RESULT_V1\ncase_contract AT1_CASE_V1\n");
590 snprintf(t, sizeof t, "case_name %s\n", c->name); put(o, t);
591 putn(o, c->semantic_text, c->semantic_len);
592 putn(o, c->acceptance_text, c->acceptance_len);
593 snprintf(t, sizeof t, "case_id %s\nacceptance_id %s\ncase_file_sha256 %s\n", c->case_id, c->acceptance_id, c->case_file_sha256); put(o, t);
594 return AT1_OK;
595}
596
597static void write_tail(buf_t *o, const at1_case *c, const char *verdict, const char *prov)
598{
599 char vid[65], ev[65];
600 size_t pre_len = 8 + 64 + 1 + 14 + 64 + 1;
601 char *pre = at1_xmalloc(pre_len + strlen(verdict) + 1);
602 snprintf(pre, pre_len + strlen(verdict) + 1, "case_id %s\nacceptance_id %s\n%s", c->case_id, c->acceptance_id, verdict);
603 at1_tagged_sha256("omega.at1.verdict.v1", pre, strlen(pre), vid);
604 free(pre);
605 put(o, verdict);
606 put(o, "verdict_id "); put(o, vid); put(o, "\n");
607 put(o, "begin provenance\n"); put(o, prov); put(o, "end provenance\n");
608 at1_tagged_sha256("omega.at1.evidence.v1", o->p, o->n, ev);
609 put(o, "evidence_digest "); put(o, ev); put(o, "\nend\n");
610}
611
612at1_status at1_result_write(const at1_case *c, const at1_values *v, const char *oracle_text, size_t oracle_len,
613 const char *prov_text, size_t prov_len, char **out, size_t *out_len, char *err, size_t cap)
614{
615 *out = NULL; *out_len = 0; err[0] = 0;
616 if (!oracle_text) return at1_result_write_error(c, "ORACLE_UNAVAILABLE", prov_text, prov_len, out, out_len, err, cap);
617 refs_t R; buf_t prov = { 0 }, o = { 0 }, verdict = { 0 };
618 at1_status st = refs_parse(c, oracle_text, oracle_len, &R, err, cap);
619 if (st == AT1_OK) st = prov_parse(prov_text, prov_len, 0, &prov, err, cap);
620 if (st == AT1_OK) st = write_common_head(&o, c);
621 if (st != AT1_OK) { refs_free(&R); free(prov.p); free(o.p); return st; }
622 eng_t E; eng_from_values(&E, v);
623 put(&o, "begin numerics\narithmetic BINARY64\nbound_kind ESTIMATED\nthreads 1\nend numerics\nbegin values\n");
624 write_engine_values(&o, c, &E);
625 for (int i = 0; i < R.nlines; i++) { put(&o, R.lines[i]); put(&o, "\n"); }
626 put(&o, "end values\n");
627 int res[12]; unsigned codes;
628 judge(c, &E, &R, res, &codes);
629 char t[256];
630 put(&verdict, "begin verdict\n");
631 for (int i = 0; i < 12; i++) { snprintf(t, sizeof t, "check %s %s\n", CHECK_NAME[i], CHECK_STATE[res[i]]); put(&verdict, t); }
632 int pass = codes == 0;
633 for (int i = 0; i < 12; i++) if (res[i] == C_FAIL || res[i] == C_INDET) pass = 0;
634 put(&verdict, pass ? "outcome PASS\n" : "outcome FAIL\n");
635 put(&verdict, "failure_codes ");
636 if (codes == 0) put(&verdict, "none");
637 for (int i = 0, first = 1; i < AT1_FAILURE_CODE_COUNT; i++) if (codes & (1u << i)) { if (!first) put(&verdict, ","); put(&verdict, at1_failure_codes[i]); first = 0; }
638 put(&verdict, "\nerror_code none\n");
639 int met = (pass ? AT1_EXPECT_PASS : AT1_EXPECT_FAIL) == c->expected_outcome && codes == c->expected_codes;
640 put(&verdict, met ? "expectation_met YES\n" : "expectation_met NO\n");
641 put(&verdict, "end verdict\n");
642 write_tail(&o, c, verdict.p, prov.p);
643 eng_free(&E); refs_free(&R); free(prov.p); free(verdict.p);
644 *out = o.p; *out_len = o.n;
645 return AT1_OK;
646}
647
648at1_status at1_result_write_error(const at1_case *c, const char *error_code, const char *prov_text, size_t prov_len,
649 char **out, size_t *out_len, char *err, size_t cap)
650{
651 *out = NULL; *out_len = 0; err[0] = 0;
652 buf_t prov = { 0 }, o = { 0 }, verdict = { 0 };
653 int oracle_none = strcmp(error_code, "ORACLE_UNAVAILABLE") == 0;
654 at1_status st = prov_parse(prov_text, prov_len, oracle_none, &prov, err, cap);
655 if (st == AT1_OK) st = write_common_head(&o, c);
656 if (st != AT1_OK) { free(prov.p); free(o.p); return st; }
657 const int M = c->label_count;
658 char t[256];
659 put(&o, "begin numerics\narithmetic BINARY64\nbound_kind ESTIMATED\nthreads 1\nend numerics\nbegin values\n");
660 put(&o, "physical_state_kernel_dim 0\nconstraint_residual undefined 0@0\npovm_residual undefined 0@0\n");
661 for (int k = 0; k < M; k++) { snprintf(t, sizeof t, "label %d %s UNDEFINED\n", k, c->labels[k]); put(&o, t); }
662 for (int k = 0; k < M; k++) { snprintf(t, sizeof t, "clock_probability %d undefined 0@0\n", k); put(&o, t); }
663 for (int k = 0; k < M; k++) for (int q = 0; q < 6; q++) { snprintf(t, sizeof t, "pauli %d %s %s undefined 0@0\n", k, AXES[q / 2], SIGNS[q % 2]); put(&o, t); }
664 for (int k = 0; k < M; k++) { snprintf(t, sizeof t, "reference_label %d %s UNDEFINED\n", k, c->labels[k]); put(&o, t); }
665 for (int k = 0; k < M; k++) { snprintf(t, sizeof t, "reference_clock_probability %d undefined 0@0\n", k); put(&o, t); }
666 for (int f = 0; f < 2; f++) for (int k = 0; k < M; k++) for (int q = 0; q < 6; q++) {
667 snprintf(t, sizeof t, "%s %d %s %s undefined 0@0\n", f == 0 ? "reference_ideal" : "reference_interacting", k, AXES[q / 2], SIGNS[q % 2]);
668 put(&o, t);
669 }
670 put(&o, "end values\n");
671 put(&verdict, "begin verdict\n");
672 for (int i = 0; i < 12; i++) { snprintf(t, sizeof t, "check %s NOT_EVALUATED\n", CHECK_NAME[i]); put(&verdict, t); }
673 snprintf(t, sizeof t, "outcome ERROR\nfailure_codes none\nerror_code %s\nexpectation_met NOT_APPLICABLE\nend verdict\n", error_code);
674 put(&verdict, t);
675 write_tail(&o, c, verdict.p, prov.p);
676 free(prov.p); free(verdict.p);
677 *out = o.p; *out_len = o.n;
678 return AT1_OK;
679}
680
681at1_status at1_values_write(const at1_case *c, const at1_values *v, char **out, size_t *out_len)
682{
683 buf_t o = { 0 };
684 eng_t E; eng_from_values(&E, v);
685 write_engine_values(&o, c, &E);
686 eng_free(&E);
687 *out = o.p; *out_len = o.n;
688 return AT1_OK;
689}