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