interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / neuralese / nl1r-task / evidence / boundary-attempt2.om

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