Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/basic/mldsa_native/src/polyvec.c

raw
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4 */
5
6 /* References
7 * ==========
8 *
9 * - [FIPS204]
10 * FIPS 204 Module-Lattice-Based Digital Signature Standard
11 * National Institute of Standards and Technology
12 * https://csrc.nist.gov/pubs/fips/204/final
13 */
14
15 #include <stdint.h>
16 #include <string.h>
17
18 #include "common.h"
19 #include "debug.h"
20 #include "poly.h"
21 #include "poly_kl.h"
22 #include "polyvec.h"
23
24 /* This namespacing is not done at the top to avoid a naming conflict
25 * with native backends, which are currently not yet namespaced. */
26 #define mld_polymat_permute_bitrev_to_custom \
27 MLD_ADD_PARAM_SET(mld_polymat_permute_bitrev_to_custom)
28 #define mld_polyvecl_permute_bitrev_to_custom \
29 MLD_ADD_PARAM_SET(mld_polyvecl_permute_bitrev_to_custom)
30 #define mld_polyvecl_pointwise_acc_montgomery_c \
31 MLD_ADD_PARAM_SET(mld_polyvecl_pointwise_acc_montgomery_c)
32
33 #if !defined(MLD_CONFIG_REDUCE_RAM)
34 /* Helper function to ensure that the polynomial entries in the output
35 * of mld_polyvec_matrix_expand use the standard (bitreversed) ordering
36 * of coefficients.
37 * No-op unless a native backend with a custom ordering is used.
38 */
39
40 static void mld_polyvecl_permute_bitrev_to_custom(mld_polyvecl *v)
41 __contract__(
42 /* We don't specify that this should be a permutation, but only
43 * that it does not change the bound established at the end of
44 * mld_polyvec_matrix_expand.
45 */
46 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
47 requires(forall(x, 0, MLDSA_L,
48 array_bound(v->vec[x].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
49 assigns(memory_slice(v, sizeof(mld_polyvecl)))
50 ensures(forall(x, 0, MLDSA_L,
51 array_bound(v->vec[x].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
52 {
53 #if defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
54 unsigned i;
55 for (i = 0; i < MLDSA_L; i++)
56 __loop__(
57 assigns(i, memory_slice(v, sizeof(mld_polyvecl)))
58 invariant(i <= MLDSA_L)
59 invariant(forall(x, 0, MLDSA_L,
60 array_bound(v->vec[x].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
61 {
62 mld_poly_permute_bitrev_to_custom(v->vec[i].coeffs);
63 }
64 #else /* MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
65 /* Nothing to do */
66 (void)v;
67 #endif /* !MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
68 }
69
70 static void mld_polymat_permute_bitrev_to_custom(mld_polymat *mat)
71 __contract__(
72 /* We don't specify that this should be a permutation, but only
73 * that it does not change the bound established at the end of
74 * mld_polyvec_matrix_expand.
75 */
76 requires(memory_no_alias(mat, sizeof(mld_polymat)))
77 requires(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
78 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
79 assigns(memory_slice(mat, sizeof(mld_polymat)))
80 ensures(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
81 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
82 )
83 {
84 unsigned int i;
85 for (i = 0; i < MLDSA_K; i++)
86 __loop__(
87 assigns(i, memory_slice(mat, sizeof(mld_polymat)))
88 invariant(i <= MLDSA_K)
89 invariant(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
90 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))))
91 {
92 mld_polyvecl_permute_bitrev_to_custom(&mat->vec[i]);
93 }
94 }
95 #endif /* !MLD_CONFIG_REDUCE_RAM */
96
97 MLD_INTERNAL_API
98 const mld_polyvecl *mld_polymat_get_row(mld_polymat *mat, unsigned int row)
99 {
100 #if defined(MLD_CONFIG_REDUCE_RAM)
101 unsigned int i;
102 MLD_ALIGN uint8_t seed_ext[MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)];
103
104 mld_memcpy(seed_ext, mat->rho, MLDSA_SEEDBYTES);
105
106 /* Generate row on-demand */
107 for (i = 0; i < MLDSA_L; i++)
108 {
109 uint8_t x = (uint8_t)row;
110 uint8_t y = (uint8_t)i;
111
112 seed_ext[MLDSA_SEEDBYTES + 0] = y;
113 seed_ext[MLDSA_SEEDBYTES + 1] = x;
114
115 mld_poly_uniform(&mat->row_buffer.vec[i], seed_ext);
116
117 #if defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
118 mld_poly_permute_bitrev_to_custom(mat->row_buffer.vec[i].coeffs);
119 #endif
120 }
121
122 /* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
123 mld_zeroize(seed_ext, sizeof(seed_ext));
124
125 return &mat->row_buffer;
126 #else /* MLD_CONFIG_REDUCE_RAM */
127 return &mat->vec[row];
128 #endif /* !MLD_CONFIG_REDUCE_RAM */
129 }
130
131 MLD_INTERNAL_API
132 void mld_polyvec_matrix_expand(mld_polymat *mat,
133 const uint8_t rho[MLDSA_SEEDBYTES])
134 {
135 #if defined(MLD_CONFIG_REDUCE_RAM)
136 /* In REDUCE_RAM mode, just copy the seed for later on-demand generation */
137 mld_memcpy(mat->rho, rho, MLDSA_SEEDBYTES);
138 #else
139 unsigned int i, j;
140 /*
141 * We generate four separate seed arrays rather than a single one to work
142 * around limitations in CBMC function contracts dealing with disjoint slices
143 * of the same parent object.
144 */
145
146 MLD_ALIGN uint8_t seed_ext[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)];
147
148 for (j = 0; j < 4; j++)
149 __loop__(
150 assigns(j, object_whole(seed_ext))
151 invariant(j <= 4)
152 )
153 {
154 mld_memcpy(seed_ext[j], rho, MLDSA_SEEDBYTES);
155 }
156
157 #if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
158 /* Sample 4 matrix entries a time. */
159 for (i = 0; i < (MLDSA_K * MLDSA_L / 4) * 4; i += 4)
160 __loop__(
161 assigns(i, j, object_whole(seed_ext), memory_slice(mat, sizeof(mld_polymat)))
162 invariant(i <= (MLDSA_K * MLDSA_L / 4) * 4 && i % 4 == 0)
163 /* vectors 0 .. i / MLDSA_L are completely sampled */
164 invariant(forall(k1, 0, i / MLDSA_L, forall(l1, 0, MLDSA_L,
165 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
166 /* last vector is sampled up to i % MLDSA_L */
167 invariant(forall(k2, i / MLDSA_L, i / MLDSA_L + 1, forall(l2, 0, i % MLDSA_L,
168 array_bound(mat->vec[k2].vec[l2].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
169 )
170 {
171 for (j = 0; j < 4; j++)
172 __loop__(
173 assigns(j, object_whole(seed_ext))
174 invariant(j <= 4)
175 )
176 {
177 uint8_t x = (uint8_t)((i + j) / MLDSA_L);
178 uint8_t y = (uint8_t)((i + j) % MLDSA_L);
179
180 seed_ext[j][MLDSA_SEEDBYTES + 0] = y;
181 seed_ext[j][MLDSA_SEEDBYTES + 1] = x;
182 }
183
184 mld_poly_uniform_4x(&mat->vec[i / MLDSA_L].vec[i % MLDSA_L],
185 &mat->vec[(i + 1) / MLDSA_L].vec[(i + 1) % MLDSA_L],
186 &mat->vec[(i + 2) / MLDSA_L].vec[(i + 2) % MLDSA_L],
187 &mat->vec[(i + 3) / MLDSA_L].vec[(i + 3) % MLDSA_L],
188 seed_ext);
189 }
190 #else /* !MLD_CONFIG_SERIAL_FIPS202_ONLY */
191 i = 0;
192 #endif /* MLD_CONFIG_SERIAL_FIPS202_ONLY */
193
194 /* Entries omitted by the batch-sampling are sampled individually. */
195 while (i < MLDSA_K * MLDSA_L)
196 __loop__(
197 assigns(i, object_whole(seed_ext), memory_slice(mat, sizeof(mld_polymat)))
198 invariant(i <= MLDSA_K * MLDSA_L)
199 /* vectors 0 .. i / MLDSA_L are completely sampled */
200 invariant(forall(k1, 0, i / MLDSA_L, forall(l1, 0, MLDSA_L,
201 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
202 /* last vector is sampled up to i % MLDSA_L */
203 invariant(forall(k2, i / MLDSA_L, i / MLDSA_L + 1, forall(l2, 0, i % MLDSA_L,
204 array_bound(mat->vec[k2].vec[l2].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
205 )
206 {
207 uint8_t x = (uint8_t)(i / MLDSA_L);
208 uint8_t y = (uint8_t)(i % MLDSA_L);
209 mld_poly *this_poly = &mat->vec[i / MLDSA_L].vec[i % MLDSA_L];
210
211 seed_ext[0][MLDSA_SEEDBYTES + 0] = y;
212 seed_ext[0][MLDSA_SEEDBYTES + 1] = x;
213
214 mld_poly_uniform(this_poly, seed_ext[0]);
215 i++;
216 }
217
218 mld_polymat_permute_bitrev_to_custom(mat);
219
220 /* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
221 mld_zeroize(seed_ext, sizeof(seed_ext));
222 #endif /* !MLD_CONFIG_REDUCE_RAM */
223 }
224
225 MLD_INTERNAL_API
226 void mld_polyvec_matrix_pointwise_montgomery(mld_polyveck *t, mld_polymat *mat,
227 const mld_polyvecl *v)
228 {
229 unsigned int i;
230 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLD_NTT_BOUND);
231
232 for (i = 0; i < MLDSA_K; ++i)
233 __loop__(
234 assigns(i, memory_slice(t, sizeof(mld_polyveck)))
235 invariant(i <= MLDSA_K)
236 invariant(forall(k0, 0, i,
237 array_abs_bound(t->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
238 )
239 {
240 const mld_polyvecl *row = mld_polymat_get_row(mat, i);
241 mld_polyvecl_pointwise_acc_montgomery(&t->vec[i], row, v);
242 }
243
244 mld_assert_abs_bound_2d(t->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
245 }
246
247 /**************************************************************/
248 /************ Vectors of polynomials of length MLDSA_L **************/
249 /**************************************************************/
250 MLD_INTERNAL_API
251 void mld_polyvecl_uniform_gamma1(mld_polyvecl *v,
252 const uint8_t seed[MLDSA_CRHBYTES],
253 uint16_t nonce)
254 {
255 #if defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
256 int i;
257 #endif
258
259 /* Safety: nonce is at most ((UINT16_MAX - MLDSA_L) / MLDSA_L), and, hence,
260 * this cast is safe. See MLD_NONCE_UB comment in sign.c. */
261 nonce = (uint16_t)(MLDSA_L * nonce);
262 /* Now, nonce <= UINT16_MAX - (MLDSA_L - 1), so the casts below are safe. */
263 #if defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
264 for (i = 0; i < MLDSA_L; i++)
265 {
266 mld_poly_uniform_gamma1(&v->vec[i], seed, (uint16_t)(nonce + i));
267 }
268 #else /* MLD_CONFIG_SERIAL_FIPS202_ONLY */
269 #if MLDSA_L == 4
270 mld_poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2], &v->vec[3],
271 seed, nonce, (uint16_t)(nonce + 1),
272 (uint16_t)(nonce + 2), (uint16_t)(nonce + 3));
273 #elif MLDSA_L == 5
274 mld_poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2], &v->vec[3],
275 seed, nonce, (uint16_t)(nonce + 1),
276 (uint16_t)(nonce + 2), (uint16_t)(nonce + 3));
277 mld_poly_uniform_gamma1(&v->vec[4], seed, (uint16_t)(nonce + 4));
278 #elif MLDSA_L == 7
279 mld_poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2],
280 &v->vec[3 /* irrelevant */], seed, nonce,
281 (uint16_t)(nonce + 1), (uint16_t)(nonce + 2),
282 0xFF /* irrelevant */);
283 mld_poly_uniform_gamma1_4x(&v->vec[3], &v->vec[4], &v->vec[5], &v->vec[6],
284 seed, (uint16_t)(nonce + 3), (uint16_t)(nonce + 4),
285 (uint16_t)(nonce + 5), (uint16_t)(nonce + 6));
286 #endif /* MLDSA_L == 7 */
287 #endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY */
288
289 mld_assert_bound_2d(v->vec, MLDSA_L, MLDSA_N, -(MLDSA_GAMMA1 - 1),
290 MLDSA_GAMMA1 + 1);
291 }
292
293 MLD_INTERNAL_API
294 void mld_polyvecl_ntt(mld_polyvecl *v)
295 {
296 unsigned int i;
297 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLDSA_Q);
298
299 for (i = 0; i < MLDSA_L; ++i)
300 __loop__(
301 assigns(i, memory_slice(v, sizeof(mld_polyvecl)))
302 invariant(i <= MLDSA_L)
303 invariant(forall(k0, i, MLDSA_L, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
304 invariant(forall(k1, 0, i, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))))
305 {
306 mld_poly_ntt(&v->vec[i]);
307 }
308
309 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLD_NTT_BOUND);
310 }
311
312 MLD_STATIC_TESTABLE void mld_polyvecl_pointwise_acc_montgomery_c(
313 mld_poly *w, const mld_polyvecl *u, const mld_polyvecl *v)
314 __contract__(
315 requires(memory_no_alias(w, sizeof(mld_poly)))
316 requires(memory_no_alias(u, sizeof(mld_polyvecl)))
317 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
318 requires(forall(l0, 0, MLDSA_L,
319 array_bound(u->vec[l0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
320 requires(forall(l1, 0, MLDSA_L,
321 array_abs_bound(v->vec[l1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
322 assigns(memory_slice(w, sizeof(mld_poly)))
323 ensures(array_abs_bound(w->coeffs, 0, MLDSA_N, MLDSA_Q))
324 )
325 {
326 unsigned int i, j;
327 mld_assert_bound_2d(u->vec, MLDSA_L, MLDSA_N, 0, MLDSA_Q);
328 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLD_NTT_BOUND);
329 for (i = 0; i < MLDSA_N; i++)
330 __loop__(
331 assigns(i, j, memory_slice(w, sizeof(mld_poly)))
332 invariant(i <= MLDSA_N)
333 invariant(array_abs_bound(w->coeffs, 0, i, MLDSA_Q))
334 )
335 {
336 int64_t t = 0;
337 int32_t r;
338 for (j = 0; j < MLDSA_L; j++)
339 __loop__(
340 assigns(j, t)
341 invariant(j <= MLDSA_L)
342 invariant(t >= -(int64_t)j*(MLDSA_Q - 1)*(MLD_NTT_BOUND - 1))
343 invariant(t <= (int64_t)j*(MLDSA_Q - 1)*(MLD_NTT_BOUND - 1))
344 )
345 {
346 t += (int64_t)u->vec[j].coeffs[i] * v->vec[j].coeffs[i];
347 }
348
349 r = mld_montgomery_reduce(t);
350 w->coeffs[i] = r;
351 }
352
353 mld_assert_abs_bound(w->coeffs, MLDSA_N, MLDSA_Q);
354 }
355
356 MLD_INTERNAL_API
357 void mld_polyvecl_pointwise_acc_montgomery(mld_poly *w, const mld_polyvecl *u,
358 const mld_polyvecl *v)
359 {
360 #if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4) && \
361 MLD_CONFIG_PARAMETER_SET == 44
362 int ret;
363 mld_assert_bound_2d(u->vec, MLDSA_L, MLDSA_N, 0, MLDSA_Q);
364 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLD_NTT_BOUND);
365 ret = mld_polyvecl_pointwise_acc_montgomery_l4_native(
366 w->coeffs, (const int32_t(*)[MLDSA_N])u->vec,
367 (const int32_t(*)[MLDSA_N])v->vec);
368 if (ret == MLD_NATIVE_FUNC_SUCCESS)
369 {
370 mld_assert_abs_bound(w->coeffs, MLDSA_N, MLDSA_Q);
371 return;
372 }
373 #elif defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5) && \
374 MLD_CONFIG_PARAMETER_SET == 65
375 int ret;
376 mld_assert_bound_2d(u->vec, MLDSA_L, MLDSA_N, 0, MLDSA_Q);
377 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLD_NTT_BOUND);
378 ret = mld_polyvecl_pointwise_acc_montgomery_l5_native(
379 w->coeffs, (const int32_t(*)[MLDSA_N])u->vec,
380 (const int32_t(*)[MLDSA_N])v->vec);
381 if (ret == MLD_NATIVE_FUNC_SUCCESS)
382 {
383 mld_assert_abs_bound(w->coeffs, MLDSA_N, MLDSA_Q);
384 return;
385 }
386 #elif defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7) && \
387 MLD_CONFIG_PARAMETER_SET == 87
388 int ret;
389 mld_assert_bound_2d(u->vec, MLDSA_L, MLDSA_N, 0, MLDSA_Q);
390 mld_assert_abs_bound_2d(v->vec, MLDSA_L, MLDSA_N, MLD_NTT_BOUND);
391 ret = mld_polyvecl_pointwise_acc_montgomery_l7_native(
392 w->coeffs, (const int32_t(*)[MLDSA_N])u->vec,
393 (const int32_t(*)[MLDSA_N])v->vec);
394 if (ret == MLD_NATIVE_FUNC_SUCCESS)
395 {
396 mld_assert_abs_bound(w->coeffs, MLDSA_N, MLDSA_Q);
397 return;
398 }
399 #endif /* !(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4 && \
400 MLD_CONFIG_PARAMETER_SET == 44) && \
401 !(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5 && \
402 MLD_CONFIG_PARAMETER_SET == 65) && \
403 MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7 && \
404 MLD_CONFIG_PARAMETER_SET == 87 */
405 /* The first input is bounded by [0, Q-1] inclusive
406 * The second input is bounded by [-9Q+1, 9Q-1] inclusive . Hence, we can
407 * safely accumulate in 64-bits without intermediate reductions as
408 * MLDSA_L * (MLD_NTT_BOUND-1) * (Q-1) < INT64_MAX
409 *
410 * The worst case is ML-DSA-87: 7 * (9Q-1) * (Q-1) < 2**52
411 * (and likewise for negative values)
412 */
413 mld_polyvecl_pointwise_acc_montgomery_c(w, u, v);
414 }
415
416 MLD_INTERNAL_API
417 uint32_t mld_polyvecl_chknorm(const mld_polyvecl *v, int32_t bound)
418 {
419 unsigned int i;
420 uint32_t t = 0;
421 mld_assert_bound_2d(v->vec, MLDSA_L, MLDSA_N, -MLD_REDUCE32_RANGE_MAX,
422 MLD_REDUCE32_RANGE_MAX);
423
424 for (i = 0; i < MLDSA_L; ++i)
425 __loop__(
426 invariant(i <= MLDSA_L)
427 invariant(t == 0 || t == 0xFFFFFFFF)
428 invariant((t == 0) == forall(k1, 0, i, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, bound)))
429 )
430 {
431 /* Reference: Leaks which polynomial violates the bound via a conditional.
432 * We are more conservative to reduce the number of declassifications in
433 * constant-time testing.
434 */
435 t |= mld_poly_chknorm(&v->vec[i], bound);
436 }
437 return t;
438 }
439
440 /**************************************************************/
441 /************ Vectors of polynomials of length MLDSA_K **************/
442 /**************************************************************/
443 MLD_INTERNAL_API
444 void mld_polyveck_reduce(mld_polyveck *v)
445 {
446 unsigned int i;
447 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, INT32_MIN,
448 MLD_REDUCE32_DOMAIN_MAX);
449
450 for (i = 0; i < MLDSA_K; ++i)
451 __loop__(
452 assigns(i, memory_slice(v, sizeof(mld_polyveck)))
453 invariant(i <= MLDSA_K)
454 invariant(forall(k0, i, MLDSA_K, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
455 invariant(forall(k2, 0, i,
456 array_bound(v->vec[k2].coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
457 )
458 {
459 mld_poly_reduce(&v->vec[i]);
460 }
461
462 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, -MLD_REDUCE32_RANGE_MAX,
463 MLD_REDUCE32_RANGE_MAX);
464 }
465
466 MLD_INTERNAL_API
467 void mld_polyveck_caddq(mld_polyveck *v)
468 {
469 unsigned int i;
470 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
471
472 for (i = 0; i < MLDSA_K; ++i)
473 __loop__(
474 assigns(i, memory_slice(v, sizeof(mld_polyveck)))
475 invariant(i <= MLDSA_K)
476 invariant(forall(k0, i, MLDSA_K, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
477 invariant(forall(k1, 0, i, array_bound(v->vec[k1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
478 {
479 mld_poly_caddq(&v->vec[i]);
480 }
481
482 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, 0, MLDSA_Q);
483 }
484
485 /* Reference: We use destructive version (output=first input) to avoid
486 * reasoning about aliasing in the CBMC specification */
487 MLD_INTERNAL_API
488 void mld_polyveck_add(mld_polyveck *u, const mld_polyveck *v)
489 {
490 unsigned int i;
491
492 for (i = 0; i < MLDSA_K; ++i)
493 __loop__(
494 assigns(i, memory_slice(u, sizeof(mld_polyveck)))
495 invariant(i <= MLDSA_K)
496 invariant(forall(k0, i, MLDSA_K,
497 forall(k1, 0, MLDSA_N, u->vec[k0].coeffs[k1] == loop_entry(*u).vec[k0].coeffs[k1])))
498 invariant(forall(k6, 0, i, array_bound(u->vec[k6].coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)))
499 )
500 {
501 mld_poly_add(&u->vec[i], &v->vec[i]);
502 }
503 mld_assert_bound_2d(u->vec, MLDSA_L, MLDSA_N, INT32_MIN,
504 MLD_REDUCE32_DOMAIN_MAX);
505 }
506
507 MLD_INTERNAL_API
508 void mld_polyveck_sub(mld_polyveck *u, const mld_polyveck *v)
509 {
510 unsigned int i;
511 mld_assert_abs_bound_2d(u->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
512 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
513
514 for (i = 0; i < MLDSA_K; ++i)
515 __loop__(
516 assigns(i, memory_slice(u, sizeof(mld_polyveck)))
517 invariant(i <= MLDSA_K)
518 invariant(forall(k0, 0, i,
519 array_bound(u->vec[k0].coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)))
520 invariant(forall(k1, i, MLDSA_K,
521 forall(n1, 0, MLDSA_N, u->vec[k1].coeffs[n1] == loop_entry(*u).vec[k1].coeffs[n1]))))
522 {
523 mld_poly_sub(&u->vec[i], &v->vec[i]);
524 }
525
526 mld_assert_bound_2d(u->vec, MLDSA_K, MLDSA_N, INT32_MIN,
527 MLD_REDUCE32_DOMAIN_MAX);
528 }
529
530 MLD_INTERNAL_API
531 void mld_polyveck_shiftl(mld_polyveck *v)
532 {
533 unsigned int i;
534 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, 0, 1 << 10);
535
536 for (i = 0; i < MLDSA_K; ++i)
537 __loop__(
538 assigns(i, memory_slice(v, sizeof(mld_polyveck)))
539 invariant(i <= MLDSA_K)
540 invariant(forall(k1, 0, i, array_bound(v->vec[k1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
541 invariant(forall(k1, i, MLDSA_K,
542 forall(n1, 0, MLDSA_N, v->vec[k1].coeffs[n1] == loop_entry(*v).vec[k1].coeffs[n1])))
543 )
544 {
545 mld_poly_shiftl(&v->vec[i]);
546 }
547
548 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, 0, MLDSA_Q);
549 }
550
551 MLD_INTERNAL_API
552 void mld_polyveck_ntt(mld_polyveck *v)
553 {
554 unsigned int i;
555 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
556
557 for (i = 0; i < MLDSA_K; ++i)
558 __loop__(
559 assigns(i, memory_slice(v, sizeof(mld_polyveck)))
560 invariant(i <= MLDSA_K)
561 invariant(forall(k0, i, MLDSA_K, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
562 invariant(forall(k1, 0, i, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND))))
563 {
564 mld_poly_ntt(&v->vec[i]);
565 }
566 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLD_NTT_BOUND);
567 }
568
569 MLD_INTERNAL_API
570 void mld_polyveck_invntt_tomont(mld_polyveck *v)
571 {
572 unsigned int i;
573 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
574
575 for (i = 0; i < MLDSA_K; ++i)
576 __loop__(
577 assigns(i, memory_slice(v, sizeof(mld_polyveck)))
578 invariant(i <= MLDSA_K)
579 invariant(forall(k0, i, MLDSA_K, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
580 invariant(forall(k1, 0, i, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_INTT_BOUND))))
581 {
582 mld_poly_invntt_tomont(&v->vec[i]);
583 }
584
585 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLD_INTT_BOUND);
586 }
587
588 MLD_INTERNAL_API
589 void mld_polyveck_pointwise_poly_montgomery(mld_polyveck *r, const mld_poly *a,
590 const mld_polyveck *v)
591 {
592 unsigned int i;
593 mld_assert_abs_bound_2d(v->vec, MLDSA_K, MLDSA_N, MLD_NTT_BOUND);
594
595 for (i = 0; i < MLDSA_K; ++i)
596 __loop__(
597 assigns(i, memory_slice(r, sizeof(mld_polyveck)))
598 invariant(i <= MLDSA_K)
599 invariant(forall(k2, 0, i, array_abs_bound(r->vec[k2].coeffs, 0, MLDSA_N, MLDSA_Q)))
600 )
601 {
602 mld_poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]);
603 }
604 mld_assert_abs_bound_2d(r->vec, MLDSA_K, MLDSA_N, MLDSA_Q);
605 }
606
607 MLD_INTERNAL_API
608 uint32_t mld_polyveck_chknorm(const mld_polyveck *v, int32_t bound)
609 {
610 unsigned int i;
611 uint32_t t = 0;
612 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, -MLD_REDUCE32_RANGE_MAX,
613 MLD_REDUCE32_RANGE_MAX);
614
615 for (i = 0; i < MLDSA_K; ++i)
616 __loop__(
617 invariant(i <= MLDSA_K)
618 invariant(t == 0 || t == 0xFFFFFFFF)
619 invariant((t == 0) == forall(k1, 0, i, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, bound)))
620 )
621 {
622 /* Reference: Leaks which polynomial violates the bound via a conditional.
623 * We are more conservative to reduce the number of declassifications in
624 * constant-time testing.
625 */
626 t |= mld_poly_chknorm(&v->vec[i], bound);
627 }
628
629 return t;
630 }
631
632 MLD_INTERNAL_API
633 void mld_polyveck_power2round(mld_polyveck *v1, mld_polyveck *v0,
634 const mld_polyveck *v)
635 {
636 unsigned int i;
637 mld_assert_bound_2d(v->vec, MLDSA_K, MLDSA_N, 0, MLDSA_Q);
638
639 for (i = 0; i < MLDSA_K; ++i)
640 __loop__(
641 assigns(i, memory_slice(v0, sizeof(mld_polyveck)), memory_slice(v1, sizeof(mld_polyveck)))
642 invariant(i <= MLDSA_K)
643 invariant(forall(k1, 0, i, array_bound(v0->vec[k1].coeffs, 0, MLDSA_N, -(MLD_2_POW_D/2)+1, (MLD_2_POW_D/2)+1)))
644 invariant(forall(k2, 0, i, array_bound(v1->vec[k2].coeffs, 0, MLDSA_N, 0, ((MLDSA_Q - 1) / MLD_2_POW_D) + 1)))
645 )
646 {
647 mld_poly_power2round(&v1->vec[i], &v0->vec[i], &v->vec[i]);
648 }
649
650 mld_assert_bound_2d(v0->vec, MLDSA_K, MLDSA_N, -(MLD_2_POW_D / 2) + 1,
651 (MLD_2_POW_D / 2) + 1);
652 mld_assert_bound_2d(v1->vec, MLDSA_K, MLDSA_N, 0,
653 ((MLDSA_Q - 1) / MLD_2_POW_D) + 1);
654 }
655
656 MLD_INTERNAL_API
657 void mld_polyveck_decompose(mld_polyveck *v1, mld_polyveck *v0)
658 {
659 unsigned int i;
660 mld_assert_bound_2d(v0->vec, MLDSA_K, MLDSA_N, 0, MLDSA_Q);
661
662 for (i = 0; i < MLDSA_K; ++i)
663 __loop__(
664 assigns(i, memory_slice(v0, sizeof(mld_polyveck)), memory_slice(v1, sizeof(mld_polyveck)))
665 invariant(i <= MLDSA_K)
666 invariant(forall(k1, 0, i,
667 array_bound(v1->vec[k1].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
668 invariant(forall(k2, 0, i,
669 array_abs_bound(v0->vec[k2].coeffs, 0, MLDSA_N, MLDSA_GAMMA2+1)))
670 invariant(forall(k3, i, MLDSA_K,
671 array_bound(v0->vec[k3].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
672 )
673 {
674 mld_poly_decompose(&v1->vec[i], &v0->vec[i]);
675 }
676
677 mld_assert_bound_2d(v1->vec, MLDSA_K, MLDSA_N, 0,
678 (MLDSA_Q - 1) / (2 * MLDSA_GAMMA2));
679 mld_assert_abs_bound_2d(v0->vec, MLDSA_K, MLDSA_N, MLDSA_GAMMA2 + 1);
680 }
681
682 MLD_INTERNAL_API
683 unsigned int mld_polyveck_make_hint(mld_polyveck *h, const mld_polyveck *v0,
684 const mld_polyveck *v1)
685 {
686 unsigned int i, s = 0;
687
688 for (i = 0; i < MLDSA_K; ++i)
689 __loop__(
690 assigns(i, s, memory_slice(h, sizeof(mld_polyveck)))
691 invariant(i <= MLDSA_K)
692 invariant(s <= i * MLDSA_N)
693 invariant(forall(k1, 0, i, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
694 )
695 {
696 s += mld_poly_make_hint(&h->vec[i], &v0->vec[i], &v1->vec[i]);
697 }
698
699 mld_assert_bound_2d(h->vec, MLDSA_K, MLDSA_N, 0, 2);
700 return s;
701 }
702
703 MLD_INTERNAL_API
704 void mld_polyveck_use_hint(mld_polyveck *w, const mld_polyveck *u,
705 const mld_polyveck *h)
706 {
707 unsigned int i;
708 mld_assert_bound_2d(u->vec, MLDSA_K, MLDSA_N, 0, MLDSA_Q);
709 mld_assert_bound_2d(h->vec, MLDSA_K, MLDSA_N, 0, 2);
710
711 for (i = 0; i < MLDSA_K; ++i)
712 __loop__(
713 assigns(i, memory_slice(w, sizeof(mld_polyveck)))
714 invariant(i <= MLDSA_K)
715 invariant(forall(k2, 0, i,
716 array_bound(w->vec[k2].coeffs, 0, MLDSA_N, 0,
717 (MLDSA_Q - 1) / (2 * MLDSA_GAMMA2))))
718 )
719 {
720 mld_poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i]);
721 }
722
723 mld_assert_bound_2d(w->vec, MLDSA_K, MLDSA_N, 0,
724 (MLDSA_Q - 1) / (2 * MLDSA_GAMMA2));
725 }
726
727 MLD_INTERNAL_API
728 void mld_polyveck_pack_w1(uint8_t r[MLDSA_K * MLDSA_POLYW1_PACKEDBYTES],
729 const mld_polyveck *w1)
730 {
731 unsigned int i;
732 mld_assert_bound_2d(w1->vec, MLDSA_K, MLDSA_N, 0,
733 (MLDSA_Q - 1) / (2 * MLDSA_GAMMA2));
734
735 for (i = 0; i < MLDSA_K; ++i)
736 __loop__(
737 assigns(i, memory_slice(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
738 invariant(i <= MLDSA_K)
739 )
740 {
741 mld_polyw1_pack(&r[i * MLDSA_POLYW1_PACKEDBYTES], &w1->vec[i]);
742 }
743 }
744
745 MLD_INTERNAL_API
746 void mld_polyveck_pack_eta(uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES],
747 const mld_polyveck *p)
748 {
749 unsigned int i;
750 mld_assert_abs_bound_2d(p->vec, MLDSA_K, MLDSA_N, MLDSA_ETA + 1);
751 for (i = 0; i < MLDSA_K; ++i)
752 __loop__(
753 assigns(i, memory_slice(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
754 invariant(i <= MLDSA_K)
755 )
756 {
757 mld_polyeta_pack(&r[i * MLDSA_POLYETA_PACKEDBYTES], &p->vec[i]);
758 }
759 }
760
761 MLD_INTERNAL_API
762 void mld_polyvecl_pack_eta(uint8_t r[MLDSA_L * MLDSA_POLYETA_PACKEDBYTES],
763 const mld_polyvecl *p)
764 {
765 unsigned int i;
766 mld_assert_abs_bound_2d(p->vec, MLDSA_L, MLDSA_N, MLDSA_ETA + 1);
767 for (i = 0; i < MLDSA_L; ++i)
768 __loop__(
769 assigns(i, memory_slice(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
770 invariant(i <= MLDSA_L)
771 )
772 {
773 mld_polyeta_pack(&r[i * MLDSA_POLYETA_PACKEDBYTES], &p->vec[i]);
774 }
775 }
776
777 MLD_INTERNAL_API
778 void mld_polyveck_pack_t0(uint8_t r[MLDSA_K * MLDSA_POLYT0_PACKEDBYTES],
779 const mld_polyveck *p)
780 {
781 unsigned int i;
782 mld_assert_bound_2d(p->vec, MLDSA_K, MLDSA_N, -(1 << (MLDSA_D - 1)) + 1,
783 (1 << (MLDSA_D - 1)) + 1);
784 for (i = 0; i < MLDSA_K; ++i)
785 __loop__(
786 assigns(i, memory_slice(r, MLDSA_K * MLDSA_POLYT0_PACKEDBYTES))
787 invariant(i <= MLDSA_K)
788 )
789 {
790 mld_polyt0_pack(&r[i * MLDSA_POLYT0_PACKEDBYTES], &p->vec[i]);
791 }
792 }
793
794 MLD_INTERNAL_API
795 void mld_polyvecl_unpack_eta(
796 mld_polyvecl *p, const uint8_t r[MLDSA_L * MLDSA_POLYETA_PACKEDBYTES])
797 {
798 unsigned int i;
799 for (i = 0; i < MLDSA_L; ++i)
800 {
801 mld_polyeta_unpack(&p->vec[i], r + i * MLDSA_POLYETA_PACKEDBYTES);
802 }
803
804 mld_assert_bound_2d(p->vec, MLDSA_L, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND,
805 MLDSA_ETA + 1);
806 }
807
808 MLD_INTERNAL_API
809 void mld_polyvecl_unpack_z(mld_polyvecl *z,
810 const uint8_t r[MLDSA_L * MLDSA_POLYZ_PACKEDBYTES])
811 {
812 unsigned int i;
813 for (i = 0; i < MLDSA_L; ++i)
814 {
815 mld_polyz_unpack(&z->vec[i], r + i * MLDSA_POLYZ_PACKEDBYTES);
816 }
817
818 mld_assert_bound_2d(z->vec, MLDSA_L, MLDSA_N, -(MLDSA_GAMMA1 - 1),
819 MLDSA_GAMMA1 + 1);
820 }
821
822 MLD_INTERNAL_API
823 void mld_polyveck_unpack_eta(
824 mld_polyveck *p, const uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES])
825 {
826 unsigned int i;
827 for (i = 0; i < MLDSA_K; ++i)
828 {
829 mld_polyeta_unpack(&p->vec[i], r + i * MLDSA_POLYETA_PACKEDBYTES);
830 }
831
832 mld_assert_bound_2d(p->vec, MLDSA_K, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND,
833 MLDSA_ETA + 1);
834 }
835
836 MLD_INTERNAL_API
837 void mld_polyveck_unpack_t0(mld_polyveck *p,
838 const uint8_t r[MLDSA_K * MLDSA_POLYT0_PACKEDBYTES])
839 {
840 unsigned int i;
841 for (i = 0; i < MLDSA_K; ++i)
842 {
843 mld_polyt0_unpack(&p->vec[i], r + i * MLDSA_POLYT0_PACKEDBYTES);
844 }
845
846 mld_assert_bound_2d(p->vec, MLDSA_K, MLDSA_N, -(1 << (MLDSA_D - 1)) + 1,
847 (1 << (MLDSA_D - 1)) + 1);
848 }
849
850 /* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
851 * Don't modify by hand -- this is auto-generated by scripts/autogen. */
852 #undef mld_polymat_permute_bitrev_to_custom
853 #undef mld_polyvecl_permute_bitrev_to_custom
854 #undef mld_polyvecl_pointwise_acc_montgomery_c