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 / estimation / est_types.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 | /* EST-0 implementation: validation, digests, fixed little-endian encoding. |
| 18 | * See est_types.h for the contract. No allocation, no globals, no I/O. |
| 19 | * |
| 20 | * Canonical encoding rules (all encoders): |
| 21 | * - Header, 8 bytes: magic "OEST", u8 kind (est_kind), u8 EST_FORMAT_VERSION, |
| 22 | * u16 reserved = 0. |
| 23 | * - Then the fields in struct order. u32, u64, i64 are little-endian. |
| 24 | * Enums are u32. A double is the little-endian IEEE-754 binary64 bit |
| 25 | * pattern. A digest is its 32 raw bytes. |
| 26 | * - Only the meaningful n / m entries of arrays are encoded (unit[n], |
| 27 | * x[n], P as n*n row-major, ...). Dimension fields precede the arrays so |
| 28 | * the total length is determined by the fixed part. Entries beyond n / m |
| 29 | * are not part of a record's identity. |
| 30 | * - NaN and Inf are refused (validation runs before encoding). -0.0 is |
| 31 | * canonicalized to +0.0 before encoding so equal values digest equally; |
| 32 | * therefore a decoded -0.0 comes back as +0.0. There are no other |
| 33 | * equal-but-different double bit patterns once NaN is excluded. |
| 34 | * - Digest = SHA-256(domain string bytes || 0x00 || encoding). */ |
| 35 | #include "est_types.h" |
| 36 | #include "omega_native.h" |
| 37 | #include "sha256.h" |
| 38 | |
| 39 | #include <math.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #define EST_TMP_MAX 2304u /* largest encoding: model with n = m = 8 is 2160 bytes */ |
| 43 | #define EST_HDR 8u |
| 44 | |
| 45 | /* ------------------------------------------------------------ validation */ |
| 46 | |
| 47 | static int in_dim(uint32_t d) { return d >= 1u && d <= EST_MAX_DIM; } |
| 48 | |
| 49 | static est_status check_finite(const double *a, size_t cnt) |
| 50 | { |
| 51 | int64_t rc = 0; |
| 52 | if (cnt == 0) return EST_OK; |
| 53 | if (omega2_est_om_check_finite(&rc, (const uint64_t *)a, cnt, cnt) != 0) |
| 54 | return EST_ERR_NONFINITE; |
| 55 | return (est_status)rc; |
| 56 | } |
| 57 | |
| 58 | static est_status check_units(const est_unit *u, uint32_t cnt) |
| 59 | { |
| 60 | int64_t rc = 0; |
| 61 | uint32_t tmp[EST_MAX_DIM]; |
| 62 | if (cnt == 0) return EST_OK; |
| 63 | if (cnt > EST_MAX_DIM) return EST_ERR_DIM; |
| 64 | for (uint32_t i = 0; i < cnt; i++) tmp[i] = (uint32_t)u[i]; |
| 65 | if (omega2_est_om_check_units(&rc, tmp, cnt, cnt) != 0) |
| 66 | return EST_ERR_UNIT; |
| 67 | return (est_status)rc; |
| 68 | } |
| 69 | |
| 70 | est_status est_check_covariance(const double *A, uint32_t n) |
| 71 | { |
| 72 | uint64_t work[72] = { 0 }, tols[4]; |
| 73 | int64_t rc = 0; |
| 74 | double d_tols[4] = { 1e-9, 1e-300, 1.0 + 1e-9, 1e-12 }; |
| 75 | if (!A) return EST_ERR_NULL; |
| 76 | if (!in_dim(n)) return EST_ERR_DIM; |
| 77 | memcpy(tols, d_tols, sizeof tols); |
| 78 | if (omega2_est_om_check_covariance(&rc, (const uint64_t *)A, (size_t)n * n, n, work, 72, tols, 4) != 0) |
| 79 | return EST_ERR_NOT_PSD; |
| 80 | return (est_status)rc; |
| 81 | } |
| 82 | |
| 83 | int est_digest_is_zero(const est_digest *d) |
| 84 | { |
| 85 | uint64_t out = 1; |
| 86 | if (!d) return 1; |
| 87 | (void)omega2_est_om_digest_is_zero(&out, d->b, EST_DIGEST_SIZE); |
| 88 | return (int)out; |
| 89 | } |
| 90 | |
| 91 | /* Strict Cholesky: true if the symmetric n*n matrix is positive definite. */ |
| 92 | static int is_pd(const double *A, uint32_t n) |
| 93 | { |
| 94 | uint64_t L[EST_MAX_DIM * EST_MAX_DIM]; |
| 95 | uint64_t ok = 0; |
| 96 | if (omega2_est_om_chol(&ok, (const uint64_t *)A, (size_t)n * n, L, EST_MAX_DIM * EST_MAX_DIM, n) != 0) |
| 97 | return 0; |
| 98 | return (int)ok; |
| 99 | } |
| 100 | |
| 101 | est_status est_check_model(const est_model *mdl) |
| 102 | { |
| 103 | if (!mdl) return EST_ERR_NULL; |
| 104 | if (!in_dim(mdl->n) || !in_dim(mdl->m)) return EST_ERR_DIM; |
| 105 | if (mdl->estimator != EST_ESTIMATOR_LINEAR_KALMAN) return EST_ERR_UNIT; |
| 106 | if (mdl->meaning != EST_UNCERTAINTY_GAUSSIAN_COVARIANCE) return EST_ERR_UNIT; |
| 107 | est_status st = check_units(mdl->state_unit, mdl->n); |
| 108 | if (st != EST_OK) return st; |
| 109 | st = check_units(mdl->obs_unit, mdl->m); |
| 110 | if (st != EST_OK) return st; |
| 111 | if (mdl->step_ns <= 0) return EST_ERR_TIME; |
| 112 | st = check_finite(mdl->F, (size_t)mdl->n * mdl->n); |
| 113 | if (st != EST_OK) return st; |
| 114 | st = check_finite(mdl->H, (size_t)mdl->m * mdl->n); |
| 115 | if (st != EST_OK) return st; |
| 116 | st = est_check_covariance(mdl->Q, mdl->n); |
| 117 | if (st != EST_OK) return st; |
| 118 | st = est_check_covariance(mdl->R, mdl->m); |
| 119 | if (st != EST_OK) return st; |
| 120 | /* Observation noise must be positive definite: R = 0 wedges every update. */ |
| 121 | return is_pd(mdl->R, mdl->m) ? EST_OK : EST_ERR_NOT_PD; |
| 122 | } |
| 123 | |
| 124 | est_status est_check_observation(const est_observation *o) |
| 125 | { |
| 126 | if (!o) return EST_ERR_NULL; |
| 127 | if (!in_dim(o->m)) return EST_ERR_DIM; |
| 128 | est_status st = check_units(o->unit, o->m); |
| 129 | if (st != EST_OK) return st; |
| 130 | st = check_finite(o->z, o->m); |
| 131 | if (st != EST_OK) return st; |
| 132 | st = est_check_covariance(o->R, o->m); |
| 133 | if (st != EST_OK) return st; |
| 134 | /* Evidence with nothing bound to it is not evidence, and an observation |
| 135 | * that names no producer is not attributable. */ |
| 136 | if (est_digest_is_zero(&o->evidence)) return EST_ERR_KIND; |
| 137 | if (est_digest_is_zero(&o->source)) return EST_ERR_KIND; |
| 138 | return EST_OK; |
| 139 | } |
| 140 | |
| 141 | est_status est_check_belief(const est_belief *b) |
| 142 | { |
| 143 | if (!b) return EST_ERR_NULL; |
| 144 | if (!in_dim(b->n)) return EST_ERR_DIM; |
| 145 | est_status st = check_units(b->unit, b->n); |
| 146 | if (st != EST_OK) return st; |
| 147 | st = check_finite(b->x, b->n); |
| 148 | if (st != EST_OK) return st; |
| 149 | /* Structure: a belief always names its model; generation 0 is a declared |
| 150 | * prior (no parent, no evidence); any later generation has a parent. The |
| 151 | * root may be zero after a coast from a prior, so it is not required. */ |
| 152 | if (est_digest_is_zero(&b->model)) return EST_ERR_MODEL; |
| 153 | if (b->generation == 0u) { |
| 154 | if (!est_digest_is_zero(&b->parent) || !est_digest_is_zero(&b->evidence_root)) return EST_ERR_STALE; |
| 155 | } else if (est_digest_is_zero(&b->parent)) { |
| 156 | return EST_ERR_STALE; |
| 157 | } |
| 158 | return est_check_covariance(b->P, b->n); |
| 159 | } |
| 160 | |
| 161 | est_status est_check_prediction(const est_prediction *p) |
| 162 | { |
| 163 | if (!p) return EST_ERR_NULL; |
| 164 | if (!in_dim(p->n) || !in_dim(p->m)) return EST_ERR_DIM; |
| 165 | if (p->horizon < 1u || p->horizon > EST_MAX_HORIZON) return EST_ERR_TIME; |
| 166 | if (p->has_control > 1u) return EST_ERR_ENCODING; |
| 167 | est_status st = check_finite(p->x, p->n); |
| 168 | if (st != EST_OK) return st; |
| 169 | if (p->has_control) { |
| 170 | st = check_finite(p->Bu, p->n); |
| 171 | if (st != EST_OK) return st; |
| 172 | } |
| 173 | st = check_finite(p->y_mean, p->m); |
| 174 | if (st != EST_OK) return st; |
| 175 | st = est_check_covariance(p->P, p->n); |
| 176 | if (st != EST_OK) return st; |
| 177 | return est_check_covariance(p->S, p->m); |
| 178 | } |
| 179 | |
| 180 | est_status est_check_innovation(const est_innovation *iv) |
| 181 | { |
| 182 | if (!iv) return EST_ERR_NULL; |
| 183 | if (!in_dim(iv->m)) return EST_ERR_DIM; |
| 184 | est_status st = check_finite(iv->nu, iv->m); |
| 185 | if (st != EST_OK) return st; |
| 186 | if (!isfinite(iv->nis)) return EST_ERR_NONFINITE; |
| 187 | if (iv->nis < 0.0) return EST_ERR_NOT_PSD; |
| 188 | return est_check_covariance(iv->S, iv->m); |
| 189 | } |
| 190 | |
| 191 | /* -------------------------------------------------------- byte plumbing */ |
| 192 | |
| 193 | typedef struct { uint8_t *p; size_t cap, len; int bad; } wr; |
| 194 | |
| 195 | static void w_raw(wr *w, const void *s, size_t n) |
| 196 | { |
| 197 | if (w->bad || n > w->cap - w->len) { w->bad = 1; return; } |
| 198 | memcpy(w->p + w->len, s, n); |
| 199 | w->len += n; |
| 200 | } |
| 201 | static void w_u32(wr *w, uint32_t v) |
| 202 | { |
| 203 | uint8_t t[4]; |
| 204 | uint64_t u = 0; |
| 205 | (void)omega2_est_om_put_u32(&u, t, 4, 0, v); |
| 206 | w_raw(w, t, 4); |
| 207 | } |
| 208 | static void w_u64(wr *w, uint64_t v) |
| 209 | { |
| 210 | uint8_t t[8]; |
| 211 | uint64_t u = 0; |
| 212 | (void)omega2_est_om_put_u64(&u, t, 8, 0, v); |
| 213 | w_raw(w, t, 8); |
| 214 | } |
| 215 | static void w_i64(wr *w, int64_t v) { w_u64(w, (uint64_t)v); } |
| 216 | static void w_f64(wr *w, double d) |
| 217 | { |
| 218 | uint8_t t[8]; |
| 219 | uint64_t bits = 0, u = 0; |
| 220 | memcpy(&bits, &d, 8); |
| 221 | (void)omega2_est_om_put_f64_canon(&u, t, 8, 0, bits); |
| 222 | w_raw(w, t, 8); |
| 223 | } |
| 224 | static void w_dig(wr *w, const est_digest *d) { w_raw(w, d->b, EST_DIGEST_SIZE); } |
| 225 | static void w_hdr(wr *w, est_kind k) |
| 226 | { |
| 227 | uint8_t t[8]; |
| 228 | uint64_t u = 0; |
| 229 | (void)omega2_est_om_write_hdr(&u, t, 8, (uint64_t)k); |
| 230 | w_raw(w, t, 8); |
| 231 | } |
| 232 | static void w_farr(wr *w, const double *a, size_t n) { for (size_t i = 0; i < n; i++) w_f64(w, a[i]); } |
| 233 | static void w_uarr(wr *w, const est_unit *u, uint32_t n) { for (uint32_t i = 0; i < n; i++) w_u32(w, (uint32_t)u[i]); } |
| 234 | |
| 235 | typedef struct { const uint8_t *p; size_t len, pos; est_status st; } rd; |
| 236 | |
| 237 | static void r_fail(rd *r, est_status s) { if (r->st == EST_OK) r->st = s; } |
| 238 | static int r_need(rd *r, size_t n) |
| 239 | { |
| 240 | if (r->st != EST_OK) return 0; |
| 241 | if (n > r->len - r->pos) { r_fail(r, EST_ERR_ENCODING); return 0; } |
| 242 | return 1; |
| 243 | } |
| 244 | static uint32_t r_u32(rd *r) |
| 245 | { |
| 246 | uint64_t v = 0; |
| 247 | if (!r_need(r, 4)) return 0; |
| 248 | (void)omega2_est_om_get_u32(&v, r->p + r->pos, 4, 0); |
| 249 | r->pos += 4; |
| 250 | return (uint32_t)v; |
| 251 | } |
| 252 | static uint64_t r_u64(rd *r) |
| 253 | { |
| 254 | uint64_t v = 0; |
| 255 | if (!r_need(r, 8)) return 0; |
| 256 | (void)omega2_est_om_get_u64(&v, r->p + r->pos, 8, 0); |
| 257 | r->pos += 8; |
| 258 | return v; |
| 259 | } |
| 260 | static int64_t r_i64(rd *r) { return (int64_t)r_u64(r); } |
| 261 | static double r_f64(rd *r) |
| 262 | { |
| 263 | uint64_t u = r_u64(r); |
| 264 | double d; |
| 265 | memcpy(&d, &u, 8); |
| 266 | return d; |
| 267 | } |
| 268 | static void r_dig(rd *r, est_digest *d) |
| 269 | { |
| 270 | if (!r_need(r, EST_DIGEST_SIZE)) return; |
| 271 | memcpy(d->b, r->p + r->pos, EST_DIGEST_SIZE); |
| 272 | r->pos += EST_DIGEST_SIZE; |
| 273 | } |
| 274 | static void r_farr(rd *r, double *a, size_t n) { for (size_t i = 0; i < n; i++) a[i] = r_f64(r); } |
| 275 | static void r_uarr(rd *r, est_unit *u, uint32_t n) |
| 276 | { |
| 277 | for (uint32_t i = 0; i < n; i++) { |
| 278 | uint32_t v = r_u32(r); |
| 279 | if (v >= (uint32_t)EST_UNIT_MAX_) { r_fail(r, EST_ERR_UNIT); v = 0; } |
| 280 | u[i] = (est_unit)v; |
| 281 | } |
| 282 | } |
| 283 | static uint32_t r_dim(rd *r) |
| 284 | { |
| 285 | uint32_t d = r_u32(r); |
| 286 | if (r->st == EST_OK && !in_dim(d)) { r_fail(r, EST_ERR_DIM); return 0; } |
| 287 | return d; |
| 288 | } |
| 289 | /* Header check. Wrong magic, version: ENCODING. Wrong kind: KIND. */ |
| 290 | static est_status r_hdr(rd *r, est_kind want) |
| 291 | { |
| 292 | int64_t rc = 0; |
| 293 | if (r->len < EST_HDR) return EST_ERR_ENCODING; |
| 294 | if (omega2_est_om_check_hdr(&rc, r->p, r->len, r->len, (uint64_t)want) != 0) |
| 295 | return EST_ERR_ENCODING; |
| 296 | if (rc != EST_OK) return (est_status)rc; |
| 297 | r->pos = EST_HDR; |
| 298 | return EST_OK; |
| 299 | } |
| 300 | static est_status r_done(rd *r) |
| 301 | { |
| 302 | if (r->st != EST_OK) return r->st; |
| 303 | return r->pos == r->len ? EST_OK : EST_ERR_ENCODING; |
| 304 | } |
| 305 | |
| 306 | /* ------------------------------------------------------------- encoders */ |
| 307 | |
| 308 | /* Model layout after the header: |
| 309 | * u32 n, u32 m, u32 estimator, u32 meaning, |
| 310 | * u32 state_unit[n], u32 obs_unit[m], |
| 311 | * f64 F[n*n], f64 Q[n*n], f64 H[m*n], f64 R[m*m] (row-major), i64 step_ns. */ |
| 312 | static est_status enc_model(const est_model *o, uint8_t *buf, size_t cap, size_t *len) |
| 313 | { |
| 314 | est_status st = est_check_model(o); |
| 315 | if (st != EST_OK) return st; |
| 316 | wr w = { buf, cap, 0, 0 }; |
| 317 | w_hdr(&w, EST_KIND_MODEL); |
| 318 | w_u32(&w, o->n); w_u32(&w, o->m); |
| 319 | w_u32(&w, (uint32_t)o->estimator); w_u32(&w, (uint32_t)o->meaning); |
| 320 | w_uarr(&w, o->state_unit, o->n); w_uarr(&w, o->obs_unit, o->m); |
| 321 | w_farr(&w, o->F, (size_t)o->n * o->n); w_farr(&w, o->Q, (size_t)o->n * o->n); |
| 322 | w_farr(&w, o->H, (size_t)o->m * o->n); w_farr(&w, o->R, (size_t)o->m * o->m); |
| 323 | w_i64(&w, o->step_ns); |
| 324 | if (w.bad) return EST_ERR_ENCODING; |
| 325 | *len = w.len; |
| 326 | return EST_OK; |
| 327 | } |
| 328 | |
| 329 | /* Observation layout after the header: |
| 330 | * u32 m, u32 unit[m], f64 z[m], f64 R[m*m], i64 t_ns, u64 seq, |
| 331 | * 32 bytes source, 32 bytes evidence. */ |
| 332 | static est_status enc_observation(const est_observation *o, uint8_t *buf, size_t cap, size_t *len) |
| 333 | { |
| 334 | est_status st = est_check_observation(o); |
| 335 | if (st != EST_OK) return st; |
| 336 | wr w = { buf, cap, 0, 0 }; |
| 337 | w_hdr(&w, EST_KIND_OBSERVATION); |
| 338 | w_u32(&w, o->m); w_uarr(&w, o->unit, o->m); |
| 339 | w_farr(&w, o->z, o->m); w_farr(&w, o->R, (size_t)o->m * o->m); |
| 340 | w_i64(&w, o->t_ns); w_u64(&w, o->seq); |
| 341 | w_dig(&w, &o->source); w_dig(&w, &o->evidence); |
| 342 | if (w.bad) return EST_ERR_ENCODING; |
| 343 | *len = w.len; |
| 344 | return EST_OK; |
| 345 | } |
| 346 | |
| 347 | /* Belief layout after the header: |
| 348 | * u32 n, u32 unit[n], f64 x[n], f64 P[n*n], u64 generation, i64 t_ns, |
| 349 | * 32 bytes model, 32 bytes parent, 32 bytes evidence_root. */ |
| 350 | static est_status enc_belief(const est_belief *o, uint8_t *buf, size_t cap, size_t *len) |
| 351 | { |
| 352 | est_status st = est_check_belief(o); |
| 353 | if (st != EST_OK) return st; |
| 354 | wr w = { buf, cap, 0, 0 }; |
| 355 | w_hdr(&w, EST_KIND_BELIEF); |
| 356 | w_u32(&w, o->n); w_uarr(&w, o->unit, o->n); |
| 357 | w_farr(&w, o->x, o->n); w_farr(&w, o->P, (size_t)o->n * o->n); |
| 358 | w_u64(&w, o->generation); w_i64(&w, o->t_ns); |
| 359 | w_dig(&w, &o->model); w_dig(&w, &o->parent); w_dig(&w, &o->evidence_root); |
| 360 | if (w.bad) return EST_ERR_ENCODING; |
| 361 | *len = w.len; |
| 362 | return EST_OK; |
| 363 | } |
| 364 | |
| 365 | /* Prediction layout after the header: |
| 366 | * 32 bytes prior, 32 bytes model, u32 horizon, u64 generation, i64 t_ns, |
| 367 | * u32 n, u32 m, f64 x[n], f64 P[n*n], f64 y_mean[m], f64 S[m*m], |
| 368 | * u32 has_control (0 or 1), then f64 Bu[n] only if has_control == 1. */ |
| 369 | static est_status enc_prediction(const est_prediction *o, uint8_t *buf, size_t cap, size_t *len) |
| 370 | { |
| 371 | est_status st = est_check_prediction(o); |
| 372 | if (st != EST_OK) return st; |
| 373 | wr w = { buf, cap, 0, 0 }; |
| 374 | w_hdr(&w, EST_KIND_PREDICTION); |
| 375 | w_dig(&w, &o->prior); w_dig(&w, &o->model); |
| 376 | w_u32(&w, o->horizon); w_u64(&w, o->generation); w_i64(&w, o->t_ns); |
| 377 | w_u32(&w, o->n); w_u32(&w, o->m); |
| 378 | w_farr(&w, o->x, o->n); w_farr(&w, o->P, (size_t)o->n * o->n); |
| 379 | w_farr(&w, o->y_mean, o->m); w_farr(&w, o->S, (size_t)o->m * o->m); |
| 380 | w_u32(&w, o->has_control); |
| 381 | if (o->has_control) w_farr(&w, o->Bu, o->n); |
| 382 | if (w.bad) return EST_ERR_ENCODING; |
| 383 | *len = w.len; |
| 384 | return EST_OK; |
| 385 | } |
| 386 | |
| 387 | /* Innovation layout after the header: |
| 388 | * 32 bytes prediction, 32 bytes observation, 32 bytes model, u32 m, |
| 389 | * f64 nu[m], f64 S[m*m], f64 nis, i64 t_ns. */ |
| 390 | static est_status enc_innovation(const est_innovation *o, uint8_t *buf, size_t cap, size_t *len) |
| 391 | { |
| 392 | est_status st = est_check_innovation(o); |
| 393 | if (st != EST_OK) return st; |
| 394 | wr w = { buf, cap, 0, 0 }; |
| 395 | w_hdr(&w, EST_KIND_INNOVATION); |
| 396 | w_dig(&w, &o->prediction); w_dig(&w, &o->observation); w_dig(&w, &o->model); |
| 397 | w_u32(&w, o->m); |
| 398 | w_farr(&w, o->nu, o->m); w_farr(&w, o->S, (size_t)o->m * o->m); |
| 399 | w_f64(&w, o->nis); w_i64(&w, o->t_ns); |
| 400 | if (w.bad) return EST_ERR_ENCODING; |
| 401 | *len = w.len; |
| 402 | return EST_OK; |
| 403 | } |
| 404 | |
| 405 | #define PUB_ENC(NAME, TYPE) \ |
| 406 | est_status est_encode_##NAME(const TYPE *o, uint8_t *buf, size_t cap, size_t *len) \ |
| 407 | { \ |
| 408 | if (!o || !buf || !len) return EST_ERR_NULL; \ |
| 409 | return enc_##NAME(o, buf, cap, len); \ |
| 410 | } |
| 411 | PUB_ENC(model, est_model) |
| 412 | PUB_ENC(observation, est_observation) |
| 413 | PUB_ENC(belief, est_belief) |
| 414 | PUB_ENC(prediction, est_prediction) |
| 415 | PUB_ENC(innovation, est_innovation) |
| 416 | |
| 417 | /* ------------------------------------------------------------- decoders */ |
| 418 | |
| 419 | est_status est_decode_model(const uint8_t *buf, size_t len, est_model *out) |
| 420 | { |
| 421 | if (!buf || !out) return EST_ERR_NULL; |
| 422 | rd r = { buf, len, 0, EST_OK }; |
| 423 | est_status st = r_hdr(&r, EST_KIND_MODEL); |
| 424 | if (st != EST_OK) return st; |
| 425 | est_model t; |
| 426 | memset(&t, 0, sizeof t); |
| 427 | t.n = r_dim(&r); t.m = r_dim(&r); |
| 428 | uint32_t e = r_u32(&r), mn = r_u32(&r); |
| 429 | if (r.st == EST_OK && (e != (uint32_t)EST_ESTIMATOR_LINEAR_KALMAN || |
| 430 | mn != (uint32_t)EST_UNCERTAINTY_GAUSSIAN_COVARIANCE)) r_fail(&r, EST_ERR_UNIT); |
| 431 | t.estimator = (est_estimator_kind)e; t.meaning = (est_uncertainty_meaning)mn; |
| 432 | if (r.st != EST_OK) return r.st; |
| 433 | r_uarr(&r, t.state_unit, t.n); r_uarr(&r, t.obs_unit, t.m); |
| 434 | r_farr(&r, t.F, (size_t)t.n * t.n); r_farr(&r, t.Q, (size_t)t.n * t.n); |
| 435 | r_farr(&r, t.H, (size_t)t.m * t.n); r_farr(&r, t.R, (size_t)t.m * t.m); |
| 436 | t.step_ns = r_i64(&r); |
| 437 | st = r_done(&r); |
| 438 | if (st != EST_OK) return st; |
| 439 | st = est_check_model(&t); |
| 440 | if (st != EST_OK) return st; |
| 441 | *out = t; |
| 442 | return EST_OK; |
| 443 | } |
| 444 | |
| 445 | est_status est_decode_observation(const uint8_t *buf, size_t len, est_observation *out) |
| 446 | { |
| 447 | if (!buf || !out) return EST_ERR_NULL; |
| 448 | rd r = { buf, len, 0, EST_OK }; |
| 449 | est_status st = r_hdr(&r, EST_KIND_OBSERVATION); |
| 450 | if (st != EST_OK) return st; |
| 451 | est_observation t; |
| 452 | memset(&t, 0, sizeof t); |
| 453 | t.m = r_dim(&r); |
| 454 | if (r.st != EST_OK) return r.st; |
| 455 | r_uarr(&r, t.unit, t.m); |
| 456 | r_farr(&r, t.z, t.m); r_farr(&r, t.R, (size_t)t.m * t.m); |
| 457 | t.t_ns = r_i64(&r); t.seq = r_u64(&r); |
| 458 | r_dig(&r, &t.source); r_dig(&r, &t.evidence); |
| 459 | st = r_done(&r); |
| 460 | if (st != EST_OK) return st; |
| 461 | st = est_check_observation(&t); |
| 462 | if (st != EST_OK) return st; |
| 463 | *out = t; |
| 464 | return EST_OK; |
| 465 | } |
| 466 | |
| 467 | est_status est_decode_belief(const uint8_t *buf, size_t len, est_belief *out) |
| 468 | { |
| 469 | if (!buf || !out) return EST_ERR_NULL; |
| 470 | rd r = { buf, len, 0, EST_OK }; |
| 471 | est_status st = r_hdr(&r, EST_KIND_BELIEF); |
| 472 | if (st != EST_OK) return st; |
| 473 | est_belief t; |
| 474 | memset(&t, 0, sizeof t); |
| 475 | t.n = r_dim(&r); |
| 476 | if (r.st != EST_OK) return r.st; |
| 477 | r_uarr(&r, t.unit, t.n); |
| 478 | r_farr(&r, t.x, t.n); r_farr(&r, t.P, (size_t)t.n * t.n); |
| 479 | t.generation = r_u64(&r); t.t_ns = r_i64(&r); |
| 480 | r_dig(&r, &t.model); r_dig(&r, &t.parent); r_dig(&r, &t.evidence_root); |
| 481 | st = r_done(&r); |
| 482 | if (st != EST_OK) return st; |
| 483 | st = est_check_belief(&t); |
| 484 | if (st != EST_OK) return st; |
| 485 | *out = t; |
| 486 | return EST_OK; |
| 487 | } |
| 488 | |
| 489 | est_status est_decode_prediction(const uint8_t *buf, size_t len, est_prediction *out) |
| 490 | { |
| 491 | if (!buf || !out) return EST_ERR_NULL; |
| 492 | rd r = { buf, len, 0, EST_OK }; |
| 493 | est_status st = r_hdr(&r, EST_KIND_PREDICTION); |
| 494 | if (st != EST_OK) return st; |
| 495 | est_prediction t; |
| 496 | memset(&t, 0, sizeof t); |
| 497 | r_dig(&r, &t.prior); r_dig(&r, &t.model); |
| 498 | t.horizon = r_u32(&r); t.generation = r_u64(&r); t.t_ns = r_i64(&r); |
| 499 | t.n = r_dim(&r); t.m = r_dim(&r); |
| 500 | if (r.st != EST_OK) return r.st; |
| 501 | r_farr(&r, t.x, t.n); r_farr(&r, t.P, (size_t)t.n * t.n); |
| 502 | r_farr(&r, t.y_mean, t.m); r_farr(&r, t.S, (size_t)t.m * t.m); |
| 503 | t.has_control = r_u32(&r); |
| 504 | if (r.st == EST_OK && t.has_control > 1u) r_fail(&r, EST_ERR_ENCODING); |
| 505 | if (t.has_control) r_farr(&r, t.Bu, t.n); |
| 506 | st = r_done(&r); |
| 507 | if (st != EST_OK) return st; |
| 508 | st = est_check_prediction(&t); |
| 509 | if (st != EST_OK) return st; |
| 510 | *out = t; |
| 511 | return EST_OK; |
| 512 | } |
| 513 | |
| 514 | est_status est_decode_innovation(const uint8_t *buf, size_t len, est_innovation *out) |
| 515 | { |
| 516 | if (!buf || !out) return EST_ERR_NULL; |
| 517 | rd r = { buf, len, 0, EST_OK }; |
| 518 | est_status st = r_hdr(&r, EST_KIND_INNOVATION); |
| 519 | if (st != EST_OK) return st; |
| 520 | est_innovation t; |
| 521 | memset(&t, 0, sizeof t); |
| 522 | r_dig(&r, &t.prediction); r_dig(&r, &t.observation); r_dig(&r, &t.model); |
| 523 | t.m = r_dim(&r); |
| 524 | if (r.st != EST_OK) return r.st; |
| 525 | r_farr(&r, t.nu, t.m); r_farr(&r, t.S, (size_t)t.m * t.m); |
| 526 | t.nis = r_f64(&r); t.t_ns = r_i64(&r); |
| 527 | st = r_done(&r); |
| 528 | if (st != EST_OK) return st; |
| 529 | st = est_check_innovation(&t); |
| 530 | if (st != EST_OK) return st; |
| 531 | *out = t; |
| 532 | return EST_OK; |
| 533 | } |
| 534 | |
| 535 | /* -------------------------------------------------------------- digests */ |
| 536 | |
| 537 | static void hash_domain(const char *domain, const uint8_t *enc, size_t len, est_digest *out) |
| 538 | { |
| 539 | sha256_ctx c; |
| 540 | static const uint8_t zero = 0; |
| 541 | sha256_init(&c); |
| 542 | sha256_update(&c, (const uint8_t *)domain, strlen(domain)); |
| 543 | sha256_update(&c, &zero, 1); |
| 544 | sha256_update(&c, enc, len); |
| 545 | sha256_final(&c, out->b); |
| 546 | } |
| 547 | |
| 548 | #define PUB_DIGEST(NAME, TYPE, DOMAIN) \ |
| 549 | est_status est_digest_##NAME(const TYPE *o, est_digest *out) \ |
| 550 | { \ |
| 551 | if (!o || !out) return EST_ERR_NULL; \ |
| 552 | uint8_t tmp[EST_TMP_MAX]; \ |
| 553 | size_t len = 0; \ |
| 554 | est_status st = enc_##NAME(o, tmp, sizeof tmp, &len); \ |
| 555 | if (st != EST_OK) return st; \ |
| 556 | hash_domain(DOMAIN, tmp, len, out); \ |
| 557 | return EST_OK; \ |
| 558 | } |
| 559 | PUB_DIGEST(model, est_model, EST_DOMAIN_MODEL) |
| 560 | PUB_DIGEST(observation, est_observation, EST_DOMAIN_OBSERVATION) |
| 561 | PUB_DIGEST(belief, est_belief, EST_DOMAIN_BELIEF) |
| 562 | PUB_DIGEST(prediction, est_prediction, EST_DOMAIN_PREDICTION) |
| 563 | PUB_DIGEST(innovation, est_innovation, EST_DOMAIN_INNOVATION) |
| 564 | |
| 565 | est_status est_evidence_root_extend(const est_digest *root, const est_digest *obs, est_digest *out) |
| 566 | { |
| 567 | if (!root || !obs || !out) return EST_ERR_NULL; |
| 568 | uint8_t tmp[2u * EST_DIGEST_SIZE]; |
| 569 | memcpy(tmp, root->b, EST_DIGEST_SIZE); |
| 570 | memcpy(tmp + EST_DIGEST_SIZE, obs->b, EST_DIGEST_SIZE); |
| 571 | est_digest r; |
| 572 | hash_domain(EST_DOMAIN_BELIEF "-root", tmp, sizeof tmp, &r); |
| 573 | *out = r; |
| 574 | return EST_OK; |
| 575 | } |