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 / ty_record.h

137 lines · 5859 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 Yield companion records (TY-1). New digest domains only; no existing
18 * turing.* record, domain or golden is touched.
19 *
20 * turing.yprofile.v0 measurement profile (what is coded, how, what passes)
21 * turing.ysplit.v0 split manifest: SHA-256 of every fit file and every
22 * held-out file, in declared order
23 * turing.yfit.v0 fit provenance of one model: profile, split, role,
24 * feature mask, fit rule, fit file digests, model digest
25 * turing.yield.v0 one T measurement: baseline and candidate code lengths
26 * on the held-out files, DL, T in micro-bits, verdict
27 *
28 * Each digest = SHA-256(domain || 0x00 || OMG0 bytes) through the existing
29 * omega_canonical_encode + src/sha256.c path, as in src/turing/field.c. These
30 * digests are record digests, never Omega semantic ids (OSC-0B).
31 */
32#ifndef TURING_TY_RECORD_H
33#define TURING_TY_RECORD_H
34
35#include <stddef.h>
36#include <stdint.h>
37
38#include "turing/ty_model.h"
39
40#define TY_DOMAIN_PROFILE "turing.yprofile.v0"
41#define TY_DOMAIN_SPLIT "turing.ysplit.v0"
42#define TY_DOMAIN_FIT "turing.yfit.v0"
43#define TY_DOMAIN_GAIN "turing.yield.v0"
44
45#define TY_MAX_FILES 12
46#define TY_TEXT 256
47
48typedef struct {
49 uint8_t b[32];
50} ty_digest;
51
52void ty_hex(const ty_digest *d, char out[65]);
53int ty_parse_hex(const char *hex, ty_digest *out);
54int ty_digest_eq(const ty_digest *a, const ty_digest *b);
55int ty_file_sha256(const char *path, ty_digest *out);
56
57typedef struct {
58 char name[64];
59 char dataset[TY_TEXT];
60 ty_digest manifest; /* SHA-256 of the manifest file bytes */
61 char x_t[TY_TEXT];
62 char side_info[TY_TEXT];
63 char context_reset[128];
64 unsigned K;
65 unsigned qbits, floor_q; /* 16, 1 */
66 char estimator[TY_TEXT];
67 char lm_code[TY_TEXT];
68 unsigned fit_rule; /* TY_FIT_* used for every model under this profile */
69 char baseline_rule[TY_TEXT];
70 unsigned baseline_mask; /* the declared baseline's feature mask */
71 char candidate_rule[TY_TEXT];
72 unsigned candidate_mask; /* the tuned candidate's feature mask */
73 char pass_rule[TY_TEXT];
74 int64_t margin_ub; /* pooled T must exceed this */
75 unsigned qerr_milli_ub; /* per-symbol fixed-point error bound, 1/1000 ub */
76 char energy_denominator[TY_TEXT];
77} ty_yprofile;
78
79typedef struct {
80 size_t nfit, nheld;
81 ty_digest fit[TY_MAX_FILES], held[TY_MAX_FILES];
82 char fit_label[TY_MAX_FILES][64], held_label[TY_MAX_FILES][64];
83} ty_split;
84
85enum { TY_ROLE_BASELINE = 0, TY_ROLE_CANDIDATE = 1 };
86
87typedef struct {
88 ty_digest profile, split;
89 unsigned role, mask, rule;
90 size_t nfit;
91 ty_digest fit[TY_MAX_FILES];
92 ty_digest model; /* ty_model_digest of the model code */
93 uint64_t model_bits; /* L(M) */
94} ty_fit_rec;
95
96typedef struct {
97 ty_digest profile, split, baseline_fit, candidate_fit, baseline_model, candidate_model;
98 uint64_t nsym;
99 uint64_t lm_b_bits, lm_c_bits;
100 int64_t ld_b_ub, ld_c_ub, dl_b_ub, dl_c_ub, t_ub;
101 size_t nheld;
102 uint64_t file_nsym[TY_MAX_FILES];
103 int64_t file_t_ub[TY_MAX_FILES]; /* per held file, each model's full L(M) charged */
104 int64_t qerr_bound_ub; /* |t_ub - exact| <= this */
105 int64_t margin_ub;
106 char verdict[16]; /* PASS or FAIL */
107} ty_gain_rec;
108
109/* Measurement profile V0 (docs/turing/TURING_YIELD_PROFILE_V0.md), all fields except
110 * manifest, which the caller sets to the SHA-256 of the committed manifest file. */
111void ty_yprofile_v0(ty_yprofile *p);
112
113int ty_profile_digest(const ty_yprofile *p, ty_digest *out);
114int ty_split_digest(const ty_split *s, ty_digest *out);
115int ty_fit_digest(const ty_fit_rec *f, ty_digest *out);
116int ty_gain_digest(const ty_gain_rec *g, ty_digest *out);
117
118/* Read and fit: re-hash each fit file against the digest given, read it, fit
119 * the model, encode it, fill the provenance record. Caller frees *code. */
120int ty_fit_files(const ty_yprofile *p, const ty_split *sp, const char *const *paths, const ty_digest *digests,
121 size_t nfiles, unsigned role, unsigned mask, ty_fit_rec *rec, uint8_t **code, size_t *ncode,
122 char *why, size_t whylen);
123
124/* Compute T of candidate over baseline on the split's held-out files. Refuses
125 * (without scoring) on profile, baseline, provenance, leak or digest mismatch. */
126int ty_gain_compute(const ty_yprofile *p, const ty_split *sp, const ty_fit_rec *brec, const uint8_t *bcode,
127 size_t nb, const ty_fit_rec *crec, const uint8_t *ccode, size_t nc,
128 const char *const *held_paths, ty_gain_rec *out, char *why, size_t whylen);
129
130/* Independent re-derivation: refit baseline and candidate from the fit files
131 * (re-hashed), recompute the gain on the held-out files (re-hashed) and
132 * compare every field and the record digest against rec/rec_digest. */
133int ty_gain_verify(const ty_yprofile *p, const ty_split *sp, const char *const *fit_paths,
134 const char *const *held_paths, const ty_gain_rec *rec, const ty_digest *rec_digest, char *why,
135 size_t whylen);
136
137#endif /* TURING_TY_RECORD_H */