Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/ng/replay/state_stream.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / ng / replay / state_stream.om` 483 lines ยท 8716 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note seed/ng/replay/state_stream.om: Compact Semantic State-Stream Delta Codec ("OSTM" v1). 3 note Proves bit-exact full-state reconstruction from tiny kilobyte tok-delta frames and 4 note measures exact byte compression ratio vs. raw full-frame state dumps. 5 note Pure seeded OM-2. Requires seed/ng/sha256.om linked earlier. 6 7 const SST_OK ulong 0 8 const SST_ERR_MAGIC ulong 1 9 const SST_ERR_VER ulong 2 10 const SST_ERR_TRUNC ulong 3 11 const SST_ERR_TRAILING ulong 4 12 const SST_ERR_SEQ ulong 5 13 const SST_ERR_OOB ulong 6 14 const SST_ERR_HASH ulong 7 15 const SST_ERR_NOMEM ulong 8 16 17 const SST_HDR_LEN ulong 24 18 const SST_MAX_REGS ulong 64 19 20 text ss_magic "OSTM" 21 text ss_dom "OSTM1" 22 23 note Growable output/input stream pool 24 global ss_buf uchar * 25 26 note Encoder shadow state (previous frame), Decoder reconstructed state, Live reference state 27 global ss_enc_regs ulong 64 28 global ss_enc_slab uchar * 29 global ss_dec_regs ulong 64 30 global ss_dec_slab uchar * 31 global ss_cur_regs ulong 64 32 global ss_cur_slab uchar * 33 34 note State: 0 buf_used, 1 frame_count, 2 n_regs, 3 slab_size, 4 raw_total_bytes, 35 note 5 dec_frames_ok, 6 tmp_pos, 7 tmp_cnt 36 global ss_st ulong 8 37 38 fn sst_ensure_buf need ulong to ulong 39 cur = len ss_buf 40 if le need cur 41 ret SST_OK 42 end 43 cap = cur 44 if eq cap 0 45 cap = ulong 4096 46 end 47 while lt cap need bound 64 48 cap = shl cap 1 49 end 50 rc = enlarge ss_buf cap 51 if lt rc 0 52 ret SST_ERR_NOMEM 53 end 54 ret SST_OK 55 end 56 57 fn sst_ensure_slabs n ulong to ulong 58 cur = len ss_enc_slab 59 if le n cur 60 ret SST_OK 61 end 62 cap = cur 63 if eq cap 0 64 cap = ulong 4096 65 end 66 while lt cap n bound 64 67 cap = shl cap 1 68 end 69 r0 = enlarge ss_enc_slab cap 70 if lt r0 0 71 ret SST_ERR_NOMEM 72 end 73 r1 = enlarge ss_dec_slab cap 74 if lt r1 0 75 ret SST_ERR_NOMEM 76 end 77 r2 = enlarge ss_cur_slab cap 78 if lt r2 0 79 ret SST_ERR_NOMEM 80 end 81 ret SST_OK 82 end 83 84 fn sst_put_u16 off ulong v ulong to void 85 put ss_buf off uchar and v 255 86 put ss_buf add off 1 uchar and shr v 8 255 87 end 88 89 fn sst_put_u32 off ulong v ulong to void 90 for j ulong 4 91 b = uchar and shr v mul j 8 255 92 put ss_buf add off j b 93 end 94 end 95 96 fn sst_put_u64 off ulong v ulong to void 97 for j ulong 8 98 b = uchar and shr v mul j 8 255 99 put ss_buf add off j b 100 end 101 end 102 103 fn sst_get_u16 off ulong to ulong 104 b0 = ulong get ss_buf off 105 b1 = ulong get ss_buf add off 1 106 ret or b0 shl b1 8 107 end 108 109 fn sst_get_u32 off ulong to ulong 110 v = ulong 0 111 for j ulong 4 112 b = ulong get ss_buf add off j 113 v = or v shl b mul j 8 114 end 115 ret v 116 end 117 118 fn sst_get_u64 off ulong to ulong 119 v = ulong 0 120 for j ulong 8 121 b = ulong get ss_buf add off j 122 v = or v shl b mul j 8 123 end 124 ret v 125 end 126 127 fn sst_sh_put64 pos ulong v ulong to void 128 for j ulong 8 129 b = uchar and shr v mul j 8 255 130 put sh_dat add pos j b 131 end 132 end 133 134 note Compute canonical SHA-256 of either encoder live state (which=0) or decoder state (which=1). 135 fn sst_hash_state which ulong to ulong 136 nr = get ss_st 2 137 ssz = get ss_st 3 138 tot = add add 14 mul nr 8 ssz 139 if gt tot len sh_dat 140 ret SST_ERR_NOMEM 141 end 142 for j ulong 5 143 put sh_dat j get ss_dom j 144 end 145 put sh_dat 5 0 146 for j ulong 4 147 s = mul j 8 148 put sh_dat add 6 j uchar and shr nr s 255 149 put sh_dat add 10 j uchar and shr ssz s 255 150 end 151 pos = ulong 14 152 for i ulong nr bound 64 153 v = ulong 0 154 if eq which 0 155 v = get ss_cur_regs i 156 else 157 v = get ss_dec_regs i 158 end 159 sst_sh_put64 pos v 160 pos = add pos 8 161 end 162 if eq which 0 163 for i ulong ssz bound `16777216` 164 b = get ss_cur_slab i 165 put sh_dat add pos i b 166 end 167 else 168 for i ulong ssz bound `16777216` 169 b = get ss_dec_slab i 170 put sh_dat add pos i b 171 end 172 end 173 sh_all tot 174 ret SST_OK 175 end 176 177 fn sst_enc_init n_regs ulong slab_size ulong to ulong 178 if gt n_regs SST_MAX_REGS 179 ret SST_ERR_OOB 180 end 181 rc = sst_ensure_slabs slab_size 182 if ne rc SST_OK 183 ret rc 184 end 185 rc = sst_ensure_buf SST_HDR_LEN 186 if ne rc SST_OK 187 ret rc 188 end 189 for j ulong 8 190 put ss_st j 0 191 end 192 put ss_st 2 n_regs 193 put ss_st 3 slab_size 194 for i ulong SST_MAX_REGS 195 put ss_enc_regs i 0 196 put ss_cur_regs i 0 197 put ss_dec_regs i 0 198 end 199 for i ulong slab_size bound `16777216` 200 put ss_enc_slab i 0 201 put ss_cur_slab i 0 202 put ss_dec_slab i 0 203 end 204 for j ulong 4 205 b = get ss_magic j 206 put ss_buf j b 207 end 208 put ss_buf 4 1 209 put ss_buf 5 0 210 put ss_buf 6 0 211 put ss_buf 7 0 212 sst_put_u32 8 n_regs 213 sst_put_u32 12 slab_size 214 sst_put_u64 16 0 215 put ss_st 0 SST_HDR_LEN 216 ret SST_OK 217 end 218 219 fn sst_write_span p ulong start ulong slen ulong to ulong 220 sst_put_u32 p start 221 sst_put_u16 add p 4 slen 222 q = add p 6 223 for j ulong slen bound 65536 224 b = get ss_cur_slab add start j 225 put ss_buf add q j b 226 put ss_enc_slab add start j b 227 end 228 ret add q slen 229 end 230 231 fn sst_emit_spans p0 ulong ssz ulong to ulong 232 p = p0 233 scnt = ulong 0 234 i = ulong 0 235 while lt i ssz bound `16777216` 236 ca = get ss_cur_slab i 237 ea = get ss_enc_slab i 238 if ne ca ea 239 start = i 240 slen = ulong 0 241 while and lt i ssz lt slen 65535 bound 65536 242 cb = get ss_cur_slab i 243 eb = get ss_enc_slab i 244 if eq cb eb 245 break 246 end 247 i = add i 1 248 slen = add slen 1 249 end 250 rc = sst_ensure_buf add p add 6 slen 251 if ne rc SST_OK 252 ret rc 253 end 254 p = sst_write_span p start slen 255 scnt = add scnt 1 256 else 257 i = add i 1 258 end 259 end 260 put ss_st 6 p 261 put ss_st 7 scnt 262 ret SST_OK 263 end 264 265 note Emit one semantic tok-delta frame comparing ss_cur_* against ss_enc_*. 266 fn sst_emit_frame tok_seq ulong to ulong 267 fseq = get ss_st 1 268 nr = get ss_st 2 269 ssz = get ss_st 3 270 off = get ss_st 0 271 rc = sst_ensure_buf add off add 22 mul nr 10 272 if ne rc SST_OK 273 ret rc 274 end 275 sst_put_u64 off fseq 276 sst_put_u64 add off 8 tok_seq 277 p = add off 18 278 rcnt = ulong 0 279 for i ulong nr bound 64 280 v = get ss_cur_regs i 281 if ne v get ss_enc_regs i 282 sst_put_u16 p i 283 sst_put_u64 add p 2 v 284 put ss_enc_regs i v 285 p = add p 10 286 rcnt = add rcnt 1 287 end 288 end 289 sst_put_u16 add off 16 rcnt 290 scnt_off = p 291 rc = sst_emit_spans add p 4 ssz 292 if ne rc SST_OK 293 ret rc 294 end 295 p = get ss_st 6 296 sst_put_u32 scnt_off get ss_st 7 297 rc = sst_ensure_buf add p 32 298 if ne rc SST_OK 299 ret rc 300 end 301 rc = sst_hash_state 0 302 if ne rc SST_OK 303 ret rc 304 end 305 for j ulong 32 306 b = get sh_dig j 307 put ss_buf add p j b 308 end 309 p = add p 32 310 new_cnt = add fseq 1 311 put ss_st 0 p 312 put ss_st 1 new_cnt 313 sst_put_u64 16 new_cnt 314 raw_frame = add mul nr 8 ssz 315 put ss_st 4 add get ss_st 4 raw_frame 316 ret SST_OK 317 end 318 319 fn sst_apply_span cur ulong soff ulong slen ulong to void 320 for k ulong slen bound 65536 321 b = get ss_buf add cur k 322 put ss_dec_slab add soff k b 323 end 324 end 325 326 fn sst_dec_spans pos ulong scnt ulong tot_bytes ulong ssz ulong to ulong 327 cur = pos 328 for j ulong scnt bound `1048576` 329 if gt add cur 6 tot_bytes 330 ret SST_ERR_TRUNC 331 end 332 soff = sst_get_u32 cur 333 slen = sst_get_u16 add cur 4 334 cur = add cur 6 335 if gt slen sub tot_bytes cur 336 ret SST_ERR_TRUNC 337 end 338 if or gt soff ssz gt slen sub ssz soff 339 ret SST_ERR_OOB 340 end 341 sst_apply_span cur soff slen 342 cur = add cur slen 343 end 344 put ss_st 6 cur 345 ret SST_OK 346 end 347 348 fn sst_dec_frame f ulong pos ulong tot_bytes ulong to ulong 349 if gt add pos 22 tot_bytes 350 ret SST_ERR_TRUNC 351 end 352 fseq = sst_get_u64 pos 353 if ne fseq f 354 ret SST_ERR_SEQ 355 end 356 rcnt = sst_get_u16 add pos 16 357 cur = add pos 18 358 if gt mul rcnt 10 sub tot_bytes cur 359 ret SST_ERR_TRUNC 360 end 361 nr = get ss_st 2 362 for j ulong rcnt bound 65536 363 ri = sst_get_u16 cur 364 rv = sst_get_u64 add cur 2 365 if ge ri nr 366 ret SST_ERR_OOB 367 end 368 put ss_dec_regs ri rv 369 cur = add cur 10 370 end 371 if gt add cur 4 tot_bytes 372 ret SST_ERR_TRUNC 373 end 374 scnt = sst_get_u32 cur 375 rc = sst_dec_spans add cur 4 scnt tot_bytes get ss_st 3 376 if ne rc SST_OK 377 ret rc 378 end 379 cur = get ss_st 6 380 if gt add cur 32 tot_bytes 381 ret SST_ERR_TRUNC 382 end 383 rc = sst_hash_state 1 384 if ne rc SST_OK 385 ret rc 386 end 387 for j ulong 32 388 b = get ss_buf add cur j 389 if ne b get sh_dig j 390 ret SST_ERR_HASH 391 end 392 end 393 put ss_st 6 add cur 32 394 ret SST_OK 395 end 396 397 note Fail-closed decode entire ss_buf[0..tot_bytes) from zero initial state into ss_dec_*. 398 fn sst_decode_verify tot_bytes ulong to ulong 399 if lt tot_bytes SST_HDR_LEN 400 ret SST_ERR_TRUNC 401 end 402 for j ulong 4 403 b = get ss_buf j 404 if ne b get ss_magic j 405 ret SST_ERR_MAGIC 406 end 407 end 408 v4 = get ss_buf 4 409 if ne v4 1 410 ret SST_ERR_VER 411 end 412 v5 = get ss_buf 5 413 v6 = get ss_buf 6 414 v7 = get ss_buf 7 415 if or ne v5 0 or ne v6 0 ne v7 0 416 ret SST_ERR_VER 417 end 418 nr = sst_get_u32 8 419 ssz = sst_get_u32 12 420 fcnt = sst_get_u64 16 421 if gt nr SST_MAX_REGS 422 ret SST_ERR_OOB 423 end 424 rc = sst_ensure_slabs ssz 425 if ne rc SST_OK 426 ret rc 427 end 428 put ss_st 2 nr 429 put ss_st 3 ssz 430 for i ulong SST_MAX_REGS 431 put ss_dec_regs i 0 432 end 433 for i ulong ssz bound `16777216` 434 put ss_dec_slab i 0 435 end 436 pos = ulong SST_HDR_LEN 437 for f ulong fcnt bound `1048576` 438 rc = sst_dec_frame f pos tot_bytes 439 if ne rc SST_OK 440 ret rc 441 end 442 pos = get ss_st 6 443 end 444 if ne pos tot_bytes 445 ret SST_ERR_TRAILING 446 end 447 put ss_st 5 fcnt 448 ret SST_OK 449 end 450 451 fn sst_verify_exact_match to bool 452 nr = get ss_st 2 453 ssz = get ss_st 3 454 for i ulong nr bound 64 455 if ne get ss_dec_regs i get ss_cur_regs i 456 ret false 457 end 458 end 459 for i ulong ssz bound `16777216` 460 a = get ss_dec_slab i 461 b = get ss_cur_slab i 462 if ne a b 463 ret false 464 end 465 end 466 ret true 467 end 468 469 fn sst_stream_bytes to ulong 470 ret get ss_st 0 471 end 472 473 fn sst_raw_bytes to ulong 474 ret get ss_st 4 475 end 476 477 fn sst_frame_count to ulong 478 ret get ss_st 1 479 end 480 481 fn sst_decoded_frames to ulong 482 ret get ss_st 5 483 end