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
omega-mv / research / atemporal / at0 / integration / at0_assemble.c
| 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 | /* AT-0 Agent 5: result assembler and exact judge (runner side, C11, -lm not needed). |
| 18 | * |
| 19 | * Joins one engine component output (OMEGA-AT0-ENGINE v1) with the `reference` lines of one |
| 20 | * oracle record, judges the ten AT0_RESULT_V2 section 4 checks in exact integer arithmetic, |
| 21 | * and writes one complete AT0_RESULT_V2 file (verdict block, verdict_id, provenance, |
| 22 | * evidence_digest) to standard output. Shares no code with model/, oracle/ or evaluator/ |
| 23 | * except omega src/sha256.c (charter section 3). |
| 24 | * |
| 25 | * at0-assemble assemble <case> <engine-out> <oracle-record> <provenance-lines> |
| 26 | * at0-assemble judge <case> <values-file> (calibration: prints verdict block + verdict_id) |
| 27 | * |
| 28 | * Exact arithmetic: every binary64 and every scaled decimal is lifted to an integer multiple of |
| 29 | * the unit 2^-1074 * 10^-40 (all binary64 exponents >= -1074, all scaled decimals have k <= 40), |
| 30 | * held in a 2304-bit two's complement integer. Nothing is ever compared as a float. |
| 31 | * No clamping: the largest binary64 lifted to these units needs about 2230 bits and the sums in section 4 |
| 32 | * add about 12 bits, so everything fits the 2304-bit signed integer. */ |
| 33 | #include <ctype.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include "sha256.h" |
| 39 | |
| 40 | #define NL 72 |
| 41 | typedef struct { uint32_t w[NL]; } Big; |
| 42 | |
| 43 | static void b_zero(Big *a) { memset(a, 0, sizeof *a); } |
| 44 | static int b_isneg(const Big *a) { return (int)(a->w[NL - 1] >> 31); } |
| 45 | static void b_add(Big *r, const Big *a, const Big *b) |
| 46 | { |
| 47 | uint64_t c = 0; |
| 48 | for (int i = 0; i < NL; i++) { c += (uint64_t)a->w[i] + b->w[i]; r->w[i] = (uint32_t)c; c >>= 32; } |
| 49 | } |
| 50 | static void b_neg(Big *r, const Big *a) |
| 51 | { |
| 52 | uint64_t c = 1; |
| 53 | for (int i = 0; i < NL; i++) { c += (uint64_t)(uint32_t)~a->w[i]; r->w[i] = (uint32_t)c; c >>= 32; } |
| 54 | } |
| 55 | static void b_sub(Big *r, const Big *a, const Big *b) { Big n; b_neg(&n, b); b_add(r, a, &n); } |
| 56 | static int b_cmp(const Big *a, const Big *b) |
| 57 | { |
| 58 | int na = b_isneg(a), nb = b_isneg(b); |
| 59 | if (na != nb) return na ? -1 : 1; |
| 60 | for (int i = NL - 1; i >= 0; i--) if (a->w[i] != b->w[i]) return a->w[i] < b->w[i] ? -1 : 1; |
| 61 | return 0; |
| 62 | } |
| 63 | static void b_abs(Big *r, const Big *a) { if (b_isneg(a)) b_neg(r, a); else *r = *a; } |
| 64 | static void b_mul_small(Big *a, uint32_t m) |
| 65 | { |
| 66 | uint64_t c = 0; |
| 67 | for (int i = 0; i < NL; i++) { c += (uint64_t)a->w[i] * m; a->w[i] = (uint32_t)c; c >>= 32; } |
| 68 | } |
| 69 | static void b_add_small(Big *a, uint32_t m) |
| 70 | { |
| 71 | uint64_t c = m; |
| 72 | for (int i = 0; i < NL && c; i++) { c += a->w[i]; a->w[i] = (uint32_t)c; c >>= 32; } |
| 73 | } |
| 74 | static void b_shl(Big *a, unsigned n) |
| 75 | { |
| 76 | unsigned ws = n / 32, bs = n % 32; |
| 77 | for (int i = NL - 1; i >= 0; i--) { |
| 78 | uint64_t v = 0; |
| 79 | if ((int)(i - ws) >= 0) v = (uint64_t)a->w[i - ws] << bs; |
| 80 | if (bs && (int)(i - ws - 1) >= 0) v |= (uint64_t)a->w[i - ws - 1] >> (32 - bs); |
| 81 | a->w[i] = (uint32_t)v; |
| 82 | } |
| 83 | } |
| 84 | static void b_pow10(Big *a, int n) { while (n >= 9) { b_mul_small(a, 1000000000u); n -= 9; } while (n-- > 0) b_mul_small(a, 10); } |
| 85 | |
| 86 | /* value kinds */ |
| 87 | enum { V_REAL = 0, V_NONFINITE = 1, V_UNDEFINED = 2, V_BAD = 3 }; |
| 88 | typedef struct { int kind; Big v; Big b; } Val; |
| 89 | |
| 90 | static Big g_one; /* the integer 1 in units */ |
| 91 | static char g_casesha[65]; |
| 92 | |
| 93 | static void from_f64(Big *r, uint64_t bits) |
| 94 | { |
| 95 | int neg = (int)(bits >> 63), ex = (int)((bits >> 52) & 0x7ff); |
| 96 | uint64_t fr = bits & 0xfffffffffffffULL, mant = ex ? (fr | (1ULL << 52)) : fr; |
| 97 | int e = ex ? ex - 1075 : -1074, shift = e + 1074; |
| 98 | b_zero(r); |
| 99 | r->w[0] = (uint32_t)mant; r->w[1] = (uint32_t)(mant >> 32); |
| 100 | b_shl(r, (unsigned)shift); |
| 101 | b_pow10(r, 40); |
| 102 | if (neg) b_neg(r, r); |
| 103 | } |
| 104 | /* "N@k": N decimal digits, 0 <= k <= 40. Returns 0 on success. */ |
| 105 | static int from_scaled(Big *r, const char *s) |
| 106 | { |
| 107 | const char *at = strchr(s, '@'); |
| 108 | if (!at || at == s || at - s > 60) return -1; |
| 109 | char *end; long k = strtol(at + 1, &end, 10); |
| 110 | if (*end || k < 0 || k > 40) return -1; |
| 111 | b_zero(r); |
| 112 | for (const char *p = s; p < at; p++) { |
| 113 | if (!isdigit((unsigned char)*p)) return -1; |
| 114 | b_mul_small(r, 10); b_add_small(r, (uint32_t)(*p - '0')); |
| 115 | } |
| 116 | b_pow10(r, (int)(40 - k)); |
| 117 | b_shl(r, 1074); |
| 118 | return 0; |
| 119 | } |
| 120 | static int parse_value(Val *x, const char *vt, const char *bt) |
| 121 | { |
| 122 | memset(x, 0, sizeof *x); |
| 123 | if (!strcmp(vt, "nonfinite")) x->kind = V_NONFINITE; |
| 124 | else if (!strcmp(vt, "undefined")) x->kind = V_UNDEFINED; |
| 125 | else if (!strncmp(vt, "f64:", 4) && strlen(vt) == 20) { |
| 126 | uint64_t bits = 0; |
| 127 | for (int i = 4; i < 20; i++) { |
| 128 | char c = vt[i]; int d = (c >= '0' && c <= '9') ? c - '0' : (c >= 'a' && c <= 'f') ? c - 'a' + 10 : -1; |
| 129 | if (d < 0) return -1; |
| 130 | bits = (bits << 4) | (uint64_t)d; |
| 131 | } |
| 132 | if (((bits >> 52) & 0x7ff) == 0x7ff) return -1; |
| 133 | from_f64(&x->v, bits); x->kind = V_REAL; |
| 134 | } else return -1; |
| 135 | if (from_scaled(&x->b, bt)) return -1; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* tri-state comparisons, section 4 */ |
| 140 | enum { C_PASS = 0, C_FAIL = 1, C_INDET = 2, C_NE = 3 }; |
| 141 | static int absform(const Big *v, const Big *b, const Big *tol) |
| 142 | { |
| 143 | Big a, t; b_abs(&a, v); |
| 144 | b_add(&t, &a, b); if (b_cmp(&t, tol) <= 0) return C_PASS; |
| 145 | b_sub(&t, &a, b); if (b_cmp(&t, tol) > 0) return C_FAIL; |
| 146 | return C_INDET; |
| 147 | } |
| 148 | static int signedform(const Big *v, const Big *b, const Big *tol) |
| 149 | { |
| 150 | Big t; |
| 151 | b_add(&t, v, b); if (b_cmp(&t, tol) <= 0) return C_PASS; |
| 152 | b_sub(&t, v, b); if (b_cmp(&t, tol) > 0) return C_FAIL; |
| 153 | return C_INDET; |
| 154 | } |
| 155 | typedef struct { int seen, fail, indet; } Agg; |
| 156 | static void agg_add(Agg *g, int c) { g->seen = 1; if (c == C_FAIL) g->fail = 1; else if (c == C_INDET) g->indet = 1; } |
| 157 | static int agg_res(const Agg *g) { return !g->seen ? C_NE : g->fail ? C_FAIL : g->indet ? C_INDET : C_PASS; } |
| 158 | |
| 159 | /* ---------- text helpers ---------- */ |
| 160 | typedef struct { char *data; size_t len; char **line; int n; } Text; |
| 161 | static int read_file(const char *path, Text *t) |
| 162 | { |
| 163 | FILE *f = fopen(path, "rb"); if (!f) return -1; |
| 164 | size_t cap = 1 << 16; t->data = malloc(cap + 1); t->len = 0; |
| 165 | for (;;) { |
| 166 | if (t->len == cap) { cap *= 2; t->data = realloc(t->data, cap + 1); } |
| 167 | size_t k = fread(t->data + t->len, 1, cap - t->len, f); if (!k) break; t->len += k; |
| 168 | } |
| 169 | fclose(f); t->data[t->len] = 0; |
| 170 | int n = 0; for (size_t i = 0; i < t->len; i++) if (t->data[i] == '\n') n++; |
| 171 | t->line = malloc(sizeof(char *) * (size_t)(n + 2)); t->n = 0; |
| 172 | char *p = t->data; |
| 173 | for (size_t i = 0; i < t->len; i++) if (t->data[i] == '\n') { t->data[i] = 0; t->line[t->n++] = p; p = t->data + i + 1; } |
| 174 | if (*p) t->line[t->n++] = p; |
| 175 | return 0; |
| 176 | } |
| 177 | static int tokens(const char *line, char tok[][96], int max) |
| 178 | { |
| 179 | int n = 0; const char *p = line; |
| 180 | while (*p && n < max) { |
| 181 | while (*p == ' ') p++; |
| 182 | if (!*p) break; |
| 183 | int k = 0; while (*p && *p != ' ') { if (k < 95) tok[n][k++] = *p; p++; } |
| 184 | tok[n][k] = 0; n++; |
| 185 | } |
| 186 | return n; |
| 187 | } |
| 188 | static int find_line(const Text *t, const char *s, int from) |
| 189 | { |
| 190 | for (int i = from; i < t->n; i++) if (!strcmp(t->line[i], s)) return i; |
| 191 | return -1; |
| 192 | } |
| 193 | static const char *key_value(const Text *t, int from, int to, const char *key) |
| 194 | { |
| 195 | size_t kl = strlen(key); |
| 196 | for (int i = from; i < to && i < t->n; i++) |
| 197 | if (!strncmp(t->line[i], key, kl) && t->line[i][kl] == ' ') return t->line[i] + kl + 1; |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | /* ---------- values ---------- */ |
| 202 | #define MAXM 256 |
| 203 | typedef struct { |
| 204 | int kernel_dim, M; |
| 205 | Val constraint, povm; |
| 206 | int status[MAXM]; /* 0 DEFINED 1 UNDEFINED 2 INDETERMINATE */ |
| 207 | Val clock[MAXM], pauli[MAXM][6], ref[MAXM][6]; |
| 208 | int have_ref; |
| 209 | } Values; |
| 210 | static int axis_index(const char *a, const char *s) |
| 211 | { |
| 212 | int ai = !strcmp(a, "X") ? 0 : !strcmp(a, "Y") ? 1 : !strcmp(a, "Z") ? 2 : -1; |
| 213 | int si = !strcmp(s, "PLUS") ? 0 : !strcmp(s, "MINUS") ? 1 : -1; |
| 214 | return (ai < 0 || si < 0) ? -1 : ai * 2 + si; |
| 215 | } |
| 216 | /* lines[from..to) are the lines strictly between "begin values" and "end values" */ |
| 217 | static int parse_values(char **lines, int from, int to, Values *V, char *err, size_t ecap) |
| 218 | { |
| 219 | memset(V, 0, sizeof *V); V->kernel_dim = -1; |
| 220 | int nlabel = 0, nclock = 0, npauli = 0, nref = 0, hk = 0, hc = 0, hp = 0; |
| 221 | char t[8][96]; |
| 222 | for (int i = from; i < to; i++) { |
| 223 | int n = tokens(lines[i], t, 8); |
| 224 | if (n < 1) continue; |
| 225 | if (!strcmp(t[0], "physical_state_kernel_dim") && n == 2) { char *e; long kd = strtol(t[1], &e, 10); if (hk++ || *e || kd < 0 || kd > 4096) goto bad; V->kernel_dim = (int)kd; } |
| 226 | else if (!strcmp(t[0], "constraint_residual") && n == 3) { if (hc++ || parse_value(&V->constraint, t[1], t[2])) goto bad; } |
| 227 | else if (!strcmp(t[0], "povm_residual") && n == 3) { if (hp++ || parse_value(&V->povm, t[1], t[2])) goto bad; } |
| 228 | else if (!strcmp(t[0], "label") && n == 4) { |
| 229 | int k = atoi(t[1]); if (k != nlabel || k >= MAXM) goto bad; |
| 230 | V->status[k] = !strcmp(t[3], "DEFINED") ? 0 : !strcmp(t[3], "UNDEFINED") ? 1 : !strcmp(t[3], "INDETERMINATE") ? 2 : -1; |
| 231 | if (V->status[k] < 0) goto bad; |
| 232 | nlabel++; |
| 233 | } else if (!strcmp(t[0], "clock_probability") && n == 4) { |
| 234 | int k = atoi(t[1]); if (k != nclock || k >= MAXM || parse_value(&V->clock[k], t[2], t[3])) goto bad; |
| 235 | nclock++; |
| 236 | } else if ((!strcmp(t[0], "pauli") || !strcmp(t[0], "reference")) && n == 6) { |
| 237 | int k = atoi(t[1]), s = axis_index(t[2], t[3]); int isref = t[0][0] == 'r'; |
| 238 | int *cnt = isref ? &nref : &npauli; |
| 239 | if (s < 0 || k >= MAXM || k * 6 + s != *cnt) goto bad; |
| 240 | if (parse_value(isref ? &V->ref[k][s] : &V->pauli[k][s], t[4], t[5])) goto bad; |
| 241 | (*cnt)++; |
| 242 | } else goto bad; |
| 243 | continue; |
| 244 | bad: |
| 245 | snprintf(err, ecap, "unparseable or out-of-order values line: %.80s", lines[i]); return -1; |
| 246 | } |
| 247 | V->M = nlabel; |
| 248 | if (!hk || !hc || !hp || V->kernel_dim < 0 || nclock != nlabel || npauli != 6 * nlabel || (nref != 0 && nref != 6 * nlabel)) { |
| 249 | snprintf(err, ecap, "values block counts: labels %d clock %d pauli %d reference %d", nlabel, nclock, npauli, nref); return -1; |
| 250 | } |
| 251 | V->have_ref = nref != 0; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* ---------- the judge ---------- */ |
| 256 | typedef struct { |
| 257 | Big tol_con, tol_povm, tol_prob, tol_zero, tol_schro; |
| 258 | int min_kind; /* 0 NONE 1 ESTIMATED 2 RIGOROUS */ |
| 259 | char expected_outcome[16], expected_codes[512]; |
| 260 | } Accept; |
| 261 | static const char *CHECKS[10] = { "bound_kind_sufficient", "values_finite", "physical_state_nontrivial", "constraint_residual", |
| 262 | "povm_normalization", "clock_probability_sum", "probability_range", "pauli_pair_sum", "conditional_defined", "schrodinger_agreement" }; |
| 263 | static const char *CODES[10] = { "BOUND_KIND_INSUFFICIENT", "CONDITIONAL_UNDEFINED", "CONSTRAINT_RESIDUAL_EXCEEDED", "NONFINITE_VALUE", |
| 264 | "POVM_NORMALIZATION_EXCEEDED", "PRECISION_INSUFFICIENT", "PROBABILITY_OUT_OF_RANGE", "PROBABILITY_SUM_EXCEEDED", |
| 265 | "SCHRODINGER_DEVIATION_EXCEEDED", "TRIVIAL_PHYSICAL_STATE" }; /* bytewise sorted */ |
| 266 | enum { K_BOUND, K_COND, K_CON, K_NONFIN, K_POVM, K_PREC, K_RANGE, K_SUM, K_SCHRO, K_TRIV }; |
| 267 | static const char *STATE[4] = { "PASS", "FAIL", "INDETERMINATE", "NOT_EVALUATED" }; |
| 268 | |
| 269 | static int is_fin(const Val *x) { return x->kind == V_REAL; } |
| 270 | |
| 271 | static void judge(const Values *V, int bound_kind, const Accept *A, int res[10], unsigned *codes, int *label_indet) |
| 272 | { |
| 273 | int trivial = V->kernel_dim == 0 || V->constraint.kind == V_UNDEFINED; |
| 274 | int M = V->M; Agg g; |
| 275 | *codes = 0; *label_indet = 0; |
| 276 | for (int k = 0; k < M; k++) if (V->status[k] == 2) *label_indet = 1; |
| 277 | res[0] = bound_kind >= A->min_kind ? C_PASS : C_FAIL; |
| 278 | int nonfin = V->constraint.kind == V_NONFINITE || V->povm.kind == V_NONFINITE; |
| 279 | for (int k = 0; k < M; k++) { |
| 280 | if (V->clock[k].kind == V_NONFINITE) nonfin = 1; |
| 281 | for (int s = 0; s < 6; s++) if (V->pauli[k][s].kind == V_NONFINITE || V->ref[k][s].kind == V_NONFINITE) nonfin = 1; |
| 282 | } |
| 283 | res[1] = nonfin ? C_FAIL : C_PASS; |
| 284 | res[2] = trivial ? C_FAIL : C_PASS; |
| 285 | memset(&g, 0, sizeof g); |
| 286 | if (!trivial && is_fin(&V->constraint)) agg_add(&g, absform(&V->constraint.v, &V->constraint.b, &A->tol_con)); |
| 287 | res[3] = trivial ? C_NE : agg_res(&g); |
| 288 | memset(&g, 0, sizeof g); |
| 289 | if (is_fin(&V->povm)) agg_add(&g, absform(&V->povm.v, &V->povm.b, &A->tol_povm)); |
| 290 | res[4] = agg_res(&g); |
| 291 | /* 6: sum of clock probabilities */ |
| 292 | memset(&g, 0, sizeof g); |
| 293 | if (!trivial) { |
| 294 | Big sv, sb, d; int all = 1; b_zero(&sv); b_zero(&sb); |
| 295 | for (int k = 0; k < M; k++) { if (!is_fin(&V->clock[k])) { all = 0; break; } b_add(&sv, &sv, &V->clock[k].v); b_add(&sb, &sb, &V->clock[k].b); } |
| 296 | if (all && M > 0) { b_sub(&d, &sv, &g_one); agg_add(&g, absform(&d, &sb, &A->tol_prob)); } |
| 297 | } |
| 298 | res[5] = trivial ? C_NE : agg_res(&g); |
| 299 | /* 7: every written probability, signed form both ways (clock, pauli, reference) */ |
| 300 | memset(&g, 0, sizeof g); |
| 301 | if (!trivial) { |
| 302 | for (int k = 0; k < M; k++) { |
| 303 | const Val *p[13]; int np = 0; |
| 304 | p[np++] = &V->clock[k]; |
| 305 | for (int s = 0; s < 6; s++) { p[np++] = &V->pauli[k][s]; p[np++] = &V->ref[k][s]; } |
| 306 | for (int i = 0; i < np; i++) { |
| 307 | if (!is_fin(p[i])) continue; |
| 308 | Big neg, over; b_neg(&neg, &p[i]->v); b_sub(&over, &p[i]->v, &g_one); |
| 309 | agg_add(&g, signedform(&neg, &p[i]->b, &A->tol_prob)); |
| 310 | agg_add(&g, signedform(&over, &p[i]->b, &A->tol_prob)); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | res[6] = trivial ? C_NE : agg_res(&g); |
| 315 | /* 8: PLUS + MINUS - 1 per defined label and axis */ |
| 316 | memset(&g, 0, sizeof g); |
| 317 | if (!trivial) |
| 318 | for (int k = 0; k < M; k++) { |
| 319 | if (V->status[k] != 0) continue; |
| 320 | for (int a = 0; a < 3; a++) { |
| 321 | const Val *pl = &V->pauli[k][2 * a], *mi = &V->pauli[k][2 * a + 1]; |
| 322 | if (!is_fin(pl) || !is_fin(mi)) continue; |
| 323 | Big s, d, b; b_add(&s, &pl->v, &mi->v); b_sub(&d, &s, &g_one); b_add(&b, &pl->b, &mi->b); |
| 324 | agg_add(&g, absform(&d, &b, &A->tol_prob)); |
| 325 | } |
| 326 | } |
| 327 | res[7] = trivial ? C_NE : agg_res(&g); |
| 328 | /* 9 */ |
| 329 | if (trivial) res[8] = C_NE; |
| 330 | else { res[8] = C_PASS; for (int k = 0; k < M; k++) if (V->status[k] == 1) res[8] = C_FAIL; } |
| 331 | /* 10 */ |
| 332 | memset(&g, 0, sizeof g); |
| 333 | if (!trivial) |
| 334 | for (int k = 0; k < M; k++) { |
| 335 | if (V->status[k] != 0) continue; |
| 336 | for (int s = 0; s < 6; s++) { |
| 337 | const Val *p = &V->pauli[k][s], *r = &V->ref[k][s]; |
| 338 | if (!is_fin(p) || !is_fin(r)) continue; |
| 339 | Big d, b; b_sub(&d, &p->v, &r->v); b_add(&b, &p->b, &r->b); |
| 340 | agg_add(&g, absform(&d, &b, &A->tol_schro)); |
| 341 | } |
| 342 | } |
| 343 | res[9] = trivial ? C_NE : agg_res(&g); |
| 344 | static const int code_of[10] = { K_BOUND, K_NONFIN, K_TRIV, K_CON, K_POVM, K_SUM, K_RANGE, K_SUM, K_COND, K_SCHRO }; |
| 345 | int indet = *label_indet; |
| 346 | for (int i = 0; i < 10; i++) { if (res[i] == C_FAIL) *codes |= 1u << code_of[i]; if (res[i] == C_INDET) indet = 1; } |
| 347 | if (indet) *codes |= 1u << K_PREC; |
| 348 | } |
| 349 | |
| 350 | /* label status derived from written clock probability and tol_zero_probability (section 3) */ |
| 351 | static int derive_status(const Val *c, const Big *tol_zero) |
| 352 | { |
| 353 | Big t; |
| 354 | b_add(&t, &c->v, &c->b); if (b_cmp(&t, tol_zero) <= 0) return 1; |
| 355 | b_sub(&t, &c->v, &c->b); if (b_cmp(&t, tol_zero) > 0) return 0; |
| 356 | return 2; |
| 357 | } |
| 358 | |
| 359 | /* ---------- output buffer ---------- */ |
| 360 | typedef struct { char *p; size_t n, cap; } Buf; |
| 361 | static void bput(Buf *b, const char *s) |
| 362 | { |
| 363 | size_t k = strlen(s); |
| 364 | if (b->n + k + 1 > b->cap) { b->cap = (b->n + k + 1) * 2; b->p = realloc(b->p, b->cap); } |
| 365 | memcpy(b->p + b->n, s, k + 1); b->n += k; |
| 366 | } |
| 367 | static void hex(const uint8_t *d, char *out) { for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", d[i]); } |
| 368 | static void sha_tagged(const char *tag, const char *pre, const char *body, size_t blen, char out[65]) |
| 369 | { |
| 370 | sha256_ctx c; uint8_t d[32]; sha256_init(&c); |
| 371 | sha256_update(&c, (const uint8_t *)tag, strlen(tag)); uint8_t z = 0; sha256_update(&c, &z, 1); |
| 372 | if (pre) sha256_update(&c, (const uint8_t *)pre, strlen(pre)); |
| 373 | sha256_update(&c, (const uint8_t *)body, blen); |
| 374 | sha256_final(&c, d); hex(d, out); |
| 375 | } |
| 376 | |
| 377 | static int die(const char *m, const char *a) { fprintf(stderr, "AT0_ASSEMBLE_ERROR %s%s%s\n", m, a ? " " : "", a ? a : ""); return 3; } |
| 378 | |
| 379 | static int load_case(const char *path, Text *c, Accept *A, int *sem_a, int *sem_b, int *acc_a, int *acc_b) |
| 380 | { |
| 381 | if (read_file(path, c)) return die("cannot read case", path); |
| 382 | *sem_a = find_line(c, "begin semantic", 0); *sem_b = find_line(c, "end semantic", 0); |
| 383 | *acc_a = find_line(c, "begin acceptance", 0); *acc_b = find_line(c, "end acceptance", 0); |
| 384 | if (*sem_a < 0 || *sem_b < *sem_a || *acc_a != *sem_b + 1 || *acc_b < *acc_a) return die("case blocks not found in", path); |
| 385 | const char *v; |
| 386 | if (!(v = key_value(c, *acc_a, *acc_b, "expected_outcome")) || strlen(v) > 15) return die("case lacks expected_outcome", NULL); |
| 387 | strcpy(A->expected_outcome, v); |
| 388 | if (!(v = key_value(c, *acc_a, *acc_b, "expected_failure_codes")) || strlen(v) > 500) return die("case lacks expected_failure_codes", NULL); |
| 389 | strcpy(A->expected_codes, v); |
| 390 | if (!(v = key_value(c, *acc_a, *acc_b, "min_bound_kind"))) return die("case lacks min_bound_kind", NULL); |
| 391 | A->min_kind = !strcmp(v, "NONE") ? 0 : !strcmp(v, "ESTIMATED") ? 1 : !strcmp(v, "RIGOROUS") ? 2 : -1; |
| 392 | if (A->min_kind < 0) return die("bad min_bound_kind", v); |
| 393 | static const char *keys[5] = { "tol_constraint_residual", "tol_povm_residual", "tol_probability", "tol_zero_probability", "tol_schrodinger" }; |
| 394 | Big *dst[5] = { &A->tol_con, &A->tol_povm, &A->tol_prob, &A->tol_zero, &A->tol_schro }; |
| 395 | for (int i = 0; i < 5; i++) { |
| 396 | if (!(v = key_value(c, *acc_a, *acc_b, keys[i])) || from_scaled(dst[i], v)) return die("case lacks or mangles", keys[i]); |
| 397 | } |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static void verdict_block(Buf *o, const int res[10], unsigned codes, int label_indet, const Accept *A, char *codes_txt) |
| 402 | { |
| 403 | int any_indet = label_indet; for (int i = 0; i < 10; i++) if (res[i] == C_INDET) any_indet = 1; |
| 404 | int outcome_pass = codes == 0 && !any_indet; |
| 405 | codes_txt[0] = 0; |
| 406 | for (int i = 0; i < 10; i++) if (codes & (1u << i)) { if (codes_txt[0]) strcat(codes_txt, ","); strcat(codes_txt, CODES[i]); } |
| 407 | if (!codes_txt[0]) strcpy(codes_txt, "none"); |
| 408 | int met = !strcmp(A->expected_outcome, outcome_pass ? "PASS" : "FAIL") && !strcmp(A->expected_codes, codes_txt); |
| 409 | bput(o, "begin verdict\n"); |
| 410 | for (int i = 0; i < 10; i++) { bput(o, "check "); bput(o, CHECKS[i]); bput(o, " "); bput(o, STATE[res[i]]); bput(o, "\n"); } |
| 411 | bput(o, "outcome "); bput(o, outcome_pass ? "PASS" : "FAIL"); bput(o, "\nfailure_codes "); bput(o, codes_txt); |
| 412 | bput(o, "\nerror_code none\nexpectation_met "); bput(o, met ? "YES" : "NO"); bput(o, "\nend verdict\n"); |
| 413 | } |
| 414 | |
| 415 | int main(int argc, char **argv) |
| 416 | { |
| 417 | memset(&g_one, 0, sizeof g_one); g_one.w[0] = 1; b_pow10(&g_one, 40); b_shl(&g_one, 1074); |
| 418 | if (argc >= 4 && !strcmp(argv[1], "judge") && argc == 4) { |
| 419 | Text c, v; Accept A; int sa, sb, aa, ab; char err[200]; |
| 420 | int rc = load_case(argv[2], &c, &A, &sa, &sb, &aa, &ab); if (rc) return rc; |
| 421 | if (read_file(argv[3], &v)) return die("cannot read", argv[3]); |
| 422 | int vb = find_line(&v, "begin values", 0), ve = find_line(&v, "end values", 0); |
| 423 | const char *bk = NULL; int nk = find_line(&v, "begin numerics", 0); |
| 424 | if (nk >= 0) bk = key_value(&v, nk, nk + 5, "bound_kind"); |
| 425 | if (vb < 0 || ve < vb) return die("no values block in", argv[3]); |
| 426 | Values *V = malloc(sizeof *V); |
| 427 | if (parse_values(v.line, vb + 1, ve, V, err, sizeof err)) return die(err, NULL); |
| 428 | int bkind = bk ? (!strcmp(bk, "RIGOROUS") ? 2 : !strcmp(bk, "ESTIMATED") ? 1 : 0) : 1; |
| 429 | int res[10]; unsigned codes; int li; char ct[600]; Buf o = { 0 }; bput(&o, ""); |
| 430 | judge(V, bkind, &A, res, &codes, &li); |
| 431 | verdict_block(&o, res, codes, li, &A, ct); |
| 432 | const char *cid = key_value(&c, 0, c.n, "case_id"), *aid = key_value(&c, 0, c.n, "acceptance_id"); |
| 433 | char pre[300]; snprintf(pre, sizeof pre, "case_id %s\nacceptance_id %s\n", cid, aid); |
| 434 | char vid[65]; sha_tagged("omega.at0.verdict.v2", pre, o.p, o.n, vid); |
| 435 | printf("%sverdict_id %s\n", o.p, vid); |
| 436 | return 0; |
| 437 | } |
| 438 | if (argc != 6 || strcmp(argv[1], "assemble")) { |
| 439 | fprintf(stderr, "usage: at0-assemble assemble <case> <engine-out> <oracle-record> <provenance-lines>\n at0-assemble judge <case> <values-file>\n"); |
| 440 | return 1; |
| 441 | } |
| 442 | Text c, e, r, pv; Accept A; int sa, sb, aa, ab; char err[200]; |
| 443 | int rc = load_case(argv[2], &c, &A, &sa, &sb, &aa, &ab); if (rc) return rc; |
| 444 | if (read_file(argv[3], &e)) return die("cannot read", argv[3]); |
| 445 | if (read_file(argv[4], &r)) return die("cannot read", argv[4]); |
| 446 | if (read_file(argv[5], &pv)) return die("cannot read", argv[5]); |
| 447 | /* case identity lines and file digest */ |
| 448 | const char *cname = key_value(&c, 0, 6, "case_name"); |
| 449 | const char *cid = key_value(&c, aa, c.n, "case_id"), *aid = key_value(&c, aa, c.n, "acceptance_id"); |
| 450 | if (!cname || !cid || !aid) return die("case lacks case_name/case_id/acceptance_id", NULL); |
| 451 | { FILE *f = fopen(argv[2], "rb"); sha256_ctx s; uint8_t d[32], b[4096]; size_t k; char h[65]; |
| 452 | sha256_init(&s); while ((k = fread(b, 1, sizeof b, f)) > 0) sha256_update(&s, b, k); fclose(f); sha256_final(&s, d); hex(d, h); |
| 453 | const char *ef = key_value(&e, 0, 12, "case_file_sha256"); |
| 454 | if (!ef || strcmp(ef, h)) return die("engine case_file_sha256 differs from the case file", NULL); |
| 455 | memcpy(g_casesha, h, 65); } |
| 456 | const char *ecid = key_value(&e, 0, 12, "case_id"), *eaid = key_value(&e, 0, 12, "acceptance_id"), *ename = key_value(&e, 0, 12, "case_name"); |
| 457 | if (!ecid || !eaid || !ename || strcmp(ecid, cid) || strcmp(eaid, aid) || strcmp(ename, cname)) return die("engine identities differ from the case file", NULL); |
| 458 | if (strcmp(e.line[0], "OMEGA-AT0-ENGINE v1") || strcmp(key_value(&e, 0, 4, "contract") ? key_value(&e, 0, 4, "contract") : "", "AT0_RESULT_V2")) |
| 459 | return die("engine output is not OMEGA-AT0-ENGINE v1 / AT0_RESULT_V2", NULL); |
| 460 | /* oracle record: identities, oracle marking (Agent 0 condition on omega#359) */ |
| 461 | const char *ocid = key_value(&r, 0, r.n, "case_id"); |
| 462 | if (!ocid || strcmp(ocid, cid)) return die("oracle record is for a different case", NULL); |
| 463 | { const char *bc = key_value(&r, 0, r.n, "build_cc"), *es = key_value(&r, 0, r.n, "engine_sha256"), *os = key_value(&r, 0, r.n, "oracle_sha256"); |
| 464 | if (!bc || strncmp(bc, "oracle ", 7)) return die("oracle record build_cc does not begin with 'oracle '", NULL); |
| 465 | if (!es || !os || strcmp(es, os)) return die("oracle record engine_sha256 != oracle_sha256", NULL); } |
| 466 | int nb = find_line(&e, "begin numerics", 0), ne = find_line(&e, "end numerics", 0); |
| 467 | int vb = find_line(&e, "begin values", 0), ve = find_line(&e, "end values", 0); |
| 468 | int ob = find_line(&r, "begin values", 0), oe = find_line(&r, "end values", 0); |
| 469 | if (nb < 0 || ne != nb + 4 || vb < ne || ve < vb || ob < 0 || oe < ob) return die("block structure of engine or oracle output", NULL); |
| 470 | /* values block = engine lines (no reference lines allowed) + oracle reference lines only */ |
| 471 | int nref = 0; char **vl = malloc(sizeof(char *) * (size_t)(e.n + r.n + 4)); int nv = 0; |
| 472 | for (int i = vb + 1; i < ve; i++) { if (!strncmp(e.line[i], "reference ", 10)) return die("engine output contains reference lines", NULL); vl[nv++] = e.line[i]; } |
| 473 | for (int i = ob + 1; i < oe; i++) if (!strncmp(r.line[i], "reference ", 10)) { vl[nv++] = r.line[i]; nref++; } |
| 474 | Values *V = malloc(sizeof *V); |
| 475 | if (parse_values(vl, 0, nv, V, err, sizeof err)) return die(err, NULL); |
| 476 | if (!V->have_ref) return die("oracle supplied no reference lines", NULL); |
| 477 | const char *bk = key_value(&e, nb, ne, "bound_kind"); const char *ar = key_value(&e, nb, ne, "arithmetic"); const char *th = key_value(&e, nb, ne, "threads"); |
| 478 | if (!bk || !ar || !th || strcmp(th, "1")) return die("engine numerics block", NULL); |
| 479 | int bkind = !strcmp(bk, "RIGOROUS") ? 2 : !strcmp(bk, "ESTIMATED") ? 1 : 0; |
| 480 | /* label status consistency note (not fatal: the independent verifier is the arbiter) */ |
| 481 | if (V->kernel_dim > 0 && V->constraint.kind != V_UNDEFINED) |
| 482 | for (int k = 0; k < V->M; k++) if (V->clock[k].kind == V_REAL && derive_status(&V->clock[k], &A.tol_zero) != V->status[k]) |
| 483 | fprintf(stderr, "AT0_ASSEMBLE_NOTE label %d written status differs from the section 3 rule\n", k); |
| 484 | int res[10]; unsigned codes; int li; char ct[600]; |
| 485 | judge(V, bkind, &A, res, &codes, &li); |
| 486 | Buf o = { 0 }; bput(&o, ""); |
| 487 | bput(&o, "OMEGA-AT0-RESULT v2\ndomain omega.at0.result.v2\ncontract AT0_RESULT_V2\ncase_contract AT0_CASE_V1\ncase_name "); |
| 488 | bput(&o, cname); bput(&o, "\n"); |
| 489 | for (int i = sa; i <= ab; i++) { bput(&o, c.line[i]); bput(&o, "\n"); } |
| 490 | bput(&o, "case_id "); bput(&o, cid); bput(&o, "\nacceptance_id "); bput(&o, aid); |
| 491 | bput(&o, "\ncase_file_sha256 "); bput(&o, g_casesha); bput(&o, "\n"); |
| 492 | for (int i = nb; i <= ne; i++) { bput(&o, e.line[i]); bput(&o, "\n"); } |
| 493 | bput(&o, "begin values\n"); |
| 494 | for (int i = 0; i < nv; i++) { |
| 495 | /* ordering: engine lines up to the pauli lines, then the oracle's reference lines */ |
| 496 | bput(&o, vl[i]); bput(&o, "\n"); |
| 497 | } |
| 498 | bput(&o, "end values\n"); |
| 499 | Buf vbk = { 0 }; bput(&vbk, ""); |
| 500 | verdict_block(&vbk, res, codes, li, &A, ct); |
| 501 | bput(&o, vbk.p); |
| 502 | char pre[300]; snprintf(pre, sizeof pre, "case_id %s\nacceptance_id %s\n", cid, aid); |
| 503 | char vid[65]; sha_tagged("omega.at0.verdict.v2", pre, vbk.p, vbk.n, vid); |
| 504 | bput(&o, "verdict_id "); bput(&o, vid); bput(&o, "\nbegin provenance\n"); |
| 505 | for (int i = 0; i < pv.n; i++) { bput(&o, pv.line[i]); bput(&o, "\n"); } |
| 506 | bput(&o, "end provenance\n"); |
| 507 | char ev[65]; sha_tagged("omega.at0.evidence.v2", NULL, o.p, o.n, ev); |
| 508 | bput(&o, "evidence_digest "); bput(&o, ev); bput(&o, "\nend\n"); |
| 509 | fwrite(o.p, 1, o.n, stdout); |
| 510 | return 0; |
| 511 | } |