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
omega-mv / seed / qualification / om2 / baseline / sha256-om1.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note sha256.om: FIPS 180-4 SHA-256 as one function-mode unit, bound through the omega-bridge native |
| 3 | note call ABI (docs/NATIVE-ABI.md) and exported as omega_omsha. It replaces the algorithm of |
| 4 | note src/sha256.c: the C file keeps only the sha256_* entry points that cut caller data into |
| 5 | note pieces of at most 4096 bytes. |
| 6 | note |
| 7 | note The whole hash state lives in the caller's sha256_ctx (104 bytes, src/sha256.h): |
| 8 | note bytes 0..31 state[8], uint32 little endian |
| 9 | note bytes 32..39 count, uint64 little endian (bytes absorbed so far) |
| 10 | note bytes 40..103 buffer, the pending partial block |
| 11 | note omsha c b n op w b8: |
| 12 | note op 0 init: count 0, FIPS initial hash value |
| 13 | note op 1 absorb b[0..n), n <= 4096, with src/sha256.c's buffering rules |
| 14 | note op 2 final: pad, absorb the bit length, the 32-byte digest packed into w[0..4) |
| 15 | note op 3 one shot: the digest of the n < 4096 bytes of b8 packed into w[0..4); c is not used |
| 16 | note w is the caller's scratch for the message schedule (64 words, contents ignored and overwritten): |
| 17 | note Omega writes every word before reading it, so the caller need not clear it. |
| 18 | note n > 4096 traps 3; any other op traps 9. |
| 19 | |
| 20 | note little-endian word of nb bytes at byte offset p of the context |
| 21 | fn getle c edit uchar 104 p ulong nb ulong to ulong |
| 22 | v = ulong 0 |
| 23 | i = nb |
| 24 | while gt i 0 bound 8 |
| 25 | i = sub i 1 |
| 26 | v = or shl v 8 ulong get c add p i |
| 27 | end |
| 28 | ret v |
| 29 | end |
| 30 | |
| 31 | fn putle c edit uchar 104 p ulong nb ulong v ulong to void |
| 32 | x = v |
| 33 | for i ulong nb |
| 34 | put c add p i uchar and x 255 |
| 35 | x = shr x 8 |
| 36 | end |
| 37 | end |
| 38 | |
| 39 | note the hash words of the context into h, and back |
| 40 | fn loadh c edit uchar 104 h edit uint 8 to void |
| 41 | for i ulong 8 |
| 42 | put h i uint getle c mul i 4 4 |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | fn storeh c edit uchar 104 h edit uint 8 to void |
| 47 | for i ulong 8 |
| 48 | putle c mul i 4 4 ulong get h i |
| 49 | end |
| 50 | end |
| 51 | |
| 52 | note big-endian words of the block at byte o of b into w[0..16), then the schedule |
| 53 | fn schedb b view uchar 4096 o ulong w edit ulong 64 to void |
| 54 | for i ulong 16 |
| 55 | q = add o mul i 4 |
| 56 | v = ulong get b q |
| 57 | v = or shl v 8 ulong get b add q 1 |
| 58 | v = or shl v 8 ulong get b add q 2 |
| 59 | v = or shl v 8 ulong get b add q 3 |
| 60 | put w i or v shl v 32 |
| 61 | end |
| 62 | expand w |
| 63 | end |
| 64 | |
| 65 | note big-endian words of the block at byte 40 of b into w[0..16), then the schedule |
| 66 | fn schedc b edit uchar 104 w edit ulong 64 to void |
| 67 | for i ulong 16 |
| 68 | q = add 40 mul i 4 |
| 69 | v = ulong get b q |
| 70 | v = or shl v 8 ulong get b add q 1 |
| 71 | v = or shl v 8 ulong get b add q 2 |
| 72 | v = or shl v 8 ulong get b add q 3 |
| 73 | put w i or v shl v 32 |
| 74 | end |
| 75 | expand w |
| 76 | end |
| 77 | |
| 78 | note the 64 rounds, unrolled with the working words in locals (names rotate instead of moving). |
| 79 | note Same lazy low-32-bit cells and rotate trick as expand; K is an immediate; the eight words are |
| 80 | note reduced to 32 bits only when they are added back into h. |
| 81 | fn rounds h edit uint 8 w view ulong 64 to void |
| 82 | a = ulong get h 0 |
| 83 | b = ulong get h 1 |
| 84 | c = ulong get h 2 |
| 85 | d = ulong get h 3 |
| 86 | e = ulong get h 4 |
| 87 | f = ulong get h 5 |
| 88 | g = ulong get h 6 |
| 89 | x = ulong get h 7 |
| 90 | t = ulong 0 |
| 91 | u = ulong 0 |
| 92 | t = add x add get w 0 1116352408 |
| 93 | u = shl e 32 |
| 94 | u = or u shr u 32 |
| 95 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 96 | t = add t u |
| 97 | t = add t xor g and e xor f g |
| 98 | u = shl a 32 |
| 99 | u = or u shr u 32 |
| 100 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 101 | u = add u xor b and xor a b xor b c |
| 102 | d = add d t |
| 103 | x = add t u |
| 104 | t = add g add get w 1 1899447441 |
| 105 | u = shl d 32 |
| 106 | u = or u shr u 32 |
| 107 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 108 | t = add t u |
| 109 | t = add t xor f and d xor e f |
| 110 | u = shl x 32 |
| 111 | u = or u shr u 32 |
| 112 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 113 | u = add u xor a and xor x a xor a b |
| 114 | c = add c t |
| 115 | g = add t u |
| 116 | t = add f add get w 2 3049323471 |
| 117 | u = shl c 32 |
| 118 | u = or u shr u 32 |
| 119 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 120 | t = add t u |
| 121 | t = add t xor e and c xor d e |
| 122 | u = shl g 32 |
| 123 | u = or u shr u 32 |
| 124 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 125 | u = add u xor x and xor g x xor x a |
| 126 | b = add b t |
| 127 | f = add t u |
| 128 | t = add e add get w 3 3921009573 |
| 129 | u = shl b 32 |
| 130 | u = or u shr u 32 |
| 131 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 132 | t = add t u |
| 133 | t = add t xor d and b xor c d |
| 134 | u = shl f 32 |
| 135 | u = or u shr u 32 |
| 136 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 137 | u = add u xor g and xor f g xor g x |
| 138 | a = add a t |
| 139 | e = add t u |
| 140 | t = add d add get w 4 961987163 |
| 141 | u = shl a 32 |
| 142 | u = or u shr u 32 |
| 143 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 144 | t = add t u |
| 145 | t = add t xor c and a xor b c |
| 146 | u = shl e 32 |
| 147 | u = or u shr u 32 |
| 148 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 149 | u = add u xor f and xor e f xor f g |
| 150 | x = add x t |
| 151 | d = add t u |
| 152 | t = add c add get w 5 1508970993 |
| 153 | u = shl x 32 |
| 154 | u = or u shr u 32 |
| 155 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 156 | t = add t u |
| 157 | t = add t xor b and x xor a b |
| 158 | u = shl d 32 |
| 159 | u = or u shr u 32 |
| 160 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 161 | u = add u xor e and xor d e xor e f |
| 162 | g = add g t |
| 163 | c = add t u |
| 164 | t = add b add get w 6 2453635748 |
| 165 | u = shl g 32 |
| 166 | u = or u shr u 32 |
| 167 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 168 | t = add t u |
| 169 | t = add t xor a and g xor x a |
| 170 | u = shl c 32 |
| 171 | u = or u shr u 32 |
| 172 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 173 | u = add u xor d and xor c d xor d e |
| 174 | f = add f t |
| 175 | b = add t u |
| 176 | t = add a add get w 7 2870763221 |
| 177 | u = shl f 32 |
| 178 | u = or u shr u 32 |
| 179 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 180 | t = add t u |
| 181 | t = add t xor x and f xor g x |
| 182 | u = shl b 32 |
| 183 | u = or u shr u 32 |
| 184 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 185 | u = add u xor c and xor b c xor c d |
| 186 | e = add e t |
| 187 | a = add t u |
| 188 | t = add x add get w 8 3624381080 |
| 189 | u = shl e 32 |
| 190 | u = or u shr u 32 |
| 191 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 192 | t = add t u |
| 193 | t = add t xor g and e xor f g |
| 194 | u = shl a 32 |
| 195 | u = or u shr u 32 |
| 196 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 197 | u = add u xor b and xor a b xor b c |
| 198 | d = add d t |
| 199 | x = add t u |
| 200 | t = add g add get w 9 310598401 |
| 201 | u = shl d 32 |
| 202 | u = or u shr u 32 |
| 203 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 204 | t = add t u |
| 205 | t = add t xor f and d xor e f |
| 206 | u = shl x 32 |
| 207 | u = or u shr u 32 |
| 208 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 209 | u = add u xor a and xor x a xor a b |
| 210 | c = add c t |
| 211 | g = add t u |
| 212 | t = add f add get w 10 607225278 |
| 213 | u = shl c 32 |
| 214 | u = or u shr u 32 |
| 215 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 216 | t = add t u |
| 217 | t = add t xor e and c xor d e |
| 218 | u = shl g 32 |
| 219 | u = or u shr u 32 |
| 220 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 221 | u = add u xor x and xor g x xor x a |
| 222 | b = add b t |
| 223 | f = add t u |
| 224 | t = add e add get w 11 1426881987 |
| 225 | u = shl b 32 |
| 226 | u = or u shr u 32 |
| 227 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 228 | t = add t u |
| 229 | t = add t xor d and b xor c d |
| 230 | u = shl f 32 |
| 231 | u = or u shr u 32 |
| 232 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 233 | u = add u xor g and xor f g xor g x |
| 234 | a = add a t |
| 235 | e = add t u |
| 236 | t = add d add get w 12 1925078388 |
| 237 | u = shl a 32 |
| 238 | u = or u shr u 32 |
| 239 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 240 | t = add t u |
| 241 | t = add t xor c and a xor b c |
| 242 | u = shl e 32 |
| 243 | u = or u shr u 32 |
| 244 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 245 | u = add u xor f and xor e f xor f g |
| 246 | x = add x t |
| 247 | d = add t u |
| 248 | t = add c add get w 13 2162078206 |
| 249 | u = shl x 32 |
| 250 | u = or u shr u 32 |
| 251 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 252 | t = add t u |
| 253 | t = add t xor b and x xor a b |
| 254 | u = shl d 32 |
| 255 | u = or u shr u 32 |
| 256 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 257 | u = add u xor e and xor d e xor e f |
| 258 | g = add g t |
| 259 | c = add t u |
| 260 | t = add b add get w 14 2614888103 |
| 261 | u = shl g 32 |
| 262 | u = or u shr u 32 |
| 263 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 264 | t = add t u |
| 265 | t = add t xor a and g xor x a |
| 266 | u = shl c 32 |
| 267 | u = or u shr u 32 |
| 268 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 269 | u = add u xor d and xor c d xor d e |
| 270 | f = add f t |
| 271 | b = add t u |
| 272 | t = add a add get w 15 3248222580 |
| 273 | u = shl f 32 |
| 274 | u = or u shr u 32 |
| 275 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 276 | t = add t u |
| 277 | t = add t xor x and f xor g x |
| 278 | u = shl b 32 |
| 279 | u = or u shr u 32 |
| 280 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 281 | u = add u xor c and xor b c xor c d |
| 282 | e = add e t |
| 283 | a = add t u |
| 284 | t = add x add get w 16 3835390401 |
| 285 | u = shl e 32 |
| 286 | u = or u shr u 32 |
| 287 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 288 | t = add t u |
| 289 | t = add t xor g and e xor f g |
| 290 | u = shl a 32 |
| 291 | u = or u shr u 32 |
| 292 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 293 | u = add u xor b and xor a b xor b c |
| 294 | d = add d t |
| 295 | x = add t u |
| 296 | t = add g add get w 17 4022224774 |
| 297 | u = shl d 32 |
| 298 | u = or u shr u 32 |
| 299 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 300 | t = add t u |
| 301 | t = add t xor f and d xor e f |
| 302 | u = shl x 32 |
| 303 | u = or u shr u 32 |
| 304 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 305 | u = add u xor a and xor x a xor a b |
| 306 | c = add c t |
| 307 | g = add t u |
| 308 | t = add f add get w 18 264347078 |
| 309 | u = shl c 32 |
| 310 | u = or u shr u 32 |
| 311 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 312 | t = add t u |
| 313 | t = add t xor e and c xor d e |
| 314 | u = shl g 32 |
| 315 | u = or u shr u 32 |
| 316 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 317 | u = add u xor x and xor g x xor x a |
| 318 | b = add b t |
| 319 | f = add t u |
| 320 | t = add e add get w 19 604807628 |
| 321 | u = shl b 32 |
| 322 | u = or u shr u 32 |
| 323 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 324 | t = add t u |
| 325 | t = add t xor d and b xor c d |
| 326 | u = shl f 32 |
| 327 | u = or u shr u 32 |
| 328 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 329 | u = add u xor g and xor f g xor g x |
| 330 | a = add a t |
| 331 | e = add t u |
| 332 | t = add d add get w 20 770255983 |
| 333 | u = shl a 32 |
| 334 | u = or u shr u 32 |
| 335 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 336 | t = add t u |
| 337 | t = add t xor c and a xor b c |
| 338 | u = shl e 32 |
| 339 | u = or u shr u 32 |
| 340 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 341 | u = add u xor f and xor e f xor f g |
| 342 | x = add x t |
| 343 | d = add t u |
| 344 | t = add c add get w 21 1249150122 |
| 345 | u = shl x 32 |
| 346 | u = or u shr u 32 |
| 347 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 348 | t = add t u |
| 349 | t = add t xor b and x xor a b |
| 350 | u = shl d 32 |
| 351 | u = or u shr u 32 |
| 352 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 353 | u = add u xor e and xor d e xor e f |
| 354 | g = add g t |
| 355 | c = add t u |
| 356 | t = add b add get w 22 1555081692 |
| 357 | u = shl g 32 |
| 358 | u = or u shr u 32 |
| 359 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 360 | t = add t u |
| 361 | t = add t xor a and g xor x a |
| 362 | u = shl c 32 |
| 363 | u = or u shr u 32 |
| 364 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 365 | u = add u xor d and xor c d xor d e |
| 366 | f = add f t |
| 367 | b = add t u |
| 368 | t = add a add get w 23 1996064986 |
| 369 | u = shl f 32 |
| 370 | u = or u shr u 32 |
| 371 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 372 | t = add t u |
| 373 | t = add t xor x and f xor g x |
| 374 | u = shl b 32 |
| 375 | u = or u shr u 32 |
| 376 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 377 | u = add u xor c and xor b c xor c d |
| 378 | e = add e t |
| 379 | a = add t u |
| 380 | t = add x add get w 24 2554220882 |
| 381 | u = shl e 32 |
| 382 | u = or u shr u 32 |
| 383 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 384 | t = add t u |
| 385 | t = add t xor g and e xor f g |
| 386 | u = shl a 32 |
| 387 | u = or u shr u 32 |
| 388 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 389 | u = add u xor b and xor a b xor b c |
| 390 | d = add d t |
| 391 | x = add t u |
| 392 | t = add g add get w 25 2821834349 |
| 393 | u = shl d 32 |
| 394 | u = or u shr u 32 |
| 395 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 396 | t = add t u |
| 397 | t = add t xor f and d xor e f |
| 398 | u = shl x 32 |
| 399 | u = or u shr u 32 |
| 400 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 401 | u = add u xor a and xor x a xor a b |
| 402 | c = add c t |
| 403 | g = add t u |
| 404 | t = add f add get w 26 2952996808 |
| 405 | u = shl c 32 |
| 406 | u = or u shr u 32 |
| 407 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 408 | t = add t u |
| 409 | t = add t xor e and c xor d e |
| 410 | u = shl g 32 |
| 411 | u = or u shr u 32 |
| 412 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 413 | u = add u xor x and xor g x xor x a |
| 414 | b = add b t |
| 415 | f = add t u |
| 416 | t = add e add get w 27 3210313671 |
| 417 | u = shl b 32 |
| 418 | u = or u shr u 32 |
| 419 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 420 | t = add t u |
| 421 | t = add t xor d and b xor c d |
| 422 | u = shl f 32 |
| 423 | u = or u shr u 32 |
| 424 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 425 | u = add u xor g and xor f g xor g x |
| 426 | a = add a t |
| 427 | e = add t u |
| 428 | t = add d add get w 28 3336571891 |
| 429 | u = shl a 32 |
| 430 | u = or u shr u 32 |
| 431 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 432 | t = add t u |
| 433 | t = add t xor c and a xor b c |
| 434 | u = shl e 32 |
| 435 | u = or u shr u 32 |
| 436 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 437 | u = add u xor f and xor e f xor f g |
| 438 | x = add x t |
| 439 | d = add t u |
| 440 | t = add c add get w 29 3584528711 |
| 441 | u = shl x 32 |
| 442 | u = or u shr u 32 |
| 443 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 444 | t = add t u |
| 445 | t = add t xor b and x xor a b |
| 446 | u = shl d 32 |
| 447 | u = or u shr u 32 |
| 448 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 449 | u = add u xor e and xor d e xor e f |
| 450 | g = add g t |
| 451 | c = add t u |
| 452 | t = add b add get w 30 113926993 |
| 453 | u = shl g 32 |
| 454 | u = or u shr u 32 |
| 455 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 456 | t = add t u |
| 457 | t = add t xor a and g xor x a |
| 458 | u = shl c 32 |
| 459 | u = or u shr u 32 |
| 460 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 461 | u = add u xor d and xor c d xor d e |
| 462 | f = add f t |
| 463 | b = add t u |
| 464 | t = add a add get w 31 338241895 |
| 465 | u = shl f 32 |
| 466 | u = or u shr u 32 |
| 467 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 468 | t = add t u |
| 469 | t = add t xor x and f xor g x |
| 470 | u = shl b 32 |
| 471 | u = or u shr u 32 |
| 472 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 473 | u = add u xor c and xor b c xor c d |
| 474 | e = add e t |
| 475 | a = add t u |
| 476 | t = add x add get w 32 666307205 |
| 477 | u = shl e 32 |
| 478 | u = or u shr u 32 |
| 479 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 480 | t = add t u |
| 481 | t = add t xor g and e xor f g |
| 482 | u = shl a 32 |
| 483 | u = or u shr u 32 |
| 484 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 485 | u = add u xor b and xor a b xor b c |
| 486 | d = add d t |
| 487 | x = add t u |
| 488 | t = add g add get w 33 773529912 |
| 489 | u = shl d 32 |
| 490 | u = or u shr u 32 |
| 491 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 492 | t = add t u |
| 493 | t = add t xor f and d xor e f |
| 494 | u = shl x 32 |
| 495 | u = or u shr u 32 |
| 496 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 497 | u = add u xor a and xor x a xor a b |
| 498 | c = add c t |
| 499 | g = add t u |
| 500 | t = add f add get w 34 1294757372 |
| 501 | u = shl c 32 |
| 502 | u = or u shr u 32 |
| 503 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 504 | t = add t u |
| 505 | t = add t xor e and c xor d e |
| 506 | u = shl g 32 |
| 507 | u = or u shr u 32 |
| 508 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 509 | u = add u xor x and xor g x xor x a |
| 510 | b = add b t |
| 511 | f = add t u |
| 512 | t = add e add get w 35 1396182291 |
| 513 | u = shl b 32 |
| 514 | u = or u shr u 32 |
| 515 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 516 | t = add t u |
| 517 | t = add t xor d and b xor c d |
| 518 | u = shl f 32 |
| 519 | u = or u shr u 32 |
| 520 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 521 | u = add u xor g and xor f g xor g x |
| 522 | a = add a t |
| 523 | e = add t u |
| 524 | t = add d add get w 36 1695183700 |
| 525 | u = shl a 32 |
| 526 | u = or u shr u 32 |
| 527 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 528 | t = add t u |
| 529 | t = add t xor c and a xor b c |
| 530 | u = shl e 32 |
| 531 | u = or u shr u 32 |
| 532 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 533 | u = add u xor f and xor e f xor f g |
| 534 | x = add x t |
| 535 | d = add t u |
| 536 | t = add c add get w 37 1986661051 |
| 537 | u = shl x 32 |
| 538 | u = or u shr u 32 |
| 539 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 540 | t = add t u |
| 541 | t = add t xor b and x xor a b |
| 542 | u = shl d 32 |
| 543 | u = or u shr u 32 |
| 544 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 545 | u = add u xor e and xor d e xor e f |
| 546 | g = add g t |
| 547 | c = add t u |
| 548 | t = add b add get w 38 2177026350 |
| 549 | u = shl g 32 |
| 550 | u = or u shr u 32 |
| 551 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 552 | t = add t u |
| 553 | t = add t xor a and g xor x a |
| 554 | u = shl c 32 |
| 555 | u = or u shr u 32 |
| 556 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 557 | u = add u xor d and xor c d xor d e |
| 558 | f = add f t |
| 559 | b = add t u |
| 560 | t = add a add get w 39 2456956037 |
| 561 | u = shl f 32 |
| 562 | u = or u shr u 32 |
| 563 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 564 | t = add t u |
| 565 | t = add t xor x and f xor g x |
| 566 | u = shl b 32 |
| 567 | u = or u shr u 32 |
| 568 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 569 | u = add u xor c and xor b c xor c d |
| 570 | e = add e t |
| 571 | a = add t u |
| 572 | t = add x add get w 40 2730485921 |
| 573 | u = shl e 32 |
| 574 | u = or u shr u 32 |
| 575 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 576 | t = add t u |
| 577 | t = add t xor g and e xor f g |
| 578 | u = shl a 32 |
| 579 | u = or u shr u 32 |
| 580 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 581 | u = add u xor b and xor a b xor b c |
| 582 | d = add d t |
| 583 | x = add t u |
| 584 | t = add g add get w 41 2820302411 |
| 585 | u = shl d 32 |
| 586 | u = or u shr u 32 |
| 587 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 588 | t = add t u |
| 589 | t = add t xor f and d xor e f |
| 590 | u = shl x 32 |
| 591 | u = or u shr u 32 |
| 592 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 593 | u = add u xor a and xor x a xor a b |
| 594 | c = add c t |
| 595 | g = add t u |
| 596 | t = add f add get w 42 3259730800 |
| 597 | u = shl c 32 |
| 598 | u = or u shr u 32 |
| 599 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 600 | t = add t u |
| 601 | t = add t xor e and c xor d e |
| 602 | u = shl g 32 |
| 603 | u = or u shr u 32 |
| 604 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 605 | u = add u xor x and xor g x xor x a |
| 606 | b = add b t |
| 607 | f = add t u |
| 608 | t = add e add get w 43 3345764771 |
| 609 | u = shl b 32 |
| 610 | u = or u shr u 32 |
| 611 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 612 | t = add t u |
| 613 | t = add t xor d and b xor c d |
| 614 | u = shl f 32 |
| 615 | u = or u shr u 32 |
| 616 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 617 | u = add u xor g and xor f g xor g x |
| 618 | a = add a t |
| 619 | e = add t u |
| 620 | t = add d add get w 44 3516065817 |
| 621 | u = shl a 32 |
| 622 | u = or u shr u 32 |
| 623 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 624 | t = add t u |
| 625 | t = add t xor c and a xor b c |
| 626 | u = shl e 32 |
| 627 | u = or u shr u 32 |
| 628 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 629 | u = add u xor f and xor e f xor f g |
| 630 | x = add x t |
| 631 | d = add t u |
| 632 | t = add c add get w 45 3600352804 |
| 633 | u = shl x 32 |
| 634 | u = or u shr u 32 |
| 635 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 636 | t = add t u |
| 637 | t = add t xor b and x xor a b |
| 638 | u = shl d 32 |
| 639 | u = or u shr u 32 |
| 640 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 641 | u = add u xor e and xor d e xor e f |
| 642 | g = add g t |
| 643 | c = add t u |
| 644 | t = add b add get w 46 4094571909 |
| 645 | u = shl g 32 |
| 646 | u = or u shr u 32 |
| 647 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 648 | t = add t u |
| 649 | t = add t xor a and g xor x a |
| 650 | u = shl c 32 |
| 651 | u = or u shr u 32 |
| 652 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 653 | u = add u xor d and xor c d xor d e |
| 654 | f = add f t |
| 655 | b = add t u |
| 656 | t = add a add get w 47 275423344 |
| 657 | u = shl f 32 |
| 658 | u = or u shr u 32 |
| 659 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 660 | t = add t u |
| 661 | t = add t xor x and f xor g x |
| 662 | u = shl b 32 |
| 663 | u = or u shr u 32 |
| 664 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 665 | u = add u xor c and xor b c xor c d |
| 666 | e = add e t |
| 667 | a = add t u |
| 668 | t = add x add get w 48 430227734 |
| 669 | u = shl e 32 |
| 670 | u = or u shr u 32 |
| 671 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 672 | t = add t u |
| 673 | t = add t xor g and e xor f g |
| 674 | u = shl a 32 |
| 675 | u = or u shr u 32 |
| 676 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 677 | u = add u xor b and xor a b xor b c |
| 678 | d = add d t |
| 679 | x = add t u |
| 680 | t = add g add get w 49 506948616 |
| 681 | u = shl d 32 |
| 682 | u = or u shr u 32 |
| 683 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 684 | t = add t u |
| 685 | t = add t xor f and d xor e f |
| 686 | u = shl x 32 |
| 687 | u = or u shr u 32 |
| 688 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 689 | u = add u xor a and xor x a xor a b |
| 690 | c = add c t |
| 691 | g = add t u |
| 692 | t = add f add get w 50 659060556 |
| 693 | u = shl c 32 |
| 694 | u = or u shr u 32 |
| 695 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 696 | t = add t u |
| 697 | t = add t xor e and c xor d e |
| 698 | u = shl g 32 |
| 699 | u = or u shr u 32 |
| 700 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 701 | u = add u xor x and xor g x xor x a |
| 702 | b = add b t |
| 703 | f = add t u |
| 704 | t = add e add get w 51 883997877 |
| 705 | u = shl b 32 |
| 706 | u = or u shr u 32 |
| 707 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 708 | t = add t u |
| 709 | t = add t xor d and b xor c d |
| 710 | u = shl f 32 |
| 711 | u = or u shr u 32 |
| 712 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 713 | u = add u xor g and xor f g xor g x |
| 714 | a = add a t |
| 715 | e = add t u |
| 716 | t = add d add get w 52 958139571 |
| 717 | u = shl a 32 |
| 718 | u = or u shr u 32 |
| 719 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 720 | t = add t u |
| 721 | t = add t xor c and a xor b c |
| 722 | u = shl e 32 |
| 723 | u = or u shr u 32 |
| 724 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 725 | u = add u xor f and xor e f xor f g |
| 726 | x = add x t |
| 727 | d = add t u |
| 728 | t = add c add get w 53 1322822218 |
| 729 | u = shl x 32 |
| 730 | u = or u shr u 32 |
| 731 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 732 | t = add t u |
| 733 | t = add t xor b and x xor a b |
| 734 | u = shl d 32 |
| 735 | u = or u shr u 32 |
| 736 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 737 | u = add u xor e and xor d e xor e f |
| 738 | g = add g t |
| 739 | c = add t u |
| 740 | t = add b add get w 54 1537002063 |
| 741 | u = shl g 32 |
| 742 | u = or u shr u 32 |
| 743 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 744 | t = add t u |
| 745 | t = add t xor a and g xor x a |
| 746 | u = shl c 32 |
| 747 | u = or u shr u 32 |
| 748 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 749 | u = add u xor d and xor c d xor d e |
| 750 | f = add f t |
| 751 | b = add t u |
| 752 | t = add a add get w 55 1747873779 |
| 753 | u = shl f 32 |
| 754 | u = or u shr u 32 |
| 755 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 756 | t = add t u |
| 757 | t = add t xor x and f xor g x |
| 758 | u = shl b 32 |
| 759 | u = or u shr u 32 |
| 760 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 761 | u = add u xor c and xor b c xor c d |
| 762 | e = add e t |
| 763 | a = add t u |
| 764 | t = add x add get w 56 1955562222 |
| 765 | u = shl e 32 |
| 766 | u = or u shr u 32 |
| 767 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 768 | t = add t u |
| 769 | t = add t xor g and e xor f g |
| 770 | u = shl a 32 |
| 771 | u = or u shr u 32 |
| 772 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 773 | u = add u xor b and xor a b xor b c |
| 774 | d = add d t |
| 775 | x = add t u |
| 776 | t = add g add get w 57 2024104815 |
| 777 | u = shl d 32 |
| 778 | u = or u shr u 32 |
| 779 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 780 | t = add t u |
| 781 | t = add t xor f and d xor e f |
| 782 | u = shl x 32 |
| 783 | u = or u shr u 32 |
| 784 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 785 | u = add u xor a and xor x a xor a b |
| 786 | c = add c t |
| 787 | g = add t u |
| 788 | t = add f add get w 58 2227730452 |
| 789 | u = shl c 32 |
| 790 | u = or u shr u 32 |
| 791 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 792 | t = add t u |
| 793 | t = add t xor e and c xor d e |
| 794 | u = shl g 32 |
| 795 | u = or u shr u 32 |
| 796 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 797 | u = add u xor x and xor g x xor x a |
| 798 | b = add b t |
| 799 | f = add t u |
| 800 | t = add e add get w 59 2361852424 |
| 801 | u = shl b 32 |
| 802 | u = or u shr u 32 |
| 803 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 804 | t = add t u |
| 805 | t = add t xor d and b xor c d |
| 806 | u = shl f 32 |
| 807 | u = or u shr u 32 |
| 808 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 809 | u = add u xor g and xor f g xor g x |
| 810 | a = add a t |
| 811 | e = add t u |
| 812 | t = add d add get w 60 2428436474 |
| 813 | u = shl a 32 |
| 814 | u = or u shr u 32 |
| 815 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 816 | t = add t u |
| 817 | t = add t xor c and a xor b c |
| 818 | u = shl e 32 |
| 819 | u = or u shr u 32 |
| 820 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 821 | u = add u xor f and xor e f xor f g |
| 822 | x = add x t |
| 823 | d = add t u |
| 824 | t = add c add get w 61 2756734187 |
| 825 | u = shl x 32 |
| 826 | u = or u shr u 32 |
| 827 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 828 | t = add t u |
| 829 | t = add t xor b and x xor a b |
| 830 | u = shl d 32 |
| 831 | u = or u shr u 32 |
| 832 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 833 | u = add u xor e and xor d e xor e f |
| 834 | g = add g t |
| 835 | c = add t u |
| 836 | t = add b add get w 62 3204031479 |
| 837 | u = shl g 32 |
| 838 | u = or u shr u 32 |
| 839 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 840 | t = add t u |
| 841 | t = add t xor a and g xor x a |
| 842 | u = shl c 32 |
| 843 | u = or u shr u 32 |
| 844 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 845 | u = add u xor d and xor c d xor d e |
| 846 | f = add f t |
| 847 | b = add t u |
| 848 | t = add a add get w 63 3329325298 |
| 849 | u = shl f 32 |
| 850 | u = or u shr u 32 |
| 851 | u = xor xor shr u 6 shr u 11 shr u 25 |
| 852 | t = add t u |
| 853 | t = add t xor x and f xor g x |
| 854 | u = shl b 32 |
| 855 | u = or u shr u 32 |
| 856 | u = xor xor shr u 2 shr u 13 shr u 22 |
| 857 | u = add u xor c and xor b c xor c d |
| 858 | e = add e t |
| 859 | a = add t u |
| 860 | t = add ulong get h 0 a |
| 861 | put h 0 uint and t 4294967295 |
| 862 | t = add ulong get h 1 b |
| 863 | put h 1 uint and t 4294967295 |
| 864 | t = add ulong get h 2 c |
| 865 | put h 2 uint and t 4294967295 |
| 866 | t = add ulong get h 3 d |
| 867 | put h 3 uint and t 4294967295 |
| 868 | t = add ulong get h 4 e |
| 869 | put h 4 uint and t 4294967295 |
| 870 | t = add ulong get h 5 f |
| 871 | put h 5 uint and t 4294967295 |
| 872 | t = add ulong get h 6 g |
| 873 | put h 6 uint and t 4294967295 |
| 874 | t = add ulong get h 7 x |
| 875 | put h 7 uint and t 4294967295 |
| 876 | end |
| 877 | |
| 878 | fn tobuf c edit uchar 104 bi ulong b view uchar 4096 bo ulong n ulong to void |
| 879 | for i ulong n |
| 880 | put c add 40 add bi i get b add bo i |
| 881 | end |
| 882 | end |
| 883 | |
| 884 | note absorb b[0..n), n <= 4096, exactly as src/sha256.c sha256_update does for that piece |
| 885 | fn absorb c edit uchar 104 b view uchar 4096 n ulong w edit ulong 64 to void |
| 886 | if gt n 4096 |
| 887 | trap 3 |
| 888 | end |
| 889 | h = new uint 8 0 |
| 890 | cnt = getle c 32 8 |
| 891 | bi = and cnt 63 |
| 892 | putle c 32 8 add cnt n |
| 893 | off = ulong 0 |
| 894 | if gt bi 0 |
| 895 | lf = sub 64 bi |
| 896 | if lt n lf |
| 897 | tobuf c bi b 0 n |
| 898 | ret |
| 899 | end |
| 900 | tobuf c bi b 0 lf |
| 901 | loadh c h |
| 902 | schedc c w |
| 903 | rounds h w |
| 904 | storeh c h |
| 905 | off = lf |
| 906 | end |
| 907 | if le add off 64 n |
| 908 | loadh c h |
| 909 | while le add off 64 n bound 65 |
| 910 | schedb b off w |
| 911 | rounds h w |
| 912 | off = add off 64 |
| 913 | end |
| 914 | storeh c h |
| 915 | end |
| 916 | tobuf c 0 b off sub n off |
| 917 | end |
| 918 | |
| 919 | fn init c edit uchar 104 to void |
| 920 | putle c 32 8 0 |
| 921 | putle c 0 4 1779033703 |
| 922 | putle c 4 4 3144134277 |
| 923 | putle c 8 4 1013904242 |
| 924 | putle c 12 4 2773480762 |
| 925 | putle c 16 4 1359893119 |
| 926 | putle c 20 4 2600822924 |
| 927 | putle c 24 4 528734635 |
| 928 | putle c 28 4 1541459225 |
| 929 | end |
| 930 | |
| 931 | note compress the context's own block buffer (bytes 40..103) into its hash words |
| 932 | fn compbuf c edit uchar 104 w edit ulong 64 to void |
| 933 | h = new uint 8 0 |
| 934 | loadh c h |
| 935 | schedc c w |
| 936 | rounds h w |
| 937 | storeh c h |
| 938 | end |
| 939 | |
| 940 | note zero buffer bytes s..e-1 (indices into the 64-byte block) |
| 941 | fn zbuf c edit uchar 104 s ulong e ulong to void |
| 942 | i = s |
| 943 | while lt i e bound 64 |
| 944 | put c add 40 i 0 |
| 945 | i = add i 1 |
| 946 | end |
| 947 | end |
| 948 | |
| 949 | note sha256_final of src/sha256.c, padding in place: 0x80, zeros to 56 mod 64 (one more block |
| 950 | note when the pending part is 56 bytes or more), the 64-bit big-endian bit count, then the digest. |
| 951 | note The count field is left as it was (src/sha256.c adds the padding to it; nothing reads it after). |
| 952 | fn final c edit uchar 104 w edit ulong 64 to void |
| 953 | cnt = getle c 32 8 |
| 954 | bi = and cnt 63 |
| 955 | put c add 40 bi 128 |
| 956 | zbuf c add bi 1 64 |
| 957 | if ge bi 56 |
| 958 | compbuf c w |
| 959 | zbuf c 0 56 |
| 960 | end |
| 961 | bits = mul cnt 8 |
| 962 | for i ulong 8 |
| 963 | sh = mul sub 7 i 8 |
| 964 | put c add 96 i uchar and shr bits sh 255 |
| 965 | end |
| 966 | compbuf c w |
| 967 | h = new uint 8 0 |
| 968 | loadh c h |
| 969 | digest8 h w |
| 970 | end |
| 971 | |
| 972 | note IV into h |
| 973 | fn initiv h edit uint 8 to void |
| 974 | put h 0 uint 1779033703 |
| 975 | put h 1 uint 3144134277 |
| 976 | put h 2 uint 1013904242 |
| 977 | put h 3 uint 2773480762 |
| 978 | put h 4 uint 1359893119 |
| 979 | put h 5 uint 2600822924 |
| 980 | put h 6 uint 528734635 |
| 981 | put h 7 uint 1541459225 |
| 982 | end |
| 983 | |
| 984 | note expand w[0..16) into the full 64-word schedule, unrolled (constant indices need no bounds checks). |
| 985 | note Every schedule word is stored duplicated, the 32-bit value in both halves (d = (x << 32) | x), so |
| 986 | note a rotate right by r is the low half of d >> r and a plain shift right by n is d >> (32 + n); sums |
| 987 | note are formed in the low 32 bits (add, and, xor, or need no masks) and duplicated once on store. |
| 988 | fn expand w edit ulong 64 to void |
| 989 | u = ulong 0 |
| 990 | v = ulong 0 |
| 991 | x = ulong 0 |
| 992 | x = get w 14 |
| 993 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 994 | x = get w 1 |
| 995 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 996 | u = add u v |
| 997 | u = add u get w 9 |
| 998 | u = add u get w 0 |
| 999 | u = shl u 32 |
| 1000 | put w 16 or u shr u 32 |
| 1001 | x = get w 15 |
| 1002 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1003 | x = get w 2 |
| 1004 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1005 | u = add u v |
| 1006 | u = add u get w 10 |
| 1007 | u = add u get w 1 |
| 1008 | u = shl u 32 |
| 1009 | put w 17 or u shr u 32 |
| 1010 | x = get w 16 |
| 1011 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1012 | x = get w 3 |
| 1013 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1014 | u = add u v |
| 1015 | u = add u get w 11 |
| 1016 | u = add u get w 2 |
| 1017 | u = shl u 32 |
| 1018 | put w 18 or u shr u 32 |
| 1019 | x = get w 17 |
| 1020 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1021 | x = get w 4 |
| 1022 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1023 | u = add u v |
| 1024 | u = add u get w 12 |
| 1025 | u = add u get w 3 |
| 1026 | u = shl u 32 |
| 1027 | put w 19 or u shr u 32 |
| 1028 | x = get w 18 |
| 1029 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1030 | x = get w 5 |
| 1031 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1032 | u = add u v |
| 1033 | u = add u get w 13 |
| 1034 | u = add u get w 4 |
| 1035 | u = shl u 32 |
| 1036 | put w 20 or u shr u 32 |
| 1037 | x = get w 19 |
| 1038 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1039 | x = get w 6 |
| 1040 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1041 | u = add u v |
| 1042 | u = add u get w 14 |
| 1043 | u = add u get w 5 |
| 1044 | u = shl u 32 |
| 1045 | put w 21 or u shr u 32 |
| 1046 | x = get w 20 |
| 1047 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1048 | x = get w 7 |
| 1049 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1050 | u = add u v |
| 1051 | u = add u get w 15 |
| 1052 | u = add u get w 6 |
| 1053 | u = shl u 32 |
| 1054 | put w 22 or u shr u 32 |
| 1055 | x = get w 21 |
| 1056 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1057 | x = get w 8 |
| 1058 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1059 | u = add u v |
| 1060 | u = add u get w 16 |
| 1061 | u = add u get w 7 |
| 1062 | u = shl u 32 |
| 1063 | put w 23 or u shr u 32 |
| 1064 | x = get w 22 |
| 1065 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1066 | x = get w 9 |
| 1067 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1068 | u = add u v |
| 1069 | u = add u get w 17 |
| 1070 | u = add u get w 8 |
| 1071 | u = shl u 32 |
| 1072 | put w 24 or u shr u 32 |
| 1073 | x = get w 23 |
| 1074 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1075 | x = get w 10 |
| 1076 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1077 | u = add u v |
| 1078 | u = add u get w 18 |
| 1079 | u = add u get w 9 |
| 1080 | u = shl u 32 |
| 1081 | put w 25 or u shr u 32 |
| 1082 | x = get w 24 |
| 1083 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1084 | x = get w 11 |
| 1085 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1086 | u = add u v |
| 1087 | u = add u get w 19 |
| 1088 | u = add u get w 10 |
| 1089 | u = shl u 32 |
| 1090 | put w 26 or u shr u 32 |
| 1091 | x = get w 25 |
| 1092 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1093 | x = get w 12 |
| 1094 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1095 | u = add u v |
| 1096 | u = add u get w 20 |
| 1097 | u = add u get w 11 |
| 1098 | u = shl u 32 |
| 1099 | put w 27 or u shr u 32 |
| 1100 | x = get w 26 |
| 1101 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1102 | x = get w 13 |
| 1103 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1104 | u = add u v |
| 1105 | u = add u get w 21 |
| 1106 | u = add u get w 12 |
| 1107 | u = shl u 32 |
| 1108 | put w 28 or u shr u 32 |
| 1109 | x = get w 27 |
| 1110 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1111 | x = get w 14 |
| 1112 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1113 | u = add u v |
| 1114 | u = add u get w 22 |
| 1115 | u = add u get w 13 |
| 1116 | u = shl u 32 |
| 1117 | put w 29 or u shr u 32 |
| 1118 | x = get w 28 |
| 1119 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1120 | x = get w 15 |
| 1121 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1122 | u = add u v |
| 1123 | u = add u get w 23 |
| 1124 | u = add u get w 14 |
| 1125 | u = shl u 32 |
| 1126 | put w 30 or u shr u 32 |
| 1127 | x = get w 29 |
| 1128 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1129 | x = get w 16 |
| 1130 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1131 | u = add u v |
| 1132 | u = add u get w 24 |
| 1133 | u = add u get w 15 |
| 1134 | u = shl u 32 |
| 1135 | put w 31 or u shr u 32 |
| 1136 | x = get w 30 |
| 1137 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1138 | x = get w 17 |
| 1139 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1140 | u = add u v |
| 1141 | u = add u get w 25 |
| 1142 | u = add u get w 16 |
| 1143 | u = shl u 32 |
| 1144 | put w 32 or u shr u 32 |
| 1145 | x = get w 31 |
| 1146 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1147 | x = get w 18 |
| 1148 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1149 | u = add u v |
| 1150 | u = add u get w 26 |
| 1151 | u = add u get w 17 |
| 1152 | u = shl u 32 |
| 1153 | put w 33 or u shr u 32 |
| 1154 | x = get w 32 |
| 1155 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1156 | x = get w 19 |
| 1157 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1158 | u = add u v |
| 1159 | u = add u get w 27 |
| 1160 | u = add u get w 18 |
| 1161 | u = shl u 32 |
| 1162 | put w 34 or u shr u 32 |
| 1163 | x = get w 33 |
| 1164 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1165 | x = get w 20 |
| 1166 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1167 | u = add u v |
| 1168 | u = add u get w 28 |
| 1169 | u = add u get w 19 |
| 1170 | u = shl u 32 |
| 1171 | put w 35 or u shr u 32 |
| 1172 | x = get w 34 |
| 1173 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1174 | x = get w 21 |
| 1175 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1176 | u = add u v |
| 1177 | u = add u get w 29 |
| 1178 | u = add u get w 20 |
| 1179 | u = shl u 32 |
| 1180 | put w 36 or u shr u 32 |
| 1181 | x = get w 35 |
| 1182 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1183 | x = get w 22 |
| 1184 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1185 | u = add u v |
| 1186 | u = add u get w 30 |
| 1187 | u = add u get w 21 |
| 1188 | u = shl u 32 |
| 1189 | put w 37 or u shr u 32 |
| 1190 | x = get w 36 |
| 1191 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1192 | x = get w 23 |
| 1193 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1194 | u = add u v |
| 1195 | u = add u get w 31 |
| 1196 | u = add u get w 22 |
| 1197 | u = shl u 32 |
| 1198 | put w 38 or u shr u 32 |
| 1199 | x = get w 37 |
| 1200 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1201 | x = get w 24 |
| 1202 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1203 | u = add u v |
| 1204 | u = add u get w 32 |
| 1205 | u = add u get w 23 |
| 1206 | u = shl u 32 |
| 1207 | put w 39 or u shr u 32 |
| 1208 | x = get w 38 |
| 1209 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1210 | x = get w 25 |
| 1211 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1212 | u = add u v |
| 1213 | u = add u get w 33 |
| 1214 | u = add u get w 24 |
| 1215 | u = shl u 32 |
| 1216 | put w 40 or u shr u 32 |
| 1217 | x = get w 39 |
| 1218 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1219 | x = get w 26 |
| 1220 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1221 | u = add u v |
| 1222 | u = add u get w 34 |
| 1223 | u = add u get w 25 |
| 1224 | u = shl u 32 |
| 1225 | put w 41 or u shr u 32 |
| 1226 | x = get w 40 |
| 1227 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1228 | x = get w 27 |
| 1229 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1230 | u = add u v |
| 1231 | u = add u get w 35 |
| 1232 | u = add u get w 26 |
| 1233 | u = shl u 32 |
| 1234 | put w 42 or u shr u 32 |
| 1235 | x = get w 41 |
| 1236 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1237 | x = get w 28 |
| 1238 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1239 | u = add u v |
| 1240 | u = add u get w 36 |
| 1241 | u = add u get w 27 |
| 1242 | u = shl u 32 |
| 1243 | put w 43 or u shr u 32 |
| 1244 | x = get w 42 |
| 1245 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1246 | x = get w 29 |
| 1247 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1248 | u = add u v |
| 1249 | u = add u get w 37 |
| 1250 | u = add u get w 28 |
| 1251 | u = shl u 32 |
| 1252 | put w 44 or u shr u 32 |
| 1253 | x = get w 43 |
| 1254 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1255 | x = get w 30 |
| 1256 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1257 | u = add u v |
| 1258 | u = add u get w 38 |
| 1259 | u = add u get w 29 |
| 1260 | u = shl u 32 |
| 1261 | put w 45 or u shr u 32 |
| 1262 | x = get w 44 |
| 1263 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1264 | x = get w 31 |
| 1265 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1266 | u = add u v |
| 1267 | u = add u get w 39 |
| 1268 | u = add u get w 30 |
| 1269 | u = shl u 32 |
| 1270 | put w 46 or u shr u 32 |
| 1271 | x = get w 45 |
| 1272 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1273 | x = get w 32 |
| 1274 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1275 | u = add u v |
| 1276 | u = add u get w 40 |
| 1277 | u = add u get w 31 |
| 1278 | u = shl u 32 |
| 1279 | put w 47 or u shr u 32 |
| 1280 | x = get w 46 |
| 1281 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1282 | x = get w 33 |
| 1283 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1284 | u = add u v |
| 1285 | u = add u get w 41 |
| 1286 | u = add u get w 32 |
| 1287 | u = shl u 32 |
| 1288 | put w 48 or u shr u 32 |
| 1289 | x = get w 47 |
| 1290 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1291 | x = get w 34 |
| 1292 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1293 | u = add u v |
| 1294 | u = add u get w 42 |
| 1295 | u = add u get w 33 |
| 1296 | u = shl u 32 |
| 1297 | put w 49 or u shr u 32 |
| 1298 | x = get w 48 |
| 1299 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1300 | x = get w 35 |
| 1301 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1302 | u = add u v |
| 1303 | u = add u get w 43 |
| 1304 | u = add u get w 34 |
| 1305 | u = shl u 32 |
| 1306 | put w 50 or u shr u 32 |
| 1307 | x = get w 49 |
| 1308 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1309 | x = get w 36 |
| 1310 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1311 | u = add u v |
| 1312 | u = add u get w 44 |
| 1313 | u = add u get w 35 |
| 1314 | u = shl u 32 |
| 1315 | put w 51 or u shr u 32 |
| 1316 | x = get w 50 |
| 1317 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1318 | x = get w 37 |
| 1319 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1320 | u = add u v |
| 1321 | u = add u get w 45 |
| 1322 | u = add u get w 36 |
| 1323 | u = shl u 32 |
| 1324 | put w 52 or u shr u 32 |
| 1325 | x = get w 51 |
| 1326 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1327 | x = get w 38 |
| 1328 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1329 | u = add u v |
| 1330 | u = add u get w 46 |
| 1331 | u = add u get w 37 |
| 1332 | u = shl u 32 |
| 1333 | put w 53 or u shr u 32 |
| 1334 | x = get w 52 |
| 1335 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1336 | x = get w 39 |
| 1337 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1338 | u = add u v |
| 1339 | u = add u get w 47 |
| 1340 | u = add u get w 38 |
| 1341 | u = shl u 32 |
| 1342 | put w 54 or u shr u 32 |
| 1343 | x = get w 53 |
| 1344 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1345 | x = get w 40 |
| 1346 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1347 | u = add u v |
| 1348 | u = add u get w 48 |
| 1349 | u = add u get w 39 |
| 1350 | u = shl u 32 |
| 1351 | put w 55 or u shr u 32 |
| 1352 | x = get w 54 |
| 1353 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1354 | x = get w 41 |
| 1355 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1356 | u = add u v |
| 1357 | u = add u get w 49 |
| 1358 | u = add u get w 40 |
| 1359 | u = shl u 32 |
| 1360 | put w 56 or u shr u 32 |
| 1361 | x = get w 55 |
| 1362 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1363 | x = get w 42 |
| 1364 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1365 | u = add u v |
| 1366 | u = add u get w 50 |
| 1367 | u = add u get w 41 |
| 1368 | u = shl u 32 |
| 1369 | put w 57 or u shr u 32 |
| 1370 | x = get w 56 |
| 1371 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1372 | x = get w 43 |
| 1373 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1374 | u = add u v |
| 1375 | u = add u get w 51 |
| 1376 | u = add u get w 42 |
| 1377 | u = shl u 32 |
| 1378 | put w 58 or u shr u 32 |
| 1379 | x = get w 57 |
| 1380 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1381 | x = get w 44 |
| 1382 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1383 | u = add u v |
| 1384 | u = add u get w 52 |
| 1385 | u = add u get w 43 |
| 1386 | u = shl u 32 |
| 1387 | put w 59 or u shr u 32 |
| 1388 | x = get w 58 |
| 1389 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1390 | x = get w 45 |
| 1391 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1392 | u = add u v |
| 1393 | u = add u get w 53 |
| 1394 | u = add u get w 44 |
| 1395 | u = shl u 32 |
| 1396 | put w 60 or u shr u 32 |
| 1397 | x = get w 59 |
| 1398 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1399 | x = get w 46 |
| 1400 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1401 | u = add u v |
| 1402 | u = add u get w 54 |
| 1403 | u = add u get w 45 |
| 1404 | u = shl u 32 |
| 1405 | put w 61 or u shr u 32 |
| 1406 | x = get w 60 |
| 1407 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1408 | x = get w 47 |
| 1409 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1410 | u = add u v |
| 1411 | u = add u get w 55 |
| 1412 | u = add u get w 46 |
| 1413 | u = shl u 32 |
| 1414 | put w 62 or u shr u 32 |
| 1415 | x = get w 61 |
| 1416 | u = xor xor shr x 17 shr x 19 shr x 42 |
| 1417 | x = get w 48 |
| 1418 | v = xor xor shr x 7 shr x 18 shr x 35 |
| 1419 | u = add u v |
| 1420 | u = add u get w 56 |
| 1421 | u = add u get w 47 |
| 1422 | u = shl u 32 |
| 1423 | put w 63 or u shr u 32 |
| 1424 | end |
| 1425 | |
| 1426 | note Word access for the aligned paths. b8 is the caller's 8-byte-aligned copy of the input lent a |
| 1427 | note second time as ulong cells (b views the same bytes), so one load brings 8 bytes. Swapping the |
| 1428 | note bytes inside each 32-bit half (two mask-and-shift steps; the constants stay in registers) turns |
| 1429 | note little-endian bytes b0..b7 into the big-endian words b0..b3 (low half) and b4..b7 (high half). |
| 1430 | note Each word is stored duplicated, as expand requires. |
| 1431 | fn blk8 b8 view ulong 512 q ulong w edit ulong 64 to void |
| 1432 | for i ulong 8 |
| 1433 | x = get b8 add q i |
| 1434 | y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 |
| 1435 | y = or shl and y 281470681808895 16 and shr y 16 281470681808895 |
| 1436 | j = mul i 2 |
| 1437 | t = shl y 32 |
| 1438 | put w j or t shr t 32 |
| 1439 | put w add j 1 or and y 18446744069414584320 shr y 32 |
| 1440 | end |
| 1441 | end |
| 1442 | |
| 1443 | note the padded tail b[off..n) (less than 64 bytes), read as words through b8 (the bytes past n |
| 1444 | note are inside the caller's 4096-byte copy and are masked off here): the word holding byte n keeps |
| 1445 | note its k valid bytes and gets 0x80 after them, later words are zero, the 64-bit bit count goes in |
| 1446 | note words 14 and 15; a second block when the tail is 56 bytes or more |
| 1447 | fn tail8 h edit uint 8 b8 view ulong 512 off ulong n ulong w edit ulong 64 to void |
| 1448 | blk8 b8 shr off 3 w |
| 1449 | r = sub n off |
| 1450 | fw = shr r 2 |
| 1451 | k = and r 3 |
| 1452 | sh = mul k 8 |
| 1453 | v = get w fw |
| 1454 | v = and v shl 4294967295 sub 32 sh |
| 1455 | v = and v 4294967295 |
| 1456 | v = or v shl 128 sub 24 sh |
| 1457 | put w fw or v shl v 32 |
| 1458 | i = add fw 1 |
| 1459 | while lt i 16 bound 16 |
| 1460 | put w i 0 |
| 1461 | i = add i 1 |
| 1462 | end |
| 1463 | if ge r 56 |
| 1464 | expand w |
| 1465 | rounds h w |
| 1466 | for j ulong 14 |
| 1467 | put w j 0 |
| 1468 | end |
| 1469 | end |
| 1470 | bits = mul n 8 |
| 1471 | v = shr bits 32 |
| 1472 | put w 14 or v shl v 32 |
| 1473 | v = and bits 4294967295 |
| 1474 | put w 15 or v shl v 32 |
| 1475 | expand w |
| 1476 | rounds h w |
| 1477 | end |
| 1478 | |
| 1479 | note the digest of h as 32 bytes packed little endian into w[0..4): word i holds the big-endian |
| 1480 | note bytes of h[2i] then h[2i+1], so the caller copies the digest out of w with memcpy |
| 1481 | fn digest8 h edit uint 8 w edit ulong 64 to void |
| 1482 | for i ulong 4 |
| 1483 | j = mul i 2 |
| 1484 | x = or ulong get h j shl ulong get h add j 1 32 |
| 1485 | y = or shl and x 71777214294589695 8 and shr x 8 71777214294589695 |
| 1486 | y = or shl and y 281470681808895 16 and shr y 16 281470681808895 |
| 1487 | put w i y |
| 1488 | end |
| 1489 | end |
| 1490 | |
| 1491 | note SHA-256 of the n < 4096 bytes in b8, state in locals only (sha256_hash's short path) |
| 1492 | fn oneshot b8 view ulong 512 n ulong w edit ulong 64 to void |
| 1493 | if ge n 4096 |
| 1494 | trap 3 |
| 1495 | end |
| 1496 | h = new uint 8 0 |
| 1497 | initiv h |
| 1498 | off = ulong 0 |
| 1499 | while le add off 64 n bound 64 |
| 1500 | blk8 b8 shr off 3 w |
| 1501 | expand w |
| 1502 | rounds h w |
| 1503 | off = add off 64 |
| 1504 | end |
| 1505 | tail8 h b8 off n w |
| 1506 | digest8 h w |
| 1507 | end |
| 1508 | |
| 1509 | fn omsha c edit uchar 104 b view uchar 4096 n ulong op ulong w edit ulong 64 b8 view ulong 512 to void |
| 1510 | if eq op 0 |
| 1511 | init c |
| 1512 | ret |
| 1513 | end |
| 1514 | if eq op 1 |
| 1515 | absorb c b n w |
| 1516 | ret |
| 1517 | end |
| 1518 | if eq op 2 |
| 1519 | final c w |
| 1520 | ret |
| 1521 | end |
| 1522 | if eq op 3 |
| 1523 | oneshot b8 n w |
| 1524 | ret |
| 1525 | end |
| 1526 | trap 9 |
| 1527 | end |