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

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