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 / at1 / model / at1_case.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 | /* AT1_CASE_V1 codec: parse and validate a case in the order of AT1_CASE_V1 section 4, first |
| 18 | * failure wins: |
| 19 | * 1. shape and encodings: CASE_PARSE_ERROR for any shape failure anywhere in the file, then |
| 20 | * CASE_NONCANONICAL for a parsed but non-canonical token (decided after the whole shape pass); |
| 21 | * 2. values of the three version-bearing lines: CASE_UNSUPPORTED_VERSION; |
| 22 | * 3. ranges and fixed values: CASE_INVALID_PARAMETER; |
| 23 | * 4. rational spectrum per clock level: CASE_IRRATIONAL_SPECTRUM; |
| 24 | * 5. identities: CASE_ID_MISMATCH; |
| 25 | * 6. acceptance codes: CASE_INVALID_PARAMETER. |
| 26 | * Readings applied (AT1_CHARTER.md section 8 and the inherited AT-0 readings): |
| 27 | * - (d) the first token of each version-bearing line is its key (checked in step 1), the rest |
| 28 | * of the line is its value (checked in step 2); |
| 29 | * - (e) an index token (interaction_pauli, clock_label) is compared with its zero-based |
| 30 | * position by parsed integer value in the shape pass; canonical form is decided afterwards; |
| 31 | * - shape before range: count lines that disagree with their repeated block, fixed literals |
| 32 | * and enumeration tokens are shape errors; token limits are rule 3; |
| 33 | * - token parse classes follow the qualified AT-0 codec: a denominator below 1 (including a |
| 34 | * negative one) is non-canonical, a scaled decimal with a sign is non-canonical. |
| 35 | * Every exact decision uses at1_bn.c (no fixed-width arithmetic can overflow). */ |
| 36 | #include "at1_model.h" |
| 37 | #include "sha256.h" |
| 38 | |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | const char *const at1_failure_codes[AT1_FAILURE_CODE_COUNT] = { |
| 43 | "BOUND_KIND_INSUFFICIENT", "CONDITIONAL_UNDEFINED", "CONSTRAINT_RESIDUAL_EXCEEDED", |
| 44 | "INTERACTING_DEVIATION_EXCEEDED", "NONFINITE_VALUE", "ORACLE_DISAGREEMENT", |
| 45 | "POVM_NORMALIZATION_EXCEEDED", "PRECISION_INSUFFICIENT", "PROBABILITY_OUT_OF_RANGE", |
| 46 | "PROBABILITY_SUM_EXCEEDED", "SCHRODINGER_DEVIATION_EXCEEDED", "TRIVIAL_PHYSICAL_STATE" |
| 47 | }; |
| 48 | |
| 49 | const char *at1_status_name(at1_status s) |
| 50 | { |
| 51 | switch (s) { |
| 52 | case AT1_OK: return "OK"; |
| 53 | case AT1_CASE_PARSE_ERROR: return "CASE_PARSE_ERROR"; |
| 54 | case AT1_CASE_NONCANONICAL: return "CASE_NONCANONICAL"; |
| 55 | case AT1_CASE_UNSUPPORTED_VERSION: return "CASE_UNSUPPORTED_VERSION"; |
| 56 | case AT1_CASE_INVALID_PARAMETER: return "CASE_INVALID_PARAMETER"; |
| 57 | case AT1_CASE_IRRATIONAL_SPECTRUM: return "CASE_IRRATIONAL_SPECTRUM"; |
| 58 | case AT1_CASE_ID_MISMATCH: return "CASE_ID_MISMATCH"; |
| 59 | case AT1_ERR_RESOURCE: return "RESOURCE_LIMIT"; |
| 60 | case AT1_ERR_INTERNAL: return "INTERNAL_ERROR"; |
| 61 | case AT1_ERR_ARGUMENT: return "ARGUMENT"; |
| 62 | } |
| 63 | return "UNKNOWN"; |
| 64 | } |
| 65 | |
| 66 | /* ---- digests ------------------------------------------------------------------------------- */ |
| 67 | static void hex32(const uint8_t d[32], char out[65]) |
| 68 | { |
| 69 | static const char hx[] = "0123456789abcdef"; |
| 70 | for (int i = 0; i < 32; i++) { out[2 * i] = hx[d[i] >> 4]; out[2 * i + 1] = hx[d[i] & 15]; } |
| 71 | out[64] = 0; |
| 72 | } |
| 73 | void at1_tagged_sha256(const char *tag, const char *data, size_t len, char out[65]) |
| 74 | { |
| 75 | sha256_ctx ctx; uint8_t d[32]; uint8_t zero = 0; |
| 76 | sha256_init(&ctx); |
| 77 | sha256_update(&ctx, (const uint8_t *)tag, strlen(tag)); |
| 78 | sha256_update(&ctx, &zero, 1); |
| 79 | sha256_update(&ctx, (const uint8_t *)data, len); |
| 80 | sha256_final(&ctx, d); |
| 81 | hex32(d, out); |
| 82 | } |
| 83 | void at1_sha256_hex(const uint8_t *data, size_t len, char out[65]) |
| 84 | { |
| 85 | uint8_t d[32]; |
| 86 | sha256_hash(data, len, d); |
| 87 | hex32(d, out); |
| 88 | } |
| 89 | |
| 90 | /* ---- line table ---------------------------------------------------------------------------- */ |
| 91 | typedef struct { char **line; size_t *off; int count, pos; char *copy; } lines_t; |
| 92 | |
| 93 | static at1_status split_lines(const uint8_t *b, size_t len, lines_t *L) |
| 94 | { |
| 95 | L->line = NULL; L->off = NULL; L->count = 0; L->pos = 0; L->copy = NULL; |
| 96 | if (len == 0 || b[len - 1] != '\n') return AT1_CASE_PARSE_ERROR; |
| 97 | for (size_t i = 0; i < len; i++) if (b[i] != '\n' && (b[i] < 0x20 || b[i] > 0x7E)) return AT1_CASE_PARSE_ERROR; |
| 98 | int n = 0; |
| 99 | for (size_t i = 0; i < len; i++) if (b[i] == '\n') n++; |
| 100 | L->copy = at1_xmalloc(len + 1); |
| 101 | memcpy(L->copy, b, len); L->copy[len] = 0; |
| 102 | L->line = at1_xmalloc((size_t)n * sizeof(char *)); |
| 103 | L->off = at1_xmalloc((size_t)(n + 1) * sizeof(size_t)); |
| 104 | size_t start = 0; |
| 105 | for (size_t i = 0; i < len; i++) { |
| 106 | if (b[i] != '\n') continue; |
| 107 | size_t ln = i - start; |
| 108 | if (ln == 0) return AT1_CASE_PARSE_ERROR; /* blank line */ |
| 109 | if (b[start] == ' ' || b[i - 1] == ' ') return AT1_CASE_PARSE_ERROR; /* leading/trailing space */ |
| 110 | for (size_t j = start + 1; j < i; j++) if (b[j] == ' ' && b[j - 1] == ' ') return AT1_CASE_PARSE_ERROR; |
| 111 | L->copy[i] = 0; |
| 112 | L->line[L->count] = L->copy + start; L->off[L->count] = start; L->count++; |
| 113 | start = i + 1; |
| 114 | } |
| 115 | L->off[L->count] = len; |
| 116 | return AT1_OK; |
| 117 | } |
| 118 | static void lines_free(lines_t *L) { free(L->line); free(L->off); free(L->copy); } |
| 119 | static const char *peek(const lines_t *L) { return L->pos < L->count ? L->line[L->pos] : NULL; } |
| 120 | static const char *take(lines_t *L) { return L->pos < L->count ? L->line[L->pos++] : NULL; } |
| 121 | |
| 122 | /* line "<key> <rest>" -> rest, or NULL when the key differs or the line has no value */ |
| 123 | static const char *key_rest(const char *line, const char *key) |
| 124 | { |
| 125 | size_t k = strlen(key); |
| 126 | if (!line || strncmp(line, key, k) != 0 || line[k] != ' ') return NULL; |
| 127 | return line + k + 1; |
| 128 | } |
| 129 | static int line_key_is(const char *line, const char *key) { return key_rest(line, key) != NULL; } |
| 130 | |
| 131 | /* ---- token grammar ------------------------------------------------------------------------- */ |
| 132 | static int is_digit(char c) { return c >= '0' && c <= '9'; } |
| 133 | |
| 134 | typedef struct { |
| 135 | int shape; /* a shape failure was seen (PARSE_ERROR) */ |
| 136 | int noncanonical; /* a parsed, non-canonical token was seen */ |
| 137 | int resource; /* a token beyond the engine's digit limit was seen */ |
| 138 | int over_limit; /* a rational token outside the section 2 limits (rule 3) */ |
| 139 | int scaled_k_over; /* a scaled decimal with k > 40 (rule 3) */ |
| 140 | } flags_t; |
| 141 | |
| 142 | /* integer token "-?[0-9]+": returns 0 shape failure, 1 parsed. *canon, *val (bn, exact). */ |
| 143 | static int parse_int_tok(const char *s, size_t len, int *canon, bn *val, flags_t *f) |
| 144 | { |
| 145 | size_t i = 0; int neg = 0; |
| 146 | if (len == 0) return 0; |
| 147 | if (s[0] == '-') { neg = 1; i = 1; } |
| 148 | if (i == len) return 0; |
| 149 | for (size_t j = i; j < len; j++) if (!is_digit(s[j])) return 0; |
| 150 | *canon = !(s[i] == '0' && (len - i > 1 || neg)); |
| 151 | size_t z = i; |
| 152 | while (z < len - 1 && s[z] == '0') z++; /* value of the significant digits */ |
| 153 | if (!bn_from_digits(val, s + z, len - z)) { f->resource = 1; bn_set_u64(val, 0); return 1; } |
| 154 | if (neg && val->n) val->neg = 1; |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | /* small integer for counts and indices; saturates far above every limit */ |
| 159 | static int parse_small_int(const char *s, size_t len, long *out, flags_t *f) |
| 160 | { |
| 161 | int canon; bn v; bn_init(&v); |
| 162 | if (!parse_int_tok(s, len, &canon, &v, f)) { bn_free(&v); return 0; } |
| 163 | int64_t x; |
| 164 | if (!bn_fits_i64(&v, &x) || x > 1000000000 || x < -1000000000) x = v.neg ? -1000000001 : 1000000001; |
| 165 | *out = (long)x; |
| 166 | if (!canon) f->noncanonical = 1; |
| 167 | bn_free(&v); |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | static int bn_within(const bn *a, int64_t lim) |
| 172 | { |
| 173 | bn l; bn_init(&l); bn_set_i64(&l, lim); |
| 174 | int ok = bn_cmp_abs(a, &l) <= 0; |
| 175 | bn_free(&l); |
| 176 | return ok; |
| 177 | } |
| 178 | |
| 179 | /* rational "n/d": 0 on shape failure; sets flags for non-canonical and over-limit tokens */ |
| 180 | static int parse_rat_tok(const char *s, size_t len, bq *out, flags_t *f) |
| 181 | { |
| 182 | const char *slash = memchr(s, '/', len); |
| 183 | if (!slash) return 0; |
| 184 | size_t ln = (size_t)(slash - s), ld = len - ln - 1; |
| 185 | int cn, cd; bn n, d, g; bn_init(&n); bn_init(&d); bn_init(&g); |
| 186 | int ok = parse_int_tok(s, ln, &cn, &n, f) && parse_int_tok(slash + 1, ld, &cd, &d, f); |
| 187 | if (!ok) { bn_free(&n); bn_free(&d); bn_free(&g); return 0; } |
| 188 | int canonical = cn && cd && bn_sign(&d) > 0; |
| 189 | if (canonical) { bn_gcd(&g, &n, &d); if (!(g.n == 1 && g.d[0] == 1)) canonical = 0; } |
| 190 | if (!canonical) f->noncanonical = 1; |
| 191 | if (!bn_within(&n, AT1_TOKEN_LIMIT) || !bn_within(&d, AT1_TOKEN_LIMIT) || bn_sign(&d) <= 0) f->over_limit = 1; |
| 192 | if (bn_sign(&d) > 0) bq_set_bn(out, &n, &d); else bq_set_i64(out, 0, 1); |
| 193 | bn_free(&n); bn_free(&d); bn_free(&g); |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | static int parse_crat_tok(const char *s, size_t len, gq *out, flags_t *f) |
| 198 | { |
| 199 | if (len < 2 || s[0] != '(' || s[len - 1] != ')') return 0; |
| 200 | const char *semi = memchr(s, ';', len); |
| 201 | if (!semi) return 0; |
| 202 | size_t la = (size_t)(semi - s - 1), lb = len - 2 - la - 1; |
| 203 | if (memchr(semi + 1, ';', lb)) return 0; |
| 204 | return parse_rat_tok(s + 1, la, &out->re, f) && parse_rat_tok(semi + 1, lb, &out->im, f); |
| 205 | } |
| 206 | |
| 207 | static int parse_scaled_tok(const char *s, size_t len, at1_scaled *out, flags_t *f) |
| 208 | { |
| 209 | const char *at = memchr(s, '@', len); |
| 210 | if (!at) return 0; |
| 211 | size_t ln = (size_t)(at - s), lk = len - ln - 1; |
| 212 | int cn, ck; bn n, k; bn_init(&n); bn_init(&k); |
| 213 | int ok = parse_int_tok(s, ln, &cn, &n, f) && parse_int_tok(at + 1, lk, &ck, &k, f); |
| 214 | if (!ok) { bn_free(&n); bn_free(&k); return 0; } |
| 215 | int canonical = cn && ck && bn_sign(&n) >= 0 && bn_sign(&k) >= 0; |
| 216 | int64_t kv; |
| 217 | if (!bn_fits_i64(&k, &kv) || kv > 1000) kv = 1000; |
| 218 | if (kv < 0) kv = 0; |
| 219 | if (canonical && bn_is_zero(&n) && kv != 0) canonical = 0; |
| 220 | if (canonical && !bn_is_zero(&n) && kv != 0) { |
| 221 | bn ten, r; bn_init(&ten); bn_init(&r); bn_set_u64(&ten, 10); |
| 222 | bn_divmod(NULL, &r, &n, &ten); |
| 223 | if (bn_is_zero(&r)) canonical = 0; |
| 224 | bn_free(&ten); bn_free(&r); |
| 225 | } |
| 226 | if (!canonical) f->noncanonical = 1; |
| 227 | if (kv > AT1_SCALED_K_MAX) f->scaled_k_over = 1; |
| 228 | bn_abs(&out->n, &n); out->k = (unsigned)kv; |
| 229 | bn_free(&n); bn_free(&k); |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | static int valid_label(const char *s) |
| 234 | { |
| 235 | size_t n = strlen(s); |
| 236 | if (n < 1 || n > AT1_LABEL_LEN || !(s[0] >= 'a' && s[0] <= 'z')) return 0; |
| 237 | for (size_t i = 1; i < n; i++) |
| 238 | if (!((s[i] >= 'a' && s[i] <= 'z') || is_digit(s[i]) || s[i] == '_')) return 0; |
| 239 | return 1; |
| 240 | } |
| 241 | static int is_alnum(char c) { return is_digit(c) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } |
| 242 | static int valid_name(const char *s) |
| 243 | { |
| 244 | size_t n = strlen(s); |
| 245 | if (n < 1 || n > AT1_NAME_LEN || !is_alnum(s[0])) return 0; |
| 246 | for (size_t i = 1; i < n; i++) if (!(is_alnum(s[i]) || s[i] == '_' || s[i] == '.' || s[i] == '-')) return 0; |
| 247 | return 1; |
| 248 | } |
| 249 | static int valid_digest(const char *s) |
| 250 | { |
| 251 | if (strlen(s) != 64) return 0; |
| 252 | for (int i = 0; i < 64; i++) if (!(is_digit(s[i]) || (s[i] >= 'a' && s[i] <= 'f'))) return 0; |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | /* split "a,b,c" (no empty items); returns item count or -1 on shape failure; "none" is 0 items */ |
| 257 | static int split_list(const char *s, const char ***items, size_t **lens) |
| 258 | { |
| 259 | *items = NULL; *lens = NULL; |
| 260 | if (strcmp(s, "none") == 0) return 0; |
| 261 | size_t n = 1; |
| 262 | for (const char *p = s; *p; p++) if (*p == ',') n++; |
| 263 | *items = at1_xmalloc(n * sizeof(char *)); |
| 264 | *lens = at1_xmalloc(n * sizeof(size_t)); |
| 265 | const char *p = s; size_t i = 0; |
| 266 | for (;;) { |
| 267 | const char *c = strchr(p, ','); |
| 268 | size_t l = c ? (size_t)(c - p) : strlen(p); |
| 269 | if (l == 0) { free(*items); free(*lens); *items = NULL; *lens = NULL; return -1; } |
| 270 | (*items)[i] = p; (*lens)[i] = l; i++; |
| 271 | if (!c) break; |
| 272 | p = c + 1; |
| 273 | } |
| 274 | return (int)n; |
| 275 | } |
| 276 | |
| 277 | /* ---- raw parse state ----------------------------------------------------------------------- */ |
| 278 | typedef struct { |
| 279 | const char *header_v, *domain_v, *contract_v; |
| 280 | long clock_dim, label_count; |
| 281 | int n_energies; bq *energies; |
| 282 | int n_coupling; bq (*coupling)[3]; |
| 283 | int n_labels; char (*labels)[AT1_LABEL_LEN + 1]; |
| 284 | const char **codes; size_t *code_lens; int n_codes; |
| 285 | int sem_begin, sem_end, acc_begin, acc_end; /* line indices */ |
| 286 | } raw_t; |
| 287 | |
| 288 | static void raw_free(raw_t *r) |
| 289 | { |
| 290 | if (r->energies) { for (int i = 0; i < r->n_energies; i++) bq_free(&r->energies[i]); free(r->energies); } |
| 291 | if (r->coupling) { for (int i = 0; i < r->n_coupling; i++) for (int a = 0; a < 3; a++) bq_free(&r->coupling[i][a]); free(r->coupling); } |
| 292 | free(r->labels); free(r->codes); free(r->code_lens); |
| 293 | } |
| 294 | |
| 295 | static void case_init(at1_case *c) |
| 296 | { |
| 297 | memset(c, 0, sizeof *c); |
| 298 | for (int j = 0; j < AT1_CLOCK_DIM_MAX; j++) { bq_init(&c->energies[j]); for (int a = 0; a < 3; a++) bq_init(&c->v[j][a]); } |
| 299 | bq_init(&c->h0); for (int a = 0; a < 3; a++) bq_init(&c->h[a]); |
| 300 | gq_init(&c->psi0[0]); gq_init(&c->psi0[1]); |
| 301 | bq_init(&c->tau); bq_init(&c->weight); |
| 302 | at1_scaled *t[5] = { &c->tol_constraint, &c->tol_povm, &c->tol_probability, &c->tol_zero, &c->tol_schrodinger }; |
| 303 | for (int i = 0; i < 5; i++) { bn_init(&t[i]->n); t[i]->k = 0; } |
| 304 | c->initialized = 1; |
| 305 | } |
| 306 | |
| 307 | void at1_case_free(at1_case *c) |
| 308 | { |
| 309 | if (!c || !c->initialized) return; |
| 310 | for (int j = 0; j < AT1_CLOCK_DIM_MAX; j++) { bq_free(&c->energies[j]); for (int a = 0; a < 3; a++) bq_free(&c->v[j][a]); } |
| 311 | bq_free(&c->h0); for (int a = 0; a < 3; a++) bq_free(&c->h[a]); |
| 312 | gq_free(&c->psi0[0]); gq_free(&c->psi0[1]); |
| 313 | bq_free(&c->tau); bq_free(&c->weight); |
| 314 | at1_scaled *t[5] = { &c->tol_constraint, &c->tol_povm, &c->tol_probability, &c->tol_zero, &c->tol_schrodinger }; |
| 315 | for (int i = 0; i < 5; i++) bn_free(&t[i]->n); |
| 316 | free(c->semantic_text); free(c->acceptance_text); |
| 317 | memset(c, 0, sizeof *c); |
| 318 | } |
| 319 | |
| 320 | #define SHAPE() do { return AT1_CASE_PARSE_ERROR; } while (0) |
| 321 | #define FIXED(text) do { const char *l_ = take(L); if (!l_ || strcmp(l_, (text)) != 0) SHAPE(); } while (0) |
| 322 | #define KEYED(key, rest) do { rest = key_rest(take(L), (key)); if (!rest) SHAPE(); } while (0) |
| 323 | #define ONE_TOKEN(rest) do { if (strchr((rest), ' ')) SHAPE(); } while (0) |
| 324 | |
| 325 | static at1_status parse_shape(lines_t *L, at1_case *c, raw_t *r, flags_t *f) |
| 326 | { |
| 327 | const char *rest; |
| 328 | /* version-bearing lines: key and placement here, value in step 2 (reading d) */ |
| 329 | KEYED("OMEGA-AT1-CASE", rest); r->header_v = rest; |
| 330 | KEYED("domain", rest); r->domain_v = rest; |
| 331 | KEYED("contract", rest); r->contract_v = rest; |
| 332 | KEYED("case_name", rest); |
| 333 | if (!valid_name(rest)) SHAPE(); |
| 334 | strcpy(c->name, rest); |
| 335 | r->sem_begin = L->pos; |
| 336 | FIXED("begin semantic"); |
| 337 | FIXED("model_family PAGE_WOOTTERS_FINITE_CLOCKDIAG"); |
| 338 | FIXED("energy_unit DIMENSIONLESS_HBAR_1"); |
| 339 | KEYED("clock_dim", rest); ONE_TOKEN(rest); |
| 340 | if (!parse_small_int(rest, strlen(rest), &r->clock_dim, f)) SHAPE(); |
| 341 | KEYED("clock_energies", rest); ONE_TOKEN(rest); |
| 342 | { |
| 343 | const char **it; size_t *ln; |
| 344 | int n = split_list(rest, &it, &ln); |
| 345 | if (n < 0 || n != r->clock_dim) { free(it); free(ln); SHAPE(); } |
| 346 | r->energies = at1_xmalloc((size_t)(n ? n : 1) * sizeof(bq)); |
| 347 | for (int i = 0; i < n; i++) bq_init(&r->energies[i]); |
| 348 | r->n_energies = n; |
| 349 | for (int i = 0; i < n; i++) if (!parse_rat_tok(it[i], ln[i], &r->energies[i], f)) { free(it); free(ln); SHAPE(); } |
| 350 | free(it); free(ln); |
| 351 | } |
| 352 | FIXED("system_dim 2"); |
| 353 | KEYED("system_hamiltonian_pauli", rest); ONE_TOKEN(rest); |
| 354 | { |
| 355 | const char **it; size_t *ln; |
| 356 | int n = split_list(rest, &it, &ln); |
| 357 | if (n != 4) { free(it); free(ln); SHAPE(); } |
| 358 | int ok = parse_rat_tok(it[0], ln[0], &c->h0, f) && parse_rat_tok(it[1], ln[1], &c->h[0], f) && |
| 359 | parse_rat_tok(it[2], ln[2], &c->h[1], f) && parse_rat_tok(it[3], ln[3], &c->h[2], f); |
| 360 | free(it); free(ln); |
| 361 | if (!ok) SHAPE(); |
| 362 | } |
| 363 | FIXED("interaction CLOCK_DIAGONAL_PAULI"); |
| 364 | { |
| 365 | /* repeated block; clock_dim is its count line (AT1_CASE_V1 section 1) */ |
| 366 | int start = L->pos, n = 0; |
| 367 | while (peek(L) && line_key_is(peek(L), "interaction_pauli")) { L->pos++; n++; } |
| 368 | if (n != r->clock_dim) SHAPE(); |
| 369 | L->pos = start; |
| 370 | r->coupling = at1_xmalloc((size_t)(n ? n : 1) * sizeof *r->coupling); |
| 371 | for (int i = 0; i < n; i++) for (int a = 0; a < 3; a++) bq_init(&r->coupling[i][a]); |
| 372 | r->n_coupling = n; |
| 373 | for (int j = 0; j < n; j++) { |
| 374 | KEYED("interaction_pauli", rest); |
| 375 | const char *sp = strchr(rest, ' '); |
| 376 | if (!sp || strchr(sp + 1, ' ')) SHAPE(); |
| 377 | long idx; |
| 378 | if (!parse_small_int(rest, (size_t)(sp - rest), &idx, f) || idx != j) SHAPE(); /* reading e */ |
| 379 | const char **it; size_t *ln; |
| 380 | int m = split_list(sp + 1, &it, &ln); |
| 381 | if (m != 3) { free(it); free(ln); SHAPE(); } |
| 382 | int ok = 1; |
| 383 | for (int a = 0; a < 3; a++) ok = ok && parse_rat_tok(it[a], ln[a], &r->coupling[j][a], f); |
| 384 | free(it); free(ln); |
| 385 | if (!ok) SHAPE(); |
| 386 | } |
| 387 | } |
| 388 | FIXED("constraint SUM_HC_HS_V"); |
| 389 | FIXED("physical_state NULLSPACE_PROJECTION"); |
| 390 | KEYED("reference_clock_label", rest); |
| 391 | if (!valid_label(rest)) SHAPE(); |
| 392 | strcpy(c->ref_label, rest); |
| 393 | KEYED("reference_system_state", rest); ONE_TOKEN(rest); |
| 394 | { |
| 395 | const char **it; size_t *ln; |
| 396 | int n = split_list(rest, &it, &ln); |
| 397 | int ok = n == 2 && parse_crat_tok(it[0], ln[0], &c->psi0[0], f) && parse_crat_tok(it[1], ln[1], &c->psi0[1], f); |
| 398 | free(it); free(ln); |
| 399 | if (!ok) SHAPE(); |
| 400 | } |
| 401 | FIXED("clock_povm COVARIANT_DISCRETE"); |
| 402 | KEYED("povm_tau_turns", rest); ONE_TOKEN(rest); |
| 403 | if (!parse_rat_tok(rest, strlen(rest), &c->tau, f)) SHAPE(); |
| 404 | KEYED("povm_weight", rest); ONE_TOKEN(rest); |
| 405 | if (!parse_rat_tok(rest, strlen(rest), &c->weight, f)) SHAPE(); |
| 406 | KEYED("clock_label_count", rest); ONE_TOKEN(rest); |
| 407 | if (!parse_small_int(rest, strlen(rest), &r->label_count, f)) SHAPE(); |
| 408 | { |
| 409 | int start = L->pos, n = 0; |
| 410 | while (peek(L) && line_key_is(peek(L), "clock_label")) { L->pos++; n++; } |
| 411 | if (n != r->label_count) SHAPE(); |
| 412 | L->pos = start; |
| 413 | r->labels = at1_xmalloc((size_t)(n ? n : 1) * sizeof *r->labels); |
| 414 | r->n_labels = n; |
| 415 | for (int k = 0; k < n; k++) { |
| 416 | KEYED("clock_label", rest); |
| 417 | const char *sp = strchr(rest, ' '); |
| 418 | if (!sp || strchr(sp + 1, ' ')) SHAPE(); |
| 419 | long idx; |
| 420 | if (!parse_small_int(rest, (size_t)(sp - rest), &idx, f) || idx != k) SHAPE(); |
| 421 | if (!valid_label(sp + 1)) SHAPE(); |
| 422 | strcpy(r->labels[k], sp + 1); |
| 423 | } |
| 424 | } |
| 425 | FIXED("observables PAULI_X,PAULI_Y,PAULI_Z"); |
| 426 | r->sem_end = L->pos; |
| 427 | FIXED("end semantic"); |
| 428 | r->acc_begin = L->pos; |
| 429 | FIXED("begin acceptance"); |
| 430 | KEYED("control_kind", rest); |
| 431 | if (strcmp(rest, "POSITIVE") == 0) c->control_positive = 1; |
| 432 | else if (strcmp(rest, "NEGATIVE") == 0) c->control_positive = 0; |
| 433 | else SHAPE(); |
| 434 | KEYED("prediction_target", rest); |
| 435 | if (strcmp(rest, "IDEAL") == 0) c->target = AT1_TARGET_IDEAL; |
| 436 | else if (strcmp(rest, "INTERACTING") == 0) c->target = AT1_TARGET_INTERACTING; |
| 437 | else SHAPE(); |
| 438 | KEYED("expected_outcome", rest); |
| 439 | if (strcmp(rest, "PASS") == 0) c->expected_outcome = AT1_EXPECT_PASS; |
| 440 | else if (strcmp(rest, "FAIL") == 0) c->expected_outcome = AT1_EXPECT_FAIL; |
| 441 | else SHAPE(); |
| 442 | KEYED("expected_failure_codes", rest); ONE_TOKEN(rest); |
| 443 | r->n_codes = split_list(rest, &r->codes, &r->code_lens); |
| 444 | if (r->n_codes < 0) SHAPE(); |
| 445 | KEYED("min_bound_kind", rest); |
| 446 | if (strcmp(rest, "RIGOROUS") == 0) c->min_bound_kind = AT1_BOUND_RIGOROUS; |
| 447 | else if (strcmp(rest, "ESTIMATED") == 0) c->min_bound_kind = AT1_BOUND_ESTIMATED; |
| 448 | else if (strcmp(rest, "NONE") == 0) c->min_bound_kind = AT1_BOUND_NONE; |
| 449 | else SHAPE(); |
| 450 | static const char *tol_keys[5] = { "tol_constraint_residual", "tol_povm_residual", "tol_probability", |
| 451 | "tol_zero_probability", "tol_schrodinger" }; |
| 452 | at1_scaled *tols[5] = { &c->tol_constraint, &c->tol_povm, &c->tol_probability, &c->tol_zero, &c->tol_schrodinger }; |
| 453 | for (int i = 0; i < 5; i++) { |
| 454 | KEYED(tol_keys[i], rest); ONE_TOKEN(rest); |
| 455 | if (!parse_scaled_tok(rest, strlen(rest), tols[i], f)) SHAPE(); |
| 456 | } |
| 457 | r->acc_end = L->pos; |
| 458 | FIXED("end acceptance"); |
| 459 | KEYED("case_id", rest); |
| 460 | if (!valid_digest(rest)) SHAPE(); |
| 461 | strcpy(c->case_id, rest); |
| 462 | KEYED("acceptance_id", rest); |
| 463 | if (!valid_digest(rest)) SHAPE(); |
| 464 | strcpy(c->acceptance_id, rest); |
| 465 | FIXED("end"); |
| 466 | if (L->pos != L->count) SHAPE(); /* trailing lines */ |
| 467 | return AT1_OK; |
| 468 | } |
| 469 | |
| 470 | static at1_status check_parameters(at1_case *c, raw_t *r, const flags_t *f) |
| 471 | { |
| 472 | if (f->over_limit || f->scaled_k_over) return AT1_CASE_INVALID_PARAMETER; |
| 473 | if (r->clock_dim < AT1_CLOCK_DIM_MIN || r->clock_dim > AT1_CLOCK_DIM_MAX) return AT1_CASE_INVALID_PARAMETER; |
| 474 | int N = (int)r->clock_dim; |
| 475 | for (int j = 1; j < N; j++) if (bq_cmp(&r->energies[j - 1], &r->energies[j]) >= 0) return AT1_CASE_INVALID_PARAMETER; |
| 476 | if (gq_is_zero(&c->psi0[0]) && gq_is_zero(&c->psi0[1])) return AT1_CASE_INVALID_PARAMETER; |
| 477 | if (bq_sign(&c->weight) <= 0 || bq_sign(&c->tau) <= 0) return AT1_CASE_INVALID_PARAMETER; |
| 478 | if (r->label_count < 1 || r->label_count > AT1_LABEL_MAX) return AT1_CASE_INVALID_PARAMETER; |
| 479 | int M = (int)r->label_count; |
| 480 | c->ref_index = -1; |
| 481 | for (int k = 0; k < M; k++) { |
| 482 | for (int l = 0; l < k; l++) if (strcmp(r->labels[k], r->labels[l]) == 0) return AT1_CASE_INVALID_PARAMETER; |
| 483 | if (strcmp(r->labels[k], c->ref_label) == 0) c->ref_index = k; |
| 484 | } |
| 485 | if (c->ref_index < 0) return AT1_CASE_INVALID_PARAMETER; |
| 486 | c->clock_dim = N; c->label_count = M; |
| 487 | for (int j = 0; j < N; j++) { |
| 488 | bq_copy(&c->energies[j], &r->energies[j]); |
| 489 | for (int a = 0; a < 3; a++) bq_copy(&c->v[j][a], &r->coupling[j][a]); |
| 490 | } |
| 491 | for (int k = 0; k < M; k++) strcpy(c->labels[k], r->labels[k]); |
| 492 | return AT1_OK; |
| 493 | } |
| 494 | |
| 495 | /* rule 4: |h + v_j|^2 is the square of a rational for every level, decided exactly */ |
| 496 | static at1_status check_spectrum(const at1_case *c) |
| 497 | { |
| 498 | at1_status st = AT1_OK; |
| 499 | bq s, t, comp; bn root; bq_init(&s); bq_init(&t); bq_init(&comp); bn_init(&root); |
| 500 | for (int j = 0; j < c->clock_dim && st == AT1_OK; j++) { |
| 501 | bq_set_i64(&s, 0, 1); |
| 502 | for (int a = 0; a < 3; a++) { |
| 503 | bq_add(&comp, &c->h[a], &c->v[j][a]); |
| 504 | bq_mul(&t, &comp, &comp); |
| 505 | bq_add(&s, &s, &t); |
| 506 | } |
| 507 | /* s is reduced with den > 0, so it is a rational square iff num and den are squares */ |
| 508 | if (!bn_isqrt_exact(&s.num, &root) || !bn_isqrt_exact(&s.den, &root)) st = AT1_CASE_IRRATIONAL_SPECTRUM; |
| 509 | } |
| 510 | bq_free(&s); bq_free(&t); bq_free(&comp); bn_free(&root); |
| 511 | return st; |
| 512 | } |
| 513 | |
| 514 | static at1_status check_acceptance(at1_case *c, const raw_t *r) |
| 515 | { |
| 516 | c->expected_codes = 0; |
| 517 | if (c->expected_outcome == AT1_EXPECT_PASS) return r->n_codes == 0 ? AT1_OK : AT1_CASE_INVALID_PARAMETER; |
| 518 | if (r->n_codes == 0) return AT1_CASE_INVALID_PARAMETER; |
| 519 | int prev = -1; |
| 520 | for (int i = 0; i < r->n_codes; i++) { |
| 521 | int idx = -1; |
| 522 | for (int j = 0; j < AT1_FAILURE_CODE_COUNT; j++) |
| 523 | if (strlen(at1_failure_codes[j]) == r->code_lens[i] && memcmp(r->codes[i], at1_failure_codes[j], r->code_lens[i]) == 0) { idx = j; break; } |
| 524 | if (idx < 0 || idx <= prev) return AT1_CASE_INVALID_PARAMETER; /* unknown, duplicate or unsorted */ |
| 525 | c->expected_codes |= 1u << idx; prev = idx; |
| 526 | } |
| 527 | return AT1_OK; |
| 528 | } |
| 529 | |
| 530 | at1_status at1_case_parse(const uint8_t *bytes, size_t len, at1_case *c) |
| 531 | { |
| 532 | lines_t L; raw_t r; flags_t f; |
| 533 | memset(&r, 0, sizeof r); memset(&f, 0, sizeof f); |
| 534 | case_init(c); |
| 535 | at1_status st; |
| 536 | if (len > AT1_FILE_MAX) { st = AT1_ERR_RESOURCE; L.line = NULL; L.off = NULL; L.copy = NULL; goto done; } |
| 537 | st = split_lines(bytes, len, &L); |
| 538 | if (st == AT1_OK) st = parse_shape(&L, c, &r, &f); |
| 539 | if (st == AT1_OK && f.resource) st = AT1_ERR_RESOURCE; |
| 540 | if (st == AT1_OK && f.noncanonical) st = AT1_CASE_NONCANONICAL; /* still step 1 */ |
| 541 | if (st == AT1_OK) { /* step 2 */ |
| 542 | if (strcmp(r.header_v, "v1") != 0 || strcmp(r.domain_v, "omega.at1.case.v1") != 0 || |
| 543 | strcmp(r.contract_v, "AT1_CASE_V1") != 0) st = AT1_CASE_UNSUPPORTED_VERSION; |
| 544 | } |
| 545 | if (st == AT1_OK) st = check_parameters(c, &r, &f); /* step 3 */ |
| 546 | if (st == AT1_OK) st = check_spectrum(c); /* step 4 */ |
| 547 | if (st == AT1_OK) { /* step 5 */ |
| 548 | size_t s0 = L.off[r.sem_begin], s1 = L.off[r.sem_end + 1]; |
| 549 | size_t a0 = L.off[r.acc_begin], a1 = L.off[r.acc_end + 1]; |
| 550 | char cid[65], aid[65]; |
| 551 | at1_tagged_sha256("omega.at1.case.v1", (const char *)bytes + s0, s1 - s0, cid); |
| 552 | at1_tagged_sha256("omega.at1.acceptance.v1", (const char *)bytes + a0, a1 - a0, aid); |
| 553 | if (strcmp(cid, c->case_id) != 0 || strcmp(aid, c->acceptance_id) != 0) st = AT1_CASE_ID_MISMATCH; |
| 554 | else { |
| 555 | c->semantic_len = s1 - s0; c->semantic_text = at1_xmalloc(c->semantic_len + 1); |
| 556 | memcpy(c->semantic_text, bytes + s0, c->semantic_len); c->semantic_text[c->semantic_len] = 0; |
| 557 | c->acceptance_len = a1 - a0; c->acceptance_text = at1_xmalloc(c->acceptance_len + 1); |
| 558 | memcpy(c->acceptance_text, bytes + a0, c->acceptance_len); c->acceptance_text[c->acceptance_len] = 0; |
| 559 | } |
| 560 | } |
| 561 | if (st == AT1_OK) st = check_acceptance(c, &r); /* step 6 */ |
| 562 | if (st == AT1_OK) at1_sha256_hex(bytes, len, c->case_file_sha256); |
| 563 | done: |
| 564 | raw_free(&r); |
| 565 | lines_free(&L); |
| 566 | if (st != AT1_OK) at1_case_free(c); |
| 567 | return st; |
| 568 | } |