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 / om / tls / sha3.om

317 lines · 9713 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/sha3.om: SHA3-256, SHA3-512, SHAKE128, SHAKE256 (FIPS 202) as an OM-2 library unit (no om2
3note line, no main, no globals). Module code S3; functions sha3_*. Composition: after x25519, before mlkem.
4note
5note API (c: S3CN = 29 ulong cells, caller owns all state)
6note sha3_init c edit ulong * rate ulong dom ulong rate in bytes (multiple of 8, 8..200), dom the
7note domain byte with the first pad bit (0x06 SHA3, 0x1f SHAKE)
8note sha3_256_init / sha3_512_init / sha3_shake128_init / sha3_shake256_init c the four FIPS 202 instances
9note sha3_absorb c edit ulong * m view uchar * any length, any chunking, before the first squeeze
10note sha3_squeeze c edit ulong * o edit uchar * fills all of o; the first call pads and permutes;
11note later calls continue the output stream (XOF)
12note sha3_256 m view uchar * o edit uchar * one-shot, writes o[0..32) (o shorter traps 3)
13note sha3_512 m view uchar * o edit uchar * one-shot, writes o[0..64)
14note sha3_shake128 / sha3_shake256 m o one-shot XOF, writes all len o bytes
15note Absorbing after a squeeze is a caller error (it xors into the squeeze state; not detected).
16note
17note Context cells: 0..24 the Keccak-f[1600] lanes A[x + 5y] (FIPS 202 3.1.2: byte p of the state is
18note bits 8 (p mod 8) .. of lane p / 8, little-endian), 25 rate in bytes, 26 byte position in the
19note current block, 27 domain byte, 28 phase (0 absorbing, 1 squeezing).
20note Padding (FIPS 202 5.1 pad10*1 with the 6.1 / 6.2 suffix bits 01 / 1111): dom xor at the position,
21note 0x80 xor at byte rate - 1 (one byte 0x86 / 0x9f when they coincide).
22note Constant time: every branch, loop count and index depends only on lengths, the rate and the
23note position (public counts), never on message or state bytes. The local state, theta and pi
24note arrays are zeroed before return; the context is the caller's to wipe. omc emits plain AArch64;
25note we rely on EOR/AND/BIC-style ops, ROR and shifts being data-independent (no DIT bit is set).
26note Message length: each absorb or squeeze call runs in chunks of 2^19 bytes (a rolled for traps 4
27note after 10^6 passes; while bound 999999 chunks).
28
29const S3CN ulong 29
30
31note RC[ir] for ir 0..23 (FIPS 202 3.2.5, rc(t) from the LFSR x^8 + x^6 + x^5 + x^4 + 1, bit 2^j - 1
32note of RC[ir] = rc(j + 7 ir)). Computed with the Wolfram Language evaluator (2026-10-26):
33note rcbit[t_] (Algorithm 5); rc=Table[Sum[rcbit[j+7 ir] 2^(2^j-1),{j,0,6}],{ir,0,23}] -> the values below.
34fn sha3_rcs k edit ulong 24 to void
35put k 0 0x0000000000000001
36put k 1 0x0000000000008082
37put k 2 0x800000000000808a
38put k 3 0x8000000080008000
39put k 4 0x000000000000808b
40put k 5 0x0000000080000001
41put k 6 0x8000000080008081
42put k 7 0x8000000000008009
43put k 8 0x000000000000008a
44put k 9 0x0000000000000088
45put k 10 0x0000000080008009
46put k 11 0x000000008000000a
47put k 12 0x000000008000808b
48put k 13 0x800000000000008b
49put k 14 0x8000000000008089
50put k 15 0x8000000000008003
51put k 16 0x8000000000008002
52put k 17 0x8000000000000080
53put k 18 0x000000000000800a
54put k 19 0x800000008000000a
55put k 20 0x8000000080008081
56put k 21 0x8000000000008080
57put k 22 0x0000000080000001
58put k 23 0x8000000080008008
59end
60
61note theta (FIPS 202 3.2.1): C[x] = xor of column x, D[x] = C[x-1] xor rot(C[x+1], 1), A[x,y] ^= D[x]
62fn sha3_theta a edit ulong 25 t edit ulong 5 to void
63put t 0 xor xor xor xor get a 0 get a 5 get a 10 get a 15 get a 20
64put t 1 xor xor xor xor get a 1 get a 6 get a 11 get a 16 get a 21
65put t 2 xor xor xor xor get a 2 get a 7 get a 12 get a 17 get a 22
66put t 3 xor xor xor xor get a 3 get a 8 get a 13 get a 18 get a 23
67put t 4 xor xor xor xor get a 4 get a 9 get a 14 get a 19 get a 24
68d = xor get t 4 rotl get t 1 1
69put a 0 xor get a 0 d
70put a 5 xor get a 5 d
71put a 10 xor get a 10 d
72put a 15 xor get a 15 d
73put a 20 xor get a 20 d
74d = xor get t 0 rotl get t 2 1
75put a 1 xor get a 1 d
76put a 6 xor get a 6 d
77put a 11 xor get a 11 d
78put a 16 xor get a 16 d
79put a 21 xor get a 21 d
80d = xor get t 1 rotl get t 3 1
81put a 2 xor get a 2 d
82put a 7 xor get a 7 d
83put a 12 xor get a 12 d
84put a 17 xor get a 17 d
85put a 22 xor get a 22 d
86d = xor get t 2 rotl get t 4 1
87put a 3 xor get a 3 d
88put a 8 xor get a 8 d
89put a 13 xor get a 13 d
90put a 18 xor get a 18 d
91put a 23 xor get a 23 d
92d = xor get t 3 rotl get t 0 1
93put a 4 xor get a 4 d
94put a 9 xor get a 9 d
95put a 14 xor get a 14 d
96put a 19 xor get a 19 d
97put a 24 xor get a 24 d
98end
99
100note rho and pi (3.2.2, 3.2.3): B[y, 2x + 3y] = rot(A[x, y], r[x, y]). Offsets r[x + 5y] from the 3.2.2
101note walk, Wolfram: (x,y)=(1,0); rho[x,y]=Mod[(t+1)(t+2)/2,64]; (x,y)=(y,Mod[2x+3y,5]) for t 0..23 ->
102note 0 1 62 28 27 36 44 6 55 20 3 10 43 25 39 41 45 15 21 8 18 2 61 56 14 (index x + 5y).
103fn sha3_rhopi a view ulong 25 b edit ulong 25 to void
104put b 0 get a 0
105put b 10 rotl get a 1 1
106put b 20 rotl get a 2 62
107put b 5 rotl get a 3 28
108put b 15 rotl get a 4 27
109put b 16 rotl get a 5 36
110put b 1 rotl get a 6 44
111put b 11 rotl get a 7 6
112put b 21 rotl get a 8 55
113put b 6 rotl get a 9 20
114put b 7 rotl get a 10 3
115put b 17 rotl get a 11 10
116put b 2 rotl get a 12 43
117put b 12 rotl get a 13 25
118put b 22 rotl get a 14 39
119put b 23 rotl get a 15 41
120put b 8 rotl get a 16 45
121put b 18 rotl get a 17 15
122put b 3 rotl get a 18 21
123put b 13 rotl get a 19 8
124put b 14 rotl get a 20 18
125put b 24 rotl get a 21 2
126put b 9 rotl get a 22 61
127put b 19 rotl get a 23 56
128put b 4 rotl get a 24 14
129end
130
131note chi (3.2.4): A[x,y] = B[x,y] xor ((not B[x+1,y]) and B[x+2,y])
132fn sha3_chi a edit ulong 25 b view ulong 25 to void
133put a 0 xor get b 0 and not get b 1 get b 2
134put a 1 xor get b 1 and not get b 2 get b 3
135put a 2 xor get b 2 and not get b 3 get b 4
136put a 3 xor get b 3 and not get b 4 get b 0
137put a 4 xor get b 4 and not get b 0 get b 1
138put a 5 xor get b 5 and not get b 6 get b 7
139put a 6 xor get b 6 and not get b 7 get b 8
140put a 7 xor get b 7 and not get b 8 get b 9
141put a 8 xor get b 8 and not get b 9 get b 5
142put a 9 xor get b 9 and not get b 5 get b 6
143put a 10 xor get b 10 and not get b 11 get b 12
144put a 11 xor get b 11 and not get b 12 get b 13
145put a 12 xor get b 12 and not get b 13 get b 14
146put a 13 xor get b 13 and not get b 14 get b 10
147put a 14 xor get b 14 and not get b 10 get b 11
148put a 15 xor get b 15 and not get b 16 get b 17
149put a 16 xor get b 16 and not get b 17 get b 18
150put a 17 xor get b 17 and not get b 18 get b 19
151put a 18 xor get b 18 and not get b 19 get b 15
152put a 19 xor get b 19 and not get b 15 get b 16
153put a 20 xor get b 20 and not get b 21 get b 22
154put a 21 xor get b 21 and not get b 22 get b 23
155put a 22 xor get b 22 and not get b 23 get b 24
156put a 23 xor get b 23 and not get b 24 get b 20
157put a 24 xor get b 24 and not get b 20 get b 21
158end
159
160note Keccak-f[1600] on cells 0..24 of c: 24 rounds theta, rho, pi, chi, iota (3.3, 3.4)
161fn sha3_perm c edit ulong * to void
162a = new ulong 25 0
163b = new ulong 25 0
164t = new ulong 5 0
165k = new ulong 24 0
166sha3_rcs k
167for i ulong 25 unroll
168put a i get c i
169end
170for r ulong 24
171sha3_theta a t
172sha3_rhopi a b
173sha3_chi a b
174put a 0 xor get a 0 get k r
175end
176for i ulong 25 unroll
177put c i get a i
178put a i 0
179put b i 0
180end
181for i ulong 5 unroll
182put t i 0
183end
184end
185
186fn sha3_init c edit ulong * rate ulong dom ulong to void
187for i ulong S3CN
188put c i 0
189end
190put c 25 rate
191put c 27 dom
192end
193
194fn sha3_256_init c edit ulong * to void
195sha3_init c 136 6
196end
197
198fn sha3_512_init c edit ulong * to void
199sha3_init c 72 6
200end
201
202fn sha3_shake128_init c edit ulong * to void
203sha3_init c 168 31
204end
205
206fn sha3_shake256_init c edit ulong * to void
207sha3_init c 136 31
208end
209
210note absorb m (at most 2^19 bytes): xor each byte into the state at the position, permute at the rate
211fn sha3_abs1 c edit ulong * m view uchar * to void
212r = get c 25
213p = get c 26
214for i ulong len m
215q = shr p 3
216put c q xor get c q shl ulong get m i shl and p 7 3
217p = add p 1
218if eq p r
219sha3_perm c
220p = 0
221end
222end
223put c 26 p
224end
225
226fn sha3_absorb c edit ulong * m view uchar * to void
227n = len m
228q = ulong 0
229k = ulong 0
230while lt q n bound 999999
231k = sub n q
232if gt k 0x80000
233k = 0x80000
234end
235sha3_abs1 c part m q k
236q = add q k
237end
238end
239
240note pad10*1 with the domain bits, the last absorbing permutation, then the squeezing phase
241fn sha3_pad c edit ulong * to void
242p = get c 26
243r = get c 25
244q = shr p 3
245put c q xor get c q shl get c 27 shl and p 7 3
246q = shr sub r 1 3
247put c q xor get c q shl ulong 0x80 shl and sub r 1 7 3
248sha3_perm c
249put c 26 0
250put c 28 1
251end
252
253note squeeze o (at most 2^19 bytes) from the position
254fn sha3_sq1 c edit ulong * o edit uchar * to void
255r = get c 25
256p = get c 26
257for i ulong len o
258if eq p r
259sha3_perm c
260p = 0
261end
262x = shr get c shr p 3 shl and p 7 3
263put o i uchar and x 0xff
264p = add p 1
265end
266put c 26 p
267end
268
269fn sha3_squeeze c edit ulong * o edit uchar * to void
270if eq get c 28 0
271sha3_pad c
272end
273n = len o
274q = ulong 0
275k = ulong 0
276while lt q n bound 999999
277k = sub n q
278if gt k 0x80000
279k = 0x80000
280end
281sha3_sq1 c part o q k
282q = add q k
283end
284end
285
286note one-shot: c initialised by the caller; absorbs m, squeezes o, wipes c
287fn sha3_once c edit ulong * m view uchar * o edit uchar * to void
288sha3_absorb c m
289sha3_squeeze c o
290for i ulong S3CN
291put c i 0
292end
293end
294
295fn sha3_256 m view uchar * o edit uchar * to void
296c = new ulong S3CN 0
297sha3_256_init c
298sha3_once c m part o 0 32
299end
300
301fn sha3_512 m view uchar * o edit uchar * to void
302c = new ulong S3CN 0
303sha3_512_init c
304sha3_once c m part o 0 64
305end
306
307fn sha3_shake128 m view uchar * o edit uchar * to void
308c = new ulong S3CN 0
309sha3_shake128_init c
310sha3_once c m o
311end
312
313fn sha3_shake256 m view uchar * o edit uchar * to void
314c = new ulong S3CN 0
315sha3_shake256_init c
316sha3_once c m o
317end