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

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