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 / omega_library.c

245 lines · 9417 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#include "omega_library.h"
18#include "sha256.h"
19#include <string.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <time.h>
23
24int omega_library_init(OmegaLibrary *lib) {
25 if (!lib) return -1;
26 memset(lib, 0, sizeof(OmegaLibrary));
27 lib->version = 1;
28 return omega_library_compute_digest(lib);
29}
30
31void omega_library_destroy(OmegaLibrary *lib) {
32 if (!lib) return;
33 for (size_t i = 0; i < lib->count; ++i) {
34 omega_program_destroy(&lib->entries[i].program);
35 }
36 memset(lib, 0, sizeof(OmegaLibrary));
37}
38
39static void lib_digest_u32(sha256_ctx *ctx, uint32_t v) {
40 uint8_t b[4];
41 b[0] = (uint8_t)((v >> 24) & 0xFF);
42 b[1] = (uint8_t)((v >> 16) & 0xFF);
43 b[2] = (uint8_t)((v >> 8) & 0xFF);
44 b[3] = (uint8_t)(v & 0xFF);
45 sha256_update(ctx, b, sizeof b);
46}
47
48/* Streaming digest over the whole library: no fixed buffer, so no entry can ever fall
49 * outside the digest however many entries or dependencies the library holds. */
50int omega_library_compute_digest(OmegaLibrary *lib) {
51 if (!lib) return -1;
52
53 sha256_ctx ctx;
54 sha256_init(&ctx);
55
56 /* Magic "LIB1" */
57 sha256_update(&ctx, (const uint8_t *)"LIB1", 4);
58
59 lib_digest_u32(&ctx, lib->version);
60 lib_digest_u32(&ctx, (uint32_t)lib->count);
61
62 for (size_t i = 0; i < lib->count; ++i) {
63 const OmegaLibraryEntry *e = &lib->entries[i];
64
65 sha256_update(&ctx, e->program.program_id.bytes, OMEGA_ID_BYTES);
66 sha256_update(&ctx, e->program.realization.realization_id.bytes, OMEGA_ID_BYTES);
67 lib_digest_u32(&ctx, e->version_introduced);
68 {
69 uint8_t kind = e->admission_kind; /* VC1:digest-kind */
70 sha256_update(&ctx, &kind, 1);
71 }
72 lib_digest_u32(&ctx, (uint32_t)e->dep_count);
73 for (size_t d = 0; d < e->dep_count && d < OMEGA_LIB_MAX_DEPS; ++d) {
74 sha256_update(&ctx, e->dependency_ids[d].bytes, OMEGA_ID_BYTES);
75 }
76 sha256_update(&ctx, e->evidence_receipt_hash, 32);
77 }
78
79 sha256_final(&ctx, lib->state_digest);
80 return 0;
81}
82
83
84int omega_library_advance_version(OmegaLibrary *lib) {
85 if (!lib) return -1;
86 lib->version++;
87 return omega_library_compute_digest(lib);
88}
89
90const OmegaLibraryEntry* omega_library_find_by_id(const OmegaLibrary *lib, const SemanticId *prog_id) {
91 if (!lib || !prog_id) return NULL;
92 for (size_t i = 0; i < lib->count; ++i) {
93 if (memcmp(lib->entries[i].program.program_id.bytes, prog_id->bytes, OMEGA_ID_BYTES) == 0) {
94 return &lib->entries[i];
95 }
96 }
97 return NULL;
98}
99
100const OmegaLibraryEntry* omega_library_find_by_realization_id(const OmegaLibrary *lib, const SemanticId *real_id) {
101 if (!lib || !real_id) return NULL;
102 for (size_t i = 0; i < lib->count; ++i) {
103 if (memcmp(lib->entries[i].program.realization.realization_id.bytes, real_id->bytes, OMEGA_ID_BYTES) == 0) {
104 return &lib->entries[i];
105 }
106 }
107 return NULL;
108}
109
110const OmegaLibraryEntry* omega_library_find_by_name(const OmegaLibrary *lib, const char *name) {
111 if (!lib || !name) return NULL;
112 for (size_t i = 0; i < lib->count; ++i) {
113 if (strcmp(lib->entries[i].program.name, name) == 0) {
114 return &lib->entries[i];
115 }
116 }
117 return NULL;
118}
119
120size_t omega_library_query_by_type(const OmegaLibrary *lib,
121 TypeTag in_type, uint16_t in_width,
122 TypeTag out_type, uint16_t out_width,
123 const OmegaLibraryEntry **out_results, size_t max_results) {
124 if (!lib || !out_results || max_results == 0) return 0;
125 size_t found = 0;
126
127 for (size_t i = 0; i < lib->count && found < max_results; ++i) {
128 const OmegaProgram *p = &lib->entries[i].program;
129 if (p->contract.input_type == in_type &&
130 p->contract.input_width == in_width &&
131 p->contract.output_type == out_type &&
132 p->contract.output_width == out_width) {
133 out_results[found++] = &lib->entries[i];
134 }
135 }
136 return found;
137}
138
139bool omega_library_has_cycle(const OmegaLibrary *lib, const SemanticId *target_id,
140 const SemanticId *new_deps, size_t new_dep_count) {
141 if (!lib || !target_id || !new_deps || new_dep_count == 0) return false;
142
143 /* Check direct containment */
144 for (size_t i = 0; i < new_dep_count; ++i) {
145 if (memcmp(target_id->bytes, new_deps[i].bytes, OMEGA_ID_BYTES) == 0) {
146 return true;
147 }
148 /* Recursive transitive lookup through library entries */
149 const OmegaLibraryEntry *dep_entry = omega_library_find_by_id(lib, &new_deps[i]);
150 if (dep_entry && dep_entry->dep_count > 0) {
151 if (omega_library_has_cycle(lib, target_id, dep_entry->dependency_ids, dep_entry->dep_count)) {
152 return true;
153 }
154 }
155 }
156 return false;
157}
158
159static bool lib_hash_is_zero(const uint8_t h[32]) {
160 uint8_t acc = 0;
161 for (size_t i = 0; i < 32; ++i) acc |= h[i];
162 return acc == 0;
163}
164
165/* Shared admission path. Every refusal returns -1 before the library is touched.
166 * Each guard sits on one tagged line: the VC1-LIB mutation test (Makefile test-library)
167 * deletes or weakens exactly that line in a copy of this file and expects the unit test
168 * to fail. Keep one guard per line when editing. */
169static int lib_admit(OmegaLibrary *lib, const OmegaProgram *prog,
170 const SemanticId *deps, size_t dep_count,
171 const uint8_t hash[32], uint8_t kind) {
172 if (!lib || !prog) return -1;
173
174 /* Fail-closed verification gating: only verified programs admitted */
175 if (!prog->is_verified || !prog->is_realized) return -1; /* VC1:verified-gate */
176
177 /* Capacity check */
178 if (lib->count >= OMEGA_LIB_MAX_PROGRAMS) return -1; /* VC1:capacity */
179
180 /* No identity (no semantic body): refuse (spec/program-identity.md 2.3) */
181 {
182 static const uint8_t zero[OMEGA_ID_BYTES];
183 if (memcmp(prog->program_id.bytes, zero, OMEGA_ID_BYTES) == 0) return -1; /* VC1:zero-id */
184 }
185
186 /* Duplicate check */
187 if (omega_library_find_by_id(lib, &prog->program_id) != NULL) return -1; /* VC1:duplicate */
188
189 /* Dependencies: never truncated, every one must already be in the library */
190 if (dep_count > OMEGA_LIB_MAX_DEPS) return -1; /* VC1:dep-overflow */
191 if (dep_count > 0 && !deps) return -1; /* VC1:dep-null */
192 for (size_t i = 0; i < dep_count; ++i) {
193 if (omega_library_find_by_id(lib, &deps[i]) == NULL) return -1; /* VC1:dep-unknown */
194 }
195
196 /* Cycle check (kept: defence in depth; unreachable while deps must pre-exist) */
197 if (dep_count > 0) {
198 if (omega_library_has_cycle(lib, &prog->program_id, deps, dep_count)) return -1; /* VC1:cycle */
199 }
200
201 OmegaLibraryEntry *e = &lib->entries[lib->count];
202 memset(e, 0, sizeof(OmegaLibraryEntry));
203
204 e->program = *prog;
205 e->version_introduced = lib->version;
206 e->timestamp_added = (uint64_t)time(NULL);
207 e->admission_kind = kind;
208 e->dep_count = dep_count;
209 for (size_t i = 0; i < dep_count; ++i) {
210 e->dependency_ids[i] = deps[i];
211 }
212 memcpy(e->evidence_receipt_hash, hash, 32);
213
214 lib->count++;
215 return omega_library_compute_digest(lib);
216}
217
218int omega_library_insert(OmegaLibrary *lib, const OmegaProgram *prog,
219 const SemanticId *deps, size_t dep_count,
220 const uint8_t receipt_hash[32]) {
221 /* Evidence: a receipt hash is mandatory */
222 if (!receipt_hash) return -1; /* VC1:null-receipt */
223 if (lib_hash_is_zero(receipt_hash)) return -1; /* VC1:zero-receipt */
224 return lib_admit(lib, prog, deps, dep_count, receipt_hash, OMEGA_LIB_ADMISSION_VERIFIED);
225}
226
227/* Genesis loading only. See the contract in omega_library.h. No flag unlocks this. */
228int omega_library_insert_bootstrap(OmegaLibrary *lib, const OmegaProgram *prog,
229 const SemanticId *deps, size_t dep_count,
230 const uint8_t audit_hash[32]) {
231 /* Evidence: the audit hash of the manual genesis audit is mandatory */
232 if (!audit_hash) return -1; /* VC1:bootstrap-null-audit */
233 if (lib_hash_is_zero(audit_hash)) return -1; /* VC1:bootstrap-zero-audit */
234 return lib_admit(lib, prog, deps, dep_count, audit_hash, OMEGA_LIB_ADMISSION_BOOTSTRAP); /* VC1:kind-bootstrap */
235}
236
237int omega_library_export_primitives(const OmegaLibrary *lib, SynthPrimitiveBank *bank) {
238 if (!lib || !bank) return -1;
239 memset(bank, 0, sizeof(SynthPrimitiveBank));
240
241 for (size_t i = 0; i < lib->count && bank->count < SYNTH_MAX_PRIMITIVES; ++i) {
242 bank->programs[bank->count++] = lib->entries[i].program;
243 }
244 return 0;
245}