interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / sha256.om

343 lines · 9727 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note sha256-om2.om: FIPS 180-4 SHA-256 in OM-2 (spec/omegese-om2.md, #747/#748, ledger L838).
3note The four entries are the API of src/sha256.h, exported through omega-bridge ABI v2 as
4note omega2_sha256_init/update/final/hash; the bridge's generated omega_api.h maps the C names.
5note
6note Invariants:
7note - All hashing state is the caller's 104-byte context (layout shactx below, equal to sha256_ctx
8note on LP64 AArch64). Nothing else persists between calls, so a byte copy of a live context is an
9note equivalent context (erratum E3).
10note - count is the number of bytes absorbed since init, modulo 2^64. buf[0 .. count mod 64) holds the
11note pending bytes of the current block; the rest of buf is unspecified. state is the chaining value
12note after the count div 64 whole sha_blocks.
13note - final reads the context and writes only the digest, so the context is unchanged by it. The
14note contract promises less (after final the context is unspecified, memory safety only).
15note - Every branch and every loop count depends only on lengths, never on message bytes (section 8).
16note - Indices into the unsized input view are checked at sha_run time (trap 3); indices into sized
17note arrays inside unrolled bodies are constants and checked when compiled.
18note bridge c-type shactx sha256_ctx
19note bridge c-define SHA256_DIGEST_SIZE 32
20note bridge c-define SHA256_BLOCK_SIZE 64
21
22layout shactx 104
23field state uint 8 at 0 little
24field count ulong at 32 little
25field buf uchar 64 at 40 little
26end
27
28note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
29note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed).
30fn sha_kinit k edit uint 64 to void
31put k 0 0x428a2f98
32put k 1 0x71374491
33put k 2 0xb5c0fbcf
34put k 3 0xe9b5dba5
35put k 4 0x3956c25b
36put k 5 0x59f111f1
37put k 6 0x923f82a4
38put k 7 0xab1c5ed5
39put k 8 0xd807aa98
40put k 9 0x12835b01
41put k 10 0x243185be
42put k 11 0x550c7dc3
43put k 12 0x72be5d74
44put k 13 0x80deb1fe
45put k 14 0x9bdc06a7
46put k 15 0xc19bf174
47put k 16 0xe49b69c1
48put k 17 0xefbe4786
49put k 18 0x0fc19dc6
50put k 19 0x240ca1cc
51put k 20 0x2de92c6f
52put k 21 0x4a7484aa
53put k 22 0x5cb0a9dc
54put k 23 0x76f988da
55put k 24 0x983e5152
56put k 25 0xa831c66d
57put k 26 0xb00327c8
58put k 27 0xbf597fc7
59put k 28 0xc6e00bf3
60put k 29 0xd5a79147
61put k 30 0x06ca6351
62put k 31 0x14292967
63put k 32 0x27b70a85
64put k 33 0x2e1b2138
65put k 34 0x4d2c6dfc
66put k 35 0x53380d13
67put k 36 0x650a7354
68put k 37 0x766a0abb
69put k 38 0x81c2c92e
70put k 39 0x92722c85
71put k 40 0xa2bfe8a1
72put k 41 0xa81a664b
73put k 42 0xc24b8b70
74put k 43 0xc76c51a3
75put k 44 0xd192e819
76put k 45 0xd6990624
77put k 46 0xf40e3585
78put k 47 0x106aa070
79put k 48 0x19a4c116
80put k 49 0x1e376c08
81put k 50 0x2748774c
82put k 51 0x34b0bcb5
83put k 52 0x391c0cb3
84put k 53 0x4ed8aa4a
85put k 54 0x5b9cca4f
86put k 55 0x682e6ff3
87put k 56 0x748f82ee
88put k 57 0x78a5636f
89put k 58 0x84c87814
90put k 59 0x8cc70208
91put k 60 0x90befffa
92put k 61 0xa4506ceb
93put k 62 0xbef9a3f7
94put k 63 0xc67178f2
95end
96
97note H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes
98note (fixtures/sha256-sha_iv.dec).
99fn sha_iv s edit uint 8 to void
100put s 0 0x6a09e667
101put s 1 0xbb67ae85
102put s 2 0x3c6ef372
103put s 3 0xa54ff53a
104put s 4 0x510e527f
105put s 5 0x9b05688c
106put s 6 0x1f83d9ab
107put s 7 0x5be0cd19
108end
109
110note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read.
111note The compiler proves that every get of w reads an element written earlier in this function
112note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence.
113fn sha_sched w edit uint 64 fresh m view uchar * o ulong to void
114q = ulong 0
115x = uint 0
116y = uint 0
117for i ulong 16 unroll
118q = add o mul i 4
119x = uint get m q
120x = or shl x 8 uint get m add q 1
121x = or shl x 8 uint get m add q 2
122put w i or shl x 8 uint get m add q 3
123end
124for i ulong 48 unroll
125x = get w add i 1
126y = get w add i 14
127x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
128put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
129end
130end
131
132note The 64 sha_rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant
133note in each copy, so get kt i and get w i need no sha_run-time check. uint arithmetic wraps mod 2^32.
134note Ch(e,f,g) = (e and f) xor (not e and g) is written g xor (e and (f xor g)); Maj(a,b,c) =
135note (a and b) xor (a and c) xor (b and c) is written (a and b) or (c and (a or b)). Both are bitwise
136note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one.
137note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and
138note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines).
139fn sha_rounds s edit uint 8 w view uint 64 kt view uint 64 to void
140a = get s 0
141b = get s 1
142c = get s 2
143d = get s 3
144e = get s 4
145f = get s 5
146g = get s 6
147h = get s 7
148t = uint 0
149for i ulong 64 unroll
150h = add h get kt i
151h = add h get w i
152t = xor rotr e 6 rotr e 11
153t = xor t rotr e 25
154h = add h t
155h = add h xor g and e xor f g
156d = add d h
157t = xor rotr a 2 rotr a 13
158t = xor t rotr a 22
159h = add h t
160t = and c or a b
161h = add h or and a b t
162t = h
163h = g
164g = f
165f = e
166e = d
167d = c
168c = b
169b = a
170a = t
171end
172put s 0 add get s 0 a
173put s 1 add get s 1 b
174put s 2 add get s 2 c
175put s 3 add get s 3 d
176put s 4 add get s 4 e
177put s 5 add get s 5 f
178put s 6 add get s 6 g
179put s 7 add get s 7 h
180end
181
182note One block m[o .. o+64) into s. sha_sched and sha_rounds stay calls: inlined, their names would share
183note one 16-register frame with this function's parameters (no spilling, error 12).
184fn sha_compress s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
185sha_sched w m o
186sha_rounds s w kt
187end
188
189note The whole sha_blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes).
190fn sha_run s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
191for j ulong shr len m 6
192sha_compress s w kt m shl j 6
193end
194end
195
196note The whole sha_blocks of m (len m a multiple of 64). A rolled loop traps 4 after 1000000 passes
197note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 sha_blocks):
198note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches 1000000
199note only above 1.7e19 bytes, which no AArch64 address space holds.
200fn sha_blocks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
201q = ulong 0
202for a ulong shr len m 44
203for b ulong 0x80000
204sha_run s w kt part m q 0x2000000
205q = add q 0x2000000
206end
207end
208for b ulong and shr len m 25 0x7ffff
209sha_run s w kt part m q 0x2000000
210q = add q 0x2000000
211end
212sha_run s w kt part m q sub len m q
213end
214
215note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the
216note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two sha_blocks.
217fn sha_finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void
218put p k 0x80
219e = ulong 64
220if ge k 56
221e = 128
222end
223bits = shl n 3
224for i ulong 8 unroll
225put p sub e add i 1 uchar and shr bits mul i 8 0xff
226end
227sha_compress s w kt view p 0
228if eq e 128
229sha_compress s w kt view p 64
230end
231end
232
233fn sha256_init c edit shactx to void
234s = new uint 8 0
235sha_iv s
236for i ulong 8 unroll
237put c state i get s i
238end
239put c count 0
240end
241
242note k bytes of d from o into the context's buffer at bi (bi + k <= 64). A function of its own: inside
243note sha_absorb the copy loop needs more than the 16 registers (error 12 under compiler-s4).
244fn sha_tobuf c edit shactx bi ulong d view uchar * o ulong k ulong to void
245for i ulong k
246put c buf add bi i get d add o i
247end
248end
249
250note Whole sha_blocks are hashed straight from d; the bytes that complete a pending block go through blk.
251fn sha_absorb c edit shactx d view uchar * bi ulong to void
252n = len d
253s = new uint 8 0
254w = new uint 64 0
255kt = new uint 64 0
256blk = new uchar 64 0
257sha_kinit kt
258for i ulong 8 unroll
259put s i get c state i
260end
261off = ulong 0
262if gt bi 0
263off = sub 64 bi
264for i ulong bi
265put blk i get c buf i
266end
267for i ulong off
268put blk add bi i get d i
269end
270sha_compress s w kt view blk 0
271end
272nb = shr sub n off 6
273sha_blocks s w kt part d off shl nb 6
274off = add off shl nb 6
275sha_tobuf c 0 d off sub n off
276for i ulong 8 unroll
277put c state i get s i
278end
279end
280
281note Any length, including 0 and >= 2^32. lt n sub 64 bi cannot wrap (bi < 64); add bi n could.
282fn sha256_update c edit shactx d view uchar * to void
283n = len d
284cnt = get c count
285bi = and cnt 63
286put c count add cnt n
287if lt n sub 64 bi
288sha_tobuf c bi d 0 n
289ret
290end
291sha_absorb c d bi
292end
293
294fn sha256_final c edit shactx o out uchar 32 to void
295s = new uint 8 0
296w = new uint 64 0
297kt = new uint 64 0
298p = new uchar 128 0
299v = ulong 0
300sha_kinit kt
301for i ulong 8 unroll
302put s i get c state i
303end
304cnt = get c count
305k = and cnt 63
306for i ulong k
307put p i get c buf i
308end
309sha_finish s w kt p k cnt
310for i ulong 8 unroll
311v = ulong get s i
312for j ulong 4 unroll
313put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
314end
315end
316end
317
318note Equal to init, update(d), final on a fresh context (section 7), with the state in locals. o may
319note overlap d: the binding copies o out only after the call returns status 0.
320fn sha256_hash d view uchar * o out uchar 32 to void
321s = new uint 8 0
322w = new uint 64 0
323kt = new uint 64 0
324p = new uchar 128 0
325v = ulong 0
326sha_kinit kt
327sha_iv s
328n = len d
329nb = shr n 6
330sha_blocks s w kt part d 0 shl nb 6
331off = shl nb 6
332k = sub n off
333for i ulong k
334put p i get d add off i
335end
336sha_finish s w kt p k n
337for i ulong 8 unroll
338v = ulong get s i
339for j ulong 4 unroll
340put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
341end
342end
343end