-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathapi.h
More file actions
649 lines (620 loc) · 30.9 KB
/
api.h
File metadata and controls
649 lines (620 loc) · 30.9 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
639
640
641
642
643
644
645
646
647
648
649
/*
* Copyright (c) The mlkem-native project authors
* Copyright (c) The mldsa-native project authors
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
*/
#ifndef MLD_NATIVE_API_H
#define MLD_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 MLD_NATIVE_FUNC_SUCCESS upon success. */
#define MLD_NATIVE_FUNC_SUCCESS (0)
/* Backends may return MLD_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.
*
* IMPORTANT: Backend implementations must ensure that the decision of whether
* to fallback (return MLD_NATIVE_FUNC_FALLBACK) or not must never depend on
* the input data itself. Fallback decisions may only depend on system
* capabilities (e.g., CPU features) and, where present, length information.
* This requirement applies to all backend functions to maintain constant-time
* properties.
*/
#define MLD_NATIVE_FUNC_FALLBACK (-1)
/* Bound on absolute value of coefficients after NTT.
*
* NOTE: This is the same bound as in poly.h and has to be kept
* in sync. */
#define MLD_NTT_BOUND (9 * MLDSA_Q)
/* 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 MLD_INTT_BOUND MLDSA_Q
/* Absolute bound for range of mld_reduce32()
*
* NOTE: This is the same bound as in reduce.h and has to be kept
* in sync. */
/* check-magic: 6283009 == (MLD_REDUCE32_DOMAIN_MAX - 255 * MLDSA_Q + 1) */
#define MLD_REDUCE32_RANGE_MAX 6283009
/*
* This is the C<->native interface allowing for the drop-in of
* native code for performance critical arithmetic components of ML-DSA.
*
* A _backend_ is a specific implementation of (part of) this interface.
*
* To add a function to a backend, define MLD_USE_NATIVE_XXX and
* implement `static inline xxx(...)` in the profile header.
*/
/*
* 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(MLD_USE_NATIVE_NTT)
/*************************************************
* Name: mld_ntt_native
*
* Description: Computes 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.
*
* Arguments: - int32_t p[MLDSA_N]: pointer to in/output polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_ntt_native(int32_t p[MLDSA_N])
__contract__(
requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
requires(array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLDSA_N, MLD_NTT_BOUND))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLDSA_N))
);
#endif /* MLD_USE_NATIVE_NTT */
#if defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
/*
* This must only be set if NTT and INTT have native implementations
* that are adapted to the custom order.
*/
#if !defined(MLD_USE_NATIVE_NTT) || !defined(MLD_USE_NATIVE_INTT)
#error \
"Invalid native profile: MLD_USE_NATIVE_NTT_CUSTOM_ORDER can only be \
set if there are native implementations for NTT and INTT."
#endif
/*************************************************
* Name: mlD_poly_permute_bitrev_to_custom
*
* Description: When MLD_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
* both the NTT and INTT.
*
* Arguments: - int32_t p[MLDSA_N]: pointer to in/output polynomial
*
**************************************************/
static MLD_INLINE void mld_poly_permute_bitrev_to_custom(int32_t p[MLDSA_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
* mld_polyvec_matrix_expand.
*/
requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
requires(array_bound(p, 0, MLDSA_N, 0, MLDSA_Q))
assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
ensures(array_bound(p, 0, MLDSA_N, 0, MLDSA_Q)));
#endif /* MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
#if defined(MLD_USE_NATIVE_INTT)
/*************************************************
* Name: mld_intt_native
*
* Description: Computes inverse of negacyclic number-theoretic transform
*(NTT) of a polynomial in place.
*
* The input polynomial is in bitreversed order.
* The output polynomial is assumed to be in normal order.
*
* Arguments: - uint32_t p[MLDSA_N]: pointer to in/output polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_intt_native(int32_t p[MLDSA_N])
__contract__(
requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
requires(array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(p, 0, MLDSA_N, MLD_INTT_BOUND))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(p, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(p, MLDSA_N))
);
#endif /* MLD_USE_NATIVE_INTT */
#if defined(MLD_USE_NATIVE_REJ_UNIFORM)
/*************************************************
* Name: mld_rej_uniform_native
*
* Description: Run rejection sampling on uniform random bytes to generate
* uniform random integers in [0, MLDSA_Q-1]
*
* Arguments: - int32_t *r: pointer to output buffer
* - unsigned len: requested number of 32-bit integers
* (uniform mod q).
* - const uint8_t *buf: pointer to input buffer
* (assumed to be uniform random bytes)
* - unsigned buflen: length of input buffer in bytes.
*
* Return -1 if the native implementation does not support the input
* lengths. Otherwise, returns non-negative number of sampled 32-bit integers
* (at most len).
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_rej_uniform_native(int32_t *r, unsigned len,
const uint8_t *buf,
unsigned buflen)
__contract__(
requires(len <= MLDSA_N)
requires(buflen <= ( 5 * 168) && buflen % 3 == 0)
requires(memory_no_alias(r, sizeof(int32_t) * len))
requires(memory_no_alias(buf, buflen))
assigns(memory_slice(r, sizeof(int32_t) * len))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || (0 <= return_value && return_value <= len))
ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==> array_bound(r, 0, (unsigned) return_value, 0, MLDSA_Q))
);
#endif /* MLD_USE_NATIVE_REJ_UNIFORM */
#if defined(MLD_USE_NATIVE_REJ_UNIFORM_ETA2)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_ETA == 2
/*************************************************
* Name: mld_rej_uniform_eta2_native
*
* Description: Run rejection sampling on uniform random bytes to generate
* uniform random integers in [-2,+2].
*
* Arguments: - int32_t *r: pointer to output buffer
* - unsigned len: requested number of 32-bit integers
* (uniform in [-2, +2]).
* - const uint8_t *buf: pointer to input buffer
* (assumed to be uniform random bytes)
* - unsigned buflen: length of input buffer in bytes.
*
* Return -1 if the native implementation does not support the input
*lengths. Otherwise, returns non-negative number of sampled 32-bit integers
*(at most len).
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_rej_uniform_eta2_native(int32_t *r, unsigned len,
const uint8_t *buf,
unsigned buflen)
__contract__(
requires(len <= MLDSA_N)
requires(buflen <= (2 * 136))
requires(memory_no_alias(r, sizeof(int32_t) * len))
requires(memory_no_alias(buf, buflen))
assigns(memory_slice(r, sizeof(int32_t) * len))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || (0 <= return_value && return_value <= len))
ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==> (array_abs_bound(r, 0, return_value, MLDSA_ETA + 1)))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_ETA == 2 */
#endif /* MLD_USE_NATIVE_REJ_UNIFORM_ETA2 */
#if defined(MLD_USE_NATIVE_REJ_UNIFORM_ETA4)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_ETA == 4
/*************************************************
* Name: mld_rej_uniform_eta4_native
*
* Description: Run rejection sampling on uniform random bytes to generate
* uniform random integers in [-4,+4].
*
* Arguments: - int32_t *r: pointer to output buffer
* - unsigned len: requested number of 32-bit integers
* (uniform in [-4, +4]).
* - const uint8_t *buf: pointer to input buffer
* (assumed to be uniform random bytes)
* - unsigned buflen: length of input buffer in bytes.
*
* Return -1 if the native implementation does not support the input
*lengths. Otherwise, returns non-negative number of sampled 32-bit integers
*(at most len).
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_rej_uniform_eta4_native(int32_t *r, unsigned len,
const uint8_t *buf,
unsigned buflen)
__contract__(
requires(len <= MLDSA_N)
requires(buflen <= (2 * 136))
requires(memory_no_alias(r, sizeof(int32_t) * len))
requires(memory_no_alias(buf, buflen))
assigns(memory_slice(r, sizeof(int32_t) * len))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || (0 <= return_value && return_value <= len))
ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==> (array_abs_bound(r, 0, return_value, MLDSA_ETA + 1)))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_ETA == 4 */
#endif /* MLD_USE_NATIVE_REJ_UNIFORM_ETA4 */
#if defined(MLD_USE_NATIVE_POLY_DECOMPOSE_32)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
(MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
/*************************************************
* Name: mld_poly_decompose_32_native
*
* Description: Native implementation of poly_decompose for GAMMA2 = (Q-1)/32.
* For all coefficients c of the input polynomial,
* compute high and low bits c0, c1 such
* c mod MLDSA_Q = c1*(2*GAMMA2) + c0
* with -(2*GAMMA2)/2 < c0 <= (2*GAMMA2)/2 except
* c1 = (MLDSA_Q-1)/(2*GAMMA2) where we set
* c1 = 0 and -(2*GAMMA2)/2 <= c0 = c mod MLDSA_Q - MLDSA_Q < 0.
* Assumes coefficients to be standard representatives.
*
* Arguments: - int32_t *a1: output polynomial with coefficients c1
* - int32_t *a0: input/output polynomial.
* Output has coefficients c0
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_decompose_32_native(int32_t *a1, int32_t *a0)
__contract__(
requires(memory_no_alias(a1, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(a0, sizeof(int32_t) * MLDSA_N))
requires(array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
assigns(memory_slice(a1, sizeof(int32_t) * MLDSA_N))
assigns(memory_slice(a0, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a1, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(a0, 0, MLDSA_N, MLDSA_GAMMA2+1))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a0, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
|| MLD_CONFIG_PARAMETER_SET == 87 */
#endif /* MLD_USE_NATIVE_POLY_DECOMPOSE_32 */
#if defined(MLD_USE_NATIVE_POLY_DECOMPOSE_88)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
/*************************************************
* Name: mld_poly_decompose_88_native
*
* Description: Native implementation of poly_decompose for GAMMA2 = (Q-1)/88.
* For all coefficients c of the input polynomial,
* compute high and low bits c0, c1 such
* c mod MLDSA_Q = c1*(2*GAMMA2) + c0
* with -(2*GAMMA2)/2 < c0 <= (2*GAMMA2)/2 except
* c1 = (MLDSA_Q-1)/(2*GAMMA2) where we set
* c1 = 0 and -(2*GAMMA2)/2 <= c0 = c mod MLDSA_Q - MLDSA_Q < 0.
* Assumes coefficients to be standard representatives.
*
* Arguments: - int32_t *a1: output polynomial with coefficients c1
* - int32_t *a0: output polynomial with coefficients c0.
* Output has coefficients c0
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_decompose_88_native(int32_t *a1, int32_t *a0)
__contract__(
requires(memory_no_alias(a1, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(a0, sizeof(int32_t) * MLDSA_N))
requires(array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
assigns(memory_slice(a1, sizeof(int32_t) * MLDSA_N))
assigns(memory_slice(a0, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a1, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(a0, 0, MLDSA_N, MLDSA_GAMMA2+1))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_bound(a0, 0, MLDSA_N, 0, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a0, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
*/
#endif /* MLD_USE_NATIVE_POLY_DECOMPOSE_88 */
#if defined(MLD_USE_NATIVE_POLY_CADDQ)
/*************************************************
* Name: mld_poly_caddq_native
*
* Description: For all coefficients of in/out polynomial add Q if
* coefficient is negative.
*
* Arguments: - int32_t *a: pointer to input/output polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_caddq_native(int32_t a[MLDSA_N])
__contract__(
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
requires(array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))
assigns(memory_slice(a, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(a, 0, MLDSA_N, 0, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(a, MLDSA_N))
);
#endif /* MLD_USE_NATIVE_POLY_CADDQ */
#if defined(MLD_USE_NATIVE_POLY_USE_HINT_32)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
(MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
/*************************************************
* Name: mld_poly_use_hint_32_native
*
* Description: Native implementation of poly_use_hint for GAMMA2 = (Q-1)/32.
* Use hint polynomial to correct the high bits of a polynomial.
*
* Arguments: - int32_t *b: pointer to output polynomial with corrected high
* bits
* - const int32_t *a: pointer to input polynomial
* - const int32_t *h: pointer to input hint polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_use_hint_32_native(int32_t *b, const int32_t *a,
const int32_t *h)
__contract__(
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(b, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(h, sizeof(int32_t) * MLDSA_N))
requires(array_bound(a, 0, MLDSA_N, 0, MLDSA_Q))
requires(array_bound(h, 0, MLDSA_N, 0, 2))
assigns(memory_slice(b, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(b, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(b, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
|| MLD_CONFIG_PARAMETER_SET == 87 */
#endif /* MLD_USE_NATIVE_POLY_USE_HINT_32 */
#if defined(MLD_USE_NATIVE_POLY_USE_HINT_88)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
/*************************************************
* Name: mld_poly_use_hint_88_native
*
* Description: Native implementation of poly_use_hint for GAMMA2 = (Q-1)/88.
* Use hint polynomial to correct the high bits of a polynomial.
*
* Arguments: - int32_t *b: pointer to output polynomial with corrected high
* bits
* - const int32_t *a: pointer to input polynomial
* - const int32_t *h: pointer to input hint polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_use_hint_88_native(int32_t *b, const int32_t *a,
const int32_t *h)
__contract__(
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(b, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(h, sizeof(int32_t) * MLDSA_N))
requires(array_bound(a, 0, MLDSA_N, 0, MLDSA_Q))
requires(array_bound(h, 0, MLDSA_N, 0, 2))
assigns(memory_slice(b, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(b, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2)))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(b, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
*/
#endif /* MLD_USE_NATIVE_POLY_USE_HINT_88 */
#if defined(MLD_USE_NATIVE_POLY_CHKNORM)
/*************************************************
* Name: mld_poly_chknorm_native
*
* Description: Check infinity norm of polynomial against given bound.
* Assumes input coefficients were reduced by mld_reduce32().
*
* Arguments: - const int32_t *a: pointer to polynomial
* - int32_t B: norm bound, which must be in the range
* 0 .. MLDSA_Q - MLD_REDUCE32_RANGE_MAX inclusive.
*
* Returns MLD_NATIVE_FUNC_FALLBACK (-1) if the target CPU cannot
* support a native implementation of this function.
*
* If the target CPU can support this function, then
* Returns MLD_NATIVE_FUNC_SUCCESS (0) if the infinity norm is strictly
* smaller than B
* Returns 1 otherwise
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_chknorm_native(const int32_t *a, int32_t B)
__contract__(
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
requires(0 <= B && B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX)
requires(array_bound(a, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == 0 ||
return_value == 1)
ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==>
((return_value == 0) == array_abs_bound(a, 0, MLDSA_N, B)))
);
#endif /* MLD_USE_NATIVE_POLY_CHKNORM */
#if defined(MLD_USE_NATIVE_POLYZ_UNPACK_17)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLD_CONFIG_PARAMETER_SET == 44
/*************************************************
* Name: mld_polyz_unpack_17_native
*
* Description: Native implementation of polyz_unpack for GAMMA1 = 2^17.
* Unpack polynomial z with coefficients
* in [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
*
* Arguments: - int32_t *r: pointer to output polynomial
* - const uint8_t *a: byte array with bit-packed polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_polyz_unpack_17_native(int32_t *r, const uint8_t *a)
__contract__(
requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(a, MLDSA_POLYZ_PACKEDBYTES))
assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(r, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 44 \
*/
#endif /* MLD_USE_NATIVE_POLYZ_UNPACK_17 */
#if defined(MLD_USE_NATIVE_POLYZ_UNPACK_19)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || \
(MLD_CONFIG_PARAMETER_SET == 65 || MLD_CONFIG_PARAMETER_SET == 87)
/*************************************************
* Name: mld_polyz_unpack_19_native
*
* Description: Native implementation of polyz_unpack for GAMMA1 = 2^19.
* Unpack polynomial z with coefficients
* in [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
*
* Arguments: - int32_t *r: pointer to output polynomial
* - const uint8_t *a: byte array with bit-packed polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_polyz_unpack_19_native(int32_t *r, const uint8_t *a)
__contract__(
requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(a, MLDSA_POLYZ_PACKEDBYTES))
assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_bound(r, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(r, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLD_CONFIG_PARAMETER_SET == 65 \
|| MLD_CONFIG_PARAMETER_SET == 87 */
#endif /* MLD_USE_NATIVE_POLYZ_UNPACK_19 */
#if defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY)
/*************************************************
* Name: mld_poly_pointwise_montgomery_native
*
* Description: Pointwise multiplication of polynomials in NTT domain
* with Montgomery reduction.
*
* Computes c[i] = a[i] * b[i] * R^(-1) mod q for all i,
* where R = 2^32.
*
* Arguments: - int32_t c[MLDSA_N]: output polynomial
* - const int32_t a[MLDSA_N]: first input polynomial
* - const int32_t b[MLDSA_N]: second input polynomial
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_poly_pointwise_montgomery_native(
int32_t c[MLDSA_N], const int32_t a[MLDSA_N], const int32_t b[MLDSA_N])
__contract__(
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(b, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(c, sizeof(int32_t) * MLDSA_N))
requires(array_abs_bound(a, 0, MLDSA_N, MLD_NTT_BOUND))
requires(array_abs_bound(b, 0, MLDSA_N, MLD_NTT_BOUND))
assigns(memory_slice(c, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(c, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(a, 0, MLDSA_N, MLD_NTT_BOUND))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_abs_bound(b, 0, MLDSA_N, MLD_NTT_BOUND))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(c, MLDSA_N))
);
#endif /* MLD_USE_NATIVE_POINTWISE_MONTGOMERY */
#if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_L == 4
/*************************************************
* Name: mld_polyvecl_pointwise_acc_montgomery_l4_native
*
* Description: Native implementation of polyvecl_pointwise_acc_montgomery for
* MLDSA_L = 4.
* Pointwise multiply vectors of polynomials of length MLDSA_L,
* multiply resulting vector by 2^{-32} and add (accumulate)
* polynomials in it.
* Input/output vectors are in NTT domain representation.
*
* Arguments: - int32_t w[MLDSA_N]: output polynomial
* - const int32_t u[MLDSA_L][MLDSA_N]: first input vector
* - const int32_t v[MLDSA_L][MLDSA_N]: second input vector
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_polyvecl_pointwise_acc_montgomery_l4_native(
int32_t w[MLDSA_N], const int32_t u[4][MLDSA_N],
const int32_t v[4][MLDSA_N])
__contract__(
requires(memory_no_alias(w, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(u, sizeof(int32_t) * 4 * MLDSA_N))
requires(memory_no_alias(v, sizeof(int32_t) * 4 * MLDSA_N))
requires(forall(l0, 0, 4,
array_bound(u[l0], 0, MLDSA_N, 0, MLDSA_Q)))
requires(forall(l1, 0, 4,
array_abs_bound(v[l1], 0, MLDSA_N, MLD_NTT_BOUND)))
assigns(memory_slice(w, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(w, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(w, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_L == 4 */
#endif /* MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4 */
#if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_L == 5
/*************************************************
* Name: mld_polyvecl_pointwise_acc_montgomery_l5_native
*
* Description: Native implementation of polyvecl_pointwise_acc_montgomery for
* MLDSA_L = 5.
* Pointwise multiply vectors of polynomials of length MLDSA_L,
* multiply resulting vector by 2^{-32} and add (accumulate)
* polynomials in it.
* Input/output vectors are in NTT domain representation.
*
* Arguments: - int32_t w[MLDSA_N]: output polynomial
* - const int32_t u[MLDSA_L][MLDSA_N]: first input vector
* - const int32_t v[MLDSA_L][MLDSA_N]: second input vector
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_polyvecl_pointwise_acc_montgomery_l5_native(
int32_t w[MLDSA_N], const int32_t u[5][MLDSA_N],
const int32_t v[5][MLDSA_N])
__contract__(
requires(memory_no_alias(w, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(u, sizeof(int32_t) * 5 * MLDSA_N))
requires(memory_no_alias(v, sizeof(int32_t) * 5 * MLDSA_N))
requires(forall(l0, 0, 5,
array_bound(u[l0], 0, MLDSA_N, 0, MLDSA_Q)))
requires(forall(l1, 0, 5,
array_abs_bound(v[l1], 0, MLDSA_N, MLD_NTT_BOUND)))
assigns(memory_slice(w, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(w, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(w, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_L == 5 */
#endif /* MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5 */
#if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7)
#if defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED) || MLDSA_L == 7
/*************************************************
* Name: mld_polyvecl_pointwise_acc_montgomery_l7_native
*
* Description: Native implementation of polyvecl_pointwise_acc_montgomery for
* MLDSA_L = 7.
* Pointwise multiply vectors of polynomials of length MLDSA_L,
* multiply resulting vector by 2^{-32} and add (accumulate)
* polynomials in it.
* Input/output vectors are in NTT domain representation.
*
* Arguments: - int32_t w[MLDSA_N]: output polynomial
* - const int32_t u[MLDSA_L][MLDSA_N]: first input vector
* - const int32_t v[MLDSA_L][MLDSA_N]: second input vector
**************************************************/
MLD_MUST_CHECK_RETURN_VALUE
static MLD_INLINE int mld_polyvecl_pointwise_acc_montgomery_l7_native(
int32_t w[MLDSA_N], const int32_t u[7][MLDSA_N],
const int32_t v[7][MLDSA_N])
__contract__(
requires(memory_no_alias(w, sizeof(int32_t) * MLDSA_N))
requires(memory_no_alias(u, sizeof(int32_t) * 7 * MLDSA_N))
requires(memory_no_alias(v, sizeof(int32_t) * 7 * MLDSA_N))
requires(forall(l0, 0, 7,
array_bound(u[l0], 0, MLDSA_N, 0, MLDSA_Q)))
requires(forall(l1, 0, 7,
array_abs_bound(v[l1], 0, MLDSA_N, MLD_NTT_BOUND)))
assigns(memory_slice(w, sizeof(int32_t) * MLDSA_N))
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
ensures((return_value == MLD_NATIVE_FUNC_SUCCESS) ==> array_abs_bound(w, 0, MLDSA_N, MLDSA_Q))
ensures((return_value == MLD_NATIVE_FUNC_FALLBACK) ==> array_unchanged(w, MLDSA_N))
);
#endif /* MLD_CONFIG_MULTILEVEL_WITH_SHARED || MLDSA_L == 7 */
#endif /* MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7 */
#endif /* !MLD_NATIVE_API_H */