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 / omega_vc_bridge.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 | /* omega_vc_bridge.c -- see omega_vc_bridge.h. */ |
| 18 | #include "omega_vc_bridge.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "omega_program_ir.h" |
| 25 | #include "omega_receipt.h" |
| 26 | #include "sha256.h" |
| 27 | |
| 28 | #define PROFILE "host-v1" |
| 29 | #define PROFILE_VERSION "1.0.0" |
| 30 | |
| 31 | int omega_vc_bridge_init(OmegaVcBridge *b) |
| 32 | { |
| 33 | if (!b) return -1; |
| 34 | memset(b, 0, sizeof *b); |
| 35 | return omega_vcstore_init(&b->store) == 0 ? 0 : -1; |
| 36 | } |
| 37 | |
| 38 | void omega_vc_bridge_destroy(OmegaVcBridge *b) |
| 39 | { |
| 40 | if (!b) return; |
| 41 | for (size_t i = 0; i < b->n_receipts; i++) free(b->receipts[i].json); |
| 42 | for (size_t i = 0; i < b->n_blobs; i++) free(b->blobs[i].bytes); |
| 43 | free(b->receipts); |
| 44 | free(b->blobs); |
| 45 | omega_vcstore_destroy(&b->store); |
| 46 | memset(b, 0, sizeof *b); |
| 47 | } |
| 48 | |
| 49 | static int fetch_copy(const uint8_t *p, size_t n, uint8_t **bytes, size_t *len) |
| 50 | { |
| 51 | *bytes = malloc(n + 1); |
| 52 | if (!*bytes) return 2; |
| 53 | memcpy(*bytes, p, n); |
| 54 | *len = n; |
| 55 | return 0; |
| 56 | } |
| 57 | int omega_vc_bridge_fetch_receipt(void *ctx, const uint8_t id[32], uint8_t **bytes, size_t *len) |
| 58 | { |
| 59 | const OmegaVcBridge *b = ctx; |
| 60 | for (size_t i = 0; b && i < b->n_receipts; i++) |
| 61 | if (memcmp(b->receipts[i].id, id, 32) == 0) return fetch_copy((const uint8_t *)b->receipts[i].json, b->receipts[i].len, bytes, len); |
| 62 | return 1; |
| 63 | } |
| 64 | int omega_vc_bridge_fetch_blob(void *ctx, const uint8_t digest[32], uint8_t **bytes, size_t *len) |
| 65 | { |
| 66 | const OmegaVcBridge *b = ctx; |
| 67 | for (size_t i = 0; b && i < b->n_blobs; i++) |
| 68 | if (memcmp(b->blobs[i].digest, digest, 32) == 0) return fetch_copy(b->blobs[i].bytes, b->blobs[i].len, bytes, len); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | static void hex32(const uint8_t *in, char out[65]) |
| 73 | { |
| 74 | static const char h[] = "0123456789abcdef"; |
| 75 | for (int i = 0; i < 32; i++) { out[2 * i] = h[in[i] >> 4]; out[2 * i + 1] = h[in[i] & 15]; } |
| 76 | out[64] = 0; |
| 77 | } |
| 78 | |
| 79 | /* the receipt JSON, with idhex in the "id" field. dep_hex is n x 65 chars. Returns bytes written or -1. */ |
| 80 | static int emit_receipt(char *o, size_t cap, const char *idhex, const char *sid, const char *dig, const char *root, |
| 81 | const char (*dep_hex)[65], size_t nd) |
| 82 | { |
| 83 | size_t n = 0; |
| 84 | #define PF(...) do { int w_ = snprintf(o + n, cap - n, __VA_ARGS__); if (w_ < 0 || (size_t)w_ >= cap - n) return -1; n += (size_t)w_; } while (0) |
| 85 | PF("{\n\"schema\": \"EvidenceReceiptV1\",\n\"version\": 1,\n\"id\": \"%s\",\n\"kind\": \"" PROFILE "\",\n", idhex); |
| 86 | PF("\"tier\": \"HOST_TEST\",\n\"result\": \"PASS\",\n\"timestamp\": 1,\n"); |
| 87 | PF("\"repo\": \"omega-vc-bridge\",\n\"commit\": \"0000000000000000000000000000000000000000\",\n\"dirty\": false,\n"); |
| 88 | PF("\"toolchain\": \"omega-vc-bridge\",\n\"procedure\": \"in-process omega_program_verify and program id recompute (self-minted HOST_TEST receipt, not an independent run)\",\n"); |
| 89 | PF("\"machine\": \"host\",\n\"env_class\": \"HOST_TEST\",\n"); |
| 90 | PF("\"input_artifacts\": [\"sha256:%s\", \"sha256:%s\"],\n\"output_artifacts\": [],\n", sid, dig); |
| 91 | PF("\"assertions\": [{\"id\": \"omega_program_verify\", \"expected\": \"pass\", \"observed\": \"pass\", \"pass\": true, \"source\": \"omega_vc_bridge\", \"note\": \"\"}],\n"); |
| 92 | PF("\"dependencies\": ["); |
| 93 | for (size_t i = 0; i < nd; i++) PF("%s\"%s\"", i ? ", " : "", dep_hex[i]); |
| 94 | PF("],\n\"declared_mutation\": \"NONE\",\n\"observed_mutation\": \"NONE\",\n\"authority\": \"\",\n\"output_digest\": \"%s\",\n\"external_refs\": [],\n\"ledger\": null,\n\"lease\": null,\n\"required_features\": [],\n\"reserved\": \"\"\n}\n", root); |
| 95 | #undef PF |
| 96 | return (int)n; |
| 97 | } |
| 98 | |
| 99 | typedef struct { uint8_t *p; size_t n, cap; int bad; } Out; |
| 100 | static void oput(Out *o, const void *d, size_t k) |
| 101 | { |
| 102 | if (o->bad) return; |
| 103 | if (o->n + k > o->cap) { |
| 104 | size_t nc = (o->n + k) * 2 + 64; |
| 105 | uint8_t *t = realloc(o->p, nc); |
| 106 | if (!t) { o->bad = 1; return; } |
| 107 | o->p = t; o->cap = nc; |
| 108 | } |
| 109 | memcpy(o->p + o->n, d, k); |
| 110 | o->n += k; |
| 111 | } |
| 112 | static void ou32(Out *o, uint32_t v) { uint8_t t[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; oput(o, t, 4); } |
| 113 | static void ostr(Out *o, const char *s) |
| 114 | { |
| 115 | size_t n = strlen(s); |
| 116 | uint8_t l[8]; |
| 117 | for (int i = 0; i < 8; i++) l[i] = (uint8_t)((uint64_t)n >> (56 - 8 * i)); |
| 118 | oput(o, l, 8); |
| 119 | oput(o, s, n); |
| 120 | } |
| 121 | |
| 122 | static int put_receipt(OmegaVcBridge *b, const uint8_t id[32], const char *json, size_t len) |
| 123 | { |
| 124 | for (size_t i = 0; i < b->n_receipts; i++) if (memcmp(b->receipts[i].id, id, 32) == 0) return 0; |
| 125 | if (b->n_receipts == b->cap_receipts) { |
| 126 | size_t nc = b->cap_receipts ? b->cap_receipts * 2 : 16; |
| 127 | OmegaVcBridgeReceipt *t = realloc(b->receipts, nc * sizeof *t); |
| 128 | if (!t) return -1; |
| 129 | b->receipts = t; b->cap_receipts = nc; |
| 130 | } |
| 131 | char *c = malloc(len + 1); |
| 132 | if (!c) return -1; |
| 133 | memcpy(c, json, len); c[len] = 0; |
| 134 | memcpy(b->receipts[b->n_receipts].id, id, 32); |
| 135 | b->receipts[b->n_receipts].json = c; |
| 136 | b->receipts[b->n_receipts].len = len; |
| 137 | b->n_receipts++; |
| 138 | return 0; |
| 139 | } |
| 140 | static int put_blob(OmegaVcBridge *b, const uint8_t digest[32], const uint8_t *bytes, size_t len) |
| 141 | { |
| 142 | for (size_t i = 0; i < b->n_blobs; i++) if (memcmp(b->blobs[i].digest, digest, 32) == 0) return 0; |
| 143 | if (b->n_blobs == b->cap_blobs) { |
| 144 | size_t nc = b->cap_blobs ? b->cap_blobs * 2 : 16; |
| 145 | OmegaVcBridgeBlob *t = realloc(b->blobs, nc * sizeof *t); |
| 146 | if (!t) return -1; |
| 147 | b->blobs = t; b->cap_blobs = nc; |
| 148 | } |
| 149 | uint8_t *c = malloc(len ? len : 1); |
| 150 | if (!c) return -1; |
| 151 | memcpy(c, bytes, len); |
| 152 | memcpy(b->blobs[b->n_blobs].digest, digest, 32); |
| 153 | b->blobs[b->n_blobs].bytes = c; |
| 154 | b->blobs[b->n_blobs].len = len; |
| 155 | b->n_blobs++; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int cmp_dep(const void *a, const void *b) { return memcmp(a, b, 32); } |
| 160 | |
| 161 | int omega_vc_bridge_admit(OmegaVcBridge *b, OmegaLibrary *lib, const OmegaProgram *prog, |
| 162 | const SemanticId *deps, size_t dep_count, const uint8_t evidence[32]) |
| 163 | { |
| 164 | static const uint8_t zero[32] = { 0 }; |
| 165 | if (!b || !lib || !prog || !evidence || memcmp(evidence, zero, 32) == 0 || dep_count > OMEGA_LIB_MAX_DEPS || (dep_count && !deps)) return -1; |
| 166 | if (!prog->is_realized || !prog->body.has_body || memcmp(prog->program_id.bytes, zero, 32) == 0) return -1; |
| 167 | int rc = -1; |
| 168 | uint8_t *ir = NULL, *vc = NULL; |
| 169 | size_t ir_len = 0; |
| 170 | uint8_t digest[32], contract[32], root[32], evid[32]; |
| 171 | uint8_t (*dep_sorted)[32] = NULL, (*dep_con)[32] = NULL, (*dep_rcpt)[32] = NULL; |
| 172 | char (*dep_hex)[65] = NULL; |
| 173 | char *json = NULL; |
| 174 | Out o = { NULL, 0, 0, 0 }; |
| 175 | OmegaVcRecord *rec = NULL; |
| 176 | OmegaProgram *cp = NULL; |
| 177 | |
| 178 | /* The bridge checks for itself; it never trusts the caller's is_verified flag or program_id. It |
| 179 | * verifies a private copy (omega_program_verify writes is_verified) and recomputes the id from the |
| 180 | * IR it is about to store. */ |
| 181 | cp = malloc(sizeof *cp); |
| 182 | if (!cp) goto done; |
| 183 | *cp = *prog; |
| 184 | { VerifyReport rep; |
| 185 | if (omega_program_verify(cp, &rep) != 0) goto done; /* VC1B:verify */ |
| 186 | } |
| 187 | if (omega_program_ir_encode(prog, &ir, &ir_len) != 0) goto done; |
| 188 | { uint8_t rid_[32]; |
| 189 | if (omega_program_ir_recompute_id(ir, ir_len, rid_) != 0 || memcmp(rid_, prog->program_id.bytes, 32) != 0) goto done; /* VC1B:id */ |
| 190 | } |
| 191 | sha256_hash(ir, ir_len, digest); |
| 192 | if (omega_program_contract_id(prog, contract) != 0) goto done; |
| 193 | { /* evidence root binds the caller's evidence digest to this program */ |
| 194 | static const uint8_t dom[] = "aien.vc1.bridge.evidence.v1"; |
| 195 | uint8_t buf[sizeof dom + 64]; |
| 196 | memcpy(buf, dom, sizeof dom); |
| 197 | memcpy(buf + sizeof dom, evidence, 32); |
| 198 | memcpy(buf + sizeof dom + 32, prog->program_id.bytes, 32); |
| 199 | sha256_hash(buf, sizeof dom + 64, root); |
| 200 | memcpy(evid, root, 32); |
| 201 | } |
| 202 | size_t nd = dep_count; |
| 203 | dep_sorted = malloc((nd ? nd : 1) * 32); |
| 204 | dep_con = malloc((nd ? nd : 1) * 32); |
| 205 | dep_rcpt = malloc((nd ? nd : 1) * 32); |
| 206 | dep_hex = malloc((nd ? nd : 1) * 65); |
| 207 | rec = malloc(sizeof *rec); |
| 208 | if (!dep_sorted || !dep_con || !dep_rcpt || !dep_hex || !rec) goto done; |
| 209 | for (size_t i = 0; i < nd; i++) memcpy(dep_sorted[i], deps[i].bytes, 32); |
| 210 | qsort(dep_sorted, nd, 32, cmp_dep); |
| 211 | for (size_t i = 0; i < nd; i++) { |
| 212 | if (i && memcmp(dep_sorted[i - 1], dep_sorted[i], 32) == 0) goto done; /* duplicate dependency */ |
| 213 | if (omega_vcstore_get(&b->store, dep_sorted[i], rec) != 0) goto done; /* must be admitted already */ |
| 214 | memcpy(dep_con[i], rec->vc.contract_id, 32); |
| 215 | memcpy(dep_rcpt[i], rec->vc.receipt_id, 32); |
| 216 | hex32(dep_rcpt[i], dep_hex[i]); |
| 217 | } |
| 218 | char sid_h[65], dig_h[65], root_h[65], zero_h[65]; |
| 219 | hex32(prog->program_id.bytes, sid_h); |
| 220 | hex32(digest, dig_h); |
| 221 | hex32(root, root_h); |
| 222 | memset(zero_h, '0', 64); zero_h[64] = 0; |
| 223 | json = malloc(8192); |
| 224 | if (!json) goto done; |
| 225 | int n0 = emit_receipt(json, 8192, zero_h, sid_h, dig_h, root_h, (const char (*)[65])dep_hex, nd); |
| 226 | if (n0 < 0) goto done; |
| 227 | uint8_t rid[32]; |
| 228 | { |
| 229 | OmegaReceipt r; |
| 230 | char err[200]; |
| 231 | if (omega_receipt_parse((const uint8_t *)json, (size_t)n0, &r, err, sizeof err) != 0) goto done; |
| 232 | int cr = omega_receipt_compute_id(&r, rid); |
| 233 | omega_receipt_free(&r); |
| 234 | if (cr != 0) goto done; |
| 235 | } |
| 236 | char rid_h[65]; |
| 237 | hex32(rid, rid_h); |
| 238 | int n1 = emit_receipt(json, 8192, rid_h, sid_h, dig_h, root_h, (const char (*)[65])dep_hex, nd); |
| 239 | if (n1 < 0) goto done; |
| 240 | /* the canonical Verified Crumb */ |
| 241 | oput(&o, "AIEN_VERIFIED_CRUMB_V1", 22); |
| 242 | ou32(&o, OMEGA_VC_FORMAT_VERSION); |
| 243 | oput(&o, prog->program_id.bytes, 32); |
| 244 | oput(&o, contract, 32); |
| 245 | { uint8_t k = OMEGA_VC_DIGEST_IR; oput(&o, &k, 1); } |
| 246 | oput(&o, digest, 32); |
| 247 | ou32(&o, 0); /* realizations */ |
| 248 | ou32(&o, (uint32_t)nd); |
| 249 | for (size_t i = 0; i < nd; i++) { oput(&o, dep_sorted[i], 32); oput(&o, dep_con[i], 32); } |
| 250 | oput(&o, rid, 32); |
| 251 | ostr(&o, PROFILE); |
| 252 | ostr(&o, PROFILE_VERSION); |
| 253 | oput(&o, evid, 32); |
| 254 | ou32(&o, 0); /* exports */ |
| 255 | ou32(&o, 1); ostr(&o, OMEGA_BRIDGE_SELFMINTED_CAPABILITY); /* capabilities: one, so the build domain refuses this record */ /* VC1B:cap */ |
| 256 | if (o.bad) goto done; |
| 257 | vc = o.p; |
| 258 | uint8_t vcid[32]; |
| 259 | omega_vc_compute_id(vc, o.n, vcid); |
| 260 | if (put_receipt(b, rid, json, (size_t)n1) != 0 || put_blob(b, digest, ir, ir_len) != 0) goto done; |
| 261 | OmegaResolver r; |
| 262 | memset(&r, 0, sizeof r); |
| 263 | r.store = &b->store; |
| 264 | r.domain = OMEGA_DOMAIN_BUILD; |
| 265 | r.fetch_receipt = omega_vc_bridge_fetch_receipt; |
| 266 | r.fetch_ctx = b; |
| 267 | r.fetch_blob = omega_vc_bridge_fetch_blob; |
| 268 | r.blob_ctx = b; |
| 269 | OmegaResolveError err; |
| 270 | if (omega_resolve_admit(&r, &b->store, vc, o.n, vcid, NULL, &err) != 0) goto done; |
| 271 | /* store first, library second: the library entry carries the receipt id as its evidence hash. |
| 272 | * Insert the copy the bridge verified itself (the library copies it by value); the caller's |
| 273 | * is_verified flag is never consulted. */ |
| 274 | rc = omega_library_insert(lib, cp, deps, dep_count, rid); |
| 275 | done: |
| 276 | free(ir); |
| 277 | free(vc); |
| 278 | free(dep_sorted); free(dep_con); free(dep_rcpt); free(dep_hex); |
| 279 | free(json); |
| 280 | free(rec); |
| 281 | free(cp); |
| 282 | return rc; |
| 283 | } |
| 284 | |
| 285 | int omega_vc_bridge_admit_abstraction(OmegaVcBridge *b, OmegaLibrary *lib, const OmegaAbstractionCandidate *cand, |
| 286 | const uint8_t evidence[32]) |
| 287 | { |
| 288 | if (!b || !lib || !cand || !evidence) return -1; |
| 289 | if (!cand->is_verified || !cand->is_nontrivial || cand->compression_score <= 0) return -1; |
| 290 | return omega_vc_bridge_admit(b, lib, &cand->abstraction, NULL, 0, evidence); |
| 291 | } |