aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / continuity_codec.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 | /* continuity_codec.c -- see continuity_codec.h. Oracle: continuity.rs = |
| 18 | * crates/aienos-kernel/src/continuity.rs at aienos 4a116e5. Contract: |
| 19 | * native/kernel/CONTINUITY_RECOVERY_CONTRACT.md (aienos#222) sections 1-2. |
| 20 | * No allocation, no libc, no Store dependency. */ |
| 21 | #include "continuity_codec.h" |
| 22 | #include "sha256.h" /* native/argus */ |
| 23 | |
| 24 | static const uint8_t MAGIC_ROOT[8] = {'A', 'I', 'E', 'N', 'R', 'O', 'O', 'T'}; /* :34 */ |
| 25 | static const uint8_t MAGIC_MANIFEST[8] = {'A', 'I', 'E', 'N', 'M', 'A', 'N', 'I'}; /* :35 */ |
| 26 | static const uint8_t MAGIC_BRANCHES[8] = {'A', 'I', 'E', 'N', 'B', 'R', 'A', 'N'}; /* :36 */ |
| 27 | static const uint8_t MAGIC_WAL[8] = {'A', 'I', 'E', 'N', 'C', 'W', 'A', 'L'}; /* :37 */ |
| 28 | |
| 29 | /* Fixed body sizes (bytes after the 16-byte header). */ |
| 30 | #define ROOT_BODY 96u /* 32+16+32+8+1+7, continuity.rs:202 */ |
| 31 | #define MANIFEST_FIXED 120u /* 32+32+8+8+32+2+6, continuity.rs:268 */ |
| 32 | #define STATE_FIXED 48u /* 32+8+4+4, continuity.rs:394 */ |
| 33 | #define WAL_FIXED 16u /* 8+4+4, continuity.rs:533 */ |
| 34 | |
| 35 | /* K-1 reason text (PROPOSED; no Rust counterpart, contract 5.3). */ |
| 36 | static const char WHY_CAP[] = "continuity object exceeds the 16384-byte sealed Store cap"; |
| 37 | |
| 38 | static int fail(const char **why, int cls, const char *w) |
| 39 | { |
| 40 | if (why) |
| 41 | *why = w; |
| 42 | return cls; |
| 43 | } |
| 44 | |
| 45 | /* ---- small byte helpers (no libc) ---- */ |
| 46 | static void cp(uint8_t *d, const uint8_t *s, size_t n) |
| 47 | { |
| 48 | for (size_t i = 0; i < n; i++) |
| 49 | d[i] = s[i]; |
| 50 | } |
| 51 | static int eq(const uint8_t *a, const uint8_t *b, size_t n) |
| 52 | { |
| 53 | uint8_t x = 0; |
| 54 | for (size_t i = 0; i < n; i++) |
| 55 | x |= (uint8_t)(a[i] ^ b[i]); |
| 56 | return x == 0; |
| 57 | } |
| 58 | static int is_zero(const uint8_t *a, size_t n) |
| 59 | { |
| 60 | uint8_t x = 0; |
| 61 | for (size_t i = 0; i < n; i++) |
| 62 | x |= a[i]; |
| 63 | return x == 0; |
| 64 | } |
| 65 | /* Lexicographic order of two ids, as Rust [u8; 32] Ord (continuity.rs:361). */ |
| 66 | static int cmp32(const uint8_t *a, const uint8_t *b) |
| 67 | { |
| 68 | for (size_t i = 0; i < 32; i++) |
| 69 | if (a[i] != b[i]) |
| 70 | return a[i] < b[i] ? -1 : 1; |
| 71 | return 0; |
| 72 | } |
| 73 | static void put16(uint8_t *p, uint16_t v) |
| 74 | { |
| 75 | p[0] = (uint8_t)v; |
| 76 | p[1] = (uint8_t)(v >> 8); |
| 77 | } |
| 78 | static void put32(uint8_t *p, uint32_t v) |
| 79 | { |
| 80 | for (int i = 0; i < 4; i++) |
| 81 | p[i] = (uint8_t)(v >> (8 * i)); |
| 82 | } |
| 83 | static void put64(uint8_t *p, uint64_t v) |
| 84 | { |
| 85 | for (int i = 0; i < 8; i++) |
| 86 | p[i] = (uint8_t)(v >> (8 * i)); |
| 87 | } |
| 88 | static uint64_t get_le(const uint8_t *p, int n) |
| 89 | { |
| 90 | uint64_t v = 0; |
| 91 | for (int i = n - 1; i >= 0; i--) |
| 92 | v = (v << 8) | p[i]; |
| 93 | return v; |
| 94 | } |
| 95 | |
| 96 | /* ---- derivations ---- */ |
| 97 | void cc_root_branch_id(const uint8_t agent[32], uint8_t out[32]) |
| 98 | { |
| 99 | static const char D[] = "AIENOS_ROOT_BRANCH_v1:"; /* continuity.rs:72 */ |
| 100 | sha256_ctx h; |
| 101 | sha256_init(&h); |
| 102 | sha256_update(&h, (const uint8_t *)D, sizeof D - 1); |
| 103 | sha256_update(&h, agent, 32); |
| 104 | sha256_final(&h, out); |
| 105 | } |
| 106 | |
| 107 | void cc_child_branch_id(const uint8_t parent[32], uint64_t index, uint8_t out[32]) |
| 108 | { |
| 109 | static const char D[] = "AIENOS_CHILD_BRANCH_v1:"; /* continuity.rs:80 */ |
| 110 | uint8_t ix[8]; |
| 111 | for (int i = 0; i < 8; i++) |
| 112 | #ifdef CC_MUTANT_CHILD_INDEX_LE |
| 113 | ix[i] = (uint8_t)(index >> (8 * i)); /* MUTANT MC-6: little-endian */ |
| 114 | #else |
| 115 | ix[i] = (uint8_t)(index >> (56 - 8 * i)); /* big-endian, continuity.rs:82 */ |
| 116 | #endif |
| 117 | sha256_ctx h; |
| 118 | sha256_init(&h); |
| 119 | sha256_update(&h, (const uint8_t *)D, sizeof D - 1); |
| 120 | sha256_update(&h, parent, 32); |
| 121 | sha256_update(&h, ix, 8); |
| 122 | sha256_final(&h, out); |
| 123 | } |
| 124 | |
| 125 | int cc_object_id(uint16_t kind, const uint8_t *bytes, size_t len, uint8_t out[32]) |
| 126 | { |
| 127 | /* store/v1.rs:27 OBJECT_DOMAIN = b"AIENOS-STORE-OBJECT-V1\0" (23 bytes). */ |
| 128 | static const char D[] = "AIENOS-STORE-OBJECT-V1"; |
| 129 | if (!bytes || !out || kind == 0 || len == 0) /* v1.rs:64 */ |
| 130 | return CC_E_ARG; |
| 131 | if ((uint64_t)len > 67108864u) /* MAX_OBJECT_BYTES, v1.rs:15, :67 */ |
| 132 | return CC_E_ARG; |
| 133 | uint8_t d[12]; |
| 134 | put16(d, kind); /* v1.rs:74 */ |
| 135 | put16(d + 2, CC_STORE_OBJECT_VERSION); /* v1.rs:75 */ |
| 136 | put64(d + 4, (uint64_t)len); /* v1.rs:76 */ |
| 137 | sha256_ctx h; |
| 138 | sha256_init(&h); |
| 139 | sha256_update(&h, (const uint8_t *)D, sizeof D); /* includes the NUL */ |
| 140 | sha256_update(&h, d, sizeof d); |
| 141 | sha256_update(&h, bytes, len); |
| 142 | sha256_final(&h, out); |
| 143 | return CC_OK; |
| 144 | } |
| 145 | |
| 146 | /* ---- strict reader (continuity.rs:97-166) ---- */ |
| 147 | struct rd { |
| 148 | const uint8_t *b; |
| 149 | size_t len, at; |
| 150 | const char *why; |
| 151 | }; |
| 152 | |
| 153 | /* Reader::take (continuity.rs:123-132): the one bounds check. */ |
| 154 | static int take(struct rd *r, size_t n, const uint8_t **p) |
| 155 | { |
| 156 | #ifndef CC_MUTANT_SKIP_TAKE_BOUND |
| 157 | if (n > r->len - r->at) { /* r->at <= r->len always holds here */ |
| 158 | r->why = "truncated object"; |
| 159 | return CC_E_CORRUPT; |
| 160 | } |
| 161 | #endif |
| 162 | *p = r->b + r->at; |
| 163 | r->at += n; |
| 164 | return CC_OK; |
| 165 | } |
| 166 | static int rd_id(struct rd *r, uint8_t out[32]) |
| 167 | { |
| 168 | const uint8_t *p = 0; |
| 169 | int e = take(r, 32, &p); |
| 170 | if (!e) |
| 171 | cp(out, p, 32); |
| 172 | return e; |
| 173 | } |
| 174 | static int rd_uint(struct rd *r, int n, uint64_t *v) |
| 175 | { |
| 176 | const uint8_t *p = 0; |
| 177 | int e = take(r, (size_t)n, &p); |
| 178 | if (!e) |
| 179 | *v = get_le(p, n); |
| 180 | return e; |
| 181 | } |
| 182 | /* Reader::zeros (continuity.rs:154-159). */ |
| 183 | static int rd_zeros(struct rd *r, size_t n) |
| 184 | { |
| 185 | const uint8_t *p = 0; |
| 186 | int e = take(r, n, &p); |
| 187 | if (e) |
| 188 | return e; |
| 189 | #ifndef CC_MUTANT_ACCEPT_RESERVED |
| 190 | if (!is_zero(p, n)) { |
| 191 | r->why = "nonzero reserved"; |
| 192 | return CC_E_CORRUPT; |
| 193 | } |
| 194 | #endif |
| 195 | return CC_OK; |
| 196 | } |
| 197 | /* Reader::finish (continuity.rs:160-165). */ |
| 198 | static int rd_finish(struct rd *r) |
| 199 | { |
| 200 | if (r->at != r->len) { |
| 201 | r->why = "trailing bytes"; |
| 202 | return CC_E_CORRUPT; |
| 203 | } |
| 204 | return CC_OK; |
| 205 | } |
| 206 | |
| 207 | /* K-1 cap (PROPOSED) first, then Reader::open (continuity.rs:105-122). */ |
| 208 | static int rd_open(struct rd *r, const uint8_t *in, size_t len, const uint8_t magic[8]) |
| 209 | { |
| 210 | const uint8_t *p = 0; |
| 211 | uint64_t v = 0; |
| 212 | int e; |
| 213 | r->b = in; |
| 214 | r->len = len; |
| 215 | r->at = 0; |
| 216 | r->why = 0; |
| 217 | if (len > CC_MAX_OBJECT_BYTES) { |
| 218 | r->why = WHY_CAP; |
| 219 | return CC_E_LIMIT; |
| 220 | } |
| 221 | if ((e = take(r, 8, &p))) |
| 222 | return e; |
| 223 | if (!eq(p, magic, 8)) { |
| 224 | r->why = "bad magic"; |
| 225 | return CC_E_CORRUPT; |
| 226 | } |
| 227 | if ((e = rd_uint(r, 2, &v))) |
| 228 | return e; |
| 229 | if (v != CC_FORMAT_VERSION) { |
| 230 | r->why = "unsupported continuity format version"; |
| 231 | return CC_E_CORRUPT; |
| 232 | } |
| 233 | if ((e = rd_uint(r, 2, &v))) |
| 234 | return e; |
| 235 | #ifndef CC_MUTANT_ACCEPT_RESERVED |
| 236 | if (v != 0) { |
| 237 | r->why = "nonzero header reserved"; |
| 238 | return CC_E_CORRUPT; |
| 239 | } |
| 240 | #endif |
| 241 | if ((e = rd_uint(r, 4, &v))) |
| 242 | return e; |
| 243 | if (v != (uint64_t)(len - CC_HEADER_BYTES)) { |
| 244 | r->why = "body length mismatch"; |
| 245 | return CC_E_CORRUPT; |
| 246 | } |
| 247 | return CC_OK; |
| 248 | } |
| 249 | |
| 250 | /* ---- writer: callers size-check first, so writes never overrun ---- */ |
| 251 | struct wr { |
| 252 | uint8_t *b; |
| 253 | size_t at; |
| 254 | }; |
| 255 | static void w_bytes(struct wr *w, const uint8_t *s, size_t n) |
| 256 | { |
| 257 | cp(w->b + w->at, s, n); |
| 258 | w->at += n; |
| 259 | } |
| 260 | static void w_zeros(struct wr *w, size_t n) |
| 261 | { |
| 262 | for (size_t i = 0; i < n; i++) |
| 263 | w->b[w->at + i] = 0; |
| 264 | w->at += n; |
| 265 | } |
| 266 | static void w_u8(struct wr *w, uint8_t v) |
| 267 | { |
| 268 | w->b[w->at++] = v; |
| 269 | } |
| 270 | static void w_u16(struct wr *w, uint16_t v) |
| 271 | { |
| 272 | put16(w->b + w->at, v); |
| 273 | w->at += 2; |
| 274 | } |
| 275 | static void w_u32(struct wr *w, uint32_t v) |
| 276 | { |
| 277 | put32(w->b + w->at, v); |
| 278 | w->at += 4; |
| 279 | } |
| 280 | static void w_u64(struct wr *w, uint64_t v) |
| 281 | { |
| 282 | put64(w->b + w->at, v); |
| 283 | w->at += 8; |
| 284 | } |
| 285 | /* header() (continuity.rs:90-95). */ |
| 286 | static void w_header(struct wr *w, const uint8_t magic[8], size_t body) |
| 287 | { |
| 288 | w_bytes(w, magic, 8); |
| 289 | w_u16(w, CC_FORMAT_VERSION); |
| 290 | w_u16(w, 0); |
| 291 | w_u32(w, (uint32_t)body); |
| 292 | } |
| 293 | |
| 294 | /* Common encoder prologue: K-1 cap (PROPOSED), then the caller's buffer. */ |
| 295 | static int enc_begin(struct wr *w, uint8_t *out, size_t cap, size_t total, |
| 296 | const char **why) |
| 297 | { |
| 298 | if (total > CC_MAX_OBJECT_BYTES) |
| 299 | return fail(why, CC_E_LIMIT, WHY_CAP); |
| 300 | if (!out || cap < total) |
| 301 | return fail(why, CC_E_ARG, "output buffer too small"); |
| 302 | w->b = out; |
| 303 | w->at = 0; |
| 304 | return CC_OK; |
| 305 | } |
| 306 | |
| 307 | /* ---- AgentRoot (continuity.rs:204-247) ---- */ |
| 308 | int cc_root_encode(const struct cc_root *v, uint8_t *out, size_t cap, size_t *len, |
| 309 | const char **why) |
| 310 | { |
| 311 | struct wr w; |
| 312 | int e; |
| 313 | if (!v || !len) |
| 314 | return fail(why, CC_E_ARG, "null argument"); |
| 315 | *len = 0; |
| 316 | /* ProvisionSource is a Rust enum (continuity.rs:186-191): no other value |
| 317 | * exists there, so another value is a caller bug here. */ |
| 318 | if (v->source != CC_SOURCE_OPERATOR && v->source != CC_SOURCE_QUALIFICATION) |
| 319 | return fail(why, CC_E_ARG, "unknown provisioning source"); |
| 320 | if ((e = enc_begin(&w, out, cap, CC_ROOT_BYTES, why))) |
| 321 | return e; |
| 322 | w_header(&w, MAGIC_ROOT, ROOT_BODY); /* :207 */ |
| 323 | w_bytes(&w, v->agent_id, 32); /* :208 */ |
| 324 | w_bytes(&w, v->store_uuid, 16); /* :209 */ |
| 325 | w_bytes(&w, v->root_branch, 32); /* :210 */ |
| 326 | w_u64(&w, v->provisioned_generation); /* :211 */ |
| 327 | w_u8(&w, v->source); /* :212 */ |
| 328 | w_zeros(&w, 7); /* :213 */ |
| 329 | *len = w.at; |
| 330 | return CC_OK; |
| 331 | } |
| 332 | |
| 333 | int cc_root_decode(const uint8_t *in, size_t len, struct cc_root *v, const char **why) |
| 334 | { |
| 335 | struct rd r; |
| 336 | uint64_t x = 0; |
| 337 | const uint8_t *p = 0; |
| 338 | int e; |
| 339 | uint8_t rb[32]; |
| 340 | if (!in || !v) |
| 341 | return fail(why, CC_E_ARG, "null argument"); |
| 342 | if ((e = rd_open(&r, in, len, MAGIC_ROOT)) || (e = rd_id(&r, v->agent_id)) || /* :218-219 */ |
| 343 | (e = take(&r, 16, &p))) /* :221 */ |
| 344 | return fail(why, e, r.why); |
| 345 | cp(v->store_uuid, p, 16); |
| 346 | if ((e = rd_id(&r, v->root_branch)) || (e = rd_uint(&r, 8, &x))) /* :222-223 */ |
| 347 | return fail(why, e, r.why); |
| 348 | v->provisioned_generation = x; |
| 349 | if ((e = rd_uint(&r, 1, &x))) /* :224 */ |
| 350 | return fail(why, e, r.why); |
| 351 | if (x != CC_SOURCE_OPERATOR && x != CC_SOURCE_QUALIFICATION) /* :224-228 */ |
| 352 | return fail(why, CC_E_CORRUPT, "unknown provisioning source"); |
| 353 | v->source = (uint8_t)x; |
| 354 | if ((e = rd_zeros(&r, 7)) || (e = rd_finish(&r))) /* :229-230 */ |
| 355 | return fail(why, e, r.why); |
| 356 | if (is_zero(v->agent_id, 32)) /* :231-233 */ |
| 357 | return fail(why, CC_E_CORRUPT, "zero agent id"); |
| 358 | cc_root_branch_id(v->agent_id, rb); |
| 359 | if (!eq(rb, v->root_branch, 32)) /* :234-238 */ |
| 360 | return fail(why, CC_E_CORRUPT, "root branch does not derive from agent"); |
| 361 | return CC_OK; |
| 362 | } |
| 363 | |
| 364 | /* ---- ContinuityManifest (continuity.rs:263-315) ---- */ |
| 365 | int cc_manifest_encode(const struct cc_manifest *v, uint8_t *out, size_t cap, size_t *len, |
| 366 | const char **why) |
| 367 | { |
| 368 | struct wr w; |
| 369 | int e; |
| 370 | if (!v || !len) |
| 371 | return fail(why, CC_E_ARG, "null argument"); |
| 372 | *len = 0; |
| 373 | if (v->n_wal > CC_MAX_WAL_SEGMENTS) /* :265-267 */ |
| 374 | return fail(why, CC_E_LIMIT, "too many cortex WAL segments"); |
| 375 | size_t body = MANIFEST_FIXED + 32u * v->n_wal; /* :268 */ |
| 376 | if ((e = enc_begin(&w, out, cap, CC_HEADER_BYTES + body, why))) |
| 377 | return e; |
| 378 | w_header(&w, MAGIC_MANIFEST, body); /* :270 */ |
| 379 | w_bytes(&w, v->root, 32); /* :271 */ |
| 380 | w_bytes(&w, v->previous, 32); /* :272 (None = zero, :177-179) */ |
| 381 | w_u64(&w, v->sequence); /* :273 */ |
| 382 | w_u64(&w, v->incarnation); /* :274 */ |
| 383 | w_bytes(&w, v->agent_state, 32); /* :275 */ |
| 384 | w_u16(&w, (uint16_t)v->n_wal); /* :276 */ |
| 385 | w_zeros(&w, 6); /* :277 */ |
| 386 | for (uint32_t i = 0; i < v->n_wal; i++) |
| 387 | #ifdef CC_MUTANT_MANIFEST_LAST_WAL_ZERO |
| 388 | w_bytes(&w, v->wal[i == 63 ? 0 : i], 32); /* MUTANT: id 63 replaced by id 0 */ |
| 389 | #else |
| 390 | w_bytes(&w, v->wal[i], 32); /* :278-280 */ |
| 391 | #endif |
| 392 | *len = w.at; |
| 393 | return CC_OK; |
| 394 | } |
| 395 | |
| 396 | int cc_manifest_decode(const uint8_t *in, size_t len, struct cc_manifest *v, const char **why) |
| 397 | { |
| 398 | struct rd r; |
| 399 | uint64_t x = 0; |
| 400 | int e; |
| 401 | if (!in || !v) |
| 402 | return fail(why, CC_E_ARG, "null argument"); |
| 403 | if ((e = rd_open(&r, in, len, MAGIC_MANIFEST)) || (e = rd_id(&r, v->root)) || /* :285-286 */ |
| 404 | (e = rd_id(&r, v->previous)) || (e = rd_uint(&r, 8, &v->sequence)) || /* :287-288 */ |
| 405 | (e = rd_uint(&r, 8, &v->incarnation)) || (e = rd_id(&r, v->agent_state)) || /* :289-290 */ |
| 406 | (e = rd_uint(&r, 2, &x)) || (e = rd_zeros(&r, 6))) /* :291-292 */ |
| 407 | return fail(why, e, r.why); |
| 408 | if (x > CC_MAX_WAL_SEGMENTS) /* :293-295 */ |
| 409 | return fail(why, CC_E_CORRUPT, "too many cortex WAL segments"); |
| 410 | v->n_wal = (uint32_t)x; |
| 411 | for (uint32_t i = 0; i < v->n_wal; i++) |
| 412 | if ((e = rd_id(&r, v->wal[i]))) /* :297-299 */ |
| 413 | return fail(why, e, r.why); |
| 414 | if ((e = rd_finish(&r))) /* :300 */ |
| 415 | return fail(why, e, r.why); |
| 416 | /* :301-305: sequence 0, or "sequence 1" differs from "no previous". */ |
| 417 | if (v->sequence == 0 || (v->sequence == 1) != is_zero(v->previous, 32)) |
| 418 | return fail(why, CC_E_CORRUPT, "manifest sequence/previous mismatch"); |
| 419 | return CC_OK; |
| 420 | } |
| 421 | |
| 422 | /* ---- AgentState branch table (continuity.rs:342-482) ---- */ |
| 423 | /* binary_search_by on the sorted ids (continuity.rs:358-362): index of id, or |
| 424 | * -1 with *ins = insertion point. */ |
| 425 | static long find_branch(const struct cc_state *s, const uint8_t id[32], uint32_t *ins) |
| 426 | { |
| 427 | uint32_t lo = 0, hi = s->n; |
| 428 | while (lo < hi) { |
| 429 | uint32_t mid = lo + (hi - lo) / 2; |
| 430 | int c = cmp32(s->br[mid].id, id); |
| 431 | if (c == 0) |
| 432 | return (long)mid; |
| 433 | if (c < 0) |
| 434 | lo = mid + 1; |
| 435 | else |
| 436 | hi = mid; |
| 437 | } |
| 438 | if (ins) |
| 439 | *ins = lo; |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | void cc_state_genesis(struct cc_state *s, const uint8_t agent[32], uint64_t written_at) |
| 444 | { |
| 445 | cp(s->agent_id, agent, 32); |
| 446 | s->written_at = written_at; |
| 447 | s->n = 1; |
| 448 | cc_root_branch_id(agent, s->br[0].id); |
| 449 | for (int i = 0; i < 32; i++) |
| 450 | s->br[0].parent[i] = 0; |
| 451 | s->br[0].depth = 0; |
| 452 | s->br[0].forks = 0; |
| 453 | } |
| 454 | |
| 455 | int cc_state_validate(const struct cc_state *s, const char **why) |
| 456 | { |
| 457 | const char *bad = 0; |
| 458 | if (!s) |
| 459 | return fail(why, CC_E_ARG, "null argument"); |
| 460 | if (s->n == 0 || s->n > CC_MAX_BRANCHES) /* :449-451 */ |
| 461 | return fail(why, CC_E_CORRUPT, "branch count out of range"); |
| 462 | for (uint32_t i = 0; i + 1 < s->n; i++) /* :452-454 */ |
| 463 | if (cmp32(s->br[i].id, s->br[i + 1].id) >= 0) |
| 464 | return fail(why, CC_E_CORRUPT, "branches not strictly sorted"); |
| 465 | uint8_t root[32], cid[32]; |
| 466 | cc_root_branch_id(s->agent_id, root); /* :455 */ |
| 467 | uint32_t roots = 0; |
| 468 | uint64_t children = 0; |
| 469 | for (uint32_t i = 0; i < s->n && !bad; i++) { |
| 470 | const struct cc_branch *b = &s->br[i]; |
| 471 | if (is_zero(b->parent, 32)) { /* :460-465 */ |
| 472 | if (!eq(b->id, root, 32) || b->depth != 0) |
| 473 | bad = "unexpected root branch"; |
| 474 | roots++; |
| 475 | continue; |
| 476 | } |
| 477 | long pi = find_branch(s, b->parent, 0); /* :467 */ |
| 478 | if (pi < 0) { |
| 479 | bad = "parent branch is absent"; |
| 480 | break; |
| 481 | } |
| 482 | const struct cc_branch *p = &s->br[pi]; |
| 483 | /* :468: any index in 0..parent.forks derives this id. The scan is |
| 484 | * capped at CC_MAX_BRANCHES: a table that passes the fork-sum check |
| 485 | * below has every forks <= 255, so the cap changes no accept/refuse |
| 486 | * outcome, only bounds the work (Rust scans up to forks, which an |
| 487 | * input controls; see the PR's oracle notes). */ |
| 488 | uint64_t lim = p->forks < CC_MAX_BRANCHES ? p->forks : CC_MAX_BRANCHES; |
| 489 | int index_ok = 0; |
| 490 | for (uint64_t k = 0; k < lim && !index_ok; k++) { |
| 491 | cc_child_branch_id(b->parent, k, cid); |
| 492 | index_ok = eq(cid, b->id, 32); |
| 493 | } |
| 494 | if (!index_ok || p->depth == UINT32_MAX || b->depth != p->depth + 1) /* :469-471 */ |
| 495 | bad = "branch lineage is inconsistent"; |
| 496 | children++; |
| 497 | } |
| 498 | if (bad) |
| 499 | return fail(why, CC_E_CORRUPT, bad); |
| 500 | /* :476-479: sum of forks == number of children, exactly one root. The |
| 501 | * sum is checked: an overflowing sum can never equal children (<= 255). */ |
| 502 | uint64_t forks = 0; |
| 503 | int over = 0; |
| 504 | for (uint32_t i = 0; i < s->n; i++) { |
| 505 | #ifdef CC_MUTANT_UNCHECKED_FORK_SUM |
| 506 | forks += s->br[i].forks; /* MUTANT: wrapping sum, overflow never refused */ |
| 507 | #else |
| 508 | if (s->br[i].forks > UINT64_MAX - forks) |
| 509 | over = 1; |
| 510 | else |
| 511 | forks += s->br[i].forks; |
| 512 | #endif |
| 513 | } |
| 514 | if (roots != 1 || over || forks != children) |
| 515 | return fail(why, CC_E_CORRUPT, "fork indexes are not contiguous"); |
| 516 | return CC_OK; |
| 517 | } |
| 518 | |
| 519 | int cc_state_fork(struct cc_state *s, const uint8_t parent[32], uint8_t child_out[32], |
| 520 | const char **why) |
| 521 | { |
| 522 | if (!s || !parent || !child_out) |
| 523 | return fail(why, CC_E_ARG, "null argument"); |
| 524 | if (s->n >= CC_MAX_BRANCHES) /* :366-368 */ |
| 525 | return fail(why, CC_E_LIMIT, "too many branches"); |
| 526 | long i = find_branch(s, parent, 0); /* :369-372 */ |
| 527 | if (i < 0) |
| 528 | return fail(why, CC_E_CORRUPT, "fork parent is absent"); |
| 529 | uint64_t index = s->br[i].forks; /* :373 */ |
| 530 | uint32_t depth = s->br[i].depth; |
| 531 | struct cc_branch c; |
| 532 | cc_child_branch_id(parent, index, c.id); /* :375 */ |
| 533 | cp(c.parent, parent, 32); |
| 534 | if (depth == UINT32_MAX) /* :377-379 */ |
| 535 | return fail(why, CC_E_LIMIT, "branch depth"); |
| 536 | c.depth = depth + 1; |
| 537 | c.forks = 0; |
| 538 | /* :382: the parent's fork count advances before the collision check, as |
| 539 | * in Rust (a collision leaves it advanced and returns Corrupt). */ |
| 540 | s->br[i].forks = index + 1; |
| 541 | uint32_t at = 0; |
| 542 | if (find_branch(s, c.id, &at) >= 0) /* :383-387 */ |
| 543 | return fail(why, CC_E_CORRUPT, "child branch id collision"); |
| 544 | for (uint32_t k = s->n; k > at; k--) /* :388 insert keeps order */ |
| 545 | s->br[k] = s->br[k - 1]; |
| 546 | s->br[at] = c; |
| 547 | s->n++; |
| 548 | cp(child_out, c.id, 32); |
| 549 | return CC_OK; |
| 550 | } |
| 551 | |
| 552 | int cc_state_encode(const struct cc_state *v, uint8_t *out, size_t cap, size_t *len, |
| 553 | const char **why) |
| 554 | { |
| 555 | struct wr w; |
| 556 | int e; |
| 557 | if (!v || !len) |
| 558 | return fail(why, CC_E_ARG, "null argument"); |
| 559 | *len = 0; |
| 560 | if ((e = cc_state_validate(v, why))) /* :393 */ |
| 561 | return e; |
| 562 | size_t body = STATE_FIXED + (size_t)CC_BRANCH_BYTES * v->n; /* :394 */ |
| 563 | if ((e = enc_begin(&w, out, cap, CC_HEADER_BYTES + body, why))) |
| 564 | return e; |
| 565 | w_header(&w, MAGIC_BRANCHES, body); /* :396 */ |
| 566 | w_bytes(&w, v->agent_id, 32); /* :397 */ |
| 567 | w_u64(&w, v->written_at); /* :398 */ |
| 568 | w_u32(&w, v->n); /* :399 */ |
| 569 | w_zeros(&w, 4); /* :400 */ |
| 570 | for (uint32_t i = 0; i < v->n; i++) { |
| 571 | w_bytes(&w, v->br[i].id, 32); /* :402 */ |
| 572 | w_bytes(&w, v->br[i].parent, 32); /* :403 */ |
| 573 | w_u32(&w, v->br[i].depth); /* :404 */ |
| 574 | w_zeros(&w, 4); /* :405 */ |
| 575 | w_u64(&w, v->br[i].forks); /* :406 */ |
| 576 | } |
| 577 | *len = w.at; |
| 578 | return CC_OK; |
| 579 | } |
| 580 | |
| 581 | int cc_state_decode(const uint8_t *in, size_t len, struct cc_state *v, const char **why) |
| 582 | { |
| 583 | struct rd r; |
| 584 | uint64_t x = 0; |
| 585 | int e; |
| 586 | if (!in || !v) |
| 587 | return fail(why, CC_E_ARG, "null argument"); |
| 588 | if ((e = rd_open(&r, in, len, MAGIC_BRANCHES)) || (e = rd_id(&r, v->agent_id)) || /* :412-413 */ |
| 589 | (e = rd_uint(&r, 8, &v->written_at)) || (e = rd_uint(&r, 4, &x)) || /* :414-415 */ |
| 590 | (e = rd_zeros(&r, 4))) /* :416 */ |
| 591 | return fail(why, e, r.why); |
| 592 | if (x == 0 || x > CC_MAX_BRANCHES) /* :417-419 */ |
| 593 | return fail(why, CC_E_CORRUPT, "branch count out of range"); |
| 594 | v->n = (uint32_t)x; |
| 595 | for (uint32_t i = 0; i < v->n; i++) { |
| 596 | struct cc_branch *b = &v->br[i]; |
| 597 | uint64_t d = 0; |
| 598 | if ((e = rd_id(&r, b->id)) || (e = rd_id(&r, b->parent)) || /* :422-423 */ |
| 599 | (e = rd_uint(&r, 4, &d)) || (e = rd_zeros(&r, 4)) || /* :424-425 */ |
| 600 | (e = rd_uint(&r, 8, &b->forks))) /* :426 */ |
| 601 | return fail(why, e, r.why); |
| 602 | b->depth = (uint32_t)d; |
| 603 | } |
| 604 | if ((e = rd_finish(&r))) /* :434 */ |
| 605 | return fail(why, e, r.why); |
| 606 | return cc_state_validate(v, why); /* :440 */ |
| 607 | } |
| 608 | |
| 609 | /* ---- CortexWalSegment (continuity.rs:527-582) ---- */ |
| 610 | int cc_wal_encode(const struct cc_wal *v, uint8_t *out, size_t cap, size_t *len, |
| 611 | const char **why) |
| 612 | { |
| 613 | struct wr w; |
| 614 | int e; |
| 615 | if (!v || !len) |
| 616 | return fail(why, CC_E_ARG, "null argument"); |
| 617 | *len = 0; |
| 618 | if (v->n == 0 || v->n > CC_MAX_WAL_RECORDS) /* :529-531 */ |
| 619 | return fail(why, CC_E_LIMIT, "WAL segment record count"); |
| 620 | size_t body = WAL_FIXED; /* :532 */ |
| 621 | for (uint32_t i = 0; i < v->n; i++) { |
| 622 | const struct cc_record *rec = &v->rec[i]; |
| 623 | if (rec->len == 0 || rec->len > CC_MAX_STATEMENT_BYTES) /* :534-536 */ |
| 624 | return fail(why, CC_E_LIMIT, "statement length"); |
| 625 | /* Epistemic is a Rust enum (:490-511): other values are a caller bug. */ |
| 626 | if (rec->status < CC_EPI_DIRECT_OBSERVATION || rec->status > CC_EPI_OPERATOR_DECISION || |
| 627 | !rec->statement) |
| 628 | return fail(why, CC_E_ARG, "bad record"); |
| 629 | body += CC_WAL_RECORD_FIXED + rec->len; /* :537 */ |
| 630 | } |
| 631 | uint32_t n = v->n; |
| 632 | #ifdef CC_MUTANT_TRUNCATE_AT_CAP |
| 633 | /* MUTANT MC-11: silently drop records to fit the K-1 cap. */ |
| 634 | while (n > 1 && CC_HEADER_BYTES + body > CC_MAX_OBJECT_BYTES) { |
| 635 | n--; |
| 636 | body -= CC_WAL_RECORD_FIXED + v->rec[n].len; |
| 637 | } |
| 638 | #endif |
| 639 | if ((e = enc_begin(&w, out, cap, CC_HEADER_BYTES + body, why))) |
| 640 | return e; |
| 641 | w_header(&w, MAGIC_WAL, body); /* :540 */ |
| 642 | w_u64(&w, v->sequence); /* :541 */ |
| 643 | w_u32(&w, n); /* :542 */ |
| 644 | w_zeros(&w, 4); /* :543 */ |
| 645 | for (uint32_t i = 0; i < n; i++) { |
| 646 | const struct cc_record *rec = &v->rec[i]; |
| 647 | w_u8(&w, rec->status); /* :545 */ |
| 648 | w_u8(&w, 0); /* :546 */ |
| 649 | w_u16(&w, rec->len); /* :547 */ |
| 650 | w_bytes(&w, rec->evidence_hash, 32); /* :548 */ |
| 651 | w_bytes(&w, rec->statement, rec->len); /* :549 */ |
| 652 | } |
| 653 | *len = w.at; |
| 654 | return CC_OK; |
| 655 | } |
| 656 | |
| 657 | int cc_wal_decode(const uint8_t *in, size_t len, struct cc_wal *v, const char **why) |
| 658 | { |
| 659 | struct rd r; |
| 660 | uint64_t x = 0; |
| 661 | int e; |
| 662 | if (!in || !v) |
| 663 | return fail(why, CC_E_ARG, "null argument"); |
| 664 | if ((e = rd_open(&r, in, len, MAGIC_WAL)) || (e = rd_uint(&r, 8, &v->sequence)) || /* :555-556 */ |
| 665 | (e = rd_uint(&r, 4, &x)) || (e = rd_zeros(&r, 4))) /* :557-558 */ |
| 666 | return fail(why, e, r.why); |
| 667 | if (x == 0 || x > CC_MAX_WAL_RECORDS) /* :559-561 */ |
| 668 | return fail(why, CC_E_CORRUPT, "WAL segment record count"); |
| 669 | v->n = (uint32_t)x; |
| 670 | for (uint32_t i = 0; i < v->n; i++) { |
| 671 | struct cc_record *rec = &v->rec[i]; |
| 672 | uint64_t st = 0, l = 0; |
| 673 | const uint8_t *p = 0; |
| 674 | if ((e = rd_uint(&r, 1, &st))) /* :564 */ |
| 675 | return fail(why, e, r.why); |
| 676 | if (st < CC_EPI_DIRECT_OBSERVATION || st > CC_EPI_OPERATOR_DECISION) /* :564-565 */ |
| 677 | return fail(why, CC_E_CORRUPT, "unknown epistemic status"); |
| 678 | if ((e = rd_zeros(&r, 1)) || (e = rd_uint(&r, 2, &l))) /* :566-567 */ |
| 679 | return fail(why, e, r.why); |
| 680 | if (l == 0 || l > CC_MAX_STATEMENT_BYTES) /* :568-570 */ |
| 681 | return fail(why, CC_E_CORRUPT, "statement length"); |
| 682 | if ((e = rd_id(&r, rec->evidence_hash)) || (e = take(&r, (size_t)l, &p))) /* :571-572 */ |
| 683 | return fail(why, e, r.why); |
| 684 | rec->status = (uint8_t)st; |
| 685 | rec->len = (uint16_t)l; |
| 686 | rec->statement = p; |
| 687 | } |
| 688 | if ((e = rd_finish(&r))) /* :579 */ |
| 689 | return fail(why, e, r.why); |
| 690 | return CC_OK; |
| 691 | } |