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