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_vcstore.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 | #ifndef OMEGA_VCSTORE_H |
| 18 | #define OMEGA_VCSTORE_H |
| 19 | |
| 20 | /* omega_vcstore: the Verified Crumb Store (VC1 stage 3, ADR 0029). |
| 21 | * |
| 22 | * A content-addressed, immutable store of VerifiedCrumbV1 records (aien-protocols |
| 23 | * specs/verified-crumb/SPEC.md), keyed by the 32-byte semantic_id (the Omega program id). |
| 24 | * The canonical encoding, the digest_kind byte and the VC id derivation are those of SPEC.md, |
| 25 | * byte for byte (VC id = SHA-256 over the canonical bytes, domain tag included). |
| 26 | * |
| 27 | * WHAT THE STORE DOES |
| 28 | * - insert: decode (format refusals of SPEC 3.6), recompute the VC id and compare it with the |
| 29 | * caller's claimed id, refuse a different record for an existing semantic_id (immutability), |
| 30 | * accept identical bytes as a no-op, and require every dependency to be in the store already |
| 31 | * with a matching contract_id (fail closed, cycles impossible by construction and refused). |
| 32 | * - get / closure: recompute the VC id of the stored bytes on every access and refuse on mismatch. |
| 33 | * - digest: streaming SHA-256 over every object (semantic_id order, admission kind, canonical |
| 34 | * bytes). Independent of insert order. Names are never part of it. |
| 35 | * - save / load: one file, explicit big-endian, digests at the end, load recomputes everything. |
| 36 | * |
| 37 | * WHAT THE STORE DOES NOT DO |
| 38 | * - It does NOT verify receipts. It holds each record's receipt_id and exposes it through |
| 39 | * omega_vcstore_receipt_of(); checking the receipt (SPEC 5.1) and the verifier profile is the |
| 40 | * evidence verifier's job (VC1 stage 4/5). A record in the store is "well-formed, immutable, |
| 41 | * closed under dependencies", not "proven". The unused ADR 0029 Decision 9 codes below are |
| 42 | * reserved for that verifier and the resolver. |
| 43 | * - It does not read lockfiles and does not decide trust from a name. |
| 44 | * |
| 45 | * ADMISSION KIND: VERIFIED is the normal path. BOOTSTRAP is for the manually audited Genesis Set |
| 46 | * VC-GENESIS-1 only (src/omega_genesis.h, docs/osc/VC-GENESIS-1.md). The two raw insert functions |
| 47 | * are private (omega_vcstore_priv.h) and reachable only through omega_resolve_admit and |
| 48 | * omega_resolve_admit_genesis. There is no flag, environment variable or build option that unlocks |
| 49 | * BOOTSTRAP or skips any refusal: a BOOTSTRAP record whose semantic id is not on the list is |
| 50 | * refused when it is inserted and when a store file holding it is loaded (GENESIS_NOT_LISTED). |
| 51 | * For a bootstrap record the receipt_id field carries the hash of the audit record. The kind is |
| 52 | * part of the state digest. |
| 53 | * |
| 54 | * NAME INDEX: a separate map name -> semantic_id. Many names may point at one id. A bound name |
| 55 | * changes only through the explicit omega_vcstore_name_rebind(). Nothing in get, closure, |
| 56 | * insert, receipt_of or the state digest reads it; omega_vcstore_resolve_name() is the only |
| 57 | * reader and returns the id and nothing else. It has its own digest. Names are convenience, |
| 58 | * digests are authority. |
| 59 | */ |
| 60 | |
| 61 | #include <stddef.h> |
| 62 | #include <stdint.h> |
| 63 | |
| 64 | #define OMEGA_VC_ID_BYTES 32 |
| 65 | #define OMEGA_VC_FORMAT_VERSION 1u |
| 66 | #define OMEGA_VC_MAX_ENTRIES 256 /* per list, SPEC 3.3; over the limit is refused, never truncated */ |
| 67 | #define OMEGA_VC_MAX_STRING 256 |
| 68 | #define OMEGA_VC_DIGEST_SOURCE 0x01 /* canonical source */ |
| 69 | #define OMEGA_VC_DIGEST_IR 0x02 /* canonical IR */ |
| 70 | |
| 71 | #define OMEGA_VCS_ADMISSION_VERIFIED 1u |
| 72 | #define OMEGA_VCS_ADMISSION_BOOTSTRAP 2u /* genesis set only */ |
| 73 | |
| 74 | /* Result codes. 0 is success. Names are what matters (omega_vcstore_code_name). */ |
| 75 | enum { |
| 76 | OMEGA_VCS_OK = 0, |
| 77 | /* ARCH-0029 Decision 9, in the ADR's order */ |
| 78 | OMEGA_VCS_UNVERIFIED_DEPENDENCY = 1, /* absent id, absent dependency, contract mismatch on an edge */ |
| 79 | OMEGA_VCS_MISSING_RECEIPT, /* all-zero receipt_id */ |
| 80 | OMEGA_VCS_RECEIPT_HASH_MISMATCH, /* reserved: evidence verifier */ |
| 81 | OMEGA_VCS_DEPENDENCY_NOT_PINNED, /* reserved: resolver / lockfile */ |
| 82 | OMEGA_VCS_DEPENDENCY_CYCLE, /* self dependency, or a cycle in a loaded or tampered graph */ |
| 83 | OMEGA_VCS_STALE_RECEIPT, /* reserved: evidence verifier */ |
| 84 | OMEGA_VCS_UNDECLARED_IMPORT, /* reserved: compiler */ |
| 85 | OMEGA_VCS_TAINTED_ARTIFACT, /* reserved: resolver */ |
| 86 | OMEGA_VCS_UNKNOWN_VERIFIER_PROFILE, /* reserved: evidence verifier */ |
| 87 | /* SPEC 3.6 format refusals (same numbers as vc1-check) */ |
| 88 | OMEGA_VCS_BAD_DOMAIN_TAG = 20, |
| 89 | OMEGA_VCS_UNKNOWN_FORMAT_VERSION, |
| 90 | OMEGA_VCS_TRUNCATED, |
| 91 | OMEGA_VCS_TRAILING_BYTES, |
| 92 | OMEGA_VCS_DUPLICATE_DEPENDENCY, |
| 93 | OMEGA_VCS_UNSORTED_DEPENDENCIES, |
| 94 | OMEGA_VCS_NONCANONICAL_SET, |
| 95 | OMEGA_VCS_BAD_STRING, |
| 96 | OMEGA_VCS_ZERO_ID, |
| 97 | OMEGA_VCS_TOO_MANY_ENTRIES, |
| 98 | OMEGA_VCS_BAD_DIGEST_KIND, |
| 99 | /* store-local */ |
| 100 | OMEGA_VCS_VCSTORE_ID_MISMATCH = 40, /* recomputed VC id or loaded digest differs */ |
| 101 | OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT, /* same semantic_id, different bytes or admission kind */ |
| 102 | OMEGA_VCS_VCSTORE_MALFORMED, /* bad argument, bad name, bad store file */ |
| 103 | OMEGA_VCS_VCSTORE_NOT_FOUND, /* name not bound */ |
| 104 | OMEGA_VCS_VCSTORE_NAME_EXISTS, /* name already bound to another id; use name_rebind */ |
| 105 | OMEGA_VCS_VCSTORE_CAPACITY, /* closure output array too small (needed count returned) */ |
| 106 | OMEGA_VCS_VCSTORE_NOMEM, |
| 107 | OMEGA_VCS_VCSTORE_IO, |
| 108 | OMEGA_VCS_GENESIS_NOT_LISTED /* BOOTSTRAP record whose semantic id is not in VC-GENESIS-1 */ |
| 109 | }; |
| 110 | |
| 111 | const char *omega_vcstore_code_name(int code); |
| 112 | |
| 113 | /* ---- decoded view (pointers into the canonical bytes; no copies) ---- */ |
| 114 | typedef struct { const uint8_t *p; size_t len; } OmegaVcStr; |
| 115 | |
| 116 | typedef struct { |
| 117 | uint32_t format_version; |
| 118 | const uint8_t *semantic_id; /* 32 */ |
| 119 | const uint8_t *contract_id; /* 32 */ |
| 120 | uint8_t digest_kind; |
| 121 | const uint8_t *source_or_ir_digest; /* 32 */ |
| 122 | uint32_t n_realizations; |
| 123 | const uint8_t *realization_ids; /* n x 32 */ |
| 124 | uint32_t n_dependencies; |
| 125 | const uint8_t *dependencies; /* n x 64: semantic_id (32) then required_contract (32) */ |
| 126 | const uint8_t *receipt_id; /* 32 */ |
| 127 | OmegaVcStr verifier_profile, verifier_version; |
| 128 | const uint8_t *evidence_root; /* 32 */ |
| 129 | uint32_t n_exports; |
| 130 | OmegaVcStr exports[OMEGA_VC_MAX_ENTRIES]; |
| 131 | uint32_t n_capabilities; |
| 132 | OmegaVcStr capabilities[OMEGA_VC_MAX_ENTRIES]; |
| 133 | } OmegaVcView; |
| 134 | |
| 135 | /* Format-only check of canonical bytes (SPEC 3, refusal order SPEC 3.5). Returns 0 or the first |
| 136 | * format refusal code. Never sorts: unsorted input is refused. On success fills *view. */ |
| 137 | int omega_vc_decode(const uint8_t *bytes, size_t len, OmegaVcView *view); |
| 138 | |
| 139 | /* VC id = SHA-256(canonical bytes), tag included (SPEC 3.4). Does not validate. */ |
| 140 | void omega_vc_compute_id(const uint8_t *bytes, size_t len, uint8_t out[OMEGA_VC_ID_BYTES]); |
| 141 | |
| 142 | /* ---- store ---- |
| 143 | * The fields are public so tests can inspect and corrupt them; production code must treat them |
| 144 | * as read-only and use the functions. objs is sorted ascending by semantic_id (memcmp). */ |
| 145 | typedef struct { |
| 146 | uint8_t semantic_id[32]; |
| 147 | uint8_t vc_id[32]; |
| 148 | uint8_t admission_kind; /* OMEGA_VCS_ADMISSION_* */ |
| 149 | uint8_t *bytes; /* owned canonical bytes, stable until destroy */ |
| 150 | size_t len; |
| 151 | } OmegaVcObject; |
| 152 | |
| 153 | typedef struct { |
| 154 | char *name; /* owned, NUL-terminated */ |
| 155 | uint8_t id[32]; |
| 156 | } OmegaVcName; |
| 157 | |
| 158 | typedef struct { |
| 159 | OmegaVcObject *objs; |
| 160 | size_t count, cap; |
| 161 | OmegaVcName *names; /* sorted ascending by strcmp */ |
| 162 | size_t n_names, cap_names; |
| 163 | } OmegaVcStore; |
| 164 | |
| 165 | typedef struct { |
| 166 | const uint8_t *canonical; /* points into the store, valid until destroy */ |
| 167 | size_t canonical_len; |
| 168 | uint8_t vc_id[32]; |
| 169 | uint8_t admission_kind; |
| 170 | OmegaVcView vc; |
| 171 | } OmegaVcRecord; |
| 172 | |
| 173 | int omega_vcstore_init(OmegaVcStore *s); |
| 174 | void omega_vcstore_destroy(OmegaVcStore *s); |
| 175 | size_t omega_vcstore_count(const OmegaVcStore *s); |
| 176 | |
| 177 | /* There is no public insert. Records enter only through omega_resolve_admit and |
| 178 | * omega_resolve_admit_genesis (src/omega_resolve.h); the raw functions are declared in the private |
| 179 | * header omega_vcstore_priv.h (VC1 stage 6). */ |
| 180 | |
| 181 | /* Fetch by semantic_id. Recomputes the VC id of the stored bytes and re-decodes them; any |
| 182 | * mismatch is VCSTORE_ID_MISMATCH. Absent: UNVERIFIED_DEPENDENCY. */ |
| 183 | int omega_vcstore_get(const OmegaVcStore *s, const uint8_t semantic_id[32], OmegaVcRecord *out); |
| 184 | |
| 185 | /* The receipt_id the record carries. NOT verified here (see header). */ |
| 186 | int omega_vcstore_receipt_of(const OmegaVcStore *s, const uint8_t semantic_id[32], uint8_t out_receipt[32]); |
| 187 | |
| 188 | int omega_vcstore_admission_kind(const OmegaVcStore *s, const uint8_t semantic_id[32], uint8_t *out_kind); |
| 189 | |
| 190 | /* Transitive dependency closure of id, root included, in deterministic post-order: every |
| 191 | * dependency appears before whoever needs it; dependencies are visited in ascending semantic_id |
| 192 | * order; each id appears once. out_ids receives n x 32 bytes; *out_n the count. Each record is |
| 193 | * fetched with the get checks, each edge's contract is checked. Errors: UNVERIFIED_DEPENDENCY |
| 194 | * (absent id/dependency, contract mismatch), VCSTORE_ID_MISMATCH, DEPENDENCY_CYCLE. If cap is |
| 195 | * smaller than the closure: VCSTORE_CAPACITY with *out_n = the needed count (cap 0 and |
| 196 | * out_ids NULL may be used to ask for the size). On any other error *out_n = 0. Never a |
| 197 | * partial success. Does not read the name index. */ |
| 198 | int omega_vcstore_closure(const OmegaVcStore *s, const uint8_t id[32], |
| 199 | uint8_t *out_ids, size_t cap, size_t *out_n); |
| 200 | |
| 201 | /* State digest over the objects only (never the names). Streaming; no size limit. */ |
| 202 | int omega_vcstore_digest(const OmegaVcStore *s, uint8_t out[32]); |
| 203 | |
| 204 | /* ---- name index ---- names match [A-Za-z_][A-Za-z0-9_.-]{0,127} (the omega.lock grammar) */ |
| 205 | int omega_vcstore_name_bind(OmegaVcStore *s, const char *name, const uint8_t id[32]); |
| 206 | int omega_vcstore_name_rebind(OmegaVcStore *s, const char *name, const uint8_t id[32]); |
| 207 | int omega_vcstore_resolve_name(const OmegaVcStore *s, const char *name, uint8_t out_id[32]); |
| 208 | int omega_vcstore_name_index_digest(const OmegaVcStore *s, uint8_t out[32]); |
| 209 | |
| 210 | /* ---- persistence ---- |
| 211 | * File layout (all integers big-endian, no host structs): |
| 212 | * "AIEN_VCSTORE_V1" (15 bytes) |
| 213 | * u64 object count, then per object: u8 admission_kind, u64 length, canonical bytes |
| 214 | * (objects strictly ascending by semantic_id) |
| 215 | * u64 name count, then per name: u64 length, name bytes, 32-byte id (names strictly ascending) |
| 216 | * 32 bytes object state digest, 32 bytes name-index digest |
| 217 | * (end of file: no trailing byte) |
| 218 | * load builds a new store, re-decodes and re-hashes every object, re-checks dependencies and |
| 219 | * acyclicity, recomputes both digests and refuses on any mismatch. The digests are integrity |
| 220 | * checks, not signatures: anyone can recompute them. On failure *s is unchanged; on success its old contents are replaced (s must be initialised). */ |
| 221 | int omega_vcstore_save(const OmegaVcStore *s, const char *path); |
| 222 | int omega_vcstore_load(OmegaVcStore *s, const char *path); |
| 223 | |
| 224 | #endif /* OMEGA_VCSTORE_H */ |