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/open-humanity/blob/main/om/sha512.om # open-humanity Privacy-first peer assistance network for sovereign autonomous agents 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 `open-humanity / om / sha512.om` 332 lines ยท 8985 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note sha512.om: SHA-512 (FIPS 180-4 section 6.4) as a library unit: functions only, no main, no 3 note globals. Every function name starts with s512. 4 note 5 note API (one shot): 6 note fn s512hash m view uchar 65536 n ulong o edit uchar 64 to void 7 note writes SHA-512(m[0..n)) as 64 bytes to o[0..64); n > 65536 traps 3. 8 note fn s512pre p view uchar 64 m view uchar 65536 n ulong o edit uchar 64 to void 9 note writes SHA-512(p[0..64) || m[0..n)) to o[0..64); n > 65536 traps 3. 10 note 11 note Words are ulong (64-bit unsigned, wrapping add is the mod 2^64 addition of FIPS 180-4 section 3.2). 12 note Omega has no rotate word, so ROTR^r(x) = (x >> r) | (x << (64 - r)) (FIPS 180-4 section 3.2 item 4). 13 note Not constant-time audited. 14 15 note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes 16 note (FIPS 180-4 section 4.2.3) 17 fn s512k k edit ulong 80 to void 18 put k 0 `4794697086780616226` 19 put k 1 `8158064640168781261` 20 put k 2 `13096744586834688815` 21 put k 3 `16840607885511220156` 22 put k 4 `4131703408338449720` 23 put k 5 `6480981068601479193` 24 put k 6 `10538285296894168987` 25 put k 7 `12329834152419229976` 26 put k 8 `15566598209576043074` 27 put k 9 `1334009975649890238` 28 put k 10 `2608012711638119052` 29 put k 11 `6128411473006802146` 30 put k 12 `8268148722764581231` 31 put k 13 `9286055187155687089` 32 put k 14 `11230858885718282805` 33 put k 15 `13951009754708518548` 34 put k 16 `16472876342353939154` 35 put k 17 `17275323862435702243` 36 put k 18 `1135362057144423861` 37 put k 19 `2597628984639134821` 38 put k 20 `3308224258029322869` 39 put k 21 `5365058923640841347` 40 put k 22 `6679025012923562964` 41 put k 23 `8573033837759648693` 42 put k 24 `10970295158949994411` 43 put k 25 `12119686244451234320` 44 put k 26 `12683024718118986047` 45 put k 27 `13788192230050041572` 46 put k 28 `14330467153632333762` 47 put k 29 `15395433587784984357` 48 put k 30 `489312712824947311` 49 put k 31 `1452737877330783856` 50 put k 32 `2861767655752347644` 51 put k 33 `3322285676063803686` 52 put k 34 `5560940570517711597` 53 put k 35 `5996557281743188959` 54 put k 36 `7280758554555802590` 55 put k 37 `8532644243296465576` 56 put k 38 `9350256976987008742` 57 put k 39 `10552545826968843579` 58 put k 40 `11727347734174303076` 59 put k 41 `12113106623233404929` 60 put k 42 `14000437183269869457` 61 put k 43 `14369950271660146224` 62 put k 44 `15101387698204529176` 63 put k 45 `15463397548674623760` 64 put k 46 `17586052441742319658` 65 put k 47 `1182934255886127544` 66 put k 48 `1847814050463011016` 67 put k 49 `2177327727835720531` 68 put k 50 `2830643537854262169` 69 put k 51 `3796741975233480872` 70 put k 52 `4115178125766777443` 71 put k 53 `5681478168544905931` 72 put k 54 `6601373596472566643` 73 put k 55 `7507060721942968483` 74 put k 56 `8399075790359081724` 75 put k 57 `8693463985226723168` 76 put k 58 `9568029438360202098` 77 put k 59 `10144078919501101548` 78 put k 60 `10430055236837252648` 79 put k 61 `11840083180663258601` 80 put k 62 `13761210420658862357` 81 put k 63 `14299343276471374635` 82 put k 64 `14566680578165727644` 83 put k 65 `15097957966210449927` 84 put k 66 `16922976911328602910` 85 put k 67 `17689382322260857208` 86 put k 68 `500013540394364858` 87 put k 69 `748580250866718886` 88 put k 70 `1242879168328830382` 89 put k 71 `1977374033974150939` 90 put k 72 `2944078676154940804` 91 put k 73 `3659926193048069267` 92 put k 74 `4368137639120453308` 93 put k 75 `4836135668995329356` 94 put k 76 `5532061633213252278` 95 put k 77 `6448918945643986474` 96 put k 78 `6902733635092675308` 97 put k 79 `7801388544844847127` 98 end 99 100 note H(0) for SHA-512: first 64 bits of the fractional parts of the square roots of the first 8 primes 101 note (FIPS 180-4 section 5.3.5) 102 fn s512iv h edit ulong 8 to void 103 put h 0 `7640891576956012808` 104 put h 1 `13503953896175478587` 105 put h 2 `4354685564936845355` 106 put h 3 `11912009170470909681` 107 put h 4 `5840696475078001361` 108 put h 5 `11170449401992604703` 109 put h 6 `2270897969802886507` 110 put h 7 `6620516959819538809` 111 end 112 113 note upper-case Sigma0{512}(x) = ROTR^28 ^ ROTR^34 ^ ROTR^39 (FIPS 180-4 section 4.1.3, (4.10)) 114 fn s512bs0 x ulong to ulong 115 r = or shr x 28 shl x 36 116 r = xor r or shr x 34 shl x 30 117 r = xor r or shr x 39 shl x 25 118 ret r 119 end 120 121 note upper-case Sigma1{512}(x) = ROTR^14 ^ ROTR^18 ^ ROTR^41 (section 4.1.3, (4.11)) 122 fn s512bs1 x ulong to ulong 123 r = or shr x 14 shl x 50 124 r = xor r or shr x 18 shl x 46 125 r = xor r or shr x 41 shl x 23 126 ret r 127 end 128 129 note lower-case sigma0{512}(x) = ROTR^1 ^ ROTR^8 ^ SHR^7 (section 4.1.3, (4.12)) 130 fn s512ss0 x ulong to ulong 131 r = or shr x 1 shl x 63 132 r = xor r or shr x 8 shl x 56 133 r = xor r shr x 7 134 ret r 135 end 136 137 note lower-case sigma1{512}(x) = ROTR^19 ^ ROTR^61 ^ SHR^6 (section 4.1.3, (4.13)) 138 fn s512ss1 x ulong to ulong 139 r = or shr x 19 shl x 45 140 r = xor r or shr x 61 shl x 3 141 r = xor r shr x 6 142 ret r 143 end 144 145 note Ch(x,y,z) = (x and y) xor (not x and z), written as z xor (x and (y xor z)) (section 4.1.3, (4.8)) 146 fn s512ch x ulong y ulong z ulong to ulong 147 ret xor z and x xor y z 148 end 149 150 note Maj(x,y,z) = (x and y) xor (x and z) xor (y and z), written as (x and y) or (z and (x or y)) 151 note (section 4.1.3, (4.9)) 152 fn s512mj x ulong y ulong z ulong to ulong 153 ret or and x y and z or x y 154 end 155 156 note big-endian 64-bit word of the 8 bytes at m[q..q+8) (section 3.1 item 3) 157 fn s512bem m view uchar 65536 q ulong to ulong 158 v = ulong 0 159 for j ulong 8 160 v = or shl v 8 ulong get m add q j 161 end 162 ret v 163 end 164 165 note the same for the 256-byte padding buffer 166 fn s512bep p view uchar 256 q ulong to ulong 167 v = ulong 0 168 for j ulong 8 169 v = or shl v 8 ulong get p add q j 170 end 171 ret v 172 end 173 174 note the same for the 64-byte prefix 175 fn s512bex p view uchar 64 q ulong to ulong 176 v = ulong 0 177 for j ulong 8 178 v = or shl v 8 ulong get p add q j 179 end 180 ret v 181 end 182 183 note message block M(i) at byte o of the stream p[0..pl) || m into w[0..16) (section 6.4.2 step 1, 184 note t = 0..15). pl is 0 or 64 and o is a multiple of 128, so every word lies wholly in p or in m. 185 fn s512ldm p view uchar 64 pl ulong m view uchar 65536 o ulong w edit ulong 80 to void 186 for i ulong 16 187 q = add o mul i 8 188 if lt q pl 189 put w i s512bex p q 190 else 191 put w i s512bem m sub q pl 192 end 193 end 194 end 195 196 note the block at byte o of the padding buffer pb into w[0..16) 197 fn s512ldp pb view uchar 256 o ulong w edit ulong 80 to void 198 for i ulong 16 199 put w i s512bep pb add o mul i 8 200 end 201 end 202 203 note one SHA-512 compression (section 6.4.2 steps 1-4); w[0..16) holds the block on entry 204 fn s512blk h edit ulong 8 w edit ulong 80 k view ulong 80 to void 205 for i ulong 64 206 t = add i 16 207 v = s512ss1 get w sub t 2 208 v = add v get w sub t 7 209 v = add v s512ss0 get w sub t 15 210 v = add v get w i 211 put w t v 212 end 213 a = get h 0 214 b = get h 1 215 c = get h 2 216 d = get h 3 217 e = get h 4 218 f = get h 5 219 g = get h 6 220 x = get h 7 221 for i ulong 80 222 t = add x s512bs1 e 223 t = add t s512ch e f g 224 t = add t get k i 225 t = add t get w i 226 u = add s512bs0 a s512mj a b c 227 x = g 228 g = f 229 f = e 230 e = add d t 231 d = c 232 c = b 233 b = a 234 a = add t u 235 end 236 put h 0 add get h 0 a 237 put h 1 add get h 1 b 238 put h 2 add get h 2 c 239 put h 3 add get h 3 d 240 put h 4 add get h 4 e 241 put h 5 add get h 5 f 242 put h 6 add get h 6 g 243 put h 7 add get h 7 x 244 end 245 246 note Padding (section 5.1.2) of the stream p[0..pl) || m, tot bytes long, whose bytes from off on 247 note (fewer than 128) are not yet compressed: they go to pb[0..r), then one 1 bit (byte 128), zero bytes 248 note up to 112 mod 128, then l = 8 * tot as a 128-bit big-endian integer. pb must be all zero on entry. 249 note Returns the padded length, 128 when r < 112, else 256. The upper 64 bits of l are always 0 here 250 note because tot <= 65600. 251 fn s512pad p view uchar 64 pl ulong m view uchar 65536 off ulong tot ulong pb edit uchar 256 to ulong 252 r = sub tot off 253 for i ulong r 254 q = add off i 255 if lt q pl 256 put pb i get p q 257 else 258 put pb i get m sub q pl 259 end 260 end 261 put pb r 128 262 e = ulong 128 263 if ge r 112 264 e = 256 265 end 266 s512len pb e mul tot 8 267 ret e 268 end 269 270 note the low 64 bits of the 128-bit big-endian length field ending at pb[e-1] 271 fn s512len pb edit uchar 256 e ulong bits ulong to void 272 for i ulong 8 273 y = sub sub e 1 i 274 put pb y uchar and shr bits mul i 8 255 275 end 276 end 277 278 note H(N) as the 64-byte big-endian digest (section 6.4.2, final hash value) 279 fn s512out h view ulong 8 o edit uchar 64 to void 280 for i ulong 8 281 v = get h i 282 for j ulong 8 283 put o add mul i 8 j uchar and shr v mul sub 7 j 8 255 284 end 285 end 286 end 287 288 note SHA-512 of the stream p[0..pl) || m[0..n), pl 0 or 64 (else trap 3), n <= 65536 (else trap 3), 289 note digest to o[0..64). 290 fn s512core p view uchar 64 pl ulong m view uchar 65536 n ulong o edit uchar 64 to void 291 if gt n 65536 292 trap 3 293 end 294 if ne pl 0 295 if ne pl 64 296 trap 3 297 end 298 end 299 h = new ulong 8 0 300 w = new ulong 80 0 301 k = new ulong 80 0 302 pb = new uchar 256 0 303 s512k k 304 s512iv h 305 tot = add pl n 306 off = ulong 0 307 while le add off 128 tot bound 514 308 s512ldm p pl m off w 309 s512blk h w k 310 off = add off 128 311 end 312 e = s512pad p pl m off tot pb 313 s512ldp pb 0 w 314 s512blk h w k 315 if eq e 256 316 s512ldp pb 128 w 317 s512blk h w k 318 end 319 s512out h o 320 end 321 322 note SHA-512(m[0..n)), n <= 65536 (else trap 3), digest to o[0..64) 323 fn s512hash m view uchar 65536 n ulong o edit uchar 64 to void 324 z = new uchar 64 0 325 s512core z 0 m n o 326 end 327 328 note SHA-512(p[0..64) || m[0..n)), n <= 65536 (else trap 3), digest to o[0..64); for Ed25519's 329 note SHA-512(R || A || M) without copying M (RFC 8032 section 5.1.7 step 2) 330 fn s512pre p view uchar 64 m view uchar 65536 n ulong o edit uchar 64 to void 331 s512core p 64 m n o 332 end