File size: 9,162 Bytes
224e773 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | // HyperKitty C-- Kernel
// Authority: Route decision validation + state commitment
// Size target: ~250 lines substantive
// Role: Final gate before state mutation
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "hyperkitty_bus.h"
// RouteDecision ABI (must match Rust RouteDecision)
#define ROUTE_DECISION_ABI_VERSION 1
#define ROUTE_DECISION_SIZE 88
typedef struct {
uint32_t abi_version;
uint8_t current_state;
uint8_t previous_state;
uint8_t next_state;
uint8_t padding_0;
uint32_t accepted;
uint32_t failed_gate;
double entropy;
uint32_t entropy_ok;
uint32_t padding_1;
uint32_t reconciliation_ok;
uint32_t route_valid;
uint32_t invariant_preserved;
uint64_t trace_id;
uint32_t proof_ok;
uint32_t padding_2;
} route_decision_t;
// Kernel state
typedef struct {
uint8_t committed_state;
uint64_t committed_at;
double last_entropy;
uint64_t acceptance_count;
uint64_t rejection_count;
} kernel_state_t;
static kernel_state_t kernel = {
.committed_state = 0xFF, // Lambda (init)
.committed_at = 0,
.last_entropy = 0.0,
.acceptance_count = 0,
.rejection_count = 0
};
// Validation gates
typedef enum {
GATE_HEADER = 1,
GATE_GLYPHS = 2,
GATE_ENTROPY = 3,
GATE_VALIDITY = 4,
GATE_CONSISTENCY = 5,
} validation_gate_t;
// Trace record for WORM
typedef struct {
uint64_t trace_id;
uint8_t decision; // 0=rejected, 1=accepted
uint8_t failed_gate; // gate code
double entropy;
uint8_t previous;
uint8_t current;
uint8_t next;
} trace_record_t;
// Forward declarations
static bool validate_header(const route_decision_t *d);
static bool validate_glyphs(const route_decision_t *d);
static bool validate_entropy(const route_decision_t *d);
static bool validate_consistency(const route_decision_t *d);
static void emit_trace(const route_decision_t *d, bool accepted, validation_gate_t gate);
static void commit_state(const route_decision_t *d);
// ============================================================
// Validation: Deterministic checks with fail-closed semantics
// ============================================================
static bool validate_header(const route_decision_t *d) {
if (d->abi_version != ROUTE_DECISION_ABI_VERSION) {
return false;
}
return true;
}
static bool validate_glyphs(const route_decision_t *d) {
// Valid glyph indices: 0-5
if (d->current_state > 5) return false;
if (d->previous_state > 5) return false;
if (d->next_state > 5) return false;
return true;
}
static bool validate_entropy(const route_decision_t *d) {
// Check finiteness
if (!(d->entropy >= 0.0 && d->entropy <= 0.20)) {
return false;
}
// Check for NaN (NaN != NaN)
if (d->entropy != d->entropy) {
return false;
}
return true;
}
static bool validate_consistency(const route_decision_t *d) {
// If accepted, all gates must be OK
if (d->accepted == 1) {
if (d->entropy_ok == 0) return false;
if (d->reconciliation_ok == 0) return false;
if (d->route_valid == 0) return false;
if (d->proof_ok == 0) return false;
}
// If rejected, failed_gate must be non-zero
if (d->accepted == 0 && d->failed_gate == 0) {
return false;
}
return true;
}
// ============================================================
// Trace and Receipt
// ============================================================
static void emit_trace(const route_decision_t *d, bool accepted, validation_gate_t gate) {
// In production, write to WORM append-only log
// For now, struct definition for sealed receipt format
trace_record_t tr = {
.trace_id = d->trace_id,
.decision = accepted ? 1 : 0,
.failed_gate = gate,
.entropy = d->entropy,
.previous = d->previous_state,
.current = d->current_state,
.next = d->next_state,
};
(void)tr; // Placeholder: in production, write tr to WORM
}
static void commit_state(const route_decision_t *d) {
kernel.committed_state = d->next_state;
kernel.committed_at = kernel.committed_at + 1;
kernel.last_entropy = d->entropy;
kernel.acceptance_count = kernel.acceptance_count + 1;
}
// ============================================================
// Main kernel entry point
// ============================================================
typedef struct {
int32_t status; // 0=accept, <0=reject
validation_gate_t gate;
uint64_t trace_id;
} kernel_result_t;
kernel_result_t hk_kernel_decide(const route_decision_t *d) {
kernel_result_t result = {0, GATE_HEADER, 0};
if (!d) {
result.status = -1;
result.gate = GATE_HEADER;
return result;
}
result.trace_id = d->trace_id;
// GATE 1: ABI Header
if (!validate_header(d)) {
result.status = -1;
result.gate = GATE_HEADER;
emit_trace(d, false, GATE_HEADER);
kernel.rejection_count = kernel.rejection_count + 1;
return result;
}
// GATE 2: Glyph Validity
if (!validate_glyphs(d)) {
result.status = -2;
result.gate = GATE_GLYPHS;
emit_trace(d, false, GATE_GLYPHS);
kernel.rejection_count = kernel.rejection_count + 1;
return result;
}
// GATE 3: Entropy Bounds
if (!validate_entropy(d)) {
result.status = -3;
result.gate = GATE_ENTROPY;
emit_trace(d, false, GATE_ENTROPY);
kernel.rejection_count = kernel.rejection_count + 1;
return result;
}
// GATE 4: Validity Status
if (d->accepted == 0) {
result.status = -4;
result.gate = GATE_VALIDITY;
emit_trace(d, false, GATE_VALIDITY);
kernel.rejection_count = kernel.rejection_count + 1;
return result;
}
// GATE 5: Consistency (all OK flags must align)
if (!validate_consistency(d)) {
result.status = -5;
result.gate = GATE_CONSISTENCY;
emit_trace(d, false, GATE_CONSISTENCY);
kernel.rejection_count = kernel.rejection_count + 1;
return result;
}
// All gates pass: commit
commit_state(d);
emit_trace(d, true, 0);
result.status = 0;
result.gate = 0;
return result;
}
// ============================================================
// Query interface (no mutation)
// ============================================================
uint8_t hk_kernel_get_state(void) {
return kernel.committed_state;
}
uint64_t hk_kernel_get_acceptance_count(void) {
return kernel.acceptance_count;
}
uint64_t hk_kernel_get_rejection_count(void) {
return kernel.rejection_count;
}
double hk_kernel_get_last_entropy(void) {
return kernel.last_entropy;
}
// ============================================================
// Tests (if compiled with -DHK_ENABLE_TESTS)
// ============================================================
#ifdef HK_ENABLE_TESTS
#include <stdio.h>
#include <assert.h>
static route_decision_t make_valid_decision(void) {
route_decision_t d = {0};
d.abi_version = ROUTE_DECISION_ABI_VERSION;
d.current_state = 0; // Pi
d.previous_state = 1; // Gamma
d.next_state = 2; // Delta
d.entropy = 0.15;
d.accepted = 1;
d.entropy_ok = 1;
d.reconciliation_ok = 1;
d.route_valid = 1;
d.invariant_preserved = 1;
d.proof_ok = 1;
d.trace_id = 42;
return d;
}
void hk_kernel_test_valid_decision(void) {
route_decision_t d = make_valid_decision();
kernel_result_t r = hk_kernel_decide(&d);
assert(r.status == 0);
assert(hk_kernel_get_acceptance_count() == 1);
printf("✓ test_valid_decision\n");
}
void hk_kernel_test_bad_abi(void) {
route_decision_t d = make_valid_decision();
d.abi_version = 999;
kernel_result_t r = hk_kernel_decide(&d);
assert(r.status == -1);
assert(hk_kernel_get_rejection_count() >= 1);
printf("✓ test_bad_abi\n");
}
void hk_kernel_test_high_entropy(void) {
route_decision_t d = make_valid_decision();
d.entropy = 0.25; // Exceeds MAX_ENTROPY (0.20)
kernel_result_t r = hk_kernel_decide(&d);
assert(r.status == -3);
printf("✓ test_high_entropy\n");
}
void hk_kernel_test_rejected_decision(void) {
route_decision_t d = make_valid_decision();
d.accepted = 0;
d.failed_gate = 4; // Proof gate
kernel_result_t r = hk_kernel_decide(&d);
assert(r.status == -4);
printf("✓ test_rejected_decision\n");
}
void hk_kernel_test_consistency_fail(void) {
route_decision_t d = make_valid_decision();
d.accepted = 1;
d.entropy_ok = 0; // Contradiction!
kernel_result_t r = hk_kernel_decide(&d);
assert(r.status == -5);
printf("✓ test_consistency_fail\n");
}
void hk_kernel_run_tests(void) {
printf("=== HyperKitty C-- Kernel Tests ===\n");
hk_kernel_test_valid_decision();
hk_kernel_test_bad_abi();
hk_kernel_test_high_entropy();
hk_kernel_test_rejected_decision();
hk_kernel_test_consistency_fail();
printf("=== All tests passed ===\n");
}
#endif // HK_ENABLE_TESTS
|