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 / src / train / tg_sha256.om

366 lines · 7506 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Pure seeded OM-2 streaming SHA-256 (FIPS 180-4) for M22 train and M20 tensor.
3note Context layout (uchar 104):
4note bytes 0..31 h[0..7] little-endian uint32
5note bytes 32..39 total byte count little-endian ulong
6note bytes 40..103 64-byte pending block buffer
7
8fn sha_k idx ulong to uint
9 if lt idx 16
10 if lt idx 8
11 if eq idx 0
12 ret uint 0x428a2f98
13 end
14 if eq idx 1
15 ret uint 0x71374491
16 end
17 if eq idx 2
18 ret uint 0xb5c0fbcf
19 end
20 if eq idx 3
21 ret uint 0xe9b5dba5
22 end
23 if eq idx 4
24 ret uint 0x3956c25b
25 end
26 if eq idx 5
27 ret uint 0x59f111f1
28 end
29 if eq idx 6
30 ret uint 0x923f82a4
31 end
32 ret uint 0xab1c5ed5
33 end
34 if eq idx 8
35 ret uint 0xd807aa98
36 end
37 if eq idx 9
38 ret uint 0x12835b01
39 end
40 if eq idx 10
41 ret uint 0x243185be
42 end
43 if eq idx 11
44 ret uint 0x550c7dc3
45 end
46 if eq idx 12
47 ret uint 0x72be5d74
48 end
49 if eq idx 13
50 ret uint 0x80deb1fe
51 end
52 if eq idx 14
53 ret uint 0x9bdc06a7
54 end
55 ret uint 0xc19bf174
56 end
57 if lt idx 32
58 if lt idx 24
59 if eq idx 16
60 ret uint 0xe49b69c1
61 end
62 if eq idx 17
63 ret uint 0xefbe4786
64 end
65 if eq idx 18
66 ret uint 0x0fc19dc6
67 end
68 if eq idx 19
69 ret uint 0x240ca1cc
70 end
71 if eq idx 20
72 ret uint 0x2de92c6f
73 end
74 if eq idx 21
75 ret uint 0x4a7484aa
76 end
77 if eq idx 22
78 ret uint 0x5cb0a9dc
79 end
80 ret uint 0x76f988da
81 end
82 if eq idx 24
83 ret uint 0x983e5152
84 end
85 if eq idx 25
86 ret uint 0xa831c66d
87 end
88 if eq idx 26
89 ret uint 0xb00327c8
90 end
91 if eq idx 27
92 ret uint 0xbf597fc7
93 end
94 if eq idx 28
95 ret uint 0xc6e00bf3
96 end
97 if eq idx 29
98 ret uint 0xd5a79147
99 end
100 if eq idx 30
101 ret uint 0x06ca6351
102 end
103 ret uint 0x14292967
104 end
105 if lt idx 48
106 if lt idx 40
107 if eq idx 32
108 ret uint 0x27b70a85
109 end
110 if eq idx 33
111 ret uint 0x2e1b2138
112 end
113 if eq idx 34
114 ret uint 0x4d2c6dfc
115 end
116 if eq idx 35
117 ret uint 0x53380d13
118 end
119 if eq idx 36
120 ret uint 0x650a7354
121 end
122 if eq idx 37
123 ret uint 0x766a0abb
124 end
125 if eq idx 38
126 ret uint 0x81c2c92e
127 end
128 ret uint 0x92722c85
129 end
130 if eq idx 40
131 ret uint 0xa2bfe8a1
132 end
133 if eq idx 41
134 ret uint 0xa81a664b
135 end
136 if eq idx 42
137 ret uint 0xc24b8b70
138 end
139 if eq idx 43
140 ret uint 0xc76c51a3
141 end
142 if eq idx 44
143 ret uint 0xd192e819
144 end
145 if eq idx 45
146 ret uint 0xd6990624
147 end
148 if eq idx 46
149 ret uint 0xf40e3585
150 end
151 ret uint 0x106aa070
152 end
153 if lt idx 56
154 if eq idx 48
155 ret uint 0x19a4c116
156 end
157 if eq idx 49
158 ret uint 0x1e376c08
159 end
160 if eq idx 50
161 ret uint 0x2748774c
162 end
163 if eq idx 51
164 ret uint 0x34b0bcb5
165 end
166 if eq idx 52
167 ret uint 0x391c0cb3
168 end
169 if eq idx 53
170 ret uint 0x4ed8aa4a
171 end
172 if eq idx 54
173 ret uint 0x5b9cca4f
174 end
175 ret uint 0x682e6ff3
176 end
177 if eq idx 56
178 ret uint 0x748f82ee
179 end
180 if eq idx 57
181 ret uint 0x78a5636f
182 end
183 if eq idx 58
184 ret uint 0x84c87814
185 end
186 if eq idx 59
187 ret uint 0x8cc70208
188 end
189 if eq idx 60
190 ret uint 0x90befffa
191 end
192 if eq idx 61
193 ret uint 0xa4506ceb
194 end
195 if eq idx 62
196 ret uint 0xbef9a3f7
197 end
198 ret uint 0xc67178f2
199end
200
201fn sha_get_u32le buf view uchar 104 off ulong to uint
202 b0 = uint get buf off
203 b1 = shl uint get buf add off 1 8
204 b2 = shl uint get buf add off 2 16
205 b3 = shl uint get buf add off 3 24
206 ret or or b0 b1 or b2 b3
207end
208
209fn sha_put_u32le buf edit uchar 104 off ulong v uint to void
210 put buf off uchar and v 255
211 put buf add off 1 uchar and shr v 8 255
212 put buf add off 2 uchar and shr v 16 255
213 put buf add off 3 uchar and shr v 24 255
214end
215
216fn sha_get_u64le buf view uchar 104 off ulong to ulong
217 v = ulong 0
218 for i ulong 8
219 v = or v shl ulong get buf add off i mul i 8
220 end
221 ret v
222end
223
224fn sha_put_u64le buf edit uchar 104 off ulong v ulong to void
225 for i ulong 8
226 put buf add off i uchar and shr v mul i 8 255
227 end
228end
229
230fn sha_expand blk view uchar 64 w edit uint 64 to void
231 for i ulong 16
232 p = mul i 4
233 v = shl uint get blk p 24
234 v = or v shl uint get blk add p 1 16
235 v = or v shl uint get blk add p 2 8
236 v = or v uint get blk add p 3
237 put w i v
238 end
239 for j ulong 48
240 i = add j 16
241 x = get w sub i 15
242 s0 = xor xor rotr x 7 rotr x 18 shr x 3
243 y = get w sub i 2
244 s1 = xor xor rotr y 17 rotr y 19 shr y 10
245 v = add get w sub i 16 s0
246 v = add v get w sub i 7
247 v = add v s1
248 put w i v
249 end
250end
251
252fn sha_round st edit uint 8 ki uint wi uint to void
253 e = get st 4
254 t1 = xor xor rotr e 6 rotr e 11 rotr e 25
255 t1 = add t1 xor and e get st 5 and not e get st 6
256 t1 = add t1 get st 7
257 t1 = add t1 add ki wi
258 a = get st 0
259 t2 = xor xor rotr a 2 rotr a 13 rotr a 22
260 b = get st 1
261 c = get st 2
262 t2 = add t2 xor xor and a b and a c and b c
263 put st 7 get st 6
264 put st 6 get st 5
265 put st 5 e
266 put st 4 add get st 3 t1
267 put st 3 c
268 put st 2 b
269 put st 1 a
270 put st 0 add t1 t2
271end
272
273fn sha_compress ctx edit uchar 104 blk view uchar 64 to void
274 w = new uint 64 0
275 st = new uint 8 0
276 sha_expand blk w
277 for i ulong 8
278 put st i sha_get_u32le ctx mul i 4
279 end
280 for i ulong 64
281 sha_round st sha_k i get w i
282 end
283 for i ulong 8
284 v = add sha_get_u32le ctx mul i 4 get st i
285 sha_put_u32le ctx mul i 4 v
286 end
287end
288
289fn tg_sha_init ctx edit uchar 104 to void
290 for i ulong 104
291 put ctx i 0
292 end
293 sha_put_u32le ctx 0 uint 0x6a09e667
294 sha_put_u32le ctx 4 uint 0xbb67ae85
295 sha_put_u32le ctx 8 uint 0x3c6ef372
296 sha_put_u32le ctx 12 uint 0xa54ff53a
297 sha_put_u32le ctx 16 uint 0x510e527f
298 sha_put_u32le ctx 20 uint 0x9b05688c
299 sha_put_u32le ctx 24 uint 0x1f83d9ab
300 sha_put_u32le ctx 28 uint 0x5be0cd19
301end
302
303fn sha_compress_ctx ctx edit uchar 104 to void
304 blk = new uchar 64 0
305 for j ulong 64
306 put blk j get ctx add 40 j
307 end
308 sha_compress ctx blk
309end
310
311fn tg_sha_update ctx edit uchar 104 data view uchar * n ulong to void
312 cnt = sha_get_u64le ctx 32
313 used = and cnt 63
314 sha_put_u64le ctx 32 add cnt n
315 pos = ulong 0
316 while lt pos n bound 16777216
317 b = get data pos
318 put ctx add 40 used b
319 used = add used 1
320 pos = add pos 1
321 if eq used 64
322 sha_compress_ctx ctx
323 used = 0
324 end
325 end
326end
327
328fn tg_sha_final ctx edit uchar 104 dig edit uchar 32 to void
329 blk = new uchar 64 0
330 cnt = sha_get_u64le ctx 32
331 used = and cnt 63
332 for j ulong 64
333 if lt j used
334 put blk j get ctx add 40 j
335 else
336 put blk j 0
337 end
338 end
339 put blk used 128
340 if ge used 56
341 sha_compress ctx blk
342 for j ulong 64
343 put blk j 0
344 end
345 end
346 bits = shl cnt 3
347 for j ulong 8
348 put blk sub 63 j uchar and shr bits mul j 8 255
349 end
350 sha_compress ctx blk
351 for i ulong 8
352 v = sha_get_u32le ctx mul i 4
353 p = mul i 4
354 put dig p uchar and shr v 24 255
355 put dig add p 1 uchar and shr v 16 255
356 put dig add p 2 uchar and shr v 8 255
357 put dig add p 3 uchar and v 255
358 end
359end
360
361fn tg_sha_hash data view uchar * n ulong dig edit uchar 32 to void
362 ctx = new uchar 104 0
363 tg_sha_init ctx
364 tg_sha_update ctx data n
365 tg_sha_final ctx dig
366end