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.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 | /* continuity_codec.h -- C port, cut 1: canonical encodings of the four M4 |
| 18 | * continuity objects (AgentRoot, ContinuityManifest, AgentState branch table, |
| 19 | * CortexWalSegment), the branch-id derivations and the logical ObjectId. |
| 20 | * |
| 21 | * Contract: native/kernel/CONTINUITY_RECOVERY_CONTRACT.md (aienos#222, |
| 22 | * SPEC/PROPOSED), sections 1.1-1.7 and INV-1..INV-3. Oracle: the Rust kernel, |
| 23 | * crates/aienos-kernel/src/continuity.rs at aienos 4a116e5 ("continuity.rs" |
| 24 | * below). Every layout here cites the Rust line it must match byte for byte. |
| 25 | * |
| 26 | * Properties: |
| 27 | * - No allocation, no globals, no libc: caller-owned structs and buffers. |
| 28 | * Needs only native/argus/sha256.{c,h}. |
| 29 | * - No dependence on the Store. The logical ObjectId is computed here with the |
| 30 | * Store v1 rule (crates/aienos-kernel/src/store/v1.rs:27, :63-78); the host |
| 31 | * test cross-checks it against sv1_object_id (native/store/store_v1.h:132). |
| 32 | * - Strict decoders (continuity.rs:97-166): every read bounds checked |
| 33 | * ("truncated object"), reserved bytes zero ("nonzero reserved"), the whole |
| 34 | * input consumed ("trailing bytes"). The error class and the Rust "why" text |
| 35 | * are both returned. |
| 36 | * - K-1 (contract 5.3, PROPOSED): no continuity object larger than |
| 37 | * CC_MAX_OBJECT_BYTES (16384, SS_MAX_PLAINTEXT, native/store/store_sealed.h:46) |
| 38 | * is encoded or decoded; both refuse with CC_E_LIMIT, never truncate. |
| 39 | * |
| 40 | * QEMU and hardware: nothing here is wired into the kernel yet (cut 1). */ |
| 41 | #ifndef CK_CONTINUITY_CODEC_H |
| 42 | #define CK_CONTINUITY_CODEC_H |
| 43 | |
| 44 | #include <stddef.h> |
| 45 | #include <stdint.h> |
| 46 | |
| 47 | /* TEST-only mutants for the host mutation run (Makefile continuity-mutants). |
| 48 | * A mutant build exists only to prove the host test fails; it must never be |
| 49 | * part of a hardware staging image. */ |
| 50 | #if defined(CK_HARDWARE_STAGING) && \ |
| 51 | (defined(CC_MUTANT_SKIP_TAKE_BOUND) || defined(CC_MUTANT_ACCEPT_RESERVED) || \ |
| 52 | defined(CC_MUTANT_CHILD_INDEX_LE) || defined(CC_MUTANT_TRUNCATE_AT_CAP) || \ |
| 53 | defined(CC_MUTANT_UNCHECKED_FORK_SUM) || defined(CC_MUTANT_MANIFEST_LAST_WAL_ZERO)) |
| 54 | #error "continuity codec mutants are TEST-only and refused under CK_HARDWARE_STAGING" |
| 55 | #endif |
| 56 | |
| 57 | /* Store kinds (continuity.rs:19-22) and versions (:24, :26). */ |
| 58 | #define CC_KIND_AGENT_ROOT 16u |
| 59 | #define CC_KIND_MANIFEST 17u |
| 60 | #define CC_KIND_AGENT_STATE 18u |
| 61 | #define CC_KIND_CORTEX_WAL 20u |
| 62 | #define CC_STORE_OBJECT_VERSION 1u |
| 63 | #define CC_FORMAT_VERSION 0u |
| 64 | |
| 65 | /* Rust bounds (continuity.rs:28-31). */ |
| 66 | #define CC_MAX_WAL_SEGMENTS 64u |
| 67 | #define CC_MAX_BRANCHES 256u |
| 68 | #define CC_MAX_WAL_RECORDS 64u |
| 69 | #define CC_MAX_STATEMENT_BYTES 1024u |
| 70 | |
| 71 | #define CC_HEADER_BYTES 16u /* continuity.rs:33 */ |
| 72 | #define CC_ROOT_BYTES (CC_HEADER_BYTES + 96u) /* ROOT_BODY, continuity.rs:202 */ |
| 73 | #define CC_BRANCH_BYTES 80u /* BRANCH_BYTES, continuity.rs:340 */ |
| 74 | #define CC_WAL_RECORD_FIXED 36u /* status, reserved, len, hash: :545-548 */ |
| 75 | /* K-1 (PROPOSED): the sealed Store's plaintext cap. */ |
| 76 | #define CC_MAX_OBJECT_BYTES 16384u |
| 77 | /* Largest branch table that fits K-1: (16384 - 64) / 80 = 204. */ |
| 78 | #define CC_MAX_BRANCHES_IN_CAP ((CC_MAX_OBJECT_BYTES - CC_HEADER_BYTES - 48u) / CC_BRANCH_BYTES) |
| 79 | |
| 80 | /* Error classes (ContinuityError, continuity.rs:40-55; only those a codec |
| 81 | * can return). CC_E_ARG is C-only: a caller bug (NULL, output buffer too |
| 82 | * small, a value Rust's types cannot hold); nothing is written. */ |
| 83 | enum { |
| 84 | CC_OK = 0, |
| 85 | CC_E_CORRUPT = 1, /* ContinuityError::Corrupt(why) */ |
| 86 | CC_E_LIMIT = 2, /* ContinuityError::Limit(why) */ |
| 87 | CC_E_ARG = 3, |
| 88 | }; |
| 89 | |
| 90 | /* ProvisionSource (continuity.rs:186-191). */ |
| 91 | #define CC_SOURCE_OPERATOR 1u |
| 92 | #define CC_SOURCE_QUALIFICATION 2u |
| 93 | |
| 94 | /* Epistemic (continuity.rs:490-511). */ |
| 95 | #define CC_EPI_DIRECT_OBSERVATION 1u |
| 96 | #define CC_EPI_OPERATOR_DECISION 6u |
| 97 | |
| 98 | /* An absent optional id (Option<ObjectId> None, parent None) is all zero, |
| 99 | * exactly as Rust encodes it (continuity.rs:38, :173-179, :403). */ |
| 100 | |
| 101 | struct cc_root { /* AgentRoot, continuity.rs:194-200 */ |
| 102 | uint8_t agent_id[32]; |
| 103 | uint8_t store_uuid[16]; |
| 104 | uint8_t root_branch[32]; |
| 105 | uint64_t provisioned_generation; |
| 106 | uint8_t source; /* CC_SOURCE_* */ |
| 107 | }; |
| 108 | |
| 109 | struct cc_manifest { /* Manifest, continuity.rs:254-261 */ |
| 110 | uint8_t root[32]; |
| 111 | uint8_t previous[32]; /* zero = none */ |
| 112 | uint64_t sequence; |
| 113 | uint64_t incarnation; |
| 114 | uint8_t agent_state[32]; /* zero = none */ |
| 115 | uint32_t n_wal; /* <= CC_MAX_WAL_SEGMENTS */ |
| 116 | uint8_t wal[CC_MAX_WAL_SEGMENTS][32]; |
| 117 | }; |
| 118 | |
| 119 | struct cc_branch { /* Branch, continuity.rs:322-329 */ |
| 120 | uint8_t id[32]; |
| 121 | uint8_t parent[32]; /* zero = root branch */ |
| 122 | uint32_t depth; |
| 123 | uint64_t forks; |
| 124 | }; |
| 125 | |
| 126 | struct cc_state { /* AgentState, continuity.rs:332-338; ~20 KiB, caller owned */ |
| 127 | uint8_t agent_id[32]; |
| 128 | uint64_t written_at; |
| 129 | uint32_t n; /* 1..CC_MAX_BRANCHES */ |
| 130 | struct cc_branch br[CC_MAX_BRANCHES]; /* strictly ascending by id */ |
| 131 | }; |
| 132 | |
| 133 | struct cc_record { /* CortexRecord, continuity.rs:514-518 */ |
| 134 | uint8_t status; /* CC_EPI_* 1..6 */ |
| 135 | uint8_t evidence_hash[32]; |
| 136 | uint16_t len; /* 1..CC_MAX_STATEMENT_BYTES */ |
| 137 | /* Decode: points into the decoded input buffer (no copy, no |
| 138 | * allocation); valid only while that buffer is. Encode: read only. */ |
| 139 | const uint8_t *statement; |
| 140 | }; |
| 141 | |
| 142 | struct cc_wal { /* WalSegment, continuity.rs:521-525 */ |
| 143 | uint64_t sequence; |
| 144 | uint32_t n; /* 1..CC_MAX_WAL_RECORDS */ |
| 145 | struct cc_record rec[CC_MAX_WAL_RECORDS]; |
| 146 | }; |
| 147 | |
| 148 | /* ---- derivations (contract 1.7, INV-3) ---- */ |
| 149 | /* SHA-256("AIENOS_ROOT_BRANCH_v1:" || agent), continuity.rs:69-75. */ |
| 150 | void cc_root_branch_id(const uint8_t agent[32], uint8_t out[32]); |
| 151 | /* SHA-256("AIENOS_CHILD_BRANCH_v1:" || parent || index u64 BIG-endian), |
| 152 | * continuity.rs:77-84. */ |
| 153 | void cc_child_branch_id(const uint8_t parent[32], uint64_t index, uint8_t out[32]); |
| 154 | /* Logical ObjectId = ObjectId::calculate(kind, 1, bytes), store/v1.rs:63-78 |
| 155 | * (contract P-2). CC_E_ARG for kind 0 or len 0 (v1.rs:64). */ |
| 156 | int cc_object_id(uint16_t kind, const uint8_t *bytes, size_t len, uint8_t out[32]); |
| 157 | |
| 158 | /* ---- codecs ---- |
| 159 | * Encoders write the canonical bytes into out[0..cap) and set *len. On any |
| 160 | * error nothing useful is in out and *len is 0; an object that would exceed |
| 161 | * CC_MAX_OBJECT_BYTES is CC_E_LIMIT (K-1), an out buffer smaller than the |
| 162 | * object is CC_E_ARG. Decoders write *v as they read; on error *v holds |
| 163 | * no meaningful value (no copy: AgentState is ~20 KiB). *why (may be NULL) |
| 164 | * gets the Rust reason text on CC_E_CORRUPT / CC_E_LIMIT. */ |
| 165 | int cc_root_encode(const struct cc_root *v, uint8_t *out, size_t cap, size_t *len, |
| 166 | const char **why); |
| 167 | int cc_root_decode(const uint8_t *in, size_t len, struct cc_root *v, const char **why); |
| 168 | |
| 169 | int cc_manifest_encode(const struct cc_manifest *v, uint8_t *out, size_t cap, size_t *len, |
| 170 | const char **why); |
| 171 | int cc_manifest_decode(const uint8_t *in, size_t len, struct cc_manifest *v, const char **why); |
| 172 | |
| 173 | int cc_state_encode(const struct cc_state *v, uint8_t *out, size_t cap, size_t *len, |
| 174 | const char **why); |
| 175 | int cc_state_decode(const uint8_t *in, size_t len, struct cc_state *v, const char **why); |
| 176 | |
| 177 | int cc_wal_encode(const struct cc_wal *v, uint8_t *out, size_t cap, size_t *len, |
| 178 | const char **why); |
| 179 | int cc_wal_decode(const uint8_t *in, size_t len, struct cc_wal *v, const char **why); |
| 180 | |
| 181 | /* ---- branch table (continuity.rs:342-482) ---- */ |
| 182 | /* AgentState::genesis: only the root branch (continuity.rs:344-355). */ |
| 183 | void cc_state_genesis(struct cc_state *s, const uint8_t agent[32], uint64_t written_at); |
| 184 | /* AgentState::validate (continuity.rs:447-481). */ |
| 185 | int cc_state_validate(const struct cc_state *s, const char **why); |
| 186 | /* AgentState::fork (continuity.rs:365-390): child id into child_out. */ |
| 187 | int cc_state_fork(struct cc_state *s, const uint8_t parent[32], uint8_t child_out[32], |
| 188 | const char **why); |
| 189 | |
| 190 | #endif /* CK_CONTINUITY_CODEC_H */ |