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 / om / tls / mlkem.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note om/tls/mlkem.om: ML-KEM-768 (FIPS 203, August 2024) as an OM-2 library unit (no om2 line, no main, |
| 3 | note no globals). Module code MK; functions mlkem_*. Uses sha3.om (H = SHA3-256, G = SHA3-512, |
| 4 | note J = SHAKE256 to 32 bytes, PRF = SHAKE256 to 128 bytes, XOF = SHAKE128). Composition: after sha3. |
| 5 | note |
| 6 | note API (sizes MKEKSZ 1184, MKDKSZ 2400, MKCTSZ 1088, MKSSZ 32; parameter set k 3, eta1 = eta2 = 2, |
| 7 | note du 10, dv 4): |
| 8 | note mlkem_keygen d view uchar 32 z view uchar 32 ek edit uchar * dk edit uchar * |
| 9 | note ML-KEM.KeyGen_internal(d, z) (Alg 16). 0 ok; 2: len ek != 1184 or len dk != 2400. |
| 10 | note mlkem_keygen_seed s view uchar * ek edit uchar * dk edit uchar * |
| 11 | note s = d || z (64 bytes, the seed form of FIPS 203 / Wycheproof). 0 ok; 2: bad lengths. |
| 12 | note mlkem_encaps ek view uchar * m view uchar 32 ct edit uchar * ss edit uchar 32 |
| 13 | note ML-KEM.Encaps_internal(ek, m) (Alg 17) with the 7.2 input checks first: 2: len ek != 1184 |
| 14 | note or len ct != 1088; 1: ek fails the modulus check (a 12-bit value of ByteDecode12 >= q). |
| 15 | note On a nonzero return ct and ss are zeroed. m is the 32 random bytes (the caller draws them). |
| 16 | note mlkem_decaps dk view uchar * ct view uchar * ss edit uchar * |
| 17 | note ML-KEM.Decaps_internal(dk, c) (Alg 18) with the 7.3 input checks first: 2: len dk != 2400, |
| 18 | note len ct != 1088 or len ss != 32; 3: H(dk[384k .. 768k+32)) != dk[768k+32 .. 768k+64). |
| 19 | note 0: ss = K' when the re-encryption matches c, else the implicit-rejection key J(z || c). |
| 20 | note On a nonzero return ss (if 32 bytes) is zeroed. |
| 21 | note |
| 22 | note Arithmetic: coefficients are ulong in [0, q), q = 3329. mlkem_red x = x - q floor(x M / 2^40), |
| 23 | note M = 330282857 = Ceiling[2^40/3329], exact for every x < 2^28: e = M q - 2^40 = 3177 and |
| 24 | note 2^28 e < 2^40, so x M / 2^40 - x / q = x e / (q 2^40) < 1/q (Wolfram: M=Ceiling[2^40/3329]; |
| 25 | note e=M 3329-2^40 -> 3177; 2^28 e < 2^40 -> True; Log2[(2^28-1) M] -> 56.3 so x M fits 64 bits; a |
| 26 | note 200000-sample check Floor[x M/2^40]==Floor[x/3329] for x < 2^28 -> True). Every product and sum |
| 27 | note reduced here is below 3 q^2 < 2^28. No div or rem instruction touches secret data (KyberSlash). |
| 28 | note Compress_d(x) = floor((2^d x + 1664) / q) mod 2^d via the same multiply-shift; Wolfram checked it |
| 29 | note equals Round[2^d x / q] mod 2^d for d in 1, 4, 5, 10, 11 and every x < q (exhaustive -> True), and |
| 30 | note Decompress_d(y) = (q y + 2^(d-1)) >> d equals Round[q y / 2^d] for every y < 2^d, d 1, 4, 10 -> True. |
| 31 | note NTT constants: zetas[i] = 17^BitRev7(i) mod q (Wolfram PowerMod[17, br7[i], 3329], i 0..127, |
| 32 | note MultiplicativeOrder[17, 3329] -> 256); base-case gammas 17^(2 BitRev7(i) + 1) mod q equal |
| 33 | note zetas[64 + i/2] for even i and q - zetas[64 + i/2] for odd i (Wolfram table compare: g[0] 17, |
| 34 | note g[1] 3312, g[2] 2761 ...), so no second table. 128^-1 mod q = 3303 (Wolfram PowerMod[128,-1,3329]). |
| 35 | note |
| 36 | note Constant time: no branch, loop count or array index depends on d, z, m, sigma, r, s, e, y, the |
| 37 | note decrypted m', K, K', or the comparison of c with the re-encryption c'. Loop counts and indices |
| 38 | note depend only on the parameter set, the public matrix seed rho (SampleNTT rejection sampling on |
| 39 | note SHAKE128(rho || j || i) is public, FIPS 203 3.3) and public lengths. The decaps compare ORs the |
| 40 | note xor of all 1088 bytes and turns it into an all-ones / zero mask arithmetically; K is selected by |
| 41 | note mask. The 7.3 hash-check verdict and the 7.2 modulus verdict are returned (they classify the |
| 42 | note input, as FIPS 203 requires). omc emits plain AArch64 (MUL, ADD, EOR, AND, shifts, no data- |
| 43 | note dependent instruction choice); the DIT bit is not set. All secret local arrays (seeds, G output, |
| 44 | note noise and secret polynomials, PRF output, m', K', K-bar, c') are zeroed before return. |
| 45 | |
| 46 | const MKEKSZ ulong 1184 |
| 47 | const MKDKSZ ulong 2400 |
| 48 | const MKCTSZ ulong 1088 |
| 49 | const MKSSZ ulong 32 |
| 50 | const MKQ ulong 3329 |
| 51 | const MKM ulong 330282857 |
| 52 | |
| 53 | note zetas[i] = 17^BitRev7(i) mod 3329, i = 0..127 (Wolfram, see the header) |
| 54 | fn mlkem_zetas z edit ulong 128 to void |
| 55 | put z 0 1 |
| 56 | put z 1 1729 |
| 57 | put z 2 2580 |
| 58 | put z 3 3289 |
| 59 | put z 4 2642 |
| 60 | put z 5 630 |
| 61 | put z 6 1897 |
| 62 | put z 7 848 |
| 63 | put z 8 1062 |
| 64 | put z 9 1919 |
| 65 | put z 10 193 |
| 66 | put z 11 797 |
| 67 | put z 12 2786 |
| 68 | put z 13 3260 |
| 69 | put z 14 569 |
| 70 | put z 15 1746 |
| 71 | put z 16 296 |
| 72 | put z 17 2447 |
| 73 | put z 18 1339 |
| 74 | put z 19 1476 |
| 75 | put z 20 3046 |
| 76 | put z 21 56 |
| 77 | put z 22 2240 |
| 78 | put z 23 1333 |
| 79 | put z 24 1426 |
| 80 | put z 25 2094 |
| 81 | put z 26 535 |
| 82 | put z 27 2882 |
| 83 | put z 28 2393 |
| 84 | put z 29 2879 |
| 85 | put z 30 1974 |
| 86 | put z 31 821 |
| 87 | put z 32 289 |
| 88 | put z 33 331 |
| 89 | put z 34 3253 |
| 90 | put z 35 1756 |
| 91 | put z 36 1197 |
| 92 | put z 37 2304 |
| 93 | put z 38 2277 |
| 94 | put z 39 2055 |
| 95 | put z 40 650 |
| 96 | put z 41 1977 |
| 97 | put z 42 2513 |
| 98 | put z 43 632 |
| 99 | put z 44 2865 |
| 100 | put z 45 33 |
| 101 | put z 46 1320 |
| 102 | put z 47 1915 |
| 103 | put z 48 2319 |
| 104 | put z 49 1435 |
| 105 | put z 50 807 |
| 106 | put z 51 452 |
| 107 | put z 52 1438 |
| 108 | put z 53 2868 |
| 109 | put z 54 1534 |
| 110 | put z 55 2402 |
| 111 | put z 56 2647 |
| 112 | put z 57 2617 |
| 113 | put z 58 1481 |
| 114 | put z 59 648 |
| 115 | put z 60 2474 |
| 116 | put z 61 3110 |
| 117 | put z 62 1227 |
| 118 | put z 63 910 |
| 119 | put z 64 17 |
| 120 | put z 65 2761 |
| 121 | put z 66 583 |
| 122 | put z 67 2649 |
| 123 | put z 68 1637 |
| 124 | put z 69 723 |
| 125 | put z 70 2288 |
| 126 | put z 71 1100 |
| 127 | put z 72 1409 |
| 128 | put z 73 2662 |
| 129 | put z 74 3281 |
| 130 | put z 75 233 |
| 131 | put z 76 756 |
| 132 | put z 77 2156 |
| 133 | put z 78 3015 |
| 134 | put z 79 3050 |
| 135 | put z 80 1703 |
| 136 | put z 81 1651 |
| 137 | put z 82 2789 |
| 138 | put z 83 1789 |
| 139 | put z 84 1847 |
| 140 | put z 85 952 |
| 141 | put z 86 1461 |
| 142 | put z 87 2687 |
| 143 | put z 88 939 |
| 144 | put z 89 2308 |
| 145 | put z 90 2437 |
| 146 | put z 91 2388 |
| 147 | put z 92 733 |
| 148 | put z 93 2337 |
| 149 | put z 94 268 |
| 150 | put z 95 641 |
| 151 | put z 96 1584 |
| 152 | put z 97 2298 |
| 153 | put z 98 2037 |
| 154 | put z 99 3220 |
| 155 | put z 100 375 |
| 156 | put z 101 2549 |
| 157 | put z 102 2090 |
| 158 | put z 103 1645 |
| 159 | put z 104 1063 |
| 160 | put z 105 319 |
| 161 | put z 106 2773 |
| 162 | put z 107 757 |
| 163 | put z 108 2099 |
| 164 | put z 109 561 |
| 165 | put z 110 2466 |
| 166 | put z 111 2594 |
| 167 | put z 112 2804 |
| 168 | put z 113 1092 |
| 169 | put z 114 403 |
| 170 | put z 115 1026 |
| 171 | put z 116 1143 |
| 172 | put z 117 2150 |
| 173 | put z 118 2775 |
| 174 | put z 119 886 |
| 175 | put z 120 1722 |
| 176 | put z 121 1212 |
| 177 | put z 122 1874 |
| 178 | put z 123 1029 |
| 179 | put z 124 2110 |
| 180 | put z 125 2935 |
| 181 | put z 126 885 |
| 182 | put z 127 2154 |
| 183 | end |
| 184 | |
| 185 | note x mod q for x < 2^28, branch-free (header) |
| 186 | fn mlkem_red x ulong to ulong inline |
| 187 | ret sub x mul MKQ shr mul x MKM 40 |
| 188 | end |
| 189 | |
| 190 | fn mlkem_pzero f edit ulong 256 to void |
| 191 | for i ulong 256 |
| 192 | put f i 0 |
| 193 | end |
| 194 | end |
| 195 | |
| 196 | note f = v[256 i .. 256 i + 256) |
| 197 | fn mlkem_vget f edit ulong 256 v view ulong * i ulong to void |
| 198 | o = mul i 256 |
| 199 | for j ulong 256 |
| 200 | put f j get v add o j |
| 201 | end |
| 202 | end |
| 203 | |
| 204 | note v[256 i .. 256 i + 256) = f |
| 205 | fn mlkem_vput v edit ulong * i ulong f view ulong 256 to void |
| 206 | o = mul i 256 |
| 207 | for j ulong 256 |
| 208 | put v add o j get f j |
| 209 | end |
| 210 | end |
| 211 | |
| 212 | note r = r + a mod q |
| 213 | fn mlkem_padd r edit ulong 256 a view ulong 256 to void |
| 214 | for i ulong 256 |
| 215 | put r i mlkem_red add get r i get a i |
| 216 | end |
| 217 | end |
| 218 | |
| 219 | note r = a - r mod q |
| 220 | fn mlkem_psubr r edit ulong 256 a view ulong 256 to void |
| 221 | for i ulong 256 |
| 222 | put r i mlkem_red sub add get a i MKQ get r i |
| 223 | end |
| 224 | end |
| 225 | |
| 226 | note one NTT layer block (Alg 9 steps 5..8): f[x], f[x + n] for x in b .. b + n with zeta w |
| 227 | fn mlkem_ntblk f edit ulong 256 w ulong b ulong n ulong to void |
| 228 | for j ulong n |
| 229 | x = add b j |
| 230 | t = mul w get f add x n |
| 231 | t = sub t mul MKQ shr mul t MKM 40 |
| 232 | u = get f x |
| 233 | v = sub add u MKQ t |
| 234 | v = sub v mul MKQ shr mul v MKM 40 |
| 235 | put f add x n v |
| 236 | u = add u t |
| 237 | u = sub u mul MKQ shr mul u MKM 40 |
| 238 | put f x u |
| 239 | end |
| 240 | end |
| 241 | |
| 242 | note NTT (Alg 9); the layer, block and zeta index are public loop counters |
| 243 | fn mlkem_ntt f edit ulong 256 z view ulong 128 to void |
| 244 | for l ulong 7 |
| 245 | n = shr ulong 128 l |
| 246 | for s ulong shl ulong 1 l |
| 247 | mlkem_ntblk f get z add shl ulong 1 l s mul s shl n 1 n |
| 248 | end |
| 249 | end |
| 250 | end |
| 251 | |
| 252 | note one NTT^-1 block (Alg 10 steps 5..8) |
| 253 | fn mlkem_itblk f edit ulong 256 w ulong b ulong n ulong to void |
| 254 | for j ulong n |
| 255 | x = add b j |
| 256 | t = get f x |
| 257 | u = get f add x n |
| 258 | v = add t u |
| 259 | v = sub v mul MKQ shr mul v MKM 40 |
| 260 | put f x v |
| 261 | v = mul w sub add u MKQ t |
| 262 | v = sub v mul MKQ shr mul v MKM 40 |
| 263 | put f add x n v |
| 264 | end |
| 265 | end |
| 266 | |
| 267 | note NTT^-1 (Alg 10): layers len 2 .. 128, zeta index 127 downwards, then times 128^-1 = 3303 |
| 268 | fn mlkem_intt f edit ulong 256 z view ulong 128 to void |
| 269 | for k ulong 7 |
| 270 | l = sub 6 k |
| 271 | n = shr ulong 128 l |
| 272 | for s ulong shl ulong 1 l |
| 273 | mlkem_itblk f get z sub sub shl ulong 2 l 1 s mul s shl n 1 n |
| 274 | end |
| 275 | end |
| 276 | for i ulong 256 |
| 277 | put f i mlkem_red mul get f i 3303 |
| 278 | end |
| 279 | end |
| 280 | |
| 281 | note r = r + a * b in T_q (MultiplyNTTs, Alg 11 / BaseCaseMultiply Alg 12); pair 2i uses gamma |
| 282 | note zetas[64 + i/2] (i even) or q - zetas[64 + i/2] (i odd) |
| 283 | fn mlkem_bmacc r edit ulong 256 a view ulong 256 b view ulong 256 z view ulong 128 to void |
| 284 | for i ulong 128 |
| 285 | g = get z add 64 shr i 1 |
| 286 | v = add mul and i 1 sub MKQ shl g 1 g |
| 287 | g = sub v mul MKQ shr mul v MKM 40 |
| 288 | x = shl i 1 |
| 289 | v = mul get a add x 1 get b add x 1 |
| 290 | v = sub v mul MKQ shr mul v MKM 40 |
| 291 | v = add mul get a x get b x mul v g |
| 292 | v = sub v mul MKQ shr mul v MKM 40 |
| 293 | v = add get r x v |
| 294 | v = sub v mul MKQ shr mul v MKM 40 |
| 295 | put r x v |
| 296 | v = mul get a x get b add x 1 |
| 297 | v = add v mul get a add x 1 get b x |
| 298 | v = sub v mul MKQ shr mul v MKM 40 |
| 299 | v = add get r add x 1 v |
| 300 | v = sub v mul MKQ shr mul v MKM 40 |
| 301 | put r add x 1 v |
| 302 | end |
| 303 | end |
| 304 | |
| 305 | note ByteEncode_d (Alg 5) of f (coefficients below 2^d) into o[off .. off + 32 d): a bit |
| 306 | note accumulator, least-significant bit first; the inner loop count depends on d only |
| 307 | fn mlkem_benc o edit uchar * off ulong f view ulong 256 d ulong to void |
| 308 | ac = ulong 0 |
| 309 | nb = ulong 0 |
| 310 | for i ulong 256 |
| 311 | ac = or ac shl get f i nb |
| 312 | nb = add nb d |
| 313 | while ge nb 8 bound 2 |
| 314 | put o off uchar and ac 0xff |
| 315 | off = add off 1 |
| 316 | ac = shr ac 8 |
| 317 | nb = sub nb 8 |
| 318 | end |
| 319 | end |
| 320 | end |
| 321 | |
| 322 | note ByteDecode_d (Alg 6) of b[off .. off + 32 d) into f; values reduced mod q (matters for d 12) |
| 323 | fn mlkem_bdec f edit ulong 256 b view uchar * off ulong d ulong to void |
| 324 | ac = ulong 0 |
| 325 | nb = ulong 0 |
| 326 | mk = sub shl ulong 1 d 1 |
| 327 | for i ulong 256 |
| 328 | while lt nb d bound 2 |
| 329 | ac = or ac shl ulong get b off nb |
| 330 | off = add off 1 |
| 331 | nb = add nb 8 |
| 332 | end |
| 333 | put f i mlkem_red and ac mk |
| 334 | ac = shr ac d |
| 335 | nb = sub nb d |
| 336 | end |
| 337 | end |
| 338 | |
| 339 | note Compress_d (4.7): floor((2^d x + 1664) / q) mod 2^d by multiply-shift (header), x < q |
| 340 | fn mlkem_comp f edit ulong 256 d ulong to void |
| 341 | mk = sub shl ulong 1 d 1 |
| 342 | for i ulong 256 |
| 343 | x = add shl get f i d 1664 |
| 344 | put f i and shr mul x MKM 40 mk |
| 345 | end |
| 346 | end |
| 347 | |
| 348 | note Decompress_d (4.8): (q y + 2^(d-1)) >> d |
| 349 | fn mlkem_dcmp f edit ulong 256 d ulong to void |
| 350 | h = shl ulong 1 sub d 1 |
| 351 | for i ulong 256 |
| 352 | put f i shr add mul MKQ get f i h d |
| 353 | end |
| 354 | end |
| 355 | |
| 356 | note SamplePolyCBD_2 (Alg 8): coefficient 2j from the low nibble of b[j], 2j + 1 from the high one |
| 357 | fn mlkem_cbd f edit ulong 256 b view uchar 128 to void |
| 358 | for j ulong 128 |
| 359 | x = ulong get b j |
| 360 | p = add and x 1 and shr x 1 1 |
| 361 | n = add and shr x 2 1 and shr x 3 1 |
| 362 | put f shl j 1 mlkem_red sub add p MKQ n |
| 363 | p = add and shr x 4 1 and shr x 5 1 |
| 364 | n = add and shr x 6 1 and shr x 7 1 |
| 365 | put f add shl j 1 1 mlkem_red sub add p MKQ n |
| 366 | end |
| 367 | end |
| 368 | |
| 369 | note f = SamplePolyCBD_2(PRF_2(s, n)), PRF_2(s, n) = SHAKE256(s || n, 128 bytes) (4.1) |
| 370 | fn mlkem_noise f edit ulong 256 s view uchar 32 n ulong to void |
| 371 | c = new ulong S3CN 0 |
| 372 | nb = new uchar 1 0 |
| 373 | o = new uchar 128 0 |
| 374 | put nb 0 uchar n |
| 375 | sha3_shake256_init c |
| 376 | sha3_absorb c s |
| 377 | sha3_absorb c nb |
| 378 | sha3_squeeze c o |
| 379 | mlkem_cbd f o |
| 380 | for i ulong S3CN |
| 381 | put c i 0 |
| 382 | end |
| 383 | for i ulong 128 |
| 384 | put o i 0 |
| 385 | end |
| 386 | end |
| 387 | |
| 388 | note SampleNTT steps 6..15 for one 168-byte SHAKE128 block o: appends accepted values to f from |
| 389 | note index n, returns the new count (public data: rejection on the public matrix seed) |
| 390 | fn mlkem_spar f edit ulong 256 o view uchar 168 n ulong to ulong |
| 391 | for t ulong 56 |
| 392 | p = mul t 3 |
| 393 | c1 = ulong get o add p 1 |
| 394 | x = add ulong get o p shl and c1 15 8 |
| 395 | y = or shr c1 4 shl ulong get o add p 2 4 |
| 396 | if lt x MKQ |
| 397 | put f n x |
| 398 | n = add n 1 |
| 399 | end |
| 400 | if and lt y MKQ lt n 256 |
| 401 | put f n y |
| 402 | n = add n 1 |
| 403 | end |
| 404 | if eq n 256 |
| 405 | break |
| 406 | end |
| 407 | end |
| 408 | ret n |
| 409 | end |
| 410 | |
| 411 | note SampleNTT(rho || j || i) (Alg 7) from SHAKE128 in 168-byte blocks. Public data: the rejection |
| 412 | note branches and the block count depend on rho only. A block yields 112 candidates accepted with |
| 413 | note probability 3329/4096; the while bound (999999 blocks) is unreachable short of a SHAKE128 break. |
| 414 | fn mlkem_sntt f edit ulong 256 rho view uchar 32 i ulong j ulong to void |
| 415 | c = new ulong S3CN 0 |
| 416 | bb = new uchar 34 0 |
| 417 | o = new uchar 168 0 |
| 418 | for k ulong 32 |
| 419 | put bb k get rho k |
| 420 | end |
| 421 | put bb 32 uchar j |
| 422 | put bb 33 uchar i |
| 423 | sha3_shake128_init c |
| 424 | sha3_absorb c bb |
| 425 | n = ulong 0 |
| 426 | while lt n 256 bound 999999 |
| 427 | sha3_squeeze c o |
| 428 | n = mlkem_spar f o n |
| 429 | end |
| 430 | end |
| 431 | |
| 432 | note A-hat (3 x 3 polynomials, entry (i, j) at 256 (3 i + j)) = SampleNTT(rho || j || i) |
| 433 | fn mlkem_genmat a edit ulong 2304 rho view uchar 32 to void |
| 434 | f = new ulong 256 0 |
| 435 | for i ulong 3 |
| 436 | for j ulong 3 |
| 437 | mlkem_sntt f rho i j |
| 438 | mlkem_vput a add mul i 3 j f |
| 439 | end |
| 440 | end |
| 441 | end |
| 442 | |
| 443 | note t[i] = sum_j A[i][j] o s[j] (tr 0) or sum_j A[j][i] o s[j] (tr 1), in T_q |
| 444 | fn mlkem_matvec t edit ulong 768 a view ulong 2304 s view ulong 768 tr ulong z view ulong 128 to void |
| 445 | r = new ulong 256 0 |
| 446 | x = new ulong 256 0 |
| 447 | y = new ulong 256 0 |
| 448 | for i ulong 3 |
| 449 | mlkem_pzero r |
| 450 | for j ulong 3 |
| 451 | k = add mul i 3 j |
| 452 | if eq tr 1 |
| 453 | k = add mul j 3 i |
| 454 | end |
| 455 | mlkem_vget x a k |
| 456 | mlkem_vget y s j |
| 457 | mlkem_bmacc r x y z |
| 458 | end |
| 459 | mlkem_vput t i r |
| 460 | end |
| 461 | mlkem_pzero r |
| 462 | mlkem_pzero y |
| 463 | end |
| 464 | |
| 465 | note r = sum_j t[j] o s[j] in T_q |
| 466 | fn mlkem_dot r edit ulong 256 t view ulong 768 s view ulong 768 z view ulong 128 to void |
| 467 | x = new ulong 256 0 |
| 468 | y = new ulong 256 0 |
| 469 | mlkem_pzero r |
| 470 | for j ulong 3 |
| 471 | mlkem_vget x t j |
| 472 | mlkem_vget y s j |
| 473 | mlkem_bmacc r x y z |
| 474 | end |
| 475 | mlkem_pzero x |
| 476 | mlkem_pzero y |
| 477 | end |
| 478 | |
| 479 | note s-hat = NTT(s), e-hat = NTT(e) with s[i] = CBD(PRF(sigma, i)), e[i] = CBD(PRF(sigma, 3 + i)) |
| 480 | fn mlkem_kgse s edit ulong 768 e edit ulong 768 sig view uchar 32 to void |
| 481 | z = new ulong 128 0 |
| 482 | f = new ulong 256 0 |
| 483 | mlkem_zetas z |
| 484 | for i ulong 3 |
| 485 | mlkem_noise f sig i |
| 486 | mlkem_ntt f z |
| 487 | mlkem_vput s i f |
| 488 | mlkem_noise f sig add i 3 |
| 489 | mlkem_ntt f z |
| 490 | mlkem_vput e i f |
| 491 | end |
| 492 | mlkem_pzero f |
| 493 | end |
| 494 | |
| 495 | note t = A-hat o s + t (t holds e-hat on entry) |
| 496 | fn mlkem_kgt t edit ulong 768 a view ulong 2304 s view ulong 768 to void |
| 497 | z = new ulong 128 0 |
| 498 | u = new ulong 768 0 |
| 499 | f = new ulong 256 0 |
| 500 | x = new ulong 256 0 |
| 501 | mlkem_zetas z |
| 502 | mlkem_matvec u a s 0 z |
| 503 | for i ulong 3 |
| 504 | mlkem_vget f u i |
| 505 | mlkem_vget x t i |
| 506 | mlkem_padd f x |
| 507 | mlkem_vput t i f |
| 508 | end |
| 509 | for i ulong 768 |
| 510 | put u i 0 |
| 511 | end |
| 512 | mlkem_pzero f |
| 513 | mlkem_pzero x |
| 514 | end |
| 515 | |
| 516 | note o[0 .. 384 k) = ByteEncode12 of the three polynomials of v |
| 517 | fn mlkem_venc o edit uchar * v view ulong 768 to void |
| 518 | f = new ulong 256 0 |
| 519 | for i ulong 3 |
| 520 | mlkem_vget f v i |
| 521 | mlkem_benc o mul i 384 f 12 |
| 522 | end |
| 523 | mlkem_pzero f |
| 524 | end |
| 525 | |
| 526 | note K-PKE.KeyGen core (Alg 13 steps 3..20) from rho and sigma: ek = ByteEncode12(t-hat) || rho, |
| 527 | note dk[0 .. 1152) = ByteEncode12(s-hat) |
| 528 | fn mlkem_kgcore rho view uchar 32 sig view uchar 32 ek edit uchar * dk edit uchar * to void |
| 529 | a = new ulong 2304 0 |
| 530 | s = new ulong 768 0 |
| 531 | t = new ulong 768 0 |
| 532 | mlkem_genmat a rho |
| 533 | mlkem_kgse s t sig |
| 534 | mlkem_kgt t a s |
| 535 | mlkem_venc ek t |
| 536 | mlkem_venc dk s |
| 537 | for i ulong 32 |
| 538 | put ek add 1152 i get rho i |
| 539 | end |
| 540 | for i ulong 768 |
| 541 | put s i 0 |
| 542 | end |
| 543 | end |
| 544 | |
| 545 | note K-PKE.Encrypt part u (Alg 14 steps 13..19, 21): c1 = ByteEncode10(Compress10(NTT^-1(A^T o y) + e1)) |
| 546 | fn mlkem_encu a view ulong 2304 yh view ulong 768 r view uchar 32 ct edit uchar * to void |
| 547 | z = new ulong 128 0 |
| 548 | u = new ulong 768 0 |
| 549 | f = new ulong 256 0 |
| 550 | x = new ulong 256 0 |
| 551 | mlkem_zetas z |
| 552 | mlkem_matvec u a yh 1 z |
| 553 | for i ulong 3 |
| 554 | mlkem_vget f u i |
| 555 | mlkem_intt f z |
| 556 | mlkem_noise x r add i 3 |
| 557 | mlkem_padd f x |
| 558 | mlkem_comp f 10 |
| 559 | mlkem_benc ct mul i 320 f 10 |
| 560 | end |
| 561 | for i ulong 768 |
| 562 | put u i 0 |
| 563 | end |
| 564 | mlkem_pzero f |
| 565 | mlkem_pzero x |
| 566 | end |
| 567 | |
| 568 | note K-PKE.Encrypt part v (steps 17, 20..23): c2 = ByteEncode4(Compress4(NTT^-1(t^T o y) + e2 + mu)), |
| 569 | note mu = Decompress1(ByteDecode1(m)) |
| 570 | fn mlkem_encv th view ulong 768 yh view ulong 768 r view uchar 32 m view uchar 32 ct edit uchar * to void |
| 571 | z = new ulong 128 0 |
| 572 | f = new ulong 256 0 |
| 573 | x = new ulong 256 0 |
| 574 | mlkem_zetas z |
| 575 | mlkem_dot f th yh z |
| 576 | mlkem_intt f z |
| 577 | mlkem_noise x r 6 |
| 578 | mlkem_padd f x |
| 579 | mlkem_bdec x m 0 1 |
| 580 | mlkem_dcmp x 1 |
| 581 | mlkem_padd f x |
| 582 | mlkem_comp f 4 |
| 583 | mlkem_benc ct 960 f 4 |
| 584 | mlkem_pzero f |
| 585 | mlkem_pzero x |
| 586 | end |
| 587 | |
| 588 | note K-PKE.Encrypt(ek, m, r) (Alg 14); ek is 1184 bytes (callers check), ct 1088 |
| 589 | fn mlkem_pkeenc ek view uchar * m view uchar 32 r view uchar 32 ct edit uchar * to void |
| 590 | z = new ulong 128 0 |
| 591 | a = new ulong 2304 0 |
| 592 | th = new ulong 768 0 |
| 593 | yh = new ulong 768 0 |
| 594 | f = new ulong 256 0 |
| 595 | rho = new uchar 32 0 |
| 596 | mlkem_zetas z |
| 597 | for i ulong 3 |
| 598 | mlkem_bdec f ek mul i 384 12 |
| 599 | mlkem_vput th i f |
| 600 | end |
| 601 | for i ulong 32 |
| 602 | put rho i get ek add 1152 i |
| 603 | end |
| 604 | mlkem_genmat a rho |
| 605 | for i ulong 3 |
| 606 | mlkem_noise f r i |
| 607 | mlkem_ntt f z |
| 608 | mlkem_vput yh i f |
| 609 | end |
| 610 | mlkem_encu a yh r ct |
| 611 | mlkem_encv th yh r m ct |
| 612 | for i ulong 768 |
| 613 | put yh i 0 |
| 614 | end |
| 615 | mlkem_pzero f |
| 616 | end |
| 617 | |
| 618 | note K-PKE.Decrypt(dk, c) (Alg 15): m = ByteEncode1(Compress1(v' - NTT^-1(s^T o NTT(u')))) |
| 619 | fn mlkem_pkedec dk view uchar * ct view uchar * m edit uchar 32 to void |
| 620 | z = new ulong 128 0 |
| 621 | uh = new ulong 768 0 |
| 622 | sh = new ulong 768 0 |
| 623 | f = new ulong 256 0 |
| 624 | w = new ulong 256 0 |
| 625 | mlkem_zetas z |
| 626 | for i ulong 3 |
| 627 | mlkem_bdec f ct mul i 320 10 |
| 628 | mlkem_dcmp f 10 |
| 629 | mlkem_ntt f z |
| 630 | mlkem_vput uh i f |
| 631 | mlkem_bdec f dk mul i 384 12 |
| 632 | mlkem_vput sh i f |
| 633 | end |
| 634 | mlkem_dot w sh uh z |
| 635 | mlkem_intt w z |
| 636 | mlkem_bdec f ct 960 4 |
| 637 | mlkem_dcmp f 4 |
| 638 | mlkem_psubr w f |
| 639 | mlkem_comp w 1 |
| 640 | mlkem_benc m 0 w 1 |
| 641 | for i ulong 768 |
| 642 | put sh i 0 |
| 643 | put uh i 0 |
| 644 | end |
| 645 | mlkem_pzero f |
| 646 | mlkem_pzero w |
| 647 | end |
| 648 | |
| 649 | note FIPS 203 7.2 modulus check on ek[0 .. 1152) (public): 1 when a 12-bit value is >= q |
| 650 | fn mlkem_ekchk ek view uchar * to ulong |
| 651 | bad = ulong 0 |
| 652 | for i ulong 384 |
| 653 | p = mul i 3 |
| 654 | b0 = ulong get ek p |
| 655 | b1 = ulong get ek add p 1 |
| 656 | b2 = ulong get ek add p 2 |
| 657 | x = or b0 shl and b1 15 8 |
| 658 | y = or shr b1 4 shl b2 4 |
| 659 | bad = or bad shr sub sub MKQ 1 x 63 |
| 660 | bad = or bad shr sub sub MKQ 1 y 63 |
| 661 | end |
| 662 | ret bad |
| 663 | end |
| 664 | |
| 665 | fn mlkem_keygen d view uchar 32 z view uchar 32 ek edit uchar * dk edit uchar * to ulong |
| 666 | if or ne len ek MKEKSZ ne len dk MKDKSZ |
| 667 | ret 2 |
| 668 | end |
| 669 | gi = new uchar 33 0 |
| 670 | g = new uchar 64 0 |
| 671 | rho = new uchar 32 0 |
| 672 | sig = new uchar 32 0 |
| 673 | for i ulong 32 |
| 674 | put gi i get d i |
| 675 | end |
| 676 | put gi 32 3 |
| 677 | sha3_512 gi g |
| 678 | for i ulong 32 |
| 679 | put rho i get g i |
| 680 | put sig i get g add i 32 |
| 681 | end |
| 682 | mlkem_kgcore rho sig ek dk |
| 683 | for i ulong MKEKSZ |
| 684 | put dk add 1152 i get ek i |
| 685 | end |
| 686 | sha3_256 ek part dk 2336 32 |
| 687 | for i ulong 32 |
| 688 | put dk add 2368 i get z i |
| 689 | end |
| 690 | for i ulong 33 |
| 691 | put gi i 0 |
| 692 | end |
| 693 | for i ulong 64 |
| 694 | put g i 0 |
| 695 | end |
| 696 | for i ulong 32 |
| 697 | put sig i 0 |
| 698 | end |
| 699 | ret 0 |
| 700 | end |
| 701 | |
| 702 | fn mlkem_keygen_seed s view uchar * ek edit uchar * dk edit uchar * to ulong |
| 703 | if ne len s 64 |
| 704 | ret 2 |
| 705 | end |
| 706 | d = new uchar 32 0 |
| 707 | z = new uchar 32 0 |
| 708 | for i ulong 32 |
| 709 | put d i get s i |
| 710 | put z i get s add i 32 |
| 711 | end |
| 712 | r = mlkem_keygen d z ek dk |
| 713 | for i ulong 32 |
| 714 | put d i 0 |
| 715 | put z i 0 |
| 716 | end |
| 717 | ret r |
| 718 | end |
| 719 | |
| 720 | fn mlkem_zap o edit uchar * to void |
| 721 | for i ulong len o |
| 722 | put o i 0 |
| 723 | end |
| 724 | end |
| 725 | |
| 726 | fn mlkem_encaps ek view uchar * m view uchar 32 ct edit uchar * ss edit uchar 32 to ulong |
| 727 | if or ne len ek MKEKSZ ne len ct MKCTSZ |
| 728 | mlkem_zap ct |
| 729 | mlkem_zap ss |
| 730 | ret 2 |
| 731 | end |
| 732 | if ne mlkem_ekchk ek 0 |
| 733 | mlkem_zap ct |
| 734 | mlkem_zap ss |
| 735 | ret 1 |
| 736 | end |
| 737 | gi = new uchar 64 0 |
| 738 | g = new uchar 64 0 |
| 739 | r = new uchar 32 0 |
| 740 | for i ulong 32 |
| 741 | put gi i get m i |
| 742 | end |
| 743 | sha3_256 ek part gi 32 32 |
| 744 | sha3_512 gi g |
| 745 | for i ulong 32 |
| 746 | put ss i get g i |
| 747 | put r i get g add i 32 |
| 748 | end |
| 749 | mlkem_pkeenc ek m r ct |
| 750 | for i ulong 64 |
| 751 | put gi i 0 |
| 752 | put g i 0 |
| 753 | end |
| 754 | for i ulong 32 |
| 755 | put r i 0 |
| 756 | end |
| 757 | ret 0 |
| 758 | end |
| 759 | |
| 760 | note all-ones when a[0 .. 1088) != b, else 0, branch-free over every byte |
| 761 | fn mlkem_ctmask a view uchar * b view uchar 1088 to ulong |
| 762 | df = ulong 0 |
| 763 | for i ulong MKCTSZ |
| 764 | df = or df ulong xor get a i get b i |
| 765 | end |
| 766 | mk = sub 0 shr or df sub 0 df 63 |
| 767 | ret mk |
| 768 | end |
| 769 | |
| 770 | note H(ek) check of dk (7.3 step 3): 0 when dk[2336 .. 2368) = SHA3-256(dk[1152 .. 2336)) |
| 771 | fn mlkem_dkchk dk view uchar * to ulong |
| 772 | h = new uchar 32 0 |
| 773 | sha3_256 part dk 1152 MKEKSZ h |
| 774 | df = ulong 0 |
| 775 | for i ulong 32 |
| 776 | df = or df ulong xor get h i get dk add 2336 i |
| 777 | end |
| 778 | ret df |
| 779 | end |
| 780 | |
| 781 | note (K', r') = G(m' || h), h = dk[2336 .. 2368) (Alg 18 step 7) |
| 782 | fn mlkem_dgk dk view uchar * mp view uchar 32 g edit uchar 64 to void |
| 783 | gi = new uchar 64 0 |
| 784 | for i ulong 32 |
| 785 | put gi i get mp i |
| 786 | put gi add i 32 get dk add 2336 i |
| 787 | end |
| 788 | sha3_512 gi g |
| 789 | for i ulong 64 |
| 790 | put gi i 0 |
| 791 | end |
| 792 | end |
| 793 | |
| 794 | note K-bar = J(z || c) = SHAKE256(z || c, 32), z = dk[2368 .. 2400) (Alg 18 step 8) |
| 795 | fn mlkem_dj dk view uchar * ct view uchar * kb edit uchar 32 to void |
| 796 | ji = new uchar 1120 0 |
| 797 | for i ulong 32 |
| 798 | put ji i get dk add 2368 i |
| 799 | end |
| 800 | for i ulong MKCTSZ |
| 801 | put ji add i 32 get ct i |
| 802 | end |
| 803 | sha3_shake256 ji kb |
| 804 | for i ulong 1120 |
| 805 | put ji i 0 |
| 806 | end |
| 807 | end |
| 808 | |
| 809 | note c' = K-PKE.Encrypt(ek_PKE = dk[1152 .. 2336), m', r'), r' = g[32 .. 64) (Alg 18 step 9) |
| 810 | fn mlkem_reenc dk view uchar * mp view uchar 32 g view uchar 64 cp edit uchar 1088 to void |
| 811 | rp = new uchar 32 0 |
| 812 | for i ulong 32 |
| 813 | put rp i get g add i 32 |
| 814 | end |
| 815 | mlkem_pkeenc part dk 1152 MKEKSZ mp rp cp |
| 816 | for i ulong 32 |
| 817 | put rp i 0 |
| 818 | end |
| 819 | end |
| 820 | |
| 821 | note ss = K' xor (mk and (K' xor K-bar)): K' when mk = 0, K-bar when mk is all ones (steps 10, 11) |
| 822 | fn mlkem_sel ss edit uchar * g view uchar 64 kb view uchar 32 mk ulong to void |
| 823 | m8 = and mk 0xff |
| 824 | for i ulong 32 |
| 825 | k = ulong get g i |
| 826 | put ss i uchar xor k and m8 xor k ulong get kb i |
| 827 | end |
| 828 | end |
| 829 | |
| 830 | fn mlkem_decaps dk view uchar * ct view uchar * ss edit uchar * to ulong |
| 831 | if ne len ss MKSSZ |
| 832 | mlkem_zap ss |
| 833 | ret 2 |
| 834 | end |
| 835 | if or ne len dk MKDKSZ ne len ct MKCTSZ |
| 836 | mlkem_zap ss |
| 837 | ret 2 |
| 838 | end |
| 839 | if ne mlkem_dkchk dk 0 |
| 840 | mlkem_zap ss |
| 841 | ret 3 |
| 842 | end |
| 843 | mp = new uchar 32 0 |
| 844 | g = new uchar 64 0 |
| 845 | kb = new uchar 32 0 |
| 846 | cp = new uchar 1088 0 |
| 847 | mlkem_pkedec dk ct mp |
| 848 | mlkem_dgk dk mp g |
| 849 | mlkem_dj dk ct kb |
| 850 | mlkem_reenc dk mp g cp |
| 851 | mk = mlkem_ctmask ct cp |
| 852 | mlkem_sel ss g kb mk |
| 853 | for i ulong 64 |
| 854 | put g i 0 |
| 855 | end |
| 856 | for i ulong 32 |
| 857 | put mp i 0 |
| 858 | put kb i 0 |
| 859 | end |
| 860 | for i ulong MKCTSZ |
| 861 | put cp i 0 |
| 862 | end |
| 863 | ret 0 |
| 864 | end |