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_model.h
| 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-1 candidate engine (Agent 3, aien-spark/omega#371). Portable C11, libc + libm only. |
| 18 | * |
| 19 | * Contracts (frozen, aien-architecture cbe4c8e): AT1_CASE_V1 (sha256 e62018d8...), |
| 20 | * AT1_RESULT_V1 (sha256 3e6efd7e...), with AT0_RESULT_V2 sections 2, 4, 5, 7 normative by |
| 21 | * reference. Mathematics: AT1_SPEC.md at aien-architecture 81047f5. Contract readings (c), (d), |
| 22 | * (e) of AT1_CHARTER.md section 8 are applied. |
| 23 | * |
| 24 | * Layers, one file each: |
| 25 | * at1_bn.c arbitrary-precision integers, exact rationals, Gaussian rationals |
| 26 | * at1_case.c AT1_CASE_V1 parser and validator (rules 1 to 6 in contract order), identities |
| 27 | * at1_exact.c literal H_total as an exact Gaussian-rational matrix, exact nullspace by |
| 28 | * Gauss-Jordan elimination, exact orthogonal projector P_0, exact zero test |
| 29 | * at1_numeric.c binary64 physics: clock states, Psi = P_0 (|t_r> (x) psi_0) by linearity from the |
| 30 | * exact per-level vectors P_0 (|E_j> (x) psi_0), residuals,|t_r> (x) psi_0), residuals, |
| 31 | * phi_k, p(k), rho_k, Pauli outcomes, ESTIMATED bounds |
| 32 | * at1_result.c oracle reference splice, provenance, exact verdict (12 checks), AT1_RESULT_V1 |
| 33 | * writer, verdict_id and evidence_digest |
| 34 | * at1_io.c the only file-system access (read a whole file) |
| 35 | * at1_main.c command-line tool |
| 36 | * No global mutable state. No clock, timer, random source, thread or network call anywhere. */ |
| 37 | #ifndef AT1_MODEL_H |
| 38 | #define AT1_MODEL_H |
| 39 | |
| 40 | #include <stddef.h> |
| 41 | #include <stdint.h> |
| 42 | |
| 43 | /* ---- contract constants ---------------------------------------------------------------- */ |
| 44 | #define AT1_CLOCK_DIM_MIN 2 |
| 45 | #define AT1_CLOCK_DIM_MAX 64 |
| 46 | #define AT1_LABEL_MAX 256 |
| 47 | #define AT1_TOKEN_LIMIT 1048576 /* |n| <= 2^20, 1 <= d <= 2^20 */ |
| 48 | #define AT1_SCALED_K_MAX 40 |
| 49 | #define AT1_LABEL_LEN 32 |
| 50 | #define AT1_NAME_LEN 64 |
| 51 | #define AT1_FILE_MAX (1u << 20) /* engine limit: case and auxiliary files up to 1 MiB */ |
| 52 | #define AT1_TOKEN_DIGITS_MAX 4096 /* engine limit: numeric tokens up to 4096 digits */ |
| 53 | |
| 54 | typedef enum { |
| 55 | AT1_OK = 0, |
| 56 | /* refusal codes, AT1_RESULT_V1 section 5, in validation order */ |
| 57 | AT1_CASE_PARSE_ERROR, |
| 58 | AT1_CASE_NONCANONICAL, |
| 59 | AT1_CASE_UNSUPPORTED_VERSION, |
| 60 | AT1_CASE_INVALID_PARAMETER, |
| 61 | AT1_CASE_IRRATIONAL_SPECTRUM, |
| 62 | AT1_CASE_ID_MISMATCH, |
| 63 | /* engine errors (never a refusal) */ |
| 64 | AT1_ERR_RESOURCE, /* an engine limit (file size, token length) */ |
| 65 | AT1_ERR_INTERNAL, /* an internal consistency check failed */ |
| 66 | AT1_ERR_ARGUMENT /* bad tool input (oracle record, provenance) */ |
| 67 | } at1_status; |
| 68 | |
| 69 | const char *at1_status_name(at1_status s); |
| 70 | |
| 71 | /* ---- arbitrary-precision integers (at1_bn.c) --------------------------------------------- */ |
| 72 | /* Sign-magnitude, 32-bit limbs, little endian; zero has n = 0 and neg = 0. Every output may |
| 73 | * alias an input. Allocation failure prints AT1_ENGINE_ERROR RESOURCE_LIMIT and exits 1. */ |
| 74 | typedef struct { int neg; int n; int cap; uint32_t *d; } bn; |
| 75 | |
| 76 | /* Engine error exit: prints one line "AT1_ENGINE_ERROR <reason>" on stderr and |
| 77 | * exits 1. Defined in at1_io.c (the I/O object) so that no compute object |
| 78 | * references stdio (AT-1 D2, omega#371 comment 6093971637: gcc lowered the |
| 79 | * former fputs in at1_bn.c to fwrite, which the isolation gate bans). */ |
| 80 | void at1_fatal(const char *reason); |
| 81 | |
| 82 | void bn_init(bn *a); |
| 83 | void bn_free(bn *a); |
| 84 | void bn_copy(bn *r, const bn *a); |
| 85 | void bn_set_i64(bn *r, int64_t v); |
| 86 | void bn_set_u64(bn *r, uint64_t v); |
| 87 | int bn_is_zero(const bn *a); |
| 88 | int bn_sign(const bn *a); /* -1, 0, +1 */ |
| 89 | int bn_cmp(const bn *a, const bn *b); |
| 90 | int bn_cmp_abs(const bn *a, const bn *b); |
| 91 | void bn_add(bn *r, const bn *a, const bn *b); |
| 92 | void bn_sub(bn *r, const bn *a, const bn *b); |
| 93 | void bn_mul(bn *r, const bn *a, const bn *b); |
| 94 | void bn_neg(bn *r, const bn *a); |
| 95 | void bn_abs(bn *r, const bn *a); |
| 96 | void bn_divmod(bn *q, bn *rem, const bn *a, const bn *b); /* truncated; q or rem may be NULL */ |
| 97 | void bn_gcd(bn *r, const bn *a, const bn *b); /* r >= 0 */ |
| 98 | void bn_shl(bn *r, const bn *a, unsigned bits); |
| 99 | void bn_pow10(bn *r, unsigned k); |
| 100 | int bn_bitlen(const bn *a); |
| 101 | int bn_isqrt_exact(const bn *a, bn *root); /* 1 if a >= 0 is a perfect square */ |
| 102 | int bn_fits_i64(const bn *a, int64_t *out); |
| 103 | /* decimal digit string (no sign) into r; returns 0 if longer than AT1_TOKEN_DIGITS_MAX */ |
| 104 | int bn_from_digits(bn *r, const char *s, size_t len); |
| 105 | |
| 106 | /* ---- exact rationals (reduced, den > 0) --------------------------------------------------- */ |
| 107 | typedef struct { bn num, den; } bq; |
| 108 | |
| 109 | void bq_init(bq *a); |
| 110 | void bq_free(bq *a); |
| 111 | void bq_copy(bq *r, const bq *a); |
| 112 | void bq_set_i64(bq *r, int64_t n, int64_t d); /* d != 0 */ |
| 113 | void bq_set_bn(bq *r, const bn *n, const bn *d); /* d != 0 */ |
| 114 | int bq_is_zero(const bq *a); |
| 115 | int bq_sign(const bq *a); |
| 116 | int bq_cmp(const bq *a, const bq *b); |
| 117 | void bq_add(bq *r, const bq *a, const bq *b); |
| 118 | void bq_sub(bq *r, const bq *a, const bq *b); |
| 119 | void bq_mul(bq *r, const bq *a, const bq *b); |
| 120 | void bq_div(bq *r, const bq *a, const bq *b); /* b != 0 */ |
| 121 | void bq_neg(bq *r, const bq *a); |
| 122 | void bq_abs(bq *r, const bq *a); |
| 123 | double bq_to_double(const bq *a); /* correctly rounded (normal range) */ |
| 124 | void bq_from_double(bq *r, double x); /* exact; x finite */ |
| 125 | void bq_from_scaled(bq *r, const bn *n, unsigned k); /* n / 10^k */ |
| 126 | |
| 127 | /* ---- exact Gaussian rationals ------------------------------------------------------------- */ |
| 128 | typedef struct { bq re, im; } gq; |
| 129 | |
| 130 | void gq_init(gq *a); |
| 131 | void gq_free(gq *a); |
| 132 | void gq_copy(gq *r, const gq *a); |
| 133 | int gq_is_zero(const gq *a); |
| 134 | void gq_add(gq *r, const gq *a, const gq *b); |
| 135 | void gq_sub(gq *r, const gq *a, const gq *b); |
| 136 | void gq_mul(gq *r, const gq *a, const gq *b); |
| 137 | void gq_div(gq *r, const gq *a, const gq *b); /* b != 0 */ |
| 138 | void gq_conj(gq *r, const gq *a); |
| 139 | void gq_neg(gq *r, const gq *a); |
| 140 | |
| 141 | /* ---- parsed case (at1_case.c) -------------------------------------------------------------- */ |
| 142 | typedef enum { AT1_TARGET_IDEAL = 0, AT1_TARGET_INTERACTING = 1 } at1_target; |
| 143 | typedef enum { AT1_BOUND_NONE = 0, AT1_BOUND_ESTIMATED = 1, AT1_BOUND_RIGOROUS = 2 } at1_bound_kind; |
| 144 | typedef enum { AT1_EXPECT_PASS = 0, AT1_EXPECT_FAIL = 1 } at1_expect; |
| 145 | |
| 146 | #define AT1_FAILURE_CODE_COUNT 12 |
| 147 | extern const char *const at1_failure_codes[AT1_FAILURE_CODE_COUNT]; /* sorted bytewise */ |
| 148 | |
| 149 | /* a scaled decimal N@k, kept exactly */ |
| 150 | typedef struct { bn n; unsigned k; } at1_scaled; |
| 151 | |
| 152 | typedef struct { |
| 153 | /* exact semantic content */ |
| 154 | char name[AT1_NAME_LEN + 1]; |
| 155 | int clock_dim; /* N */ |
| 156 | bq energies[AT1_CLOCK_DIM_MAX]; |
| 157 | bq h0, h[3]; /* system_hamiltonian_pauli h0,hx,hy,hz */ |
| 158 | bq v[AT1_CLOCK_DIM_MAX][3]; /* interaction_pauli j vx,vy,vz */ |
| 159 | int ref_index; /* r */ |
| 160 | char ref_label[AT1_LABEL_LEN + 1]; |
| 161 | gq psi0[2]; |
| 162 | bq tau, weight; |
| 163 | int label_count; /* M */ |
| 164 | char labels[AT1_LABEL_MAX][AT1_LABEL_LEN + 1]; |
| 165 | /* acceptance */ |
| 166 | int control_positive; |
| 167 | at1_target target; |
| 168 | at1_expect expected_outcome; |
| 169 | unsigned expected_codes; /* bit i = at1_failure_codes[i] */ |
| 170 | at1_bound_kind min_bound_kind; |
| 171 | at1_scaled tol_constraint, tol_povm, tol_probability, tol_zero, tol_schrodinger; |
| 172 | /* identities (lowercase hex) and the copied text blocks */ |
| 173 | char case_id[65], acceptance_id[65], case_file_sha256[65]; |
| 174 | char *semantic_text; size_t semantic_len; /* "begin semantic\n" .. "end semantic\n" */ |
| 175 | char *acceptance_text; size_t acceptance_len; /* "begin acceptance\n" .. "end acceptance\n" */ |
| 176 | int initialized; |
| 177 | } at1_case; |
| 178 | |
| 179 | void at1_case_free(at1_case *c); |
| 180 | /* Parse and validate case bytes (AT1_CASE_V1 section 4, first failure in order). On a refusal |
| 181 | * the case is freed and the refusal status returned. AT1_ERR_RESOURCE for engine limits. */ |
| 182 | at1_status at1_case_parse(const uint8_t *bytes, size_t len, at1_case *c); |
| 183 | /* domain-tagged SHA-256 as lowercase hex (tag || 0x00 || data) */ |
| 184 | void at1_tagged_sha256(const char *tag, const char *data, size_t len, char out[65]); |
| 185 | void at1_sha256_hex(const uint8_t *data, size_t len, char out[65]); |
| 186 | |
| 187 | /* ---- exact kernel (at1_exact.c) ------------------------------------------------------------ */ |
| 188 | typedef struct { |
| 189 | int dim; /* D = 2N */ |
| 190 | int kernel_dim; /* D - rank(H_total), exact */ |
| 191 | int psi_zero; /* Psi exactly zero (exact decision) */ |
| 192 | int levels_in_psi; /* L: clock levels j with P_0 (e_j (x) psi_0) != 0 */ |
| 193 | double *h_re, *h_im; /* H_total rounded to binary64, D x D row major */ |
| 194 | double *y_re, *y_im; /* Y = sum_j P_0 (|E_j> (x) psi_0), exact, rounded once (D) */ |
| 195 | int *owner; /* clock level j whose y_j covers entry i, or -1 (D) */ |
| 196 | double hmax; /* max_i sum over l with owner[l] >= 0 of |H_total[i,l]| */ |
| 197 | } at1_kernel; |
| 198 | |
| 199 | void at1_kernel_free(at1_kernel *k); |
| 200 | at1_status at1_kernel_build(const at1_case *c, at1_kernel *k); |
| 201 | |
| 202 | /* ---- numerics (at1_numeric.c) --------------------------------------------------------------- */ |
| 203 | typedef enum { AT1_LABEL_DEFINED = 0, AT1_LABEL_UNDEFINED = 1, AT1_LABEL_INDETERMINATE = 2 } at1_label_status; |
| 204 | |
| 205 | typedef struct { |
| 206 | double value, bound; /* bound >= 0, ESTIMATED */ |
| 207 | int undefined; /* write "undefined 0@0" */ |
| 208 | } at1_val; |
| 209 | |
| 210 | typedef struct { |
| 211 | int kernel_dim; |
| 212 | int trivial; /* kernel_dim == 0 or Psi exactly zero */ |
| 213 | at1_val constraint, povm; |
| 214 | int M; |
| 215 | at1_label_status status[AT1_LABEL_MAX]; |
| 216 | at1_val clock[AT1_LABEL_MAX]; |
| 217 | at1_val pauli[AT1_LABEL_MAX][6]; /* X+, X-, Y+, Y-, Z+, Z- */ |
| 218 | } at1_values; |
| 219 | |
| 220 | /* reversed != 0 evaluates the labels in reverse order (test T8; values must be bit-identical) */ |
| 221 | at1_status at1_compute(const at1_case *c, const at1_kernel *k, int reversed, at1_values *out); |
| 222 | /* label status rule (AT1_RESULT_V1 section 3) decided exactly on the binary64 value and the |
| 223 | * scaled-decimal bound written for it */ |
| 224 | at1_label_status at1_status_rule(double p, double bound, const at1_scaled *tol_zero); |
| 225 | /* smallest scaled decimal >= x (x >= 0 finite), at most 7 significant digits; "0@0" for 0 */ |
| 226 | void at1_bound_format(double x, char *buf, size_t cap); |
| 227 | void at1_f64_format(double x, char *buf, size_t cap); |
| 228 | |
| 229 | /* ---- result (at1_result.c) ------------------------------------------------------------------- */ |
| 230 | /* Writes the complete AT1_RESULT_V1 file into a malloc'd buffer (*out, *out_len). |
| 231 | * oracle_text == NULL means the oracle is unavailable: outcome ERROR ORACLE_UNAVAILABLE. |
| 232 | * Returns AT1_ERR_ARGUMENT with a message in err (cap bytes) for a malformed oracle record |
| 233 | * or provenance file. */ |
| 234 | at1_status at1_result_write(const at1_case *c, const at1_values *v, |
| 235 | const char *oracle_text, size_t oracle_len, |
| 236 | const char *prov_text, size_t prov_len, |
| 237 | char **out, size_t *out_len, char *err, size_t cap); |
| 238 | /* an ERROR result with the given run error code (INTERNAL_ERROR, RESOURCE_LIMIT) */ |
| 239 | at1_status at1_result_write_error(const at1_case *c, const char *error_code, |
| 240 | const char *prov_text, size_t prov_len, |
| 241 | char **out, size_t *out_len, char *err, size_t cap); |
| 242 | /* engine-only values lines (component output for debugging; not an interface) */ |
| 243 | at1_status at1_values_write(const at1_case *c, const at1_values *v, char **out, size_t *out_len); |
| 244 | |
| 245 | /* ---- io (at1_io.c) ------------------------------------------------------------------------- */ |
| 246 | at1_status at1_read_file(const char *path, uint8_t **bytes, size_t *len); |
| 247 | |
| 248 | /* ---- allocation helpers (at1_bn.c) --------------------------------------------------------- */ |
| 249 | void *at1_xmalloc(size_t n); |
| 250 | void *at1_xcalloc(size_t n, size_t sz); |
| 251 | void *at1_xrealloc(void *p, size_t n); |
| 252 | |
| 253 | #endif |