Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/basic/mldsa_native/src/poly.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_POLY_H
6 #define MLD_POLY_H
7
8 #include <stdint.h>
9 #include "cbmc.h"
10 #include "common.h"
11 #include "reduce.h"
12 #include "rounding.h"
13
14 /* Absolute exclusive upper bound for the output of the forward NTT */
15 #define MLD_NTT_BOUND (9 * MLDSA_Q)
16 /* Absolute exclusive upper bound for the output of the inverse NTT*/
17 #define MLD_INTT_BOUND MLDSA_Q
18
19 typedef struct
20 {
21 int32_t coeffs[MLDSA_N];
22 } MLD_ALIGN mld_poly;
23
24 #define mld_poly_reduce MLD_NAMESPACE(poly_reduce)
25 /*************************************************
26 * Name: mld_poly_reduce
27 *
28 * Description: Inplace reduction of all coefficients of polynomial to
29 * representative in
30 *[-MLD_REDUCE32_RANGE_MAX,MLD_REDUCE32_RANGE_MAX].
31 *
32 * Arguments: - mld_poly *a: pointer to input/output polynomial
33 **************************************************/
34 MLD_INTERNAL_API
35 void mld_poly_reduce(mld_poly *a)
36 __contract__(
37 requires(memory_no_alias(a, sizeof(mld_poly)))
38 requires(array_bound(a->coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX))
39 assigns(memory_slice(a, sizeof(mld_poly)))
40 ensures(array_bound(a->coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
41 );
42
43 #define mld_poly_caddq MLD_NAMESPACE(poly_caddq)
44 /*************************************************
45 * Name: mld_poly_caddq
46 *
47 * Description: For all coefficients of in/out polynomial add MLDSA_Q if
48 * coefficient is negative.
49 *
50 * Arguments: - mld_poly *a: pointer to input/output polynomial
51 **************************************************/
52 MLD_INTERNAL_API
53 void mld_poly_caddq(mld_poly *a)
54 __contract__(
55 requires(memory_no_alias(a, sizeof(mld_poly)))
56 requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
57 assigns(memory_slice(a, sizeof(mld_poly)))
58 ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
59 );
60
61 #define mld_poly_add MLD_NAMESPACE(poly_add)
62 /*************************************************
63 * Name: mld_poly_add
64 *
65 * Description: Add polynomials. No modular reduction is performed.
66 *
67 * Arguments: - r: Pointer to input-output polynomial to be added to.
68 * - b: Pointer to input polynomial that should be added
69 * to r. Must be disjoint from r.
70 **************************************************/
71
72 /*
73 * NOTE: The reference implementation uses a 3-argument poly_add.
74 * We specialize to the accumulator form to avoid reasoning about aliasing.
75 */
76 MLD_INTERNAL_API
77 void mld_poly_add(mld_poly *r, const mld_poly *b)
78 __contract__(
79 requires(memory_no_alias(b, sizeof(mld_poly)))
80 requires(memory_no_alias(r, sizeof(mld_poly)))
81 requires(forall(k0, 0, MLDSA_N, (int64_t) r->coeffs[k0] + b->coeffs[k0] < MLD_REDUCE32_DOMAIN_MAX))
82 requires(forall(k1, 0, MLDSA_N, (int64_t) r->coeffs[k1] + b->coeffs[k1] >= INT32_MIN))
83 assigns(memory_slice(r, sizeof(mld_poly)))
84 ensures(forall(k2, 0, MLDSA_N, r->coeffs[k2] == old(*r).coeffs[k2] + b->coeffs[k2]))
85 ensures(forall(k3, 0, MLDSA_N, r->coeffs[k3] < MLD_REDUCE32_DOMAIN_MAX))
86 ensures(forall(k4, 0, MLDSA_N, r->coeffs[k4] >= INT32_MIN))
87 );
88
89 #define mld_poly_sub MLD_NAMESPACE(poly_sub)
90 /*************************************************
91 * Name: mld_poly_sub
92 *
93 * Description: Subtract polynomials. No modular reduction is
94 * performed.
95 *
96 * Arguments: - mld_poly *r: Pointer to input-output polynomial.
97 * - const mld_poly *b: Pointer to input polynomial that should be
98 * subtracted from r. Must be disjoint from r.
99 **************************************************/
100 /*
101 * NOTE: The reference implementation uses a 3-argument poly_sub.
102 * We specialize to the accumulator form to avoid reasoning about aliasing.
103 */
104 MLD_INTERNAL_API
105 void mld_poly_sub(mld_poly *r, const mld_poly *b)
106 __contract__(
107 requires(memory_no_alias(b, sizeof(mld_poly)))
108 requires(memory_no_alias(r, sizeof(mld_poly)))
109 requires(array_abs_bound(r->coeffs, 0, MLDSA_N, MLDSA_Q))
110 requires(array_abs_bound(b->coeffs, 0, MLDSA_N, MLDSA_Q))
111 assigns(memory_slice(r, sizeof(mld_poly)))
112 ensures(array_bound(r->coeffs, 0, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX))
113 );
114
115 #define mld_poly_shiftl MLD_NAMESPACE(poly_shiftl)
116 /*************************************************
117 * Name: mld_poly_shiftl
118 *
119 * Description: Multiply polynomial by 2^MLDSA_D without modular reduction.
120 *Assumes input coefficients to be less than 2^{31-MLDSA_D} in absolute value.
121 *
122 * Arguments: - mld_poly *a: pointer to input/output polynomial
123 **************************************************/
124 MLD_INTERNAL_API
125 void mld_poly_shiftl(mld_poly *a)
126 __contract__(
127 requires(memory_no_alias(a, sizeof(mld_poly)))
128 requires(array_bound(a->coeffs, 0, MLDSA_N, 0, 1 << 10))
129 assigns(memory_slice(a, sizeof(mld_poly)))
130 ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
131 );
132
133 #define mld_poly_ntt MLD_NAMESPACE(poly_ntt)
134 /*************************************************
135 * Name: mld_poly_ntt
136 *
137 * Description: Inplace forward NTT. Coefficients can grow by
138 * 8*MLDSA_Q in absolute value.
139 *
140 * Arguments: - mld_poly *a: pointer to input/output polynomial
141 **************************************************/
142 MLD_INTERNAL_API
143 void mld_poly_ntt(mld_poly *a)
144 __contract__(
145 requires(memory_no_alias(a, sizeof(mld_poly)))
146 requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
147 assigns(memory_slice(a, sizeof(mld_poly)))
148 ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
149 );
150
151
152 #define mld_poly_invntt_tomont MLD_NAMESPACE(poly_invntt_tomont)
153 /*************************************************
154 * Name: mld_poly_invntt_tomont
155 *
156 * Description: Inplace inverse NTT and multiplication by 2^{32}.
157 * Input coefficients need to be less than MLDSA_Q in absolute
158 * value and output coefficients are bounded by
159 * MLD_INTT_BOUND.
160 *
161 * Arguments: - mld_poly *a: pointer to input/output polynomial
162 **************************************************/
163 MLD_INTERNAL_API
164 void mld_poly_invntt_tomont(mld_poly *a)
165 __contract__(
166 requires(memory_no_alias(a, sizeof(mld_poly)))
167 requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q))
168 assigns(memory_slice(a, sizeof(mld_poly)))
169 ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_INTT_BOUND))
170 );
171
172 #define mld_poly_pointwise_montgomery MLD_NAMESPACE(poly_pointwise_montgomery)
173 /*************************************************
174 * Name: mld_poly_pointwise_montgomery
175 *
176 * Description: Pointwise multiplication of polynomials in NTT domain
177 * representation and multiplication of resulting polynomial
178 * by 2^{-32}.
179 *
180 * Arguments: - mld_poly *c: pointer to output polynomial
181 * - const mld_poly *a: pointer to first input polynomial
182 * - const mld_poly *b: pointer to second input polynomial
183 **************************************************/
184 MLD_INTERNAL_API
185 void mld_poly_pointwise_montgomery(mld_poly *c, const mld_poly *a,
186 const mld_poly *b)
187 __contract__(
188 requires(memory_no_alias(a, sizeof(mld_poly)))
189 requires(memory_no_alias(b, sizeof(mld_poly)))
190 requires(memory_no_alias(c, sizeof(mld_poly)))
191 requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
192 requires(array_abs_bound(b->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
193 assigns(memory_slice(c, sizeof(mld_poly)))
194 ensures(array_abs_bound(c->coeffs, 0, MLDSA_N, MLDSA_Q))
195 );
196
197 #define mld_poly_power2round MLD_NAMESPACE(poly_power2round)
198 /*************************************************
199 * Name: mld_poly_power2round
200 *
201 * Description: For all coefficients c of the input polynomial,
202 * compute c0, c1 such that c mod MLDSA_Q = c1*2^MLDSA_D + c0
203 * with -2^{MLDSA_D-1} < c0 <= 2^{MLDSA_D-1}. Assumes coefficients
204 *to be standard representatives.
205 *
206 * Arguments: - mld_poly *a1: pointer to output polynomial with coefficients
207 *c1
208 * - mld_poly *a0: pointer to output polynomial with coefficients
209 *c0
210 * - const mld_poly *a: pointer to input polynomial
211 **************************************************/
212 MLD_INTERNAL_API
213 void mld_poly_power2round(mld_poly *a1, mld_poly *a0, const mld_poly *a)
214 __contract__(
215 requires(memory_no_alias(a0, sizeof(mld_poly)))
216 requires(memory_no_alias(a1, sizeof(mld_poly)))
217 requires(memory_no_alias(a, sizeof(mld_poly)))
218 requires(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
219 assigns(memory_slice(a1, sizeof(mld_poly)))
220 assigns(memory_slice(a0, sizeof(mld_poly)))
221 ensures(array_bound(a0->coeffs, 0, MLDSA_N, -(MLD_2_POW_D/2)+1, (MLD_2_POW_D/2)+1))
222 ensures(array_bound(a1->coeffs, 0, MLDSA_N, 0, ((MLDSA_Q - 1) / MLD_2_POW_D) + 1))
223 );
224
225 #define mld_poly_uniform MLD_NAMESPACE(poly_uniform)
226 /*************************************************
227 * Name: mld_poly_uniform
228 *
229 * Description: Sample polynomial with uniformly random coefficients
230 * in [0,MLDSA_Q-1] by performing rejection sampling on the
231 * output stream of SHAKE128(seed|nonce)
232 *
233 * Arguments: - mld_poly *a: pointer to output polynomial
234 * - const uint8_t seed[]: byte array with seed of length
235 * MLDSA_SEEDBYTES and the packed 2-byte nonce
236 **************************************************/
237 MLD_INTERNAL_API
238 void mld_poly_uniform(mld_poly *a, const uint8_t seed[MLDSA_SEEDBYTES + 2])
239 __contract__(
240 requires(memory_no_alias(a, sizeof(mld_poly)))
241 requires(memory_no_alias(seed, MLDSA_SEEDBYTES + 2))
242 assigns(memory_slice(a, sizeof(mld_poly)))
243 ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
244 );
245
246 #if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY) && !defined(MLD_CONFIG_REDUCE_RAM)
247 #define mld_poly_uniform_4x MLD_NAMESPACE(poly_uniform_4x)
248 /*************************************************
249 * Name: mld_poly_uniform_x4
250 *
251 * Description: Generate four polynomials using rejection sampling
252 * on (pseudo-)uniformly random bytes sampled from a seed.
253 *
254 * Arguments: - mld_poly *vec0, *vec1, *vec2, *vec3:
255 * Pointers to 4 polynomials to be sampled.
256 * - uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)]:
257 * Pointer consecutive array of seed buffers of size
258 * MLDSA_SEEDBYTES + 2 each, plus padding for alignment.
259 *
260 **************************************************/
261 MLD_INTERNAL_API
262 void mld_poly_uniform_4x(mld_poly *vec0, mld_poly *vec1, mld_poly *vec2,
263 mld_poly *vec3,
264 uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)])
265 __contract__(
266 requires(memory_no_alias(vec0, sizeof(mld_poly)))
267 requires(memory_no_alias(vec1, sizeof(mld_poly)))
268 requires(memory_no_alias(vec2, sizeof(mld_poly)))
269 requires(memory_no_alias(vec3, sizeof(mld_poly)))
270 requires(memory_no_alias(seed, 4 * MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)))
271 assigns(memory_slice(vec0, sizeof(mld_poly)))
272 assigns(memory_slice(vec1, sizeof(mld_poly)))
273 assigns(memory_slice(vec2, sizeof(mld_poly)))
274 assigns(memory_slice(vec3, sizeof(mld_poly)))
275 ensures(array_bound(vec0->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
276 ensures(array_bound(vec1->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
277 ensures(array_bound(vec2->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
278 ensures(array_bound(vec3->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
279 );
280 #endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY && !MLD_CONFIG_REDUCE_RAM */
281
282 #define mld_polyt1_pack MLD_NAMESPACE(polyt1_pack)
283 /*************************************************
284 * Name: mld_polyt1_pack
285 *
286 * Description: Bit-pack polynomial t1 with coefficients fitting in 10 bits.
287 * Input coefficients are assumed to be standard representatives.
288 *
289 * Arguments: - uint8_t *r: pointer to output byte array with at least
290 * MLDSA_POLYT1_PACKEDBYTES bytes
291 * - const mld_poly *a: pointer to input polynomial
292 **************************************************/
293 MLD_INTERNAL_API
294 void mld_polyt1_pack(uint8_t r[MLDSA_POLYT1_PACKEDBYTES], const mld_poly *a)
295 __contract__(
296 requires(memory_no_alias(r, MLDSA_POLYT1_PACKEDBYTES))
297 requires(memory_no_alias(a, sizeof(mld_poly)))
298 requires(array_bound(a->coeffs, 0, MLDSA_N, 0, 1 << 10))
299 assigns(memory_slice(r, MLDSA_POLYT1_PACKEDBYTES))
300 );
301
302 #define mld_polyt1_unpack MLD_NAMESPACE(polyt1_unpack)
303 /*************************************************
304 * Name: mld_polyt1_unpack
305 *
306 * Description: Unpack polynomial t1 with 10-bit coefficients.
307 * Output coefficients are standard representatives.
308 *
309 * Arguments: - mld_poly *r: pointer to output polynomial
310 * - const uint8_t *a: byte array with bit-packed polynomial
311 **************************************************/
312 MLD_INTERNAL_API
313 void mld_polyt1_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYT1_PACKEDBYTES])
314 __contract__(
315 requires(memory_no_alias(r, sizeof(mld_poly)))
316 requires(memory_no_alias(a, MLDSA_POLYT1_PACKEDBYTES))
317 assigns(memory_slice(r, sizeof(mld_poly)))
318 ensures(array_bound(r->coeffs, 0, MLDSA_N, 0, 1 << 10))
319 );
320
321 #define mld_polyt0_pack MLD_NAMESPACE(polyt0_pack)
322 /*************************************************
323 * Name: mld_polyt0_pack
324 *
325 * Description: Bit-pack polynomial t0 with coefficients in ]-2^{MLDSA_D-1},
326 * 2^{MLDSA_D-1}].
327 *
328 * Arguments: - uint8_t *r: pointer to output byte array with at least
329 * MLDSA_POLYT0_PACKEDBYTES bytes
330 * - const mld_poly *a: pointer to input polynomial
331 **************************************************/
332 MLD_INTERNAL_API
333 void mld_polyt0_pack(uint8_t r[MLDSA_POLYT0_PACKEDBYTES], const mld_poly *a)
334 __contract__(
335 requires(memory_no_alias(r, MLDSA_POLYT0_PACKEDBYTES))
336 requires(memory_no_alias(a, sizeof(mld_poly)))
337 requires(array_bound(a->coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))
338 assigns(memory_slice(r, MLDSA_POLYT0_PACKEDBYTES))
339 );
340
341
342 #define mld_polyt0_unpack MLD_NAMESPACE(polyt0_unpack)
343 /*************************************************
344 * Name: mld_polyt0_unpack
345 *
346 * Description: Unpack polynomial t0 with coefficients in ]-2^{MLDSA_D-1},
347 *2^{MLDSA_D-1}].
348 *
349 * Arguments: - mld_poly *r: pointer to output polynomial
350 * - const uint8_t *a: byte array with bit-packed polynomial
351 **************************************************/
352 MLD_INTERNAL_API
353 void mld_polyt0_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYT0_PACKEDBYTES])
354 __contract__(
355 requires(memory_no_alias(r, sizeof(mld_poly)))
356 requires(memory_no_alias(a, MLDSA_POLYT0_PACKEDBYTES))
357 assigns(memory_slice(r, sizeof(mld_poly)))
358 ensures(array_bound(r->coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))
359 );
360
361 #define mld_poly_chknorm MLD_NAMESPACE(poly_chknorm)
362 /*************************************************
363 * Name: mld_poly_chknorm
364 *
365 * Description: Check infinity norm of polynomial against given bound.
366 * Assumes input coefficients were reduced by mld_reduce32().
367 *
368 * Arguments: - const mld_poly *a: pointer to polynomial
369 * - int32_t B: norm bound
370 *
371 * Returns 0 if norm is strictly smaller than
372 * B <= (MLDSA_Q - MLD_REDUCE32_RANGE_MAX) and 0xFFFFFFFF otherwise.
373 *
374 * Specification: The definition of this FIPS-204 requires signed canonical
375 * reduction prior to applying the bounds check.
376 * However, `-B < (a mod± MLDSA_Q) < B` is equivalent to
377 * `-B < a < B` under the assumption that
378 * `B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX` (cf. the assertion in
379 * the code). Hence, the present spec and implementation are
380 * correct without reduction.
381 *
382 **************************************************/
383 MLD_INTERNAL_API
384 uint32_t mld_poly_chknorm(const mld_poly *a, int32_t B)
385 __contract__(
386 requires(memory_no_alias(a, sizeof(mld_poly)))
387 requires(0 <= B && B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX)
388 requires(array_bound(a->coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
389 ensures(return_value == 0 || return_value == 0xFFFFFFFF)
390 ensures((return_value == 0) == array_abs_bound(a->coeffs, 0, MLDSA_N, B))
391 );
392
393 #endif /* !MLD_POLY_H */