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_range.om

1665 lines · 32017 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note src/turing/tc_range.om: TURING Yield coder A (TCR1, range coder) written in Omega, the peer
5note of src/turing/tc_range.c (ledger L111). Normative text: calibration/docs/CODER_SPEC.md
6note sections 2 (TPS1), 3 (TSY1), 4 (coded header), 5 (range coder), 8 (ideal length) and
7note docs/turing/TURING_YIELD_RANGE_CODER_OM.md (the length bound and its proof).
8note
9note Program mode, OM-1 code in an OM-2 unit: composed after the FIPS 180-4 unit seed/ng/sha256.om
10note (prefix sh_), with an om2 first line for the unsized text views:
11note { printf 'om2\n'; cat seed/ng/sha256.om src/turing/tc_range.om; } | seed/out/omc > tcr-om
12note
13note CLI (same shape as turing-coder, range only):
14note tcr-om encode range IN.tps IN.tsy OUT.tcr
15note tcr-om decode range IN.tps IN.tcr OUT.tsy
16note tcr-om verify range IN.tps IN.tsy IN.tcr
17note tcr-om selftest
18note Exit 0 accepted, 2 refused (one line on stderr with the CODER_SPEC s.10 code), 1 usage,
19note 3 ALARM: a proved property failed on this run (length identity, upper or lower bound, ub rule,
20note or a micro-bit sum that would wrap).
21note Refusals come in turing-coder's order (encode: TPS1, TSY1, pair; decode: TPS1, coded file,
22note decode; verify: TPS1, coded file and decode, TSY1 and pair, byte identity).
23note
24note Every file is streamed through fixed buffers, so memory does not grow with the number of
25note symbols. The micro-bit sums are 64-bit, and one past either ceiling wraps 2^64-1 (Wolfram W3):
26note tpsload refuses n > 1152514738906 with ARG, before the body-size check, because
27note 16005647*n + 480000000 no longer fits
28note report refuses a coded file of more than 2305843009213 bytes with ARG, because
29note coded_bytes*8*1000000 no longer fits
30note These are the word limits of the check, not a buffer size. A sum that would wrap anyway
31note raises ALARM 7 (uadd / umul). Each input is read twice: a validation pass (trailer digest,
32note header, every record or symbol, in the refusal order of src/turing/tc_pstream.c) and the
33note coding pass, which re-checks every record and symbol and re-hashes the file (DIGEST if it
34note changed in between). An output path naming an input is refused; a refusal after the output
35note was created, including IO (102) from a failed fsync or fclose in ofinish, removes it.
36note
37note Length bounds. The Lean file docs/turing/proofs/TcrBound.lean proves the natural-number model
38note (step, renorm, run), not encs. The log step and Wolfram receipts are in
39note docs/turing/TURING_YIELD_RANGE_CODER_OM.md.
40note payload_bytes = renorm shifts + 4, coded_bits = 8 * shifts + 480
41note encode: shifts are EN[5], one per encs renorm; payload is the bytes emitted (EN[6])
42note decode and verify: shifts are DC[5], one per decs renorm byte (range < 2^24). The four
43note bytes decbeg reads are not shifts. Payload is the declared length. ST[7] is DC[5],
44note not payload - 4. ALARM 1 if they disagree.
45note selftest: both comparisons, on the encoded bytes and after a successful decode
46note upper: 1000000 * coded_bits <= ideal_ub + 5647 * n + 480000000
47note lower: 1000000 * coded_bits >= ideal_ub + 472000000 - n - gain_ub, gain_ub = sum over symbols
48note coded as the last of their row of an upper bound of 1e6 * log2(1 + 256/q) (gub).
49note That integer test is a necessary consequence of coded_bits >= T + 472 - G, not a
50note sufficient one. lower=PASS certifies coded_bits >= T + 472 - G -
51note (1.49984 n + 24.37 m) / 1e6, m = last-symbol count (see the range-coder doc).
52note The upper and lower comparisons run on every encode, decode, verify and selftest case.
53
54global RB uchar 196608
55global RS ulong 24
56global OB uchar 65536
57global OS ulong 8
58global MB uchar 16777216
59global HB uchar 256
60global HS uint 16
61global HP uchar 128
62global HN ulong 2
63global Q ulong 16
64global EN ulong 8
65global DC ulong 8
66global ST ulong 32
67global UBT ulong 65537
68global AR uchar 24576
69global AL ulong 8
70global DG uchar 256
71global INO ulong 8
72global HSEL ulong 4
73global FS uchar 128
74global PB uchar 4096
75global PL ulong 2
76global RG ulong 2
77
78text DPS "turing.tc.pstream.v1"
79text DSY "turing.tc.symbols.v1"
80text ENAM "ARG IO FORMAT DIGEST NORM ZERO PROFILE MODEL DATASET COUNT HEADER BINDING TRUNC TRAIL CORRUPT SYMBOL INDEX "
81text TREF "tcr-om: refused -"
82text TALM "tcr-om: ALARM "
83text TAT " at "
84text TUSE "usage: tcr-om encode|decode|verify range IN.tps IN.tsy|IN.tcr OUT|IN, or tcr-om selftest\n"
85text KMODE "mode="
86text KCNT "count="
87text KIDL "ideal_ub="
88text KCB "coded_bytes="
89text KCBT "coded_bits="
90text KPAY "payload_bytes="
91text KSHF "shifts="
92text KBND "bound_ub="
93text KSLK "slack_ub="
94text KOVH "overhead_ub="
95text KTPS "tps1_digest="
96text KTSY "tsy1_digest="
97text KBOK "bound=PASS\n"
98text KLOW "lower_ub="
99text KLOK "lower=PASS\n"
100text KRT "roundtrip=byte-exact\n"
101text KCASE "selftest cases="
102text KSYM " symbols="
103text KFLIP " flips_detected="
104text KMIN " min_slack_ub="
105text KMIN1 " min_slack_n_ge_1_ub="
106text KLMIN " min_lower_slack_ub="
107text KBELOW " max_below_ideal_ub="
108text KPASS "\nselftest=PASS\n"
109text WENC "encode"
110text WDEC "decode"
111text WVER "verify"
112text WSLF "selftest"
113text WRNG "range"
114text ALID "length identity payload_bytes != shifts + 4"
115text ALBD "length bound 1e6*coded_bits > ideal_ub + 5647*n + 480e6"
116text ALRT "selftest round trip"
117text ALFL "selftest corruption not detected"
118text ALUB "ub rule differs from the Wolfram receipt"
119text ALLO "lower bound 1e6*coded_bits < ideal_ub + 472e6 - n - gain_ub"
120text ALOV "length sum would wrap the 64-bit micro-bit word"
121text WOVF "overflow"
122text KUB "ub_rule q=1..65535 sum="
123text KUBQ " qsum="
124
125note ---------------------------------------------------------------- printing (PB to fd 1 or 2)
126
127fn pbyte b ulong to void
128n = get PL 0
129if lt n 4096
130put PB n uchar b
131put PL 0 add n 1
132end
133end
134
135fn ptext t view uchar * to void
136for i ulong len t bound 4096
137pbyte ulong get t i
138end
139end
140
141fn pdec v ulong to void
142d = new uchar 24 0
143k = ulong 0
144x = v
145while true bound 21
146put d k uchar add rem x 10 48
147k = add k 1
148x = div x 10
149if eq x 0
150break
151end
152end
153while gt k 0 bound 21
154k = sub k 1
155pbyte ulong get d k
156end
157end
158
159fn phex o ulong to void
160for i ulong 32
161b = ulong get DG add o i
162h = shr b 4
163l = and b 15
164if lt h 10
165pbyte add h 48
166else
167pbyte add h 87
168end
169if lt l 10
170pbyte add l 48
171else
172pbyte add l 87
173end
174end
175end
176
177fn pkv k view uchar * v ulong to void
178ptext k
179pdec v
180pbyte 10
181end
182
183fn pflush fd int to void
184n = get PL 0
185o = ulong 0
186while lt o n bound 4096
187k = write fd PB o sub n o
188if le k 0
189break
190end
191o = add o ulong k
192end
193put PL 0 0
194end
195
196note ---------------------------------------------------------------- refusal and cleanup
197
198fn cleanup to void
199note Remove a partly written output file (encode, decode) after a refusal.
200if eq get OS 4 1
201f = int get OS 0
202z = fclose f
203o = mul get OS 5 4096
204k = unlink AR o get AL get OS 5
205put OS 4 0
206end
207end
208
209fn refuse c ulong t ulong to void
210note c = 101..117 (CODER_SPEC s.10), t = the record or symbol index (or 0).
211cleanup
212put PL 0 0
213ptext TREF
214pdec c
215pbyte 32
216o = mul sub c 101 8
217for i ulong 8
218b = ulong get ENAM add o i
219if ne b 32
220pbyte b
221end
222end
223ptext TAT
224pdec t
225pbyte 10
226pflush 2
227exit 2
228end
229
230fn alarm w ulong to void
231cleanup
232pflush 1
233ptext TALM
234if eq w 1
235ptext ALID
236end
237if eq w 2
238ptext ALBD
239end
240if eq w 3
241ptext ALRT
242end
243if eq w 4
244ptext ALFL
245end
246if eq w 5
247ptext ALUB
248end
249if eq w 6
250ptext ALLO
251end
252if eq w 7
253ptext ALOV
254end
255pbyte 10
256pflush 2
257exit 3
258end
259
260note ---------------------------------------------------------------- input streams (0 TPS, 1 TSY, 2 coded)
261
262fn rdfill s ulong to ulong
263b = mul s 8
264f = int get RS b
265k = read f RB mul s 65536 65536
266if lt k 0
267refuse 102 0
268end
269put RS add b 1 0
270put RS add b 2 ulong k
271ret ulong k
272end
273
274fn rdb s ulong to ulong
275note Next byte of stream s, or 256 at end of file. HSEL[s] = 1 + h feeds the byte to hash stream h.
276b = mul s 8
277if ge get RS add b 1 get RS add b 2
278k = rdfill s
279if eq k 0
280ret 256
281end
282end
283p = get RS add b 1
284put RS add b 1 add p 1
285v = ulong get RB add mul s 65536 p
286h = get HSEL s
287if ne h 0
288hbyte sub h 1 v
289end
290ret v
291end
292
293fn lefs o ulong to ulong
294v = ulong 0
295for i ulong 8
296v = or shl v 8 ulong get FS sub add o 7 i
297end
298ret v
299end
300
301fn sopen s ulong a ulong to ulong
302note Open argument a read-only as stream s; returns its size in bytes and keeps its device and
303note inode (INO[2s], INO[2s+1]) so that an output path naming an input is refused.
304o = mul a 4096
305f = fopen AR o get AL a 0 0
306if lt f 0
307refuse 102 0
308end
309b = mul s 8
310put RS b ulong f
311put RS add b 1 0
312put RS add b 2 0
313put HSEL s 0
314k = fstat int f FS 0 128
315if lt k 0
316refuse 102 0
317end
318put INO mul s 2 lefs 0
319put INO add mul s 2 1 lefs 8
320ret lefs 48
321end
322
323fn srew s ulong to void
324note Back to byte 0 of stream s.
325b = mul s 8
326f = int get RS b
327k = fseek f long 0 int 0
328if ne k 0
329refuse 102 0
330end
331put RS add b 1 0
332put RS add b 2 0
333end
334
335fn rdn s ulong n ulong to void
336note n bytes of stream s into HB[0..n); a short file here is an IO refusal (sizes are checked first).
337for i ulong n bound 256
338b = rdb s
339if eq b 256
340refuse 102 0
341end
342put HB i uchar b
343end
344end
345
346fn lev o ulong w ulong to ulong
347note Little-endian value of w bytes at HB[o].
348v = ulong 0
349for i ulong w bound 8
350v = or shl v 8 ulong get HB sub add o sub w 1 i
351end
352ret v
353end
354
355fn magic a ulong b ulong c ulong d ulong to bool
356r = true
357if ne ulong get HB 0 a
358r = false
359end
360if ne ulong get HB 1 b
361r = false
362end
363if ne ulong get HB 2 c
364r = false
365end
366if ne ulong get HB 3 d
367r = false
368end
369ret r
370end
371
372note ---------------------------------------------------------------- SHA-256 streams over sh_ (h 0, 1)
373
374fn hblk h ulong to void
375for j ulong 8
376put sh_h j get HS add mul h 8 j
377end
378for j ulong 64
379put sh_pad j get HP add mul h 64 j
380end
381sh_blk 1 0
382for j ulong 8
383put HS add mul h 8 j get sh_h j
384end
385end
386
387fn hbyte h ulong b ulong to void
388n = get HN h
389put HP add mul h 64 rem n 64 uchar b
390n = add n 1
391put HN h n
392if eq rem n 64 0
393hblk h
394end
395end
396
397fn hbeg h ulong d ulong to void
398note Start stream h with domain d (0 pstream, 1 symbols) and its 0x00 separator.
399sh_init
400for j ulong 8
401put HS add mul h 8 j get sh_k add j 64
402end
403put HN h 0
404for i ulong 20
405if eq d 0
406hbyte h ulong get DPS i
407else
408hbyte h ulong get DSY i
409end
410end
411hbyte h 0
412end
413
414fn hend h ulong o ulong to void
415note Finish stream h; the 32-byte digest goes to DG[o..o+32).
416bits = mul get HN h 8
417hbyte h 128
418while ne rem get HN h 64 56 bound 64
419hbyte h 0
420end
421for j ulong 8
422hbyte h and shr bits mul sub 7 j 8 255
423end
424for j ulong 8
425put sh_h j get HS add mul h 8 j
426end
427sh_out
428for i ulong 32
429put DG add o i get sh_dig i
430end
431end
432
433fn dgeq a ulong b ulong to bool
434r = true
435for i ulong 32
436if ne get DG add a i get DG add b i
437r = false
438end
439end
440ret r
441end
442
443fn hfile s ulong d ulong sz ulong o ulong to bool
444note Validation pass: hash domain d || bytes[0..sz-32) of stream s, read the 32-byte trailer
445note into DG[o+32..o+64) and the computed digest into DG[o..o+32); true when they agree.
446hbeg 0 d
447m = sub sz 32
448x = ulong 0
449while lt x m bound 9223372036854775807
450b = rdb s
451if eq b 256
452refuse 102 0
453end
454hbyte 0 b
455x = add x 1
456end
457for i ulong 32
458b = rdb s
459if eq b 256
460refuse 102 0
461end
462put DG add add o 32 i uchar b
463end
464hend 0 o
465ret dgeq o add o 32
466end
467
468note ---------------------------------------------------------------- ideal length (CODER_SPEC s.8)
469
470fn sq62 m ulong to ulong
471note floor(m * m / 2^62) for m < 2^63 through 32-bit limbs (as tqsq, native/turing/origsel.om).
472a = shr m 32
473b = and m 4294967295
474p0 = mul b b
475p1 = mul a b
476p2 = mul a a
477c = shl p1 1
478mid = add shr p0 32 and c 4294967295
479lo = or and p0 4294967295 shl and mid 4294967295 32
480hi = add add p2 shr c 32 shr mid 32
481ret or shl hi 2 shr lo 62
482end
483
484fn hibit q ulong to ulong
485i = ulong 63
486while eq and shr q i 1 0 bound 64
487i = sub i 1
488end
489ret i
490end
491
492fn ubcalc q ulong to ulong
493ip = hibit q
494m = shl q sub 62 ip
495f = ulong 0
496for i ulong 32
497m = sq62 m
498if ge m 9223372036854775808
499f = or f shl 1 sub 31 i
500m = shr m 1
501end
502end
503l = or shl ip 32 f
504x = sub 68719476736 l
505ret shr add mul x 1000000 2147483648 32
506end
507
508fn ub q ulong to ulong
509note ub(q) in micro-bits, memoised (UBT holds ub + 1, 0 = not yet computed).
510v = get UBT q
511if eq v 0
512v = add ubcalc q 1
513put UBT q v
514end
515ret sub v 1
516end
517
518note ---------------------------------------------------------------- payload byte sink and source
519
520fn oflush to void
521n = get OS 1
522f = int get OS 0
523o = ulong 0
524while lt o n bound 65536
525k = write f OB o sub n o
526if le k 0
527refuse 102 0
528end
529o = add o ulong k
530end
531put OS 1 0
532end
533
534fn obyte b ulong to void
535note Output byte to the file buffer (OS[3] = 0) or to the selftest memory MB (OS[3] = 1).
536if eq get OS 3 1
537p = get OS 2
538put MB p uchar b
539put OS 2 add p 1
540ret
541end
542n = get OS 1
543put OB n uchar b
544put OS 1 add n 1
545if eq add n 1 65536
546oflush
547end
548end
549
550fn nxtb to ulong
551note Next payload byte for the decoder, or 256 when the declared payload is used up.
552p = get DC 2
553if ge p get DC 3
554ret 256
555end
556put DC 2 add p 1
557if eq get DC 4 1
558ret ulong get MB p
559end
560b = rdb 2
561if eq b 256
562refuse 102 0
563end
564ret b
565end
566
567note ---------------------------------------------------------------- range coder (CODER_SPEC s.5)
568
569fn shlow to ulong
570note shift_low; returns 0. A carry before the first cached byte has nowhere to land, so the
571note emitted length cannot equal the renorm shifts. That is the length identity, ALARM 1,
572note not a bad input (decode still reports CORRUPT).
573lo = get EN 0
574c = shr lo 32
575if or lt and lo 4294967295 4278190080 ne c 0
576if eq get EN 3 1
577obyte and add get EN 2 c 255
578put EN 6 add get EN 6 1
579else
580if ne c 0
581alarm 1
582end
583end
584while gt get EN 4 0 bound 9223372036854775807
585obyte and add 255 c 255
586put EN 6 add get EN 6 1
587put EN 4 sub get EN 4 1
588end
589put EN 2 and shr lo 24 255
590put EN 3 1
591else
592put EN 4 add get EN 4 1
593end
594put EN 0 shl and lo 16777215 8
595ret 0
596end
597
598fn encbeg to void
599put EN 0 0
600put EN 1 4294967295
601put EN 2 0
602put EN 3 0
603put EN 4 0
604put EN 5 0
605put EN 6 0
606end
607
608fn encs s ulong to ulong
609note Code symbol s with the row Q[0..K), K = ST[2]; returns 0 or a refusal number.
610k = get ST 2
611cum = ulong 0
612for x ulong s bound 16
613cum = add cum get Q x
614end
615rg = get EN 1
616r = shr rg 16
617put EN 0 add get EN 0 mul r cum
618if eq s sub k 1
619rg = sub rg mul r cum
620else
621rg = mul r get Q s
622end
623while lt rg 16777216 bound 2
624rg = shl rg 8
625put EN 5 add get EN 5 1
626e = shlow
627if ne e 0
628ret e
629end
630end
631put EN 1 rg
632ret 0
633end
634
635fn encend to ulong
636for i ulong 5
637e = shlow
638if ne e 0
639ret e
640end
641end
642ret 0
643end
644
645fn decbeg to ulong
646note DC: 0 code, 1 range, 2 payload bytes consumed, 3 payload bytes declared, 4 source,
647note 5 renorm shifts (one per decs byte, not these four).
648if lt get DC 3 4
649ret 113
650end
651put DC 2 0
652put DC 5 0
653c = ulong 0
654for i ulong 4
655c = or shl c 8 nxtb
656end
657put DC 0 c
658put DC 1 4294967295
659ret 0
660end
661
662fn decs to ulong
663note Decode one symbol with the row Q[0..K); returns it, or 1000 + a refusal number.
664k = get ST 2
665code = get DC 0
666rg = get DC 1
667if ge code rg
668ret 1115
669end
670r = shr rg 16
671cum = ulong 0
672s = ulong 0
673nk = sub k 1
674while lt s nk bound 16
675nx = add cum get Q s
676if lt code mul r nx
677break
678end
679cum = nx
680s = add s 1
681end
682code = sub code mul r cum
683if eq s nk
684rg = sub rg mul r cum
685else
686rg = mul r get Q s
687end
688while lt rg 16777216 bound 2
689b = nxtb
690if gt b 255
691ret 1113
692end
693code = and or shl code 8 b 4294967295
694rg = shl rg 8
695put DC 5 add get DC 5 1
696end
697put DC 0 code
698put DC 1 rg
699ret s
700end
701
702fn decend to ulong
703if ne get DC 0 0
704ret 115
705end
706if ne get DC 2 get DC 3
707ret 114
708end
709ret 0
710end
711
712note ---------------------------------------------------------------- TPS1 (stream 0) and TSY1 (stream 1)
713
714fn tpsrow t ulong chk ulong to void
715note Read record t into Q (K = ST[2], record bytes ST[3]); chk 1 applies INDEX, ZERO, NORM.
716k = get ST 2
717rdn 0 get ST 3
718if eq chk 1
719if ne lev 0 8 t
720refuse 117 t
721end
722end
723sum = ulong 0
724zero = false
725for x ulong k bound 16
726v = lev add 24 mul x 2 2
727put Q x v
728sum = add sum v
729if eq v 0
730zero = true
731end
732end
733if eq chk 1
734if zero
735refuse 106 t
736end
737if ne sum 65536
738refuse 105 t
739end
740if ne lev 20 4 65536
741refuse 105 t
742end
743end
744end
745
746fn tpshdr chk ulong to void
747note Read the 128-byte header; chk 1 applies the FORMAT checks and fills ST and DG.
748rdn 0 128
749if eq chk 0
750ret
751end
752k = ulong get HB 6
753ok = true
754if ne lev 4 2 1
755ok = false
756end
757if ne ulong get HB 7 16
758ok = false
759end
760if ne lev 8 4 0
761ok = false
762end
763if ne lev 24 4 0
764ok = false
765end
766if ne lev 124 4 0
767ok = false
768end
769if lt k 2
770ok = false
771end
772if gt k 16
773ok = false
774end
775if not ok
776refuse 103 0
777end
778put ST 2 k
779put ST 0 lev 12 8
780put ST 3 add 24 mul k 2
781if ne lev 20 4 get ST 3
782refuse 103 0
783end
784for i ulong 32
785put DG add 64 i get HB add 92 i
786end
787end
788
789fn uadd a ulong b ulong to ulong
790note a + b, or ALARM 7 if the sum would wrap 2^64.
791if gt a sub 18446744073709551615 b
792alarm 7
793end
794ret add a b
795end
796
797fn umul a ulong b ulong to ulong
798note a * b, or ALARM 7 if the product would wrap 2^64.
799if eq a 0
800ret 0
801end
802if eq b 0
803ret 0
804end
805if gt a div 18446744073709551615 b
806alarm 7
807end
808ret mul a b
809end
810
811fn ncap n ulong to void
812note ARG when n is past the last count whose maximum upper sum fits in 64 bits.
813note 16005647 * n + 480000000 <= 2^64-1 for n = 1152514738906 and not for n + 1 (Wolfram W3).
814if gt n 1152514738906
815refuse 101 0
816end
817end
818
819fn bndsum idl ulong n ulong to ulong
820note ideal_ub + 5647*n + 480000000. ALARM 7 if that sum would wrap.
821ret uadd uadd idl umul n 5647 480000000
822end
823
824fn cubit cb ulong to ulong
825note 1000000 * coded_bits = cb * 8 * 1000000. ALARM 7 if that product would wrap.
826ret umul umul cb 8 1000000
827end
828
829fn tpsload a ulong to void
830note Validate TPS1 argument a fully (CODER_SPEC s.2 order). DG[0..32) = its digest,
831note DG[64..96) = its dataset digest, ST[0] = count, ST[2] = K, ST[3] = record bytes.
832sz = sopen 0 a
833if lt sz 160
834refuse 103 0
835end
836rdn 0 4
837if not magic 84 80 83 49
838refuse 103 0
839end
840srew 0
841if not hfile 0 0 sz 0
842refuse 104 0
843end
844srew 0
845tpshdr 1
846n = get ST 0
847note Before the body-size check. A short file whose header count is past the cap must refuse
848note ARG here; the size check would otherwise report FORMAT and the cap would be unreachable
849note without a multi-terabyte body.
850ncap n
851rb = get ST 3
852if gt n div sub sz 160 rb
853refuse 103 0
854end
855if ne mul n rb sub sz 160
856refuse 103 0
857end
858t = ulong 0
859while lt t n bound 9223372036854775807
860tpsrow t 1
861t = add t 1
862end
863end
864
865fn tsyload a ulong to void
866note Validate TSY1 argument a (s.3) and the pair check against the loaded TPS1.
867sz = sopen 1 a
868if lt sz 88
869refuse 103 0
870end
871rdn 1 4
872if not magic 84 83 89 49
873refuse 103 0
874end
875srew 1
876if not hfile 1 1 sz 96
877refuse 104 0
878end
879srew 1
880rdn 1 56
881k = ulong get HB 6
882ok = true
883if ne lev 4 2 1
884ok = false
885end
886if ne ulong get HB 7 0
887ok = false
888end
889if ne lev 16 8 0
890ok = false
891end
892if lt k 2
893ok = false
894end
895if gt k 16
896ok = false
897end
898if not ok
899refuse 103 0
900end
901n = lev 8 8
902if ne n sub sz 88
903refuse 110 0
904end
905for i ulong 32
906put DG add 128 i get HB add 24 i
907end
908put ST 4 k
909put ST 5 n
910t = ulong 0
911while lt t n bound 9223372036854775807
912b = rdb 1
913if ge b k
914refuse 116 t
915end
916t = add t 1
917end
918if ne k get ST 2
919refuse 110 0
920end
921if ne n get ST 0
922refuse 110 0
923end
924if not dgeq 64 128
925refuse 109 0
926end
927end
928
929note ---------------------------------------------------------------- arguments
930
931fn argget i ulong to void
932o = mul i 4096
933k = argread i AR o 4096
934if lt k 0
935refuse 101 0
936end
937put AL i ulong k
938end
939
940fn argis i ulong t view uchar * to bool
941if ne get AL i len t
942ret false
943end
944o = mul i 4096
945r = true
946for j ulong len t bound 4096
947if ne get AR add o j get t j
948r = false
949end
950end
951ret r
952end
953
954note ---------------------------------------------------------------- report
955
956fn gub q ulong to ulong
957note Upper bound, in ub, of 1e6 * log2(1 + 256/q): the most the last symbol of a row can gain
958note over its ideal (lower bound, TcrBound.lean encode_lower). Each ub is within
959note [-0.500192, +0.49984] of exact (Wolfram W2), so the difference plus 2 is safe; for q > 65280
960note the gain is below its value at 65281, 1e6 * log2(1 + 256/65281) = 5646.48 (Wolfram W3).
961if le q 65280
962ret add sub ub q ub add q 256 2
963end
964ret 5649
965end
966
967fn acc s ulong to void
968note Add symbol s (row Q) to the ideal length ST[1] and, for the last symbol of the row, to the
969note lower-bound gain ST[8].
970q = get Q s
971put ST 1 uadd get ST 1 ub q
972if eq s sub get ST 2 1
973put ST 8 uadd get ST 8 gub q
974end
975end
976
977fn report to void
978note ST: 0 n, 1 ideal_ub, 6 payload bytes, 7 renorm shifts, 8 last-symbol gain. Prints the length
979note lines and checks the identity and both bounds; a failure is an ALARM (exit 3).
980note n is capped in tpsload. cb above 2305843009213 is ARG: coded_bytes*8*1000000 would not fit.
981note The additions themselves alarm if they would wrap, which is what a bypassed cap would hit.
982n = get ST 0
983pay = get ST 6
984cb = uadd pay 56
985if gt cb 2305843009213
986refuse 101 0
987end
988pkv KCNT n
989pkv KIDL get ST 1
990pkv KCB cb
991pkv KCBT mul cb 8
992pkv KPAY pay
993pkv KSHF get ST 7
994bnd = bndsum get ST 1 n
995cub = cubit cb
996pkv KBND bnd
997if ne pay add get ST 7 4
998alarm 1
999end
1000if gt cub bnd
1001alarm 2
1002end
1003pkv KSLK sub bnd cub
1004lw = uadd get ST 1 472000000
1005g = uadd n get ST 8
1006lo = ulong 0
1007if gt lw g
1008lo = sub lw g
1009end
1010pkv KLOW lo
1011if lt cub lo
1012alarm 6
1013end
1014ptext KOVH
1015if ge cub get ST 1
1016pdec sub cub get ST 1
1017else
1018pbyte 45
1019pdec sub get ST 1 cub
1020end
1021pbyte 10
1022ptext KBOK
1023ptext KLOK
1024end
1025
1026fn hdrw t view uchar * to void
1027note The 56-byte coded header (s.4) into HB: magic t, version 1, coder 1, count, TPS1 digest, payload.
1028for i ulong 56
1029put HB i 0
1030end
1031for i ulong 4
1032put HB i get t i
1033end
1034put HB 4 1
1035put HB 5 1
1036n = get ST 0
1037for i ulong 8
1038put HB add 8 i uchar and shr n mul i 8 255
1039end
1040for i ulong 32
1041put HB add 16 i get DG i
1042end
1043p = get ST 6
1044for i ulong 8
1045put HB add 48 i uchar and shr p mul i 8 255
1046end
1047end
1048
1049text MTCR "TCR1"
1050
1051fn sameio d ulong i ulong to bool
1052r = false
1053for s ulong 3
1054if eq get INO mul s 2 d
1055if eq get INO add mul s 2 1 i
1056r = true
1057end
1058end
1059end
1060ret r
1061end
1062
1063fn ocreate a ulong to void
1064note Refuse (ARG) an output path that names one of the open inputs (same device and inode):
1065note O_TRUNC would destroy the input and the refusal cleanup would then remove it.
1066o = mul a 4096
1067k = stat AR o get AL a FS 0 128
1068if eq k 0
1069if sameio lefs 0 lefs 8
1070refuse 101 0
1071end
1072end
1073f = fopen AR o get AL a 577 420
1074if lt f 0
1075refuse 102 0
1076end
1077put OS 0 ulong f
1078put OS 1 0
1079put OS 3 0
1080put OS 4 1
1081put OS 5 a
1082end
1083
1084fn ofinish to void
1085note Flush, fsync and close. Refuse IO (102) on fsync or fclose failure before clearing
1086note OS[4], so cleanup still unlinks the partial output. After a successful close,
1087note zero OS[0] so the stale descriptor is not reused by a later fclose.
1088oflush
1089f = int get OS 0
1090k = fsync f
1091z = fclose f
1092if lt k 0
1093refuse 102 0
1094end
1095if lt z 0
1096refuse 102 0
1097end
1098put OS 0 0
1099put OS 4 0
1100end
1101
1102fn rehbeg s ulong h ulong d ulong to void
1103note Coding pass: re-read stream s from byte 0 through hash stream h (domain d).
1104srew s
1105hbeg h d
1106put HSEL s add h 1
1107end
1108
1109fn rehend s ulong h ulong o ulong w ulong to void
1110note End of a coding pass: skip the 32-byte trailer, finish hash h into DG[o..o+32) and refuse
1111note DIGEST unless it equals the validation-pass digest DG[w..w+32) (the file changed between
1112note the passes).
1113put HSEL s 0
1114rdn s 32
1115hend h o
1116if not dgeq o w
1117refuse 104 0
1118end
1119end
1120
1121note ---------------------------------------------------------------- modes
1122
1123fn cmdenc to int
1124tpsload 3
1125tsyload 4
1126ocreate 5
1127for i ulong 56
1128obyte 0
1129end
1130rehbeg 0 0 0
1131tpshdr 0
1132rehbeg 1 1 1
1133rdn 1 56
1134encbeg
1135n = get ST 0
1136k = get ST 2
1137put ST 1 0
1138put ST 8 0
1139t = ulong 0
1140while lt t n bound 9223372036854775807
1141tpsrow t 1
1142s = rdb 1
1143if ge s k
1144refuse 116 t
1145end
1146e = encs s
1147if ne e 0
1148refuse e t
1149end
1150acc s
1151t = add t 1
1152end
1153e = encend
1154if ne e 0
1155refuse e n
1156end
1157rehend 0 0 192 0
1158rehend 1 1 224 96
1159put ST 6 get EN 6
1160put ST 7 get EN 5
1161oflush
1162hdrw MTCR
1163f = int get OS 0
1164w = pwrite f HB 0 56 long 0
1165if ne w 56
1166refuse 102 0
1167end
1168ofinish
1169ptext KMODE
1170ptext WENC
1171pbyte 10
1172ptext KTPS
1173phex 0
1174pbyte 10
1175report
1176pflush 1
1177ret 0
1178end
1179
1180fn codhdr a ulong to void
1181note Check the coded file header of argument a as stream 2 (s.4 order); DC[3] = payload bytes.
1182sz = sopen 2 a
1183if lt sz 56
1184refuse 111 0
1185end
1186rdn 2 56
1187if not magic 84 67 82 49
1188refuse 111 0
1189end
1190if ne ulong get HB 4 1
1191refuse 111 0
1192end
1193if ne ulong get HB 5 1
1194refuse 111 0
1195end
1196if ne lev 6 2 0
1197refuse 111 0
1198end
1199for i ulong 32
1200put DG add 32 i get HB add 16 i
1201end
1202if not dgeq 0 32
1203refuse 112 0
1204end
1205if ne lev 8 8 get ST 0
1206refuse 110 0
1207end
1208p = lev 48 8
1209if lt sub sz 56 p
1210refuse 113 0
1211end
1212if gt sub sz 56 p
1213refuse 114 0
1214end
1215put DC 3 p
1216put DC 4 0
1217end
1218
1219fn tsyhdr w ulong to void
1220note The TSY1 header of the decoded symbols (K, count, dataset digest of the TPS1), fed to hash
1221note stream 1 and, when w = 1, written to the output.
1222for i ulong 56
1223put HB i 0
1224end
1225put HB 0 84
1226put HB 1 83
1227put HB 2 89
1228put HB 3 49
1229put HB 4 1
1230put HB 6 uchar get ST 2
1231n = get ST 0
1232for i ulong 8
1233put HB add 8 i uchar and shr n mul i 8 255
1234end
1235for i ulong 32
1236put HB add 24 i get DG add 64 i
1237end
1238for i ulong 56
1239b = ulong get HB i
1240if eq w 1
1241obyte b
1242end
1243hbyte 1 b
1244end
1245end
1246
1247fn decrun w ulong to void
1248note Decode all symbols into hash stream 1 (and, w = 1, the output file). Fills ST[1] (ideal),
1249note ST[6], ST[7], ST[8]. The TPS1 is re-hashed and its rows re-checked on the way.
1250rehbeg 0 0 0
1251tpshdr 0
1252e = decbeg
1253if ne e 0
1254refuse e 0
1255end
1256n = get ST 0
1257put ST 1 0
1258put ST 8 0
1259t = ulong 0
1260while lt t n bound 9223372036854775807
1261tpsrow t 1
1262s = decs
1263if gt s 999
1264refuse sub s 1000 t
1265end
1266if eq w 1
1267obyte s
1268end
1269hbyte 1 s
1270acc s
1271t = add t 1
1272end
1273e = decend
1274if ne e 0
1275refuse e n
1276end
1277rehend 0 0 192 0
1278put ST 6 get DC 3
1279put ST 7 get DC 5
1280end
1281
1282fn cmddec to int
1283tpsload 3
1284codhdr 4
1285ocreate 5
1286hbeg 1 1
1287tsyhdr 1
1288decrun 1
1289hend 1 96
1290for i ulong 32
1291obyte ulong get DG add 96 i
1292end
1293ofinish
1294ptext KMODE
1295ptext WDEC
1296pbyte 10
1297ptext KTSY
1298phex 96
1299pbyte 10
1300report
1301pflush 1
1302ret 0
1303end
1304
1305fn cmdver to int
1306note Same order as turing-coder verify: probability stream, coded file and full decode, then the
1307note symbol file and the pair check, then byte identity of the symbol file with the decoded one
1308note (compared through their TSY1 digests: equal digests of domain || header || symbols).
1309tpsload 3
1310codhdr 5
1311hbeg 1 1
1312tsyhdr 0
1313decrun 0
1314hend 1 160
1315tsyload 4
1316if not dgeq 160 96
1317refuse 116 0
1318end
1319ptext KMODE
1320ptext WVER
1321pbyte 10
1322ptext KTPS
1323phex 0
1324pbyte 10
1325report
1326ptext KRT
1327pflush 1
1328ret 0
1329end
1330
1331note ---------------------------------------------------------------- selftest (synthetic streams in memory)
1332
1333fn rnd to ulong
1334x = add mul get RG 0 6364136223846793005 1442695040888963407
1335put RG 0 x
1336ret shr x 33
1337end
1338
1339fn genrow k ulong md ulong to void
1340note Row Q[0..k) summing to 65536, every entry >= 1. md 0 random weights, 1 one dominant
1341note entry (the rest 1), 2 two-level skew, 3 uniform-ish.
1342if eq md 1
1343for x ulong k bound 16
1344put Q x 1
1345end
1346d = rem rnd k
1347put Q d sub 65537 k
1348ret
1349end
1350w = new ulong 16 0
1351tot = ulong 0
1352for x ulong k bound 16
1353v = add rem rnd 1000 1
1354if eq md 2
1355if eq rem rnd 4 0
1356v = add v 40000
1357end
1358end
1359if eq md 3
1360v = ulong 1000
1361end
1362put w x v
1363tot = add tot v
1364end
1365rest = sub 65536 k
1366used = ulong 0
1367for x ulong k bound 16
1368v = add div mul get w x rest tot 1
1369put Q x v
1370used = add used v
1371end
1372d = rem rnd k
1373put Q d add get Q d sub 65536 used
1374end
1375
1376fn gensym k ulong md ulong to ulong
1377note md 0: sample from Q; md 1: the least likely symbol (worst information per symbol);
1378note md 2: always the last symbol of the row (the lower side of the bound).
1379if eq md 2
1380ret sub k 1
1381end
1382if eq md 1
1383b = ulong 0
1384for x ulong k bound 16
1385if lt get Q x get Q b
1386b = x
1387end
1388end
1389ret b
1390end
1391u = rem rnd 65536
1392c = ulong 0
1393for x ulong k bound 16
1394c = add c get Q x
1395if lt u c
1396ret x
1397end
1398end
1399ret sub k 1
1400end
1401
1402fn stenc seed ulong n ulong k ulong md ulong to ulong
1403note Encode case (seed, n, k, md) into MB; returns ideal_ub (gain in ST[8]). md: low 2 bits row
1404note mode, md >> 2 symbol mode.
1405put RG 0 seed
1406put ST 2 k
1407put OS 3 1
1408put OS 2 0
1409encbeg
1410put ST 1 0
1411put ST 8 0
1412t = ulong 0
1413while lt t n bound 9223372036854775807
1414genrow k and md 3
1415s = gensym k shr md 2
1416e = encs s
1417if ne e 0
1418alarm 3
1419end
1420acc s
1421t = add t 1
1422end
1423if ne encend 0
1424alarm 3
1425end
1426ret get ST 1
1427end
1428
1429fn stdec seed ulong n ulong k ulong md ulong to ulong
1430note Decode MB[0..OS[2]) against the regenerated case; 0 = identical and terminated, 1 = refused or different.
1431put RG 0 seed
1432put ST 2 k
1433put DC 3 get OS 2
1434put DC 4 1
1435if ne decbeg 0
1436ret 1
1437end
1438t = ulong 0
1439while lt t n bound 9223372036854775807
1440genrow k and md 3
1441s = gensym k shr md 2
1442d = decs
1443if ne d s
1444ret 1
1445end
1446t = add t 1
1447end
1448if ne decend 0
1449ret 1
1450end
1451ret 0
1452end
1453
1454fn flip pay ulong to void
1455note XOR one random payload byte of MB[0..pay) with a nonzero value.
1456p = rem rnd pay
1457x = add rem rnd 255 1
1458put MB p uchar xor ulong get MB p x
1459end
1460
1461fn stlow n ulong idl ulong cub ulong to void
1462note Lower bound for a selftest case (ST[8] = last-symbol gain), its smallest slack (ST[27]) and
1463note the largest amount any case codes below its ideal (ST[28]).
1464lw = uadd idl 472000000
1465g = uadd n get ST 8
1466lo = ulong 0
1467if gt lw g
1468lo = sub lw g
1469end
1470if lt cub lo
1471alarm 6
1472end
1473if lt sub cub lo get ST 27
1474put ST 27 sub cub lo
1475end
1476if gt idl cub
1477if gt sub idl cub get ST 28
1478put ST 28 sub idl cub
1479end
1480end
1481end
1482
1483fn stcase seed ulong n ulong k ulong md ulong to void
1484note One case: round trip, length identity, both bounds, then flip one payload byte and require
1485note detection.
1486idl = stenc seed n k md
1487pay = get OS 2
1488if ne pay add get EN 5 4
1489alarm 1
1490end
1491bnd = bndsum idl n
1492cub = cubit uadd pay 56
1493if gt cub bnd
1494alarm 2
1495end
1496stlow n idl cub
1497if lt sub bnd cub get ST 20
1498put ST 20 sub bnd cub
1499end
1500if gt n 0
1501if lt sub bnd cub get ST 24
1502put ST 24 sub bnd cub
1503end
1504end
1505if ne stdec seed n k md 0
1506alarm 3
1507end
1508if ne get DC 3 add get DC 5 4
1509alarm 1
1510end
1511flip pay
1512if eq stdec seed n k md 0
1513alarm 4
1514end
1515put ST 21 add get ST 21 1
1516put ST 22 add get ST 22 1
1517put ST 23 add get ST 23 n
1518end
1519
1520fn ubchk to void
1521note Exhaustive check of the s.8 ub rule: sum of ub(q) and sum of q * ub(q) mod 2^64 over
1522note q = 1..65535 must equal the values Wolfram computed from the same rule
1523note (docs/turing/TURING_YIELD_RANGE_CODER_OM.md, receipt W2): 94539136349 and 1549081999916376.
1524note The same Wolfram run gives max(exact - ub) = 0.49984 ub, inside the 0.501 the bound uses.
1525s1 = ulong 0
1526s2 = ulong 0
1527q = ulong 1
1528while lt q 65536 bound 65535
1529v = ub q
1530s1 = add s1 v
1531s2 = add s2 mul q v
1532q = add q 1
1533end
1534put ST 25 s1
1535put ST 26 s2
1536if ne s1 94539136349
1537alarm 5
1538end
1539if ne s2 1549081999916376
1540alarm 5
1541end
1542end
1543
1544fn ceilchk to void
1545note The last legal n and the last legal coded length still fit. cmdovf is one past the n cap.
1546note A constant that no longer fits alarms here and selftest does not pass.
1547ncap 1152514738906
1548z = bndsum umul 1152514738906 16000000 1152514738906
1549y = cubit 2305843009213
1550if eq z 0
1551alarm 7
1552end
1553if eq y 0
1554alarm 7
1555end
1556end
1557
1558fn cmdovf to int
1559note n = cap+1 at ub(q=1) per symbol. 16005647*(cap+1) + 480000000 does not fit in 64 bits.
1560note Must ALARM 7. Returning 0 means the sum wrapped or the check was removed.
1561z = bndsum umul 1152514738907 16000000 1152514738907
1562put ST 29 z
1563ret 0
1564end
1565
1566fn cmdself to int
1567note Cases: K 2..16, all four row modes, three symbol modes, n from 0 to 200000.
1568ceilchk
1569put ST 20 18446744073709551615
1570put ST 24 18446744073709551615
1571put ST 27 18446744073709551615
1572put ST 28 0
1573ubchk
1574put ST 21 0
1575put ST 22 0
1576put ST 23 0
1577seed = ulong 1
1578for k ulong 15
1579for md ulong 12
1580stcase seed 0 add k 2 md
1581seed = add seed 1
1582stcase seed 1 add k 2 md
1583seed = add seed 1
1584stcase seed 37 add k 2 md
1585seed = add seed 1
1586stcase seed 4000 add k 2 md
1587seed = add seed 1
1588end
1589end
1590stcase 777 200000 9 0
1591stcase 778 200000 2 4
1592stcase 779 200000 16 5
1593stcase 780 200000 16 7
1594stcase 781 200000 16 9
1595stcase 782 200000 2 9
1596ptext KUB
1597pdec get ST 25
1598ptext KUBQ
1599pdec get ST 26
1600pbyte 10
1601ptext KCASE
1602pdec get ST 22
1603ptext KSYM
1604pdec get ST 23
1605ptext KFLIP
1606pdec get ST 21
1607ptext KMIN
1608pdec get ST 20
1609ptext KMIN1
1610pdec get ST 24
1611ptext KLMIN
1612pdec get ST 27
1613ptext KBELOW
1614pdec get ST 28
1615ptext KPASS
1616pflush 1
1617ret 0
1618end
1619
1620fn main to int
1621for i ulong 8
1622put INO i 18446744073709551615
1623end
1624c = argcnt
1625if eq c 2
1626argget 1
1627if argis 1 WSLF
1628ret cmdself
1629end
1630end
1631if eq c 3
1632argget 1
1633if argis 1 WSLF
1634argget 2
1635if argis 2 WOVF
1636ret cmdovf
1637end
1638end
1639end
1640if ne c 6
1641ptext TUSE
1642pflush 2
1643ret 1
1644end
1645for i ulong 6
1646argget i
1647end
1648if not argis 2 WRNG
1649ptext TUSE
1650pflush 2
1651ret 1
1652end
1653if argis 1 WENC
1654ret cmdenc
1655end
1656if argis 1 WDEC
1657ret cmddec
1658end
1659if argis 1 WVER
1660ret cmdver
1661end
1662ptext TUSE
1663pflush 2
1664ret 1
1665end