1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
* Copyright (c) The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
*/
/* References
* ==========
*
* - [FIPS203]
* FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
* National Institute of Standards and Technology
* https://csrc.nist.gov/pubs/fips/203/final
*/
/* Parameter set namespacing
* This is to facilitate building multiple instances
* of mlkem-native (e.g. with varying parameter sets)
* within a single compilation unit. */
/* End of parameter set namespacing */
typedef mlk_poly mlk_polyvec;
typedef mlk_poly mlk_polymat;
typedef mlk_poly_mulcache mlk_polyvec_mulcache;
/*************************************************
* Name: mlk_poly_compress_du
*
* Description: Compression (du bits) and subsequent serialization of a
* polynomial
*
* Arguments: - uint8_t *r: pointer to output byte array
* (of length MLKEM_POLYCOMPRESSEDBYTES_DU bytes)
* - const mlk_poly *a: pointer to input polynomial
* Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* Specification: Implements `ByteEncode_{d_u} (Compress_{d_u} (u))`
* in @[FIPS203, Algorithm 14 (K-PKE.Encrypt), L22],
* with level-specific d_u defined in @[FIPS203, Table 2],
* and given by MLKEM_DU here.
*
**************************************************/
static MLK_INLINE void
/*************************************************
* Name: mlk_poly_decompress_du
*
* Description: De-serialization and subsequent decompression (du bits) of a
* polynomial; approximate inverse of mlk_poly_compress_du
*
* Arguments: - mlk_poly *r: pointer to output polynomial
* - const uint8_t *a: pointer to input byte array
* (of length MLKEM_POLYCOMPRESSEDBYTES_DU bytes)
*
* Upon return, the coefficients of the output polynomial are unsigned-canonical
* (non-negative and smaller than MLKEM_Q).
*
* Specification: Implements `Decompress_{d_u} (ByteDecode_{d_u} (u))`
* in @[FIPS203, Algorithm 15 (K-PKE.Decrypt), L3].
* with level-specific d_u defined in @[FIPS203, Table 2],
* and given by MLKEM_DU here.
*
**************************************************/
static MLK_INLINE void
/*************************************************
* Name: mlk_poly_compress_dv
*
* Description: Compression (dv bits) and subsequent serialization of a
* polynomial
*
* Arguments: - uint8_t *r: pointer to output byte array
* (of length MLKEM_POLYCOMPRESSEDBYTES_DV bytes)
* - const mlk_poly *a: pointer to input polynomial
* Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* Specification: Implements `ByteEncode_{d_v} (Compress_{d_v} (v))`
* in @[FIPS203, Algorithm 14 (K-PKE.Encrypt), L23].
* with level-specific d_v defined in @[FIPS203, Table 2],
* and given by MLKEM_DV here.
*
**************************************************/
static MLK_INLINE void
/*************************************************
* Name: mlk_poly_decompress_dv
*
* Description: De-serialization and subsequent decompression (dv bits) of a
* polynomial; approximate inverse of poly_compress
*
* Arguments: - mlk_poly *r: pointer to output polynomial
* - const uint8_t *a: pointer to input byte array
* (of length MLKEM_POLYCOMPRESSEDBYTES_DV bytes)
*
* Upon return, the coefficients of the output polynomial are unsigned-canonical
* (non-negative and smaller than MLKEM_Q).
*
* Specification: Implements `Decompress_{d_v} (ByteDecode_{d_v} (v))`
* in @[FIPS203, Algorithm 15 (K-PKE.Decrypt), L4].
* with level-specific d_v defined in @[FIPS203, Table 2],
* and given by MLKEM_DV here.
*
**************************************************/
static MLK_INLINE void
/*************************************************
* Name: mlk_polyvec_compress_du
*
* Description: Compress and serialize vector of polynomials
*
* Arguments: - uint8_t *r: pointer to output byte array
* (needs space for MLKEM_POLYVECCOMPRESSEDBYTES_DU)
* - const mlk_polyvec a: pointer to input vector of polynomials.
* Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* Specification: Implements `ByteEncode_{d_u} (Compress_{d_u} (u))`
* in @[FIPS203, Algorithm 14 (K-PKE.Encrypt), L22].
* with level-specific d_u defined in @[FIPS203, Table 2],
* and given by MLKEM_DU here.
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_decompress_du
*
* Description: De-serialize and decompress vector of polynomials;
* approximate inverse of mlk_polyvec_compress_du
*
* Arguments: - mlk_polyvec r: pointer to output vector of polynomials.
* Output will have coefficients normalized to [0,..,q-1].
* - const uint8_t *a: pointer to input byte array
* (of length MLKEM_POLYVECCOMPRESSEDBYTES_DU)
*
* Specification: Implements `Decompress_{d_u} (ByteDecode_{d_u} (u))`
* in @[FIPS203, Algorithm 15 (K-PKE.Decrypt), L3].
* with level-specific d_u defined in @[FIPS203, Table 2],
* and given by MLKEM_DU here.
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_tobytes
*
* Description: Serialize vector of polynomials
*
* Arguments: - uint8_t *r: pointer to output byte array
* (needs space for MLKEM_POLYVECBYTES)
* - const mlk_polyvec a: pointer to input vector of polynomials
* Each polynomial must have coefficients in [0,..,q-1].
*
* Specification: Implements ByteEncode_12 @[FIPS203, Algorithm 5].
* Extended to vectors as per
* @[FIPS203, 2.4.8 Applying Algorithms to Arrays]
* and @[FIPS203, 2.4.6, Matrices and Vectors]
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_frombytes
*
* Description: De-serialize vector of polynomials;
* inverse of mlk_polyvec_tobytes
*
* Arguments: - const mlk_polyvec a: pointer to output vector of polynomials
* (of length MLKEM_POLYVECBYTES). Output will have coefficients
* normalized in [0..4095].
* - uint8_t *r: pointer to input byte array
*
* Specification: Implements ByteDecode_12 @[FIPS203, Algorithm 6].
* Extended to vectors as per
* @[FIPS203, 2.4.8 Applying Algorithms to Arrays]
* and @[FIPS203, 2.4.6, Matrices and Vectors]
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_ntt
*
* Description: Apply forward NTT to all elements of a vector of polynomials.
*
* The input is assumed to be in normal order and
* coefficient-wise bound by MLKEM_Q in absolute value.
*
* The output polynomial is in bitreversed order, and
* coefficient-wise bound by MLK_NTT_BOUND in absolute value.
*
* Arguments: - mlk_polyvec r: pointer to in/output vector of polynomials
*
* Specification:
* - Implements @[FIPS203, Algorithm 9, NTT]
* - Extended to vectors as per @[FIPS203, 2.4.6, Matrices and Vectors]
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_invntt_tomont
*
* Description: Apply inverse NTT to all elements of a vector of polynomials
* and multiply by Montgomery factor 2^16
*
* The input is assumed to be in bitreversed order, and can
* have arbitrary coefficients in int16_t.
*
* The output polynomial is in normal order, and
* coefficient-wise bound by MLK_INVNTT_BOUND in absolute value.
*
* Arguments: - mlk_polyvec r: pointer to in/output vector of polynomials
*
* Specification:
* - Implements @[FIPS203, Algorithm 10, NTT^{-1}]
* - Extended to vectors as per @[FIPS203, 2.4.6, Matrices and Vectors]
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_basemul_acc_montgomery_cached
*
* Description: Scalar product of two vectors of polynomials in NTT domain,
* using mulcache for second operand.
*
* Bounds:
* - Every coefficient of a is assumed to be in [0..4095]
* - No bounds guarantees for the coefficients in the result.
*
* Arguments: - mlk_poly *r: pointer to output polynomial
* - const mlk_polyvec a: pointer to first input polynomial vector
* - const mlk_polyvec b: pointer to second input polynomial
* vector
* - const mlk_polyvec_mulcache b_cache: pointer to mulcache
* for second input polynomial vector. Can be computed
* via mlk_polyvec_mulcache_compute().
*
* Specification: Implements
* - @[FIPS203, Section 2.4.7, Eq (2.14)]
* - @[FIPS203, Algorithm 11, MultiplyNTTs]
* - @[FIPS203, Algorithm 12, BaseCaseMultiply]
*
**************************************************/
void ;
/************************************************************
* Name: mlk_polyvec_mulcache_compute
*
* Description: Computes the mulcache for a vector of polynomials in NTT domain
*
* The mulcache of a degree-2 polynomial b := b0 + b1*X
* in Fq[X]/(X^2-zeta) is the value b1*zeta, needed when
* computing products of b in Fq[X]/(X^2-zeta).
*
* The mulcache of a polynomial in NTT domain -- which is
* a 128-tuple of degree-2 polynomials in Fq[X]/(X^2-zeta),
* for varying zeta, is the 128-tuple of mulcaches of those
* polynomials.
*
* The mulcache of a vector of polynomials is the vector
* of mulcaches of its entries.
*
* Arguments: - x: Pointer to mulcache to be populated
* - a: Pointer to input polynomial vector
*
* Specification:
* - Caches `b_1 * \gamma` in @[FIPS203, Algorithm 12, BaseCaseMultiply, L1]
*
************************************************************/
/*
* NOTE: The default C implementation of this function populates
* the mulcache with values in (-q,q), but this is not needed for the
* higher level safety proofs, and thus not part of the spec.
*/
void ;
/*************************************************
* Name: mlk_polyvec_reduce
*
* Description: Applies Barrett reduction to each coefficient
* of each element of a vector of polynomials;
* for details of the Barrett reduction see comments in reduce.c
*
* Arguments: - mlk_polyvec r: pointer to input/output polynomial
*
* Specification: Normalizes on unsigned canoncial representatives
* ahead of calling @[FIPS203, Compress_d, Eq (4.7)].
* This is not made explicit in FIPS 203.
*
**************************************************/
/*
* NOTE: The semantics of mlk_polyvec_reduce() is different in
* the reference implementation, which requires
* signed canonical output data. Unsigned canonical
* outputs are better suited to the only remaining
* use of mlk_poly_reduce() in the context of (de)serialization.
*/
void ;
/*************************************************
* Name: mlk_polyvec_add
*
* Description: Add vectors of polynomials
*
* Arguments: - mlk_polyvec r: pointer to input-output vector of polynomials to
* be added to
* - const mlk_polyvec b: pointer to second input vector of
* polynomials
*
* The coefficients of r and b must be so that the addition does
* not overflow. Otherwise, the behaviour of this function is undefined.
*
* The coefficients returned in *r are in int16_t which is sufficient
* to prove type-safety of calling units. Therefore, no stronger
* ensures clause is required on this function.
*
* Specification:
* - @[FIPS203, 2.4.5, Arithmetic With Polynomials and NTT Representations]
* - Used in @[FIPS203, Algorithm 14 (K-PKE.Encrypt), L19]
*
**************************************************/
void ;
/*************************************************
* Name: mlk_polyvec_tomont
*
* Description: Inplace conversion of all coefficients of a polynomial
* vector from normal domain to Montgomery domain
*
* Bounds: Output < q in absolute value.
*
*
* Specification: Internal normalization required in `mlk_indcpa_keypair_derand`
* as part of matrix-vector multiplication
* @[FIPS203, Algorithm 13, K-PKE.KeyGen, L18].
*
**************************************************/
void ;
/*************************************************
* Name: mlk_poly_getnoise_eta1_4x
*
* Description: Batch sample four polynomials deterministically from a seed
* and nonces, with output polynomials close to centered binomial
* distribution with parameter MLKEM_ETA1.
*
* Arguments: - mlk_poly *r{0,1,2,3}: pointer to output polynomial
* - const uint8_t *seed: pointer to input seed
* (of length MLKEM_SYMBYTES bytes)
* - uint8_t nonce{0,1,2,3}: one-byte input nonce
*
* Specification:
* Implements 4x `SamplePolyCBD_{eta1} (PRF_{eta1} (sigma, N))`:
* - @[FIPS203, Algorithm 8, SamplePolyCBD_eta]
* - @[FIPS203, Eq (4.3), PRF_eta]
* - `SamplePolyCBD_{eta1} (PRF_{eta1} (sigma, N))` appears in
* @[FIPS203, Algorithm 13, K-PKE.KeyGen, L{9, 13}]
* @[FIPS203, Algorithm 14, K-PKE.Encrypt, L10]
*
**************************************************/
void ;
/*
* We only require mlk_poly_getnoise_eta2_4x for ml-kem-768 and ml-kem-1024
* where MLKEM_ETA2 = MLKEM_ETA1 = 2.
* For ml-kem-512, mlk_poly_getnoise_eta1122_4x is used instead.
*/
/* MLKEM_ETA1 == MLKEM_ETA2 */
/*************************************************
* Name: mlk_poly_getnoise_eta2
*
* Description: Sample a polynomial deterministically from a seed and a nonce,
* with output polynomial close to centered binomial distribution
* with parameter MLKEM_ETA2
*
* Arguments: - mlk_poly *r: pointer to output polynomial
* - const uint8_t *seed: pointer to input seed
* (of length MLKEM_SYMBYTES bytes)
* - uint8_t nonce: one-byte input nonce
*
* Specification:
* Implements `SamplePolyCBD_{eta2} (PRF_{eta2} (sigma, N))`:
* - @[FIPS203, Algorithm 8, SamplePolyCBD_eta]
* - @[FIPS203, Eq (4.3), PRF_eta]
* - `SamplePolyCBD_{eta2} (PRF_{eta2} (sigma, N))` appears in
* @[FIPS203, Algorithm 14, K-PKE.Encrypt, L14]
*
**************************************************/
void ;
/* MLKEM_K == 2 || MLKEM_K == 4 */
/*************************************************
* Name: mlk_poly_getnoise_eta1122_4x
*
* Description: Batch sample four polynomials deterministically from a seed
* and a nonces, with output polynomials close to centered binomial
* distribution with parameter MLKEM_ETA1 and MLKEM_ETA2
*
* Arguments: - mlk_poly *r{0,1,2,3}: pointer to output polynomial
* - const uint8_t *seed: pointer to input seed
* (of length MLKEM_SYMBYTES bytes)
* - uint8_t nonce{0,1,2,3}: one-byte input nonce
*
* Specification:
* Implements two instances each of
* `SamplePolyCBD_{eta1} (PRF_{eta1} (sigma, N))` and
* `SamplePolyCBD_{eta2} (PRF_{eta2} (sigma, N))`:
* - @[FIPS203, Algorithm 8, SamplePolyCBD_eta]
* - @[FIPS203, Eq (4.3), PRF_eta]
* - `SamplePolyCBD_{eta2} (PRF_{eta2} (sigma, N))` appears in
* @[FIPS203, Algorithm 14, K-PKE.Encrypt, L14]
*
**************************************************/
void ;
/* MLKEM_K == 2 */
/* !MLK_POLY_K_H */