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 / at0 / evaluator / src / at0e_result.c

529 lines · 37193 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/* Independent AT0_RESULT_V1 parser, exact re-derivation of the ten checks, and
18 * the evaluator's verification pass (identities, bindings, provenance, shadow). */
19#include "at0e.h"
20#include <math.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25/* aien-architecture commits verified (sha256sum) to hold AT0_CASE_V1.md d90af74b... and AT0_RESULT_V1.md dc52c573... unchanged */
26/* commits verified by Agent 4 to hold the frozen contract bytes (AT0_CASE_V1.md d90af74b..., AT0_RESULT_V2.md bd0f9eb8... at the last two) */
27const char *const at0e_contract_commits[] = { AT0E_CONTRACT_COMMIT, "00e9f2308666f74e58f524b177b2a31eccdc69ba",
28 "c7a7181eda57e353dbff3b7d20cde33fb65faeb2", "fe86e43aae63370b3084f78e971a33b81ce75a9d", NULL };
29const char *const at0e_check_names[AT0E_NCHECKS] = {
30 "bound_kind_sufficient", "values_finite", "physical_state_nontrivial", "constraint_residual",
31 "povm_normalization", "clock_probability_sum", "probability_range", "pauli_pair_sum",
32 "conditional_defined", "schrodinger_agreement" };
33const char *const at0e_check_codes[AT0E_NCHECKS] = {
34 "BOUND_KIND_INSUFFICIENT", "NONFINITE_VALUE", "TRIVIAL_PHYSICAL_STATE", "CONSTRAINT_RESIDUAL_EXCEEDED",
35 "POVM_NORMALIZATION_EXCEEDED", "PROBABILITY_SUM_EXCEEDED", "PROBABILITY_OUT_OF_RANGE", "PROBABILITY_SUM_EXCEEDED",
36 "CONDITIONAL_UNDEFINED", "SCHRODINGER_DEVIATION_EXCEEDED" };
37static const char *const axis_names[3] = { "X", "Y", "Z" };
38static const char *const sign_names[2] = { "PLUS", "MINUS" };
39
40/* ---------------- parse ---------------- */
41typedef struct { char **lines; size_t n, i; const char *finding, *detail; } rp;
42#define FAIL_WITH(code, det) do { p->finding = code; p->detail = det; return 0; } while (0)
43static int rp_exact(rp *p, const char *line) {
44 if (p->i >= p->n) FAIL_WITH("E4-STRUCT-TRUNCATED", "unexpected end of file");
45 if (strcmp(p->lines[p->i], line) != 0) FAIL_WITH("E4-STRUCT-ORDER", p->lines[p->i]);
46 p->i++; return 1;
47}
48static int rp_key(rp *p, const char *key, char **val) {
49 char *tok[8];
50 if (p->i >= p->n) FAIL_WITH("E4-STRUCT-TRUNCATED", "unexpected end of file");
51 int n = split_tokens(p->lines[p->i], tok, 8);
52 if (n != 2 || strcmp(tok[0], key) != 0) FAIL_WITH("E4-STRUCT-ORDER", p->lines[p->i]);
53 *val = tok[1]; p->i++; return 1;
54}
55static int rp_text_key(rp *p, const char *key, char *dst, size_t cap, int allow_none) {
56 if (p->i >= p->n) FAIL_WITH("E4-STRUCT-TRUNCATED", "unexpected end of file");
57 const char *line = p->lines[p->i]; size_t kl = strlen(key);
58 if (strncmp(line, key, kl) != 0 || line[kl] != ' ') FAIL_WITH("E4-STRUCT-ORDER", line);
59 const char *v = line + kl + 1;
60 if (!is_text200(v)) FAIL_WITH("E4-STRUCT-ENCODING", line);
61 (void)allow_none;
62 snprintf(dst, cap, "%s", v); p->i++; return 1;
63}
64static int parse_rval(rp *p, const char *vtok, const char *btok, rval *out) {
65 memset(out, 0, sizeof *out);
66 int canon = 1;
67 if (!scaled_parse(btok, &out->bound, &canon) || !canon) FAIL_WITH("E4-STRUCT-ENCODING", p->lines[p->i]);
68 if (!strcmp(vtok, "undefined")) { out->is_undefined = 1; if (out->bound.n != 0) FAIL_WITH("E4-STRUCT-ENCODING", p->lines[p->i]); return 1; }
69 if (!strcmp(vtok, "nonfinite")) { out->is_nonfinite = 1; return 1; }
70 if (strncmp(vtok, "f64:", 4) != 0 || !hex_to_u64(vtok + 4, &out->bits)) FAIL_WITH("E4-STRUCT-ENCODING", p->lines[p->i]);
71 if (((out->bits >> 52) & 0x7ff) == 0x7ff) FAIL_WITH("E4-STRUCT-ENCODING", "nonfinite bit pattern written as f64:");
72 if (out->bits == 0x8000000000000000ULL) FAIL_WITH("E4-STRUCT-ENCODING", "-0.0 must be written as +0.0");
73 return 1;
74}
75static int rp_value_line(rp *p, const char *key, rval *out) {
76 char *tok[8];
77 if (p->i >= p->n) FAIL_WITH("E4-STRUCT-TRUNCATED", "unexpected end of file");
78 int n = split_tokens(p->lines[p->i], tok, 8);
79 if (n != 3 || strcmp(tok[0], key) != 0) FAIL_WITH("E4-STRUCT-ORDER", p->lines[p->i]);
80 if (!parse_rval(p, tok[1], tok[2], out)) return 0;
81 p->i++; return 1;
82}
83static int rp_indexed_value(rp *p, const char *key, int k, int axis, int sign, rval *out) {
84 char *tok[8]; long kk;
85 if (p->i >= p->n) FAIL_WITH("E4-STRUCT-TRUNCATED", "unexpected end of file");
86 int n = split_tokens(p->lines[p->i], tok, 8);
87 int want = axis < 0 ? 4 : 6;
88 if (n != want || strcmp(tok[0], key) != 0) FAIL_WITH("E4-STRUCT-ORDER", p->lines[p->i]);
89 if (!parse_int_canonical(tok[1], &kk) || kk != k) FAIL_WITH("E4-STRUCT-ORDER", p->lines[p->i]);
90 if (axis >= 0) {
91 if (strcmp(tok[2], axis_names[axis]) != 0 || strcmp(tok[3], sign_names[sign]) != 0) FAIL_WITH("E4-STRUCT-ORDER", p->lines[p->i]);
92 }
93 if (!parse_rval(p, tok[want - 2], tok[want - 1], out)) return 0;
94 p->i++; return 1;
95}
96static int parse_codes(const char *val, char codes[16][48], int *count) {
97 *count = 0;
98 if (!strcmp(val, "none")) return 1;
99 char buf[AT0E_MAX_LINE]; snprintf(buf, sizeof buf, "%s", val);
100 size_t l = strlen(buf);
101 if (l == 0 || buf[0] == ',' || buf[l - 1] == ',' || strstr(buf, ",,")) return 0;
102 char *s = buf;
103 while (s) {
104 char *c = strchr(s, ','); if (c) *c = 0;
105 if (*count >= 16 || strlen(s) >= 48 || !failure_code_valid(s)) return 0;
106 if (*count > 0 && strcmp(codes[*count - 1], s) >= 0) return 0; /* sorted, unique */
107 memcpy(codes[(*count)++], s, strlen(s) + 1);
108 s = c ? c + 1 : NULL;
109 }
110 return 1;
111}
112
113int result_parse(const textfile *tf, at0_result *r, const char **finding, const char **detail) {
114 rp pp = { tf->lines, tf->n, 0, NULL, NULL }, *p = &pp;
115 memset(r, 0, sizeof *r);
116 char *v;
117 *finding = NULL; *detail = "";
118 if (p->i < p->n && !strcmp(p->lines[0], "OMEGA-AT0-RESULT v2")) r->version = 2;
119 else if (p->i < p->n && !strcmp(p->lines[0], "OMEGA-AT0-RESULT v1")) r->version = 1;
120 else { p->finding = "E4-STRUCT-VERSION"; p->detail = tf->n ? tf->lines[0] : "empty"; goto out; }
121 p->i = 1;
122 if (!rp_exact(p, r->version == 2 ? "domain omega.at0.result.v2" : "domain omega.at0.result.v1")) goto out;
123 if (!rp_exact(p, r->version == 2 ? "contract AT0_RESULT_V2" : "contract AT0_RESULT_V1")) goto out;
124 if (!rp_exact(p, "case_contract AT0_CASE_V1")) goto out;
125 {
126 size_t consumed = 0; const char *d = "";
127 refusal rf = case_parse_blocks(tf->lines + p->i, tf->n - p->i, &consumed, &r->c, &d, 1);
128 if (rf == REFUSE_PARSE_ERROR) { p->finding = "E4-STRUCT-CASE-SHAPE"; p->detail = d; goto out; }
129 if (rf == REFUSE_ID_MISMATCH) { p->finding = "E4-ID-CASE"; p->detail = d; goto out; }
130 if (rf != REFUSE_NONE) { p->finding = "E4-STRUCT-CASE-REFUSED"; p->detail = d; goto out; }
131 p->i += consumed;
132 }
133 if (!rp_key(p, "case_file_sha256", &v)) goto out;
134 if (!is_digest(v)) goto enc_err;
135 snprintf(r->case_file_sha256, 65, "%s", v);
136 if (!rp_exact(p, "begin numerics")) goto out;
137 if (!rp_key(p, "arithmetic", &v)) goto out;
138 if (!strcmp(v, "BINARY64")) r->arithmetic = 0; else if (!strcmp(v, "BINARY64_INTERVAL")) r->arithmetic = 1;
139 else if (!strcmp(v, "BINARY64_COMPENSATED")) r->arithmetic = 2; else if (!strcmp(v, "NONE")) r->arithmetic = 3;
140 else goto enc_err;
141 if (!rp_key(p, "bound_kind", &v)) goto out;
142 if (!strcmp(v, "RIGOROUS")) r->bound_kind = BK_RIGOROUS; else if (!strcmp(v, "ESTIMATED")) r->bound_kind = BK_ESTIMATED;
143 else if (!strcmp(v, "NONE")) r->bound_kind = BK_NONE; else goto enc_err;
144 if (!rp_exact(p, "threads 1")) goto out;
145 if (!rp_exact(p, "end numerics")) goto out;
146 if (!rp_exact(p, "begin values")) goto out;
147 if (!rp_key(p, "physical_state_kernel_dim", &v)) goto out;
148 if (!parse_int_canonical(v, &r->kernel_dim) || r->kernel_dim < 0) goto enc_err;
149 if (!rp_value_line(p, "constraint_residual", &r->constraint_residual)) goto out;
150 if (!rp_value_line(p, "povm_residual", &r->povm_residual)) goto out;
151 for (int k = 0; k < r->c.M; k++) {
152 char *tok[8]; long kk;
153 if (p->i >= p->n) { p->finding = "E4-STRUCT-TRUNCATED"; p->detail = "values"; goto out; }
154 int n = split_tokens(p->lines[p->i], tok, 8);
155 if (n != 4 || strcmp(tok[0], "label") || !parse_int_canonical(tok[1], &kk) || kk != k || strcmp(tok[2], r->c.label[k]) != 0) { p->finding = "E4-STRUCT-ORDER"; p->detail = p->lines[p->i]; goto out; }
156 if (!strcmp(tok[3], "DEFINED")) r->label_status[k] = LS_DEFINED; else if (!strcmp(tok[3], "UNDEFINED")) r->label_status[k] = LS_UNDEFINED;
157 else if (!strcmp(tok[3], "INDETERMINATE")) r->label_status[k] = LS_INDETERMINATE; else { p->finding = "E4-STRUCT-ENCODING"; p->detail = p->lines[p->i]; goto out; }
158 p->i++;
159 }
160 for (int k = 0; k < r->c.M; k++) if (!rp_indexed_value(p, "clock_probability", k, -1, 0, &r->clock_p[k])) goto out;
161 for (int k = 0; k < r->c.M; k++) for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) if (!rp_indexed_value(p, "pauli", k, a, s, &r->pauli[k][a][s])) goto out;
162 for (int k = 0; k < r->c.M; k++) for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) if (!rp_indexed_value(p, "reference", k, a, s, &r->reference[k][a][s])) goto out;
163 if (!rp_exact(p, "end values")) goto out;
164 size_t ver_start = p->i;
165 if (!rp_exact(p, "begin verdict")) goto out;
166 for (int i = 0; i < AT0E_NCHECKS; i++) {
167 char *tok[8];
168 if (p->i >= p->n) { p->finding = "E4-STRUCT-TRUNCATED"; p->detail = "verdict"; goto out; }
169 int n = split_tokens(p->lines[p->i], tok, 8);
170 if (n != 3 || strcmp(tok[0], "check") || strcmp(tok[1], at0e_check_names[i])) { p->finding = "E4-STRUCT-ORDER"; p->detail = p->lines[p->i]; goto out; }
171 if (!strcmp(tok[2], "PASS")) r->check[i] = CK_PASS; else if (!strcmp(tok[2], "FAIL")) r->check[i] = CK_FAIL;
172 else if (!strcmp(tok[2], "INDETERMINATE")) r->check[i] = CK_INDET; else if (!strcmp(tok[2], "NOT_EVALUATED")) r->check[i] = CK_NOTEVAL;
173 else { p->finding = "E4-STRUCT-ENCODING"; p->detail = p->lines[p->i]; goto out; }
174 p->i++;
175 }
176 if (!rp_key(p, "outcome", &v)) goto out;
177 if (!strcmp(v, "PASS")) r->outcome = OUT_PASS; else if (!strcmp(v, "FAIL")) r->outcome = OUT_FAIL; else if (!strcmp(v, "ERROR")) r->outcome = OUT_ERROR;
178 else if (!strcmp(v, "NOT_RUN")) r->outcome = OUT_NOT_RUN; else goto enc_err;
179 if (!rp_key(p, "failure_codes", &v)) goto out;
180 if (!parse_codes(v, r->failure_codes, &r->n_failure_codes)) goto enc_err;
181 if (!rp_key(p, "error_code", &v)) goto out;
182 if (strcmp(v, "none") && strcmp(v, "ORACLE_UNAVAILABLE") && strcmp(v, "RESOURCE_LIMIT") && strcmp(v, "INTERNAL_ERROR")) goto enc_err;
183 snprintf(r->error_code, sizeof r->error_code, "%s", v);
184 if (!rp_key(p, "expectation_met", &v)) goto out;
185 if (!strcmp(v, "YES")) r->expectation_met = 0; else if (!strcmp(v, "NO")) r->expectation_met = 1; else if (!strcmp(v, "NOT_APPLICABLE")) r->expectation_met = 2; else goto enc_err;
186 if (!rp_exact(p, "end verdict")) goto out;
187 {
188 size_t total = 0;
189 for (size_t i = ver_start; i < p->i; i++) total += strlen(tf->lines[i]) + 1;
190 r->ver_block = malloc(total + 1); size_t o = 0;
191 for (size_t i = ver_start; i < p->i; i++) { size_t l = strlen(tf->lines[i]); memcpy(r->ver_block + o, tf->lines[i], l); o += l; r->ver_block[o++] = '\n'; }
192 r->ver_block[o] = 0; r->ver_len = o;
193 }
194 if (!rp_key(p, "verdict_id", &v)) goto out;
195 if (!is_digest(v)) goto enc_err;
196 snprintf(r->verdict_id, 65, "%s", v);
197 if (!rp_exact(p, "begin provenance")) goto out;
198 if (!rp_key(p, "source_repo", &v)) goto out;
199 if (!strchr(v, '/') || strlen(v) > 79) goto enc_err;
200 snprintf(r->source_repo, sizeof r->source_repo, "%s", v);
201 if (!rp_key(p, "source_commit", &v)) goto out;
202 if (!is_sha1hex(v)) goto enc_err;
203 snprintf(r->source_commit, sizeof r->source_commit, "%s", v);
204 if (!rp_key(p, "source_tree_clean", &v)) goto out;
205 if (!strcmp(v, "YES")) r->source_tree_clean = 1; else if (!strcmp(v, "NO")) r->source_tree_clean = 0; else goto enc_err;
206 if (!rp_key(p, "contract_commit", &v)) goto out;
207 if (!is_sha1hex(v)) goto enc_err;
208 snprintf(r->contract_commit, sizeof r->contract_commit, "%s", v);
209 int placeholder = r->outcome == OUT_ERROR || r->outcome == OUT_NOT_RUN;
210 if (!rp_key(p, "engine_sha256", &v)) goto out;
211 if (!(is_digest(v) || (placeholder && !strcmp(v, "none")))) goto enc_err;
212 snprintf(r->engine_sha256, 65, "%s", v);
213 if (!rp_key(p, "oracle_repo", &v)) goto out;
214 if (!((strchr(v, '/') && strlen(v) < 80) || (placeholder && !strcmp(v, "none")))) goto enc_err;
215 snprintf(r->oracle_repo, sizeof r->oracle_repo, "%s", v);
216 if (!rp_key(p, "oracle_commit", &v)) goto out;
217 if (!(is_sha1hex(v) || (placeholder && !strcmp(v, "none")))) goto enc_err;
218 snprintf(r->oracle_commit, sizeof r->oracle_commit, "%s", v);
219 if (!rp_key(p, "oracle_sha256", &v)) goto out;
220 if (!(is_digest(v) || (placeholder && !strcmp(v, "none")))) goto enc_err;
221 snprintf(r->oracle_sha256, 65, "%s", v);
222 if (!rp_text_key(p, "build_cc", r->build_cc, sizeof r->build_cc, placeholder)) goto out;
223 if (!rp_text_key(p, "build_flags", r->build_flags, sizeof r->build_flags, placeholder)) goto out;
224 if (!rp_text_key(p, "host", r->host, sizeof r->host, placeholder)) goto out;
225 for (int t = 0; t < 2; t++) {
226 char *dst = t ? r->run_finished : r->run_started;
227 if (!rp_key(p, t ? "run_finished_utc" : "run_started_utc", &v)) goto out;
228 if (!(placeholder && !strcmp(v, "none"))) {
229 if (strlen(v) != 20 || v[4] != '-' || v[7] != '-' || v[10] != 'T' || v[13] != ':' || v[16] != ':' || v[19] != 'Z') goto enc_err;
230 for (int i = 0; i < 19; i++) if (i != 4 && i != 7 && i != 10 && i != 13 && i != 16 && (v[i] < '0' || v[i] > '9')) goto enc_err;
231 }
232 snprintf(dst, 32, "%s", v);
233 }
234 {
235 char prev[AT0E_MAX_LINE] = "";
236 while (p->i < p->n && strncmp(tf->lines[p->i], "artifact ", 9) == 0) {
237 char *tok[8];
238 int n = split_tokens(tf->lines[p->i], tok, 8);
239 if (n != 3 || !is_digest(tok[2]) || strlen(tok[1]) == 0) { p->finding = "E4-STRUCT-ENCODING"; p->detail = tf->lines[p->i]; goto out; }
240 if (prev[0] && strcmp(prev, tok[1]) >= 0) { p->finding = "E4-STRUCT-ORDER"; p->detail = tf->lines[p->i]; goto out; }
241 snprintf(prev, sizeof prev, "%s", tok[1]);
242 r->n_artifacts++; p->i++;
243 }
244 }
245 if (!rp_exact(p, "end provenance")) goto out;
246 r->evidence_line_index = p->i;
247 if (!rp_key(p, "evidence_digest", &v)) goto out;
248 if (!is_digest(v)) goto enc_err;
249 snprintf(r->evidence_digest, 65, "%s", v);
250 if (!rp_exact(p, "end")) goto out;
251 if (p->i != tf->n) { p->finding = "E4-STRUCT-TRAILING"; p->detail = tf->lines[p->i]; goto out; }
252 return 0;
253enc_err:
254 p->finding = "E4-STRUCT-ENCODING"; p->detail = p->lines[p->i - 1]; goto out;
255out:
256 *finding = p->finding ? p->finding : "E4-STRUCT-PARSE";
257 *detail = p->detail ? p->detail : "";
258 return 1;
259}
260void result_free(at0_result *r) { case_free(&r->c); free(r->ver_block); r->ver_block = NULL; }
261
262/* ---------------- exact re-derivation ---------------- */
263static int worst(int a, int b) { /* FAIL > INDET > PASS ; NOT_EVALUATED is neutral */
264 if (a == CK_NOTEVAL) return b;
265 if (b == CK_NOTEVAL) return a;
266 return a > b ? a : b;
267}
268static int tri_to_ck(tri t) { return t == CMP_PASS ? CK_PASS : t == CMP_FAIL ? CK_FAIL : CK_INDET; }
269static int label_status_derive(const rval *p, scaled tol_zero) {
270 if (p->is_undefined || p->is_nonfinite) return LS_UNDEFINED;
271 sbig v, b, t, hi, lo, nb;
272 sbig_from_f64_bits(&v, p->bits); sbig_from_scaled(&b, p->bound); sbig_from_scaled(&t, tol_zero);
273 sbig_add(&hi, &v, &b);
274 if (sbig_cmp(&hi, &t) <= 0) return LS_UNDEFINED;
275 nb = b; sbig_neg(&nb); sbig_add(&lo, &v, &nb);
276 if (sbig_cmp(&lo, &t) > 0) return LS_DEFINED;
277 return LS_INDETERMINATE;
278}
279static void add_code(rederived *d, const char *code) {
280 for (int i = 0; i < d->n_failure_codes; i++) if (!strcmp(d->failure_codes[i], code)) return;
281 if (d->n_failure_codes >= 16) return;
282 snprintf(d->failure_codes[d->n_failure_codes++], 48, "%s", code);
283 for (int i = d->n_failure_codes - 1; i > 0 && strcmp(d->failure_codes[i - 1], d->failure_codes[i]) > 0; i--) {
284 char t[48]; memcpy(t, d->failure_codes[i], 48); memcpy(d->failure_codes[i], d->failure_codes[i - 1], 48); memcpy(d->failure_codes[i - 1], t, 48);
285 }
286}
287void result_rederive(const at0_result *r, rederived *d) {
288 memset(d, 0, sizeof *d);
289 const at0_case *c = &r->c;
290 sbig tol_prob, tol_schro, tol_c, tol_p, one;
291 sbig_from_scaled(&tol_prob, c->tol_prob); sbig_from_scaled(&tol_schro, c->tol_schro);
292 sbig_from_scaled(&tol_c, c->tol_constraint); sbig_from_scaled(&tol_p, c->tol_povm); sbig_from_int(&one, 1);
293 for (int i = 0; i < AT0E_NCHECKS; i++) d->check[i] = CK_NOTEVAL;
294
295 /* 1 */ d->check[0] = r->bound_kind >= c->min_bound_kind ? CK_PASS : CK_FAIL;
296 /* 2 */ {
297 int nf = r->constraint_residual.is_nonfinite || r->povm_residual.is_nonfinite;
298 for (int k = 0; k < c->M; k++) { nf |= r->clock_p[k].is_nonfinite; for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) nf |= r->pauli[k][a][s].is_nonfinite | r->reference[k][a][s].is_nonfinite; }
299 d->check[1] = nf ? CK_FAIL : CK_PASS;
300 }
301 /* 3: exact, from the case */ shadow sh; const char *why; int have_sh = shadow_compute(c, &sh, &why);
302 int nontrivial = have_sh ? (sh.kernel_dim >= 1 && sh.psi_nonzero) : (r->kernel_dim >= 1);
303 d->check[2] = nontrivial ? CK_PASS : CK_FAIL;
304 /* trivial kernel: checks 4..10 lose their input (charter N1, spec 9.2 N1); ambiguity A1 in README */
305 if (nontrivial) {
306 /* 4 */ if (!r->constraint_residual.is_undefined && !r->constraint_residual.is_nonfinite) {
307 sbig v, b; sbig_from_f64_bits(&v, r->constraint_residual.bits); sbig_from_scaled(&b, r->constraint_residual.bound);
308 d->check[3] = tri_to_ck(tri_absolute(&v, &b, &tol_c));
309 }
310 }
311 /* 5 */ if (!r->povm_residual.is_undefined && !r->povm_residual.is_nonfinite) {
312 sbig v, b; sbig_from_f64_bits(&v, r->povm_residual.bits); sbig_from_scaled(&b, r->povm_residual.bound);
313 d->check[4] = tri_to_ck(tri_absolute(&v, &b, &tol_p));
314 }
315 if (nontrivial) {
316 /* 6 */ {
317 int ok = 1; sbig sum, bsum; sbig_zero(&sum); sbig_zero(&bsum);
318 for (int k = 0; k < c->M && ok; k++) {
319 if (r->clock_p[k].is_undefined || r->clock_p[k].is_nonfinite) { ok = 0; break; }
320 sbig v, b; sbig_from_f64_bits(&v, r->clock_p[k].bits); sbig_from_scaled(&b, r->clock_p[k].bound);
321 sbig_add(&sum, &sum, &v); sbig_add(&bsum, &bsum, &b);
322 }
323 if (ok) { sbig m1 = one; sbig_neg(&m1); sbig_add(&sum, &sum, &m1); d->check[5] = tri_to_ck(tri_absolute(&sum, &bsum, &tol_prob)); }
324 }
325 /* 7 */ {
326 int st = CK_NOTEVAL;
327 const rval *all[1 + AT0E_MAX_M * 13]; int n = 0;
328 for (int k = 0; k < c->M; k++) { all[n++] = &r->clock_p[k]; for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) { all[n++] = &r->pauli[k][a][s]; all[n++] = &r->reference[k][a][s]; } }
329 for (int i = 0; i < n; i++) {
330 if (all[i]->is_undefined || all[i]->is_nonfinite) continue;
331 sbig v, b, nv, vm1, m1 = one; sbig_from_f64_bits(&v, all[i]->bits); sbig_from_scaled(&b, all[i]->bound);
332 nv = v; sbig_neg(&nv); st = worst(st, tri_to_ck(tri_signed(&nv, &b, &tol_prob)));
333 sbig_neg(&m1); sbig_add(&vm1, &v, &m1); st = worst(st, tri_to_ck(tri_signed(&vm1, &b, &tol_prob)));
334 }
335 d->check[6] = st;
336 }
337 /* 8 */ {
338 int st = CK_NOTEVAL;
339 for (int k = 0; k < c->M; k++) {
340 if (r->label_status[k] != LS_DEFINED) continue;
341 for (int a = 0; a < 3; a++) {
342 const rval *pp = &r->pauli[k][a][0], *pm = &r->pauli[k][a][1];
343 if (pp->is_undefined || pp->is_nonfinite || pm->is_undefined || pm->is_nonfinite) continue;
344 sbig v1, v2, b1, b2, s, bs, m1 = one;
345 sbig_from_f64_bits(&v1, pp->bits); sbig_from_f64_bits(&v2, pm->bits); sbig_from_scaled(&b1, pp->bound); sbig_from_scaled(&b2, pm->bound);
346 sbig_add(&s, &v1, &v2); sbig_neg(&m1); sbig_add(&s, &s, &m1); sbig_add(&bs, &b1, &b2);
347 st = worst(st, tri_to_ck(tri_absolute(&s, &bs, &tol_prob)));
348 }
349 }
350 d->check[7] = st;
351 }
352 /* 9 */ {
353 int any_undef = 0;
354 for (int k = 0; k < c->M; k++) { if (r->label_status[k] == LS_UNDEFINED) any_undef = 1; if (r->label_status[k] == LS_INDETERMINATE) d->any_label_indeterminate = 1; }
355 d->check[8] = any_undef ? CK_FAIL : CK_PASS;
356 }
357 /* 10 */ {
358 int st = CK_NOTEVAL;
359 for (int k = 0; k < c->M; k++) {
360 if (r->label_status[k] != LS_DEFINED) continue;
361 for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) {
362 const rval *pv = &r->pauli[k][a][s], *rv = &r->reference[k][a][s];
363 if (pv->is_undefined || pv->is_nonfinite || rv->is_undefined || rv->is_nonfinite) continue;
364 sbig v1, v2, b1, b2, dlt, bs;
365 sbig_from_f64_bits(&v1, pv->bits); sbig_from_f64_bits(&v2, rv->bits); sbig_from_scaled(&b1, pv->bound); sbig_from_scaled(&b2, rv->bound);
366 sbig_neg(&v2); sbig_add(&dlt, &v1, &v2); sbig_add(&bs, &b1, &b2);
367 st = worst(st, tri_to_ck(tri_absolute(&dlt, &bs, &tol_schro)));
368 }
369 }
370 d->check[9] = st;
371 }
372 } /* nontrivial */
373 /* codes and outcome */
374 int any_bad = 0;
375 for (int i = 0; i < AT0E_NCHECKS; i++) {
376 if (d->check[i] == CK_FAIL) { add_code(d, at0e_check_codes[i]); any_bad = 1; }
377 if (d->check[i] == CK_INDET) { add_code(d, "PRECISION_INSUFFICIENT"); any_bad = 1; }
378 }
379 if (d->any_label_indeterminate) { add_code(d, "PRECISION_INSUFFICIENT"); any_bad = 1; }
380 d->outcome = any_bad ? OUT_FAIL : OUT_PASS;
381 int same = (d->outcome == OUT_PASS) == (c->expected_pass != 0) && d->n_failure_codes == c->n_expected_codes;
382 for (int i = 0; same && i < d->n_failure_codes; i++) if (strcmp(d->failure_codes[i], c->expected_codes[i])) same = 0;
383 d->expectation_met = same ? 0 : 1;
384}
385
386/* ---------------- verification ---------------- */
387static long double f64_ld(uint64_t bits) { double x; memcpy(&x, &bits, 8); return (long double)x; }
388static long double scaled_ld(scaled s) { long double v = (long double)(unsigned long long)(s.n >> 64) * 18446744073709551616.0L + (long double)(unsigned long long)s.n; for (int i = 0; i < s.k; i++) v /= 10.0L; return v; }
389static const char *const fail_codes_sev[] = {
390 "E4-STRUCT-", "E4-ID-", "E4-BIND-", "E4-CHECK-", "E4-OUTCOME-", "E4-CODES-", "E4-EXPECTATION-", "E4-LABEL-",
391 "E4-SHADOW-", "E4-BOUND-", "E4-PLACEHOLDER-", "E4-TIME-", "E4-PROV-ORACLE-IS-ENGINE", "E4-PROV-NONE-", NULL };
392static int is_fail_code(const char *code) { if (!strcmp(code, "E4-SHADOW-UNAVAILABLE")) return 0; for (int i = 0; fail_codes_sev[i]; i++) if (!strncmp(code, fail_codes_sev[i], strlen(fail_codes_sev[i]))) return 1; return 0; }
393
394const char *result_verify(const textfile *tf, const at0_result *r, const textfile *case_tf, int use_shadow, findings *fs) {
395 const at0_case *c = &r->c;
396 char id[65];
397 int inconclusive = 0;
398 /* identities */
399 {
400 char pre[200]; int pl = snprintf(pre, sizeof pre, "case_id %s\nacceptance_id %s\n", c->case_id, c->acceptance_id);
401 uint8_t *buf = malloc((size_t)pl + r->ver_len); if (!buf) { findings_add(fs, "E4-STRUCT-UNREADABLE", "out of memory"); return "FAIL"; } memcpy(buf, pre, (size_t)pl); memcpy(buf + pl, r->ver_block, r->ver_len);
402 sha256_tagged_hex(r->version == 2 ? AT0E_VERDICT_DOMAIN_V2 : AT0E_VERDICT_DOMAIN_V1, buf, (size_t)pl + r->ver_len, id); free(buf);
403 if (strcmp(id, r->verdict_id)) findings_add(fs, "E4-ID-VERDICT", "verdict_id %s does not recompute (expected %s)", r->verdict_id, id);
404 size_t upto = 0;
405 for (size_t i = 0; i < r->evidence_line_index; i++) upto += strlen(tf->lines[i]) + 1;
406 sha256_tagged_hex(r->version == 2 ? AT0E_EVIDENCE_DOMAIN_V2 : AT0E_EVIDENCE_DOMAIN_V1, tf->bytes, upto, id);
407 if (strcmp(id, r->evidence_digest)) findings_add(fs, "E4-ID-EVIDENCE", "evidence_digest does not recompute: record altered or digest forged (expected %s)", id);
408 }
409 /* binding to the supplied case file */
410 if (!case_tf) { findings_add(fs, "E4-UNBOUND-NO-CASE-FILE", "no case file supplied: the embedded case is trusted, so this verification is not citable (run with --case)"); inconclusive = 1; }
411 if (case_tf) {
412 at0_case cc; const char *d;
413 refusal rf = case_parse_validate(case_tf, &cc, &d);
414 if (rf != REFUSE_NONE) findings_add(fs, "E4-BIND-CASE-REFUSED", "supplied case file is refused: %s (%s)", refusal_name(rf), d);
415 else {
416 if (strcmp(cc.case_file_sha256, r->case_file_sha256)) findings_add(fs, "E4-BIND-CASE-FILE", "case_file_sha256 %s differs from the supplied case file %s", r->case_file_sha256, cc.case_file_sha256);
417 if (strcmp(cc.case_id, c->case_id)) findings_add(fs, "E4-BIND-CASE-ID", "embedded case_id differs from the supplied case file");
418 if (strcmp(cc.acceptance_id, c->acceptance_id)) findings_add(fs, "E4-BIND-ACCEPTANCE-ID", "embedded acceptance_id differs from the supplied case file");
419 if (cc.sem_len != c->sem_len || memcmp(cc.sem_block, c->sem_block, c->sem_len)) findings_add(fs, "E4-BIND-SEMANTIC-BLOCK", "embedded semantic block is not byte-identical to the supplied case");
420 if (cc.acc_len != c->acc_len || memcmp(cc.acc_block, c->acc_block, c->acc_len)) findings_add(fs, "E4-BIND-ACCEPTANCE-BLOCK", "embedded acceptance block is not byte-identical to the supplied case");
421 if (strcmp(cc.case_name, c->case_name)) findings_add(fs, "E4-BIND-CASE-NAME", "case_name differs from the supplied case file");
422 }
423 case_free(&cc);
424 }
425 /* provenance */
426 { int known = 0; for (int i = 0; at0e_contract_commits[i]; i++) if (!strcmp(r->contract_commit, at0e_contract_commits[i])) known = 1;
427 if (!known) { findings_add(fs, "E4-PROV-CONTRACT-UNVERIFIED", "contract_commit %s is not in the verified allowlist at0e_contract_commits (src/at0e_result.c); verify its contract-file digests and add it, or treat this record as not citable", r->contract_commit); inconclusive = 1; } }
428 if (!r->source_tree_clean) { findings_add(fs, "E4-PROV-DIRTY", "source_tree_clean NO: result may not be cited for a gate (AT0_RESULT_V1 section 7)"); inconclusive = 1; }
429 int placeholder = r->outcome == OUT_ERROR || r->outcome == OUT_NOT_RUN;
430 if (!placeholder) {
431 if (!strcmp(r->engine_sha256, "none") || !strcmp(r->oracle_sha256, "none") || !strcmp(r->run_started, "none") || !strcmp(r->run_finished, "none"))
432 findings_add(fs, "E4-PROV-NONE-IN-COMPLETED-RUN", "a completed run carries a `none` provenance token");
433 if (strcmp(r->run_started, "none") && strcmp(r->run_finished, "none") && strcmp(r->run_finished, r->run_started) < 0)
434 findings_add(fs, "E4-TIME-ORDER", "run_finished_utc %s precedes run_started_utc %s", r->run_finished, r->run_started);
435 if (!strcmp(r->engine_sha256, r->oracle_sha256) && strcmp(r->engine_sha256, "none")) findings_add(fs, "E4-PROV-ORACLE-IS-ENGINE", "engine_sha256 equals oracle_sha256: reference values did not come from a separate executable");
436 } else {
437 int bad = r->kernel_dim != 0 || r->n_failure_codes != 0 || r->expectation_met != 2;
438 for (int i = 0; i < AT0E_NCHECKS; i++) if (r->check[i] != CK_NOTEVAL) bad = 1;
439 if (!r->constraint_residual.is_undefined || !r->povm_residual.is_undefined) bad = 1;
440 for (int k = 0; k < c->M; k++) { if (!r->clock_p[k].is_undefined) bad = 1; for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) if (!r->pauli[k][a][s].is_undefined || !r->reference[k][a][s].is_undefined) bad = 1; }
441 if (r->outcome == OUT_ERROR && !strcmp(r->error_code, "none")) bad = 1;
442 if (r->outcome == OUT_NOT_RUN && strcmp(r->error_code, "none")) bad = 1;
443 if (bad) findings_add(fs, "E4-PLACEHOLDER-SHAPE", "ERROR/NOT_RUN result violates the placeholder shape of AT0_RESULT_V1 section 5");
444 return fs->n ? (is_fail_code(fs->f[0].code) ? "FAIL" : "INCONCLUSIVE") : "PASS";
445 }
446 if (strcmp(r->error_code, "none")) findings_add(fs, "E4-STRUCT-ERROR-CODE", "completed run carries error_code %s", r->error_code);
447 /* label status and token discipline */
448 {
449 shadow sx; const char *wx; int triv = shadow_compute(c, &sx, &wx) ? !(sx.kernel_dim >= 1 && sx.psi_nonzero) : 0;
450 if (!triv) {
451 int u = r->constraint_residual.is_undefined || r->povm_residual.is_undefined;
452 for (int k = 0; k < c->M; k++) { u |= r->clock_p[k].is_undefined; for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) u |= r->reference[k][a][s].is_undefined; } /* pauli undefined is governed by label status (V2 section 1) */
453 if (u) findings_add(fs, "E4-STRUCT-UNDEFINED-TOKEN", "undefined token on constraint_residual, povm_residual, clock_probability or reference in a result whose kernel is nontrivial (refusal under AT0_RESULT_V2 section 2)");
454 } else {
455 int bad = !r->constraint_residual.is_undefined || r->povm_residual.is_undefined;
456 for (int k = 0; k < c->M; k++) { bad |= !r->clock_p[k].is_undefined || r->label_status[k] != LS_UNDEFINED; for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) bad |= !r->pauli[k][a][s].is_undefined || r->reference[k][a][s].is_undefined; }
457 if (bad) findings_add(fs, "E4-STRUCT-TRIVIAL-KERNEL-SHAPE", "trivial kernel: constraint_residual, clock probabilities and pauli values must be undefined, labels UNDEFINED, povm_residual and references real");
458 }
459 }
460 for (int k = 0; k < c->M; k++) {
461 int want = label_status_derive(&r->clock_p[k], c->tol_zero);
462 if (want != r->label_status[k]) findings_add(fs, "E4-LABEL-STATUS", "label %d status does not follow from clock_probability and tol_zero_probability", k);
463 for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) {
464 int u = r->pauli[k][a][s].is_undefined;
465 if (r->label_status[k] == LS_DEFINED && u) findings_add(fs, "E4-STRUCT-UNDEFINED-ON-DEFINED", "label %d is DEFINED but pauli %s %s is undefined", k, axis_names[a], sign_names[s]);
466 if (r->label_status[k] != LS_DEFINED && !u) findings_add(fs, "E4-STRUCT-VALUE-ON-UNDEFINED", "label %d is not DEFINED but pauli %s %s carries a value", k, axis_names[a], sign_names[s]);
467 if (r->reference[k][a][s].is_undefined) findings_add(fs, "E4-STRUCT-REFERENCE-UNDEFINED", "reference %d %s %s is undefined; references are always written", k, axis_names[a], sign_names[s]);
468 }
469 }
470 if (r->bound_kind == BK_NONE) {
471 int nz = r->constraint_residual.bound.n || r->povm_residual.bound.n;
472 for (int k = 0; k < c->M; k++) { nz |= r->clock_p[k].bound.n != 0; for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) nz |= (r->pauli[k][a][s].bound.n != 0) | (r->reference[k][a][s].bound.n != 0); }
473 if (nz) findings_add(fs, "E4-BOUND-NONE-NONZERO", "bound_kind NONE but a nonzero bound is written");
474 }
475 /* re-derivation */
476 rederived d; result_rederive(r, &d);
477 for (int i = 0; i < AT0E_NCHECKS; i++) if (d.check[i] != r->check[i]) {
478 static const char *const nm[] = { "PASS", "FAIL", "INDETERMINATE", "NOT_EVALUATED" };
479 findings_add(fs, "E4-CHECK-MISMATCH", "check %s reported %s, re-derived %s", at0e_check_names[i], nm[r->check[i]], nm[d.check[i]]);
480 }
481 if (d.outcome != r->outcome) findings_add(fs, "E4-OUTCOME-MISMATCH", "outcome reported %s, re-derived %s", r->outcome == OUT_PASS ? "PASS" : "FAIL", d.outcome == OUT_PASS ? "PASS" : "FAIL");
482 {
483 int same = d.n_failure_codes == r->n_failure_codes;
484 for (int i = 0; same && i < d.n_failure_codes; i++) if (strcmp(d.failure_codes[i], r->failure_codes[i])) same = 0;
485 if (!same) findings_add(fs, "E4-CODES-MISMATCH", "failure_codes differ from the re-derived set (%d reported, %d re-derived)", r->n_failure_codes, d.n_failure_codes);
486 }
487 if (d.expectation_met != r->expectation_met) findings_add(fs, "E4-EXPECTATION-MISMATCH", "expectation_met reported %s, re-derived %s", r->expectation_met == 0 ? "YES" : r->expectation_met == 1 ? "NO" : "NOT_APPLICABLE", d.expectation_met ? "NO" : "YES");
488 /* shadow */
489 if (use_shadow) {
490 shadow sh; const char *why;
491 if (!shadow_compute(c, &sh, &why)) { findings_add(fs, "E4-SHADOW-UNAVAILABLE", "%s", why); inconclusive = 1; }
492 else {
493 if (sh.kernel_dim != r->kernel_dim) findings_add(fs, "E4-SHADOW-KERNEL-DIM", "physical_state_kernel_dim %ld, exact value %d", r->kernel_dim, sh.kernel_dim);
494 int nontrivial = sh.kernel_dim >= 1 && sh.psi_nonzero;
495 if (nontrivial && !r->constraint_residual.is_undefined && !r->constraint_residual.is_nonfinite) {
496 long double v = fabsl(f64_ld(r->constraint_residual.bits)), b = scaled_ld(r->constraint_residual.bound);
497 if (v > b) findings_add(fs, "E4-BOUND-CLAIM-FALSE", "constraint_residual %.3Le exceeds its own bound %.3Le although the exact residual of a kernel projection is 0", v, b);
498 }
499 if (!nontrivial && !r->constraint_residual.is_undefined) findings_add(fs, "E4-STRUCT-RESIDUAL-ON-TRIVIAL", "constraint_residual written although the kernel is trivial");
500 if (!r->povm_residual.is_undefined && !r->povm_residual.is_nonfinite) {
501 long double v = fabsl(f64_ld(r->povm_residual.bits)), b = scaled_ld(r->povm_residual.bound);
502 if (sh.povm_exact_identity) { if (v > b) findings_add(fs, "E4-BOUND-CLAIM-FALSE", "povm_residual %.3Le exceeds its bound %.3Le although sum F_k = I exactly", v, b); }
503 if (fabsl(v - sh.povm_residual) > b + sh.bound) findings_add(fs, "E4-SHADOW-POVM", "povm_residual %.6Le vs shadow %.6Le beyond bound %.1Le", v, sh.povm_residual, b);
504 } else if (!r->povm_residual.is_nonfinite) findings_add(fs, "E4-STRUCT-POVM-UNDEFINED", "povm_residual is undefined; it is always computable");
505 int n_clock = 0, n_pauli = 0, n_ref = 0;
506 for (int k = 0; k < c->M; k++) {
507 if (nontrivial && !r->clock_p[k].is_undefined && !r->clock_p[k].is_nonfinite) {
508 long double v = f64_ld(r->clock_p[k].bits), b = scaled_ld(r->clock_p[k].bound);
509 if (fabsl(v - sh.clock_p[k]) > b + sh.bound) { if (n_clock++ < 3) findings_add(fs, "E4-SHADOW-CLOCK-PROB", "clock_probability %d %.9Le vs shadow %.9Le beyond bound %.1Le", k, v, sh.clock_p[k], b); }
510 }
511 for (int a = 0; a < 3; a++) for (int s = 0; s < 2; s++) {
512 if (r->label_status[k] == LS_DEFINED && nontrivial && !r->pauli[k][a][s].is_undefined && !r->pauli[k][a][s].is_nonfinite) {
513 long double v = f64_ld(r->pauli[k][a][s].bits), b = scaled_ld(r->pauli[k][a][s].bound);
514 if (fabsl(v - sh.pauli[k][a][s]) > b + sh.bound) { if (n_pauli++ < 6) findings_add(fs, "E4-SHADOW-PAULI", "pauli %d %s %s %.9Le vs shadow %.9Le beyond bound %.1Le", k, axis_names[a], sign_names[s], v, sh.pauli[k][a][s], b); }
515 }
516 if (!r->reference[k][a][s].is_undefined && !r->reference[k][a][s].is_nonfinite) {
517 long double v = f64_ld(r->reference[k][a][s].bits), b = scaled_ld(r->reference[k][a][s].bound);
518 if (fabsl(v - sh.reference[k][a][s]) > b + sh.bound) { if (n_ref++ < 6) findings_add(fs, "E4-SHADOW-REFERENCE", "reference %d %s %s %.9Le vs shadow %.9Le beyond bound %.1Le", k, axis_names[a], sign_names[s], v, sh.reference[k][a][s], b); }
519 }
520 }
521 }
522 if (n_clock > 3) findings_add(fs, "E4-SHADOW-CLOCK-PROB", "... %d clock probabilities disagree in total", n_clock);
523 if (n_pauli > 6) findings_add(fs, "E4-SHADOW-PAULI", "... %d pauli values disagree in total", n_pauli);
524 if (n_ref > 6) findings_add(fs, "E4-SHADOW-REFERENCE", "... %d reference values disagree in total", n_ref);
525 }
526 } else inconclusive = 1;
527 for (int i = 0; i < fs->n; i++) if (is_fail_code(fs->f[i].code)) return "FAIL";
528 return inconclusive ? "INCONCLUSIVE" : "PASS";
529}