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
/*************************************************
* Name: ml_dsa_polyvec_matrix_expand
*
* Description: FIPS 204: Algorithm 32 ExpandA.
* Generates matrix A with uniformly
* random coefficients a_{i,j} by performing rejection
* sampling on the output stream of SHAKE128(rho|j|i)
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl mat: pointer to output matrix
* - const uint8_t rho[]: byte array containing seed rho
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvec_matrix_pointwise_montgomery
*
* Description: Pointwise multiply vectors of polynomials of length K,
* wrapper for polyvecl_pointwise_acc_montgomery.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck t: pointer to output polynomial
* - polyvecl mat: pointer to first input vector
* - polyvecl v: pointer to second input vector
**************************************************/
void
/**************************************************************/
/************ Vectors of polynomials of length L **************/
/**************************************************************/
/*************************************************
* Name: ml_dsa_polyvecl_uniform_eta
*
* Description: FIPS 204: Algorithm 33 ExpandS (for vectors l).
* Samples vector v with polynomial coordinates whose
* coefficients are in [-eta, eta].
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl v: pointer to output vector
* - const uint8_t seed: byte array containing seed
* - uint16_t nonce: 2-byte nonce
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_uniform_gamma1
*
* Description: FIPS 204: Algorithm 34 ExpandMask.
* Samples vector v with polynomial coordinates whose
* coefficients are in [-gamma1 + 1, gamma1].
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl v: pointer to output vector
* - const uint8_t seed: byte array containing seed
* - uint16_t nonce: 2-byte nonce
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_reduce
*
* Description: Reduce coefficients of polynomials in vector of length L
* to representatives in [-6283009,6283007].
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_add
*
* Description: Add vectors of polynomials of length L.
* No modular reduction is performed.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl *w: pointer to output vector
* - const polyvecl *u: pointer to first summand
* - const polyvecl *v: pointer to second summand
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_ntt
*
* Description: Forward NTT of all polynomials in vector of length L. Output
* coefficients can be up to 16*Q larger than input coefficients.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_invntt_tomont
*
* Description: Inverse NTT and multiplication by 2^{32} of polynomials
* in vector of length l. Input coefficients need to be less
* than 2*Q.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_pointwise_poly_montgomery
*
* Description: Pointwise multiplication of polynomials in NTT domain
* representation and multiplication of resulting polynomial
* by 2^{-32}.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyvecl *r: pointer to output polynomial
* - const poly *a: pointer to input polynomial
* - const polyvecl *v: pointer to input vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_pointwise_acc_montgomery
*
* Description: Pointwise multiply vectors of polynomials of length L, multiply
* resulting vector by 2^{-32} and add (accumulate) polynomials
* in it. Input/output vectors are in NTT domain representation.
*
* Arguments: - ml_dsa_params: parameter struct
* - poly *w: output polynomial
* - const polyvecl *u: pointer to first input vector
* - const polyvecl *v: pointer to second input vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyvecl_chknorm
*
* Description: Check infinity norm of polynomials in vector of length L.
* Assumes input polyvecl to be reduced by polyvecl_reduce().
*
* Arguments: - ml_dsa_params: parameter struct
* - const polyvecl *v: pointer to vector
* - int32_t B: norm bound
*
* Returns 0 if norm of all polynomials is strictly smaller than B <= (Q-1)/8
* and 1 otherwise.
**************************************************/
int
/**************************************************************/
/************ Vectors of polynomials of length K **************/
/**************************************************************/
/*************************************************
* Name: ml_dsa_polyvecl_uniform_eta
*
* Description: FIPS 204: Algorithm 33 ExpandS (for vectors k).
* Samples vector v with polynomial coordinates whose
* coefficients are in [-eta, eta].
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck v: pointer to output vector
* - const uint8_t seed: byte array containing seed
* - uint16_t nonce: 2-byte nonce
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_reduce
*
* Description: Reduce coefficients of polynomials in vector of length K
* to representatives in [-6283009,6283007].
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_caddq
*
* Description: For all coefficients of polynomials in vector of length K
* add Q if coefficient is negative.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_add
*
* Description: Add vectors of polynomials of length K.
* No modular reduction is performed.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *w: pointer to output vector
* - const polyveck *u: pointer to first summand
* - const polyveck *v: pointer to second summand
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_sub
*
* Description: Subtract vectors of polynomials of length K.
* No modular reduction is performed.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *w: pointer to output vector
* - const polyveck *u: pointer to first input vector
* - const polyveck *v: pointer to second input vector to be
* subtracted from first input vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_shiftl
*
* Description: Multiply vector of polynomials of Length K by 2^D without modular
* reduction. Assumes input coefficients to be less than 2^{31-D}.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_ntt
*
* Description: Forward NTT of all polynomials in vector of length K. Output
* coefficients can be up to 16*Q larger than input coefficients.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_invntt_tomont
*
* Description: Inverse NTT and multiplication by 2^{32} of polynomials
* in vector of length K. Input coefficients need to be less
* than 2*Q.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v: pointer to input/output vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_pointwise_poly_montgomery
*
* Description: Pointwise multiplication of polynomials in NTT domain
* representation and multiplication of resulting polynomial
* by 2^{-32}.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *r: pointer to output polynomial
* - const poly *a: pointer to input polynomial
* - const polyveck *v: pointer to input vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_chknorm
*
* Description: Check infinity norm of polynomials in vector of length K.
* Assumes input polyveck to be reduced by polyveck_reduce().
*
* Arguments: - ml_dsa_params: parameter struct
* - const polyveck *v: pointer to vector
* - int32_t B: norm bound
*
* Returns 0 if norm of all polynomials are strictly smaller than B <= (Q-1)/8
* and 1 otherwise.
**************************************************/
int
/*************************************************
* Name: ml_dsa_polyveck_power2round
*
* Description: For all coefficients a of polynomials in vector of length K,
* compute a0, a1 such that a mod^+ Q = a1*2^D + a0
* with -2^{D-1} < a0 <= 2^{D-1}. Assumes coefficients to be
* standard representatives.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v1: pointer to output vector of polynomials with
* coefficients a1
* - polyveck *v0: pointer to output vector of polynomials with
* coefficients a0
* - const polyveck *v: pointer to input vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_decompose
*
* Description: For all coefficients a of polynomials in vector of length K,
* compute high and low bits a0, a1 such a mod^+ Q = a1*ALPHA + a0
* with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (Q-1)/ALPHA where we
* set a1 = 0 and -ALPHA/2 <= a0 = a mod Q - Q < 0.
* Assumes coefficients to be standard representatives.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *v1: pointer to output vector of polynomials with
* coefficients a1
* - polyveck *v0: pointer to output vector of polynomials with
* coefficients a0
* - const polyveck *v: pointer to input vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_make_hint
*
* Description: Compute hint vector.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *h: pointer to output vector
* - const polyveck *v0: pointer to low part of input vector
* - const polyveck *v1: pointer to high part of input vector
*
* Returns number of 1 bits.
**************************************************/
unsigned int
/*************************************************
* Name: ml_dsa_polyveck_use_hint
*
* Description: Use hint vector to correct the high bits of input vector.
*
* Arguments: - ml_dsa_params: parameter struct
* - polyveck *w: pointer to output vector of polynomials with
* corrected high bits
* - const polyveck *u: pointer to input vector
* - const polyveck *h: pointer to input hint vector
**************************************************/
void
/*************************************************
* Name: ml_dsa_polyveck_pack_w1
*
* Description: FIPS 204: Algorithm 28 w1Encode.
* Encodes a polynomial vector |w1| into a byte string.
*
* Arguments: - ml_dsa_params: parameter struct
* - uint8_t *r: pointer to output byte array with at least
* POLYW1_PACKEDBYTES bytes
* - const polyvecl *w1: pointer to vector w1
**************************************************/
void