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