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 / train / aien-train / native / neuralese / nl1r-task / evidence / boundary-attempt4.om

1368 lines · 27371 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Host synthetic one-shot CLI: receiver-context-file expected-op expected-operand.
3note This harness cannot authorize effects or reserve a production microjob.
4om2 debug
5note Closed compiled ordinary Omega operations, never dynamically execute vector bytes.
6fn nlinc x uint to uint
7requires le x 16
8ensures le result 32
9ret add x 1
10end
11fn nldouble x uint to uint
12requires le x 16
13ensures le result 32
14ret mul x 2
15end
16fn nlident x uint to uint
17requires le x 16
18ensures le result 32
19ret x
20end
21text nlrhead "NL4R1 0 "
22text nlrtail " 1 1 0 1 0 "
23text nlrno "NL4R1 "
24text nlzeros " 0 0 0 0 0 0 0 0 0\n"
25text nlendl "\n"
26text nlspace " "
27fn nlout b view uchar * fd int to void
28pos = ulong 0
29while lt pos len b bound 8192
30r = write fd b pos sub len b pos
31if le r 0
32exit 81
33end
34pos = add pos ulong r
35end
36end
37fn nlnumber v uint fd int to void
38b = new uchar 10 0
39pos = ulong 10
40x = v
41while true bound 10
42pos = sub pos 1
43put b pos uchar add rem x 10 48
44x = div x 10
45if eq x 0
46break
47end
48end
49nlout part b pos sub 10 pos fd
50end
51fn nlfail code int to int
52nlout nlrno 2
53nlnumber uint code 2
54nlout nlzeros 2
55ret code
56end
57fn nlread fd int b edit uchar * to long
58pos = ulong 0
59while lt pos len b bound 5121
60r = read fd b pos sub len b pos
61if lt r 0
62ret r
63end
64if eq r 0
65break
66end
67pos = add pos ulong r
68end
69ret long pos
70end
71fn nlcontext c edit uchar 1025 to int
72path = new uchar 4096 0
73n = argread 1 path 0 4096
74if le n 0
75ret 80
76end
77fd = fopen path 0 ulong n 0 0
78if lt fd 0
79ret 80
80end
81r = nlread int fd c
82cl = fclose int fd
83if or ne r 1024 ne cl 0
84ret 80
85end
86ret 0
87end
88fn nlrequest e edit uint 16 to int
89argbuf = new uchar 2 0
90r = argread 2 argbuf 0 2
91if ne r 1
92ret 80
93end
94v = get argbuf 0
95if or lt v 49 gt v 51
96ret 80
97end
98put e 0 uint sub v 48
99r = argread 3 argbuf 0 2
100if ne r 1
101ret 80
102end
103v = get argbuf 0
104if or lt v 48 gt v 51
105ret 80
106end
107put e 1 uint sub v 48
108if eq get e 0 1
109put e 2 1
110end
111if eq get e 0 2
112put e 2 2
113end
114put e 3 1
115put e 4 1
116put e 5 1
117put e 6 4
118put e 7 1
119put e 8 0
120put e 9 1
121ret 0
122end
123fn nlexec p view uint 16 to void
124z = uint 0
125if eq get p 0 1
126z = nlinc get p 1
127end
128if eq get p 0 2
129z = nldouble get p 1
130end
131if eq get p 0 3
132z = nlident get p 1
133end
134nlout nlrhead 1
135for i ulong 3
136if ne i 0
137nlout nlspace 1
138end
139nlnumber get p i 1
140end
141nlout nlrtail 1
142nlnumber z 1
143nlout nlendl 1
144end
145note SPDX-License-Identifier: AGPL-3.0-only
146note Verification adapter, not a receiver or authority. Reuses unchanged NL4 functions.
147note Compose after host-proposal.om lines1..144, before pinned h/proposal/v/sha2.
148note CLI context-file predicted-bits trusted-target-bits task abstain frame-output-fd.
149note Trusted target is evaluator-owned and never flows back to sender or receiver.
150global BARG uchar 5
151global BCFG ulong 5
152fn barg ix ulong to ulong
153n = argread ix BARG 0 5
154if or le n 0 gt n 4
155exit 80
156end
157v = ulong 0
158for i ulong ulong n
159c = get BARG i
160if or lt c 48 gt c 57
161exit 80
162end
163v = add mul v 10 ulong sub c 48
164end
165ret v
166end
167fn bargs to int
168put BCFG 0 barg 2
169put BCFG 1 barg 3
170put BCFG 2 barg 4
171put BCFG 3 barg 6
172put BCFG 4 barg 5
173if or gt get BCFG 0 15 gt get BCFG 1 15
174ret 80
175end
176if or gt get BCFG 2 2 gt get BCFG 4 1
177ret 80
178end
179if or lt get BCFG 3 3 gt get BCFG 3 1023
180ret 80
181end
182if eq get BCFG 2 1
183if or gt get BCFG 0 1 gt get BCFG 1 1
184ret 80
185end
186end
187if eq get BCFG 4 1
188ret 2
189end
190if eq get BCFG 2 1
191put BCFG 2 1
192else
193put BCFG 2 2
194end
195ret 0
196end
197fn bheader frame edit uchar 1056 ctx view uchar 1024 to void
198for i ulong 1024
199put frame i get ctx i
200end
201note Fresh one-shot SYNTHETIC sequence, checked with existing lastseq0 rule.
202for i ulong 8
203put frame add 80 i uchar 0
204end
205put frame 87 uchar 1
206end
207fn bexpect e edit uint 16 value uint to void
208put e 0 3
209put e 1 value
210put e 3 1
211put e 4 1
212put e 5 1
213put e 6 4
214put e 7 1
215put e 9 1
216end
217fn bframe frame edit uchar 1056 value ulong to int
218f = new f64 4 0.0f64
219put f 0 1.0f64
220put f 1 1.0f64
221put f 2 f64 and value 1
222put f 3 f64 and shr value 1 1
223wire = new uchar 32 0
224rc = nlencode f wire
225if ne rc 0
226ret rc
227end
228for i ulong 32
229put frame add 1024 i get wire i
230end
231state = new ulong 40 0
232sha256_init state
233sha256_update state part frame 0 992
234sha256_update state part frame 1024 32
235sha256_final state part frame 992 32
236note Raw actual envelope bytes are audit output, not an execution receipt.
237z = write int get BCFG 3 frame 0 1056
238if ne z 1056
239ret 81
240end
241ret 0
242end
243fn bverify frame edit uchar 1056 ctx view uchar 1024 p edit uint 16 to int
244value = and shr get BCFG 0 mul get BCFG 4 2 3
245rc = bframe frame value
246if ne rc 0
247ret rc
248end
249now = lpbe64 ctx 64
250rc = lpvalid frame ctx now 0
251if ne rc 0
252ret rc
253end
254rc = nlproposal frame p
255if ne rc 0
256ret rc
257end
258e = new uint 16 0
259sh = mul get BCFG 4 2
260want = and shr get BCFG 1 sh 3
261bexpect e uint want
262if not nlmatch p e
263ret 49
264end
265ret 0
266end
267fn main to int
268if ne argcnt 7
269ret nlfail 80
270end
271rc = bargs
272if ne rc 0
273ret nlfail rc
274end
275ctx = new uchar 1025 0
276frame = new uchar 1056 0
277props = new uint 32 0
278rc = nlcontext ctx
279if ne rc 0
280ret nlfail rc
281end
282bheader frame part ctx 0 1024
283for chunk ulong get BCFG 2
284put BCFG 4 chunk
285rc = bverify frame part ctx 0 1024 part props mul chunk 16 16
286if ne rc 0
287ret nlfail rc
288end
289end
290note Every chunk must verify BEFORE any pure execution or execution receipt.
291for chunk ulong get BCFG 2
292nlexec part props mul chunk 16 16
293end
294ret 0
295end
296note SPDX-License-Identifier: AGPL-3.0-only
297note NL1 G0 fixed eight-coordinate mathematical decoder, not a learned channel.
298note Input scores propose information only. No effects, threshold, or authority.
299global NM f64 8
300global NF f64 4
301global NT f64 4
302
303fn ndcol j ulong to ulong
304if eq j 0
305ret 1
306end
307if eq j 1
308ret 2
309end
310if eq j 2
311ret 4
312end
313ret 7
314end
315
316fn ndsign r ulong c ulong to f64
317x = and r c
318p = ulong 0
319for b ulong 3
320p = xor p and shr x b 1
321end
322if eq p 0
323ret 1.0f64
324end
325ret -1.0f64
326end
327
328fn ndelem r ulong j ulong to f64
329ret div ndsign r ndcol j fsqrt 8.0f64
330end
331
332fn nddec n ulong to int
333if ne n 8
334ret 1
335end
336note Ordered comparison rejects NaN, infinities, and outside the frozen range.
337for r ulong 8
338if not le fabs get NM r 4.0f64
339ret 2
340end
341end
342for j ulong 4
343v = 0.0f64
344for r ulong 8
345v = add v mul get NM r ndelem r j
346end
347put NT j v
348end
349for j ulong 4
350put NF j get NT j
351end
352ret 0
353end
354note SPDX-License-Identifier: Apache-2.0
355note NL4 consumes NL3 lpvalid and NL1 nddec; neither is execution authority.
356note Caller must run lpvalid against receiver-owned context before this decoder.
357note No host words, executable payload, capability, allocation from input or effects.
358note Concatenate after an om2 debug entry, NL1 hadamard.om and NL3 validator.om.
359fn nlencode f view f64 4 wire edit uchar 32 to int
360stage = new uint 8 0
361for i ulong 4
362if not and ge get f i 0.0f64 le get f i 1.0f64
363ret 50
364end
365end
366for i ulong 8
367v = 0.0f64
368for j ulong 4
369v = add v mul get f j ndelem i j
370end
371put stage i bitcast uint f32 v
372end
373for i ulong 8
374u = get stage i
375off = mul i 4
376for j ulong 4
377sh = mul sub ulong 3 j 8
378c = uchar and shr u sh 255
379put wire add off j c
380end
381end
382ret 0
383end
384fn nlbe32 a view uchar * off ulong to uint
385v = uint 0
386for i ulong 4
387v = or shl v 8 uint get a add off i
388end
389ret v
390end
391fn nlbit v f64 to int
392note Closed synthetic codebook tolerance; ambiguous and nonfinite scores refuse.
393if and ge v -0.0625f64 le v 0.0625f64
394ret 0
395end
396if and ge v 0.9375f64 le v 1.0625f64
397ret 1
398end
399ret -1
400end
401fn nlproposal a view uchar * p edit uint 16 to int
402note This function proposes only; it does not validate context, reserve or execute.
403b = new uint 4 0
404if lt len a 1024
405ret 40
406end
407if ne get a 5 1
408ret 41
409end
410dtype = get a 10
411width = uint 4
412if eq dtype 2
413width = 1
414else
415if ne dtype 1
416ret 41
417end
418end
419if ne len a add ulong 1024 ulong mul width 8
420ret 40
421end
422if or ne get a 11 1 ne nlbe32 a 24 8
423ret 42
424end
425if or ne nlbe32 a 16 mul width 8 ne nlbe32 a 20 8
426ret 42
427end
428if ne get a 101 0
429ret 43
430end
431if lt nlbe32 a 104 1
432ret 44
433end
434factor = 1.0f64
435zero = int 0
436if eq dtype 2
437scale = nlbe32 a 56
438zero = bitcast int nlbe32 a 60
439if or eq scale 0 ge scale 0x7f800000
440ret 45
441end
442if or lt zero -128 gt zero 127
443ret 45
444end
445factor = f64 bitcast f32 scale
446end
447for i ulong 8
448if eq dtype 1
449u = nlbe32 a add 1024 mul i 4
450note Exponent255 includes every NaN and infinity, without floating comparison.
451if eq and shr u 23 255 255
452ret 45
453end
454put NM i f64 bitcast f32 u
455else
456q = int get a add 1024 i
457if ge q 128
458q = sub q 256
459end
460put NM i mul f64 sub q zero factor
461end
462end
463r = nddec 8
464if ne r 0
465ret 46
466end
467for i ulong 4
468v = nlbit get NF i
469if lt v 0
470ret 47
471end
472put b i uint v
473end
474op = add get b 0 mul get b 1 2
475if eq op 0
476ret 48
477end
478arg = add get b 2 mul get b 3 2
479note All checks precede output writes: refusal preserves the caller's proposal.
480for i ulong 16
481put p i 0
482end
483put p 0 op
484put p 1 arg
485if eq op 1
486put p 2 1
487end
488if eq op 2
489put p 2 2
490end
491put p 3 1
492put p 4 1
493put p 5 1
494put p 6 4
495put p 7 1
496put p 8 0
497put p 9 1
498ret 0
499end
500fn nlmatch p view uint 16 e view uint 16 to bool
501note Independent receiver expectation, never sourced from the numerical message.
502for i ulong 16
503if ne get p i get e i
504ret false
505end
506end
507ret true
508end
509note SPDX-License-Identifier: Apache-2.0
510note LTP0 pure admission-template validator. No main, effects or authority.
511note Compose after om2 debug and before pinned om/tls/sha2.om.
512fn lpbe64 a view uchar * off ulong to ulong
513v = ulong 0
514for i ulong 8
515v = or shl v 8 ulong get a add off i
516end
517ret v
518end
519fn lpbe32 a view uchar * off ulong to ulong
520v = ulong 0
521for i ulong 4
522v = or shl v 8 ulong get a add off i
523end
524ret v
525end
526fn lpeq a view uchar * c view uchar * off ulong n ulong to bool
527for i ulong n
528p = add off i
529if ne get a p get c p
530ret false
531end
532end
533ret true
534end
535fn lpnonzero a view uchar * off ulong n ulong to bool
536v = uchar 0
537for i ulong n
538v = or v get a add off i
539end
540ret ne v 0
541end
542fn lpenums a view uchar * to bool
543if or lt get a 5 1 gt get a 5 2
544ret false
545end
546if or ne get a 6 1 ne get a 7 1
547ret false
548end
549if or ne get a 14 1 ne get a 15 0
550ret false
551end
552if or lt get a 100 1 gt get a 100 3
553ret false
554end
555if gt get a 101 2
556ret false
557end
558if or lt get a 102 2 gt get a 102 3
559ret false
560end
561ret true
562end
563fn lpquant a view uchar * to bool
564scale = lpbe32 a 56
565zero = lpbe32 a 60
566if eq get a 10 1
567if or ne get a 12 1 ne get a 13 0
568ret false
569end
570ret and eq scale 0x3f800000 eq zero 0
571end
572if eq get a 10 2
573if or ne get a 12 5 ne get a 13 1
574ret false
575end
576if or eq scale 0 ge scale 0x7f800000
577ret false
578end
579ret or le zero 127 ge zero 0xffffff80
580end
581ret false
582end
583fn lpshape a view uchar * to bool
584rank = ulong get a 11
585srank = ulong get a 103
586if or lt rank 1 gt rank 4
587ret false
588end
589if or lt srank 1 gt srank 4
590ret false
591end
592for i ulong 4
593sdim = lpbe32 a add 112 mul i 4
594if lt i srank
595if eq sdim 0
596ret false
597end
598else
599if ne sdim 0
600ret false
601end
602end
603end
604width = ulong 1
605if eq get a 10 1
606width = 4
607end
608prod = ulong 1
609stride = width
610for k ulong 4
611i = sub 3 k
612dim = lpbe32 a add 24 mul i 4
613st = lpbe32 a add 40 mul i 4
614if ge i rank
615if or ne dim 0 ne st 0
616ret false
617end
618else
619if or eq dim 0 gt dim 4096
620ret false
621end
622if gt prod div ulong 4096 dim
623ret false
624end
625if ne st stride
626ret false
627end
628prod = mul prod dim
629stride = mul prod width
630end
631end
632ret eq prod lpbe32 a 20
633end
634fn lpsize a view uchar * to bool
635n = lpbe32 a 16
636cap = lpbe32 a 108
637if or eq n 0 gt n 4096
638ret false
639end
640if or eq cap 0 gt cap 4096
641ret false
642end
643if gt n cap
644ret false
645end
646if ne len a add 1024 n
647ret false
648end
649width = ulong 1
650if eq get a 10 1
651width = 4
652end
653ret eq n mul lpbe32 a 20 width
654end
655fn lpids a view uchar * to bool
656if or eq lpbe32 a 88 0 eq lpbe32 a 96 0
657ret false
658end
659if eq lpbe32 a 104 0
660ret false
661end
662for i ulong 2
663if not lpnonzero a add 128 mul i 16 16
664ret false
665end
666end
667for i ulong 18
668if not lpnonzero a add 160 mul i 32 32
669ret false
670end
671end
672for i ulong 2
673if not lpnonzero a add 736 mul i 16 16
674ret false
675end
676end
677for i ulong 3
678if not lpnonzero a add 768 mul i 32 32
679ret false
680end
681end
682ret true
683end
684fn lpfinite a view uchar * to bool
685if eq get a 10 1
686n = lpbe32 a 20
687for i ulong n
688bits = lpbe32 a add 1024 mul i 4
689if eq and bits 0x7f800000 0x7f800000
690ret false
691end
692end
693end
694ret true
695end
696fn lphash a view uchar * to bool
697state = new ulong 40 0
698digest = new uchar 32 0
699sha256_init state
700sha256_update state part a 0 992
701sha256_update state part a 1024 sub len a 1024
702sha256_final state digest
703for i ulong 32
704if ne get digest i get a add 992 i
705ret false
706end
707end
708ret true
709end
710fn lpvalid a view uchar * c view uchar * now ulong lastseq ulong to int
711if or lt len a 1024 gt len a 5120
712ret 10
713end
714if ne len c 1024
715ret 10
716end
717if ne lpbe32 a 0 0x4c545030
718ret 11
719end
720if or ne get a 4 0 ne get a 8 4
721ret 11
722end
723if ne get a 9 0
724ret 11
725end
726if not lpenums a
727ret 12
728end
729if not lpquant a
730ret 13
731end
732if not lpshape a
733ret 14
734end
735if not lpsize a
736ret 15
737end
738for i ulong 128
739if ne get a add 864 i 0
740ret 16
741end
742end
743if not lpids a
744ret 17
745end
746issued = lpbe64 a 64
747expiry = lpbe64 a 72
748if or eq issued 0 gt issued now
749ret 18
750end
751if or le expiry now le expiry issued
752ret 18
753end
754if gt expiry lpbe64 c 72
755ret 18
756end
757if le lpbe64 a 80 lastseq
758ret 19
759end
760if not lpeq a c 0 16
761ret 20
762end
763if not lpeq a c 24 40
764ret 20
765end
766if not lpeq a c 88 776
767ret 20
768end
769if not lpfinite a
770ret 21
771end
772if not lphash a
773ret 22
774end
775ret 0
776end
777note SPDX-License-Identifier: Apache-2.0
778note om/tls/sha2.om: SHA-256, SHA-384, SHA-512 (FIPS 180-4) streaming, as an OM-2 library unit
779note (no om2 line, no main, no globals). Module code SH; functions sha2_*, sha256_*, sha384_*, sha512_*.
780note
781note API
782note sha256_init c edit ulong * c: 40 ulong cells (caller owns all state)
783note sha256_update c edit ulong * m view uchar * any length, any chunking
784note sha256_final c edit ulong * o edit uchar * writes 32 bytes (o shorter traps 3)
785note sha384_* / sha512_* the same; final writes 48 / 64 bytes
786note sha2_copy d edit ulong * s view ulong * copies len s cells (a context snapshot)
787note sha2_digest alg ulong m view uchar * o edit uchar * to ulong
788note alg 256|384|512: one-shot digest into o, returns 32|48|64; any other alg returns 0, writes nothing
789note After final the context is spent (its state cells hold the last chaining value); init it again.
790note A context initialised for one algorithm must be used with that algorithm's update/final; the
791note common code reads the algorithm from cell 9, so sha256_update on a sha512 context hashes SHA-512.
792note
793note Context cells: 0..7 chaining value H (32-bit words zero-extended for SHA-256), 8 = bytes absorbed
794note mod 2^64, 9 = algorithm (256, 384, 512), 16..31 = pending block bytes packed big-endian (byte i
795note in cell 16 + i/8, bits 56 - 8*(i mod 8)), 10..15 and 32..39 = 0.
796note Constant time: every branch, loop count and index depends only on lengths and the algorithm,
797note never on message or state bytes. Local schedule/state/padding arrays are zeroed before return
798note (HMAC keys pass through them). omc emits plain AArch64; MUL/ADD/EOR/ROR/shift data-independent.
799note Message length: any length below 2^64 bytes; update cuts its input into runs of 2^25 bytes so no
800note loop passes 2^20 times (a rolled for traps 4 after 10^6 passes; the while bound 999999 of runs
801note allows inputs up to 999999 * 32 MiB per update call).
802
803note K{256}[0..64): first 32 bits of the fractional parts of the cube roots of the first 64 primes
804note (FIPS 180-4 4.2.2). Same values as om/sha256/sha256-om2.om; recomputed with Wolfram:
805note IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^32],16,8] for i = 1..64.
806fn sha2_k256 k edit uint 64 to void
807put k 0 0x428a2f98
808put k 1 0x71374491
809put k 2 0xb5c0fbcf
810put k 3 0xe9b5dba5
811put k 4 0x3956c25b
812put k 5 0x59f111f1
813put k 6 0x923f82a4
814put k 7 0xab1c5ed5
815put k 8 0xd807aa98
816put k 9 0x12835b01
817put k 10 0x243185be
818put k 11 0x550c7dc3
819put k 12 0x72be5d74
820put k 13 0x80deb1fe
821put k 14 0x9bdc06a7
822put k 15 0xc19bf174
823put k 16 0xe49b69c1
824put k 17 0xefbe4786
825put k 18 0x0fc19dc6
826put k 19 0x240ca1cc
827put k 20 0x2de92c6f
828put k 21 0x4a7484aa
829put k 22 0x5cb0a9dc
830put k 23 0x76f988da
831put k 24 0x983e5152
832put k 25 0xa831c66d
833put k 26 0xb00327c8
834put k 27 0xbf597fc7
835put k 28 0xc6e00bf3
836put k 29 0xd5a79147
837put k 30 0x06ca6351
838put k 31 0x14292967
839put k 32 0x27b70a85
840put k 33 0x2e1b2138
841put k 34 0x4d2c6dfc
842put k 35 0x53380d13
843put k 36 0x650a7354
844put k 37 0x766a0abb
845put k 38 0x81c2c92e
846put k 39 0x92722c85
847put k 40 0xa2bfe8a1
848put k 41 0xa81a664b
849put k 42 0xc24b8b70
850put k 43 0xc76c51a3
851put k 44 0xd192e819
852put k 45 0xd6990624
853put k 46 0xf40e3585
854put k 47 0x106aa070
855put k 48 0x19a4c116
856put k 49 0x1e376c08
857put k 50 0x2748774c
858put k 51 0x34b0bcb5
859put k 52 0x391c0cb3
860put k 53 0x4ed8aa4a
861put k 54 0x5b9cca4f
862put k 55 0x682e6ff3
863put k 56 0x748f82ee
864put k 57 0x78a5636f
865put k 58 0x84c87814
866put k 59 0x8cc70208
867put k 60 0x90befffa
868put k 61 0xa4506ceb
869put k 62 0xbef9a3f7
870put k 63 0xc67178f2
871end
872
873note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes
874note (FIPS 180-4 4.2.3). Wolfram: IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^64],16,16];
875note equal to the decimal table of omnet om/sha512/sha512.om.
876fn sha2_k512 k edit ulong 80 to void
877put k 0 0x428a2f98d728ae22
878put k 1 0x7137449123ef65cd
879put k 2 0xb5c0fbcfec4d3b2f
880put k 3 0xe9b5dba58189dbbc
881put k 4 0x3956c25bf348b538
882put k 5 0x59f111f1b605d019
883put k 6 0x923f82a4af194f9b
884put k 7 0xab1c5ed5da6d8118
885put k 8 0xd807aa98a3030242
886put k 9 0x12835b0145706fbe
887put k 10 0x243185be4ee4b28c
888put k 11 0x550c7dc3d5ffb4e2
889put k 12 0x72be5d74f27b896f
890put k 13 0x80deb1fe3b1696b1
891put k 14 0x9bdc06a725c71235
892put k 15 0xc19bf174cf692694
893put k 16 0xe49b69c19ef14ad2
894put k 17 0xefbe4786384f25e3
895put k 18 0x0fc19dc68b8cd5b5
896put k 19 0x240ca1cc77ac9c65
897put k 20 0x2de92c6f592b0275
898put k 21 0x4a7484aa6ea6e483
899put k 22 0x5cb0a9dcbd41fbd4
900put k 23 0x76f988da831153b5
901put k 24 0x983e5152ee66dfab
902put k 25 0xa831c66d2db43210
903put k 26 0xb00327c898fb213f
904put k 27 0xbf597fc7beef0ee4
905put k 28 0xc6e00bf33da88fc2
906put k 29 0xd5a79147930aa725
907put k 30 0x06ca6351e003826f
908put k 31 0x142929670a0e6e70
909put k 32 0x27b70a8546d22ffc
910put k 33 0x2e1b21385c26c926
911put k 34 0x4d2c6dfc5ac42aed
912put k 35 0x53380d139d95b3df
913put k 36 0x650a73548baf63de
914put k 37 0x766a0abb3c77b2a8
915put k 38 0x81c2c92e47edaee6
916put k 39 0x92722c851482353b
917put k 40 0xa2bfe8a14cf10364
918put k 41 0xa81a664bbc423001
919put k 42 0xc24b8b70d0f89791
920put k 43 0xc76c51a30654be30
921put k 44 0xd192e819d6ef5218
922put k 45 0xd69906245565a910
923put k 46 0xf40e35855771202a
924put k 47 0x106aa07032bbd1b8
925put k 48 0x19a4c116b8d2d0c8
926put k 49 0x1e376c085141ab53
927put k 50 0x2748774cdf8eeb99
928put k 51 0x34b0bcb5e19b48a8
929put k 52 0x391c0cb3c5c95a63
930put k 53 0x4ed8aa4ae3418acb
931put k 54 0x5b9cca4f7763e373
932put k 55 0x682e6ff3d6b2b8a3
933put k 56 0x748f82ee5defb2fc
934put k 57 0x78a5636f43172f60
935put k 58 0x84c87814a1f0ab72
936put k 59 0x8cc702081a6439ec
937put k 60 0x90befffa23631e28
938put k 61 0xa4506cebde82bde9
939put k 62 0xbef9a3f7b2c67915
940put k 63 0xc67178f2e372532b
941put k 64 0xca273eceea26619c
942put k 65 0xd186b8c721c0c207
943put k 66 0xeada7dd6cde0eb1e
944put k 67 0xf57d4f7fee6ed178
945put k 68 0x06f067aa72176fba
946put k 69 0x0a637dc5a2c898a6
947put k 70 0x113f9804bef90dae
948put k 71 0x1b710b35131c471b
949put k 72 0x28db77f523047d84
950put k 73 0x32caab7b40c72493
951put k 74 0x3c9ebe0a15c9bebc
952put k 75 0x431d67c49c100d4c
953put k 76 0x4cc5d4becb3e42b6
954put k 77 0x597f299cfc657e2a
955put k 78 0x5fcb6fab3ad6faec
956put k 79 0x6c44198c4a475817
957end
958
959note H(0) (FIPS 180-4 5.3.3 SHA-256, 5.3.4 SHA-384, 5.3.5 SHA-512). Wolfram: fractional parts of
960note sqrt(Prime[i]) * 2^32 (i 1..8), * 2^64 (i 1..8 for SHA-512, i 9..16 for SHA-384).
961note Any other alg traps 9 (caller contract).
962fn sha2_init c edit ulong * alg ulong to void
963for i ulong 40
964put c i 0
965end
966put c 9 alg
967if eq alg 256
968put c 0 0x6a09e667
969put c 1 0xbb67ae85
970put c 2 0x3c6ef372
971put c 3 0xa54ff53a
972put c 4 0x510e527f
973put c 5 0x9b05688c
974put c 6 0x1f83d9ab
975put c 7 0x5be0cd19
976ret
977end
978if eq alg 384
979put c 0 0xcbbb9d5dc1059ed8
980put c 1 0x629a292a367cd507
981put c 2 0x9159015a3070dd17
982put c 3 0x152fecd8f70e5939
983put c 4 0x67332667ffc00b31
984put c 5 0x8eb44a8768581511
985put c 6 0xdb0c2e0d64f98fa7
986put c 7 0x47b5481dbefa4fa4
987ret
988end
989if eq alg 512
990put c 0 0x6a09e667f3bcc908
991put c 1 0xbb67ae8584caa73b
992put c 2 0x3c6ef372fe94f82b
993put c 3 0xa54ff53a5f1d36f1
994put c 4 0x510e527fade682d1
995put c 5 0x9b05688c2b3e6c1f
996put c 6 0x1f83d9abfb41bd6b
997put c 7 0x5be0cd19137e2179
998ret
999end
1000trap 9
1001end
1002
1003note SHA-256 message schedule (FIPS 180-4 6.2.2 step 1) of the block m[o .. o+64).
1004fn sha2_s256 w edit uint 64 m view uchar * o ulong to void
1005q = ulong 0
1006x = uint 0
1007y = uint 0
1008for i ulong 16 unroll
1009q = add o mul i 4
1010x = uint get m q
1011x = or shl x 8 uint get m add q 1
1012x = or shl x 8 uint get m add q 2
1013put w i or shl x 8 uint get m add q 3
1014end
1015for i ulong 48 unroll
1016x = get w add i 1
1017y = get w add i 14
1018x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
1019put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
1020end
1021end
1022
1023note SHA-256 rounds (6.2.2 steps 2-4). Ch(e,f,g) = g xor (e and (f xor g)); Maj(a,b,c) =
1024note (a and b) or (c and (a or b)); Sigma1 = ROTR 6,11,25; Sigma0 = ROTR 2,13,22 (4.1.2).
1025fn sha2_r256 s edit uint 8 w view uint 64 kt view uint 64 to void
1026a = get s 0
1027b = get s 1
1028c = get s 2
1029d = get s 3
1030e = get s 4
1031f = get s 5
1032g = get s 6
1033h = get s 7
1034t = uint 0
1035for i ulong 64 unroll
1036h = add h get kt i
1037h = add h get w i
1038t = xor rotr e 6 rotr e 11
1039t = xor t rotr e 25
1040h = add h t
1041h = add h xor g and e xor f g
1042d = add d h
1043t = xor rotr a 2 rotr a 13
1044t = xor t rotr a 22
1045h = add h t
1046t = and c or a b
1047h = add h or and a b t
1048t = h
1049h = g
1050g = f
1051f = e
1052e = d
1053d = c
1054c = b
1055b = a
1056a = t
1057end
1058put s 0 add get s 0 a
1059put s 1 add get s 1 b
1060put s 2 add get s 2 c
1061put s 3 add get s 3 d
1062put s 4 add get s 4 e
1063put s 5 add get s 5 f
1064put s 6 add get s 6 g
1065put s 7 add get s 7 h
1066end
1067
1068note the whole 64-byte blocks of m (len m a multiple of 64, at most 2^25) into c's chaining value
1069fn sha2_b256 c edit ulong * m view uchar * to void
1070s = new uint 8 0
1071w = new uint 64 0
1072kt = new uint 64 0
1073sha2_k256 kt
1074for i ulong 8 unroll
1075put s i uint get c i
1076end
1077for j ulong shr len m 6
1078sha2_s256 w m shl j 6
1079sha2_r256 s w kt
1080end
1081for i ulong 8 unroll
1082put c i ulong get s i
1083put s i 0
1084end
1085for i ulong 64
1086put w i 0
1087end
1088end
1089
1090note SHA-512 message schedule (6.4.2 step 1) of the block m[o .. o+128); sigma0 = ROTR 1,8 SHR 7,
1091note sigma1 = ROTR 19,61 SHR 6 (4.1.3).
1092fn sha2_s512 w edit ulong 80 m view uchar * o ulong to void
1093q = ulong 0
1094x = ulong 0
1095y = ulong 0
1096for i ulong 16 unroll
1097q = add o mul i 8
1098x = ulong get m q
1099x = or shl x 8 ulong get m add q 1
1100x = or shl x 8 ulong get m add q 2
1101x = or shl x 8 ulong get m add q 3
1102x = or shl x 8 ulong get m add q 4
1103x = or shl x 8 ulong get m add q 5
1104x = or shl x 8 ulong get m add q 6
1105put w i or shl x 8 ulong get m add q 7
1106end
1107for i ulong 64 unroll
1108x = get w add i 1
1109y = get w add i 14
1110x = add add get w i get w add i 9 xor xor rotr x 1 rotr x 8 shr x 7
1111put w add i 16 add x xor xor rotr y 19 rotr y 61 shr y 6
1112end
1113end
1114
1115note SHA-512 rounds (6.4.2 steps 2-4), a rolled loop (unroll allows at most 64 copies); Sigma1 = ROTR 14,18,41; Sigma0 = ROTR 28,34,39 (4.1.3).
1116fn sha2_r512 s edit ulong 8 w view ulong 80 kt view ulong 80 to void
1117a = get s 0
1118b = get s 1
1119c = get s 2
1120d = get s 3
1121e = get s 4
1122f = get s 5
1123g = get s 6
1124h = get s 7
1125t = ulong 0
1126for i ulong 80
1127h = add h get kt i
1128h = add h get w i
1129t = xor rotr e 14 rotr e 18
1130t = xor t rotr e 41
1131h = add h t
1132h = add h xor g and e xor f g
1133d = add d h
1134t = xor rotr a 28 rotr a 34
1135t = xor t rotr a 39
1136h = add h t
1137t = and c or a b
1138h = add h or and a b t
1139t = h
1140h = g
1141g = f
1142f = e
1143e = d
1144d = c
1145c = b
1146b = a
1147a = t
1148end
1149put s 0 add get s 0 a
1150put s 1 add get s 1 b
1151put s 2 add get s 2 c
1152put s 3 add get s 3 d
1153put s 4 add get s 4 e
1154put s 5 add get s 5 f
1155put s 6 add get s 6 g
1156put s 7 add get s 7 h
1157end
1158
1159note the whole 128-byte blocks of m (len m a multiple of 128, at most 2^25) into c
1160fn sha2_b512 c edit ulong * m view uchar * to void
1161s = new ulong 8 0
1162w = new ulong 80 0
1163kt = new ulong 80 0
1164sha2_k512 kt
1165for i ulong 8 unroll
1166put s i get c i
1167end
1168for j ulong shr len m 7
1169sha2_s512 w m shl j 7
1170sha2_r512 s w kt
1171end
1172for i ulong 8 unroll
1173put c i get s i
1174put s i 0
1175end
1176for i ulong 80
1177put w i 0
1178end
1179end
1180
1181note whole blocks of m (len m a multiple of the block size) in runs of at most 2^25 bytes
1182fn sha2_run c edit ulong * m view uchar * to void
1183n = len m
1184q = ulong 0
1185k = ulong 0
1186while lt q n bound 999999
1187k = sub n q
1188if gt k 0x2000000
1189k = 0x2000000
1190end
1191if eq get c 9 256
1192sha2_b256 c part m q k
1193else
1194sha2_b512 c part m q k
1195end
1196q = add q k
1197end
1198end
1199
1200note pending byte i (i < 128) of the packed buffer
1201fn sha2_bget c view ulong * i ulong to uchar
1202x = shr get c add 16 shr i 3 sub 56 shl and i 7 3
1203ret uchar and x 0xff
1204end
1205
1206note set pending byte i of the packed buffer to v
1207fn sha2_bput c edit ulong * i ulong v uchar to void
1208q = add 16 shr i 3
1209sh = sub 56 shl and i 7 3
1210x = and get c q not shl ulong 0xff sh
1211put c q or x shl ulong v sh
1212end
1213
1214note the bytes m[0..k) into the buffer at bi (bi + k <= block size)
1215fn sha2_fill c edit ulong * bi ulong m view uchar * k ulong to void
1216for i ulong k
1217sha2_bput c add bi i get m i
1218end
1219end
1220
1221fn sha2_update c edit ulong * m view uchar * to void
1222blk = new uchar 128 0
1223n = len m
1224cnt = get c 8
1225bs = ulong 128
1226if eq get c 9 256
1227bs = 64
1228end
1229bi = and cnt sub bs 1
1230put c 8 add cnt n
1231off = ulong 0
1232if gt bi 0
1233off = sub bs bi
1234if lt n off
1235off = n
1236end
1237sha2_fill c bi m off
1238if eq add bi off bs
1239for i ulong bs
1240put blk i sha2_bget c i
1241end
1242sha2_run c part blk 0 bs
1243for i ulong bs
1244put blk i 0
1245end
1246end
1247end
1248nb = and sub n off not sub bs 1
1249sha2_run c part m off nb
1250off = add off nb
1251sha2_fill c 0 part m off sub n off sub n off
1252end
1253
1254note H(N) as big-endian bytes: 32 (SHA-256, 8 words of 32 bits), 48 (SHA-384, first 6 words), 64
1255fn sha2_put c view ulong * o edit uchar * to ulong
1256alg = get c 9
1257sh = ulong 0
1258if eq alg 256
1259for i ulong 32
1260sh = sub 24 shl and i 3 3
1261put o i uchar and shr get c shr i 2 sh 0xff
1262end
1263ret 32
1264end
1265dl = ulong 64
1266if eq alg 384
1267dl = 48
1268end
1269for i ulong dl
1270sh = sub 56 shl and i 7 3
1271put o i uchar and shr get c shr i 3 sh 0xff
1272end
1273ret dl
1274end
1275
1276note padding (FIPS 180-4 5.1.1 / 5.1.2): 0x80, zeros, the bit length big-endian in the last 8 (SHA-256)
1277note or 16 (SHA-384/512) bytes of one or two blocks; returns the digest length
1278fn sha2_fin c edit ulong * o edit uchar * to ulong
1279p = new uchar 256 0
1280bs = ulong 128
1281ls = ulong 16
1282if eq get c 9 256
1283bs = 64
1284ls = 8
1285end
1286cnt = get c 8
1287k = and cnt sub bs 1
1288for i ulong k
1289put p i sha2_bget c i
1290end
1291put p k 0x80
1292e = bs
1293if ge add k ls bs
1294e = shl bs 1
1295end
1296x = shl cnt 3
1297for i ulong 8 unroll
1298put p sub e add i 1 uchar and shr x mul i 8 0xff
1299end
1300if eq ls 16
1301put p sub e 9 uchar shr cnt 61
1302end
1303sha2_run c part p 0 e
1304for i ulong 256
1305put p i 0
1306end
1307ret sha2_put c o
1308end
1309
1310fn sha256_init c edit ulong * to void
1311sha2_init c 256
1312end
1313
1314fn sha384_init c edit ulong * to void
1315sha2_init c 384
1316end
1317
1318fn sha512_init c edit ulong * to void
1319sha2_init c 512
1320end
1321
1322fn sha256_update c edit ulong * m view uchar * to void
1323sha2_update c m
1324end
1325
1326fn sha384_update c edit ulong * m view uchar * to void
1327sha2_update c m
1328end
1329
1330fn sha512_update c edit ulong * m view uchar * to void
1331sha2_update c m
1332end
1333
1334fn sha256_final c edit ulong * o edit uchar * to void
1335dl = sha2_fin c o
1336end
1337
1338fn sha384_final c edit ulong * o edit uchar * to void
1339dl = sha2_fin c o
1340end
1341
1342fn sha512_final c edit ulong * o edit uchar * to void
1343dl = sha2_fin c o
1344end
1345
1346fn sha2_copy d edit ulong * s view ulong * to void
1347for i ulong len s
1348put d i get s i
1349end
1350end
1351
1352fn sha2_digest alg ulong m view uchar * o edit uchar * to ulong
1353c = new ulong 40 0
1354if ne alg 256
1355if ne alg 384
1356if ne alg 512
1357ret 0
1358end
1359end
1360end
1361sha2_init c alg
1362sha2_update c m
1363dl = sha2_fin c o
1364for i ulong 40
1365put c i 0
1366end
1367ret dl
1368end