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/chacha-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 / chacha-s.om` 462 lines ยท 12722 bytes Raw Blame History 1 om2 2 note chacha-s: kernels/chacha.om with 1 pass over the 1 MiB buffer (was 64), for ngrun in gate-front-ir 3 note SPDX-License-Identifier: Apache-2.0 4 note chacha.om: ChaCha20, Poly1305 and AEAD_CHACHA20_POLY1305 exactly as RFC 8439 (module CC). 5 note OM-2 library unit (no om2 line, no main): the build writes `om2`, then the library files. 6 note 7 note API (0 = success; reject codes are documented per function): 8 note chacha20_xor kn ctr buf kn = key(32) || nonce(12); buf ^= keystream from block counter ctr. 9 note 2: len kn != 44, ctr above 2^32 - 1, or the last block would 10 note need a counter above 2^32 - 1 (ctr + blocks > 2^32). 11 note poly1305_mac kd m o o[0..16) = Poly1305(kd one-time key, m). 2: len kd != 32 or len o < 16. 12 note aead_chacha_seal kn aad buf buf = plaintext || 16 bytes of tag room; encrypts in place and 13 note writes the tag at buf[n-16..n). 2: len kn != 44, len buf < 16, 14 note plaintext over the counter range (2^32 - 1 blocks). 15 note aead_chacha_open kn aad buf buf = ciphertext || tag. 0: tag valid, buf[0..n-16) decrypted in 16 note place. 1: tag invalid, buf[0..n-16) zeroed (no unauthenticated 17 note plaintext is ever released). 2: bad lengths as for seal; buf untouched. 18 note 19 note Constant time: no branch, loop count or array index depends on key, nonce, plaintext, keystream, 20 note Poly1305 accumulator or tag bytes; they depend only on lengths, the public counter and the 21 note public verdict of the tag comparison (accumulated with or over all 16 bytes). Poly1305 uses 22 note 26-bit limbs in ulong (products under 2^59) and a branch-free final reduction mod 2^130 - 5 23 note (mask select) and addition of s mod 2^128. Compiler-level caveat: omc emits plain AArch64; we 24 note rely on MUL/ADD/EOR/AND/ORR/shift being data-independent; the DIT bit is not set. Run-time bounds 25 note checks compare public indices only. Keystream, ChaCha state, one-time key and Poly1305 state 26 note arrays are zeroed before return. 27 28 note little-endian 32-bit load of a[o .. o+4) 29 fn chacha20_ld32 a view uchar * o ulong to uint inline 30 x = uint get a add o 3 31 x = or shl x 8 uint get a add o 2 32 x = or shl x 8 uint get a add o 1 33 ret or shl x 8 uint get a o 34 end 35 36 note RFC 8439 2.1 / 2.2: QUARTERROUND(a, b, c, d) on the state x; the four indices are the bytes 37 note of q, a in bits 0..7 up to d in bits 24..31 (one parameter keeps the inlined body in 16 registers) 38 fn chacha20_qr x edit uint 16 q ulong to void inline 39 a = get x and q 15 40 b = get x and shr q 8 15 41 c = get x and shr q 16 15 42 d = get x and shr q 24 15 43 a = add a b 44 d = rotl xor d a 16 45 c = add c d 46 b = rotl xor b c 12 47 a = add a b 48 d = rotl xor d a 8 49 c = add c d 50 b = rotl xor b c 7 51 put x and q 15 a 52 put x and shr q 8 15 b 53 put x and shr q 16 15 c 54 put x and shr q 24 15 d 55 end 56 57 note one column round and one diagonal round (RFC 8439 2.3); i is a constant in each unrolled copy, 58 note so q folds to a number 59 fn chacha20_dround x edit uint 16 to void 60 for i ulong 4 unroll 61 chacha20_qr x or or or i shl add i 4 8 shl add i 8 16 shl add i 12 24 62 end 63 for i ulong 4 unroll 64 chacha20_qr x or or or i shl add and add i 1 3 4 8 shl add and add i 2 3 8 16 shl add and add i 3 3 12 24 65 end 66 end 67 68 note RFC 8439 2.3 state: constants, key words, counter, nonce words (len kn = 44, ctr < 2^32) 69 fn chacha20_init s edit uint 16 kn view uchar * ctr ulong to void 70 put s 0 0x61707865 71 put s 1 0x3320646e 72 put s 2 0x79622d32 73 put s 3 0x6b206574 74 for i ulong 8 unroll 75 put s add i 4 chacha20_ld32 kn mul i 4 76 end 77 put s 12 uint ctr 78 for i ulong 3 unroll 79 put s add i 13 chacha20_ld32 kn add mul i 4 32 80 end 81 end 82 83 note ks = serialize(20 rounds(s) + s); w is the working state 84 fn chacha20_block s view uint 16 w edit uint 16 ks edit uchar 64 to void 85 x = uint 0 86 for i ulong 16 unroll 87 put w i get s i 88 end 89 for r ulong 10 90 chacha20_dround w 91 end 92 for i ulong 16 unroll 93 x = add get w i get s i 94 put ks mul i 4 uchar and x 0xff 95 put ks add mul i 4 1 uchar and shr x 8 0xff 96 put ks add mul i 4 2 uchar and shr x 16 0xff 97 put ks add mul i 4 3 uchar shr x 24 98 end 99 end 100 101 note one keystream block of s into buf[o .. min(o + 64, len buf)); s[12] += 1; returns the new o 102 fn chacha20_step s edit uint 16 w edit uint 16 ks edit uchar 64 buf edit uchar * o ulong to ulong 103 chacha20_block s w ks 104 k = sub len buf o 105 if gt k 64 106 k = 64 107 end 108 for i ulong k 109 q = add o i 110 put buf q xor get buf q get ks i 111 end 112 put s 12 add get s 12 1 113 ret add o k 114 end 115 116 note buf ^= keystream blocks of s, s[12] counting up from its initial value; w, ks scratch 117 fn chacha20_run s edit uint 16 w edit uint 16 ks edit uchar 64 buf edit uchar * to void 118 n = len buf 119 o = ulong 0 120 for h ulong add shr n 22 1 121 for l ulong 65536 122 if ge o n 123 break 124 end 125 o = chacha20_step s w ks buf o 126 end 127 end 128 end 129 130 note RFC 8439 2.4: buf ^= ChaCha20 keystream from block counter ctr 131 fn chacha20_xor kn view uchar * ctr ulong buf edit uchar * to ulong 132 s = new uint 16 0 133 w = new uint 16 0 134 ks = new uchar 64 0 135 if ne len kn 44 136 ret 2 137 end 138 if gt ctr 0xffffffff 139 ret 2 140 end 141 if gt shr add len buf 63 6 sub 0x100000000 ctr 142 ret 2 143 end 144 chacha20_init s kn ctr 145 chacha20_run s w ks buf 146 for i ulong 16 unroll 147 put s i 0 148 put w i 0 149 end 150 for i ulong 64 unroll 151 put ks i 0 152 end 153 ret 0 154 end 155 156 note Poly1305 state p (ulong 24): 0..4 r limbs (26 bits), 5..8 5*r1..5*r4, 9..13 accumulator h 157 note limbs, 14..18 scratch, 19..22 s as four 32-bit words. 158 note Load the one-time key kd (32 bytes): r clamped as RFC 8439 2.5 (bytes 3, 7, 11, 15 and 15; 159 note bytes 4, 8, 12 and 252), then cut into 26-bit limbs; s little-endian words; h = 0. 160 fn poly1305_key p edit ulong 24 kd view uchar * to void 161 c = new uchar 16 0 162 for i ulong 16 unroll 163 put c i get kd i 164 end 165 put c 3 and get c 3 15 166 put c 7 and get c 7 15 167 put c 11 and get c 11 15 168 put c 15 and get c 15 15 169 put c 4 and get c 4 252 170 put c 8 and get c 8 252 171 put c 12 and get c 12 252 172 put p 0 and ulong chacha20_ld32 c 0 0x3ffffff 173 put p 1 and shr ulong chacha20_ld32 c 3 2 0x3ffffff 174 put p 2 and shr ulong chacha20_ld32 c 6 4 0x3ffffff 175 put p 3 and shr ulong chacha20_ld32 c 9 6 0x3ffffff 176 put p 4 shr ulong chacha20_ld32 c 12 8 177 for i ulong 4 unroll 178 put p add i 5 mul get p add i 1 5 179 put p add i 19 ulong chacha20_ld32 kd add mul i 4 16 180 end 181 for i ulong 5 unroll 182 put p add i 9 0 183 end 184 for i ulong 16 unroll 185 put c i 0 186 end 187 end 188 189 note h = (h + block b + hib * 2^128) * r mod 2^130 - 5 (partial: limbs below 2^26 + 2^6) 190 fn poly1305_block p edit ulong 24 b view uchar 16 hib ulong to void 191 h0 = add get p 9 and ulong chacha20_ld32 b 0 0x3ffffff 192 h1 = add get p 10 and shr ulong chacha20_ld32 b 3 2 0x3ffffff 193 h2 = add get p 11 and shr ulong chacha20_ld32 b 6 4 0x3ffffff 194 h3 = add get p 12 and shr ulong chacha20_ld32 b 9 6 0x3ffffff 195 h4 = add get p 13 or shr ulong chacha20_ld32 b 12 8 shl hib 24 196 d = mul h0 get p 0 197 d = add d mul h1 get p 8 198 d = add d mul h2 get p 7 199 d = add d mul h3 get p 6 200 put p 14 add d mul h4 get p 5 201 d = mul h0 get p 1 202 d = add d mul h1 get p 0 203 d = add d mul h2 get p 8 204 d = add d mul h3 get p 7 205 put p 15 add d mul h4 get p 6 206 d = mul h0 get p 2 207 d = add d mul h1 get p 1 208 d = add d mul h2 get p 0 209 d = add d mul h3 get p 8 210 put p 16 add d mul h4 get p 7 211 d = mul h0 get p 3 212 d = add d mul h1 get p 2 213 d = add d mul h2 get p 1 214 d = add d mul h3 get p 0 215 put p 17 add d mul h4 get p 8 216 d = mul h0 get p 4 217 d = add d mul h1 get p 3 218 d = add d mul h2 get p 2 219 d = add d mul h3 get p 1 220 put p 18 add d mul h4 get p 0 221 c = ulong 0 222 for i ulong 5 unroll 223 d = add get p add i 14 c 224 put p add i 9 and d 0x3ffffff 225 c = shr d 26 226 end 227 d = add get p 9 mul c 5 228 put p 9 and d 0x3ffffff 229 put p 10 add get p 10 shr d 26 230 end 231 232 note absorb m: whole 16-byte blocks with the 2^128 bit; a final partial block either RFC 8439 2.5 233 note style (pad 1: append 0x01, zero fill, no 2^128 bit) or zero padded to a whole block as the 234 note AEAD construction of 2.8 requires (pad 0). b is the caller's 16-byte block scratch. 235 fn poly1305_feed p edit ulong 24 b edit uchar 16 m view uchar * pad ulong to void 236 n = len m 237 o = ulong 0 238 for h ulong add shr n 20 1 239 for l ulong 65536 240 if gt add o 16 n 241 break 242 end 243 for i ulong 16 unroll 244 put b i get m add o i 245 end 246 poly1305_block p b 1 247 o = add o 16 248 end 249 end 250 r = sub n o 251 if eq r 0 252 ret 253 end 254 for i ulong 16 unroll 255 put b i 0 256 end 257 for i ulong r 258 put b i get m add o i 259 end 260 if eq pad 1 261 put b r 1 262 poly1305_block p b 0 263 else 264 poly1305_block p b 1 265 end 266 end 267 268 note full carry, then h = h - (2^130 - 5) when that is not negative, by mask select (RFC 8439 2.5) 269 fn poly1305_reduce p edit ulong 24 to void 270 c = ulong 0 271 d = ulong 0 272 for i ulong 4 unroll 273 d = add get p add i 10 c 274 put p add i 10 and d 0x3ffffff 275 c = shr d 26 276 end 277 d = add get p 9 mul c 5 278 put p 9 and d 0x3ffffff 279 put p 10 add get p 10 shr d 26 280 c = 5 281 for i ulong 4 unroll 282 d = add get p add i 9 c 283 put p add i 14 and d 0x3ffffff 284 c = shr d 26 285 end 286 d = sub add get p 13 c 0x4000000 287 put p 18 d 288 m = sub shr d 63 1 289 for i ulong 5 unroll 290 put p add i 9 or and get p add i 9 not m and get p add i 14 m 291 end 292 end 293 294 note o[0..16) = (h + s) mod 2^128, little-endian (h fully reduced, limbs below 2^26) 295 fn poly1305_finish p edit ulong 24 o edit uchar * to void 296 poly1305_reduce p 297 a = add get p 9 shl get p 10 26 298 f = add and a 0xffffffff get p 19 299 put p 14 and f 0xffffffff 300 a = add shr a 32 shl get p 11 20 301 f = add add and a 0xffffffff get p 20 shr f 32 302 put p 15 and f 0xffffffff 303 a = add shr a 32 shl get p 12 14 304 f = add add and a 0xffffffff get p 21 shr f 32 305 put p 16 and f 0xffffffff 306 a = add shr a 32 shl get p 13 8 307 f = add add and a 0xffffffff get p 22 shr f 32 308 put p 17 and f 0xffffffff 309 x = ulong 0 310 for i ulong 4 unroll 311 x = get p add i 14 312 put o mul i 4 uchar and x 0xff 313 put o add mul i 4 1 uchar and shr x 8 0xff 314 put o add mul i 4 2 uchar and shr x 16 0xff 315 put o add mul i 4 3 uchar and shr x 24 0xff 316 end 317 end 318 319 note RFC 8439 2.5: o[0..16) = Poly1305(kd, m) 320 fn poly1305_mac kd view uchar * m view uchar * o edit uchar * to ulong 321 p = new ulong 24 0 322 b = new uchar 16 0 323 if ne len kd 32 324 ret 2 325 end 326 if lt len o 16 327 ret 2 328 end 329 poly1305_key p kd 330 poly1305_feed p b m 1 331 poly1305_finish p o 332 for i ulong 24 unroll 333 put p i 0 334 end 335 for i ulong 16 unroll 336 put b i 0 337 end 338 ret 0 339 end 340 341 note RFC 8439 2.8 tag over aad, pad16, buf[0..n-16), pad16, le64 len aad, le64 (n-16), keyed by the 342 note one-time key pk; the tag replaces pk[0..16) and pk[16..32) is zeroed. 343 fn aead_chacha_mac pk edit uchar 32 aad view uchar * buf view uchar * to void 344 p = new ulong 24 0 345 b = new uchar 16 0 346 poly1305_key p pk 347 poly1305_feed p b aad 0 348 n = sub len buf 16 349 poly1305_feed p b part buf 0 n 0 350 a = len aad 351 for i ulong 8 unroll 352 put b i uchar and shr a mul i 8 0xff 353 put b add i 8 uchar and shr n mul i 8 0xff 354 end 355 poly1305_block p b 1 356 poly1305_finish p pk 357 for i ulong 16 unroll 358 put pk add i 16 0 359 end 360 for i ulong 24 unroll 361 put p i 0 362 end 363 for i ulong 16 unroll 364 put b i 0 365 end 366 end 367 368 note 0 when kn and buf have AEAD lengths (len kn 44, len buf >= 16, ciphertext within the counter 369 note range from block 1), else 2 370 fn aead_chacha_lens kn view uchar * buf view uchar * to ulong 371 if ne len kn 44 372 ret 2 373 end 374 if lt len buf 16 375 ret 2 376 end 377 if gt shr add sub len buf 16 63 6 0xffffffff 378 ret 2 379 end 380 ret 0 381 end 382 383 note zero a, any length (two loop levels: one rolled loop traps past `1000000` passes) 384 fn aead_chacha_zero a edit uchar * to void 385 n = len a 386 o = ulong 0 387 for h ulong add shr n 20 1 388 for l ulong 65536 389 if ge o n 390 break 391 end 392 k = sub n o 393 if gt k 16 394 k = 16 395 end 396 for i ulong k 397 put a add o i 0 398 end 399 o = add o k 400 end 401 end 402 end 403 404 note RFC 8439 2.8 AEAD_CHACHA20_POLY1305 encryption, in place 405 fn aead_chacha_seal kn view uchar * aad view uchar * buf edit uchar * to ulong 406 pk = new uchar 32 0 407 if ne aead_chacha_lens kn buf 0 408 ret 2 409 end 410 m = sub len buf 16 411 r = chacha20_xor kn 0 pk 412 r = or r chacha20_xor kn 1 part buf 0 m 413 aead_chacha_mac pk aad buf 414 for i ulong 16 unroll 415 put buf add m i get pk i 416 end 417 for i ulong 32 unroll 418 put pk i 0 419 end 420 ret r 421 end 422 423 note RFC 8439 2.8 AEAD_CHACHA20_POLY1305 decryption, in place; the tag is checked before any 424 note plaintext is produced, with a constant-time or-accumulated comparison 425 fn aead_chacha_open kn view uchar * aad view uchar * buf edit uchar * to ulong 426 pk = new uchar 32 0 427 if ne aead_chacha_lens kn buf 0 428 ret 2 429 end 430 m = sub len buf 16 431 r = chacha20_xor kn 0 pk 432 aead_chacha_mac pk aad buf 433 d = uchar 0 434 for i ulong 16 unroll 435 d = or d xor get pk i get buf add m i 436 end 437 for i ulong 32 unroll 438 put pk i 0 439 end 440 if ne d 0 441 aead_chacha_zero part buf 0 m 442 ret 1 443 end 444 ret or r chacha20_xor kn 1 part buf 0 m 445 end 446 note bench: ChaCha20 (om/tls/chacha.om chacha20_xor) over a 1 MiB buffer, 64 times in place 447 fn main to int 448 b = new uchar `1048576` 0 449 for i ulong `1048576` 450 put b i uchar and add mul i 31 7 255 451 end 452 kn = new uchar 44 0 453 for i ulong 44 454 put kn i uchar i 455 end 456 bad = ulong 0 457 for r ulong 1 458 bad = or bad chacha20_xor kn 1 b 459 end 460 w = write 1 b 0 `1048576` 461 ret int bad 462 end