interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / driver / gsp / sha2.om

518 lines · 12495 bytes Raw Blame History
1note gsp/sha2.om: SHA-256 and SHA-384 (FIPS 180-4) in seeded Omega (OM-1), for the GB10 firmware
2note inputs. Ports the digests the aienux PR #441 tools use: argus/sha256.c (sha256_init/update/final,
3note used by ck_gsp_fw_inspect.c and ck_gsp_bindata_extract.c) and the SHA-384 of
4note ck_gsp_bindata_extract.c lines 84-197 (sha512_block, sha384). Library unit: no main.
5note Prefix sh. Input is always the window shin[off..off+n), n <= SHWIN; callers copy or pread into it.
6note SHA-256: eight independent streaming contexts cx 0..7 (shh, shtot, shbn, shbf), so one pass over a
7note file can feed several digests. SHA-384: one streaming context (sh5h, sh5t, sh5n, sh5b).
8note A digest lands in shout (32 or 48 bytes); shhex n writes its lowercase hex into shhx[0..2n).
9note Constants: K256 = first 32 bits of the fractional parts of the cube roots of the first 64 primes,
10note K512 = first 64 bits for the first 80 primes, IV256 = square roots of the first 8 primes (32 bits),
11note IV384 = square roots of primes 9..16 (64 bits). All decimal, computed by WolframLanguageEvaluator:
12note Table[Floor[FractionalPart[N[CubeRoot[Prime[i]],120]]*2^64],{i,80}] (and the 2^32 / Sqrt forms);
13note spot checks against the C hex: K512[0] 0x428a2f98d728ae22, K512[79] 0x6c44198c4a475817,
14note IV384[0] 0xcbbb9d5dc1059ed8, IV384[7] 0x47b5481dbefa4fa4, K256[63] 0xc67178f2.
15const SHWIN ulong 524288
16global shin uchar 524288
17global shok ulong 1
18global shk uint 64
19global shk5 ulong 80
20global shh uint 64
21global shv uint 8
22global shtot ulong 8
23global shbn ulong 8
24global shbf uchar 512
25global shw uint 64
26global sh5h ulong 8
27global sh5t ulong 1
28global sh5n ulong 1
29global sh5b uchar 128
30global sh5w ulong 80
31global shout uchar 64
32global shhx uchar 128
33fn shsetup to void
34if eq get shok 0 1
35ret
36end
37put shk 0 1116352408
38put shk 1 1899447441
39put shk 2 3049323471
40put shk 3 3921009573
41put shk 4 961987163
42put shk 5 1508970993
43put shk 6 2453635748
44put shk 7 2870763221
45put shk 8 3624381080
46put shk 9 310598401
47put shk 10 607225278
48put shk 11 1426881987
49put shk 12 1925078388
50put shk 13 2162078206
51put shk 14 2614888103
52put shk 15 3248222580
53put shk 16 3835390401
54put shk 17 4022224774
55put shk 18 264347078
56put shk 19 604807628
57put shk 20 770255983
58put shk 21 1249150122
59put shk 22 1555081692
60put shk 23 1996064986
61put shk 24 2554220882
62put shk 25 2821834349
63put shk 26 2952996808
64put shk 27 3210313671
65put shk 28 3336571891
66put shk 29 3584528711
67put shk 30 113926993
68put shk 31 338241895
69put shk 32 666307205
70put shk 33 773529912
71put shk 34 1294757372
72put shk 35 1396182291
73put shk 36 1695183700
74put shk 37 1986661051
75put shk 38 2177026350
76put shk 39 2456956037
77put shk 40 2730485921
78put shk 41 2820302411
79put shk 42 3259730800
80put shk 43 3345764771
81put shk 44 3516065817
82put shk 45 3600352804
83put shk 46 4094571909
84put shk 47 275423344
85put shk 48 430227734
86put shk 49 506948616
87put shk 50 659060556
88put shk 51 883997877
89put shk 52 958139571
90put shk 53 1322822218
91put shk 54 1537002063
92put shk 55 1747873779
93put shk 56 1955562222
94put shk 57 2024104815
95put shk 58 2227730452
96put shk 59 2361852424
97put shk 60 2428436474
98put shk 61 2756734187
99put shk 62 3204031479
100put shk 63 3329325298
101put shk5 0 4794697086780616226
102put shk5 1 8158064640168781261
103put shk5 2 13096744586834688815
104put shk5 3 16840607885511220156
105put shk5 4 4131703408338449720
106put shk5 5 6480981068601479193
107put shk5 6 10538285296894168987
108put shk5 7 12329834152419229976
109put shk5 8 15566598209576043074
110put shk5 9 1334009975649890238
111put shk5 10 2608012711638119052
112put shk5 11 6128411473006802146
113put shk5 12 8268148722764581231
114put shk5 13 9286055187155687089
115put shk5 14 11230858885718282805
116put shk5 15 13951009754708518548
117put shk5 16 16472876342353939154
118put shk5 17 17275323862435702243
119put shk5 18 1135362057144423861
120put shk5 19 2597628984639134821
121put shk5 20 3308224258029322869
122put shk5 21 5365058923640841347
123put shk5 22 6679025012923562964
124put shk5 23 8573033837759648693
125put shk5 24 10970295158949994411
126put shk5 25 12119686244451234320
127put shk5 26 12683024718118986047
128put shk5 27 13788192230050041572
129put shk5 28 14330467153632333762
130put shk5 29 15395433587784984357
131put shk5 30 489312712824947311
132put shk5 31 1452737877330783856
133put shk5 32 2861767655752347644
134put shk5 33 3322285676063803686
135put shk5 34 5560940570517711597
136put shk5 35 5996557281743188959
137put shk5 36 7280758554555802590
138put shk5 37 8532644243296465576
139put shk5 38 9350256976987008742
140put shk5 39 10552545826968843579
141put shk5 40 11727347734174303076
142put shk5 41 12113106623233404929
143put shk5 42 14000437183269869457
144put shk5 43 14369950271660146224
145put shk5 44 15101387698204529176
146put shk5 45 15463397548674623760
147put shk5 46 17586052441742319658
148put shk5 47 1182934255886127544
149put shk5 48 1847814050463011016
150put shk5 49 2177327727835720531
151put shk5 50 2830643537854262169
152put shk5 51 3796741975233480872
153put shk5 52 4115178125766777443
154put shk5 53 5681478168544905931
155put shk5 54 6601373596472566643
156put shk5 55 7507060721942968483
157put shk5 56 8399075790359081724
158put shk5 57 8693463985226723168
159put shk5 58 9568029438360202098
160put shk5 59 10144078919501101548
161put shk5 60 10430055236837252648
162put shk5 61 11840083180663258601
163put shk5 62 13761210420658862357
164put shk5 63 14299343276471374635
165put shk5 64 14566680578165727644
166put shk5 65 15097957966210449927
167put shk5 66 16922976911328602910
168put shk5 67 17689382322260857208
169put shk5 68 500013540394364858
170put shk5 69 748580250866718886
171put shk5 70 1242879168328830382
172put shk5 71 1977374033974150939
173put shk5 72 2944078676154940804
174put shk5 73 3659926193048069267
175put shk5 74 4368137639120453308
176put shk5 75 4836135668995329356
177put shk5 76 5532061633213252278
178put shk5 77 6448918945643986474
179put shk5 78 6902733635092675308
180put shk5 79 7801388544844847127
181put shok 0 1
182end
183note SHA-256 context cx (0..7): chaining value shh[8cx..8cx+8), bytes absorbed shtot[cx], pending
184note block shbf[64cx..64cx+shbn[cx])
185fn sh2init cx ulong to void
186shsetup
187b = mul cx 8
188put shh b 1779033703
189put shh add b 1 3144134277
190put shh add b 2 1013904242
191put shh add b 3 2773480762
192put shh add b 4 1359893119
193put shh add b 5 2600822924
194put shh add b 6 528734635
195put shh add b 7 1541459225
196put shtot cx 0
197put shbn cx 0
198end
199note the 64 rounds over the schedule in shw, folded into the chaining value in shv (16 registers:
200note the state lives in a..h, the sums are built one term at a time)
201fn sh2rnd to void
202a = get shv 0
203b = get shv 1
204c = get shv 2
205d = get shv 3
206e = get shv 4
207f = get shv 5
208g = get shv 6
209h = get shv 7
210for t ulong 64
211x = or shr e 6 shl e 26
212x = xor x or shr e 11 shl e 21
213x = xor x or shr e 25 shl e 7
214x = add x h
215x = add x xor and e f and not e g
216x = add x get shk t
217x = add x get shw t
218h = g
219g = f
220f = e
221e = add d x
222d = c
223c = b
224b = a
225y = or shr b 2 shl b 30
226y = xor y or shr b 13 shl b 19
227y = xor y or shr b 22 shl b 10
228x = add x y
229y = xor and b c and b d
230y = xor y and c d
231a = add x y
232end
233put shv 0 add get shv 0 a
234put shv 1 add get shv 1 b
235put shv 2 add get shv 2 c
236put shv 3 add get shv 3 d
237put shv 4 add get shv 4 e
238put shv 5 add get shv 5 f
239put shv 6 add get shv 6 g
240put shv 7 add get shv 7 h
241end
242note one 64-byte block: src 0 = shin[off..off+64), src 1 = shbf[off..off+64)
243fn sh2comp cx ulong src ulong off ulong to void
244for t ulong 16
245p = add off mul t 4
246if eq src 0
247v = or shl uint get shin p 24 shl uint get shin add p 1 16
248v = or v or shl uint get shin add p 2 8 uint get shin add p 3
249put shw t v
250else
251v = or shl uint get shbf p 24 shl uint get shbf add p 1 16
252v = or v or shl uint get shbf add p 2 8 uint get shbf add p 3
253put shw t v
254end
255end
256for u ulong 48
257i = add u 16
258x = get shw sub i 2
259s = or shr x 17 shl x 15
260s = xor s or shr x 19 shl x 13
261s = xor s shr x 10
262y = get shw sub i 15
263r = or shr y 7 shl y 25
264r = xor r or shr y 18 shl y 14
265r = xor r shr y 3
266s = add s get shw sub i 7
267r = add r get shw sub i 16
268put shw i add s r
269end
270bs = mul cx 8
271for j ulong 8
272put shv j get shh add bs j
273end
274sh2rnd
275for j2 ulong 8
276put shh add bs j2 get shv j2
277end
278end
279note absorb shin[off..off+n) into context cx; n <= SHWIN
280fn sh2upd cx ulong off ulong n ulong to void
281if gt n SHWIN
282trap 3
283end
284bo = mul cx 64
285i = ulong 0
286while lt i n bound 999999
287bn = get shbn cx
288if and eq bn 0 le add i 64 n
289sh2comp cx 0 add off i
290i = add i 64
291else
292put shbf add bo bn get shin add off i
293bn = add bn 1
294i = add i 1
295if eq bn 64
296sh2comp cx 1 bo
297bn = ulong 0
298end
299put shbn cx bn
300end
301end
302put shtot cx add get shtot cx n
303end
304note pad and finish context cx: the 32-byte digest goes to shout[0..32)
305fn sh2fin cx ulong to void
306bo = mul cx 64
307bits = mul get shtot cx 8
308bn = get shbn cx
309put shbf add bo bn 128
310bn = add bn 1
311if gt bn 56
312while lt bn 64 bound 64
313put shbf add bo bn 0
314bn = add bn 1
315end
316sh2comp cx 1 bo
317bn = ulong 0
318end
319while lt bn 56 bound 64
320put shbf add bo bn 0
321bn = add bn 1
322end
323for k ulong 8
324bn = and shr bits mul sub 7 k 8 255
325put shbf add bo add 56 k uchar bn
326end
327sh2comp cx 1 bo
328put shbn cx 0
329bo = mul cx 8
330for j ulong 8
331bits = ulong get shh add bo j
332bn = mul j 4
333put shout bn uchar and shr bits 24 255
334put shout add bn 1 uchar and shr bits 16 255
335put shout add bn 2 uchar and shr bits 8 255
336put shout add bn 3 uchar and bits 255
337end
338end
339note hash bytes [off, off+n) of the open file fd into context cx through shin; returns 0, the negative
340note errno of a failed pread, or -5 (EIO) when the file ends early
341fn sh2file cx ulong fd int off ulong n ulong to long
342p = ulong 0
343while lt p n bound 999999
344k = sub n p
345if gt k SHWIN
346k = SHWIN
347end
348q = ulong 0
349while lt q k bound 999999
350r = pread fd shin q sub k q long add add off p q
351if lt r 0
352ret r
353end
354if eq r 0
355ret -5
356end
357q = add q ulong r
358end
359sh2upd cx 0 k
360p = add p k
361end
362ret 0
363end
364note SHA-384 (one context)
365fn sh5init to void
366shsetup
367put sh5h 0 14680500436340154072
368put sh5h 1 7105036623409894663
369put sh5h 2 10473403895298186519
370put sh5h 3 1526699215303891257
371put sh5h 4 7436329637833083697
372put sh5h 5 10282925794625328401
373put sh5h 6 15784041429090275239
374put sh5h 7 5167115440072839076
375put sh5t 0 0
376put sh5n 0 0
377end
378fn sh5rnd to void
379a = get sh5h 0
380b = get sh5h 1
381c = get sh5h 2
382d = get sh5h 3
383e = get sh5h 4
384f = get sh5h 5
385g = get sh5h 6
386h = get sh5h 7
387for t ulong 80
388x = or shr e 14 shl e 50
389x = xor x or shr e 18 shl e 46
390x = xor x or shr e 41 shl e 23
391x = add x h
392x = add x xor and e f and not e g
393x = add x get shk5 t
394x = add x get sh5w t
395h = g
396g = f
397f = e
398e = add d x
399d = c
400c = b
401b = a
402y = or shr b 28 shl b 36
403y = xor y or shr b 34 shl b 30
404y = xor y or shr b 39 shl b 25
405x = add x y
406y = xor and b c and b d
407y = xor y and c d
408a = add x y
409end
410put sh5h 0 add get sh5h 0 a
411put sh5h 1 add get sh5h 1 b
412put sh5h 2 add get sh5h 2 c
413put sh5h 3 add get sh5h 3 d
414put sh5h 4 add get sh5h 4 e
415put sh5h 5 add get sh5h 5 f
416put sh5h 6 add get sh5h 6 g
417put sh5h 7 add get sh5h 7 h
418end
419note one 128-byte block: src 0 = shin[off..off+128), src 1 = sh5b
420fn sh5comp src ulong off ulong to void
421for t ulong 16
422v = ulong 0
423for k ulong 8
424if eq src 0
425v = or shl v 8 ulong get shin add add off mul t 8 k
426else
427v = or shl v 8 ulong get sh5b add mul t 8 k
428end
429end
430put sh5w t v
431end
432for u ulong 64
433i = add u 16
434x = get sh5w sub i 2
435s = or shr x 19 shl x 45
436s = xor s or shr x 61 shl x 3
437s = xor s shr x 6
438y = get sh5w sub i 15
439r = or shr y 1 shl y 63
440r = xor r or shr y 8 shl y 56
441r = xor r shr y 7
442s = add s get sh5w sub i 7
443r = add r get sh5w sub i 16
444put sh5w i add s r
445end
446sh5rnd
447end
448fn sh5upd off ulong n ulong to void
449if gt n SHWIN
450trap 3
451end
452i = ulong 0
453while lt i n bound 999999
454bn = get sh5n 0
455if and eq bn 0 le add i 128 n
456sh5comp 0 add off i
457i = add i 128
458else
459put sh5b bn get shin add off i
460bn = add bn 1
461i = add i 1
462if eq bn 128
463sh5comp 1 0
464bn = ulong 0
465end
466put sh5n 0 bn
467end
468end
469put sh5t 0 add get sh5t 0 n
470end
471note pad and finish: the 48-byte digest goes to shout[0..48) (message length below 2^61 bytes)
472fn sh5fin to void
473bits = mul get sh5t 0 8
474bn = get sh5n 0
475put sh5b bn 128
476bn = add bn 1
477if gt bn 112
478while lt bn 128 bound 128
479put sh5b bn 0
480bn = add bn 1
481end
482sh5comp 1 0
483bn = ulong 0
484end
485while lt bn 120 bound 128
486put sh5b bn 0
487bn = add bn 1
488end
489for k ulong 8
490bn = and shr bits mul sub 7 k 8 255
491put sh5b add 120 k uchar bn
492end
493sh5comp 1 0
494put sh5n 0 0
495for j ulong 48
496bits = get sh5h div j 8
497bn = and shr bits mul sub 7 rem j 8 8 255
498put shout j uchar bn
499end
500end
501note lowercase hex of shout[0..n) into shhx[0..2n), n <= 64
502fn shhex n ulong to void
503for i ulong n
504b = get shout i
505hi = shr b 4
506lo = and b 15
507if lt hi 10
508put shhx mul i 2 add hi 48
509else
510put shhx mul i 2 add hi 87
511end
512if lt lo 10
513put shhx add mul i 2 1 add lo 48
514else
515put shhx add mul i 2 1 add lo 87
516end
517end
518end