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/seed/ng/front/runs/sha-s.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 / seed / ng / front / runs / sha-s.om` 610 lines ยท 14772 bytes Raw Blame History 1 om2 2 note sha-s: kernels/sha.om with 1 update of the 1 MiB buffer (was 64), for ngrun in gate-front-ir 3 note SPDX-License-Identifier: Apache-2.0 4 note om/tls/sha2.om: SHA-256, SHA-384, SHA-512 (FIPS 180-4) streaming, as an OM-2 library unit 5 note (no om2 line, no main, no globals). Module code SH; functions sha2_*, sha256_*, sha384_*, sha512_*. 6 note 7 note API 8 note sha256_init c edit ulong * c: 40 ulong cells (caller owns all state) 9 note sha256_update c edit ulong * m view uchar * any length, any chunking 10 note sha256_final c edit ulong * o edit uchar * writes 32 bytes (o shorter traps 3) 11 note sha384_* / sha512_* the same; final writes 48 / 64 bytes 12 note sha2_copy d edit ulong * s view ulong * copies len s cells (a context snapshot) 13 note sha2_digest alg ulong m view uchar * o edit uchar * to ulong 14 note alg 256|384|512: one-shot digest into o, returns 32|48|64; any other alg returns 0, writes nothing 15 note After final the context is spent (its state cells hold the last chaining value); init it again. 16 note A context initialised for one algorithm must be used with that algorithm's update/final; the 17 note common code reads the algorithm from cell 9, so sha256_update on a sha512 context hashes SHA-512. 18 note 19 note Context cells: 0..7 chaining value H (32-bit words zero-extended for SHA-256), 8 = bytes absorbed 20 note mod 2^64, 9 = algorithm (256, 384, 512), 16..31 = pending block bytes packed big-endian (byte i 21 note in cell 16 + i/8, bits 56 - 8*(i mod 8)), 10..15 and 32..39 = 0. 22 note Constant time: every branch, loop count and index depends only on lengths and the algorithm, 23 note never on message or state bytes. Local schedule/state/padding arrays are zeroed before return 24 note (HMAC keys pass through them). omc emits plain AArch64; MUL/ADD/EOR/ROR/shift data-independent. 25 note Message length: any length below 2^64 bytes; update cuts its input into runs of 2^25 bytes so no 26 note loop passes 2^20 times (a rolled for traps 4 after 10^6 passes; the while bound 999999 of runs 27 note allows inputs up to 999999 * 32 MiB per update call). 28 29 note K{256}[0..64): first 32 bits of the fractional parts of the cube roots of the first 64 primes 30 note (FIPS 180-4 4.2.2). Same values as om/sha256/sha256-om2.om; recomputed with Wolfram: 31 note IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^32],16,8] for i = 1..64. 32 fn sha2_k256 k edit uint 64 to void 33 put k 0 0x428a2f98 34 put k 1 0x71374491 35 put k 2 0xb5c0fbcf 36 put k 3 0xe9b5dba5 37 put k 4 0x3956c25b 38 put k 5 0x59f111f1 39 put k 6 0x923f82a4 40 put k 7 0xab1c5ed5 41 put k 8 0xd807aa98 42 put k 9 0x12835b01 43 put k 10 0x243185be 44 put k 11 0x550c7dc3 45 put k 12 0x72be5d74 46 put k 13 0x80deb1fe 47 put k 14 0x9bdc06a7 48 put k 15 0xc19bf174 49 put k 16 0xe49b69c1 50 put k 17 0xefbe4786 51 put k 18 0x0fc19dc6 52 put k 19 0x240ca1cc 53 put k 20 0x2de92c6f 54 put k 21 0x4a7484aa 55 put k 22 0x5cb0a9dc 56 put k 23 0x76f988da 57 put k 24 0x983e5152 58 put k 25 0xa831c66d 59 put k 26 0xb00327c8 60 put k 27 0xbf597fc7 61 put k 28 0xc6e00bf3 62 put k 29 0xd5a79147 63 put k 30 0x06ca6351 64 put k 31 0x14292967 65 put k 32 0x27b70a85 66 put k 33 0x2e1b2138 67 put k 34 0x4d2c6dfc 68 put k 35 0x53380d13 69 put k 36 0x650a7354 70 put k 37 0x766a0abb 71 put k 38 0x81c2c92e 72 put k 39 0x92722c85 73 put k 40 0xa2bfe8a1 74 put k 41 0xa81a664b 75 put k 42 0xc24b8b70 76 put k 43 0xc76c51a3 77 put k 44 0xd192e819 78 put k 45 0xd6990624 79 put k 46 0xf40e3585 80 put k 47 0x106aa070 81 put k 48 0x19a4c116 82 put k 49 0x1e376c08 83 put k 50 0x2748774c 84 put k 51 0x34b0bcb5 85 put k 52 0x391c0cb3 86 put k 53 0x4ed8aa4a 87 put k 54 0x5b9cca4f 88 put k 55 0x682e6ff3 89 put k 56 0x748f82ee 90 put k 57 0x78a5636f 91 put k 58 0x84c87814 92 put k 59 0x8cc70208 93 put k 60 0x90befffa 94 put k 61 0xa4506ceb 95 put k 62 0xbef9a3f7 96 put k 63 0xc67178f2 97 end 98 99 note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes 100 note (FIPS 180-4 4.2.3). Wolfram: IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^64],16,16]; 101 note equal to the decimal table of omnet om/sha512/sha512.om. 102 fn sha2_k512 k edit ulong 80 to void 103 put k 0 0x428a2f98d728ae22 104 put k 1 0x7137449123ef65cd 105 put k 2 0xb5c0fbcfec4d3b2f 106 put k 3 0xe9b5dba58189dbbc 107 put k 4 0x3956c25bf348b538 108 put k 5 0x59f111f1b605d019 109 put k 6 0x923f82a4af194f9b 110 put k 7 0xab1c5ed5da6d8118 111 put k 8 0xd807aa98a3030242 112 put k 9 0x12835b0145706fbe 113 put k 10 0x243185be4ee4b28c 114 put k 11 0x550c7dc3d5ffb4e2 115 put k 12 0x72be5d74f27b896f 116 put k 13 0x80deb1fe3b1696b1 117 put k 14 0x9bdc06a725c71235 118 put k 15 0xc19bf174cf692694 119 put k 16 0xe49b69c19ef14ad2 120 put k 17 0xefbe4786384f25e3 121 put k 18 0x0fc19dc68b8cd5b5 122 put k 19 0x240ca1cc77ac9c65 123 put k 20 0x2de92c6f592b0275 124 put k 21 0x4a7484aa6ea6e483 125 put k 22 0x5cb0a9dcbd41fbd4 126 put k 23 0x76f988da831153b5 127 put k 24 0x983e5152ee66dfab 128 put k 25 0xa831c66d2db43210 129 put k 26 0xb00327c898fb213f 130 put k 27 0xbf597fc7beef0ee4 131 put k 28 0xc6e00bf33da88fc2 132 put k 29 0xd5a79147930aa725 133 put k 30 0x06ca6351e003826f 134 put k 31 0x142929670a0e6e70 135 put k 32 0x27b70a8546d22ffc 136 put k 33 0x2e1b21385c26c926 137 put k 34 0x4d2c6dfc5ac42aed 138 put k 35 0x53380d139d95b3df 139 put k 36 0x650a73548baf63de 140 put k 37 0x766a0abb3c77b2a8 141 put k 38 0x81c2c92e47edaee6 142 put k 39 0x92722c851482353b 143 put k 40 0xa2bfe8a14cf10364 144 put k 41 0xa81a664bbc423001 145 put k 42 0xc24b8b70d0f89791 146 put k 43 0xc76c51a30654be30 147 put k 44 0xd192e819d6ef5218 148 put k 45 0xd69906245565a910 149 put k 46 0xf40e35855771202a 150 put k 47 0x106aa07032bbd1b8 151 put k 48 0x19a4c116b8d2d0c8 152 put k 49 0x1e376c085141ab53 153 put k 50 0x2748774cdf8eeb99 154 put k 51 0x34b0bcb5e19b48a8 155 put k 52 0x391c0cb3c5c95a63 156 put k 53 0x4ed8aa4ae3418acb 157 put k 54 0x5b9cca4f7763e373 158 put k 55 0x682e6ff3d6b2b8a3 159 put k 56 0x748f82ee5defb2fc 160 put k 57 0x78a5636f43172f60 161 put k 58 0x84c87814a1f0ab72 162 put k 59 0x8cc702081a6439ec 163 put k 60 0x90befffa23631e28 164 put k 61 0xa4506cebde82bde9 165 put k 62 0xbef9a3f7b2c67915 166 put k 63 0xc67178f2e372532b 167 put k 64 0xca273eceea26619c 168 put k 65 0xd186b8c721c0c207 169 put k 66 0xeada7dd6cde0eb1e 170 put k 67 0xf57d4f7fee6ed178 171 put k 68 0x06f067aa72176fba 172 put k 69 0x0a637dc5a2c898a6 173 put k 70 0x113f9804bef90dae 174 put k 71 0x1b710b35131c471b 175 put k 72 0x28db77f523047d84 176 put k 73 0x32caab7b40c72493 177 put k 74 0x3c9ebe0a15c9bebc 178 put k 75 0x431d67c49c100d4c 179 put k 76 0x4cc5d4becb3e42b6 180 put k 77 0x597f299cfc657e2a 181 put k 78 0x5fcb6fab3ad6faec 182 put k 79 0x6c44198c4a475817 183 end 184 185 note H(0) (FIPS 180-4 5.3.3 SHA-256, 5.3.4 SHA-384, 5.3.5 SHA-512). Wolfram: fractional parts of 186 note sqrt(Prime[i]) * 2^32 (i 1..8), * 2^64 (i 1..8 for SHA-512, i 9..16 for SHA-384). 187 note Any other alg traps 9 (caller contract). 188 fn sha2_init c edit ulong * alg ulong to void 189 for i ulong 40 190 put c i 0 191 end 192 put c 9 alg 193 if eq alg 256 194 put c 0 0x6a09e667 195 put c 1 0xbb67ae85 196 put c 2 0x3c6ef372 197 put c 3 0xa54ff53a 198 put c 4 0x510e527f 199 put c 5 0x9b05688c 200 put c 6 0x1f83d9ab 201 put c 7 0x5be0cd19 202 ret 203 end 204 if eq alg 384 205 put c 0 0xcbbb9d5dc1059ed8 206 put c 1 0x629a292a367cd507 207 put c 2 0x9159015a3070dd17 208 put c 3 0x152fecd8f70e5939 209 put c 4 0x67332667ffc00b31 210 put c 5 0x8eb44a8768581511 211 put c 6 0xdb0c2e0d64f98fa7 212 put c 7 0x47b5481dbefa4fa4 213 ret 214 end 215 if eq alg 512 216 put c 0 0x6a09e667f3bcc908 217 put c 1 0xbb67ae8584caa73b 218 put c 2 0x3c6ef372fe94f82b 219 put c 3 0xa54ff53a5f1d36f1 220 put c 4 0x510e527fade682d1 221 put c 5 0x9b05688c2b3e6c1f 222 put c 6 0x1f83d9abfb41bd6b 223 put c 7 0x5be0cd19137e2179 224 ret 225 end 226 trap 9 227 end 228 229 note SHA-256 message schedule (FIPS 180-4 6.2.2 step 1) of the block m[o .. o+64). 230 fn sha2_s256 w edit uint 64 m view uchar * o ulong to void 231 q = ulong 0 232 x = uint 0 233 y = uint 0 234 for i ulong 16 unroll 235 q = add o mul i 4 236 x = uint get m q 237 x = or shl x 8 uint get m add q 1 238 x = or shl x 8 uint get m add q 2 239 put w i or shl x 8 uint get m add q 3 240 end 241 for i ulong 48 unroll 242 x = get w add i 1 243 y = get w add i 14 244 x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 245 put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 246 end 247 end 248 249 note SHA-256 rounds (6.2.2 steps 2-4). Ch(e,f,g) = g xor (e and (f xor g)); Maj(a,b,c) = 250 note (a and b) or (c and (a or b)); Sigma1 = ROTR 6,11,25; Sigma0 = ROTR 2,13,22 (4.1.2). 251 fn sha2_r256 s edit uint 8 w view uint 64 kt view uint 64 to void 252 a = get s 0 253 b = get s 1 254 c = get s 2 255 d = get s 3 256 e = get s 4 257 f = get s 5 258 g = get s 6 259 h = get s 7 260 t = uint 0 261 for i ulong 64 unroll 262 h = add h get kt i 263 h = add h get w i 264 t = xor rotr e 6 rotr e 11 265 t = xor t rotr e 25 266 h = add h t 267 h = add h xor g and e xor f g 268 d = add d h 269 t = xor rotr a 2 rotr a 13 270 t = xor t rotr a 22 271 h = add h t 272 t = and c or a b 273 h = add h or and a b t 274 t = h 275 h = g 276 g = f 277 f = e 278 e = d 279 d = c 280 c = b 281 b = a 282 a = t 283 end 284 put s 0 add get s 0 a 285 put s 1 add get s 1 b 286 put s 2 add get s 2 c 287 put s 3 add get s 3 d 288 put s 4 add get s 4 e 289 put s 5 add get s 5 f 290 put s 6 add get s 6 g 291 put s 7 add get s 7 h 292 end 293 294 note the whole 64-byte blocks of m (len m a multiple of 64, at most 2^25) into c's chaining value 295 fn sha2_b256 c edit ulong * m view uchar * to void 296 s = new uint 8 0 297 w = new uint 64 0 298 kt = new uint 64 0 299 sha2_k256 kt 300 for i ulong 8 unroll 301 put s i uint get c i 302 end 303 for j ulong shr len m 6 304 sha2_s256 w m shl j 6 305 sha2_r256 s w kt 306 end 307 for i ulong 8 unroll 308 put c i ulong get s i 309 put s i 0 310 end 311 for i ulong 64 312 put w i 0 313 end 314 end 315 316 note SHA-512 message schedule (6.4.2 step 1) of the block m[o .. o+128); sigma0 = ROTR 1,8 SHR 7, 317 note sigma1 = ROTR 19,61 SHR 6 (4.1.3). 318 fn sha2_s512 w edit ulong 80 m view uchar * o ulong to void 319 q = ulong 0 320 x = ulong 0 321 y = ulong 0 322 for i ulong 16 unroll 323 q = add o mul i 8 324 x = ulong get m q 325 x = or shl x 8 ulong get m add q 1 326 x = or shl x 8 ulong get m add q 2 327 x = or shl x 8 ulong get m add q 3 328 x = or shl x 8 ulong get m add q 4 329 x = or shl x 8 ulong get m add q 5 330 x = or shl x 8 ulong get m add q 6 331 put w i or shl x 8 ulong get m add q 7 332 end 333 for i ulong 64 unroll 334 x = get w add i 1 335 y = get w add i 14 336 x = add add get w i get w add i 9 xor xor rotr x 1 rotr x 8 shr x 7 337 put w add i 16 add x xor xor rotr y 19 rotr y 61 shr y 6 338 end 339 end 340 341 note SHA-512 rounds (6.4.2 steps 2-4), a rolled loop (unroll allows at most 64 copies); Sigma1 = ROTR 14,18,41; Sigma0 = ROTR 28,34,39 (4.1.3). 342 fn sha2_r512 s edit ulong 8 w view ulong 80 kt view ulong 80 to void 343 a = get s 0 344 b = get s 1 345 c = get s 2 346 d = get s 3 347 e = get s 4 348 f = get s 5 349 g = get s 6 350 h = get s 7 351 t = ulong 0 352 for i ulong 80 353 h = add h get kt i 354 h = add h get w i 355 t = xor rotr e 14 rotr e 18 356 t = xor t rotr e 41 357 h = add h t 358 h = add h xor g and e xor f g 359 d = add d h 360 t = xor rotr a 28 rotr a 34 361 t = xor t rotr a 39 362 h = add h t 363 t = and c or a b 364 h = add h or and a b t 365 t = h 366 h = g 367 g = f 368 f = e 369 e = d 370 d = c 371 c = b 372 b = a 373 a = t 374 end 375 put s 0 add get s 0 a 376 put s 1 add get s 1 b 377 put s 2 add get s 2 c 378 put s 3 add get s 3 d 379 put s 4 add get s 4 e 380 put s 5 add get s 5 f 381 put s 6 add get s 6 g 382 put s 7 add get s 7 h 383 end 384 385 note the whole 128-byte blocks of m (len m a multiple of 128, at most 2^25) into c 386 fn sha2_b512 c edit ulong * m view uchar * to void 387 s = new ulong 8 0 388 w = new ulong 80 0 389 kt = new ulong 80 0 390 sha2_k512 kt 391 for i ulong 8 unroll 392 put s i get c i 393 end 394 for j ulong shr len m 7 395 sha2_s512 w m shl j 7 396 sha2_r512 s w kt 397 end 398 for i ulong 8 unroll 399 put c i get s i 400 put s i 0 401 end 402 for i ulong 80 403 put w i 0 404 end 405 end 406 407 note whole blocks of m (len m a multiple of the block size) in runs of at most 2^25 bytes 408 fn sha2_run c edit ulong * m view uchar * to void 409 n = len m 410 q = ulong 0 411 k = ulong 0 412 while lt q n bound 999999 413 k = sub n q 414 if gt k 0x2000000 415 k = 0x2000000 416 end 417 if eq get c 9 256 418 sha2_b256 c part m q k 419 else 420 sha2_b512 c part m q k 421 end 422 q = add q k 423 end 424 end 425 426 note pending byte i (i < 128) of the packed buffer 427 fn sha2_bget c view ulong * i ulong to uchar 428 x = shr get c add 16 shr i 3 sub 56 shl and i 7 3 429 ret uchar and x 0xff 430 end 431 432 note set pending byte i of the packed buffer to v 433 fn sha2_bput c edit ulong * i ulong v uchar to void 434 q = add 16 shr i 3 435 sh = sub 56 shl and i 7 3 436 x = and get c q not shl ulong 0xff sh 437 put c q or x shl ulong v sh 438 end 439 440 note the bytes m[0..k) into the buffer at bi (bi + k <= block size) 441 fn sha2_fill c edit ulong * bi ulong m view uchar * k ulong to void 442 for i ulong k 443 sha2_bput c add bi i get m i 444 end 445 end 446 447 fn sha2_update c edit ulong * m view uchar * to void 448 blk = new uchar 128 0 449 n = len m 450 cnt = get c 8 451 bs = ulong 128 452 if eq get c 9 256 453 bs = 64 454 end 455 bi = and cnt sub bs 1 456 put c 8 add cnt n 457 off = ulong 0 458 if gt bi 0 459 off = sub bs bi 460 if lt n off 461 off = n 462 end 463 sha2_fill c bi m off 464 if eq add bi off bs 465 for i ulong bs 466 put blk i sha2_bget c i 467 end 468 sha2_run c part blk 0 bs 469 for i ulong bs 470 put blk i 0 471 end 472 end 473 end 474 nb = and sub n off not sub bs 1 475 sha2_run c part m off nb 476 off = add off nb 477 sha2_fill c 0 part m off sub n off sub n off 478 end 479 480 note H(N) as big-endian bytes: 32 (SHA-256, 8 words of 32 bits), 48 (SHA-384, first 6 words), 64 481 fn sha2_put c view ulong * o edit uchar * to ulong 482 alg = get c 9 483 sh = ulong 0 484 if eq alg 256 485 for i ulong 32 486 sh = sub 24 shl and i 3 3 487 put o i uchar and shr get c shr i 2 sh 0xff 488 end 489 ret 32 490 end 491 dl = ulong 64 492 if eq alg 384 493 dl = 48 494 end 495 for i ulong dl 496 sh = sub 56 shl and i 7 3 497 put o i uchar and shr get c shr i 3 sh 0xff 498 end 499 ret dl 500 end 501 502 note padding (FIPS 180-4 5.1.1 / 5.1.2): 0x80, zeros, the bit length big-endian in the last 8 (SHA-256) 503 note or 16 (SHA-384/512) bytes of one or two blocks; returns the digest length 504 fn sha2_fin c edit ulong * o edit uchar * to ulong 505 p = new uchar 256 0 506 bs = ulong 128 507 ls = ulong 16 508 if eq get c 9 256 509 bs = 64 510 ls = 8 511 end 512 cnt = get c 8 513 k = and cnt sub bs 1 514 for i ulong k 515 put p i sha2_bget c i 516 end 517 put p k 0x80 518 e = bs 519 if ge add k ls bs 520 e = shl bs 1 521 end 522 x = shl cnt 3 523 for i ulong 8 unroll 524 put p sub e add i 1 uchar and shr x mul i 8 0xff 525 end 526 if eq ls 16 527 put p sub e 9 uchar shr cnt 61 528 end 529 sha2_run c part p 0 e 530 for i ulong 256 531 put p i 0 532 end 533 ret sha2_put c o 534 end 535 536 fn sha256_init c edit ulong * to void 537 sha2_init c 256 538 end 539 540 fn sha384_init c edit ulong * to void 541 sha2_init c 384 542 end 543 544 fn sha512_init c edit ulong * to void 545 sha2_init c 512 546 end 547 548 fn sha256_update c edit ulong * m view uchar * to void 549 sha2_update c m 550 end 551 552 fn sha384_update c edit ulong * m view uchar * to void 553 sha2_update c m 554 end 555 556 fn sha512_update c edit ulong * m view uchar * to void 557 sha2_update c m 558 end 559 560 fn sha256_final c edit ulong * o edit uchar * to void 561 dl = sha2_fin c o 562 end 563 564 fn sha384_final c edit ulong * o edit uchar * to void 565 dl = sha2_fin c o 566 end 567 568 fn sha512_final c edit ulong * o edit uchar * to void 569 dl = sha2_fin c o 570 end 571 572 fn sha2_copy d edit ulong * s view ulong * to void 573 for i ulong len s 574 put d i get s i 575 end 576 end 577 578 fn sha2_digest alg ulong m view uchar * o edit uchar * to ulong 579 c = new ulong 40 0 580 if ne alg 256 581 if ne alg 384 582 if ne alg 512 583 ret 0 584 end 585 end 586 end 587 sha2_init c alg 588 sha2_update c m 589 dl = sha2_fin c o 590 for i ulong 40 591 put c i 0 592 end 593 ret dl 594 end 595 note bench: SHA-256 of 64 MiB (a 1 MiB pattern absorbed 64 times) through om/tls/sha2.om 596 fn main to int 597 b = new uchar `1048576` 0 598 for i ulong `1048576` 599 put b i uchar and add mul i 31 7 255 600 end 601 c = new ulong 40 0 602 o = new uchar 32 0 603 sha256_init c 604 for r ulong 1 605 sha256_update c b 606 end 607 sha256_final c o 608 w = write 1 o 0 32 609 ret 0 610 end