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 / src / turing / tc_tool.om

776 lines · 14331 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note src/turing/tc_tool.om: turing-coder CLI in pure seeded OM-2.
5note Commands: pstream, encode, decode, verify, envelope.
6note Composed after seed/ng/sha256.om, tc_pstream.om, tc_range_lib.om, tc_rans.om, tc_produce.om.
7
8text CT_USE "usage:\n turing-coder pstream <model.tym|uniform:K> <profile_hex> <trace.ctr> <out.tps> <out.tsy>\n turing-coder encode|decode|verify <range|rans> <in.tps> <in.tsy|in.coded> <out|in> [--profile H] [--model H] [--dataset H]\n turing-coder envelope <profile_hex> <out.csv> <trace.ctr> <model.tym|uniform:K>...\n"
9text CT_REF "REFUSED "
10text CT_COL ": "
11text CT_NODET "(no detail)"
12text CT_CMD_PS "pstream"
13text CT_CMD_ENC "encode"
14text CT_CMD_DEC "decode"
15text CT_CMD_VER "verify"
16text CT_CMD_ENV "envelope"
17text CT_COD_RNG "range"
18text CT_COD_RNS "rans"
19text CT_OPT_P "--profile"
20text CT_OPT_M "--model"
21text CT_OPT_D "--dataset"
22text CT_UNI "uniform:"
23text CT_ST_MOD "model"
24text CT_ST_PRF "profile digest"
25text CT_ST_DAT "dataset"
26text CT_ST_TRC "trace"
27text CT_ST_PRD "produce"
28text CT_ST_SER "serialize"
29text CT_ST_WR "write"
30text CT_ST_PS "probability stream"
31text CT_ST_SY "symbols"
32text CT_ST_ENC "encode"
33text CT_ST_COD "coded file"
34text CT_ST_DEC "decode"
35text CT_ST_VER "verify"
36text CT_ST_CSV "csv"
37text CT_ST_RT "round trip"
38text CT_W_BADOPT "bad --profile/--model/--dataset option"
39text CT_W_DIFF "decoded symbol file is not byte-identical to input"
40text CT_W_RTDIFF "whole-file round trip differs"
41text CT_K_MD "model_digest="
42text CT_K_LM "\nlm_bits="
43text CT_K_DD "\ndataset_digest="
44text CT_K_TD "\ntps1_digest="
45text CT_K_SD "\ntsy1_digest="
46text CT_K_CN "\ncount="
47text CT_K_UB "\nideal_ub="
48text CT_K_CB "coded_bytes="
49text CT_K_CBT "\ncoded_bits="
50text CT_K_COD "coder="
51text CT_K_OVH "\noverhead_ub="
52text CT_K_RT "\nroundtrip=byte-exact\n"
53text CT_CSV_HDR "unit,model,index,n,ideal_ub,range_bytes,rans_bytes\n"
54text CT_CSV_FILE "file,"
55text CT_CSV_CRUMB "crumb,"
56text CT_ENV_FILE "file "
57text CT_ENV_MOD " model="
58text CT_ENV_MASK " mask="
59text CT_ENV_K " K="
60text CT_ENV_LM " lm_bits="
61text CT_ENV_N " n="
62text CT_ENV_UB " ideal_ub="
63text CT_ENV_RB " range_bytes="
64text CT_ENV_AB " rans_bytes="
65
66note Argument storage: up to 16 args of 1024 bytes each
67global ct_args uchar 16384
68global ct_alen ulong 16
69global ct_have ulong 3
70global ct_raw_sy uchar 524288
71global ct_raw_sn ulong 1
72
73fn ct_load_args to ulong
74ac = argcnt
75if lt ac 0
76ret 0
77end
78n = ulong ac
79if gt n 16
80n = ulong 16
81end
82for i ulong n bound 16
83k = argread i ct_args mul i 1024 1024
84if lt k 0
85put ct_alen i 0
86else
87put ct_alen i ulong k
88end
89end
90ret n
91end
92
93fn ct_arg_eq idx ulong s view uchar * to bool
94al = get ct_alen idx
95if ne al len s
96ret false
97end
98off = mul idx 1024
99for i ulong al bound 1024
100if ne get ct_args add off i get s i
101ret false
102end
103end
104ret true
105end
106
107fn ct_arg_prefix idx ulong s view uchar * to bool
108al = get ct_alen idx
109sl = len s
110if lt al sl
111ret false
112end
113off = mul idx 1024
114for i ulong sl bound 64
115if ne get ct_args add off i get s i
116ret false
117end
118end
119ret true
120end
121
122fn ct_refuse stage view uchar * rc int to int
123tc_out_reset
124tc_out_str CT_REF
125tc_out_str stage
126tc_out_str CT_COL
127tc_out_errname rc
128tc_out_str CT_COL
129if gt get tc_whylen 0 0
130tc_out_why
131else
132tc_out_str CT_NODET
133end
134tc_out_byte 10
135z = tc_out_flush 2
136ret 2
137end
138
139fn ct_parse_u64 idx ulong off_in ulong to long
140al = get ct_alen idx
141if ge off_in al
142ret -1
143end
144base = mul idx 1024
145v = ulong 0
146i = off_in
147while lt i al bound 32
148c = ulong get ct_args add base i
149if lt c 48
150ret -1
151end
152if gt c 57
153ret -1
154end
155v = add mul v 10 sub c 48
156i = add i 1
157end
158ret long v
159end
160
161fn ct_load_model_arg idx ulong to int
162p = new uchar 1024 0
163off = mul idx 1024
164al = get ct_alen idx
165for i ulong al bound 1024
166put p i get ct_args add off i
167end
168ret tc_load_model p 0 al
169end
170
171fn ct_unhex_arg idx ulong w ulong to int
172p = new uchar 1024 0
173off = mul idx 1024
174al = get ct_alen idx
175for i ulong al bound 1024
176put p i get ct_args add off i
177end
178ret tc_unhex_to_exp p 0 al w
179end
180
181fn ct_sha256_arg idx ulong to int
182p = new uchar 1024 0
183off = mul idx 1024
184al = get ct_alen idx
185for i ulong al bound 1024
186put p i get ct_args add off i
187end
188ret tc_file_sha256 p 0 al
189end
190
191fn ct_ctr1_arg idx ulong to int
192p = new uchar 1024 0
193off = mul idx 1024
194al = get ct_alen idx
195for i ulong al bound 1024
196put p i get ct_args add off i
197end
198ret tp_ctr1_read p 0 al
199end
200
201fn ct_read_pb_arg idx ulong to int
202p = new uchar 1024 0
203off = mul idx 1024
204al = get ct_alen idx
205for i ulong al bound 1024
206put p i get ct_args add off i
207end
208ret tc_read_pb p 0 al
209end
210
211fn ct_write_pb_arg idx ulong to int
212p = new uchar 1024 0
213off = mul idx 1024
214al = get ct_alen idx
215for i ulong al bound 1024
216put p i get ct_args add off i
217end
218ret tc_write_pb p 0 al
219end
220
221fn ct_read_sb_arg idx ulong to int
222p = new uchar 1024 0
223off = mul idx 1024
224al = get ct_alen idx
225for i ulong al bound 1024
226put p i get ct_args add off i
227end
228ret tc_read_sb p 0 al
229end
230
231fn ct_write_sb_arg idx ulong use_db ulong to int
232p = new uchar 1024 0
233off = mul idx 1024
234al = get ct_alen idx
235for i ulong al bound 1024
236put p i get ct_args add off i
237end
238ret tc_write_sb p 0 al use_db
239end
240
241fn ct_read_cb_arg idx ulong to int
242p = new uchar 1024 0
243off = mul idx 1024
244al = get ct_alen idx
245for i ulong al bound 1024
246put p i get ct_args add off i
247end
248ret tc_read_cb p 0 al
249end
250
251fn ct_write_cb_arg idx ulong to int
252p = new uchar 1024 0
253off = mul idx 1024
254al = get ct_alen idx
255for i ulong al bound 1024
256put p i get ct_args add off i
257end
258ret tc_write_cb p 0 al
259end
260
261fn ct_get_model idx ulong to int
262if ct_arg_prefix idx CT_UNI
263kv = ct_parse_u64 idx 8
264if lt kv 0
265ret TC_E_ARG
266end
267ret tp_model_uniform ulong kv
268end
269ret ct_load_model_arg idx
270end
271
272fn ct_parse_expect ac ulong start ulong to int
273put ct_have 0 0
274put ct_have 1 0
275put ct_have 2 0
276i = start
277while lt add i 1 ac bound 16
278w = ulong 99
279if ct_arg_eq i CT_OPT_P
280w = ulong 0
281else
282if ct_arg_eq i CT_OPT_M
283w = ulong 1
284else
285if ct_arg_eq i CT_OPT_D
286w = ulong 2
287end
288end
289end
290if eq w 99
291ret TC_E_ARG
292end
293vi = add i 1
294rc = ct_unhex_arg vi w
295if ne rc TC_OK
296ret TC_E_ARG
297end
298put ct_have w 1
299i = add i 2
300end
301if ne i ac
302ret TC_E_ARG
303end
304ret TC_OK
305end
306
307fn ct_coder_id idx ulong to ulong
308if ct_arg_eq idx CT_COD_RNG
309ret 1
310end
311if ct_arg_eq idx CT_COD_RNS
312ret 2
313end
314ret 0
315end
316
317fn ct_cmd_pstream to int
318rc = ct_get_model 2
319if ne rc TC_OK
320ret ct_refuse CT_ST_MOD rc
321end
322rc = ct_unhex_arg 3 0
323if ne rc TC_OK
324ret ct_refuse CT_ST_PRF TC_E_ARG
325end
326for i ulong 32
327put tp_prof_d i get tc_exp_prof i
328end
329rc = ct_sha256_arg 4
330if ne rc TC_OK
331ret ct_refuse CT_ST_DAT TC_E_IO
332end
333for i ulong 32
334put tp_dat_d i get tc_sh_out i
335end
336rc = ct_ctr1_arg 4
337if ne rc TC_OK
338ret ct_refuse CT_ST_TRC TC_E_FORMAT
339end
340rc = tc_produce
341if ne rc TC_OK
342ret ct_refuse CT_ST_PRD rc
343end
344rc = tc_ps_serialize
345if ne rc TC_OK
346ret ct_refuse CT_ST_SER rc
347end
348rc = tc_sy_serialize 0
349if ne rc TC_OK
350ret ct_refuse CT_ST_SER rc
351end
352rc = ct_write_pb_arg 5
353if ne rc TC_OK
354ret ct_refuse CT_ST_WR TC_E_IO
355end
356rc = ct_write_sb_arg 6 0
357if ne rc TC_OK
358ret ct_refuse CT_ST_WR TC_E_IO
359end
360ub = tc_ideal_ub 0 get tc_ps_n 0
361tc_out_reset
362tc_out_str CT_K_MD
363for i ulong 32
364tc_out_hex_byte get tp_mdig i
365end
366tc_out_str CT_K_LM
367tc_out_u64 get tp_mst 4
368tc_out_str CT_K_DD
369for i ulong 32
370tc_out_hex_byte get tp_dat_d i
371end
372tc_out_str CT_K_TD
373tc_out_hex_ps_dig 32
374tc_out_str CT_K_SD
375tc_out_hex_sy_dig 32
376tc_out_str CT_K_CN
377tc_out_u64 get tc_ps_n 0
378tc_out_str CT_K_UB
379tc_out_i64 ub
380tc_out_byte 10
381z = tc_out_flush 1
382ret 0
383end
384
385fn ct_cmd_coder ac ulong mode ulong to int
386note mode: 1=encode, 2=decode, 3=verify
387cid = ct_coder_id 2
388if eq cid 0
389ret 1
390end
391rc = ct_read_pb_arg 3
392if ne rc TC_OK
393ret ct_refuse CT_ST_PS rc
394end
395rc = tc_ps_parse get tc_pb_len 0
396if ne rc TC_OK
397ret ct_refuse CT_ST_PS rc
398end
399rc = ct_parse_expect ac 6
400if ne rc TC_OK
401tc_why_set CT_W_BADOPT
402ret ct_refuse CT_ST_PS TC_E_ARG
403end
404rc = tc_ps_expect get ct_have 0 get ct_have 1 get ct_have 2
405if ne rc TC_OK
406ret ct_refuse CT_ST_PS rc
407end
408if eq mode 1
409rc = ct_read_sb_arg 4
410if ne rc TC_OK
411ret ct_refuse CT_ST_SY rc
412end
413rc = tc_sy_parse get tc_sb_len 0
414if ne rc TC_OK
415ret ct_refuse CT_ST_SY rc
416end
417if eq cid 1
418rc = tc_range_encode
419else
420rc = tc_rans_encode
421end
422if ne rc TC_OK
423ret ct_refuse CT_ST_ENC rc
424end
425rc = ct_write_cb_arg 5
426if ne rc TC_OK
427ret ct_refuse CT_ST_WR TC_E_IO
428end
429cn = get tc_cb_len 0
430tc_out_reset
431tc_out_str CT_K_CB
432tc_out_u64 cn
433tc_out_str CT_K_CBT
434tc_out_u64 mul cn 8
435tc_out_byte 10
436z = tc_out_flush 1
437ret 0
438end
439cidx = ulong 4
440if eq mode 3
441cidx = ulong 5
442end
443rc = ct_read_cb_arg cidx
444if ne rc TC_OK
445ret ct_refuse CT_ST_COD TC_E_IO
446end
447cn = get tc_cb_len 0
448if eq cid 1
449rc = tc_range_decode cn
450else
451rc = tc_rans_decode cn
452end
453if ne rc TC_OK
454ret ct_refuse CT_ST_DEC rc
455end
456rc = tc_sy_serialize 1
457if ne rc TC_OK
458ret ct_refuse CT_ST_DEC rc
459end
460if eq mode 2
461rc = ct_write_sb_arg 5 1
462if ne rc TC_OK
463ret ct_refuse CT_ST_WR TC_E_IO
464end
465ret 0
466end
467rc = ct_read_sb_arg 4
468if ne rc TC_OK
469ret ct_refuse CT_ST_SY rc
470end
471rawn = get tc_sb_len 0
472for i ulong rawn bound 524288
473put ct_raw_sy i get tc_sb i
474end
475put ct_raw_sn 0 rawn
476rc = tc_sy_parse rawn
477if ne rc TC_OK
478ret ct_refuse CT_ST_SY rc
479end
480rc = tc_pair_check
481if ne rc TC_OK
482ret ct_refuse CT_ST_SY rc
483end
484dn = get tc_db_len 0
485if ne rawn dn
486tc_why_set CT_W_DIFF
487ret ct_refuse CT_ST_VER TC_E_SYMBOL
488end
489for i ulong dn bound 524288
490if ne get ct_raw_sy i get tc_db i
491tc_why_set CT_W_DIFF
492ret ct_refuse CT_ST_VER TC_E_SYMBOL
493end
494end
495ub = tc_ideal_ub 0 get tc_ps_n 0
496ovh = sub mul long mul cn 8 1000000 ub
497tc_out_reset
498tc_out_str CT_K_COD
499if eq cid 1
500tc_out_str CT_COD_RNG
501else
502tc_out_str CT_COD_RNS
503end
504tc_out_str CT_K_TD
505tc_out_hex_ps_dig 32
506tc_out_str CT_K_CN
507tc_out_u64 get tc_ps_n 0
508tc_out_byte 10
509tc_out_str CT_K_CB
510tc_out_u64 cn
511tc_out_str CT_K_CBT
512tc_out_u64 mul cn 8
513tc_out_str CT_K_UB
514tc_out_i64 ub
515tc_out_str CT_K_OVH
516tc_out_i64 ovh
517tc_out_str CT_K_RT
518z = tc_out_flush 1
519ret 0
520end
521
522fn ct_sym_eq lo ulong n ulong to bool
523for i ulong n bound 500000
524if ne get tc_dsy_sym i get tc_sy_sym add lo i
525ret false
526end
527end
528ret true
529end
530
531fn ct_env_crumb csv_fd int lo ulong hi ulong to int
532n = sub hi lo
533k = get tc_ps_k 0
534cu = tc_ideal_ub lo hi
535rc = tc_range_encode_raw lo n k 0
536if ne rc TC_OK
537ret ct_refuse CT_ST_ENC TC_E_IO
538end
539nx = get tc_cb_plen 0
540rc = tc_range_decode_raw lo n k 0 nx
541if ne rc TC_OK
542ret ct_refuse CT_ST_RT TC_E_SYMBOL
543end
544if not ct_sym_eq lo n
545ret ct_refuse CT_ST_RT TC_E_SYMBOL
546end
547rc = tc_rans_encode_raw lo n k 0
548if ne rc TC_OK
549ret ct_refuse CT_ST_ENC TC_E_IO
550end
551ny = get tc_cb_plen 0
552rc = tc_rans_decode_raw lo n k 0 ny
553if ne rc TC_OK
554ret ct_refuse CT_ST_RT TC_E_SYMBOL
555end
556if not ct_sym_eq lo n
557ret ct_refuse CT_ST_RT TC_E_SYMBOL
558end
559tc_out_reset
560tc_out_str CT_CSV_CRUMB
561for i ulong 8
562tc_out_hex_byte get tp_mdig i
563end
564tc_out_byte 44
565tc_out_u64 ulong get tc_ps_crumb lo
566tc_out_byte 44
567tc_out_u64 n
568tc_out_byte 44
569tc_out_i64 cu
570tc_out_byte 44
571tc_out_u64 add nx TC_CODED_HEADER
572tc_out_byte 44
573tc_out_u64 add ny TC_CODED_HEADER
574tc_out_byte 10
575z = tc_out_flush csv_fd
576ret 0
577end
578
579fn ct_env_crumbs csv_fd int pn ulong to int
580lo = ulong 0
581while lt lo pn bound 500000
582hi = add lo 1
583cl = get tc_ps_crumb lo
584while lt hi pn bound 500000
585if ne get tc_ps_crumb hi cl
586break
587end
588hi = add hi 1
589end
590erc = ct_env_crumb csv_fd lo hi
591if ne erc 0
592ret erc
593end
594lo = hi
595end
596ret 0
597end
598
599fn ct_env_one csv_fd int mi ulong to int
600rc = ct_get_model mi
601if ne rc TC_OK
602ret ct_refuse CT_ST_MOD rc
603end
604rc = tc_produce
605if ne rc TC_OK
606ret ct_refuse CT_ST_PRD rc
607end
608rc = tc_ps_serialize
609if ne rc TC_OK
610ret ct_refuse CT_ST_SER rc
611end
612rc = tc_sy_serialize 0
613if ne rc TC_OK
614ret ct_refuse CT_ST_SER rc
615end
616rc = tc_ps_parse get tc_pb_len 0
617if ne rc TC_OK
618ret ct_refuse CT_ST_PS rc
619end
620rc = tc_sy_parse get tc_sb_len 0
621if ne rc TC_OK
622ret ct_refuse CT_ST_SY rc
623end
624pn = get tc_ps_n 0
625ub = tc_ideal_ub 0 pn
626rc = tc_range_encode
627if ne rc TC_OK
628ret ct_refuse CT_ST_ENC rc
629end
630na = get tc_cb_len 0
631rc = tc_range_decode na
632if ne rc TC_OK
633ret ct_refuse CT_ST_DEC rc
634end
635for i ulong pn bound 500000
636if ne get tc_dsy_sym i get tc_sy_sym i
637tc_why_set CT_W_RTDIFF
638ret ct_refuse CT_ST_RT TC_E_SYMBOL
639end
640end
641rc = tc_rans_encode
642if ne rc TC_OK
643ret ct_refuse CT_ST_ENC rc
644end
645nb = get tc_cb_len 0
646rc = tc_rans_decode nb
647if ne rc TC_OK
648ret ct_refuse CT_ST_DEC rc
649end
650for i ulong pn bound 500000
651if ne get tc_dsy_sym i get tc_sy_sym i
652tc_why_set CT_W_RTDIFF
653ret ct_refuse CT_ST_RT TC_E_SYMBOL
654end
655end
656tc_out_reset
657tc_out_str CT_ENV_FILE
658tlen = get ct_alen 4
659toff = ulong 4096
660for i ulong tlen bound 1024
661tc_out_byte get ct_args add toff i
662end
663tc_out_str CT_ENV_MOD
664for i ulong 8
665tc_out_hex_byte get tp_mdig i
666end
667tc_out_str CT_ENV_MASK
668tc_out_u64 get tp_mst 1
669tc_out_str CT_ENV_K
670tc_out_u64 get tp_mst 0
671tc_out_str CT_ENV_LM
672tc_out_u64 get tp_mst 4
673tc_out_str CT_ENV_N
674tc_out_u64 pn
675tc_out_str CT_ENV_UB
676tc_out_i64 ub
677tc_out_str CT_ENV_RB
678tc_out_u64 na
679tc_out_str CT_ENV_AB
680tc_out_u64 nb
681tc_out_byte 10
682z = tc_out_flush 1
683tc_out_reset
684tc_out_str CT_CSV_FILE
685for i ulong 8
686tc_out_hex_byte get tp_mdig i
687end
688tc_out_byte 44
689tc_out_byte 48
690tc_out_byte 44
691tc_out_u64 pn
692tc_out_byte 44
693tc_out_i64 ub
694tc_out_byte 44
695tc_out_u64 na
696tc_out_byte 44
697tc_out_u64 nb
698tc_out_byte 10
699z = tc_out_flush csv_fd
700ret ct_env_crumbs csv_fd pn
701end
702
703fn ct_cmd_envelope ac ulong to int
704rc = ct_unhex_arg 2 0
705if ne rc TC_OK
706ret ct_refuse CT_ST_PRF TC_E_ARG
707end
708for i ulong 32
709put tp_prof_d i get tc_exp_prof i
710end
711cfd = fopen ct_args 3072 get ct_alen 3 577 420
712if lt cfd 0
713ret ct_refuse CT_ST_CSV TC_E_IO
714end
715rc = ct_sha256_arg 4
716if ne rc TC_OK
717z = fclose int cfd
718ret ct_refuse CT_ST_DAT TC_E_IO
719end
720for i ulong 32
721put tp_dat_d i get tc_sh_out i
722end
723rc = ct_ctr1_arg 4
724if ne rc TC_OK
725z = fclose int cfd
726ret ct_refuse CT_ST_TRC TC_E_FORMAT
727end
728tc_out_reset
729tc_out_str CT_CSV_HDR
730z2 = tc_out_flush int cfd
731mi = ulong 5
732while lt mi ac bound 16
733erc = ct_env_one int cfd mi
734if ne erc 0
735z = fclose int cfd
736ret erc
737end
738mi = add mi 1
739end
740z = fclose int cfd
741if ne z 0
742ret ct_refuse CT_ST_CSV TC_E_IO
743end
744ret 0
745end
746
747fn main to int
748ac = ct_load_args
749if eq ac 7
750if ct_arg_eq 1 CT_CMD_PS
751ret ct_cmd_pstream
752end
753end
754if ge ac 6
755if eq rem sub ac 6 2 0
756if ne ct_coder_id 2 0
757if ct_arg_eq 1 CT_CMD_ENC
758ret ct_cmd_coder ac 1
759end
760if ct_arg_eq 1 CT_CMD_DEC
761ret ct_cmd_coder ac 2
762end
763if ct_arg_eq 1 CT_CMD_VER
764ret ct_cmd_coder ac 3
765end
766end
767end
768if ct_arg_eq 1 CT_CMD_ENV
769ret ct_cmd_envelope ac
770end
771end
772tc_out_reset
773tc_out_str CT_USE
774z = tc_out_flush 2
775ret 1
776end