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 / seed / ng / bench / kernels / sha.om

609 lines · 14675 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note om/tls/sha2.om: SHA-256, SHA-384, SHA-512 (FIPS 180-4) streaming, as an OM-2 library unit
4note (no om2 line, no main, no globals). Module code SH; functions sha2_*, sha256_*, sha384_*, sha512_*.
5note
6note API
7note sha256_init c edit ulong * c: 40 ulong cells (caller owns all state)
8note sha256_update c edit ulong * m view uchar * any length, any chunking
9note sha256_final c edit ulong * o edit uchar * writes 32 bytes (o shorter traps 3)
10note sha384_* / sha512_* the same; final writes 48 / 64 bytes
11note sha2_copy d edit ulong * s view ulong * copies len s cells (a context snapshot)
12note sha2_digest alg ulong m view uchar * o edit uchar * to ulong
13note alg 256|384|512: one-shot digest into o, returns 32|48|64; any other alg returns 0, writes nothing
14note After final the context is spent (its state cells hold the last chaining value); init it again.
15note A context initialised for one algorithm must be used with that algorithm's update/final; the
16note common code reads the algorithm from cell 9, so sha256_update on a sha512 context hashes SHA-512.
17note
18note Context cells: 0..7 chaining value H (32-bit words zero-extended for SHA-256), 8 = bytes absorbed
19note mod 2^64, 9 = algorithm (256, 384, 512), 16..31 = pending block bytes packed big-endian (byte i
20note in cell 16 + i/8, bits 56 - 8*(i mod 8)), 10..15 and 32..39 = 0.
21note Constant time: every branch, loop count and index depends only on lengths and the algorithm,
22note never on message or state bytes. Local schedule/state/padding arrays are zeroed before return
23note (HMAC keys pass through them). omc emits plain AArch64; MUL/ADD/EOR/ROR/shift data-independent.
24note Message length: any length below 2^64 bytes; update cuts its input into runs of 2^25 bytes so no
25note loop passes 2^20 times (a rolled for traps 4 after 10^6 passes; the while bound 999999 of runs
26note allows inputs up to 999999 * 32 MiB per update call).
27
28note K{256}[0..64): first 32 bits of the fractional parts of the cube roots of the first 64 primes
29note (FIPS 180-4 4.2.2). Same values as om/sha256/sha256-om2.om; recomputed with Wolfram:
30note IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^32],16,8] for i = 1..64.
31fn sha2_k256 k edit uint 64 to void
32put k 0 0x428a2f98
33put k 1 0x71374491
34put k 2 0xb5c0fbcf
35put k 3 0xe9b5dba5
36put k 4 0x3956c25b
37put k 5 0x59f111f1
38put k 6 0x923f82a4
39put k 7 0xab1c5ed5
40put k 8 0xd807aa98
41put k 9 0x12835b01
42put k 10 0x243185be
43put k 11 0x550c7dc3
44put k 12 0x72be5d74
45put k 13 0x80deb1fe
46put k 14 0x9bdc06a7
47put k 15 0xc19bf174
48put k 16 0xe49b69c1
49put k 17 0xefbe4786
50put k 18 0x0fc19dc6
51put k 19 0x240ca1cc
52put k 20 0x2de92c6f
53put k 21 0x4a7484aa
54put k 22 0x5cb0a9dc
55put k 23 0x76f988da
56put k 24 0x983e5152
57put k 25 0xa831c66d
58put k 26 0xb00327c8
59put k 27 0xbf597fc7
60put k 28 0xc6e00bf3
61put k 29 0xd5a79147
62put k 30 0x06ca6351
63put k 31 0x14292967
64put k 32 0x27b70a85
65put k 33 0x2e1b2138
66put k 34 0x4d2c6dfc
67put k 35 0x53380d13
68put k 36 0x650a7354
69put k 37 0x766a0abb
70put k 38 0x81c2c92e
71put k 39 0x92722c85
72put k 40 0xa2bfe8a1
73put k 41 0xa81a664b
74put k 42 0xc24b8b70
75put k 43 0xc76c51a3
76put k 44 0xd192e819
77put k 45 0xd6990624
78put k 46 0xf40e3585
79put k 47 0x106aa070
80put k 48 0x19a4c116
81put k 49 0x1e376c08
82put k 50 0x2748774c
83put k 51 0x34b0bcb5
84put k 52 0x391c0cb3
85put k 53 0x4ed8aa4a
86put k 54 0x5b9cca4f
87put k 55 0x682e6ff3
88put k 56 0x748f82ee
89put k 57 0x78a5636f
90put k 58 0x84c87814
91put k 59 0x8cc70208
92put k 60 0x90befffa
93put k 61 0xa4506ceb
94put k 62 0xbef9a3f7
95put k 63 0xc67178f2
96end
97
98note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes
99note (FIPS 180-4 4.2.3). Wolfram: IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^64],16,16];
100note equal to the decimal table of omnet om/sha512/sha512.om.
101fn sha2_k512 k edit ulong 80 to void
102put k 0 0x428a2f98d728ae22
103put k 1 0x7137449123ef65cd
104put k 2 0xb5c0fbcfec4d3b2f
105put k 3 0xe9b5dba58189dbbc
106put k 4 0x3956c25bf348b538
107put k 5 0x59f111f1b605d019
108put k 6 0x923f82a4af194f9b
109put k 7 0xab1c5ed5da6d8118
110put k 8 0xd807aa98a3030242
111put k 9 0x12835b0145706fbe
112put k 10 0x243185be4ee4b28c
113put k 11 0x550c7dc3d5ffb4e2
114put k 12 0x72be5d74f27b896f
115put k 13 0x80deb1fe3b1696b1
116put k 14 0x9bdc06a725c71235
117put k 15 0xc19bf174cf692694
118put k 16 0xe49b69c19ef14ad2
119put k 17 0xefbe4786384f25e3
120put k 18 0x0fc19dc68b8cd5b5
121put k 19 0x240ca1cc77ac9c65
122put k 20 0x2de92c6f592b0275
123put k 21 0x4a7484aa6ea6e483
124put k 22 0x5cb0a9dcbd41fbd4
125put k 23 0x76f988da831153b5
126put k 24 0x983e5152ee66dfab
127put k 25 0xa831c66d2db43210
128put k 26 0xb00327c898fb213f
129put k 27 0xbf597fc7beef0ee4
130put k 28 0xc6e00bf33da88fc2
131put k 29 0xd5a79147930aa725
132put k 30 0x06ca6351e003826f
133put k 31 0x142929670a0e6e70
134put k 32 0x27b70a8546d22ffc
135put k 33 0x2e1b21385c26c926
136put k 34 0x4d2c6dfc5ac42aed
137put k 35 0x53380d139d95b3df
138put k 36 0x650a73548baf63de
139put k 37 0x766a0abb3c77b2a8
140put k 38 0x81c2c92e47edaee6
141put k 39 0x92722c851482353b
142put k 40 0xa2bfe8a14cf10364
143put k 41 0xa81a664bbc423001
144put k 42 0xc24b8b70d0f89791
145put k 43 0xc76c51a30654be30
146put k 44 0xd192e819d6ef5218
147put k 45 0xd69906245565a910
148put k 46 0xf40e35855771202a
149put k 47 0x106aa07032bbd1b8
150put k 48 0x19a4c116b8d2d0c8
151put k 49 0x1e376c085141ab53
152put k 50 0x2748774cdf8eeb99
153put k 51 0x34b0bcb5e19b48a8
154put k 52 0x391c0cb3c5c95a63
155put k 53 0x4ed8aa4ae3418acb
156put k 54 0x5b9cca4f7763e373
157put k 55 0x682e6ff3d6b2b8a3
158put k 56 0x748f82ee5defb2fc
159put k 57 0x78a5636f43172f60
160put k 58 0x84c87814a1f0ab72
161put k 59 0x8cc702081a6439ec
162put k 60 0x90befffa23631e28
163put k 61 0xa4506cebde82bde9
164put k 62 0xbef9a3f7b2c67915
165put k 63 0xc67178f2e372532b
166put k 64 0xca273eceea26619c
167put k 65 0xd186b8c721c0c207
168put k 66 0xeada7dd6cde0eb1e
169put k 67 0xf57d4f7fee6ed178
170put k 68 0x06f067aa72176fba
171put k 69 0x0a637dc5a2c898a6
172put k 70 0x113f9804bef90dae
173put k 71 0x1b710b35131c471b
174put k 72 0x28db77f523047d84
175put k 73 0x32caab7b40c72493
176put k 74 0x3c9ebe0a15c9bebc
177put k 75 0x431d67c49c100d4c
178put k 76 0x4cc5d4becb3e42b6
179put k 77 0x597f299cfc657e2a
180put k 78 0x5fcb6fab3ad6faec
181put k 79 0x6c44198c4a475817
182end
183
184note H(0) (FIPS 180-4 5.3.3 SHA-256, 5.3.4 SHA-384, 5.3.5 SHA-512). Wolfram: fractional parts of
185note sqrt(Prime[i]) * 2^32 (i 1..8), * 2^64 (i 1..8 for SHA-512, i 9..16 for SHA-384).
186note Any other alg traps 9 (caller contract).
187fn sha2_init c edit ulong * alg ulong to void
188for i ulong 40
189put c i 0
190end
191put c 9 alg
192if eq alg 256
193put c 0 0x6a09e667
194put c 1 0xbb67ae85
195put c 2 0x3c6ef372
196put c 3 0xa54ff53a
197put c 4 0x510e527f
198put c 5 0x9b05688c
199put c 6 0x1f83d9ab
200put c 7 0x5be0cd19
201ret
202end
203if eq alg 384
204put c 0 0xcbbb9d5dc1059ed8
205put c 1 0x629a292a367cd507
206put c 2 0x9159015a3070dd17
207put c 3 0x152fecd8f70e5939
208put c 4 0x67332667ffc00b31
209put c 5 0x8eb44a8768581511
210put c 6 0xdb0c2e0d64f98fa7
211put c 7 0x47b5481dbefa4fa4
212ret
213end
214if eq alg 512
215put c 0 0x6a09e667f3bcc908
216put c 1 0xbb67ae8584caa73b
217put c 2 0x3c6ef372fe94f82b
218put c 3 0xa54ff53a5f1d36f1
219put c 4 0x510e527fade682d1
220put c 5 0x9b05688c2b3e6c1f
221put c 6 0x1f83d9abfb41bd6b
222put c 7 0x5be0cd19137e2179
223ret
224end
225trap 9
226end
227
228note SHA-256 message schedule (FIPS 180-4 6.2.2 step 1) of the block m[o .. o+64).
229fn sha2_s256 w edit uint 64 m view uchar * o ulong to void
230q = ulong 0
231x = uint 0
232y = uint 0
233for i ulong 16 unroll
234q = add o mul i 4
235x = uint get m q
236x = or shl x 8 uint get m add q 1
237x = or shl x 8 uint get m add q 2
238put w i or shl x 8 uint get m add q 3
239end
240for i ulong 48 unroll
241x = get w add i 1
242y = get w add i 14
243x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
244put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
245end
246end
247
248note SHA-256 rounds (6.2.2 steps 2-4). Ch(e,f,g) = g xor (e and (f xor g)); Maj(a,b,c) =
249note (a and b) or (c and (a or b)); Sigma1 = ROTR 6,11,25; Sigma0 = ROTR 2,13,22 (4.1.2).
250fn sha2_r256 s edit uint 8 w view uint 64 kt view uint 64 to void
251a = get s 0
252b = get s 1
253c = get s 2
254d = get s 3
255e = get s 4
256f = get s 5
257g = get s 6
258h = get s 7
259t = uint 0
260for i ulong 64 unroll
261h = add h get kt i
262h = add h get w i
263t = xor rotr e 6 rotr e 11
264t = xor t rotr e 25
265h = add h t
266h = add h xor g and e xor f g
267d = add d h
268t = xor rotr a 2 rotr a 13
269t = xor t rotr a 22
270h = add h t
271t = and c or a b
272h = add h or and a b t
273t = h
274h = g
275g = f
276f = e
277e = d
278d = c
279c = b
280b = a
281a = t
282end
283put s 0 add get s 0 a
284put s 1 add get s 1 b
285put s 2 add get s 2 c
286put s 3 add get s 3 d
287put s 4 add get s 4 e
288put s 5 add get s 5 f
289put s 6 add get s 6 g
290put s 7 add get s 7 h
291end
292
293note the whole 64-byte blocks of m (len m a multiple of 64, at most 2^25) into c's chaining value
294fn sha2_b256 c edit ulong * m view uchar * to void
295s = new uint 8 0
296w = new uint 64 0
297kt = new uint 64 0
298sha2_k256 kt
299for i ulong 8 unroll
300put s i uint get c i
301end
302for j ulong shr len m 6
303sha2_s256 w m shl j 6
304sha2_r256 s w kt
305end
306for i ulong 8 unroll
307put c i ulong get s i
308put s i 0
309end
310for i ulong 64
311put w i 0
312end
313end
314
315note SHA-512 message schedule (6.4.2 step 1) of the block m[o .. o+128); sigma0 = ROTR 1,8 SHR 7,
316note sigma1 = ROTR 19,61 SHR 6 (4.1.3).
317fn sha2_s512 w edit ulong 80 m view uchar * o ulong to void
318q = ulong 0
319x = ulong 0
320y = ulong 0
321for i ulong 16 unroll
322q = add o mul i 8
323x = ulong get m q
324x = or shl x 8 ulong get m add q 1
325x = or shl x 8 ulong get m add q 2
326x = or shl x 8 ulong get m add q 3
327x = or shl x 8 ulong get m add q 4
328x = or shl x 8 ulong get m add q 5
329x = or shl x 8 ulong get m add q 6
330put w i or shl x 8 ulong get m add q 7
331end
332for i ulong 64 unroll
333x = get w add i 1
334y = get w add i 14
335x = add add get w i get w add i 9 xor xor rotr x 1 rotr x 8 shr x 7
336put w add i 16 add x xor xor rotr y 19 rotr y 61 shr y 6
337end
338end
339
340note 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).
341fn sha2_r512 s edit ulong 8 w view ulong 80 kt view ulong 80 to void
342a = get s 0
343b = get s 1
344c = get s 2
345d = get s 3
346e = get s 4
347f = get s 5
348g = get s 6
349h = get s 7
350t = ulong 0
351for i ulong 80
352h = add h get kt i
353h = add h get w i
354t = xor rotr e 14 rotr e 18
355t = xor t rotr e 41
356h = add h t
357h = add h xor g and e xor f g
358d = add d h
359t = xor rotr a 28 rotr a 34
360t = xor t rotr a 39
361h = add h t
362t = and c or a b
363h = add h or and a b t
364t = h
365h = g
366g = f
367f = e
368e = d
369d = c
370c = b
371b = a
372a = t
373end
374put s 0 add get s 0 a
375put s 1 add get s 1 b
376put s 2 add get s 2 c
377put s 3 add get s 3 d
378put s 4 add get s 4 e
379put s 5 add get s 5 f
380put s 6 add get s 6 g
381put s 7 add get s 7 h
382end
383
384note the whole 128-byte blocks of m (len m a multiple of 128, at most 2^25) into c
385fn sha2_b512 c edit ulong * m view uchar * to void
386s = new ulong 8 0
387w = new ulong 80 0
388kt = new ulong 80 0
389sha2_k512 kt
390for i ulong 8 unroll
391put s i get c i
392end
393for j ulong shr len m 7
394sha2_s512 w m shl j 7
395sha2_r512 s w kt
396end
397for i ulong 8 unroll
398put c i get s i
399put s i 0
400end
401for i ulong 80
402put w i 0
403end
404end
405
406note whole blocks of m (len m a multiple of the block size) in runs of at most 2^25 bytes
407fn sha2_run c edit ulong * m view uchar * to void
408n = len m
409q = ulong 0
410k = ulong 0
411while lt q n bound 999999
412k = sub n q
413if gt k 0x2000000
414k = 0x2000000
415end
416if eq get c 9 256
417sha2_b256 c part m q k
418else
419sha2_b512 c part m q k
420end
421q = add q k
422end
423end
424
425note pending byte i (i < 128) of the packed buffer
426fn sha2_bget c view ulong * i ulong to uchar
427x = shr get c add 16 shr i 3 sub 56 shl and i 7 3
428ret uchar and x 0xff
429end
430
431note set pending byte i of the packed buffer to v
432fn sha2_bput c edit ulong * i ulong v uchar to void
433q = add 16 shr i 3
434sh = sub 56 shl and i 7 3
435x = and get c q not shl ulong 0xff sh
436put c q or x shl ulong v sh
437end
438
439note the bytes m[0..k) into the buffer at bi (bi + k <= block size)
440fn sha2_fill c edit ulong * bi ulong m view uchar * k ulong to void
441for i ulong k
442sha2_bput c add bi i get m i
443end
444end
445
446fn sha2_update c edit ulong * m view uchar * to void
447blk = new uchar 128 0
448n = len m
449cnt = get c 8
450bs = ulong 128
451if eq get c 9 256
452bs = 64
453end
454bi = and cnt sub bs 1
455put c 8 add cnt n
456off = ulong 0
457if gt bi 0
458off = sub bs bi
459if lt n off
460off = n
461end
462sha2_fill c bi m off
463if eq add bi off bs
464for i ulong bs
465put blk i sha2_bget c i
466end
467sha2_run c part blk 0 bs
468for i ulong bs
469put blk i 0
470end
471end
472end
473nb = and sub n off not sub bs 1
474sha2_run c part m off nb
475off = add off nb
476sha2_fill c 0 part m off sub n off sub n off
477end
478
479note H(N) as big-endian bytes: 32 (SHA-256, 8 words of 32 bits), 48 (SHA-384, first 6 words), 64
480fn sha2_put c view ulong * o edit uchar * to ulong
481alg = get c 9
482sh = ulong 0
483if eq alg 256
484for i ulong 32
485sh = sub 24 shl and i 3 3
486put o i uchar and shr get c shr i 2 sh 0xff
487end
488ret 32
489end
490dl = ulong 64
491if eq alg 384
492dl = 48
493end
494for i ulong dl
495sh = sub 56 shl and i 7 3
496put o i uchar and shr get c shr i 3 sh 0xff
497end
498ret dl
499end
500
501note padding (FIPS 180-4 5.1.1 / 5.1.2): 0x80, zeros, the bit length big-endian in the last 8 (SHA-256)
502note or 16 (SHA-384/512) bytes of one or two blocks; returns the digest length
503fn sha2_fin c edit ulong * o edit uchar * to ulong
504p = new uchar 256 0
505bs = ulong 128
506ls = ulong 16
507if eq get c 9 256
508bs = 64
509ls = 8
510end
511cnt = get c 8
512k = and cnt sub bs 1
513for i ulong k
514put p i sha2_bget c i
515end
516put p k 0x80
517e = bs
518if ge add k ls bs
519e = shl bs 1
520end
521x = shl cnt 3
522for i ulong 8 unroll
523put p sub e add i 1 uchar and shr x mul i 8 0xff
524end
525if eq ls 16
526put p sub e 9 uchar shr cnt 61
527end
528sha2_run c part p 0 e
529for i ulong 256
530put p i 0
531end
532ret sha2_put c o
533end
534
535fn sha256_init c edit ulong * to void
536sha2_init c 256
537end
538
539fn sha384_init c edit ulong * to void
540sha2_init c 384
541end
542
543fn sha512_init c edit ulong * to void
544sha2_init c 512
545end
546
547fn sha256_update c edit ulong * m view uchar * to void
548sha2_update c m
549end
550
551fn sha384_update c edit ulong * m view uchar * to void
552sha2_update c m
553end
554
555fn sha512_update c edit ulong * m view uchar * to void
556sha2_update c m
557end
558
559fn sha256_final c edit ulong * o edit uchar * to void
560dl = sha2_fin c o
561end
562
563fn sha384_final c edit ulong * o edit uchar * to void
564dl = sha2_fin c o
565end
566
567fn sha512_final c edit ulong * o edit uchar * to void
568dl = sha2_fin c o
569end
570
571fn sha2_copy d edit ulong * s view ulong * to void
572for i ulong len s
573put d i get s i
574end
575end
576
577fn sha2_digest alg ulong m view uchar * o edit uchar * to ulong
578c = new ulong 40 0
579if ne alg 256
580if ne alg 384
581if ne alg 512
582ret 0
583end
584end
585end
586sha2_init c alg
587sha2_update c m
588dl = sha2_fin c o
589for i ulong 40
590put c i 0
591end
592ret dl
593end
594note bench: SHA-256 of 64 MiB (a 1 MiB pattern absorbed 64 times) through om/tls/sha2.om
595fn main to int
596b = new uchar 1048576 0
597for i ulong 1048576
598put b i uchar and add mul i 31 7 255
599end
600c = new ulong 40 0
601o = new uchar 32 0
602sha256_init c
603for r ulong 64
604sha256_update c b
605end
606sha256_final c o
607w = write 1 o 0 32
608ret 0
609end