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 / src / turing / ty_record.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 | /* Turing Yield companion records. See ty_record.h. */ |
| 18 | #include "turing/ty_record.h" |
| 19 | |
| 20 | #include "omega_canonical.h" |
| 21 | #include "sha256.h" |
| 22 | |
| 23 | #include <inttypes.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #define WHY(...) \ |
| 29 | do { \ |
| 30 | if (why && whylen) snprintf(why, whylen, __VA_ARGS__); \ |
| 31 | } while (0) |
| 32 | |
| 33 | #define CANON_CAP 40000 |
| 34 | |
| 35 | /* ---------------------------------------------------------------- digests */ |
| 36 | |
| 37 | void ty_hex(const ty_digest *d, char out[65]) { |
| 38 | static const char hx[] = "0123456789abcdef"; |
| 39 | for (int i = 0; i < 32; ++i) { |
| 40 | out[2 * i] = hx[d->b[i] >> 4]; |
| 41 | out[2 * i + 1] = hx[d->b[i] & 15]; |
| 42 | } |
| 43 | out[64] = 0; |
| 44 | } |
| 45 | |
| 46 | int ty_parse_hex(const char *hex, ty_digest *out) { |
| 47 | if (!hex || strlen(hex) < 64) return TY_E_FORMAT; |
| 48 | for (int i = 0; i < 32; ++i) { |
| 49 | int v = 0; |
| 50 | for (int j = 0; j < 2; ++j) { |
| 51 | char c = hex[2 * i + j]; |
| 52 | int n = (c >= '0' && c <= '9') ? c - '0' : (c >= 'a' && c <= 'f') ? c - 'a' + 10 : -1; |
| 53 | if (n < 0) return TY_E_FORMAT; |
| 54 | v = v * 16 + n; |
| 55 | } |
| 56 | out->b[i] = (uint8_t)v; |
| 57 | } |
| 58 | return TY_OK; |
| 59 | } |
| 60 | |
| 61 | int ty_digest_eq(const ty_digest *a, const ty_digest *b) { return memcmp(a->b, b->b, 32) == 0; } |
| 62 | |
| 63 | int ty_file_sha256(const char *path, ty_digest *out) { |
| 64 | FILE *f = fopen(path, "rb"); |
| 65 | if (!f) return TY_E_IO; |
| 66 | sha256_ctx c; |
| 67 | sha256_init(&c); |
| 68 | uint8_t *buf = malloc(1 << 20); |
| 69 | if (!buf) { |
| 70 | fclose(f); |
| 71 | return TY_E_IO; |
| 72 | } |
| 73 | size_t k; |
| 74 | while ((k = fread(buf, 1, 1 << 20, f)) > 0) sha256_update(&c, buf, k); |
| 75 | int err = ferror(f); |
| 76 | fclose(f); |
| 77 | free(buf); |
| 78 | if (err) return TY_E_IO; |
| 79 | sha256_final(&c, out->b); |
| 80 | return TY_OK; |
| 81 | } |
| 82 | |
| 83 | /* OMG0 object builder (same layout rules as src/turing/field.c). */ |
| 84 | typedef struct { |
| 85 | OmegaObject *o; |
| 86 | int err; |
| 87 | } obj_b; |
| 88 | |
| 89 | static void ob_text(obj_b *b, const char *key, const char *val) { |
| 90 | if (b->err) return; |
| 91 | size_t kl = strlen(key), vl = strlen(val); |
| 92 | if (b->o->attr_count >= OMEGA_MAX_ATTRIBUTES || kl >= OMEGA_MAX_KEY_LEN || vl > OMEGA_MAX_VAL_LEN) { |
| 93 | b->err = 1; |
| 94 | return; |
| 95 | } |
| 96 | OmegaAttribute *a = &b->o->attributes[b->o->attr_count++]; |
| 97 | memcpy(a->key, key, kl + 1); |
| 98 | memcpy(a->value, val, vl); |
| 99 | a->val_len = (uint16_t)vl; |
| 100 | } |
| 101 | |
| 102 | static void ob_u64(obj_b *b, const char *key, uint64_t v) { |
| 103 | char t[24]; |
| 104 | snprintf(t, sizeof t, "%" PRIu64, v); |
| 105 | ob_text(b, key, t); |
| 106 | } |
| 107 | |
| 108 | static void ob_i64(obj_b *b, const char *key, int64_t v) { |
| 109 | char t[24]; |
| 110 | snprintf(t, sizeof t, "%" PRId64, v); |
| 111 | ob_text(b, key, t); |
| 112 | } |
| 113 | |
| 114 | static void ob_digest(obj_b *b, const char *key, const ty_digest *d) { |
| 115 | char h[65]; |
| 116 | ty_hex(d, h); |
| 117 | ob_text(b, key, h); |
| 118 | } |
| 119 | |
| 120 | static int ob_new(obj_b *b, SemanticKind kind, const char *record) { |
| 121 | b->err = 0; |
| 122 | b->o = calloc(1, sizeof(OmegaObject)); |
| 123 | if (!b->o) return -1; |
| 124 | b->o->kind = kind; |
| 125 | ob_text(b, "record", record); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int ob_finish(obj_b *b, const char *domain, ty_digest *out) { |
| 130 | int rc = TY_E_FORMAT; |
| 131 | uint8_t *buf = NULL; |
| 132 | size_t n = 0; |
| 133 | if (!b->o || b->err) goto done; |
| 134 | buf = malloc(CANON_CAP); |
| 135 | if (!buf) goto done; |
| 136 | if (omega_canonical_encode(b->o, buf, CANON_CAP, &n) != 0) goto done; |
| 137 | sha256_ctx c; |
| 138 | sha256_init(&c); |
| 139 | sha256_update(&c, (const uint8_t *)domain, strlen(domain) + 1); /* includes the 0x00 separator */ |
| 140 | sha256_update(&c, buf, n); |
| 141 | sha256_final(&c, out->b); |
| 142 | rc = TY_OK; |
| 143 | done: |
| 144 | free(buf); |
| 145 | free(b->o); |
| 146 | b->o = NULL; |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | int ty_profile_digest(const ty_yprofile *p, ty_digest *out) { |
| 151 | obj_b b; |
| 152 | if (!p || !out || ob_new(&b, KIND_CONSTRAINT, "turing.yprofile") != 0) return TY_E_ARG; |
| 153 | ob_text(&b, "name", p->name); |
| 154 | ob_text(&b, "dataset", p->dataset); |
| 155 | ob_digest(&b, "manifest", &p->manifest); |
| 156 | ob_text(&b, "x_t", p->x_t); |
| 157 | ob_text(&b, "side_info", p->side_info); |
| 158 | ob_text(&b, "context_reset", p->context_reset); |
| 159 | ob_u64(&b, "alphabet_k", p->K); |
| 160 | ob_u64(&b, "qbits", p->qbits); |
| 161 | ob_u64(&b, "floor_q", p->floor_q); |
| 162 | ob_text(&b, "estimator", p->estimator); |
| 163 | ob_text(&b, "lm_code", p->lm_code); |
| 164 | ob_u64(&b, "fit_rule", p->fit_rule); |
| 165 | ob_text(&b, "baseline_rule", p->baseline_rule); |
| 166 | ob_u64(&b, "baseline_mask", p->baseline_mask); |
| 167 | ob_text(&b, "candidate_rule", p->candidate_rule); |
| 168 | ob_u64(&b, "candidate_mask", p->candidate_mask); |
| 169 | ob_text(&b, "pass_rule", p->pass_rule); |
| 170 | ob_i64(&b, "margin_ub", p->margin_ub); |
| 171 | ob_u64(&b, "qerr_milli_ub_per_symbol", p->qerr_milli_ub); |
| 172 | ob_text(&b, "energy_denominator", p->energy_denominator); |
| 173 | ob_text(&b, "unit", "micro-bits (1 T = 1 bit = 1000000 ub); model lengths in whole bits"); |
| 174 | return ob_finish(&b, TY_DOMAIN_PROFILE, out); |
| 175 | } |
| 176 | |
| 177 | static void file_attr(obj_b *b, const char *pre, size_t i, const char *label, const ty_digest *d) { |
| 178 | char k[32], v[160], h[65]; |
| 179 | snprintf(k, sizeof k, "%s.%02zu", pre, i); |
| 180 | ty_hex(d, h); |
| 181 | snprintf(v, sizeof v, "%s %s", h, label ? label : ""); |
| 182 | ob_text(b, k, v); |
| 183 | } |
| 184 | |
| 185 | int ty_split_digest(const ty_split *s, ty_digest *out) { |
| 186 | obj_b b; |
| 187 | if (!s || !out || s->nfit > TY_MAX_FILES || s->nheld > TY_MAX_FILES) return TY_E_ARG; |
| 188 | if (ob_new(&b, KIND_EVIDENCE, "turing.ysplit") != 0) return TY_E_ARG; |
| 189 | ob_u64(&b, "nfit", s->nfit); |
| 190 | ob_u64(&b, "nheld", s->nheld); |
| 191 | for (size_t i = 0; i < s->nfit; ++i) file_attr(&b, "fit", i, s->fit_label[i], &s->fit[i]); |
| 192 | for (size_t i = 0; i < s->nheld; ++i) file_attr(&b, "held", i, s->held_label[i], &s->held[i]); |
| 193 | return ob_finish(&b, TY_DOMAIN_SPLIT, out); |
| 194 | } |
| 195 | |
| 196 | int ty_fit_digest(const ty_fit_rec *f, ty_digest *out) { |
| 197 | obj_b b; |
| 198 | if (!f || !out || f->nfit > TY_MAX_FILES) return TY_E_ARG; |
| 199 | if (ob_new(&b, KIND_EVIDENCE, "turing.yfit") != 0) return TY_E_ARG; |
| 200 | ob_digest(&b, "profile", &f->profile); |
| 201 | ob_digest(&b, "split", &f->split); |
| 202 | ob_text(&b, "role", f->role == TY_ROLE_BASELINE ? "baseline" : "candidate"); |
| 203 | ob_u64(&b, "mask", f->mask); |
| 204 | ob_u64(&b, "rule", f->rule); |
| 205 | ob_u64(&b, "nfit", f->nfit); |
| 206 | for (size_t i = 0; i < f->nfit; ++i) file_attr(&b, "fit", i, NULL, &f->fit[i]); |
| 207 | ob_digest(&b, "model", &f->model); |
| 208 | ob_u64(&b, "model_bits", f->model_bits); |
| 209 | return ob_finish(&b, TY_DOMAIN_FIT, out); |
| 210 | } |
| 211 | |
| 212 | int ty_gain_digest(const ty_gain_rec *g, ty_digest *out) { |
| 213 | obj_b b; |
| 214 | if (!g || !out || g->nheld > TY_MAX_FILES) return TY_E_ARG; |
| 215 | if (ob_new(&b, KIND_EVIDENCE, "turing.yield") != 0) return TY_E_ARG; |
| 216 | ob_digest(&b, "profile", &g->profile); |
| 217 | ob_digest(&b, "split", &g->split); |
| 218 | ob_digest(&b, "baseline_fit", &g->baseline_fit); |
| 219 | ob_digest(&b, "candidate_fit", &g->candidate_fit); |
| 220 | ob_digest(&b, "baseline_model", &g->baseline_model); |
| 221 | ob_digest(&b, "candidate_model", &g->candidate_model); |
| 222 | ob_u64(&b, "nsym", g->nsym); |
| 223 | ob_u64(&b, "lm_b_bits", g->lm_b_bits); |
| 224 | ob_u64(&b, "lm_c_bits", g->lm_c_bits); |
| 225 | ob_i64(&b, "ld_b_ub", g->ld_b_ub); |
| 226 | ob_i64(&b, "ld_c_ub", g->ld_c_ub); |
| 227 | ob_i64(&b, "dl_b_ub", g->dl_b_ub); |
| 228 | ob_i64(&b, "dl_c_ub", g->dl_c_ub); |
| 229 | ob_i64(&b, "t_microbits", g->t_ub); |
| 230 | ob_u64(&b, "nheld", g->nheld); |
| 231 | for (size_t i = 0; i < g->nheld; ++i) { |
| 232 | char k[32], v[64]; |
| 233 | snprintf(k, sizeof k, "file.%02zu", i); |
| 234 | snprintf(v, sizeof v, "%" PRIu64 " %" PRId64, g->file_nsym[i], g->file_t_ub[i]); |
| 235 | ob_text(&b, k, v); |
| 236 | } |
| 237 | ob_i64(&b, "qerr_bound_ub", g->qerr_bound_ub); |
| 238 | ob_i64(&b, "margin_ub", g->margin_ub); |
| 239 | ob_text(&b, "verdict", g->verdict); |
| 240 | return ob_finish(&b, TY_DOMAIN_GAIN, out); |
| 241 | } |
| 242 | |
| 243 | /* ------------------------------------------------------------- fit + gain */ |
| 244 | |
| 245 | static int in_set(const ty_digest *d, const ty_digest *set, size_t n) { |
| 246 | for (size_t i = 0; i < n; ++i) |
| 247 | if (ty_digest_eq(d, &set[i])) return 1; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int ty_fit_files(const ty_yprofile *p, const ty_split *sp, const char *const *paths, const ty_digest *digests, |
| 252 | size_t nfiles, unsigned role, unsigned mask, ty_fit_rec *rec, uint8_t **code, size_t *ncode, |
| 253 | char *why, size_t whylen) { |
| 254 | if (!p || !sp || !paths || !digests || !rec || !code || !ncode || nfiles == 0 || nfiles > TY_MAX_FILES) |
| 255 | return TY_E_ARG; |
| 256 | memset(rec, 0, sizeof *rec); |
| 257 | ty_stream *st = calloc(nfiles, sizeof *st); |
| 258 | const ty_stream **sv = calloc(nfiles, sizeof *sv); |
| 259 | int rc = (st && sv) ? TY_OK : TY_E_IO; |
| 260 | size_t loaded = 0; |
| 261 | for (size_t i = 0; rc == TY_OK && i < nfiles; ++i) { |
| 262 | ty_digest d; |
| 263 | if ((rc = ty_file_sha256(paths[i], &d)) != TY_OK) { |
| 264 | WHY("cannot hash %s", paths[i]); |
| 265 | break; |
| 266 | } |
| 267 | if (!ty_digest_eq(&d, &digests[i])) { |
| 268 | WHY("fit file %s does not match its manifest digest", paths[i]); |
| 269 | rc = TY_E_DIGEST; |
| 270 | break; |
| 271 | } |
| 272 | ty_stream_init(&st[i]); |
| 273 | loaded = i + 1; |
| 274 | int64_t n = ty_ctr1_read(paths[i], &st[i], why, whylen); |
| 275 | if (n < 0) { |
| 276 | rc = (int)n; |
| 277 | break; |
| 278 | } |
| 279 | sv[i] = &st[i]; |
| 280 | rec->fit[i] = d; |
| 281 | } |
| 282 | ty_model m; |
| 283 | memset(&m, 0, sizeof m); |
| 284 | if (rc == TY_OK) rc = ty_model_fit(&m, sv, nfiles, mask, p->K, (int)p->fit_rule); |
| 285 | for (size_t i = 0; i < loaded; ++i) ty_stream_free(&st[i]); |
| 286 | free(st); |
| 287 | free(sv); |
| 288 | if (rc != TY_OK) return rc; |
| 289 | rc = ty_model_encode(&m, code, ncode, &rec->model_bits); |
| 290 | ty_model_free(&m); |
| 291 | if (rc != TY_OK) return rc; |
| 292 | ty_model_digest(*code, *ncode, rec->model.b); |
| 293 | rec->nfit = nfiles; |
| 294 | rec->role = role; |
| 295 | rec->mask = mask; |
| 296 | rec->rule = p->fit_rule; |
| 297 | if ((rc = ty_profile_digest(p, &rec->profile)) != TY_OK) return rc; |
| 298 | return ty_split_digest(sp, &rec->split); |
| 299 | } |
| 300 | |
| 301 | static int check_rec(const ty_yprofile *p, const ty_split *sp, const ty_digest *pd, const ty_digest *sd, |
| 302 | const ty_fit_rec *r, const uint8_t *code, size_t n, ty_model *m, const char *who, char *why, |
| 303 | size_t whylen) { |
| 304 | if (!ty_digest_eq(&r->profile, pd)) { |
| 305 | WHY("%s was fit under another measurement profile", who); |
| 306 | return TY_E_PROFILE; |
| 307 | } |
| 308 | if (!ty_digest_eq(&r->split, sd)) { |
| 309 | WHY("%s names another split manifest", who); |
| 310 | return TY_E_PROVENANCE; |
| 311 | } |
| 312 | for (size_t i = 0; i < r->nfit; ++i) |
| 313 | if (in_set(&r->fit[i], sp->held, sp->nheld)) { |
| 314 | WHY("%s was fit on a held-out file (fit.%02zu)", who, i); |
| 315 | return TY_E_LEAK; |
| 316 | } |
| 317 | if (r->nfit != sp->nfit) { |
| 318 | WHY("%s fit on %zu files, split declares %zu", who, r->nfit, sp->nfit); |
| 319 | return TY_E_PROVENANCE; |
| 320 | } |
| 321 | for (size_t i = 0; i < r->nfit; ++i) |
| 322 | if (!ty_digest_eq(&r->fit[i], &sp->fit[i])) { |
| 323 | WHY("%s fit file %zu is not the split's fit file %zu", who, i, i); |
| 324 | return TY_E_PROVENANCE; |
| 325 | } |
| 326 | if (r->rule != p->fit_rule) { |
| 327 | WHY("%s fit rule %u, profile pins %u", who, r->rule, p->fit_rule); |
| 328 | return TY_E_PROFILE; |
| 329 | } |
| 330 | ty_digest md; |
| 331 | ty_model_digest(code, n, md.b); |
| 332 | if (!ty_digest_eq(&md, &r->model)) { |
| 333 | WHY("%s model code does not match its fit record", who); |
| 334 | return TY_E_DIGEST; |
| 335 | } |
| 336 | uint64_t bits; |
| 337 | char w[160]; |
| 338 | int rc = ty_model_decode(code, n, m, &bits, w, sizeof w); |
| 339 | if (rc != TY_OK) { |
| 340 | WHY("%s model refused: %s", who, w); |
| 341 | return rc; |
| 342 | } |
| 343 | if (m->K != p->K || bits != r->model_bits || m->mask != r->mask) { |
| 344 | WHY("%s model K/length/mask disagree with profile or fit record", who); |
| 345 | ty_model_free(m); |
| 346 | return TY_E_PROFILE; |
| 347 | } |
| 348 | return TY_OK; |
| 349 | } |
| 350 | |
| 351 | int ty_gain_compute(const ty_yprofile *p, const ty_split *sp, const ty_fit_rec *brec, const uint8_t *bcode, |
| 352 | size_t nb, const ty_fit_rec *crec, const uint8_t *ccode, size_t nc, |
| 353 | const char *const *held_paths, ty_gain_rec *out, char *why, size_t whylen) { |
| 354 | if (!p || !sp || !brec || !bcode || !crec || !ccode || !held_paths || !out || sp->nheld == 0) return TY_E_ARG; |
| 355 | memset(out, 0, sizeof *out); |
| 356 | ty_digest pd, sd; |
| 357 | int rc = ty_profile_digest(p, &pd); |
| 358 | if (rc == TY_OK) rc = ty_split_digest(sp, &sd); |
| 359 | if (rc != TY_OK) return rc; |
| 360 | if (p->qbits != TY_QBITS || p->floor_q != 1) { |
| 361 | WHY("profile quantization/floor not supported by this scorer"); |
| 362 | return TY_E_PROFILE; |
| 363 | } |
| 364 | if (brec->role != TY_ROLE_BASELINE || brec->mask != p->baseline_mask) { |
| 365 | WHY("baseline mask %u is not the profile's declared baseline (mask %u)", brec->mask, p->baseline_mask); |
| 366 | return TY_E_BASELINE; |
| 367 | } |
| 368 | if (crec->role != TY_ROLE_CANDIDATE) return TY_E_ARG; |
| 369 | ty_model mb, mc; |
| 370 | if ((rc = check_rec(p, sp, &pd, &sd, brec, bcode, nb, &mb, "baseline", why, whylen)) != TY_OK) return rc; |
| 371 | if ((rc = check_rec(p, sp, &pd, &sd, crec, ccode, nc, &mc, "candidate", why, whylen)) != TY_OK) { |
| 372 | ty_model_free(&mb); |
| 373 | return rc; |
| 374 | } |
| 375 | int64_t lb_ub, lc_ub; |
| 376 | rc = ty_bits_to_ub(brec->model_bits, &lb_ub); |
| 377 | if (rc == TY_OK) rc = ty_bits_to_ub(crec->model_bits, &lc_ub); |
| 378 | for (size_t i = 0; rc == TY_OK && i < sp->nheld; ++i) { |
| 379 | ty_digest d; |
| 380 | if ((rc = ty_file_sha256(held_paths[i], &d)) != TY_OK) { |
| 381 | WHY("cannot hash %s", held_paths[i]); |
| 382 | break; |
| 383 | } |
| 384 | if (!ty_digest_eq(&d, &sp->held[i])) { |
| 385 | WHY("held-out file %s does not match the split manifest", held_paths[i]); |
| 386 | rc = TY_E_DIGEST; |
| 387 | break; |
| 388 | } |
| 389 | ty_stream s; |
| 390 | ty_stream_init(&s); |
| 391 | int64_t n = ty_ctr1_read(held_paths[i], &s, why, whylen); |
| 392 | int64_t ub_b = 0, ub_c = 0; |
| 393 | uint64_t ns = 0; |
| 394 | if (n < 0) rc = (int)n; |
| 395 | if (rc == TY_OK) rc = ty_model_score(&mb, &s, &ub_b, &ns); |
| 396 | if (rc == TY_OK) rc = ty_model_score(&mc, &s, &ub_c, &ns); |
| 397 | ty_stream_free(&s); |
| 398 | if (rc != TY_OK) break; |
| 399 | int64_t fb, fc; |
| 400 | rc = ty_add(lb_ub, ub_b, &fb); |
| 401 | if (rc == TY_OK) rc = ty_add(lc_ub, ub_c, &fc); |
| 402 | if (rc == TY_OK) rc = ty_gain(fb, fc, &out->file_t_ub[i]); |
| 403 | if (rc == TY_OK) rc = ty_add(out->ld_b_ub, ub_b, &out->ld_b_ub); |
| 404 | if (rc == TY_OK) rc = ty_add(out->ld_c_ub, ub_c, &out->ld_c_ub); |
| 405 | out->file_nsym[i] = ns; |
| 406 | out->nsym += ns; |
| 407 | } |
| 408 | ty_model_free(&mb); |
| 409 | ty_model_free(&mc); |
| 410 | if (rc != TY_OK) return rc; |
| 411 | out->nheld = sp->nheld; |
| 412 | out->profile = pd; |
| 413 | out->split = sd; |
| 414 | out->baseline_model = brec->model; |
| 415 | out->candidate_model = crec->model; |
| 416 | out->lm_b_bits = brec->model_bits; |
| 417 | out->lm_c_bits = crec->model_bits; |
| 418 | if ((rc = ty_fit_digest(brec, &out->baseline_fit)) != TY_OK) return rc; |
| 419 | if ((rc = ty_fit_digest(crec, &out->candidate_fit)) != TY_OK) return rc; |
| 420 | if ((rc = ty_dl(out->lm_b_bits, out->ld_b_ub, &out->dl_b_ub)) != TY_OK) return rc; |
| 421 | if ((rc = ty_dl(out->lm_c_bits, out->ld_c_ub, &out->dl_c_ub)) != TY_OK) return rc; |
| 422 | if ((rc = ty_gain(out->dl_b_ub, out->dl_c_ub, &out->t_ub)) != TY_OK) return rc; |
| 423 | /* Both code lengths carry at most 0.501 ub per symbol of fixed-point error. */ |
| 424 | out->qerr_bound_ub = (int64_t)((2 * out->nsym * (uint64_t)p->qerr_milli_ub + 999) / 1000); |
| 425 | out->margin_ub = p->margin_ub; |
| 426 | int pass = out->t_ub - out->qerr_bound_ub > (p->margin_ub > 0 ? p->margin_ub : 0); |
| 427 | for (size_t i = 0; i < out->nheld; ++i) |
| 428 | if (out->file_t_ub[i] - out->qerr_bound_ub <= 0) pass = 0; |
| 429 | snprintf(out->verdict, sizeof out->verdict, "%s", pass ? "PASS" : "FAIL"); |
| 430 | return TY_OK; |
| 431 | } |
| 432 | |
| 433 | int ty_gain_verify(const ty_yprofile *p, const ty_split *sp, const char *const *fit_paths, |
| 434 | const char *const *held_paths, const ty_gain_rec *rec, const ty_digest *rec_digest, char *why, |
| 435 | size_t whylen) { |
| 436 | if (!p || !sp || !fit_paths || !held_paths || !rec || !rec_digest) return TY_E_ARG; |
| 437 | ty_digest d; |
| 438 | int rc = ty_gain_digest(rec, &d); |
| 439 | if (rc != TY_OK) return rc; |
| 440 | if (!ty_digest_eq(&d, rec_digest)) { |
| 441 | WHY("gain record does not hash to its recorded digest"); |
| 442 | return TY_E_DIGEST; |
| 443 | } |
| 444 | ty_fit_rec br, cr; |
| 445 | uint8_t *bc = NULL, *cc = NULL; |
| 446 | size_t nb = 0, nc = 0; |
| 447 | rc = ty_fit_files(p, sp, fit_paths, sp->fit, sp->nfit, TY_ROLE_BASELINE, p->baseline_mask, &br, &bc, &nb, why, |
| 448 | whylen); |
| 449 | if (rc == TY_OK) |
| 450 | rc = ty_fit_files(p, sp, fit_paths, sp->fit, sp->nfit, TY_ROLE_CANDIDATE, p->candidate_mask, &cr, &cc, &nc, |
| 451 | why, whylen); |
| 452 | ty_gain_rec again; |
| 453 | if (rc == TY_OK) rc = ty_gain_compute(p, sp, &br, bc, nb, &cr, cc, nc, held_paths, &again, why, whylen); |
| 454 | free(bc); |
| 455 | free(cc); |
| 456 | if (rc != TY_OK) return rc; |
| 457 | ty_digest d2; |
| 458 | if ((rc = ty_gain_digest(&again, &d2)) != TY_OK) return rc; |
| 459 | if (!ty_digest_eq(&d2, rec_digest)) { |
| 460 | WHY("re-derived gain record differs from the recorded one (T %lld vs %lld ub)", (long long)again.t_ub, |
| 461 | (long long)rec->t_ub); |
| 462 | return TY_E_DIGEST; |
| 463 | } |
| 464 | return TY_OK; |
| 465 | } |