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 / model / at0_case.c

529 lines · 25108 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/* AT0_CASE_V1 codec: parse, validate (rules 1..6 in contract order), compute
18 * identities, emit canonical bytes. The only shared omega source is sha256. */
19#include "at0_model.h"
20#include "sha256.h"
21
22#include <ctype.h>
23#include <stdlib.h>
24#include <string.h>
25
26const char *const at0_failure_codes[AT0_FAILURE_CODE_COUNT] = {
27 "BOUND_KIND_INSUFFICIENT", "CONDITIONAL_UNDEFINED", "CONSTRAINT_RESIDUAL_EXCEEDED",
28 "NONFINITE_VALUE", "POVM_NORMALIZATION_EXCEEDED", "PRECISION_INSUFFICIENT",
29 "PROBABILITY_OUT_OF_RANGE", "PROBABILITY_SUM_EXCEEDED", "SCHRODINGER_DEVIATION_EXCEEDED",
30 "TRIVIAL_PHYSICAL_STATE"
31};
32
33#define LINE_CAP 4096
34
35typedef struct {
36 const char *line[LINE_CAP];
37 size_t len[LINE_CAP];
38 int count;
39 int pos;
40} lines_t;
41
42/* rule 1: byte shape. Fills line table pointing into a NUL-free copy. */
43static at0_status split_lines(const uint8_t *bytes, size_t len, char *copy, lines_t *L)
44{
45 if (len == 0 || bytes[len - 1] != '\n') return AT0_CASE_PARSE_ERROR;
46 memcpy(copy, bytes, len);
47 L->count = 0; L->pos = 0;
48 size_t start = 0;
49 for (size_t i = 0; i < len; i++) {
50 uint8_t b = bytes[i];
51 if (b == '\n') {
52 size_t n = i - start;
53 if (n == 0) return AT0_CASE_PARSE_ERROR; /* blank line */
54 if (copy[start] == ' ' || copy[i - 1] == ' ') return AT0_CASE_PARSE_ERROR;
55 for (size_t j = start + 1; j < i; j++)
56 if (copy[j] == ' ' && copy[j - 1] == ' ') return AT0_CASE_PARSE_ERROR;
57 if (L->count >= LINE_CAP) return AT0_CASE_PARSE_ERROR;
58 copy[i] = 0;
59 L->line[L->count] = copy + start; L->len[L->count] = n; L->count++;
60 start = i + 1;
61 } else if (b < 0x20 || b > 0x7E) {
62 return AT0_CASE_PARSE_ERROR;
63 }
64 }
65 return AT0_OK;
66}
67
68static const char *next_line(lines_t *L) { return L->pos < L->count ? L->line[L->pos++] : NULL; }
69
70/* "<key> <rest>": returns rest or NULL if key does not match */
71static const char *key_rest(const char *line, const char *key)
72{
73 size_t k = strlen(key);
74 if (!line || strncmp(line, key, k) != 0 || line[k] != ' ') return NULL;
75 return line + k + 1;
76}
77
78static int valid_label(const char *s)
79{
80 size_t n = strlen(s);
81 if (n < 1 || n > AT0_LABEL_LEN || !(s[0] >= 'a' && s[0] <= 'z')) return 0;
82 for (size_t i = 1; i < n; i++)
83 if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_')) return 0;
84 return 1;
85}
86static int valid_name(const char *s)
87{
88 size_t n = strlen(s);
89 if (n < 1 || n > AT0_NAME_LEN || !isalnum((unsigned char)s[0])) return 0;
90 for (size_t i = 1; i < n; i++)
91 if (!(isalnum((unsigned char)s[i]) || s[i] == '_' || s[i] == '.' || s[i] == '-')) return 0;
92 return 1;
93}
94static int valid_digest(const char *s)
95{
96 if (strlen(s) != AT0_DIGEST_HEX) return 0;
97 for (int i = 0; i < AT0_DIGEST_HEX; i++)
98 if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return 0;
99 return 1;
100}
101/* canonical integer token for counts and indices (same tri-state as the rational tokens:
102 * 1 canonical, 0 shape error, -1 noncanonical such as "04" or "-0"). Canonical form is judged
103 * before the length cut, so a long noncanonical token still reports CASE_NONCANONICAL; a canonical
104 * token above 9 digits saturates (above every limit; it then fails the list-count shape check).
105 * A negative value also fails the shape pass (list counts / next key), never silently. */
106static int parse_small_int(const char *s, long *out)
107{
108 size_t n = strlen(s), i = 0; int neg = 0;
109 if (n > 0 && s[0] == '-') { neg = 1; i = 1; }
110 if (n == i) return 0;
111 for (size_t j = i; j < n; j++) if (!isdigit((unsigned char)s[j])) return 0;
112 int canonical = !(s[i] == '0' && (n - i > 1 || neg));
113 while (i < n - 1 && s[i] == '0') i++; /* value of a noncanonical token: significant digits only */
114 long v = 0;
115 if (n - i > 9) v = 1000000000L; /* saturation, above every limit */
116 else for (; i < n; i++) v = v * 10 + (s[i] - '0');
117 *out = neg ? -v : v;
118 return canonical ? 1 : -1;
119}
120
121/* split a comma list in place (no spaces, no empty items, no trailing comma) */
122static int split_list(char *s, char **items, int cap, int *count)
123{
124 *count = 0;
125 if (strcmp(s, "none") == 0) return 1;
126 if (*s == 0) return 0;
127 char *p = s;
128 for (;;) {
129 char *c = strchr(p, ',');
130 if (c) *c = 0;
131 if (*p == 0) return 0;
132 if (*count < cap) items[*count] = p;
133 (*count)++;
134 if (!c) break;
135 p = c + 1;
136 }
137 return 1;
138}
139
140typedef struct {
141 /* raw values that are only judged after the shape pass */
142 char header[64], domain[64], contract[64];
143 long clock_dim, label_count;
144 int energies_seen;
145 int expected_codes_raw_count;
146 char expected_codes_raw[AT0_FAILURE_CODE_COUNT + 1][48];
147 int expected_codes_overflow;
148 int noncanonical; /* a well-formed but noncanonical token was seen (rule 2, judged after the shape pass) */
149} raw_t;
150
151static at0_status copy_tok(const char *src, char *dst, size_t cap)
152{
153 if (!src || strlen(src) >= cap) return AT0_CASE_PARSE_ERROR;
154 strcpy(dst, src); return AT0_OK;
155}
156
157/* token parse, both within AT0_CASE_V1 section 4 rule 1 (shape and encodings): a shape error
158 * returns at once; a well-formed but noncanonical token is recorded and judged after the whole
159 * shape pass, so a shape error anywhere in the file wins over canonical form */
160#define NEED(expr) do { at0_status st_ = (expr); if (st_ == AT0_CASE_NONCANONICAL) r->noncanonical = 1; else if (st_ != AT0_OK) return st_; } while (0)
161#define SMALL_INT(tok, out) do { int c_ = parse_small_int((tok), (out)); if (c_ == 0) return AT0_CASE_PARSE_ERROR; if (c_ < 0) r->noncanonical = 1; } while (0)
162#define FIXED(L, text) do { const char *l_ = next_line(L); if (!l_ || strcmp(l_, text) != 0) return AT0_CASE_PARSE_ERROR; } while (0)
163#define KEYED(L, key, rest) do { rest = key_rest(next_line(L), key); if (!rest) return AT0_CASE_PARSE_ERROR; } while (0)
164
165/* rule 1 (shape + token canonicality) for the whole file */
166static at0_status parse_shape(lines_t *L, at0_case *c, raw_t *r)
167{
168 const char *rest;
169 char tmp[AT0_LINE_MAX];
170 memset(c, 0, sizeof *c); memset(r, 0, sizeof *r);
171
172 KEYED(L, "OMEGA-AT0-CASE", rest); NEED(copy_tok(rest, r->header, sizeof r->header));
173 KEYED(L, "domain", rest); NEED(copy_tok(rest, r->domain, sizeof r->domain));
174 KEYED(L, "contract", rest); NEED(copy_tok(rest, r->contract, sizeof r->contract));
175 KEYED(L, "case_name", rest);
176 if (!valid_name(rest)) return AT0_CASE_PARSE_ERROR;
177 strcpy(c->name, rest);
178 FIXED(L, "begin semantic");
179 /* bare literals of the section 1 grammar (Agent 0 ruling on omega#358, R37): a different
180 * token is a shape failure, CASE_PARSE_ERROR, not a rule-3 parameter */
181 FIXED(L, "model_family PAGE_WOOTTERS_FINITE_IDEAL");
182 FIXED(L, "energy_unit DIMENSIONLESS_HBAR_1");
183 KEYED(L, "clock_dim", rest); SMALL_INT(rest, &r->clock_dim);
184 KEYED(L, "clock_energies", rest);
185 {
186 if (strlen(rest) >= sizeof tmp) return AT0_CASE_PARSE_ERROR;
187 strcpy(tmp, rest);
188 char *items[AT0_CLOCK_DIM_MAX]; int n;
189 if (!split_list(tmp, items, AT0_CLOCK_DIM_MAX, &n) || n == 0) return AT0_CASE_PARSE_ERROR;
190 if (n != r->clock_dim) return AT0_CASE_PARSE_ERROR;
191 for (int i = 0; i < n && i < AT0_CLOCK_DIM_MAX; i++) NEED(at0_rat_parse(items[i], &c->clock_energies[i]));
192 r->energies_seen = n;
193 }
194 FIXED(L, "system_dim 2");
195 KEYED(L, "system_hamiltonian_pauli", rest);
196 {
197 if (strlen(rest) >= sizeof tmp) return AT0_CASE_PARSE_ERROR;
198 strcpy(tmp, rest);
199 char *items[4]; int n;
200 if (!split_list(tmp, items, 4, &n) || n != 4) return AT0_CASE_PARSE_ERROR;
201 NEED(at0_rat_parse(items[0], &c->h0)); NEED(at0_rat_parse(items[1], &c->hx));
202 NEED(at0_rat_parse(items[2], &c->hy)); NEED(at0_rat_parse(items[3], &c->hz));
203 }
204 FIXED(L, "interaction NONE");
205 FIXED(L, "constraint SUM_HC_HS");
206 FIXED(L, "physical_state NULLSPACE_PROJECTION");
207 KEYED(L, "reference_clock_label", rest);
208 if (!valid_label(rest)) return AT0_CASE_PARSE_ERROR;
209 strcpy(c->reference_clock_label, rest);
210 KEYED(L, "reference_system_state", rest);
211 {
212 if (strlen(rest) >= sizeof tmp) return AT0_CASE_PARSE_ERROR;
213 strcpy(tmp, rest);
214 char *items[2]; int n;
215 if (!split_list(tmp, items, 2, &n) || n != 2) return AT0_CASE_PARSE_ERROR;
216 NEED(at0_crat_parse(items[0], &c->psi0[0])); NEED(at0_crat_parse(items[1], &c->psi0[1]));
217 }
218 FIXED(L, "clock_povm COVARIANT_DISCRETE");
219 KEYED(L, "povm_tau_turns", rest); NEED(at0_rat_parse(rest, &c->povm_tau_turns));
220 KEYED(L, "povm_weight", rest); NEED(at0_rat_parse(rest, &c->povm_weight));
221 KEYED(L, "clock_label_count", rest); SMALL_INT(rest, &r->label_count);
222 for (long k = 0; k < r->label_count; k++) {
223 KEYED(L, "clock_label", rest);
224 if (strlen(rest) >= sizeof tmp) return AT0_CASE_PARSE_ERROR;
225 strcpy(tmp, rest);
226 char *sp = strchr(tmp, ' ');
227 if (!sp) return AT0_CASE_PARSE_ERROR;
228 *sp = 0;
229 long idx;
230 SMALL_INT(tmp, &idx);
231 if (idx != k) return AT0_CASE_PARSE_ERROR;
232 if (!valid_label(sp + 1)) return AT0_CASE_PARSE_ERROR;
233 if (k < AT0_LABEL_MAX) strcpy(c->labels[k], sp + 1);
234 }
235 FIXED(L, "observables PAULI_X,PAULI_Y,PAULI_Z");
236 FIXED(L, "end semantic");
237 FIXED(L, "begin acceptance");
238 KEYED(L, "control_kind", rest);
239 if (strcmp(rest, "POSITIVE") == 0) c->control_kind = AT0_CTRL_POSITIVE;
240 else if (strcmp(rest, "NEGATIVE") == 0) c->control_kind = AT0_CTRL_NEGATIVE;
241 else return AT0_CASE_PARSE_ERROR;
242 KEYED(L, "expected_outcome", rest);
243 if (strcmp(rest, "PASS") == 0) c->expected_outcome = AT0_EXPECT_PASS;
244 else if (strcmp(rest, "FAIL") == 0) c->expected_outcome = AT0_EXPECT_FAIL;
245 else return AT0_CASE_PARSE_ERROR;
246 KEYED(L, "expected_failure_codes", rest);
247 {
248 if (strlen(rest) >= sizeof tmp) return AT0_CASE_PARSE_ERROR;
249 strcpy(tmp, rest);
250 char *items[AT0_FAILURE_CODE_COUNT + 1]; int n;
251 if (!split_list(tmp, items, AT0_FAILURE_CODE_COUNT + 1, &n)) return AT0_CASE_PARSE_ERROR;
252 r->expected_codes_raw_count = n;
253 if (n > AT0_FAILURE_CODE_COUNT) r->expected_codes_overflow = 1;
254 for (int i = 0; i < n && i <= AT0_FAILURE_CODE_COUNT; i++)
255 NEED(copy_tok(items[i], r->expected_codes_raw[i], sizeof r->expected_codes_raw[i]));
256 }
257 KEYED(L, "min_bound_kind", rest);
258 if (strcmp(rest, "RIGOROUS") == 0) c->min_bound_kind = AT0_BOUND_RIGOROUS;
259 else if (strcmp(rest, "ESTIMATED") == 0) c->min_bound_kind = AT0_BOUND_ESTIMATED;
260 else if (strcmp(rest, "NONE") == 0) c->min_bound_kind = AT0_BOUND_NONE;
261 else return AT0_CASE_PARSE_ERROR;
262 KEYED(L, "tol_constraint_residual", rest); NEED(at0_scaled_parse(rest, &c->tol_constraint_residual));
263 KEYED(L, "tol_povm_residual", rest); NEED(at0_scaled_parse(rest, &c->tol_povm_residual));
264 KEYED(L, "tol_probability", rest); NEED(at0_scaled_parse(rest, &c->tol_probability));
265 KEYED(L, "tol_zero_probability", rest); NEED(at0_scaled_parse(rest, &c->tol_zero_probability));
266 KEYED(L, "tol_schrodinger", rest); NEED(at0_scaled_parse(rest, &c->tol_schrodinger));
267 FIXED(L, "end acceptance");
268 KEYED(L, "case_id", rest);
269 if (!valid_digest(rest)) return AT0_CASE_PARSE_ERROR;
270 strcpy(c->case_id, rest);
271 KEYED(L, "acceptance_id", rest);
272 if (!valid_digest(rest)) return AT0_CASE_PARSE_ERROR;
273 strcpy(c->acceptance_id, rest);
274 FIXED(L, "end");
275 if (L->pos != L->count) return AT0_CASE_PARSE_ERROR; /* trailing lines */
276 return r->noncanonical ? AT0_CASE_NONCANONICAL : AT0_OK; /* canonical form, after the whole shape pass (still rule 1) */
277}
278
279/* rule 3: ranges and fixed values */
280static at0_status check_parameters(at0_case *c, const raw_t *r)
281{
282 /* contract limits on written tokens (AT0_CASE_V1 section 2), judged here so that rules 1 and 2 come first */
283 for (int j = 0; j < r->energies_seen && j < AT0_CLOCK_DIM_MAX; j++)
284 if (!at0_rat_in_limits(c->clock_energies[j])) return AT0_CASE_INVALID_PARAMETER;
285 if (!at0_rat_in_limits(c->h0) || !at0_rat_in_limits(c->hx) || !at0_rat_in_limits(c->hy) || !at0_rat_in_limits(c->hz)) return AT0_CASE_INVALID_PARAMETER;
286 for (int s = 0; s < AT0_SYSTEM_DIM; s++)
287 if (!at0_rat_in_limits(c->psi0[s].re) || !at0_rat_in_limits(c->psi0[s].im)) return AT0_CASE_INVALID_PARAMETER;
288 if (!at0_rat_in_limits(c->povm_tau_turns) || !at0_rat_in_limits(c->povm_weight)) return AT0_CASE_INVALID_PARAMETER;
289 if (!at0_scaled_in_limits(c->tol_constraint_residual) || !at0_scaled_in_limits(c->tol_povm_residual) ||
290 !at0_scaled_in_limits(c->tol_probability) || !at0_scaled_in_limits(c->tol_zero_probability) ||
291 !at0_scaled_in_limits(c->tol_schrodinger)) return AT0_CASE_INVALID_PARAMETER;
292 /* the eight fixed literals of section 1 are judged in the shape pass (parse_shape) */
293 if (r->clock_dim < AT0_CLOCK_DIM_MIN || r->clock_dim > AT0_CLOCK_DIM_MAX) return AT0_CASE_INVALID_PARAMETER;
294 c->clock_dim = (int)r->clock_dim;
295 for (int j = 1; j < c->clock_dim; j++)
296 if (at0_rat_cmp(c->clock_energies[j - 1], c->clock_energies[j]) >= 0) return AT0_CASE_INVALID_PARAMETER;
297 if (at0_rat_is_zero(c->psi0[0].re) && at0_rat_is_zero(c->psi0[0].im) &&
298 at0_rat_is_zero(c->psi0[1].re) && at0_rat_is_zero(c->psi0[1].im)) return AT0_CASE_INVALID_PARAMETER;
299 if (c->povm_weight.n <= 0) return AT0_CASE_INVALID_PARAMETER;
300 if (c->povm_tau_turns.n <= 0) return AT0_CASE_INVALID_PARAMETER;
301 if (r->label_count < 1 || r->label_count > AT0_LABEL_MAX) return AT0_CASE_INVALID_PARAMETER;
302 c->label_count = (int)r->label_count;
303 c->reference_index = -1;
304 for (int k = 0; k < c->label_count; k++) {
305 for (int l = 0; l < k; l++)
306 if (strcmp(c->labels[k], c->labels[l]) == 0) return AT0_CASE_INVALID_PARAMETER;
307 if (strcmp(c->labels[k], c->reference_clock_label) == 0) c->reference_index = k;
308 }
309 if (c->reference_index < 0) return AT0_CASE_INVALID_PARAMETER;
310 return AT0_OK;
311}
312
313/* rule 4: |h|^2 must be the square of a rational */
314static at0_status check_spectrum(at0_case *c)
315{
316 /* |h|^2 is formed from unreduced 128-bit integers: the reduced rational |h|^2 can have a
317 * denominator up to 2^120 for in-limit tokens with distinct denominators. */
318 int sq;
319 at0_status st = at0_rat_norm_exact(c->hx, c->hy, c->hz, &c->h_norm, &sq);
320 if (st != AT0_OK) return st;
321 if (!sq) return AT0_CASE_IRRATIONAL_SPECTRUM;
322 return AT0_OK;
323}
324
325/* rule 6: expected failure code list */
326static at0_status check_acceptance(at0_case *c, const raw_t *r)
327{
328 c->expected_failure_count = 0;
329 if (c->expected_outcome == AT0_EXPECT_PASS) {
330 return r->expected_codes_raw_count == 0 ? AT0_OK : AT0_CASE_INVALID_PARAMETER;
331 }
332 if (r->expected_codes_raw_count == 0 || r->expected_codes_overflow) return AT0_CASE_INVALID_PARAMETER;
333 int prev = -1;
334 for (int i = 0; i < r->expected_codes_raw_count; i++) {
335 int idx = -1;
336 for (int j = 0; j < AT0_FAILURE_CODE_COUNT; j++)
337 if (strcmp(r->expected_codes_raw[i], at0_failure_codes[j]) == 0) { idx = j; break; }
338 if (idx < 0 || idx <= prev) return AT0_CASE_INVALID_PARAMETER; /* unknown, duplicate or unsorted */
339 c->expected_failure_idx[i] = idx; prev = idx;
340 }
341 c->expected_failure_count = r->expected_codes_raw_count;
342 return AT0_OK;
343}
344
345/* ---- emission ------------------------------------------------------------ */
346typedef struct { char *buf; size_t cap; size_t len; int overflow; } out_t;
347
348static void put(out_t *o, const char *s)
349{
350 size_t n = strlen(s);
351 if (o->len + n >= o->cap) { o->overflow = 1; return; }
352 memcpy(o->buf + o->len, s, n); o->len += n; o->buf[o->len] = 0;
353}
354static void putf_rat(out_t *o, at0_rat r) { char t[64]; at0_rat_format(r, t, sizeof t); put(o, t); }
355
356static void emit_semantic(const at0_case *c, out_t *o)
357{
358 char t[128];
359 put(o, "begin semantic\n");
360 put(o, "model_family PAGE_WOOTTERS_FINITE_IDEAL\n");
361 put(o, "energy_unit DIMENSIONLESS_HBAR_1\n");
362 snprintf(t, sizeof t, "clock_dim %d\n", c->clock_dim); put(o, t);
363 put(o, "clock_energies ");
364 for (int j = 0; j < c->clock_dim; j++) { if (j) put(o, ","); putf_rat(o, c->clock_energies[j]); }
365 put(o, "\n");
366 put(o, "system_dim 2\n");
367 put(o, "system_hamiltonian_pauli ");
368 putf_rat(o, c->h0); put(o, ","); putf_rat(o, c->hx); put(o, ","); putf_rat(o, c->hy); put(o, ","); putf_rat(o, c->hz);
369 put(o, "\n");
370 put(o, "interaction NONE\n");
371 put(o, "constraint SUM_HC_HS\n");
372 put(o, "physical_state NULLSPACE_PROJECTION\n");
373 snprintf(t, sizeof t, "reference_clock_label %s\n", c->reference_clock_label); put(o, t);
374 put(o, "reference_system_state ");
375 at0_crat_format(c->psi0[0], t, sizeof t); put(o, t); put(o, ",");
376 at0_crat_format(c->psi0[1], t, sizeof t); put(o, t); put(o, "\n");
377 put(o, "clock_povm COVARIANT_DISCRETE\n");
378 put(o, "povm_tau_turns "); putf_rat(o, c->povm_tau_turns); put(o, "\n");
379 put(o, "povm_weight "); putf_rat(o, c->povm_weight); put(o, "\n");
380 snprintf(t, sizeof t, "clock_label_count %d\n", c->label_count); put(o, t);
381 for (int k = 0; k < c->label_count; k++) { snprintf(t, sizeof t, "clock_label %d %s\n", k, c->labels[k]); put(o, t); }
382 put(o, "observables PAULI_X,PAULI_Y,PAULI_Z\n");
383 put(o, "end semantic\n");
384}
385
386static void emit_acceptance(const at0_case *c, out_t *o)
387{
388 char t[128];
389 put(o, "begin acceptance\n");
390 put(o, c->control_kind == AT0_CTRL_POSITIVE ? "control_kind POSITIVE\n" : "control_kind NEGATIVE\n");
391 put(o, c->expected_outcome == AT0_EXPECT_PASS ? "expected_outcome PASS\n" : "expected_outcome FAIL\n");
392 put(o, "expected_failure_codes ");
393 if (c->expected_failure_count == 0) put(o, "none");
394 for (int i = 0; i < c->expected_failure_count; i++) { if (i) put(o, ","); put(o, at0_failure_codes[c->expected_failure_idx[i]]); }
395 put(o, "\n");
396 put(o, c->min_bound_kind == AT0_BOUND_RIGOROUS ? "min_bound_kind RIGOROUS\n"
397 : c->min_bound_kind == AT0_BOUND_ESTIMATED ? "min_bound_kind ESTIMATED\n" : "min_bound_kind NONE\n");
398 at0_scaled_format(c->tol_constraint_residual, t, sizeof t); put(o, "tol_constraint_residual "); put(o, t); put(o, "\n");
399 at0_scaled_format(c->tol_povm_residual, t, sizeof t); put(o, "tol_povm_residual "); put(o, t); put(o, "\n");
400 at0_scaled_format(c->tol_probability, t, sizeof t); put(o, "tol_probability "); put(o, t); put(o, "\n");
401 at0_scaled_format(c->tol_zero_probability, t, sizeof t); put(o, "tol_zero_probability "); put(o, t); put(o, "\n");
402 at0_scaled_format(c->tol_schrodinger, t, sizeof t); put(o, "tol_schrodinger "); put(o, t); put(o, "\n");
403 put(o, "end acceptance\n");
404}
405
406long at0_case_emit(const at0_case *c, char *buf, size_t cap)
407{
408 if (!c || !buf || cap == 0) return -1;
409 out_t o = { buf, cap, 0, 0 };
410 char t[512];
411 buf[0] = 0;
412 put(&o, "OMEGA-AT0-CASE v1\ndomain omega.at0.case.v1\ncontract AT0_CASE_V1\n");
413 snprintf(t, sizeof t, "case_name %s\n", c->name); put(&o, t);
414 emit_semantic(c, &o);
415 emit_acceptance(c, &o);
416 snprintf(t, sizeof t, "case_id %s\nacceptance_id %s\nend\n", c->case_id, c->acceptance_id); put(&o, t);
417 return o.overflow ? -1 : (long)o.len;
418}
419
420static void hex_digest(const uint8_t d[32], char out[AT0_DIGEST_HEX + 1])
421{
422 static const char hx[] = "0123456789abcdef";
423 for (int i = 0; i < 32; i++) { out[2 * i] = hx[d[i] >> 4]; out[2 * i + 1] = hx[d[i] & 15]; }
424 out[AT0_DIGEST_HEX] = 0;
425}
426
427static void tagged_digest(const char *tag, const char *text, size_t len, char out[AT0_DIGEST_HEX + 1])
428{
429 sha256_ctx ctx; uint8_t d[32]; uint8_t zero = 0;
430 sha256_init(&ctx);
431 sha256_update(&ctx, (const uint8_t *)tag, strlen(tag));
432 sha256_update(&ctx, &zero, 1);
433 sha256_update(&ctx, (const uint8_t *)text, len);
434 sha256_final(&ctx, d);
435 hex_digest(d, out);
436}
437
438at0_status at0_case_identities(const at0_case *c, char case_id[AT0_DIGEST_HEX + 1],
439 char acceptance_id[AT0_DIGEST_HEX + 1])
440{
441 if (!c || !case_id || !acceptance_id) return AT0_ERR_ARGUMENT;
442 char *buf = malloc(AT0_LINE_MAX * 4);
443 if (!buf) return AT0_ERR_INTERNAL;
444 out_t o = { buf, AT0_LINE_MAX * 4, 0, 0 };
445 buf[0] = 0;
446 emit_semantic(c, &o);
447 if (o.overflow) { free(buf); return AT0_ERR_INTERNAL; }
448 tagged_digest("omega.at0.case.v1", buf, o.len, case_id);
449 o.len = 0; buf[0] = 0;
450 emit_acceptance(c, &o);
451 if (o.overflow) { free(buf); return AT0_ERR_INTERNAL; }
452 tagged_digest("omega.at0.acceptance.v1", buf, o.len, acceptance_id);
453 free(buf);
454 return AT0_OK;
455}
456
457at0_status at0_case_parse(const uint8_t *bytes, size_t len, at0_case *out)
458{
459 if (!bytes || !out) return AT0_ERR_ARGUMENT;
460 if (len > (size_t)LINE_CAP * 256) return AT0_CASE_PARSE_ERROR;
461 char *copy = malloc(len + 1);
462 lines_t *L = malloc(sizeof *L);
463 raw_t *r = malloc(sizeof *r);
464 char *re = malloc(len + 1);
465 at0_status st = AT0_ERR_INTERNAL;
466 if (!copy || !L || !r || !re) goto done;
467
468 /* rule 1 */
469 if ((st = split_lines(bytes, len, copy, L)) != AT0_OK) goto done;
470 if ((st = parse_shape(L, out, r)) != AT0_OK) goto done;
471 /* rule 2 */
472 if (strcmp(r->header, "v1") != 0 || strcmp(r->domain, "omega.at0.case.v1") != 0 ||
473 strcmp(r->contract, "AT0_CASE_V1") != 0) { st = AT0_CASE_UNSUPPORTED_VERSION; goto done; }
474 /* rule 3 */
475 if ((st = check_parameters(out, r)) != AT0_OK) goto done;
476 /* canonical-form guard: the parsed case must re-emit byte for byte */
477 {
478 /* the emitted text uses parsed codes; copy raw codes so the guard sees the file's list */
479 at0_case tmp = *out;
480 tmp.expected_failure_count = 0;
481 int ok = 1;
482 if (r->expected_codes_raw_count > 0 && !r->expected_codes_overflow) {
483 for (int i = 0; i < r->expected_codes_raw_count; i++) {
484 int idx = -1;
485 for (int j = 0; j < AT0_FAILURE_CODE_COUNT; j++)
486 if (strcmp(r->expected_codes_raw[i], at0_failure_codes[j]) == 0) idx = j;
487 if (idx < 0) { ok = 0; break; }
488 tmp.expected_failure_idx[i] = idx;
489 }
490 tmp.expected_failure_count = r->expected_codes_raw_count;
491 }
492 if (ok && !r->expected_codes_overflow) {
493 long n = at0_case_emit(&tmp, re, len + 1);
494 if (n != (long)len || memcmp(re, bytes, len) != 0) { st = AT0_CASE_NONCANONICAL; goto done; }
495 }
496 }
497 /* rule 4 */
498 if ((st = check_spectrum(out)) != AT0_OK) goto done;
499 /* rule 5 */
500 {
501 char cid[AT0_DIGEST_HEX + 1], aid[AT0_DIGEST_HEX + 1];
502 at0_case tmp = *out;
503 /* acceptance text must be hashed exactly as written; raw codes are canonical only if rule 6 passes,
504 * so hash the file's own acceptance lines instead of re-emitting them */
505 (void)tmp;
506 size_t a0 = 0, a1 = 0, s0 = 0, s1 = 0;
507 for (int i = 0; i < L->count; i++) {
508 if (strcmp(L->line[i], "begin semantic") == 0) s0 = (size_t)(L->line[i] - copy);
509 if (strcmp(L->line[i], "end semantic") == 0) s1 = (size_t)(L->line[i] - copy) + L->len[i] + 1;
510 if (strcmp(L->line[i], "begin acceptance") == 0) a0 = (size_t)(L->line[i] - copy);
511 if (strcmp(L->line[i], "end acceptance") == 0) a1 = (size_t)(L->line[i] - copy) + L->len[i] + 1;
512 }
513 tagged_digest("omega.at0.case.v1", (const char *)bytes + s0, s1 - s0, cid);
514 tagged_digest("omega.at0.acceptance.v1", (const char *)bytes + a0, a1 - a0, aid);
515 if (strcmp(cid, out->case_id) != 0 || strcmp(aid, out->acceptance_id) != 0) { st = AT0_CASE_ID_MISMATCH; goto done; }
516 }
517 /* rule 6 */
518 if ((st = check_acceptance(out, r)) != AT0_OK) goto done;
519 {
520 sha256_ctx ctx; uint8_t d[32];
521 sha256_init(&ctx); sha256_update(&ctx, bytes, len); sha256_final(&ctx, d);
522 hex_digest(d, out->case_file_sha256);
523 }
524 st = AT0_OK;
525done:
526 free(copy); free(L); free(r); free(re);
527 return st;
528}
529