interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / ng / replay / state_stream.om

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