-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpoly.h
773 lines (734 loc) · 28.3 KB
/
poly.h
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef POLY_H
#define POLY_H
#include <stddef.h>
#include <stdint.h>
#include "cbmc.h"
#include "params.h"
#include "reduce.h"
#include "verify.h"
/* Absolute exclusive upper bound for the output of the inverse NTT */
#define INVNTT_BOUND (8 * MLKEM_Q)
/* Absolute exclusive upper bound for the output of the forward NTT */
#define NTT_BOUND (8 * MLKEM_Q)
/*
* Elements of R_q = Z_q[X]/(X^n + 1). Represents polynomial
* coeffs[0] + X*coeffs[1] + X^2*coeffs[2] + ... + X^{n-1}*coeffs[n-1]
*/
typedef struct
{
int16_t coeffs[MLKEM_N];
} ALIGN poly;
/*
* INTERNAL presentation of precomputed data speeding up
* the base multiplication of two polynomials in NTT domain.
*/
/*
* REF-CHANGE: This structure does not exist in the reference
* implementation.
*/
typedef struct
{
int16_t coeffs[MLKEM_N >> 1];
} poly_mulcache;
/************************************************************
* Name: scalar_compress_d1
*
* Description: Computes round(u * 2 / q)
*
* Implements Compress_d from FIPS203, Eq (4.7),
* for d = 1.
*
* Arguments: - u: Unsigned canonical modulus modulo q
* to be compressed.
************************************************************/
/*
* The multiplication in this routine will exceed UINT32_MAX
* and wrap around for large values of u. This is expected and required.
*/
#ifdef CBMC
#pragma CPROVER check push
#pragma CPROVER check disable "unsigned-overflow"
#endif
static INLINE uint32_t scalar_compress_d1(uint16_t u)
__contract__(
requires(u <= MLKEM_Q - 1)
ensures(return_value < 2)
ensures(return_value == (((uint32_t)u * 2 + MLKEM_Q / 2) / MLKEM_Q) % 2) )
{
uint32_t d0 = u << 1;
d0 *= 645083;
d0 += 1u << 30;
d0 >>= 31;
return d0;
}
#ifdef CBMC
#pragma CPROVER check pop
#endif
/************************************************************
* Name: scalar_compress_d4
*
* Description: Computes round(u * 16 / q) % 16
*
* Implements Compress_d from FIPS203, Eq (4.7),
* for d = 4.
*
* Arguments: - u: Unsigned canonical modulus modulo q
* to be compressed.
************************************************************/
/*
* The multiplication in this routine will exceed UINT32_MAX
* and wrap around for large values of u. This is expected and required.
*/
#ifdef CBMC
#pragma CPROVER check push
#pragma CPROVER check disable "unsigned-overflow"
#endif
static INLINE uint32_t scalar_compress_d4(uint16_t u)
__contract__(
requires(u <= MLKEM_Q - 1)
ensures(return_value < 16)
ensures(return_value == (((uint32_t)u * 16 + MLKEM_Q / 2) / MLKEM_Q) % 16))
{
uint32_t d0 = (uint32_t)u * 1290160; /* 16 * round(2^28 / MLKEM_Q) */
return (d0 + (1u << 27)) >> 28; /* round(d0/2^28) */
}
#ifdef CBMC
#pragma CPROVER check pop
#endif
/************************************************************
* Name: scalar_decompress_d4
*
* Description: Computes round(u * q / 16)
*
* Implements Decompress_d from FIPS203, Eq (4.8),
* for d = 4.
*
* Arguments: - u: Unsigned canonical modulus modulo 16
* to be decompressed.
************************************************************/
static INLINE uint16_t scalar_decompress_d4(uint32_t u)
__contract__(
requires(0 <= u && u < 16)
ensures(return_value <= (MLKEM_Q - 1))
) { return ((u * MLKEM_Q) + 8) / 16; }
/************************************************************
* Name: scalar_compress_d5
*
* Description: Computes round(u * 32 / q) % 32
*
* Implements Compress_d from FIPS203, Eq (4.7),
* for d = 5.
*
* Arguments: - u: Unsigned canonical modulus modulo q
* to be compressed.
************************************************************/
/*
* The multiplication in this routine will exceed UINT32_MAX
* and wrap around for large values of u. This is expected and required.
*/
#ifdef CBMC
#pragma CPROVER check push
#pragma CPROVER check disable "unsigned-overflow"
#endif
static INLINE uint32_t scalar_compress_d5(uint16_t u)
__contract__(
requires(u <= MLKEM_Q - 1)
ensures(return_value < 32)
ensures(return_value == (((uint32_t)u * 32 + MLKEM_Q / 2) / MLKEM_Q) % 32) )
{
uint32_t d0 = (uint32_t)u * 1290176; /* 2^5 * round(2^27 / MLKEM_Q) */
return (d0 + (1u << 26)) >> 27; /* round(d0/2^27) */
}
#ifdef CBMC
#pragma CPROVER check pop
#endif
/************************************************************
* Name: scalar_decompress_d5
*
* Description: Computes round(u * q / 32)
*
* Implements Decompress_d from FIPS203, Eq (4.8),
* for d = 5.
*
* Arguments: - u: Unsigned canonical modulus modulo 32
* to be decompressed.
************************************************************/
static INLINE uint16_t scalar_decompress_d5(uint32_t u)
__contract__(
requires(0 <= u && u < 32)
ensures(return_value <= MLKEM_Q - 1)
) { return ((u * MLKEM_Q) + 16) / 32; }
/************************************************************
* Name: scalar_compress_d10
*
* Description: Computes round(u * 2**10 / q) % 2**10
*
* Implements Compress_d from FIPS203, Eq (4.7),
* for d = 10.
*
* Arguments: - u: Unsigned canonical modulus modulo q
* to be compressed.
************************************************************/
/*
* The multiplication in this routine will exceed UINT32_MAX
* and wrap around for large values of u. This is expected and required.
*/
#ifdef CBMC
#pragma CPROVER check push
#pragma CPROVER check disable "unsigned-overflow"
#endif
static INLINE uint32_t scalar_compress_d10(uint16_t u)
__contract__(
requires(u <= MLKEM_Q - 1)
ensures(return_value < (1u << 10))
ensures(return_value == (((uint32_t)u * (1u << 10) + MLKEM_Q / 2) / MLKEM_Q) % (1 << 10)))
{
uint64_t d0 = (uint64_t)u * 2642263040; /* 2^10 * round(2^32 / MLKEM_Q) */
d0 = (d0 + ((uint64_t)1u << 32)) >> 33;
return (d0 & 0x3FF);
}
#ifdef CBMC
#pragma CPROVER check pop
#endif
/************************************************************
* Name: scalar_decompress_d10
*
* Description: Computes round(u * q / 1024)
*
* Implements Decompress_d from FIPS203, Eq (4.8),
* for d = 10.
*
* Arguments: - u: Unsigned canonical modulus modulo 16
* to be decompressed.
************************************************************/
static INLINE uint16_t scalar_decompress_d10(uint32_t u)
__contract__(
requires(0 <= u && u < 1024)
ensures(return_value <= (MLKEM_Q - 1))
) { return ((u * MLKEM_Q) + 512) / 1024; }
/************************************************************
* Name: scalar_compress_d11
*
* Description: Computes round(u * 2**11 / q) % 2**11
*
* Implements Compress_d from FIPS203, Eq (4.7),
* for d = 11.
*
* Arguments: - u: Unsigned canonical modulus modulo q
* to be compressed.
************************************************************/
/*
* The multiplication in this routine will exceed UINT32_MAX
* and wrap around for large values of u. This is expected and required.
*/
#ifdef CBMC
#pragma CPROVER check push
#pragma CPROVER check disable "unsigned-overflow"
#endif
static INLINE uint32_t scalar_compress_d11(uint16_t u)
__contract__(
requires(u <= MLKEM_Q - 1)
ensures(return_value < (1u << 11))
ensures(return_value == (((uint32_t)u * (1u << 11) + MLKEM_Q / 2) / MLKEM_Q) % (1 << 11)))
{
uint64_t d0 = (uint64_t)u * 5284526080; /* 2^11 * round(2^33 / MLKEM_Q) */
d0 = (d0 + ((uint64_t)1u << 32)) >> 33;
return (d0 & 0x7FF);
}
#ifdef CBMC
#pragma CPROVER check pop
#endif
/************************************************************
* Name: scalar_decompress_d11
*
* Description: Computes round(u * q / 1024)
*
* Implements Decompress_d from FIPS203, Eq (4.8),
* for d = 10.
*
* Arguments: - u: Unsigned canonical modulus modulo 16
* to be decompressed.
************************************************************/
static INLINE uint16_t scalar_decompress_d11(uint32_t u)
__contract__(
requires(0 <= u && u < 2048)
ensures(return_value <= (MLKEM_Q - 1))
) { return ((u * MLKEM_Q) + 1024) / 2048; }
/************************************************************
* Name: scalar_signed_to_unsigned_q
*
* Description: converts signed polynomial coefficient
* from signed (-3328 .. 3328) form to
* unsigned form (0 .. 3328).
*
* Note: Cryptographic constant time implementation
*
* Examples: 0 -> 0
* 1 -> 1
* 3328 -> 3328
* -1 -> 3328
* -2 -> 3327
* -3328 -> 1
*
* Arguments: c: signed coefficient to be converted
************************************************************/
static INLINE uint16_t scalar_signed_to_unsigned_q(int16_t c)
__contract__(
requires(c >= -(MLKEM_Q - 1) && c <= (MLKEM_Q - 1))
ensures(return_value >= 0 && return_value <= (MLKEM_Q - 1))
ensures(return_value == (int32_t)c + (((int32_t)c < 0) * MLKEM_Q)))
{
/* Add Q if c is negative, but in constant time */
c = ct_sel_int16(c + MLKEM_Q, c, ct_cmask_neg_i16(c));
cassert(c >= 0, "scalar_signed_to_unsigned_q result lower bound");
cassert(c < MLKEM_Q, "scalar_signed_to_unsigned_q result upper bound");
/* and therefore cast to uint16_t is safe. */
return (uint16_t)c;
}
#define poly_compress_du MLKEM_NAMESPACE(poly_compress_du)
/*************************************************
* Name: 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)
* - const poly *a: pointer to input polynomial
* Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
**************************************************/
void poly_compress_du(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_DU], const poly *a)
__contract__(
requires(memory_no_alias(r, MLKEM_POLYCOMPRESSEDBYTES_DU))
requires(memory_no_alias(a, sizeof(poly)))
requires(array_bound(a->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
assigns(memory_slice(r, MLKEM_POLYCOMPRESSEDBYTES_DU))
);
#define poly_decompress_du MLKEM_NAMESPACE(poly_decompress_du)
/*************************************************
* Name: poly_decompress_du
*
* Description: De-serialization and subsequent decompression (du bits) of a
*polynomial; approximate inverse of poly_compress_du
*
* Arguments: - poly *r: pointer to output polynomial
* - const uint8_t *a: pointer to input byte array
* (of length MLKEM_POLYCOMPRESSEDBYTES bytes)
*
* Upon return, the coefficients of the output polynomial are unsigned-canonical
* (non-negative and smaller than MLKEM_Q).
*
**************************************************/
void poly_decompress_du(poly *r, const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_DU])
__contract__(
requires(memory_no_alias(a, MLKEM_POLYCOMPRESSEDBYTES_DU))
requires(memory_no_alias(r, sizeof(poly)))
assigns(memory_slice(r, sizeof(poly)))
ensures(array_bound(r->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
);
#define poly_compress_dv MLKEM_NAMESPACE(poly_compress_dv)
/*************************************************
* Name: 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)
* - const poly *a: pointer to input polynomial
* Coefficients must be unsigned canonical,
* i.e. in [0,1,..,MLKEM_Q-1].
**************************************************/
void poly_compress_dv(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_DV], const poly *a)
__contract__(
requires(memory_no_alias(r, MLKEM_POLYCOMPRESSEDBYTES_DV))
requires(memory_no_alias(a, sizeof(poly)))
requires(array_bound(a->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
assigns(object_whole(r))
);
#define poly_decompress_dv MLKEM_NAMESPACE(poly_decompress_dv)
/*************************************************
* Name: poly_decompress_dv
*
* Description: De-serialization and subsequent decompression (dv bits) of a
*polynomial; approximate inverse of poly_compress
*
* Arguments: - 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).
*
**************************************************/
void poly_decompress_dv(poly *r, const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_DV])
__contract__(
requires(memory_no_alias(a, MLKEM_POLYCOMPRESSEDBYTES_DV))
requires(memory_no_alias(r, sizeof(poly)))
assigns(object_whole(r))
ensures(array_bound(r->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
);
#define poly_tobytes MLKEM_NAMESPACE(poly_tobytes)
/*************************************************
* Name: poly_tobytes
*
* Description: Serialization of a polynomial.
* Signed coefficients are converted to
* unsigned form before serialization.
*
* Arguments: INPUT:
* - a: const pointer to input polynomial,
* with each coefficient in the range [0,1,..,Q-1]
* OUTPUT
* - r: pointer to output byte array
* (of MLKEM_POLYBYTES bytes)
**************************************************/
void poly_tobytes(uint8_t r[MLKEM_POLYBYTES], const poly *a)
__contract__(
requires(memory_no_alias(r, MLKEM_POLYBYTES))
requires(memory_no_alias(a, sizeof(poly)))
requires(array_bound(a->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
assigns(object_whole(r))
);
#define poly_frombytes MLKEM_NAMESPACE(poly_frombytes)
/*************************************************
* Name: poly_frombytes
*
* Description: De-serialization of a polynomial.
*
* Arguments: INPUT
* - a: pointer to input byte array
* (of MLKEM_POLYBYTES bytes)
* OUTPUT
* - r: pointer to output polynomial, with
* each coefficient unsigned and in the range
* 0 .. 4095
**************************************************/
void poly_frombytes(poly *r, const uint8_t a[MLKEM_POLYBYTES])
__contract__(
requires(memory_no_alias(a, MLKEM_POLYBYTES))
requires(memory_no_alias(r, sizeof(poly)))
assigns(memory_slice(r, sizeof(poly)))
ensures(array_bound(r->coeffs, 0, (MLKEM_N - 1), 0, 4095))
);
#define poly_frommsg MLKEM_NAMESPACE(poly_frommsg)
/*************************************************
* Name: poly_frommsg
*
* Description: Convert 32-byte message to polynomial
*
* Arguments: - poly *r: pointer to output polynomial
* - const uint8_t *msg: pointer to input message
**************************************************/
void poly_frommsg(poly *r, const uint8_t msg[MLKEM_INDCPA_MSGBYTES])
__contract__(
requires(memory_no_alias(msg, MLKEM_INDCPA_MSGBYTES))
requires(memory_no_alias(r, sizeof(poly)))
assigns(object_whole(r))
ensures(array_bound(r->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
);
#define poly_tomsg MLKEM_NAMESPACE(poly_tomsg)
/*************************************************
* Name: poly_tomsg
*
* Description: Convert polynomial to 32-byte message
*
* Arguments: - uint8_t *msg: pointer to output message
* - const poly *r: pointer to input polynomial
* Coefficients must be unsigned canonical
**************************************************/
void poly_tomsg(uint8_t msg[MLKEM_INDCPA_MSGBYTES], const poly *r)
__contract__(
requires(memory_no_alias(msg, MLKEM_INDCPA_MSGBYTES))
requires(memory_no_alias(r, sizeof(poly)))
requires(array_bound(r->coeffs, 0, (MLKEM_N - 1), 0, (MLKEM_Q - 1)))
assigns(object_whole(msg))
);
#define poly_getnoise_eta1_4x MLKEM_NAMESPACE(poly_getnoise_eta1_4x)
/*************************************************
* Name: 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: - 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
**************************************************/
void poly_getnoise_eta1_4x(poly *r0, poly *r1, poly *r2, poly *r3,
const uint8_t seed[MLKEM_SYMBYTES], uint8_t nonce0,
uint8_t nonce1, uint8_t nonce2, uint8_t nonce3)
/* Depending on MLKEM_K, the pointers passed to this function belong
to the same objects, so we cannot use memory_no_alias for r0-r3.
NOTE: Somehow it is important to use memory_no_alias() first in the
conjunctions defining each case.
*/
#if MLKEM_K == 2
__contract__(
requires(memory_no_alias(seed, MLKEM_SYMBYTES))
requires( /* Case A: r0, r1 consecutive, r2, r3 consecutive */
(memory_no_alias(r0, 2 * sizeof(poly)) && memory_no_alias(r2, 2 * sizeof(poly)) &&
r1 == r0 + 1 && r3 == r2 + 1 && !same_object(r0, r2)))
assigns(memory_slice(r0, sizeof(poly)))
assigns(memory_slice(r1, sizeof(poly)))
assigns(memory_slice(r2, sizeof(poly)))
assigns(memory_slice(r3, sizeof(poly)))
ensures(
array_abs_bound(r0->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r1->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r2->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r3->coeffs,0, MLKEM_N - 1, MLKEM_ETA1));
);
#elif MLKEM_K == 4
__contract__(
requires(memory_no_alias(seed, MLKEM_SYMBYTES))
requires( /* Case B: r0, r1, r2, r3 consecutive */
(memory_no_alias(r0, 4 * sizeof(poly)) && r1 == r0 + 1 && r2 == r0 + 2 && r3 == r0 + 3))
assigns(memory_slice(r0, sizeof(poly)))
assigns(memory_slice(r1, sizeof(poly)))
assigns(memory_slice(r2, sizeof(poly)))
assigns(memory_slice(r3, sizeof(poly)))
ensures(
array_abs_bound(r0->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r1->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r2->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r3->coeffs,0, MLKEM_N - 1, MLKEM_ETA1));
);
#elif MLKEM_K == 3
__contract__(
requires(memory_no_alias(seed, MLKEM_SYMBYTES))
requires( /* Case C: r0, r1, r2 consecutive */
(memory_no_alias(r0, 3 * sizeof(poly)) && memory_no_alias(r3, 1 * sizeof(poly)) &&
r1 == r0 + 1 && r2 == r0 + 2 && !same_object(r3, r0)))
assigns(memory_slice(r0, sizeof(poly)))
assigns(memory_slice(r1, sizeof(poly)))
assigns(memory_slice(r2, sizeof(poly)))
assigns(memory_slice(r3, sizeof(poly)))
ensures(
array_abs_bound(r0->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r1->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r2->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r3->coeffs,0, MLKEM_N - 1, MLKEM_ETA1));
);
#endif /* MLKEM_K */
#if MLKEM_ETA1 == MLKEM_ETA2
/*
* We only require poly_getnoise_eta2_4x for ml-kem-768 and ml-kem-1024
* where MLKEM_ETA2 = MLKEM_ETA1 = 2.
* For ml-kem-512, poly_getnoise_eta1122_4x is used instead.
*/
#define poly_getnoise_eta2_4x poly_getnoise_eta1_4x
#endif /* MLKEM_ETA1 == MLKEM_ETA2 */
#define poly_getnoise_eta2 MLKEM_NAMESPACE(poly_getnoise_eta2)
/*************************************************
* Name: 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: - 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
**************************************************/
void poly_getnoise_eta2(poly *r, const uint8_t seed[MLKEM_SYMBYTES],
uint8_t nonce)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
requires(memory_no_alias(seed, MLKEM_SYMBYTES))
assigns(object_whole(r))
ensures(array_abs_bound(r->coeffs, 0, MLKEM_N - 1, MLKEM_ETA2))
);
#define poly_getnoise_eta1122_4x MLKEM_NAMESPACE(poly_getnoise_eta1122_4x)
/*************************************************
* Name: 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: - 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
**************************************************/
void poly_getnoise_eta1122_4x(poly *r0, poly *r1, poly *r2, poly *r3,
const uint8_t seed[MLKEM_SYMBYTES],
uint8_t nonce0, uint8_t nonce1, uint8_t nonce2,
uint8_t nonce3)
__contract__(
requires( /* r0, r1 consecutive, r2, r3 consecutive */
(memory_no_alias(r0, 2 * sizeof(poly)) && memory_no_alias(r2, 2 * sizeof(poly)) &&
r1 == r0 + 1 && r3 == r2 + 1 && !same_object(r0, r2)))
requires(memory_no_alias(seed, MLKEM_SYMBYTES))
assigns(object_whole(r0), object_whole(r1), object_whole(r2), object_whole(r3))
ensures(array_abs_bound(r0->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r1->coeffs,0, MLKEM_N - 1, MLKEM_ETA1)
&& array_abs_bound(r2->coeffs,0, MLKEM_N - 1, MLKEM_ETA2)
&& array_abs_bound(r3->coeffs,0, MLKEM_N - 1, MLKEM_ETA2));
);
#define poly_basemul_montgomery_cached \
MLKEM_NAMESPACE(poly_basemul_montgomery_cached)
/*************************************************
* Name: poly_basemul_montgomery_cached
*
* Description: Multiplication of two polynomials in NTT domain,
* using mulcache for second operand.
*
* Bounds:
* - a is assumed to be coefficient-wise < q in absolute value.
*
* The result is coefficient-wise bound by 3/2 q in absolute
* value.
*
* Arguments: - poly *r: pointer to output polynomial
* - const poly *a: pointer to first input polynomial
* - const poly *b: pointer to second input polynomial
* - const poly_mulcache *b_cache: pointer to mulcache
* for second input polynomial. Can be computed
* via poly_mulcache_compute().
**************************************************/
void poly_basemul_montgomery_cached(poly *r, const poly *a, const poly *b,
const poly_mulcache *b_cache)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
requires(memory_no_alias(a, sizeof(poly)))
requires(memory_no_alias(b, sizeof(poly)))
requires(memory_no_alias(b_cache, sizeof(poly_mulcache)))
requires(array_abs_bound(a->coeffs, 0, MLKEM_N - 1, (MLKEM_Q - 1)))
assigns(object_whole(r))
ensures(array_abs_bound(r->coeffs, 0, MLKEM_N - 1, (3 * HALF_Q - 1)))
);
#define poly_tomont MLKEM_NAMESPACE(poly_tomont)
/*************************************************
* Name: poly_tomont
*
* Description: Inplace conversion of all coefficients of a polynomial
* from normal domain to Montgomery domain
*
* Bounds: Output < q in absolute value.
*
* Arguments: - poly *r: pointer to input/output polynomial
**************************************************/
void poly_tomont(poly *r)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
assigns(memory_slice(r, sizeof(poly)))
ensures(array_abs_bound(r->coeffs, 0, MLKEM_N - 1, (MLKEM_Q - 1)))
);
/* REF-CHANGE: This function does not exist in the reference implementation */
#define poly_mulcache_compute MLKEM_NAMESPACE(poly_mulcache_compute)
/************************************************************
* Name: poly_mulcache_compute
*
* Description: Computes the mulcache for a polynomial 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.
*
* Arguments: - x: Pointer to mulcache to be populated
* - a: Pointer to input polynomial
************************************************************/
/*
* 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 poly_mulcache_compute(poly_mulcache *x, const poly *a)
__contract__(
requires(memory_no_alias(x, sizeof(poly_mulcache)))
requires(memory_no_alias(a, sizeof(poly)))
assigns(object_whole(x))
);
#define poly_reduce MLKEM_NAMESPACE(poly_reduce)
/*************************************************
* Name: poly_reduce
*
* Description: Converts polynomial to _unsigned canonical_ representatives.
*
* The input coefficients can be arbitrary integers in int16_t.
* The output coefficients are in [0,1,...,MLKEM_Q-1].
*
* Arguments: - poly *r: pointer to input/output polynomial
**************************************************/
/*
* REF-CHANGE: The semantics of poly_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 poly_reduce() in the context of (de)serialization.
*/
void poly_reduce(poly *r)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
assigns(memory_slice(r, sizeof(poly)))
ensures(array_bound(r->coeffs, 0, MLKEM_N - 1, 0, (MLKEM_Q - 1)))
);
#define poly_add MLKEM_NAMESPACE(poly_add)
/************************************************************
* Name: poly_add
*
* Description: Adds two polynomials in place
*
* Arguments: - r: Pointer to input-output polynomial to be added to.
* - b: Pointer to input polynomial that should be added
* to r. Must be disjoint from r.
*
* The coefficients of r and b must be so that the addition does
* not overflow. Otherwise, the behaviour of this function is undefined.
*
************************************************************/
/*
* REF-CHANGE:
* The reference implementation uses a 3-argument poly_add.
* We specialize to the accumulator form to avoid reasoning about aliasing.
*/
void poly_add(poly *r, const poly *b)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
requires(memory_no_alias(b, sizeof(poly)))
requires(forall(int, k0, 0, MLKEM_N - 1, (int32_t) r->coeffs[k0] + b->coeffs[k0] <= INT16_MAX))
requires(forall(int, k1, 0, MLKEM_N - 1, (int32_t) r->coeffs[k1] + b->coeffs[k1] >= INT16_MIN))
ensures(forall(int, k, 0, MLKEM_N - 1, r->coeffs[k] == old(*r).coeffs[k] + b->coeffs[k]))
assigns(memory_slice(r, sizeof(poly)))
);
#define poly_sub MLKEM_NAMESPACE(poly_sub)
/*************************************************
* Name: poly_sub
*
* Description: Subtract two polynomials; no modular reduction is performed
*
* Arguments: - poly *r: Pointer to input-output polynomial to be added
*to.
* - const poly *b: Pointer to second input polynomial
**************************************************/
/*
* REF-CHANGE:
* The reference implementation uses a 3-argument poly_sub.
* We specialize to the accumulator form to avoid reasoning about aliasing.
*/
void poly_sub(poly *r, const poly *b)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
requires(memory_no_alias(b, sizeof(poly)))
requires(forall(int, k0, 0, MLKEM_N - 1, (int32_t) r->coeffs[k0] - b->coeffs[k0] <= INT16_MAX))
requires(forall(int, k1, 0, MLKEM_N - 1, (int32_t) r->coeffs[k1] - b->coeffs[k1] >= INT16_MIN))
ensures(forall(int, k, 0, MLKEM_N - 1, r->coeffs[k] == old(*r).coeffs[k] - b->coeffs[k]))
assigns(object_whole(r))
);
#endif