-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathapi.h
More file actions
638 lines (607 loc) · 27.7 KB
/
api.h
File metadata and controls
638 lines (607 loc) · 27.7 KB
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
/*
* 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
*/
#ifndef MLK_NATIVE_API_H
#define MLK_NATIVE_API_H
/*
* Native arithmetic interface
*
* This header is primarily for documentation purposes.
* It should not be included by backend implementations.
*
* To ensure consistency with backends, the header will be
* included automatically after inclusion of the active
* backend, to ensure consistency of function signatures,
* and run sanity checks.
*/
#include "../cbmc.h"
#include "../common.h"
/* Backends must return MLK_NATIVE_FUNC_SUCCESS upon success. */
#define MLK_NATIVE_FUNC_SUCCESS (0)
/* Backends may return MLK_NATIVE_FUNC_FALLBACK to signal to the frontend that
* the target/parameters are unsupported; typically, this would be because of
* dependencies on CPU features not detected on the host CPU. In this case,
* the frontend falls back to the default C implementation. */
#define MLK_NATIVE_FUNC_FALLBACK (-1)
/* Absolute exclusive upper bound for the output of the inverse NTT
*
* NOTE: This is the same bound as in poly.h and has to be kept
* in sync. */
#define MLK_INVNTT_BOUND (8 * MLKEM_Q)
/* Absolute exclusive upper bound for the output of the forward NTT
*
* NOTE: This is the same bound as in poly.h and has to be kept
* in sync. */
#define MLK_NTT_BOUND (8 * MLKEM_Q)
/*
* This is the C<->native interface allowing for the drop-in of
* native code for performance critical arithmetic components of ML-KEM.
*
* A _backend_ is a specific implementation of (part of) this interface.
*
* To add a function to a backend, define MLK_USE_NATIVE_XXX and
* implement `static inline xxx(...)` in the profile header.
*
* The only exception is MLK_USE_NATIVE_NTT_CUSTOM_ORDER. This option can
* be set if there are native implementations for all of NTT, invNTT, and
* base multiplication, and allows the native implementation to use a
* custom order of polynomial coefficients in NTT domain -- the use of such
* custom order is not an implementation-detail since the public matrix
* is generated in NTT domain. In this case, a permutation function
* mlk_poly_permute_bitrev_to_custom() needs to be provided that permutes
* polynomials in NTT domain from bitreversed to the custom order.
*/
/*
* Those functions are meant to be trivial wrappers around the chosen native
* implementation. The are static inline to avoid unnecessary calls.
* The macro before each declaration controls whether a native
* implementation is present.
*/
#if defined(MLK_USE_NATIVE_NTT)
/**
* Compute the negacyclic number-theoretic transform (NTT) of a polynomial
* in place.
*
* The input polynomial is assumed to be in normal order. The output
* polynomial is in bitreversed order, or of a custom order if
* MLK_USE_NATIVE_NTT_CUSTOM_ORDER is set. See the documentation of
* MLK_USE_NATIVE_NTT_CUSTOM_ORDER for more information.
*
* @param[in,out] p Input/output polynomial.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_ntt_native(int16_t p[MLKEM_N])
__contract__(
requires(memory_no_alias(p, sizeof(int16_t) * MLKEM_N))
requires(array_abs_bound(p, 0, MLKEM_N, MLKEM_Q))
assigns(memory_slice(p, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLKEM_N, MLK_NTT_BOUND))
ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(p, 0, MLKEM_N, MLKEM_Q))
ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLKEM_N))
);
#endif /* MLK_USE_NATIVE_NTT */
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
/*
* This must only be set if NTT, invNTT, basemul, mulcache, and
* to/from byte stream conversions all have native implementations
* that are adapted to the custom order.
*/
#if !defined(MLK_USE_NATIVE_NTT) || !defined(MLK_USE_NATIVE_INTT) || \
!defined(MLK_USE_NATIVE_POLY_MULCACHE_COMPUTE) || \
!defined(MLK_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED) || \
!defined(MLK_USE_NATIVE_POLY_TOBYTES) || \
!defined(MLK_USE_NATIVE_POLY_FROMBYTES)
#error \
"Invalid native profile: MLK_USE_NATIVE_NTT_CUSTOM_ORDER can only be \
set if there are native implementations for NTT, invNTT, mulcache, basemul, \
and to/from bytes conversions."
#endif /* !MLK_USE_NATIVE_NTT || !MLK_USE_NATIVE_INTT || \
!MLK_USE_NATIVE_POLY_MULCACHE_COMPUTE || \
!MLK_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED || \
!MLK_USE_NATIVE_POLY_TOBYTES || !MLK_USE_NATIVE_POLY_FROMBYTES */
/**
* When MLK_USE_NATIVE_NTT_CUSTOM_ORDER is defined, convert a polynomial in
* NTT domain from bitreversed order to the custom order output by the
* native NTT.
*
* This must only be defined if there is native code for all of (a) NTT,
* (b) invNTT, (c) basemul, (d) mulcache.
*
* @param[in,out] p Input/output polynomial.
*/
static MLK_INLINE void mlk_poly_permute_bitrev_to_custom(int16_t p[MLKEM_N])
__contract__(
/* We don't specify that this should be a permutation, but only
* that it does not change the bound established at the end of mlk_gen_matrix. */
requires(memory_no_alias(p, sizeof(int16_t) * MLKEM_N))
requires(array_bound(p, 0, MLKEM_N, 0, MLKEM_Q))
assigns(memory_slice(p, sizeof(int16_t) * MLKEM_N))
ensures(array_bound(p, 0, MLKEM_N, 0, MLKEM_Q)));
#endif /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
#if defined(MLK_USE_NATIVE_INTT)
/**
* Compute the inverse negacyclic number-theoretic transform (NTT) of a
* polynomial in place.
*
* The input polynomial is in bitreversed order, or of a custom order if
* MLK_USE_NATIVE_NTT_CUSTOM_ORDER is set. See the documentation of
* MLK_USE_NATIVE_NTT_CUSTOM_ORDER for more information. The output
* polynomial is assumed to be in normal order.
*
* @param[in,out] p Input/output polynomial.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_intt_native(int16_t p[MLKEM_N])
__contract__(
requires(memory_no_alias(p, sizeof(int16_t) * MLKEM_N))
assigns(memory_slice(p, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLKEM_N, MLK_INVNTT_BOUND))
ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLKEM_N))
);
#endif /* MLK_USE_NATIVE_INTT */
#if defined(MLK_USE_NATIVE_POLY_REDUCE)
/**
* Apply modular reduction to all coefficients of a polynomial, mapping them
* to unsigned canonical representatives in [0,..,MLKEM_Q-1].
*
* @param[in,out] p Input/output polynomial.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_reduce_native(int16_t p[MLKEM_N])
__contract__(
requires(memory_no_alias(p, sizeof(int16_t) * MLKEM_N))
assigns(memory_slice(p, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_bound(p, 0, MLKEM_N, 0, MLKEM_Q))
ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLKEM_N))
);
#endif /* MLK_USE_NATIVE_POLY_REDUCE */
#if defined(MLK_USE_NATIVE_POLY_TOMONT)
/**
* In-place conversion of all coefficients of a polynomial from the normal
* domain to the Montgomery domain.
*
* @param[in,out] p Input/output polynomial.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_tomont_native(int16_t p[MLKEM_N])
__contract__(
requires(memory_no_alias(p, sizeof(int16_t) * MLKEM_N))
assigns(memory_slice(p, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLKEM_N, MLKEM_Q))
ensures((return_value == MLK_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLKEM_N))
);
#endif /* MLK_USE_NATIVE_POLY_TOMONT */
#if defined(MLK_USE_NATIVE_POLY_MULCACHE_COMPUTE)
/**
* Compute multiplication cache for a polynomial in NTT domain.
*
* The purpose of the multiplication cache is to cache repeated computations
* required during a base multiplication of polynomials in NTT domain. The
* structure of the multiplication-cache is implementation defined.
*
* @param[out] cache Multiplication cache.
* @param[in] mlk_poly Input polynomial. Must be in NTT domain and in
* bitreversed order, or of a custom order if
* MLK_USE_NATIVE_NTT_CUSTOM_ORDER is set. See the
* documentation of MLK_USE_NATIVE_NTT_CUSTOM_ORDER for
* more information.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_mulcache_compute_native(
int16_t cache[MLKEM_N / 2], const int16_t mlk_poly[MLKEM_N])
__contract__(
requires(memory_no_alias(cache, sizeof(int16_t) * (MLKEM_N / 2)))
requires(memory_no_alias(mlk_poly, sizeof(int16_t) * MLKEM_N))
assigns(memory_slice(cache, sizeof(int16_t) * (MLKEM_N / 2)))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(cache, 0, MLKEM_N/2, MLKEM_Q))
);
#endif /* MLK_USE_NATIVE_POLY_MULCACHE_COMPUTE */
#if defined(MLK_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED)
#if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || MLKEM_K == 2
/**
* Compute scalar product of length-2 polynomial vectors in NTT domain.
*
* @param[out] r Result of the scalar product. Again in NTT domain, of
* the same ordering as @p a and @p b.
* @param[in] a First polynomial vector operand. Must be in NTT
* domain and in bitreversed order, or of a custom order
* if MLK_USE_NATIVE_NTT_CUSTOM_ORDER is set. See the
* documentation of MLK_USE_NATIVE_NTT_CUSTOM_ORDER for
* more information.
* @param[in] b Second polynomial vector operand. As for @p a.
* @param[in] b_cache Multiplication-cache for @p b.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_polyvec_basemul_acc_montgomery_cached_k2_native(
int16_t r[MLKEM_N], const int16_t a[2 * MLKEM_N],
const int16_t b[2 * MLKEM_N], const int16_t b_cache[2 * (MLKEM_N / 2)])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, sizeof(int16_t) * 2 * MLKEM_N))
requires(memory_no_alias(b, sizeof(int16_t) * 2 * MLKEM_N))
requires(memory_no_alias(b_cache, sizeof(int16_t) * 2 * (MLKEM_N / 2)))
requires(array_bound(a, 0, 2 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
);
#endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_K == 2 */
#if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || MLKEM_K == 3
/**
* Compute scalar product of length-3 polynomial vectors in NTT domain.
*
* @param[out] r Result of the scalar product. Again in NTT domain, of
* the same ordering as @p a and @p b.
* @param[in] a First polynomial vector operand. Must be in NTT
* domain and in bitreversed order, or of a custom order
* if MLK_USE_NATIVE_NTT_CUSTOM_ORDER is set. See the
* documentation of MLK_USE_NATIVE_NTT_CUSTOM_ORDER for
* more information.
* @param[in] b Second polynomial vector operand. As for @p a.
* @param[in] b_cache Multiplication-cache for @p b.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_polyvec_basemul_acc_montgomery_cached_k3_native(
int16_t r[MLKEM_N], const int16_t a[3 * MLKEM_N],
const int16_t b[3 * MLKEM_N], const int16_t b_cache[3 * (MLKEM_N / 2)])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, sizeof(int16_t) * 3 * MLKEM_N))
requires(memory_no_alias(b, sizeof(int16_t) * 3 * MLKEM_N))
requires(memory_no_alias(b_cache, sizeof(int16_t) * 3 * (MLKEM_N / 2)))
requires(array_bound(a, 0, 3 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
);
#endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_K == 3 */
#if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || MLKEM_K == 4
/**
* Compute scalar product of length-4 polynomial vectors in NTT domain.
*
* @param[out] r Result of the scalar product. Again in NTT domain, of
* the same ordering as @p a and @p b.
* @param[in] a First polynomial vector operand. Must be in NTT
* domain and in bitreversed order, or of a custom order
* if MLK_USE_NATIVE_NTT_CUSTOM_ORDER is set. See the
* documentation of MLK_USE_NATIVE_NTT_CUSTOM_ORDER for
* more information.
* @param[in] b Second polynomial vector operand. As for @p a.
* @param[in] b_cache Multiplication-cache for @p b.
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_polyvec_basemul_acc_montgomery_cached_k4_native(
int16_t r[MLKEM_N], const int16_t a[4 * MLKEM_N],
const int16_t b[4 * MLKEM_N], const int16_t b_cache[4 * (MLKEM_N / 2)])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, sizeof(int16_t) * 4 * MLKEM_N))
requires(memory_no_alias(b, sizeof(int16_t) * 4 * MLKEM_N))
requires(memory_no_alias(b_cache, sizeof(int16_t) * 4 * (MLKEM_N / 2)))
requires(array_bound(a, 0, 4 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_FALLBACK || return_value == MLK_NATIVE_FUNC_SUCCESS)
);
#endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_K == 4 */
#endif /* MLK_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED */
#if defined(MLK_USE_NATIVE_POLY_TOBYTES)
/**
* Serialization of a polynomial with unsigned canonical coefficients.
*
* @spec{Implements ByteEncode_12 from @[FIPS203, Algorithm 5].}
*
* @param[out] r Output byte array (of MLKEM_POLYBYTES bytes).
* @param[in] a Input polynomial, with each coefficient in [0,..,MLKEM_Q-1].
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_tobytes_native(uint8_t r[MLKEM_POLYBYTES],
const int16_t a[MLKEM_N])
__contract__(
requires(memory_no_alias(r, MLKEM_POLYBYTES))
requires(memory_no_alias(a, sizeof(int16_t) * MLKEM_N))
requires(array_bound(a, 0, MLKEM_N, 0, MLKEM_Q))
assigns(memory_slice(r, MLKEM_POLYBYTES))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK)
);
#endif /* MLK_USE_NATIVE_POLY_TOBYTES */
#if defined(MLK_USE_NATIVE_POLY_FROMBYTES)
/**
* Deserialization of a polynomial.
*
* @spec{Implements ByteDecode_12 from @[FIPS203, Algorithm 6].}
*
* @param[out] a Output polynomial in NTT domain.
* @param[in] r Input byte array (of MLKEM_POLYBYTES bytes).
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_frombytes_native(
int16_t a[MLKEM_N], const uint8_t r[MLKEM_POLYBYTES])
__contract__(
requires(memory_no_alias(r, MLKEM_POLYBYTES))
requires(memory_no_alias(a, sizeof(int16_t) * MLKEM_N))
assigns(memory_slice(a, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_bound(a, 0, MLKEM_N, 0, MLKEM_UINT12_LIMIT))
);
#endif /* MLK_USE_NATIVE_POLY_FROMBYTES */
#if defined(MLK_USE_NATIVE_REJ_UNIFORM)
/**
* Run rejection sampling on uniformly random bytes to generate uniformly random
* integers mod MLKEM_Q, represented in [0,..,MLKEM_Q-1].
*
* @param[out] r Output buffer.
* @param len Requested number of 16-bit integers (uniform mod MLKEM_Q).
* @param[in] buf Input buffer (assumed to be uniform random bytes).
* @param buflen Length of input buffer in bytes.
*
* @retval MLK_NATIVE_FUNC_FALLBACK Native implementation does not support
* the input lengths.
* @retval other Non-negative number of sampled 16-bit
* integers (at most @p len).
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_rej_uniform_native(int16_t *r, unsigned len,
const uint8_t *buf,
unsigned buflen)
__contract__(
requires(len <= 4096 && buflen <= 4096 && buflen % 3 == 0)
requires(memory_no_alias(r, sizeof(int16_t) * len))
requires(memory_no_alias(buf, buflen))
assigns(memory_slice(r, sizeof(int16_t) * len))
ensures(return_value != MLK_NATIVE_FUNC_FALLBACK
==> (0 <= return_value && return_value <= len))
ensures(return_value != MLK_NATIVE_FUNC_FALLBACK
==> array_bound(r, 0, (unsigned) return_value, 0, MLKEM_Q))
);
#endif /* MLK_USE_NATIVE_REJ_UNIFORM */
#if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || (MLKEM_K == 2 || MLKEM_K == 3)
#if defined(MLK_USE_NATIVE_POLY_COMPRESS_D4)
/**
* Compression (4 bits) and subsequent serialization of a polynomial.
*
* @spec{Compress_4 from @[FIPS203, Eq (4.7)].}
*
* @param[out] r Output byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D4
* bytes).
* @param[in] a Input polynomial. Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_compress_d4_native(
uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D4], const int16_t a[MLKEM_N])
__contract__(
requires(memory_no_alias(r, MLKEM_POLYCOMPRESSEDBYTES_D4))
requires(memory_no_alias(a, sizeof(int16_t) * MLKEM_N))
requires(array_bound(a, 0, MLKEM_N, 0, MLKEM_Q))
assigns(memory_slice(r, MLKEM_POLYCOMPRESSEDBYTES_D4))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK));
#endif /* MLK_USE_NATIVE_POLY_COMPRESS_D4 */
#if defined(MLK_USE_NATIVE_POLY_COMPRESS_D10)
/**
* Compression (10 bits) and subsequent serialization of a polynomial.
*
* @spec{Compress_10 from @[FIPS203, Eq (4.7)].}
*
* @param[out] r Output byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D10
* bytes).
* @param[in] a Input polynomial. Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_compress_d10_native(
uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D10], const int16_t a[MLKEM_N])
__contract__(
requires(memory_no_alias(r, MLKEM_POLYCOMPRESSEDBYTES_D10))
requires(memory_no_alias(a, sizeof(int16_t) * MLKEM_N))
requires(array_bound(a, 0, MLKEM_N, 0, MLKEM_Q))
assigns(memory_slice(r, MLKEM_POLYCOMPRESSEDBYTES_D10))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK));
#endif /* MLK_USE_NATIVE_POLY_COMPRESS_D10 */
#if defined(MLK_USE_NATIVE_POLY_DECOMPRESS_D4)
/**
* De-serialization and subsequent decompression (4 bits) of a polynomial;
* approximate inverse of mlk_poly_compress_d4.
*
* @spec{Decompress_4 from @[FIPS203, Eq (4.8)].}
*
* Upon return, the coefficients of the output polynomial are
* unsigned-canonical (non-negative and smaller than MLKEM_Q).
*
* @param[out] r Output polynomial.
* @param[in] a Input byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D4
* bytes).
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_decompress_d4_native(
int16_t r[MLKEM_N], const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_D4])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, MLKEM_POLYCOMPRESSEDBYTES_D4))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLKEM_N, 0, MLKEM_Q)));
#endif /* MLK_USE_NATIVE_POLY_DECOMPRESS_D4 */
#if defined(MLK_USE_NATIVE_POLY_DECOMPRESS_D10)
/**
* De-serialization and subsequent decompression (10 bits) of a polynomial;
* approximate inverse of mlk_poly_compress_d10.
*
* @spec{Decompress_10 from @[FIPS203, Eq (4.8)].}
*
* Upon return, the coefficients of the output polynomial are
* unsigned-canonical (non-negative and smaller than MLKEM_Q).
*
* @param[out] r Output polynomial.
* @param[in] a Input byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D10
* bytes).
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_decompress_d10_native(
int16_t r[MLKEM_N], const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_D10])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, MLKEM_POLYCOMPRESSEDBYTES_D10))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLKEM_N, 0, MLKEM_Q)));
#endif /* MLK_USE_NATIVE_POLY_DECOMPRESS_D10 */
#endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_K == 2 || MLKEM_K == 3 */
#if defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || MLKEM_K == 4
#if defined(MLK_USE_NATIVE_POLY_COMPRESS_D5)
/**
* Compression (5 bits) and subsequent serialization of a polynomial.
*
* @spec{Compress_5 from @[FIPS203, Eq (4.7)].}
*
* @param[out] r Output byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D5
* bytes).
* @param[in] a Input polynomial. Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_compress_d5_native(
uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D5], const int16_t a[MLKEM_N])
__contract__(
requires(memory_no_alias(r, MLKEM_POLYCOMPRESSEDBYTES_D5))
requires(memory_no_alias(a, sizeof(int16_t) * MLKEM_N))
requires(array_bound(a, 0, MLKEM_N, 0, MLKEM_Q))
assigns(memory_slice(r, MLKEM_POLYCOMPRESSEDBYTES_D5))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK));
#endif /* MLK_USE_NATIVE_POLY_COMPRESS_D5 */
#if defined(MLK_USE_NATIVE_POLY_COMPRESS_D11)
/**
* Compression (11 bits) and subsequent serialization of a polynomial.
*
* @spec{Compress_11 from @[FIPS203, Eq (4.7)].}
*
* @param[out] r Output byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D11
* bytes).
* @param[in] a Input polynomial. Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_compress_d11_native(
uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D11], const int16_t a[MLKEM_N])
__contract__(
requires(memory_no_alias(r, MLKEM_POLYCOMPRESSEDBYTES_D11))
requires(memory_no_alias(a, sizeof(int16_t) * MLKEM_N))
requires(array_bound(a, 0, MLKEM_N, 0, MLKEM_Q))
assigns(memory_slice(r, MLKEM_POLYCOMPRESSEDBYTES_D11))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK));
#endif /* MLK_USE_NATIVE_POLY_COMPRESS_D11 */
#if defined(MLK_USE_NATIVE_POLY_DECOMPRESS_D5)
/**
* De-serialization and subsequent decompression (5 bits) of a polynomial;
* approximate inverse of mlk_poly_compress_d5.
*
* @spec{Decompress_5 from @[FIPS203, Eq (4.8)].}
*
* Upon return, the coefficients of the output polynomial are
* unsigned-canonical (non-negative and smaller than MLKEM_Q).
*
* @param[out] r Output polynomial.
* @param[in] a Input byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D5
* bytes).
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_decompress_d5_native(
int16_t r[MLKEM_N], const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_D5])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, MLKEM_POLYCOMPRESSEDBYTES_D5))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLKEM_N, 0, MLKEM_Q)));
#endif /* MLK_USE_NATIVE_POLY_DECOMPRESS_D5 */
#if defined(MLK_USE_NATIVE_POLY_DECOMPRESS_D11)
/**
* De-serialization and subsequent decompression (11 bits) of a polynomial;
* approximate inverse of mlk_poly_compress_d11.
*
* @spec{Decompress_11 from @[FIPS203, Eq (4.8)].}
*
* Upon return, the coefficients of the output polynomial are
* unsigned-canonical (non-negative and smaller than MLKEM_Q).
*
* @param[out] r Output polynomial.
* @param[in] a Input byte array (of length MLKEM_POLYCOMPRESSEDBYTES_D11
* bytes).
*
* @retval MLK_NATIVE_FUNC_SUCCESS Operation succeeded.
* @retval MLK_NATIVE_FUNC_FALLBACK Backend declined; caller should fall back.
*/
MLK_MUST_CHECK_RETURN_VALUE
static MLK_INLINE int mlk_poly_decompress_d11_native(
int16_t r[MLKEM_N], const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_D11])
__contract__(
requires(memory_no_alias(r, sizeof(int16_t) * MLKEM_N))
requires(memory_no_alias(a, MLKEM_POLYCOMPRESSEDBYTES_D11))
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
ensures(return_value == MLK_NATIVE_FUNC_SUCCESS || return_value == MLK_NATIVE_FUNC_FALLBACK)
ensures((return_value == MLK_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLKEM_N, 0, MLKEM_Q)));
#endif /* MLK_USE_NATIVE_POLY_DECOMPRESS_D11 */
#endif /* MLK_CONFIG_MULTILEVEL_WITH_SHARED || MLKEM_K == 4 */
#endif /* !MLK_NATIVE_API_H */