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 / src / turing / field.c

666 lines · 26303 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/* TURING Field V0 records: canonical bytes, digests, store, adapters, verify.
18 * See field.h. Plain C11. */
19#include "turing/field.h"
20#include "omega_native.h"
21
22#include "algebra/realize_common.h"
23#include "omega_canonical.h"
24#include "sha256.h"
25
26#include <errno.h>
27#include <inttypes.h>
28#include <math.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#define TURING_CANON_CAP 40000
34
35static const char *const reason_names[TURING_R__COUNT] = {
36 "CHOSEN", "RANKED", "TIED", "CONTRACT_MISMATCH", "NOT_EXACT", "MAX_N", "NO_EVIDENCE",
37 "RECEIPT_UNVERIFIED", "UNVERIFIED_RUN", "CONTENTION_FORCED", "TIER", "EXPLORE"};
38
39const char *turing_reason_name(turing_reason r) {
40 return (r >= 0 && r < TURING_R__COUNT) ? reason_names[r] : "?";
41}
42
43/* ---------------------------------------------------------------- digests */
44
45void turing_hex(const turing_digest *d, char out[65]) {
46 uint64_t u = 0;
47 (void)omega2_turing_om_hex(&u, d->b, TURING_DIGEST_BYTES, (uint8_t *)out, 64);
48 out[64] = 0;
49}
50
51void turing_hex_short(const turing_digest *d, char out[13]) {
52 char h[65];
53 turing_hex(d, h);
54 memcpy(out, h, 12);
55 out[12] = 0;
56}
57
58int turing_digest_eq(const turing_digest *a, const turing_digest *b) {
59 uint64_t eq = 0;
60 (void)omega2_turing_om_digest_eq(&eq, a->b, TURING_DIGEST_BYTES, b->b, TURING_DIGEST_BYTES);
61 return (int)eq;
62}
63
64int turing_file_digest(const char *path, turing_digest *out) {
65 FILE *f = fopen(path, "rb");
66 if (!f) return -1;
67 sha256_ctx c;
68 sha256_init(&c);
69 uint8_t buf[8192];
70 size_t k;
71 while ((k = fread(buf, 1, sizeof buf, f)) > 0) sha256_update(&c, buf, k);
72 int err = ferror(f);
73 fclose(f);
74 if (err) return -1;
75 sha256_final(&c, out->b);
76 return 0;
77}
78
79/* OMG0 object builder. */
80typedef struct {
81 OmegaObject *o;
82 int err;
83} obj_b;
84
85static void ob_text(obj_b *b, const char *key, const char *val) {
86 if (b->err) return;
87 size_t kl = strlen(key), vl = strlen(val);
88 if (b->o->attr_count >= OMEGA_MAX_ATTRIBUTES || kl >= OMEGA_MAX_KEY_LEN || vl > OMEGA_MAX_VAL_LEN) {
89 b->err = 1;
90 return;
91 }
92 OmegaAttribute *a = &b->o->attributes[b->o->attr_count++];
93 memcpy(a->key, key, kl + 1);
94 memcpy(a->value, val, vl);
95 a->val_len = (uint16_t)vl;
96}
97
98static void ob_u64(obj_b *b, const char *key, uint64_t v) {
99 char t[24];
100 snprintf(t, sizeof t, "%" PRIu64, v);
101 ob_text(b, key, t);
102}
103
104static void ob_digest(obj_b *b, const char *key, const turing_digest *d) {
105 char h[65];
106 turing_hex(d, h);
107 ob_text(b, key, h);
108}
109
110static void ob_rel(obj_b *b, uint16_t kind, const turing_digest *d) {
111 if (b->err) return;
112 if (b->o->rel_count >= OMEGA_MAX_RELATIONS) {
113 b->err = 1;
114 return;
115 }
116 OmegaRelation *r = &b->o->relations[b->o->rel_count++];
117 r->kind = kind;
118 memcpy(r->target_id.bytes, d->b, TURING_DIGEST_BYTES);
119}
120
121static OmegaObject *ob_new(obj_b *b, SemanticKind kind, const char *record) {
122 b->err = 0;
123 b->o = calloc(1, sizeof(OmegaObject));
124 if (!b->o) return NULL;
125 b->o->kind = kind;
126 ob_text(b, "record", record);
127 return b->o;
128}
129
130static int ob_finish(obj_b *b, const char *domain, turing_digest *out, uint8_t *copy, size_t cap, size_t *len) {
131 int rc = -1;
132 uint8_t *buf = NULL;
133 size_t n = 0;
134 if (!b->o || b->err) goto done;
135 buf = malloc(TURING_CANON_CAP);
136 if (!buf) goto done;
137 if (omega_canonical_encode(b->o, buf, TURING_CANON_CAP, &n) != 0) goto done;
138 if (out) {
139 sha256_ctx c;
140 sha256_init(&c);
141 sha256_update(&c, (const uint8_t *)domain, strlen(domain) + 1); /* includes the 0x00 separator */
142 sha256_update(&c, buf, n);
143 sha256_final(&c, out->b);
144 }
145 if (copy) {
146 if (n > cap) goto done;
147 memcpy(copy, buf, n);
148 *len = n;
149 }
150 rc = 0;
151done:
152 free(buf);
153 free(b->o);
154 b->o = NULL;
155 return rc;
156}
157
158/* --------------------------------------------------------------- contract */
159
160void turing_contract_omega_x(turing_contract *c) {
161 memset(c, 0, sizeof *c);
162 snprintf(c->name, sizeof c->name, "Omega-X ternary GEMV");
163 snprintf(c->statement, sizeof c->statement,
164 "y = W.x; W in {-1,0,+1}^(m x n) canonical row-major int8; x in int8^n; y in int32^m");
165 snprintf(c->exactness, sizeof c->exactness, "EXACT: bit-identical to the naive integer oracle");
166 snprintf(c->overflow, sizeof c->overflow, "|y_i| <= 128*n; n <= floor(INT32_MAX/128)");
167 snprintf(c->oracle, sizeof c->oracle, "oma_rz_oracle (src/algebra/realize_common.h)");
168 snprintf(c->source, sizeof c->source, "spec/mixed-algebra-ma2.md");
169 c->max_n = (uint64_t)OMA_RZ_MAX_N;
170}
171
172static int contract_obj(const turing_contract *c, obj_b *b) {
173 if (!ob_new(b, KIND_OPERATION, "turing.contract")) return -1;
174 ob_text(b, "status", "PROVISIONAL V0 contract digest; not an Omega semantic id (OSC-0B open)");
175 ob_text(b, "name", c->name);
176 ob_text(b, "statement", c->statement);
177 ob_text(b, "exactness", c->exactness);
178 ob_text(b, "overflow", c->overflow);
179 ob_text(b, "oracle", c->oracle);
180 ob_text(b, "source", c->source);
181 ob_u64(b, "max_n", c->max_n);
182 return 0;
183}
184
185int turing_contract_digest(const turing_contract *c, turing_digest *out) {
186 obj_b b;
187 if (!c || contract_obj(c, &b) != 0) return -1;
188 return ob_finish(&b, TURING_DOMAIN_CONTRACT, out, NULL, 0, NULL);
189}
190
191/* ------------------------------------------------------------------- spec */
192
193static int spec_obj(const turing_rz_spec *s, obj_b *b) {
194 if (!ob_new(b, KIND_REALIZATION, "turing.rz_spec")) return -1;
195 ob_digest(b, "contract", &s->contract_digest);
196 ob_text(b, "rz_id", s->rz_id);
197 ob_text(b, "algorithm", s->algorithm);
198 ob_text(b, "algebra", s->algebra);
199 ob_text(b, "representation", s->representation);
200 ob_text(b, "precision", s->precision);
201 ob_text(b, "language", s->language);
202 ob_text(b, "backend", s->backend);
203 ob_text(b, "machine_class", s->machine_class);
204 ob_u64(b, "max_n", s->max_n);
205 ob_u64(b, "exact", (uint64_t)s->exact);
206 ob_u64(b, "weak_baseline", (uint64_t)s->weak_baseline);
207 return 0;
208}
209
210int turing_spec_digest(const turing_rz_spec *s, turing_digest *out) {
211 obj_b b;
212 if (!s || spec_obj(s, &b) != 0) return -1;
213 return ob_finish(&b, TURING_DOMAIN_SPEC, out, NULL, 0, NULL);
214}
215
216int turing_spec_bytes(const turing_rz_spec *s, uint8_t *buf, size_t cap, size_t *len) {
217 obj_b b;
218 if (!s || !buf || !len || spec_obj(s, &b) != 0) return -1;
219 return ob_finish(&b, TURING_DOMAIN_SPEC, NULL, buf, cap, len);
220}
221
222static const struct {
223 const char *family, *algebra, *representation;
224} family_map[] = {
225 {"binary", "two's complement integer (int8 weights)", "int8 per weight"},
226 {"bitplane", "balanced ternary", "pos/neg bitplanes, 2 bits per weight"},
227 {"lut", "balanced ternary", "4-activation subset-sum LUT codes, 2 bits per weight"},
228 {"crumb2", "balanced ternary", "2-bit two's-complement crumbs"},
229 {"sparse", "balanced ternary (signed index sets)", "uint16 +1/-1 index lists per row"},
230 {"rns", "residue number system {256,255,253}", "int8 weights, per-call residues of x"},
231 {"dense5", "balanced ternary", "5 trits per byte, 1.6 bits per weight"},
232};
233
234int turing_spec_from_fields(const turing_digest *contract, const char *rz_id, const char *family,
235 int exact, int weak_baseline, uint64_t max_n, turing_rz_spec *s) {
236 if (!contract || !rz_id || !family || !s || strlen(rz_id) >= sizeof s->rz_id) return -1;
237 size_t k = sizeof family_map / sizeof family_map[0], i;
238 for (i = 0; i < k; ++i)
239 if (strcmp(family_map[i].family, family) == 0) break;
240 if (i == k) return -1;
241 memset(s, 0, sizeof *s);
242 s->contract_digest = *contract;
243 snprintf(s->rz_id, sizeof s->rz_id, "%s", rz_id);
244 snprintf(s->algorithm, sizeof s->algorithm, "oma_rz_impl %s (pack once, run per x)", rz_id);
245 snprintf(s->algebra, sizeof s->algebra, "%s", family_map[i].algebra);
246 snprintf(s->representation, sizeof s->representation, "%s", family_map[i].representation);
247 snprintf(s->precision, sizeof s->precision, "int8 x trit -> int32, exact");
248 snprintf(s->language, sizeof s->language, "C11 + ACLE NEON/I8MM intrinsics");
249 snprintf(s->backend, sizeof s->backend, "cpu, single core, single thread");
250 snprintf(s->machine_class, sizeof s->machine_class, "aarch64 armv8.6-a+dotprod+i8mm+sve");
251 s->max_n = max_n;
252 s->exact = exact;
253 s->weak_baseline = weak_baseline;
254 return 0;
255}
256
257/* --------------------------------------------------------------- evidence */
258
259static int evidence_obj(const turing_evidence *e, obj_b *b) {
260 if (!ob_new(b, KIND_EVIDENCE, "turing.evidence")) return -1;
261 ob_digest(b, "spec", &e->spec_id);
262 ob_u64(b, "n", e->n);
263 ob_u64(b, "m", e->m);
264 ob_u64(b, "sparsity_milli", e->sparsity_milli);
265 ob_digest(b, "receipt", &e->receipt_digest);
266 ob_text(b, "run_id", e->run_id);
267 ob_text(b, "tier", e->tier);
268 ob_text(b, "tier_source", e->tier_source);
269 ob_u64(b, "verified", (uint64_t)e->verified);
270 ob_u64(b, "forced", e->forced);
271 ob_u64(b, "retries", e->retries);
272 ob_u64(b, "samples", e->samples);
273 ob_u64(b, "median_ps", e->median_ps);
274 ob_u64(b, "q25_ps", e->q25_ps);
275 ob_u64(b, "q75_ps", e->q75_ps);
276 ob_u64(b, "min_ps", e->min_ps);
277 ob_u64(b, "pack_ps", e->pack_ps);
278 ob_u64(b, "noise_ppm", e->noise_ppm);
279 ob_u64(b, "pack_noise_ppm", e->pack_noise_ppm);
280 ob_u64(b, "weight_bytes", e->weight_bytes);
281 ob_u64(b, "working_set_bytes", e->working_set_bytes);
282 ob_text(b, "build_digest", e->build_digest);
283 ob_text(b, "git_commit", e->git_commit);
284 ob_u64(b, "tree_dirty", (uint64_t)e->tree_dirty);
285 ob_text(b, "toolchain", e->toolchain);
286 ob_text(b, "cpu_freq_state", e->cpu_freq_state);
287 ob_text(b, "quiet_flag", e->quiet_flag);
288 ob_text(b, "thermal", e->thermal);
289 return 0;
290}
291
292int turing_evidence_digest(const turing_evidence *e, turing_digest *out) {
293 obj_b b;
294 if (!e || evidence_obj(e, &b) != 0) return -1;
295 return ob_finish(&b, TURING_DOMAIN_EVIDENCE, out, NULL, 0, NULL);
296}
297
298uint64_t turing_ev_cost(const turing_evidence *e, int pack) {
299 return e->median_ps + (pack == TURING_PACK_PER_CALL ? e->pack_ps : 0);
300}
301
302/* --------------------------------------------------------------- decision */
303
304/* Cite-set digest: SHA-256(TURING_DOMAIN_CITESET 0x00 || sorted cited evidence
305 * digests). One DERIVED_FROM relation commits to the whole cite list, so a v1
306 * decision can cite a full footprint (up to TURING_MAX_CITE rows) within the
307 * OMG0 relation limit. */
308static int digest_cmp(const void *a, const void *b) { return memcmp(a, b, TURING_DIGEST_BYTES); }
309
310static void citeset_digest(const turing_decision *d, turing_digest *out) {
311 turing_digest sorted[TURING_MAX_CITE];
312 memcpy(sorted, d->cite, d->ncite * sizeof sorted[0]);
313 qsort(sorted, d->ncite, sizeof sorted[0], digest_cmp);
314 sha256_ctx c;
315 sha256_init(&c);
316 sha256_update(&c, (const uint8_t *)TURING_DOMAIN_CITESET, strlen(TURING_DOMAIN_CITESET) + 1);
317 for (size_t i = 0; i < d->ncite; ++i) sha256_update(&c, sorted[i].b, TURING_DIGEST_BYTES);
318 sha256_final(&c, out->b);
319}
320
321static int decision_obj(const turing_decision *d, obj_b *b) {
322 if (d->ncand > TURING_MAX_CAND || d->ncite > TURING_MAX_CITE) return -1;
323 if (!ob_new(b, KIND_EVIDENCE, "turing.decision")) return -1;
324 ob_digest(b, "contract", &d->contract_digest);
325 ob_text(b, "selector", d->selector);
326 ob_text(b, "constraints", d->constraints);
327 ob_u64(b, "n", d->n);
328 ob_u64(b, "m", d->m);
329 ob_u64(b, "sparsity_milli", d->sparsity_milli);
330 ob_text(b, "pack", d->pack == TURING_PACK_PER_CALL ? "per_call" : "once_amortized");
331 ob_u64(b, "cell_n", d->cell_n);
332 ob_u64(b, "cell_m", d->cell_m);
333 ob_u64(b, "cell_sparsity_milli", d->cell_sparsity_milli);
334 ob_u64(b, "exact_cell", (uint64_t)d->exact_cell);
335 char list[OMEGA_MAX_VAL_LEN + 1];
336 size_t pos = 0;
337 list[0] = 0;
338 for (size_t i = 0; i < d->ncand; ++i) {
339 int w = snprintf(list + pos, sizeof list - pos, "%s%s=%s:%" PRIu64, i ? ";" : "", d->cand_rz[i],
340 turing_reason_name(d->reason[i]), d->cost_ps[i]);
341 if (w < 0 || (size_t)w >= sizeof list - pos) return b->err = 1, -1;
342 pos += (size_t)w;
343 }
344 ob_text(b, "candidates", list);
345 ob_text(b, "chosen", d->chosen >= 0 && (size_t)d->chosen < d->ncand ? d->cand_rz[d->chosen] : "-");
346 ob_text(b, "verdict", d->verdict);
347 ob_text(b, "tie_resolution", d->tie_resolution);
348 ob_u64(b, "margin_ppm", d->margin_ppm);
349 ob_u64(b, "band_ppm", d->band_ppm);
350 if (d->has_supersedes)
351 ob_digest(b, "supersedes", &d->supersedes);
352 else
353 ob_text(b, "supersedes", "none");
354 for (size_t i = 0; i < d->ncand; ++i) ob_rel(b, REL_DEPENDS_ON, &d->cand[i]);
355 ob_u64(b, "cite_count", d->ncite);
356 if (d->ncite) {
357 turing_digest cs;
358 citeset_digest(d, &cs);
359 ob_rel(b, REL_DERIVED_FROM, &cs);
360 }
361 return 0;
362}
363
364int turing_decision_digest(const turing_decision *d, turing_digest *out) {
365 obj_b b;
366 b.o = NULL;
367 if (!d || decision_obj(d, &b) != 0) {
368 free(b.o);
369 return -1;
370 }
371 return ob_finish(&b, TURING_DOMAIN_DECISION, out, NULL, 0, NULL);
372}
373
374/* ------------------------------------------------------------------ store */
375
376turing_store *turing_store_new(void) {
377 turing_store *st = calloc(1, sizeof *st);
378 if (!st) return NULL;
379 turing_contract_omega_x(&st->contract);
380 if (turing_contract_digest(&st->contract, &st->contract_digest) != 0) {
381 free(st);
382 return NULL;
383 }
384 return st;
385}
386
387void turing_store_free(turing_store *st) { free(st); }
388
389int turing_find_spec(const turing_store *st, const char *rz_id) {
390 for (size_t i = 0; i < st->nspec; ++i)
391 if (strcmp(st->spec[i].rz_id, rz_id) == 0) return (int)i;
392 return -1;
393}
394
395int turing_find_evidence(const turing_store *st, const turing_digest *id) {
396 for (size_t i = 0; i < st->nev; ++i)
397 if (turing_digest_eq(&st->ev_id[i], id)) return (int)i;
398 return -1;
399}
400
401/* Add a spec; an existing spec with the same rz_id must have the same id. */
402static int store_add_spec(turing_store *st, const turing_rz_spec *s) {
403 turing_digest id;
404 if (turing_spec_digest(s, &id) != 0) return -1;
405 int k = turing_find_spec(st, s->rz_id);
406 if (k >= 0) return turing_digest_eq(&st->spec_id[k], &id) ? k : -1;
407 if (st->nspec >= TURING_MAX_SPECS) return -1;
408 st->spec[st->nspec] = *s;
409 st->spec_id[st->nspec] = id;
410 return (int)st->nspec++;
411}
412
413int turing_ingest_registry(turing_store *st) {
414 size_t n = oma_rz_count();
415 for (size_t i = 0; i < n; ++i) {
416 const oma_rz_impl *r = oma_rz_get(i);
417 turing_rz_spec s;
418 if (!r || turing_spec_from_fields(&st->contract_digest, r->id, r->family, r->exact, r->weak_baseline,
419 (uint64_t)r->max_n, &s) != 0)
420 return -1;
421 if (store_add_spec(st, &s) < 0) return -1;
422 }
423 return (int)n;
424}
425
426/* ---------------------------------------------------- receipt line parser */
427
428/* Find `"key": ` in line; return pointer to the value or NULL. */
429static const char *jfind(const char *line, const char *key) {
430 char pat[80];
431 snprintf(pat, sizeof pat, "\"%s\": ", key);
432 const char *p = strstr(line, pat);
433 return p ? p + strlen(pat) : NULL;
434}
435
436static int jstr(const char *line, const char *key, char *out, size_t cap) {
437 const char *p = jfind(line, key);
438 if (!p || *p != '"') return -1;
439 ++p;
440 size_t k = 0;
441 while (*p && *p != '"') {
442 if (*p == '\\' && p[1]) ++p;
443 if (k + 1 >= cap) return -1;
444 out[k++] = *p++;
445 }
446 if (*p != '"') return -1;
447 out[k] = 0;
448 return 0;
449}
450
451static int jnum(const char *line, const char *key, double *out) {
452 const char *p = jfind(line, key);
453 if (!p) return -1;
454 char *end;
455 errno = 0;
456 double v = strtod(p, &end);
457 if (end == p || errno) return -1;
458 *out = v;
459 return 0;
460}
461
462static int ju64(const char *line, const char *key, uint64_t *out) {
463 double v;
464 if (jnum(line, key, &v) != 0 || v < 0 || v > 1.8e19) return -1;
465 *out = (uint64_t)llround(v);
466 return 0;
467}
468
469static int jbool(const char *line, const char *key, int *out) {
470 const char *p = jfind(line, key);
471 if (!p) return -1;
472 if (strncmp(p, "true", 4) == 0) return *out = 1, 0;
473 if (strncmp(p, "false", 5) == 0) return *out = 0, 0;
474 return -1;
475}
476
477static char *read_file(const char *path, size_t *len) {
478 FILE *f = fopen(path, "rb");
479 if (!f) return NULL;
480 size_t cap = 1 << 16, n = 0;
481 char *buf = malloc(cap + 1);
482 size_t k;
483 while (buf && (k = fread(buf + n, 1, cap - n, f)) > 0) {
484 n += k;
485 if (n == cap) {
486 char *nb = realloc(buf, cap * 2 + 1);
487 if (!nb) {
488 free(buf);
489 buf = NULL;
490 break;
491 }
492 buf = nb;
493 cap *= 2;
494 }
495 }
496 fclose(f);
497 if (!buf) return NULL;
498 buf[n] = 0;
499 if (len) *len = n;
500 return buf;
501}
502
503int turing_ingest_receipt(turing_store *st, const char *path) {
504 turing_digest rd;
505 if (turing_file_digest(path, &rd) != 0) return -1;
506 char *text = read_file(path, NULL);
507 if (!text) return -1;
508 int added = 0, rc = -1, section = 0; /* 0 header, 1 realizations, 2 cost_table, 3 after */
509 char schema[TURING_TEXT] = "", run_id[TURING_TEXT] = "", commit[48] = "", bin[72] = "", cc[TURING_TEXT] = "";
510 char thermal[TURING_TEXT] = "not recorded";
511 int dirty = -1;
512 uint64_t mismatches = UINT64_MAX;
513 for (char *line = text; line && *line;) {
514 char *nl = strchr(line, '\n');
515 if (nl) *nl = 0;
516 if (section == 0) {
517 (void)jstr(line, "schema", schema, sizeof schema);
518 (void)jstr(line, "run_id", run_id, sizeof run_id);
519 (void)jstr(line, "run_commit", commit, sizeof commit);
520 (void)jstr(line, "bench_binary_sha256", bin, sizeof bin);
521 (void)jstr(line, "compiler", cc, sizeof cc);
522 (void)jbool(line, "tree_dirty", &dirty);
523 uint64_t t0, t1;
524 if (strstr(line, "\"host\": ") && ju64(line, "thermal_c_start", &t0) == 0 &&
525 ju64(line, "thermal_c_end", &t1) == 0)
526 snprintf(thermal, sizeof thermal, "SoC %" PRIu64 " C at start, %" PRIu64 " C at end", t0, t1);
527 if (strncmp(line, " \"realizations\": [", 19) == 0) section = 1;
528 } else if (section == 1) {
529 char id[32], fam[32];
530 uint64_t ex, wb, mx;
531 if (jstr(line, "id", id, sizeof id) == 0) {
532 if (jstr(line, "family", fam, sizeof fam) != 0 || ju64(line, "exact", &ex) != 0 ||
533 ju64(line, "weak_baseline", &wb) != 0 || ju64(line, "max_n", &mx) != 0)
534 goto out;
535 turing_rz_spec s;
536 if (turing_spec_from_fields(&st->contract_digest, id, fam, (int)ex, (int)wb, mx, &s) != 0 ||
537 store_add_spec(st, &s) < 0)
538 goto out; /* registry and receipt disagree on the spec: refuse */
539 } else if (strncmp(line, " ],", 4) == 0) {
540 section = 2;
541 }
542 } else if (section == 2) {
543 if (strncmp(line, " \"correctness\"", 15) == 0) (void)ju64(line, "mismatches", &mismatches);
544 if (strncmp(line, " \"energy\"", 10) == 0) {
545 section = 3;
546 } else if (jfind(line, "rz") && jfind(line, "median_ps")) {
547 char rz[32];
548 double sp, noise, pnoise;
549 turing_evidence e;
550 memset(&e, 0, sizeof e);
551 uint64_t forced, retries, samples;
552 if (jstr(line, "rz", rz, sizeof rz) != 0 || ju64(line, "n", &e.n) != 0 ||
553 ju64(line, "m", &e.m) != 0 || jnum(line, "sparsity", &sp) != 0 ||
554 jbool(line, "verified", &e.verified) != 0 || ju64(line, "forced", &forced) != 0 ||
555 ju64(line, "retries", &retries) != 0 || ju64(line, "samples", &samples) != 0 ||
556 ju64(line, "median_ps", &e.median_ps) != 0 || ju64(line, "q25_ps", &e.q25_ps) != 0 ||
557 ju64(line, "q75_ps", &e.q75_ps) != 0 || ju64(line, "min_ps", &e.min_ps) != 0 ||
558 ju64(line, "pack_ps", &e.pack_ps) != 0 || jnum(line, "noise_rel", &noise) != 0 ||
559 jnum(line, "pack_noise_rel", &pnoise) != 0 ||
560 ju64(line, "weight_bytes", &e.weight_bytes) != 0 ||
561 ju64(line, "working_set_bytes", &e.working_set_bytes) != 0)
562 goto out;
563 int k = turing_find_spec(st, rz);
564 if (k < 0 || st->nev >= TURING_MAX_EVIDENCE) goto out;
565 e.spec_id = st->spec_id[k];
566 e.sparsity_milli = (uint32_t)llround(sp * 1000.0);
567 e.forced = (uint32_t)forced;
568 e.retries = (uint32_t)retries;
569 e.samples = (uint32_t)samples;
570 e.noise_ppm = (uint32_t)llround(noise * 1e6);
571 e.pack_noise_ppm = (uint32_t)llround(pnoise * 1e6);
572 e.receipt_digest = rd;
573 snprintf(e.receipt_path, sizeof e.receipt_path, "%s", path);
574 snprintf(e.run_id, sizeof e.run_id, "%s", run_id);
575 /* E2 = differential vs the oracle with declared coverage
576 * (spec/mixed-algebra-ma2.md); E0 when this row or the run
577 * reports a mismatch. Set after the correctness line below. */
578 snprintf(e.tier_source, sizeof e.tier_source, "%s", TURING_TIER_SOURCE);
579 snprintf(e.build_digest, sizeof e.build_digest, "%s", bin);
580 snprintf(e.git_commit, sizeof e.git_commit, "%s", commit);
581 e.tree_dirty = dirty;
582 snprintf(e.toolchain, sizeof e.toolchain, "%s", cc);
583 snprintf(e.cpu_freq_state, sizeof e.cpu_freq_state,
584 "not recorded by this receipt (MA-3 bench has no cpufreq capture)");
585 snprintf(e.quiet_flag, sizeof e.quiet_flag, "not recorded by this receipt");
586 snprintf(e.thermal, sizeof e.thermal, "%s", thermal);
587 st->ev[st->nev++] = e;
588 ++added;
589 }
590 }
591 line = nl ? nl + 1 : NULL;
592 }
593 if (strncmp(schema, "OMEGA_MIXED_ALGEBRA_MA", 22) != 0 || !strstr(schema, "_BENCH_V1") || !run_id[0] ||
594 section != 3 || added == 0 || dirty < 0)
595 goto out;
596 /* Tier and digests once the whole receipt is read. */
597 for (size_t i = st->nev - (size_t)added; i < st->nev; ++i) {
598 turing_evidence *e = &st->ev[i];
599 snprintf(e->tier, sizeof e->tier, "%s", (mismatches == 0 && e->verified) ? "E2" : "E0");
600 if (turing_evidence_digest(e, &st->ev_id[i]) != 0) goto out;
601 }
602 if (st->nreceipt < TURING_MAX_RECEIPTS) {
603 turing_receipt_ref *r = &st->receipt[st->nreceipt++];
604 r->digest = rd;
605 snprintf(r->path, sizeof r->path, "%s", path);
606 snprintf(r->run_id, sizeof r->run_id, "%s", run_id);
607 }
608 rc = added;
609out:
610 if (rc < 0) st->nev -= (size_t)added; /* drop a half-read receipt */
611 free(text);
612 return rc;
613}
614
615/* ----------------------------------------------------------------- verify */
616
617int turing_decision_verify(const turing_store *st, const turing_decision *d, char *why, size_t wl) {
618 if (!st || !d) return -1;
619 if (!turing_digest_eq(&d->contract_digest, &st->contract_digest)) {
620 snprintf(why, wl, "decision names another contract");
621 return -2;
622 }
623 if (d->ncite == 0) {
624 snprintf(why, wl, "decision cites no evidence");
625 return -3;
626 }
627 const turing_evidence *checked[TURING_MAX_RECEIPTS];
628 size_t nchecked = 0;
629 for (size_t i = 0; i < d->ncite; ++i) {
630 char h[13];
631 turing_hex_short(&d->cite[i], h);
632 int k = turing_find_evidence(st, &d->cite[i]);
633 if (k < 0) {
634 snprintf(why, wl, "cited evidence %s not in the store", h);
635 return -4;
636 }
637 turing_digest re;
638 if (turing_evidence_digest(&st->ev[k], &re) != 0 || !turing_digest_eq(&re, &d->cite[i])) {
639 snprintf(why, wl, "evidence %s no longer matches its digest", h);
640 return -5;
641 }
642 int in_cand = 0;
643 for (size_t c = 0; c < d->ncand; ++c) in_cand |= turing_digest_eq(&d->cand[c], &st->ev[k].spec_id);
644 if (!in_cand) {
645 snprintf(why, wl, "evidence %s is for a spec that is not a candidate", h);
646 return -6;
647 }
648 int cached = 0;
649 for (size_t j = 0; j < nchecked; ++j)
650 cached |= !strcmp(checked[j]->receipt_path, st->ev[k].receipt_path) &&
651 turing_digest_eq(&checked[j]->receipt_digest, &st->ev[k].receipt_digest);
652 if (cached) continue; /* same receipt file already re-hashed in this call */
653 turing_digest fd;
654 if (turing_file_digest(st->ev[k].receipt_path, &fd) != 0) {
655 snprintf(why, wl, "receipt %s unreadable", st->ev[k].receipt_path);
656 return -7;
657 }
658 if (!turing_digest_eq(&fd, &st->ev[k].receipt_digest)) {
659 snprintf(why, wl, "receipt %s bytes do not match the cited digest", st->ev[k].receipt_path);
660 return -8;
661 }
662 if (nchecked < TURING_MAX_RECEIPTS) checked[nchecked++] = &st->ev[k];
663 }
664 if (why && wl) snprintf(why, wl, "ok");
665 return 0;
666}