interplane.Get Omega
Read in your language

mailhand

mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

mailhand / vendor / omega-mv / om / sha256 / sha256-om2.om

344 lines · 9583 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 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 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
21om2
22
23layout shactx 104
24field state uint 8 at 0 little
25field count ulong at 32 little
26field buf uchar 64 at 40 little
27end
28
29note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
30note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed).
31fn kinit 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 H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes
99note (fixtures/sha256-iv.dec).
100fn iv s edit uint 8 to void
101put s 0 0x6a09e667
102put s 1 0xbb67ae85
103put s 2 0x3c6ef372
104put s 3 0xa54ff53a
105put s 4 0x510e527f
106put s 5 0x9b05688c
107put s 6 0x1f83d9ab
108put s 7 0x5be0cd19
109end
110
111note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read.
112note The compiler proves that every get of w reads an element written earlier in this function
113note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence.
114fn sched w edit uint 64 fresh m view uchar * o ulong to void
115q = ulong 0
116x = uint 0
117y = uint 0
118for i ulong 16 unroll
119q = add o mul i 4
120x = uint get m q
121x = or shl x 8 uint get m add q 1
122x = or shl x 8 uint get m add q 2
123put w i or shl x 8 uint get m add q 3
124end
125for i ulong 48 unroll
126x = get w add i 1
127y = get w add i 14
128x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
129put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
130end
131end
132
133note The 64 rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant
134note in each copy, so get kt i and get w i need no run-time check. uint arithmetic wraps mod 2^32.
135note 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) =
136note (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
137note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one.
138note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and
139note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines).
140fn rounds s edit uint 8 w view uint 64 kt view uint 64 to void
141a = get s 0
142b = get s 1
143c = get s 2
144d = get s 3
145e = get s 4
146f = get s 5
147g = get s 6
148h = get s 7
149t = uint 0
150for i ulong 64 unroll
151h = add h get kt i
152h = add h get w i
153t = xor rotr e 6 rotr e 11
154t = xor t rotr e 25
155h = add h t
156h = add h xor g and e xor f g
157d = add d h
158t = xor rotr a 2 rotr a 13
159t = xor t rotr a 22
160h = add h t
161t = and c or a b
162h = add h or and a b t
163t = h
164h = g
165g = f
166f = e
167e = d
168d = c
169c = b
170b = a
171a = t
172end
173put s 0 add get s 0 a
174put s 1 add get s 1 b
175put s 2 add get s 2 c
176put s 3 add get s 3 d
177put s 4 add get s 4 e
178put s 5 add get s 5 f
179put s 6 add get s 6 g
180put s 7 add get s 7 h
181end
182
183note One block m[o .. o+64) into s. sched and rounds stay calls: inlined, their names would share
184note one 16-register frame with this function's parameters (no spilling, error 12).
185fn compress s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
186sched w m o
187rounds s w kt
188end
189
190note The whole blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes).
191fn run s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
192for j ulong shr len m 6
193compress s w kt m shl j 6
194end
195end
196
197note The whole blocks of m (len m a multiple of 64). A rolled loop traps 4 after 1000000 passes
198note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 blocks):
199note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches 1000000
200note only above 1.7e19 bytes, which no AArch64 address space holds.
201fn blocks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
202q = ulong 0
203for a ulong shr len m 44
204for b ulong 0x80000
205run s w kt part m q 0x2000000
206q = add q 0x2000000
207end
208end
209for b ulong and shr len m 25 0x7ffff
210run s w kt part m q 0x2000000
211q = add q 0x2000000
212end
213run s w kt part m q sub len m q
214end
215
216note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the
217note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two blocks.
218fn finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void
219put p k 0x80
220e = ulong 64
221if ge k 56
222e = 128
223end
224bits = shl n 3
225for i ulong 8 unroll
226put p sub e add i 1 uchar and shr bits mul i 8 0xff
227end
228compress s w kt view p 0
229if eq e 128
230compress s w kt view p 64
231end
232end
233
234export fn sha256_init c edit shactx to void
235s = new uint 8 0
236iv s
237for i ulong 8 unroll
238put c state i get s i
239end
240put c count 0
241end
242
243note k bytes of d from o into the context's buffer at bi (bi + k <= 64). A function of its own: inside
244note absorb the copy loop needs more than the 16 registers (error 12 under compiler-s4).
245fn tobuf c edit shactx bi ulong d view uchar * o ulong k ulong to void
246for i ulong k
247put c buf add bi i get d add o i
248end
249end
250
251note Whole blocks are hashed straight from d; the bytes that complete a pending block go through blk.
252fn absorb c edit shactx d view uchar * bi ulong to void
253n = len d
254s = new uint 8 0
255w = new uint 64 0
256kt = new uint 64 0
257blk = new uchar 64 0
258kinit kt
259for i ulong 8 unroll
260put s i get c state i
261end
262off = ulong 0
263if gt bi 0
264off = sub 64 bi
265for i ulong bi
266put blk i get c buf i
267end
268for i ulong off
269put blk add bi i get d i
270end
271compress s w kt view blk 0
272end
273nb = shr sub n off 6
274blocks s w kt part d off shl nb 6
275off = add off shl nb 6
276tobuf c 0 d off sub n off
277for i ulong 8 unroll
278put c state i get s i
279end
280end
281
282note Any length, including 0 and >= 2^32. lt n sub 64 bi cannot wrap (bi < 64); add bi n could.
283export fn sha256_update c edit shactx d view uchar * to void
284n = len d
285cnt = get c count
286bi = and cnt 63
287put c count add cnt n
288if lt n sub 64 bi
289tobuf c bi d 0 n
290ret
291end
292absorb c d bi
293end
294
295export fn sha256_final c edit shactx o out uchar 32 to void
296s = new uint 8 0
297w = new uint 64 0
298kt = new uint 64 0
299p = new uchar 128 0
300v = ulong 0
301kinit kt
302for i ulong 8 unroll
303put s i get c state i
304end
305cnt = get c count
306k = and cnt 63
307for i ulong k
308put p i get c buf i
309end
310finish s w kt p k cnt
311for i ulong 8 unroll
312v = ulong get s i
313for j ulong 4 unroll
314put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
315end
316end
317end
318
319note Equal to init, update(d), final on a fresh context (section 7), with the state in locals. o may
320note overlap d: the binding copies o out only after the call returns status 0.
321export fn sha256_hash d view uchar * o out uchar 32 to void
322s = new uint 8 0
323w = new uint 64 0
324kt = new uint 64 0
325p = new uchar 128 0
326v = ulong 0
327kinit kt
328iv s
329n = len d
330nb = shr n 6
331blocks s w kt part d 0 shl nb 6
332off = shl nb 6
333k = sub n off
334for i ulong k
335put p i get d add off i
336end
337finish s w kt p k n
338for i ulong 8 unroll
339v = ulong get s i
340for j ulong 4 unroll
341put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
342end
343end
344end