Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4 */
5 #include <string.h>
6
7 #include "common.h"
8 #include "packing.h"
9 #include "poly.h"
10 #include "polyvec.h"
11
12 /* Parameter set namespacing
13 * This is to facilitate building multiple instances
14 * of mldsa-native (e.g. with varying parameter sets)
15 * within a single compilation unit. */
16 #define mld_unpack_hints MLD_ADD_PARAM_SET(mld_unpack_hints)
17 /* End of parameter set namespacing */
18
19 MLD_INTERNAL_API
20 void mld_pack_pk(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES],
21 const uint8_t rho[MLDSA_SEEDBYTES], const mld_polyveck *t1)
22 {
23 unsigned int i;
24
25 mld_memcpy(pk, rho, MLDSA_SEEDBYTES);
26 for (i = 0; i < MLDSA_K; ++i)
27 __loop__(
28 assigns(i, memory_slice(pk, MLDSA_CRYPTO_PUBLICKEYBYTES))
29 invariant(i <= MLDSA_K)
30 )
31 {
32 mld_polyt1_pack(pk + MLDSA_SEEDBYTES + i * MLDSA_POLYT1_PACKEDBYTES,
33 &t1->vec[i]);
34 }
35 }
36
37 MLD_INTERNAL_API
38 void mld_unpack_pk(uint8_t rho[MLDSA_SEEDBYTES], mld_polyveck *t1,
39 const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES])
40 {
41 unsigned int i;
42
43 mld_memcpy(rho, pk, MLDSA_SEEDBYTES);
44 pk += MLDSA_SEEDBYTES;
45
46 for (i = 0; i < MLDSA_K; ++i)
47 {
48 mld_polyt1_unpack(&t1->vec[i], pk + i * MLDSA_POLYT1_PACKEDBYTES);
49 }
50 }
51
52 MLD_INTERNAL_API
53 void mld_pack_sk(uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES],
54 const uint8_t rho[MLDSA_SEEDBYTES],
55 const uint8_t tr[MLDSA_TRBYTES],
56 const uint8_t key[MLDSA_SEEDBYTES], const mld_polyveck *t0,
57 const mld_polyvecl *s1, const mld_polyveck *s2)
58 {
59 mld_memcpy(sk, rho, MLDSA_SEEDBYTES);
60 sk += MLDSA_SEEDBYTES;
61
62 mld_memcpy(sk, key, MLDSA_SEEDBYTES);
63 sk += MLDSA_SEEDBYTES;
64
65 mld_memcpy(sk, tr, MLDSA_TRBYTES);
66 sk += MLDSA_TRBYTES;
67
68 mld_polyvecl_pack_eta(sk, s1);
69 sk += MLDSA_L * MLDSA_POLYETA_PACKEDBYTES;
70
71 mld_polyveck_pack_eta(sk, s2);
72 sk += MLDSA_K * MLDSA_POLYETA_PACKEDBYTES;
73
74 mld_polyveck_pack_t0(sk, t0);
75 }
76
77 MLD_INTERNAL_API
78 void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
79 uint8_t key[MLDSA_SEEDBYTES], mld_polyveck *t0,
80 mld_polyvecl *s1, mld_polyveck *s2,
81 const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES])
82 {
83 mld_memcpy(rho, sk, MLDSA_SEEDBYTES);
84 sk += MLDSA_SEEDBYTES;
85
86 mld_memcpy(key, sk, MLDSA_SEEDBYTES);
87 sk += MLDSA_SEEDBYTES;
88
89 mld_memcpy(tr, sk, MLDSA_TRBYTES);
90 sk += MLDSA_TRBYTES;
91
92 mld_polyvecl_unpack_eta(s1, sk);
93 sk += MLDSA_L * MLDSA_POLYETA_PACKEDBYTES;
94
95 mld_polyveck_unpack_eta(s2, sk);
96 sk += MLDSA_K * MLDSA_POLYETA_PACKEDBYTES;
97
98 mld_polyveck_unpack_t0(t0, sk);
99 }
100
101 MLD_INTERNAL_API
102 void mld_pack_sig_c_h(uint8_t sig[MLDSA_CRYPTO_BYTES],
103 const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h,
104 const unsigned int number_of_hints)
105 {
106 unsigned int i, j, k;
107
108 mld_memcpy(sig, c, MLDSA_CTILDEBYTES);
109 sig += MLDSA_CTILDEBYTES;
110
111 /* skip z component - packed via mld_pack_sig_z */
112 sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
113
114 /* Encode hints h */
115
116 /* The final section of sig[] is MLDSA_POLYVECH_PACKEDBYTES long, where
117 * MLDSA_POLYVECH_PACKEDBYTES = MLDSA_OMEGA + MLDSA_K
118 *
119 * The first OMEGA bytes record the index numbers of the coefficients
120 * that are not equal to 0
121 *
122 * The final K bytes record a running tally of the number of hints
123 * coming from each of the K polynomials in h.
124 *
125 * The pre-condition tells us that number_of_hints <= OMEGA, so some
126 * bytes may not be written, so we initialize all of them to zero
127 * to start.
128 */
129 mld_memset(sig, 0, MLDSA_POLYVECH_PACKEDBYTES);
130
131 k = 0;
132 /* For each polynomial in h... */
133 for (i = 0; i < MLDSA_K; ++i)
134 __loop__(
135 assigns(i, j, k, memory_slice(sig, MLDSA_POLYVECH_PACKEDBYTES))
136 invariant(i <= MLDSA_K)
137 invariant(k <= number_of_hints)
138 invariant(number_of_hints <= MLDSA_OMEGA)
139 )
140 {
141 /* For each coefficient in that polynomial, record it as as hint */
142 /* if its value is not zero */
143 for (j = 0; j < MLDSA_N; ++j)
144 __loop__(
145 assigns(j, k, memory_slice(sig, MLDSA_POLYVECH_PACKEDBYTES))
146 invariant(i <= MLDSA_K)
147 invariant(j <= MLDSA_N)
148 invariant(k <= number_of_hints)
149 invariant(number_of_hints <= MLDSA_OMEGA)
150 )
151 {
152 /* The reference implementation implicitly relies on the total */
153 /* number of hints being less than OMEGA, assuming h is valid. */
154 /* In mldsa-native, we check this explicitly to ease proof of */
155 /* type safety. */
156 if (h->vec[i].coeffs[j] != 0 && k < number_of_hints)
157 {
158 /* The enclosing if condition AND the loop invariant infer */
159 /* that k < MLDSA_OMEGA, so writing to sig[k] is safe and k */
160 /* can be incremented. */
161 sig[k++] = (uint8_t)j;
162 }
163 }
164 /* Having recorded all the hints for this polynomial, also */
165 /* record the running tally into the correct "slot" for that */
166 /* coefficient in the final K bytes */
167 sig[MLDSA_OMEGA + i] = (uint8_t)k;
168 }
169 }
170
171 MLD_INTERNAL_API
172 void mld_pack_sig_z(uint8_t sig[MLDSA_CRYPTO_BYTES], const mld_poly *zi,
173 unsigned i)
174 {
175 sig += MLDSA_CTILDEBYTES;
176 sig += i * MLDSA_POLYZ_PACKEDBYTES;
177 mld_polyz_pack(sig, zi);
178 }
179
180 /*************************************************
181 * Name: mld_unpack_hints
182 *
183 * Description: Unpack raw hint bytes into a polyveck
184 * struct
185 *
186 * Arguments: - mld_polyveck *h: pointer to output hint vector h
187 * - const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES]:
188 * raw hint bytes
189 *
190 * Returns 1 in case of malformed hints; otherwise 0.
191 **************************************************/
192 static int mld_unpack_hints(
193 mld_polyveck *h, const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES])
194 __contract__(
195 requires(memory_no_alias(packed_hints, MLDSA_POLYVECH_PACKEDBYTES))
196 requires(memory_no_alias(h, sizeof(mld_polyveck)))
197 assigns(memory_slice(h, sizeof(mld_polyveck)))
198 /* All returned coefficients are either 0 or 1 */
199 ensures(forall(k1, 0, MLDSA_K,
200 array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
201 ensures(return_value >= 0 && return_value <= 1)
202 )
203 {
204 unsigned int i, j;
205 unsigned int old_hint_count;
206
207 /* Set all coefficients of all polynomials to 0. */
208 /* Only those that are actually non-zero hints will */
209 /* be overwritten below. */
210 mld_memset(h, 0, sizeof(mld_polyveck));
211
212 old_hint_count = 0;
213 for (i = 0; i < MLDSA_K; ++i)
214 __loop__(
215 invariant(i <= MLDSA_K)
216 /* Maintain the post-condition */
217 invariant(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
218 )
219 {
220 /* Grab the hint count for the i'th polynomial */
221 const unsigned int new_hint_count = packed_hints[MLDSA_OMEGA + i];
222
223 /* new_hint_count must increase or stay the same, but also remain */
224 /* less than or equal to MLDSA_OMEGA */
225 if (new_hint_count < old_hint_count || new_hint_count > MLDSA_OMEGA)
226 {
227 /* Error - new_hint_count is invalid */
228 return 1;
229 }
230
231 /* If new_hint_count == old_hint_count, then this polynomial has */
232 /* zero hints, so this loop executes zero times and we move */
233 /* straight on to the next polynomial. */
234 for (j = old_hint_count; j < new_hint_count; ++j)
235 __loop__(
236 invariant(i <= MLDSA_K)
237 /* Maintain the post-condition */
238 invariant(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
239 )
240 {
241 const uint8_t this_hint_index = packed_hints[j];
242
243 /* Coefficients must be ordered for strong unforgeability */
244 if (j > old_hint_count && this_hint_index <= packed_hints[j - 1])
245 {
246 return 1;
247 }
248 h->vec[i].coeffs[this_hint_index] = 1;
249 }
250
251 old_hint_count = new_hint_count;
252 }
253
254 /* Extra indices must be zero for strong unforgeability */
255 for (j = old_hint_count; j < MLDSA_OMEGA; ++j)
256 __loop__(
257 invariant(j <= MLDSA_OMEGA)
258 /* Maintain the post-condition */
259 invariant(forall(k1, 0, MLDSA_K, array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
260 )
261 {
262 if (packed_hints[j] != 0)
263 {
264 return 1;
265 }
266 }
267
268 return 0;
269 }
270
271 MLD_INTERNAL_API
272 int mld_unpack_sig(uint8_t c[MLDSA_CTILDEBYTES], mld_polyvecl *z,
273 mld_polyveck *h, const uint8_t sig[MLDSA_CRYPTO_BYTES])
274 {
275 mld_memcpy(c, sig, MLDSA_CTILDEBYTES);
276 sig += MLDSA_CTILDEBYTES;
277
278 mld_polyvecl_unpack_z(z, sig);
279 sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
280
281 return mld_unpack_hints(h, sig);
282 }
283
284 /* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
285 * Don't modify by hand -- this is auto-generated by scripts/autogen. */
286 #undef mld_unpack_hints