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 / front / runs / sha-s.om

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