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 / runtime / aien_machine_id.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 | /* |
| 18 | * aien_machine_id.h -- the one canonical AIEN machine identity (M20). |
| 19 | * |
| 20 | * A MachineId names one physical machine for its whole life (aienos ADR 0010: |
| 21 | * created once at provisioning, resumed across boot, reboot, restart and model |
| 22 | * reload; a part swap keeps it; a new box of the same brand gets a new one). |
| 23 | * It is the identity carried by every 32-byte machine slot in the stack: |
| 24 | * |
| 25 | * aienos receipt `machine_id_digest` (ADR 0014 offset 352): the slot holds |
| 26 | * SHA-256("AIENOS-MACHINE-ID-V1\0" || provisioned MachineId bytes), |
| 27 | * which is exactly AienMachineId.id below. Zero there = absent. |
| 28 | * aienos ARGUS ABI `machine_id[32]` (native/argus/argus_abi.h). |
| 29 | * physics FORGE V2 `machine_identity` d32 (tags 0x0101, 0x0602): the stable |
| 30 | * name, distinct from the descriptor digest. |
| 31 | * omega rx_argus event slot; the Capability Graph's machine column through |
| 32 | * AienMachineIndex (below). |
| 33 | * |
| 34 | * What it is NOT (demoted forms, kept for what they are): |
| 35 | * - OmegaMachineGraph.machine_id / omega_blackwell_get_machine_id: a digest |
| 36 | * of a hardware PROFILE (pipeline, caches, registers). Every DGX Spark has |
| 37 | * the same one. It names a machine model inside realization triple ids; |
| 38 | * it is never a machine identity. |
| 39 | * - CqEntry/CqCandidate/CqNeed `machine_id` (uint32_t, rx_capq.h): a local |
| 40 | * runtime INDEX, valid in one process only. Never persisted, never sent. |
| 41 | * Resolve it through AienMachineIndex. |
| 42 | * - rx_argus provisional id SHA-256("ARGUS-PROVISIONAL-MACHINE-v1"||run_id): |
| 43 | * a per-run handle. Used only while no canonical record is configured. |
| 44 | * |
| 45 | * Derivation. id = SHA-256("AIENOS-MACHINE-ID-V1\0" || root), where root is |
| 46 | * AIEN_MID_ROOT_PROVISIONED the opaque bytes generated once at provisioning |
| 47 | * AIEN_MID_ROOT_HARDWARE the 32-byte digest of the owner-enrolled key |
| 48 | * (TRUST-1), when the machine has one |
| 49 | * AIEN_MID_ROOT_IMPORTED marks an id taken from a bare 32-byte slot whose root |
| 50 | * is not known here. The root kind is provenance only: equality and hashing use |
| 51 | * the 32 id bytes, so the same machine compares equal whichever slot it came |
| 52 | * from. |
| 53 | * |
| 54 | * Persisted / transported form (44 bytes, every field a byte string, so no |
| 55 | * endianness): |
| 56 | * 0 4 magic "AMID" |
| 57 | * 4 1 version 0x01 |
| 58 | * 5 1 root AIEN_MID_ROOT_* |
| 59 | * 6 2 reserved, zero |
| 60 | * 8 32 id non-zero |
| 61 | * 40 4 check first 4 bytes of SHA-256(bytes 0..39) |
| 62 | * Text form: the 88 lowercase hex digits of those 44 bytes. A decoder rejects |
| 63 | * any other length, magic, version, root, nonzero reserved, zero id or bad |
| 64 | * check. Nothing transient (pid, run id, thread, pointer, index) is in it. |
| 65 | * |
| 66 | * Ownership of runtime state (one writer each; nothing here moves them): |
| 67 | * World state rx_world.h. Shared logical state changes only by a |
| 68 | * reaction's atomic publish at a World commit. |
| 69 | * Cortex rx_cortex.h. Durable, append-only memory/evidence; |
| 70 | * records name machines by AienMachineId, never by index. |
| 71 | * J-Space rx_jspace.h. Runtime state-space material; placement |
| 72 | * names a machine by AienMachineId; mutation of shared |
| 73 | * logical state goes through the World commit. |
| 74 | * Capability Graph rx_capq.h. Holds uint32 machine indexes from |
| 75 | * AienMachineIndex; anything persisted or sent converts |
| 76 | * the index back to AienMachineId first. |
| 77 | * |
| 78 | * Header-only codec (needs only sha256.c) so every producer links it as-is; |
| 79 | * persistence and the index table live in aien_machine_id.c. |
| 80 | */ |
| 81 | #ifndef AIEN_MACHINE_ID_H |
| 82 | #define AIEN_MACHINE_ID_H |
| 83 | |
| 84 | #include "omega_native.h" |
| 85 | #include "sha256.h" |
| 86 | |
| 87 | #include <stddef.h> |
| 88 | #include <stdint.h> |
| 89 | #include <string.h> |
| 90 | |
| 91 | #define AIEN_MID_ID_BYTES 32u |
| 92 | #define AIEN_MID_RECORD_BYTES 44u |
| 93 | #define AIEN_MID_TEXT_CHARS (2u * AIEN_MID_RECORD_BYTES) |
| 94 | #define AIEN_MID_VERSION 0x01u |
| 95 | #define AIEN_MID_ROOT_MAX 4096u /* longest provisioned root accepted */ |
| 96 | |
| 97 | static const char AIEN_MID_DOMAIN[] = "AIENOS-MACHINE-ID-V1"; /* + its NUL: 21 bytes */ |
| 98 | #define AIEN_MID_DOMAIN_BYTES 21u |
| 99 | |
| 100 | enum { |
| 101 | AIEN_MID_ROOT_PROVISIONED = 1, |
| 102 | AIEN_MID_ROOT_HARDWARE = 2, |
| 103 | AIEN_MID_ROOT_IMPORTED = 3 |
| 104 | }; |
| 105 | |
| 106 | enum { |
| 107 | AIEN_MID_OK = 0, |
| 108 | AIEN_MID_E_ARG = -1, /* null, bad length, unknown root */ |
| 109 | AIEN_MID_E_ZERO = -2, /* all-zero id: "absent", never an identity */ |
| 110 | AIEN_MID_E_FORMAT = -3, /* bad magic, version, reserved or hex */ |
| 111 | AIEN_MID_E_CHECK = -4, /* check bytes do not match */ |
| 112 | AIEN_MID_E_IO = -5, |
| 113 | AIEN_MID_E_FULL = -6 /* index table full */ |
| 114 | }; |
| 115 | |
| 116 | typedef struct { |
| 117 | uint8_t root; /* AIEN_MID_ROOT_*: provenance, not identity */ |
| 118 | uint8_t id[AIEN_MID_ID_BYTES]; /* the identity */ |
| 119 | } AienMachineId; |
| 120 | |
| 121 | static inline int aien_mid_is_zero_(const uint8_t *b) { |
| 122 | uint64_t res = 1; |
| 123 | if (omega2_rx_mid_is_zero(&res, b, AIEN_MID_ID_BYTES) != OMEGA_OK) return 1; |
| 124 | return res != 0; |
| 125 | } |
| 126 | |
| 127 | static inline int aien_mid_root_valid_(uint8_t r) { |
| 128 | return r == AIEN_MID_ROOT_PROVISIONED || r == AIEN_MID_ROOT_HARDWARE || |
| 129 | r == AIEN_MID_ROOT_IMPORTED; |
| 130 | } |
| 131 | |
| 132 | /* Derive from a root. HARDWARE takes exactly the 32-byte owner-key digest; |
| 133 | * PROVISIONED takes 1..AIEN_MID_ROOT_MAX opaque bytes. */ |
| 134 | static inline int aien_mid_derive(uint8_t root_kind, const uint8_t *root, size_t len, |
| 135 | AienMachineId *out) { |
| 136 | if (!root || !out) return AIEN_MID_E_ARG; |
| 137 | if (root_kind == AIEN_MID_ROOT_HARDWARE) { |
| 138 | if (len != AIEN_MID_ID_BYTES) return AIEN_MID_E_ARG; |
| 139 | } else if (root_kind == AIEN_MID_ROOT_PROVISIONED) { |
| 140 | if (len == 0 || len > AIEN_MID_ROOT_MAX) return AIEN_MID_E_ARG; |
| 141 | } else { |
| 142 | return AIEN_MID_E_ARG; |
| 143 | } |
| 144 | sha256_ctx h; |
| 145 | sha256_init(&h); |
| 146 | sha256_update(&h, (const uint8_t *)AIEN_MID_DOMAIN, AIEN_MID_DOMAIN_BYTES); |
| 147 | sha256_update(&h, root, len); |
| 148 | sha256_final(&h, out->id); |
| 149 | out->root = root_kind; |
| 150 | return aien_mid_is_zero_(out->id) ? AIEN_MID_E_ZERO : AIEN_MID_OK; |
| 151 | } |
| 152 | |
| 153 | /* From a bare 32-byte slot (FORGE machine_identity, aienos receipt |
| 154 | * machine_id_digest, ARGUS machine_id). All-zero means "absent": rejected. */ |
| 155 | static inline int aien_mid_from_slot(const uint8_t slot[AIEN_MID_ID_BYTES], AienMachineId *out) { |
| 156 | if (!slot || !out) return AIEN_MID_E_ARG; |
| 157 | if (aien_mid_is_zero_(slot)) return AIEN_MID_E_ZERO; |
| 158 | memcpy(out->id, slot, AIEN_MID_ID_BYTES); |
| 159 | out->root = AIEN_MID_ROOT_IMPORTED; |
| 160 | return AIEN_MID_OK; |
| 161 | } |
| 162 | |
| 163 | static inline void aien_mid_to_slot(const AienMachineId *m, uint8_t slot[AIEN_MID_ID_BYTES]) { |
| 164 | memcpy(slot, m->id, AIEN_MID_ID_BYTES); |
| 165 | } |
| 166 | |
| 167 | /* Equality and hash: the 32 id bytes only. Hash is the first 8 id bytes read |
| 168 | * big-endian: the same on every host, no seed. */ |
| 169 | static inline int aien_mid_equal(const AienMachineId *a, const AienMachineId *b) { |
| 170 | if (a == b) return 1; |
| 171 | uint64_t res = 0; |
| 172 | if (omega2_rx_mid_equal(&res, a->id, AIEN_MID_ID_BYTES, b->id, AIEN_MID_ID_BYTES) != OMEGA_OK) |
| 173 | return 0; |
| 174 | return res != 0; |
| 175 | } |
| 176 | |
| 177 | static inline int aien_mid_compare(const AienMachineId *a, const AienMachineId *b) { |
| 178 | if (a == b) return 0; |
| 179 | uint64_t res = 0; |
| 180 | if (omega2_rx_mid_compare(&res, a->id, AIEN_MID_ID_BYTES, b->id, AIEN_MID_ID_BYTES) != OMEGA_OK) |
| 181 | return 0; |
| 182 | return (int)(int64_t)res; |
| 183 | } |
| 184 | |
| 185 | static inline uint64_t aien_mid_hash(const AienMachineId *m) { |
| 186 | uint64_t res = 0; |
| 187 | (void)omega2_rx_mid_hash(&res, m->id, AIEN_MID_ID_BYTES); |
| 188 | return res; |
| 189 | } |
| 190 | |
| 191 | static inline void aien_mid_check_(const uint8_t rec[AIEN_MID_RECORD_BYTES], uint8_t out[4]) { |
| 192 | uint8_t d[SHA256_DIGEST_SIZE]; |
| 193 | sha256_hash(rec, 40, d); |
| 194 | memcpy(out, d, 4); |
| 195 | } |
| 196 | |
| 197 | static inline int aien_mid_encode(const AienMachineId *m, uint8_t out[AIEN_MID_RECORD_BYTES]) { |
| 198 | if (!m || !out) return AIEN_MID_E_ARG; |
| 199 | uint64_t res = 0; |
| 200 | if (omega2_rx_mid_pack_hdr(&res, (uint64_t)m->root, m->id, AIEN_MID_ID_BYTES, |
| 201 | out, AIEN_MID_RECORD_BYTES) != OMEGA_OK) |
| 202 | return AIEN_MID_E_ARG; |
| 203 | if ((int64_t)res != AIEN_MID_OK) return (int)(int64_t)res; |
| 204 | aien_mid_check_(out, out + 40); |
| 205 | return AIEN_MID_OK; |
| 206 | } |
| 207 | |
| 208 | static inline int aien_mid_decode(const uint8_t *in, size_t len, AienMachineId *out) { |
| 209 | if (!in || !out) return AIEN_MID_E_ARG; |
| 210 | if (len != AIEN_MID_RECORD_BYTES) return AIEN_MID_E_FORMAT; |
| 211 | uint8_t chk[4], dst[33]; |
| 212 | aien_mid_check_(in, chk); |
| 213 | uint64_t res = 0; |
| 214 | if (omega2_rx_mid_val_rec(&res, in, AIEN_MID_RECORD_BYTES, chk, 4, dst, 33) != OMEGA_OK) |
| 215 | return AIEN_MID_E_ARG; |
| 216 | if ((int64_t)res != AIEN_MID_OK) return (int)(int64_t)res; |
| 217 | out->root = dst[0]; |
| 218 | memcpy(out->id, dst + 1, AIEN_MID_ID_BYTES); |
| 219 | return AIEN_MID_OK; |
| 220 | } |
| 221 | |
| 222 | /* Text form: out must hold AIEN_MID_TEXT_CHARS + 1 bytes (NUL-terminated). */ |
| 223 | static inline int aien_mid_to_text(const AienMachineId *m, char out[AIEN_MID_TEXT_CHARS + 1]) { |
| 224 | uint8_t rec[AIEN_MID_RECORD_BYTES]; |
| 225 | int rc = aien_mid_encode(m, rec); |
| 226 | if (rc != AIEN_MID_OK) return rc; |
| 227 | uint64_t res = 0; |
| 228 | if (omega2_rx_mid_to_hex(&res, rec, AIEN_MID_RECORD_BYTES, |
| 229 | (uint8_t *)out, AIEN_MID_TEXT_CHARS) != OMEGA_OK) |
| 230 | return AIEN_MID_E_ARG; |
| 231 | out[AIEN_MID_TEXT_CHARS] = 0; |
| 232 | return AIEN_MID_OK; |
| 233 | } |
| 234 | |
| 235 | static inline int aien_mid_from_text(const char *s, AienMachineId *out) { |
| 236 | if (!s || !out) return AIEN_MID_E_ARG; |
| 237 | if (strlen(s) != AIEN_MID_TEXT_CHARS) return AIEN_MID_E_FORMAT; |
| 238 | uint8_t rec[AIEN_MID_RECORD_BYTES]; |
| 239 | uint64_t res = 0; |
| 240 | if (omega2_rx_mid_from_hex(&res, (const uint8_t *)s, AIEN_MID_TEXT_CHARS, |
| 241 | rec, AIEN_MID_RECORD_BYTES) != OMEGA_OK) |
| 242 | return AIEN_MID_E_FORMAT; |
| 243 | if ((int64_t)res != AIEN_MID_OK) return (int)(int64_t)res; |
| 244 | return aien_mid_decode(rec, sizeof rec, out); |
| 245 | } |
| 246 | |
| 247 | /* ---- aien_machine_id.c -------------------------------------------------- */ |
| 248 | |
| 249 | /* Persist atomically (temp file, fsync, rename, fsync directory). A path that |
| 250 | * src/omega_path_check.h refuses (a ".." component, a byte outside its set, too |
| 251 | * long) is AIEN_MID_E_ARG for both calls, before any file is touched. */ |
| 252 | int aien_mid_store(const char *path, const AienMachineId *m); |
| 253 | /* Load; any file that is not exactly one valid record is rejected. */ |
| 254 | int aien_mid_load(const char *path, AienMachineId *out); |
| 255 | |
| 256 | /* Local runtime index <-> canonical identity. Index 0 means "none" (as in |
| 257 | * rx_capq); indexes are dense from 1 in bind order and live only in this |
| 258 | * process. The table is caller-owned and bounded. */ |
| 259 | typedef struct { |
| 260 | uint32_t capacity, count; |
| 261 | AienMachineId *slots; /* slots[i] is index i + 1 */ |
| 262 | } AienMachineIndex; |
| 263 | |
| 264 | void aien_mid_index_init(AienMachineIndex *t, AienMachineId *slots, uint32_t capacity); |
| 265 | /* Index for m, binding a new one if m is not known yet. 0 on full or bad id. */ |
| 266 | uint32_t aien_mid_index_bind(AienMachineIndex *t, const AienMachineId *m); |
| 267 | /* Index for m if bound, else 0. */ |
| 268 | uint32_t aien_mid_index_find(const AienMachineIndex *t, const AienMachineId *m); |
| 269 | /* Identity behind an index; AIEN_MID_E_ARG for 0 or an unbound index. */ |
| 270 | int aien_mid_index_get(const AienMachineIndex *t, uint32_t index, AienMachineId *out); |
| 271 | |
| 272 | #endif |