interplane.Get Omega
Read in your language

benchmarks-mv

Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.

Brought by drakestapleton

branch main Branches Tags History

benchmarks-mv / native / neuralese / nl1r / evidence / boundary-independent-source.om

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