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