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