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 / evaluator / src / at0e.h

258 lines · 11765 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/* AT-0 Agent 4 independent evaluator: shared declarations.
18 *
19 * Contracts: AT0_CASE_V1 and AT0_RESULT_V1, frozen at aien-architecture
20 * 044c9d11256d8642f80eedd42cbae8763faf63f5 (docs/plans/atemporal/).
21 * This code shares nothing with src/at0/ (engine, oracle, codec) and never will;
22 * the only omega source it links is src/sha256.c (charter section 3).
23 *
24 * Every decision is made in exact integer arithmetic. binary64 values read from a
25 * result are converted to exact dyadic rationals; scaled decimals are exact
26 * decimal rationals; both are brought over a common denominator 2^1100 * 10^80
27 * and compared as fixed-width big integers (at0e_big). No float is ever compared.
28 */
29#ifndef AT0E_H
30#define AT0E_H
31
32#include <stddef.h>
33#include <stdint.h>
34
35#define AT0E_CONTRACT_COMMIT "044c9d11256d8642f80eedd42cbae8763faf63f5"
36#define AT0E_CASE_DOMAIN "omega.at0.case.v1"
37#define AT0E_ACC_DOMAIN "omega.at0.acceptance.v1"
38#define AT0E_VERDICT_DOMAIN_V1 "omega.at0.verdict.v1"
39#define AT0E_EVIDENCE_DOMAIN_V1 "omega.at0.evidence.v1"
40#define AT0E_VERDICT_DOMAIN_V2 "omega.at0.verdict.v2"
41#define AT0E_EVIDENCE_DOMAIN_V2 "omega.at0.evidence.v2"
42/* Result contract version in force: V2 (Agent 0 ruling, omega#358 comment 6090404007). V1 results are still parsed and judged under their own tags. */
43#define AT0E_RESULT_VERSION_DEFAULT 2
44
45#define AT0E_MAX_N 64
46#define AT0E_MAX_M 256
47#define AT0E_MAX_LINE 4096
48#define AT0E_RAT_LIMIT 1048576
49
50__extension__ typedef __int128 i128;
51__extension__ typedef unsigned __int128 u128;
52
53/* ---- exact rationals (int128, always reduced, d >= 1) ---- */
54typedef struct { i128 n, d; } rat;
55rat rat_make(i128 n, i128 d); /* reduces; aborts on d == 0 */
56rat rat_add(rat a, rat b);
57rat rat_sub(rat a, rat b);
58rat rat_mul(rat a, rat b);
59rat rat_neg(rat a);
60int rat_cmp(rat a, rat b);
61int rat_is_zero(rat a);
62int rat_is_int(rat a);
63rat rat_mod1(rat a); /* a - floor(a), in [0,1) */
64int rat_sqrt(rat a, rat *out); /* 1 if a is the square of a rational (a >= 0) */
65int rat_parse(const char *tok, rat *out, int *canonical); /* 1 on parse ok */
66void rat_format(rat a, char *buf, size_t cap);
67
68typedef struct { rat re, im; } crat;
69int crat_parse(const char *tok, crat *out, int *canonical);
70int crat_is_zero(crat z);
71
72/* ---- scaled decimal N@k ---- */
73typedef struct { u128 n; int k; } scaled; /* n < 10^38 fits u128 ; k 0..40 */
74int scaled_parse(const char *tok, scaled *out, int *canonical);
75
76/* ---- fixed-width big integers (4096 bits) ---- */
77#define BIG_LIMBS 128
78typedef struct { uint32_t l[BIG_LIMBS]; } big;
79typedef struct { int neg; big m; } sbig; /* sign-magnitude; zero has neg = 0 */
80void big_zero(big *a);
81void big_from_u128(big *a, u128 v);
82int big_is_zero(const big *a);
83int big_cmp(const big *a, const big *b);
84void big_add(big *r, const big *a, const big *b); /* aborts on overflow */
85void big_sub(big *r, const big *a, const big *b); /* requires a >= b */
86void big_shl(big *a, unsigned bits); /* aborts on overflow */
87void big_mul_u32(big *a, uint32_t m); /* aborts on overflow */
88void big_mul_pow10(big *a, unsigned k);
89void sbig_zero(sbig *a);
90void sbig_add(sbig *r, const sbig *a, const sbig *b);
91void sbig_neg(sbig *a);
92void sbig_abs(sbig *a);
93int sbig_cmp(const sbig *a, const sbig *b);
94/* Common-denominator representation Q = value * 2^1100 * 10^80. */
95#define AT0E_DEN_P2 1100u
96#define AT0E_DEN_P10 80u
97void sbig_from_f64_bits(sbig *q, uint64_t bits); /* finite only */
98void sbig_from_scaled(sbig *q, scaled s);
99void sbig_from_int(sbig *q, long v);
100
101/* ---- tri-state comparison of a quantity against a tolerance ---- */
102typedef enum { CMP_PASS = 0, CMP_FAIL = 1, CMP_INDET = 2 } tri;
103/* absolute form: PASS if |v|+b <= tol, FAIL if |v|-b > tol, else INDET */
104tri tri_absolute(const sbig *v, const sbig *b, const sbig *tol);
105/* signed form: PASS if v+b <= tol, FAIL if v-b > tol, else INDET */
106tri tri_signed(const sbig *v, const sbig *b, const sbig *tol);
107
108/* ---- text / files ---- */
109typedef struct {
110 char **lines; size_t n; /* each line without its LF */
111 uint8_t *bytes; size_t len; /* whole file */
112} textfile;
113/* Reads a file and enforces AT0_CASE_V1 section 1 byte rules. Returns 0 ok,
114 * else a nonzero reason code (see at0e_text.c) and *why names the rule. */
115int textfile_read(const char *path, textfile *tf, const char **why);
116int textfile_from_bytes(const uint8_t *b, size_t len, textfile *tf, const char **why);
117void textfile_free(textfile *tf);
118int split_tokens(const char *line, char **tok, int max); /* single-space split; -1 if rule broken */
119int is_label(const char *s);
120int is_name(const char *s);
121int is_digest(const char *s);
122int is_sha1hex(const char *s);
123int is_text200(const char *s);
124int parse_int_canonical(const char *s, long *out); /* canonical integer text */
125void sha256_hex(const uint8_t *data, size_t len, char out[65]);
126void sha256_tagged_hex(const char *domain, const uint8_t *data, size_t len, char out[65]);
127int hex_to_u64(const char *s16, uint64_t *out);
128
129/* ---- parsed case ---- */
130enum { BK_NONE = 0, BK_ESTIMATED = 1, BK_RIGOROUS = 2 };
131typedef struct {
132 char case_name[80];
133 int N;
134 rat E[AT0E_MAX_N];
135 rat h0, hx, hy, hz;
136 char ref_label[40];
137 crat psi0[2];
138 rat tau, w;
139 int M;
140 char label[AT0E_MAX_M][40];
141 int control_positive; /* 1 POSITIVE, 0 NEGATIVE */
142 int expected_pass; /* 1 PASS, 0 FAIL */
143 char expected_codes[16][48]; int n_expected_codes;
144 int min_bound_kind;
145 scaled tol_constraint, tol_povm, tol_prob, tol_zero, tol_schro;
146 char case_id[65], acceptance_id[65];
147 /* raw blocks, LF-terminated, for identity recomputation */
148 char *sem_block; size_t sem_len;
149 char *acc_block; size_t acc_len;
150 char case_file_sha256[65];
151 /* derived (exact) */
152 rat radius; /* |h|, rational */
153 int ref_index; /* k of reference label */
154} at0_case;
155
156/* Refusal codes (AT0_RESULT_V1 section 5), returned by case_parse_validate. */
157typedef enum {
158 REFUSE_NONE = 0,
159 REFUSE_PARSE_ERROR, REFUSE_NONCANONICAL, REFUSE_UNSUPPORTED_VERSION,
160 REFUSE_INVALID_PARAMETER, REFUSE_IRRATIONAL_SPECTRUM, REFUSE_ID_MISMATCH
161} refusal;
162const char *refusal_name(refusal r);
163/* Parses the case lines (already byte-checked). On refusal, *detail names the
164 * offending line. The identities are recomputed and compared (rule 5). */
165refusal case_parse_validate(const textfile *tf, at0_case *c, const char **detail);
166/* Same, on a sequence of lines starting at case_name (used inside results). */
167refusal case_parse_blocks(char **lines, size_t n, size_t *consumed, at0_case *c, const char **detail, int in_result);
168void case_free(at0_case *c);
169/* Writes the canonical case text for a filled-in semantic/acceptance struct,
170 * computing both identities. Returns bytes written or -1. */
171long case_emit(const at0_case *c, char *buf, size_t cap);
172int failure_code_valid(const char *code);
173
174/* ---- shadow oracle (independent physics, long double) ---- */
175typedef struct {
176 int kernel_dim; /* exact */
177 int matched[2]; /* e_+ (index 0) / e_- (index 1) matched by a clock energy */
178 int psi_nonzero; /* exact: Psi != 0 */
179 int povm_exact_identity; /* exact: sum F_k == I */
180 long double povm_residual; /* numeric Frobenius norm */
181 long double clock_p[AT0E_MAX_M];
182 int clock_p_exact_zero[AT0E_MAX_M];/* exact: phi_k == 0 */
183 long double pauli[AT0E_MAX_M][3][2]; /* [k][axis][sign] from the relational model */
184 long double reference[AT0E_MAX_M][3][2];/* Schrodinger reference */
185 long double bound; /* claimed absolute bound on every numeric above */
186} shadow;
187int shadow_compute(const at0_case *c, shadow *s, const char **why);
188
189/* ---- parsed result ---- */
190typedef struct { int is_undefined, is_nonfinite; uint64_t bits; scaled bound; } rval;
191enum { LS_DEFINED = 0, LS_UNDEFINED = 1, LS_INDETERMINATE = 2 };
192enum { CK_PASS = 0, CK_FAIL = 1, CK_INDET = 2, CK_NOTEVAL = 3 };
193enum { OUT_PASS = 0, OUT_FAIL = 1, OUT_ERROR = 2, OUT_NOT_RUN = 3 };
194#define AT0E_NCHECKS 10
195extern const char *const at0e_contract_commits[];
196extern const char *const at0e_check_names[AT0E_NCHECKS];
197extern const char *const at0e_check_codes[AT0E_NCHECKS];
198typedef struct {
199 at0_case c;
200 int version; /* 1 or 2, from the header */
201 char case_file_sha256[65];
202 int arithmetic; /* 0 BINARY64 1 INTERVAL 2 COMPENSATED 3 NONE */
203 int bound_kind;
204 long kernel_dim;
205 rval constraint_residual, povm_residual;
206 int label_status[AT0E_MAX_M];
207 rval clock_p[AT0E_MAX_M];
208 rval pauli[AT0E_MAX_M][3][2];
209 rval reference[AT0E_MAX_M][3][2];
210 int check[AT0E_NCHECKS];
211 int outcome;
212 char failure_codes[16][48]; int n_failure_codes;
213 char error_code[32];
214 int expectation_met; /* 0 YES 1 NO 2 NOT_APPLICABLE */
215 char verdict_id[65];
216 char source_repo[80], source_commit[48], contract_commit[48], engine_sha256[65];
217 char oracle_repo[80], oracle_commit[48], oracle_sha256[65];
218 char build_cc[208], build_flags[208], host[208], run_started[32], run_finished[32];
219 int source_tree_clean; /* 1 YES 0 NO */
220 int n_artifacts;
221 char evidence_digest[65];
222 /* raw */
223 char *ver_block; size_t ver_len;
224 size_t evidence_line_index; /* index of evidence_digest line */
225} at0_result;
226
227/* Structural parse of a result (shape, encodings, embedded case). Returns 0 ok,
228 * else sets *finding to an evaluator finding code and *detail. */
229int result_parse(const textfile *tf, at0_result *r, const char **finding, const char **detail);
230void result_free(at0_result *r);
231
232/* Independent re-derivation of the ten checks, outcome, codes, expectation. */
233typedef struct {
234 int check[AT0E_NCHECKS];
235 int outcome;
236 char failure_codes[16][48]; int n_failure_codes;
237 int expectation_met;
238 int any_label_indeterminate;
239} rederived;
240void result_rederive(const at0_result *r, rederived *d);
241
242/* Evaluator findings: appended to a list and printed as JSON. */
243typedef struct { char code[48]; char detail[320]; } finding;
244typedef struct { finding f[256]; int n; } findings;
245void findings_add(findings *fs, const char *code, const char *fmt, ...);
246
247/* Full verification of one result file against an optional case file and the
248 * shadow oracle. Returns the evaluator status string: PASS, FAIL, INCONCLUSIVE. */
249const char *result_verify(const textfile *tf, const at0_result *r, const textfile *case_tf,
250 int use_shadow, findings *fs);
251
252/* Synthetic result writer (self-test fixtures only; never evidence). mutant may
253 * be NULL or one of the names listed by synth_mutant_names(). */
254long result_synth(const at0_case *c, const uint8_t *case_bytes, size_t case_len,
255 const char *mutant, char *buf, size_t cap, const char **why);
256const char *const *synth_mutant_names(int *count);
257
258#endif /* AT0E_H */