Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4 */
5 #ifndef MLD_POLYVEC_H
6 #define MLD_POLYVEC_H
7
8 #include <stdint.h>
9 #include "cbmc.h"
10 #include "common.h"
11 #include "poly.h"
12 #include "poly_kl.h"
13
14 /* Parameter set namespacing
15 * This is to facilitate building multiple instances
16 * of mldsa-native (e.g. with varying parameter sets)
17 * within a single compilation unit. */
18 #define mld_polyvecl MLD_ADD_PARAM_SET(mld_polyvecl)
19 #define mld_polyveck MLD_ADD_PARAM_SET(mld_polyveck)
20 #define mld_polymat MLD_ADD_PARAM_SET(mld_polymat)
21 /* End of parameter set namespacing */
22
23 /* Vectors of polynomials of length MLDSA_L */
24 typedef struct
25 {
26 mld_poly vec[MLDSA_L];
27 } mld_polyvecl;
28
29
30 #define mld_polyvecl_uniform_gamma1 MLD_NAMESPACE_KL(polyvecl_uniform_gamma1)
31 /*************************************************
32 * Name: mld_polyvecl_uniform_gamma1
33 *
34 * Description: Sample vector of polynomials with uniformly random coefficients
35 * in [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] by unpacking output
36 * stream of SHAKE256(seed|nonce)
37 *
38 * Arguments: - mld_polyvecl *v: pointer to output vector
39 * - const uint8_t seed[]: byte array with seed of length
40 * MLDSA_CRHBYTES
41 * - uint16_t nonce: 16-bit nonce
42 *************************************************/
43 MLD_INTERNAL_API
44 void mld_polyvecl_uniform_gamma1(mld_polyvecl *v,
45 const uint8_t seed[MLDSA_CRHBYTES],
46 uint16_t nonce)
47 __contract__(
48 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
49 requires(memory_no_alias(seed, MLDSA_CRHBYTES))
50 requires(nonce <= (UINT16_MAX - MLDSA_L) / MLDSA_L)
51 assigns(memory_slice(v, sizeof(mld_polyvecl)))
52 ensures(forall(k0, 0, MLDSA_L,
53 array_bound(v->vec[k0].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1)))
54 );
55
56 #define mld_polyvecl_ntt MLD_NAMESPACE_KL(polyvecl_ntt)
57 /*************************************************
58 * Name: mld_polyvecl_ntt
59 *
60 * Description: Forward NTT of all polynomials in vector of length MLDSA_L.
61 * Coefficients can grow by 8*MLDSA_Q in absolute value.
62 *
63 * Arguments: - mld_polyvecl *v: pointer to input/output vector
64 **************************************************/
65 MLD_INTERNAL_API
66 void mld_polyvecl_ntt(mld_polyvecl *v)
67 __contract__(
68 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
69 requires(forall(k0, 0, MLDSA_L, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
70 assigns(memory_slice(v, sizeof(mld_polyvecl)))
71 ensures(forall(k1, 0, MLDSA_L, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
72 );
73
74 #define mld_polyvecl_pointwise_acc_montgomery \
75 MLD_NAMESPACE_KL(polyvecl_pointwise_acc_montgomery)
76 /*************************************************
77 * Name: mld_polyvecl_pointwise_acc_montgomery
78 *
79 * Description: Pointwise multiply vectors of polynomials of length MLDSA_L,
80 * multiply resulting vector by 2^{-32} and add (accumulate)
81 * polynomials in it.
82 * Input/output vectors are in NTT domain representation.
83 *
84 * The first input "u" must be the output of
85 * polyvec_matrix_expand() and so have coefficients in [0, Q-1]
86 * inclusive.
87 *
88 * The second input "v" is assumed to be output of an NTT, and
89 * hence must have coefficients bounded by [-9q+1, +9q-1]
90 * inclusive.
91 *
92 *
93 * Arguments: - mld_poly *w: output polynomial
94 * - const mld_polyvecl *u: pointer to first input vector
95 * - const mld_polyvecl *v: pointer to second input vector
96 **************************************************/
97 MLD_INTERNAL_API
98 void mld_polyvecl_pointwise_acc_montgomery(mld_poly *w, const mld_polyvecl *u,
99 const mld_polyvecl *v)
100 __contract__(
101 requires(memory_no_alias(w, sizeof(mld_poly)))
102 requires(memory_no_alias(u, sizeof(mld_polyvecl)))
103 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
104 requires(forall(l0, 0, MLDSA_L,
105 array_bound(u->vec[l0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
106 requires(forall(l1, 0, MLDSA_L,
107 array_abs_bound(v->vec[l1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
108 assigns(memory_slice(w, sizeof(mld_poly)))
109 ensures(array_abs_bound(w->coeffs, 0, MLDSA_N, MLDSA_Q))
110 );
111
112
113 #define mld_polyvecl_chknorm MLD_NAMESPACE_KL(polyvecl_chknorm)
114 /*************************************************
115 * Name: mld_polyvecl_chknorm
116 *
117 * Description: Check infinity norm of polynomials in vector of length MLDSA_L.
118 * Assumes input mld_polyvecl to be reduced by polyvecl_reduce().
119 *
120 * Arguments: - const mld_polyvecl *v: pointer to vector
121 * - int32_t B: norm bound
122 *
123 * Returns 0 if norm of all polynomials is strictly smaller than B <=
124 * (MLDSA_Q-1)/8 and 0xFFFFFFFF otherwise.
125 **************************************************/
126 MLD_INTERNAL_API
127 MLD_MUST_CHECK_RETURN_VALUE
128 uint32_t mld_polyvecl_chknorm(const mld_polyvecl *v, int32_t B)
129 __contract__(
130 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
131 requires(0 <= B && B <= (MLDSA_Q - 1) / 8)
132 requires(forall(k0, 0, MLDSA_L,
133 array_bound(v->vec[k0].coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
134 ensures(return_value == 0 || return_value == 0xFFFFFFFF)
135 ensures((return_value == 0) == forall(k1, 0, MLDSA_L, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, B)))
136 );
137
138 /* Vectors of polynomials of length MLDSA_K */
139 typedef struct
140 {
141 mld_poly vec[MLDSA_K];
142 } mld_polyveck;
143
144 /* Matrix of polynomials (K x L) */
145 typedef struct
146 {
147 #if defined(MLD_CONFIG_REDUCE_RAM)
148 mld_polyvecl row_buffer;
149 uint8_t rho[MLDSA_SEEDBYTES];
150 #else
151 mld_polyvecl vec[MLDSA_K];
152 #endif
153 } mld_polymat;
154
155 #define mld_polyveck_reduce MLD_NAMESPACE_KL(polyveck_reduce)
156 /*************************************************
157 * Name: polyveck_reduce
158 *
159 * Description: Reduce coefficients of polynomials in vector of length MLDSA_K
160 * to representatives in
161 *[-MLD_REDUCE32_RANGE_MAX,MLD_REDUCE32_RANGE_MAX].
162 *
163 * Arguments: - mld_polyveck *v: pointer to input/output vector
164 **************************************************/
165 MLD_INTERNAL_API
166 void mld_polyveck_reduce(mld_polyveck *v)
167 __contract__(
168 requires(memory_no_alias(v, sizeof(mld_polyveck)))
169 requires(forall(k0, 0, MLDSA_K,
170 array_bound(v->vec[k0].coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)))
171 assigns(memory_slice(v, sizeof(mld_polyveck)))
172 ensures(forall(k1, 0, MLDSA_K,
173 array_bound(v->vec[k1].coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
174 );
175
176 #define mld_polyveck_caddq MLD_NAMESPACE_KL(polyveck_caddq)
177 /*************************************************
178 * Name: mld_polyveck_caddq
179 *
180 * Description: For all coefficients of polynomials in vector of length MLDSA_K
181 * add MLDSA_Q if coefficient is negative.
182 *
183 * Arguments: - mld_polyveck *v: pointer to input/output vector
184 **************************************************/
185 MLD_INTERNAL_API
186 void mld_polyveck_caddq(mld_polyveck *v)
187 __contract__(
188 requires(memory_no_alias(v, sizeof(mld_polyveck)))
189 requires(forall(k0, 0, MLDSA_K,
190 array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
191 assigns(memory_slice(v, sizeof(mld_polyveck)))
192 ensures(forall(k1, 0, MLDSA_K,
193 array_bound(v->vec[k1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
194 );
195
196 #define mld_polyveck_add MLD_NAMESPACE_KL(polyveck_add)
197 /*************************************************
198 * Name: mld_polyveck_add
199 *
200 * Description: Add vectors of polynomials of length MLDSA_K.
201 * No modular reduction is performed.
202 *
203 * Arguments: - mld_polyveck *u: pointer to input-output vector of polynomials
204 * to be added to
205 * - const mld_polyveck *v: pointer to second input vector of
206 * polynomials
207 **************************************************/
208 MLD_INTERNAL_API
209 void mld_polyveck_add(mld_polyveck *u, const mld_polyveck *v)
210 __contract__(
211 requires(memory_no_alias(u, sizeof(mld_polyveck)))
212 requires(memory_no_alias(v, sizeof(mld_polyveck)))
213 requires(forall(p0, 0, MLDSA_K, array_abs_bound(u->vec[p0].coeffs, 0, MLDSA_N, MLD_INTT_BOUND)))
214 requires(forall(p1, 0, MLDSA_K,
215 array_bound(v->vec[p1].coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
216 assigns(memory_slice(u, sizeof(mld_polyveck)))
217 ensures(forall(q2, 0, MLDSA_K,
218 array_bound(u->vec[q2].coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)))
219 );
220
221 #define mld_polyveck_sub MLD_NAMESPACE_KL(polyveck_sub)
222 /*************************************************
223 * Name: mld_polyveck_sub
224 *
225 * Description: Subtract vectors of polynomials of length MLDSA_K.
226 * No modular reduction is performed.
227 *
228 * Arguments: - mld_polyveck *u: pointer to first input vector
229 * - const mld_polyveck *v: pointer to second input vector to be
230 * subtracted from first input vector
231 **************************************************/
232 MLD_INTERNAL_API
233 void mld_polyveck_sub(mld_polyveck *u, const mld_polyveck *v)
234 __contract__(
235 requires(memory_no_alias(u, sizeof(mld_polyveck)))
236 requires(memory_no_alias(v, sizeof(mld_polyveck)))
237 requires(forall(k0, 0, MLDSA_K, array_abs_bound(u->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
238 requires(forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLDSA_Q)))
239 assigns(memory_slice(u, sizeof(mld_polyveck)))
240 ensures(forall(k0, 0, MLDSA_K,
241 array_bound(u->vec[k0].coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)))
242 );
243
244 #define mld_polyveck_shiftl MLD_NAMESPACE_KL(polyveck_shiftl)
245 /*************************************************
246 * Name: mld_polyveck_shiftl
247 *
248 * Description: Multiply vector of polynomials of Length MLDSA_K by 2^MLDSA_D
249 *without modular reduction. Assumes input coefficients to be less than
250 *2^{31-MLDSA_D}.
251 *
252 * Arguments: - mld_polyveck *v: pointer to input/output vector
253 **************************************************/
254 MLD_INTERNAL_API
255 void mld_polyveck_shiftl(mld_polyveck *v)
256 __contract__(
257 requires(memory_no_alias(v, sizeof(mld_polyveck)))
258 requires(forall(k0, 0, MLDSA_K, array_bound(v->vec[k0].coeffs, 0, MLDSA_N, 0, 1 << 10)))
259 assigns(memory_slice(v, sizeof(mld_polyveck)))
260 ensures(forall(k1, 0, MLDSA_K, array_bound(v->vec[k1].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
261 );
262
263 #define mld_polyveck_ntt MLD_NAMESPACE_KL(polyveck_ntt)
264 /*************************************************
265 * Name: mld_polyveck_ntt
266 *
267 * Description: Forward NTT of all polynomials in vector of length MLDSA_K.
268 * Coefficients can grow by 8*MLDSA_Q in absolute value.
269 *
270 * Arguments: - mld_polyveck *v: pointer to input/output vector
271 **************************************************/
272 MLD_INTERNAL_API
273 void mld_polyveck_ntt(mld_polyveck *v)
274 __contract__(
275 requires(memory_no_alias(v, sizeof(mld_polyveck)))
276 requires(forall(k0, 0, MLDSA_K, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
277 assigns(memory_slice(v, sizeof(mld_polyveck)))
278 ensures(forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
279 );
280
281 #define mld_polyveck_invntt_tomont MLD_NAMESPACE_KL(polyveck_invntt_tomont)
282 /*************************************************
283 * Name: mld_polyveck_invntt_tomont
284 *
285 * Description: Inverse NTT and multiplication by 2^{32} of polynomials
286 * in vector of length MLDSA_K.
287 * Input coefficients need to be less than MLDSA_Q, and
288 * Output coefficients are bounded by MLD_INTT_BOUND.
289 * Arguments: - mld_polyveck *v: pointer to input/output vector
290 **************************************************/
291 MLD_INTERNAL_API
292 void mld_polyveck_invntt_tomont(mld_polyveck *v)
293 __contract__(
294 requires(memory_no_alias(v, sizeof(mld_polyveck)))
295 requires(forall(k0, 0, MLDSA_K, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
296 assigns(memory_slice(v, sizeof(mld_polyveck)))
297 ensures(forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLD_INTT_BOUND)))
298 );
299
300 #define mld_polyveck_pointwise_poly_montgomery \
301 MLD_NAMESPACE_KL(polyveck_pointwise_poly_montgomery)
302 /*************************************************
303 * Name: mld_polyveck_pointwise_poly_montgomery
304 *
305 * Description: Pointwise multiplication of a polynomial vector of length
306 * MLDSA_K by a single polynomial in NTT domain and multiplication
307 * of the resulting polynomial vector by 2^{-32}.
308 *
309 * Arguments: - mld_polyveck *r: pointer to output vector
310 * - mld_poly *a: pointer to input polynomial
311 * - mld_polyveck *v: pointer to input vector
312 **************************************************/
313 MLD_INTERNAL_API
314 void mld_polyveck_pointwise_poly_montgomery(mld_polyveck *r, const mld_poly *a,
315 const mld_polyveck *v)
316 __contract__(
317 requires(memory_no_alias(r, sizeof(mld_polyveck)))
318 requires(memory_no_alias(a, sizeof(mld_poly)))
319 requires(memory_no_alias(v, sizeof(mld_polyveck)))
320 requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
321 requires(forall(k0, 0, MLDSA_K, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
322 assigns(memory_slice(r, sizeof(mld_polyveck)))
323 ensures(forall(k1, 0, MLDSA_K, array_abs_bound(r->vec[k1].coeffs, 0, MLDSA_N, MLDSA_Q)))
324 );
325
326 #define mld_polyveck_chknorm MLD_NAMESPACE_KL(polyveck_chknorm)
327 /*************************************************
328 * Name: mld_polyveck_chknorm
329 *
330 * Description: Check infinity norm of polynomials in vector of length MLDSA_K.
331 * Assumes input mld_polyveck to be reduced by polyveck_reduce().
332 *
333 * Arguments: - const mld_polyveck *v: pointer to vector
334 * - int32_t B: norm bound
335 *
336 * Returns 0 if norm of all polynomials are strictly smaller than B <=
337 *(MLDSA_Q-1)/8 and 0xFFFFFFFF otherwise.
338 **************************************************/
339 MLD_INTERNAL_API
340 MLD_MUST_CHECK_RETURN_VALUE
341 uint32_t mld_polyveck_chknorm(const mld_polyveck *v, int32_t B)
342 __contract__(
343 requires(memory_no_alias(v, sizeof(mld_polyveck)))
344 requires(0 <= B && B <= (MLDSA_Q - 1) / 8)
345 requires(forall(k0, 0, MLDSA_K,
346 array_bound(v->vec[k0].coeffs, 0, MLDSA_N,
347 -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)))
348 ensures(return_value == 0 || return_value == 0xFFFFFFFF)
349 ensures((return_value == 0) == forall(k1, 0, MLDSA_K, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, B)))
350 );
351
352 #define mld_polyveck_power2round MLD_NAMESPACE_KL(polyveck_power2round)
353 /*************************************************
354 * Name: mld_polyveck_power2round
355 *
356 * Description: For all coefficients a of polynomials in vector of length
357 *MLDSA_K, compute a0, a1 such that a mod^+ MLDSA_Q = a1*2^MLDSA_D + a0 with
358 *-2^{MLDSA_D-1} < a0 <= 2^{MLDSA_D-1}. Assumes coefficients to be standard
359 *representatives.
360 *
361 * Arguments: - mld_polyveck *v1: pointer to output vector of polynomials with
362 * coefficients a1
363 * - mld_polyveck *v0: pointer to output vector of polynomials with
364 * coefficients a0
365 * - const mld_polyveck *v: pointer to input vector
366 **************************************************/
367 MLD_INTERNAL_API
368 void mld_polyveck_power2round(mld_polyveck *v1, mld_polyveck *v0,
369 const mld_polyveck *v)
370 __contract__(
371 requires(memory_no_alias(v1, sizeof(mld_polyveck)))
372 requires(memory_no_alias(v0, sizeof(mld_polyveck)))
373 requires(memory_no_alias(v, sizeof(mld_polyveck)))
374 requires(forall(k0, 0, MLDSA_K, array_bound(v->vec[k0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
375 assigns(memory_slice(v1, sizeof(mld_polyveck)))
376 assigns(memory_slice(v0, sizeof(mld_polyveck)))
377 ensures(forall(k1, 0, MLDSA_K, array_bound(v0->vec[k1].coeffs, 0, MLDSA_N, -(MLD_2_POW_D/2)+1, (MLD_2_POW_D/2)+1)))
378 ensures(forall(k2, 0, MLDSA_K, array_bound(v1->vec[k2].coeffs, 0, MLDSA_N, 0, ((MLDSA_Q - 1) / MLD_2_POW_D) + 1)))
379 );
380
381 #define mld_polyveck_decompose MLD_NAMESPACE_KL(polyveck_decompose)
382 /*************************************************
383 * Name: mld_polyveck_decompose
384 *
385 * Description: For all coefficients a of polynomials in vector of length
386 * MLDSA_K, compute high and low bits a0, a1 such a mod^+ MLDSA_Q = a1*ALPHA
387 * + a0 with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (MLDSA_Q-1)/ALPHA where we set
388 * a1 = 0 and -ALPHA/2 <= a0 = a mod MLDSA_Q - MLDSA_Q < 0. Assumes coefficients
389 * to be standard representatives.
390 *
391 * Arguments: - mld_polyveck *v1: pointer to output vector of polynomials with
392 * coefficients a1
393 * - mld_polyveck *v0: pointer to input/output vector of
394 * polynomials with. Output polynomial has
395 * coefficients a0
396 *
397 * Reference: The reference implementation has the input polynomial as a
398 * separate argument that may be aliased with either of the outputs.
399 * Removing the aliasing eases CBMC proofs.
400 *
401 **************************************************/
402 MLD_INTERNAL_API
403 void mld_polyveck_decompose(mld_polyveck *v1, mld_polyveck *v0)
404 __contract__(
405 requires(memory_no_alias(v1, sizeof(mld_polyveck)))
406 requires(memory_no_alias(v0, sizeof(mld_polyveck)))
407 requires(forall(k0, 0, MLDSA_K,
408 array_bound(v0->vec[k0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
409 assigns(memory_slice(v1, sizeof(mld_polyveck)))
410 assigns(memory_slice(v0, sizeof(mld_polyveck)))
411 ensures(forall(k1, 0, MLDSA_K,
412 array_bound(v1->vec[k1].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
413 ensures(forall(k2, 0, MLDSA_K,
414 array_abs_bound(v0->vec[k2].coeffs, 0, MLDSA_N, MLDSA_GAMMA2+1)))
415 );
416
417 #define mld_polyveck_make_hint MLD_NAMESPACE_KL(polyveck_make_hint)
418 /*************************************************
419 * Name: mld_polyveck_make_hint
420 *
421 * Description: Compute hint vector.
422 *
423 * Arguments: - mld_polyveck *h: pointer to output vector
424 * - const mld_polyveck *v0: pointer to low part of input vector
425 * - const mld_polyveck *v1: pointer to high part of input vector
426 *
427 * Returns number of 1 bits.
428 **************************************************/
429 MLD_INTERNAL_API
430 MLD_MUST_CHECK_RETURN_VALUE
431 unsigned int mld_polyveck_make_hint(mld_polyveck *h, const mld_polyveck *v0,
432 const mld_polyveck *v1)
433 __contract__(
434 requires(memory_no_alias(h, sizeof(mld_polyveck)))
435 requires(memory_no_alias(v0, sizeof(mld_polyveck)))
436 requires(memory_no_alias(v1, sizeof(mld_polyveck)))
437 assigns(memory_slice(h, sizeof(mld_polyveck)))
438 ensures(return_value <= MLDSA_N * MLDSA_K)
439 ensures(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
440 );
441
442 #define mld_polyveck_use_hint MLD_NAMESPACE_KL(polyveck_use_hint)
443 /*************************************************
444 * Name: mld_polyveck_use_hint
445 *
446 * Description: Use hint vector to correct the high bits of input vector.
447 *
448 * Arguments: - mld_polyveck *w: pointer to output vector of polynomials with
449 * corrected high bits
450 * - const mld_polyveck *u: pointer to input vector
451 * - const mld_polyveck *h: pointer to input hint vector
452 **************************************************/
453 MLD_INTERNAL_API
454 void mld_polyveck_use_hint(mld_polyveck *w, const mld_polyveck *v,
455 const mld_polyveck *h)
456 __contract__(
457 requires(memory_no_alias(w, sizeof(mld_polyveck)))
458 requires(memory_no_alias(v, sizeof(mld_polyveck)))
459 requires(memory_no_alias(h, sizeof(mld_polyveck)))
460 requires(forall(k0, 0, MLDSA_K,
461 array_bound(v->vec[k0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
462 requires(forall(k1, 0, MLDSA_K,
463 array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
464 assigns(memory_slice(w, sizeof(mld_polyveck)))
465 ensures(forall(k2, 0, MLDSA_K,
466 array_bound(w->vec[k2].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
467 );
468
469 #define mld_polyveck_pack_w1 MLD_NAMESPACE_KL(polyveck_pack_w1)
470 /*************************************************
471 * Name: mld_polyveck_pack_w1
472 *
473 * Description: Bit-pack polynomial vector w1 with coefficients in [0,15] or
474 * [0,43].
475 * Input coefficients are assumed to be standard representatives.
476 *
477 * Arguments: - uint8_t *r: pointer to output byte array with at least
478 * MLDSA_K* MLDSA_POLYW1_PACKEDBYTES bytes
479 * - const mld_polyveck *a: pointer to input polynomial vector
480 **************************************************/
481 MLD_INTERNAL_API
482 void mld_polyveck_pack_w1(uint8_t r[MLDSA_K * MLDSA_POLYW1_PACKEDBYTES],
483 const mld_polyveck *w1)
484 __contract__(
485 requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
486 requires(memory_no_alias(w1, sizeof(mld_polyveck)))
487 requires(forall(k1, 0, MLDSA_K,
488 array_bound(w1->vec[k1].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
489 assigns(memory_slice(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
490 );
491
492 #define mld_polyveck_pack_eta MLD_NAMESPACE_KL(polyveck_pack_eta)
493 /*************************************************
494 * Name: mld_polyveck_pack_eta
495 *
496 * Description: Bit-pack polynomial vector with coefficients
497 * in [-MLDSA_ETA,MLDSA_ETA].
498 *
499 * Arguments: - uint8_t *r: pointer to output byte array with
500 * MLDSA_K * MLDSA_POLYETA_PACKEDBYTES bytes
501 * - const polyveck *p: pointer to input polynomial vector
502 **************************************************/
503 MLD_INTERNAL_API
504 void mld_polyveck_pack_eta(uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES],
505 const mld_polyveck *p)
506 __contract__(
507 requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
508 requires(memory_no_alias(p, sizeof(mld_polyveck)))
509 requires(forall(k1, 0, MLDSA_K,
510 array_abs_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLDSA_ETA + 1)))
511 assigns(memory_slice(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
512 );
513
514 #define mld_polyvecl_pack_eta MLD_NAMESPACE_KL(polyvecl_pack_eta)
515 /*************************************************
516 * Name: mld_polyvecl_pack_eta
517 *
518 * Description: Bit-pack polynomial vector with coefficients in
519 * [-MLDSA_ETA,MLDSA_ETA].
520 *
521 * Arguments: - uint8_t *r: pointer to output byte array with
522 * MLDSA_L * MLDSA_POLYETA_PACKEDBYTES bytes
523 * - const polyveck *p: pointer to input polynomial vector
524 **************************************************/
525 MLD_INTERNAL_API
526 void mld_polyvecl_pack_eta(uint8_t r[MLDSA_L * MLDSA_POLYETA_PACKEDBYTES],
527 const mld_polyvecl *p)
528 __contract__(
529 requires(memory_no_alias(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
530 requires(memory_no_alias(p, sizeof(mld_polyvecl)))
531 requires(forall(k1, 0, MLDSA_L,
532 array_abs_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLDSA_ETA + 1)))
533 assigns(memory_slice(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
534 );
535
536 #define mld_polyveck_pack_t0 MLD_NAMESPACE_KL(polyveck_pack_t0)
537 /*************************************************
538 * Name: mld_polyveck_pack_t0
539 *
540 * Description: Bit-pack polynomial vector to with coefficients in
541 * ]-2^{MLDSA_D-1}, 2^{MLDSA_D-1}].
542 *
543 * Arguments: - uint8_t *r: pointer to output byte array with
544 * MLDSA_K * MLDSA_POLYT0_PACKEDBYTES bytes
545 * - const mld_poly *p: pointer to input polynomial vector
546 **************************************************/
547 MLD_INTERNAL_API
548 void mld_polyveck_pack_t0(uint8_t r[MLDSA_K * MLDSA_POLYT0_PACKEDBYTES],
549 const mld_polyveck *p)
550 __contract__(
551 requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYT0_PACKEDBYTES))
552 requires(memory_no_alias(p, sizeof(mld_polyveck)))
553 requires(forall(k0, 0, MLDSA_K,
554 array_bound(p->vec[k0].coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1)))
555 assigns(memory_slice(r, MLDSA_K * MLDSA_POLYT0_PACKEDBYTES))
556 );
557
558 #define mld_polyvecl_unpack_eta MLD_NAMESPACE_KL(polyvecl_unpack_eta)
559 /*************************************************
560 * Name: mld_polyvecl_unpack_eta
561 *
562 * Description: Unpack polynomial vector with coefficients in
563 * [-MLDSA_ETA,MLDSA_ETA].
564 *
565 * Arguments: - mld_polyvecl *p: pointer to output polynomial vector
566 * - const uint8_t *r: input byte array with
567 * bit-packed polynomial vector
568 **************************************************/
569 MLD_INTERNAL_API
570 void mld_polyvecl_unpack_eta(
571 mld_polyvecl *p, const uint8_t r[MLDSA_L * MLDSA_POLYETA_PACKEDBYTES])
572 __contract__(
573 requires(memory_no_alias(r, MLDSA_L * MLDSA_POLYETA_PACKEDBYTES))
574 requires(memory_no_alias(p, sizeof(mld_polyvecl)))
575 assigns(memory_slice(p, sizeof(mld_polyvecl)))
576 ensures(forall(k1, 0, MLDSA_L,
577 array_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1)))
578 );
579
580 #define mld_polyvecl_unpack_z MLD_NAMESPACE_KL(polyvecl_unpack_z)
581 /*************************************************
582 * Name: mld_polyvecl_unpack_z
583 *
584 * Description: Unpack polynomial vector with coefficients in
585 * [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1].
586 *
587 * Arguments: - mld_polyvecl *z: pointer to output polynomial vector
588 * - const uint8_t *r: input byte array with
589 * bit-packed polynomial vector
590 **************************************************/
591 MLD_INTERNAL_API
592 void mld_polyvecl_unpack_z(mld_polyvecl *z,
593 const uint8_t r[MLDSA_L * MLDSA_POLYZ_PACKEDBYTES])
594 __contract__(
595 requires(memory_no_alias(r, MLDSA_L * MLDSA_POLYZ_PACKEDBYTES))
596 requires(memory_no_alias(z, sizeof(mld_polyvecl)))
597 assigns(memory_slice(z, sizeof(mld_polyvecl)))
598 ensures(forall(k1, 0, MLDSA_L,
599 array_bound(z->vec[k1].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1)))
600 );
601
602 #define mld_polyveck_unpack_eta MLD_NAMESPACE_KL(polyveck_unpack_eta)
603 /*************************************************
604 * Name: mld_polyveck_unpack_eta
605 *
606 * Description: Unpack polynomial vector with coefficients in
607 * [-MLDSA_ETA,MLDSA_ETA].
608 *
609 * Arguments: - mld_polyveck *p: pointer to output polynomial vector
610 * - const uint8_t *r: input byte array with
611 * bit-packed polynomial vector
612 **************************************************/
613 MLD_INTERNAL_API
614 void mld_polyveck_unpack_eta(
615 mld_polyveck *p, const uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES])
616 __contract__(
617 requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYETA_PACKEDBYTES))
618 requires(memory_no_alias(p, sizeof(mld_polyveck)))
619 assigns(memory_slice(p, sizeof(mld_polyveck)))
620 ensures(forall(k1, 0, MLDSA_K,
621 array_bound(p->vec[k1].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1)))
622 );
623
624 #define mld_polyveck_unpack_t0 MLD_NAMESPACE_KL(polyveck_unpack_t0)
625 /*************************************************
626 * Name: mld_polyveck_unpack_t0
627 *
628 * Description: Unpack polynomial vector with coefficients in
629 * ]-2^{MLDSA_D-1}, 2^{MLDSA_D-1}].
630 *
631 * Arguments: - mld_polyveck *p: pointer to output polynomial vector
632 * - const uint8_t *r: input byte array with
633 * bit-packed polynomial vector
634 **************************************************/
635 MLD_INTERNAL_API
636 void mld_polyveck_unpack_t0(mld_polyveck *p,
637 const uint8_t r[MLDSA_K * MLDSA_POLYT0_PACKEDBYTES])
638 __contract__(
639 requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYT0_PACKEDBYTES))
640 requires(memory_no_alias(p, sizeof(mld_polyveck)))
641 assigns(memory_slice(p, sizeof(mld_polyveck)))
642 ensures(forall(k1, 0, MLDSA_K,
643 array_bound(p->vec[k1].coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1)))
644 );
645
646 #define mld_polymat_get_row MLD_NAMESPACE_KL(polymat_get_row)
647 /*************************************************
648 * Name: mld_polymat_get_row
649 *
650 * Description: Retrieve a pointer to a specific row of the matrix.
651 * In MLD_CONFIG_REDUCE_RAM mode, generates the row on-demand.
652 *
653 * Arguments: - mld_polymat *mat: pointer to matrix
654 * - unsigned int row: row index (must be < MLDSA_K)
655 *
656 * Returns pointer to the row (mld_polyvecl)
657 **************************************************/
658 MLD_INTERNAL_API
659 const mld_polyvecl *mld_polymat_get_row(mld_polymat *mat, unsigned int row);
660
661 #define mld_polyvec_matrix_expand MLD_NAMESPACE_KL(polyvec_matrix_expand)
662 /*************************************************
663 * Name: mld_polyvec_matrix_expand
664 *
665 * Description: Implementation of ExpandA. Generates matrix A with uniformly
666 * random coefficients a_{i,j} by performing rejection
667 * sampling on the output stream of SHAKE128(rho|j|i)
668 *
669 * Arguments: - mld_polymat *mat: pointer to output matrix
670 * - const uint8_t rho[]: byte array containing seed rho
671 **************************************************/
672 MLD_INTERNAL_API
673 void mld_polyvec_matrix_expand(mld_polymat *mat,
674 const uint8_t rho[MLDSA_SEEDBYTES])
675 __contract__(
676 requires(memory_no_alias(mat, sizeof(mld_polymat)))
677 requires(memory_no_alias(rho, MLDSA_SEEDBYTES))
678 assigns(memory_slice(mat, sizeof(mld_polymat)))
679 ensures(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
680 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
681 );
682
683
684
685 #define mld_polyvec_matrix_pointwise_montgomery \
686 MLD_NAMESPACE_KL(polyvec_matrix_pointwise_montgomery)
687 /*************************************************
688 * Name: mld_polyvec_matrix_pointwise_montgomery
689 *
690 * Description: Compute matrix-vector multiplication in NTT domain with
691 * pointwise multiplication and multiplication by 2^{-32}.
692 * Input matrix and vector must be in NTT domain representation.
693 *
694 * The first input "mat" must be the output of
695 * polyvec_matrix_expand() and so have coefficients in [0, Q-1]
696 * inclusive.
697 *
698 * The second input "v" is assumed to be output of an NTT, and
699 * hence must have coefficients bounded by [-9q+1, +9q-1]
700 * inclusive.
701 *
702 * Note: In MLD_CONFIG_REDUCE_RAM mode, mat cannot be const
703 * as rows are generated on-demand.
704 *
705 * Arguments: - mld_polyveck *t: pointer to output vector t
706 * - mld_polymat *mat: pointer to input matrix
707 * - const mld_polyvecl *v: pointer to input vector v
708 **************************************************/
709 MLD_INTERNAL_API
710 void mld_polyvec_matrix_pointwise_montgomery(mld_polyveck *t, mld_polymat *mat,
711 const mld_polyvecl *v)
712 __contract__(
713 requires(memory_no_alias(t, sizeof(mld_polyveck)))
714 requires(memory_no_alias(mat, sizeof(mld_polymat)))
715 requires(memory_no_alias(v, sizeof(mld_polyvecl)))
716 requires(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
717 array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
718 requires(forall(l1, 0, MLDSA_L,
719 array_abs_bound(v->vec[l1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
720 assigns(memory_slice(t, sizeof(mld_polyveck)))
721 ensures(forall(k0, 0, MLDSA_K,
722 array_abs_bound(t->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
723 );
724
725 #endif /* !MLD_POLYVEC_H */