Skip to content

Commit 5ad011f

Browse files
committed
Undo splitting of 2D arrays into separate objects
Previously, we had to split various 2D arrays into separate objects to accommodate the strict semantics of is_fresh/memory_no_alias in function contract preconditions. With the new relaxed semantics of is_fresh/memory_no_alias, this is no longer necessary. Signed-off-by: Hanno Becker <[email protected]>
1 parent c0a20af commit 5ad011f

File tree

9 files changed

+94
-141
lines changed

9 files changed

+94
-141
lines changed

examples/monolithic_build/mlkem_native_monobuild.c

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
#undef mlk_polyvec_tomont
199199
/* mlkem/sys.h */
200200
#undef MLK_ALIGN
201+
#undef MLK_ALIGN_UP
201202
#undef MLK_ALWAYS_INLINE
202203
#undef MLK_CET_ENDBR
203204
#undef MLK_CT_TESTING_DECLASSIFY

mlkem/indcpa.c

+13-24
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,11 @@ void mlk_gen_matrix(mlk_polyvec *a, const uint8_t seed[MLKEM_SYMBYTES],
197197
* of the same parent object.
198198
*/
199199

200-
MLK_ALIGN uint8_t seed0[MLKEM_SYMBYTES + 2];
201-
MLK_ALIGN uint8_t seed1[MLKEM_SYMBYTES + 2];
202-
MLK_ALIGN uint8_t seed2[MLKEM_SYMBYTES + 2];
203-
MLK_ALIGN uint8_t seed3[MLKEM_SYMBYTES + 2];
204-
uint8_t *seedxy[4];
205-
seedxy[0] = seed0;
206-
seedxy[1] = seed1;
207-
seedxy[2] = seed2;
208-
seedxy[3] = seed3;
200+
MLK_ALIGN uint8_t seed_ext[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)];
209201

210202
for (j = 0; j < 4; j++)
211203
{
212-
memcpy(seedxy[j], seed, MLKEM_SYMBYTES);
204+
memcpy(seed_ext[j], seed, MLKEM_SYMBYTES);
213205
}
214206

215207
/* Sample 4 matrix entries a time. */
@@ -223,21 +215,21 @@ void mlk_gen_matrix(mlk_polyvec *a, const uint8_t seed[MLKEM_SYMBYTES],
223215
y = (i + j) % MLKEM_K;
224216
if (transposed)
225217
{
226-
seedxy[j][MLKEM_SYMBYTES + 0] = x;
227-
seedxy[j][MLKEM_SYMBYTES + 1] = y;
218+
seed_ext[j][MLKEM_SYMBYTES + 0] = x;
219+
seed_ext[j][MLKEM_SYMBYTES + 1] = y;
228220
}
229221
else
230222
{
231-
seedxy[j][MLKEM_SYMBYTES + 0] = y;
232-
seedxy[j][MLKEM_SYMBYTES + 1] = x;
223+
seed_ext[j][MLKEM_SYMBYTES + 0] = y;
224+
seed_ext[j][MLKEM_SYMBYTES + 1] = x;
233225
}
234226
}
235227

236228
/*
237229
* This call writes across mlk_polyvec boundaries for K=2 and K=3.
238230
* This is intentional and safe.
239231
*/
240-
mlk_poly_rej_uniform_x4(&a[0].vec[0] + i, seedxy);
232+
mlk_poly_rej_uniform_x4(&a[0].vec[0] + i, seed_ext);
241233
}
242234

243235
/* For MLKEM_K == 3, sample the last entry individually. */
@@ -249,16 +241,16 @@ void mlk_gen_matrix(mlk_polyvec *a, const uint8_t seed[MLKEM_SYMBYTES],
249241

250242
if (transposed)
251243
{
252-
seed0[MLKEM_SYMBYTES + 0] = x;
253-
seed0[MLKEM_SYMBYTES + 1] = y;
244+
seed_ext[0][MLKEM_SYMBYTES + 0] = x;
245+
seed_ext[0][MLKEM_SYMBYTES + 1] = y;
254246
}
255247
else
256248
{
257-
seed0[MLKEM_SYMBYTES + 0] = y;
258-
seed0[MLKEM_SYMBYTES + 1] = x;
249+
seed_ext[0][MLKEM_SYMBYTES + 0] = y;
250+
seed_ext[0][MLKEM_SYMBYTES + 1] = x;
259251
}
260252

261-
mlk_poly_rej_uniform(&a[0].vec[0] + i, seed0);
253+
mlk_poly_rej_uniform(&a[0].vec[0] + i, seed_ext[0]);
262254
i++;
263255
}
264256

@@ -278,10 +270,7 @@ void mlk_gen_matrix(mlk_polyvec *a, const uint8_t seed[MLKEM_SYMBYTES],
278270

279271
/* Specification: Partially implements
280272
* [FIPS 203, Section 3.3, Destruction of intermediate values] */
281-
mlk_zeroize(seed0, sizeof(seed0));
282-
mlk_zeroize(seed1, sizeof(seed1));
283-
mlk_zeroize(seed2, sizeof(seed2));
284-
mlk_zeroize(seed3, sizeof(seed3));
273+
mlk_zeroize(seed_ext, sizeof(seed_ext));
285274
}
286275

287276
/*************************************************

mlkem/poly_k.c

+40-66
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,21 @@ void mlk_poly_getnoise_eta1_4x(mlk_poly *r0, mlk_poly *r1, mlk_poly *r2,
286286
uint8_t nonce0, uint8_t nonce1, uint8_t nonce2,
287287
uint8_t nonce3)
288288
{
289-
MLK_ALIGN uint8_t buf0[MLKEM_ETA1 * MLKEM_N / 4];
290-
MLK_ALIGN uint8_t buf1[MLKEM_ETA1 * MLKEM_N / 4];
291-
MLK_ALIGN uint8_t buf2[MLKEM_ETA1 * MLKEM_N / 4];
292-
MLK_ALIGN uint8_t buf3[MLKEM_ETA1 * MLKEM_N / 4];
293-
MLK_ALIGN uint8_t extkey0[MLKEM_SYMBYTES + 1];
294-
MLK_ALIGN uint8_t extkey1[MLKEM_SYMBYTES + 1];
295-
MLK_ALIGN uint8_t extkey2[MLKEM_SYMBYTES + 1];
296-
MLK_ALIGN uint8_t extkey3[MLKEM_SYMBYTES + 1];
297-
memcpy(extkey0, seed, MLKEM_SYMBYTES);
298-
memcpy(extkey1, seed, MLKEM_SYMBYTES);
299-
memcpy(extkey2, seed, MLKEM_SYMBYTES);
300-
memcpy(extkey3, seed, MLKEM_SYMBYTES);
301-
extkey0[MLKEM_SYMBYTES] = nonce0;
302-
extkey1[MLKEM_SYMBYTES] = nonce1;
303-
extkey2[MLKEM_SYMBYTES] = nonce2;
304-
extkey3[MLKEM_SYMBYTES] = nonce3;
305-
mlk_prf_eta1_x4(buf0, buf1, buf2, buf3, extkey0, extkey1, extkey2, extkey3);
306-
mlk_poly_cbd_eta1(r0, buf0);
307-
mlk_poly_cbd_eta1(r1, buf1);
308-
mlk_poly_cbd_eta1(r2, buf2);
309-
mlk_poly_cbd_eta1(r3, buf3);
289+
MLK_ALIGN uint8_t buf[4][MLK_ALIGN_UP(MLKEM_ETA1 * MLKEM_N / 4)];
290+
MLK_ALIGN uint8_t extkey[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 1)];
291+
memcpy(extkey[0], seed, MLKEM_SYMBYTES);
292+
memcpy(extkey[1], seed, MLKEM_SYMBYTES);
293+
memcpy(extkey[2], seed, MLKEM_SYMBYTES);
294+
memcpy(extkey[3], seed, MLKEM_SYMBYTES);
295+
extkey[0][MLKEM_SYMBYTES] = nonce0;
296+
extkey[1][MLKEM_SYMBYTES] = nonce1;
297+
extkey[2][MLKEM_SYMBYTES] = nonce2;
298+
extkey[3][MLKEM_SYMBYTES] = nonce3;
299+
mlk_prf_eta1_x4(buf, extkey);
300+
mlk_poly_cbd_eta1(r0, buf[0]);
301+
mlk_poly_cbd_eta1(r1, buf[1]);
302+
mlk_poly_cbd_eta1(r2, buf[2]);
303+
mlk_poly_cbd_eta1(r3, buf[3]);
310304

311305
mlk_assert_abs_bound(r0, MLKEM_N, MLKEM_ETA1 + 1);
312306
mlk_assert_abs_bound(r1, MLKEM_N, MLKEM_ETA1 + 1);
@@ -315,14 +309,8 @@ void mlk_poly_getnoise_eta1_4x(mlk_poly *r0, mlk_poly *r1, mlk_poly *r2,
315309

316310
/* Specification: Partially implements
317311
* [FIPS 203, Section 3.3, Destruction of intermediate values] */
318-
mlk_zeroize(buf0, sizeof(buf0));
319-
mlk_zeroize(buf1, sizeof(buf1));
320-
mlk_zeroize(buf2, sizeof(buf2));
321-
mlk_zeroize(buf3, sizeof(buf3));
322-
mlk_zeroize(extkey0, sizeof(extkey0));
323-
mlk_zeroize(extkey1, sizeof(extkey1));
324-
mlk_zeroize(extkey2, sizeof(extkey2));
325-
mlk_zeroize(extkey3, sizeof(extkey3));
312+
mlk_zeroize(buf, sizeof(buf));
313+
mlk_zeroize(extkey, sizeof(extkey));
326314
}
327315

328316
#if MLKEM_K == 2 || MLKEM_K == 4
@@ -400,42 +388,34 @@ void mlk_poly_getnoise_eta1122_4x(mlk_poly *r0, mlk_poly *r1, mlk_poly *r2,
400388
#if MLKEM_ETA2 >= MLKEM_ETA1
401389
#error mlk_poly_getnoise_eta1122_4x assumes MLKEM_ETA1 > MLKEM_ETA2
402390
#endif
403-
MLK_ALIGN uint8_t buf0[MLKEM_ETA1 * MLKEM_N / 4];
404-
MLK_ALIGN uint8_t buf1[MLKEM_ETA1 * MLKEM_N / 4];
405-
/* Pad to larger buffer */
406-
MLK_ALIGN uint8_t buf2[MLKEM_ETA1 * MLKEM_N / 4];
407-
MLK_ALIGN uint8_t buf3[MLKEM_ETA1 * MLKEM_N / 4];
408-
409-
MLK_ALIGN uint8_t extkey0[MLKEM_SYMBYTES + 1];
410-
MLK_ALIGN uint8_t extkey1[MLKEM_SYMBYTES + 1];
411-
MLK_ALIGN uint8_t extkey2[MLKEM_SYMBYTES + 1];
412-
MLK_ALIGN uint8_t extkey3[MLKEM_SYMBYTES + 1];
413-
414-
memcpy(extkey0, seed, MLKEM_SYMBYTES);
415-
memcpy(extkey1, seed, MLKEM_SYMBYTES);
416-
memcpy(extkey2, seed, MLKEM_SYMBYTES);
417-
memcpy(extkey3, seed, MLKEM_SYMBYTES);
418-
extkey0[MLKEM_SYMBYTES] = nonce0;
419-
extkey1[MLKEM_SYMBYTES] = nonce1;
420-
extkey2[MLKEM_SYMBYTES] = nonce2;
421-
extkey3[MLKEM_SYMBYTES] = nonce3;
391+
MLK_ALIGN uint8_t buf[4][MLK_ALIGN_UP(MLKEM_ETA1 * MLKEM_N / 4)];
392+
MLK_ALIGN uint8_t extkey[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 1)];
393+
394+
memcpy(extkey[0], seed, MLKEM_SYMBYTES);
395+
memcpy(extkey[1], seed, MLKEM_SYMBYTES);
396+
memcpy(extkey[2], seed, MLKEM_SYMBYTES);
397+
memcpy(extkey[3], seed, MLKEM_SYMBYTES);
398+
extkey[0][MLKEM_SYMBYTES] = nonce0;
399+
extkey[1][MLKEM_SYMBYTES] = nonce1;
400+
extkey[2][MLKEM_SYMBYTES] = nonce2;
401+
extkey[3][MLKEM_SYMBYTES] = nonce3;
422402

423403
/* On systems with fast batched Keccak, we use 4-fold batched PRF,
424-
* even though that means generating more random data in buf2 and buf3
404+
* even though that means generating more random data in buf[2] and buf[3]
425405
* than necessary. */
426406
#if !defined(FIPS202_X4_DEFAULT_IMPLEMENTATION)
427-
mlk_prf_eta1_x4(buf0, buf1, buf2, buf3, extkey0, extkey1, extkey2, extkey3);
407+
mlk_prf_eta1_x4(buf, extkey);
428408
#else /* FIPS202_X4_DEFAULT_IMPLEMENTATION */
429-
mlk_prf_eta1(buf0, extkey0);
430-
mlk_prf_eta1(buf1, extkey1);
431-
mlk_prf_eta2(buf2, extkey2);
432-
mlk_prf_eta2(buf3, extkey3);
409+
mlk_prf_eta1(buf[0], extkey[0]);
410+
mlk_prf_eta1(buf[1], extkey[1]);
411+
mlk_prf_eta2(buf[2], extkey[2]);
412+
mlk_prf_eta2(buf[3], extkey[3]);
433413
#endif /* FIPS202_X4_DEFAULT_IMPLEMENTATION */
434414

435-
mlk_poly_cbd_eta1(r0, buf0);
436-
mlk_poly_cbd_eta1(r1, buf1);
437-
mlk_poly_cbd_eta2(r2, buf2);
438-
mlk_poly_cbd_eta2(r3, buf3);
415+
mlk_poly_cbd_eta1(r0, buf[0]);
416+
mlk_poly_cbd_eta1(r1, buf[1]);
417+
mlk_poly_cbd_eta2(r2, buf[2]);
418+
mlk_poly_cbd_eta2(r3, buf[3]);
439419

440420
mlk_assert_abs_bound(r0, MLKEM_N, MLKEM_ETA1 + 1);
441421
mlk_assert_abs_bound(r1, MLKEM_N, MLKEM_ETA1 + 1);
@@ -444,14 +424,8 @@ void mlk_poly_getnoise_eta1122_4x(mlk_poly *r0, mlk_poly *r1, mlk_poly *r2,
444424

445425
/* Specification: Partially implements
446426
* [FIPS 203, Section 3.3, Destruction of intermediate values] */
447-
mlk_zeroize(buf0, sizeof(buf0));
448-
mlk_zeroize(buf1, sizeof(buf1));
449-
mlk_zeroize(buf2, sizeof(buf2));
450-
mlk_zeroize(buf3, sizeof(buf3));
451-
mlk_zeroize(extkey0, sizeof(extkey0));
452-
mlk_zeroize(extkey1, sizeof(extkey1));
453-
mlk_zeroize(extkey2, sizeof(extkey2));
454-
mlk_zeroize(extkey3, sizeof(extkey3));
427+
mlk_zeroize(buf, sizeof(buf));
428+
mlk_zeroize(extkey, sizeof(extkey));
455429
}
456430
#endif /* MLKEM_K == 2 */
457431

mlkem/sampling.c

+18-25
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,31 @@ __contract__(
132132
* - x4-batched version of `rej_uniform()` from the
133133
* reference implementation, leveraging x4-batched Keccak-f1600. */
134134
MLK_INTERNAL_API
135-
void mlk_poly_rej_uniform_x4(mlk_poly *vec, uint8_t *seed[4])
135+
void mlk_poly_rej_uniform_x4(mlk_poly *vec,
136+
uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)])
136137
{
137138
/* Temporary buffers for XOF output before rejection sampling */
138-
MLK_ALIGN uint8_t buf0[MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE];
139-
MLK_ALIGN uint8_t buf1[MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE];
140-
MLK_ALIGN uint8_t buf2[MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE];
141-
MLK_ALIGN uint8_t buf3[MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE];
139+
MLK_ALIGN uint8_t
140+
buf[4][MLK_ALIGN_UP(MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE)];
142141

143142
/* Tracks the number of coefficients we have already sampled */
144143
unsigned ctr[4];
145144
mlk_xof_x4_ctx statex;
146145
unsigned buflen;
147146

148-
/* seed is MLKEM_SYMBYTES + 2 bytes long, but padded to MLKEM_SYMBYTES + 16 */
149147
mlk_xof_x4_init(&statex);
150-
mlk_xof_x4_absorb(&statex, seed[0], seed[1], seed[2], seed[3],
151-
MLKEM_SYMBYTES + 2);
148+
mlk_xof_x4_absorb(&statex, seed, MLKEM_SYMBYTES + 2);
152149

153150
/*
154151
* Initially, squeeze heuristic number of MLKEM_GEN_MATRIX_NBLOCKS.
155152
* This should generate the matrix entries with high probability.
156153
*/
157-
mlk_xof_x4_squeezeblocks(buf0, buf1, buf2, buf3, MLKEM_GEN_MATRIX_NBLOCKS,
158-
&statex);
154+
mlk_xof_x4_squeezeblocks(buf, MLKEM_GEN_MATRIX_NBLOCKS, &statex);
159155
buflen = MLKEM_GEN_MATRIX_NBLOCKS * MLK_XOF_RATE;
160-
ctr[0] = mlk_rej_uniform(vec[0].coeffs, MLKEM_N, 0, buf0, buflen);
161-
ctr[1] = mlk_rej_uniform(vec[1].coeffs, MLKEM_N, 0, buf1, buflen);
162-
ctr[2] = mlk_rej_uniform(vec[2].coeffs, MLKEM_N, 0, buf2, buflen);
163-
ctr[3] = mlk_rej_uniform(vec[3].coeffs, MLKEM_N, 0, buf3, buflen);
156+
ctr[0] = mlk_rej_uniform(vec[0].coeffs, MLKEM_N, 0, buf[0], buflen);
157+
ctr[1] = mlk_rej_uniform(vec[1].coeffs, MLKEM_N, 0, buf[1], buflen);
158+
ctr[2] = mlk_rej_uniform(vec[2].coeffs, MLKEM_N, 0, buf[2], buflen);
159+
ctr[3] = mlk_rej_uniform(vec[3].coeffs, MLKEM_N, 0, buf[3], buflen);
164160

165161
/*
166162
* So long as not all matrix entries have been generated, squeeze
@@ -170,30 +166,27 @@ void mlk_poly_rej_uniform_x4(mlk_poly *vec, uint8_t *seed[4])
170166
while (ctr[0] < MLKEM_N || ctr[1] < MLKEM_N || ctr[2] < MLKEM_N ||
171167
ctr[3] < MLKEM_N)
172168
__loop__(
173-
assigns(ctr, statex, memory_slice(vec, sizeof(mlk_poly) * 4), object_whole(buf0),
174-
object_whole(buf1), object_whole(buf2), object_whole(buf3))
169+
assigns(ctr, statex, memory_slice(vec, sizeof(mlk_poly) * 4), object_whole(buf[0]),
170+
object_whole(buf[1]), object_whole(buf[2]), object_whole(buf[3]))
175171
invariant(ctr[0] <= MLKEM_N && ctr[1] <= MLKEM_N)
176172
invariant(ctr[2] <= MLKEM_N && ctr[3] <= MLKEM_N)
177173
invariant(array_bound(vec[0].coeffs, 0, ctr[0], 0, MLKEM_Q))
178174
invariant(array_bound(vec[1].coeffs, 0, ctr[1], 0, MLKEM_Q))
179175
invariant(array_bound(vec[2].coeffs, 0, ctr[2], 0, MLKEM_Q))
180176
invariant(array_bound(vec[3].coeffs, 0, ctr[3], 0, MLKEM_Q)))
181177
{
182-
mlk_xof_x4_squeezeblocks(buf0, buf1, buf2, buf3, 1, &statex);
183-
ctr[0] = mlk_rej_uniform(vec[0].coeffs, MLKEM_N, ctr[0], buf0, buflen);
184-
ctr[1] = mlk_rej_uniform(vec[1].coeffs, MLKEM_N, ctr[1], buf1, buflen);
185-
ctr[2] = mlk_rej_uniform(vec[2].coeffs, MLKEM_N, ctr[2], buf2, buflen);
186-
ctr[3] = mlk_rej_uniform(vec[3].coeffs, MLKEM_N, ctr[3], buf3, buflen);
178+
mlk_xof_x4_squeezeblocks(buf, 1, &statex);
179+
ctr[0] = mlk_rej_uniform(vec[0].coeffs, MLKEM_N, ctr[0], buf[0], buflen);
180+
ctr[1] = mlk_rej_uniform(vec[1].coeffs, MLKEM_N, ctr[1], buf[1], buflen);
181+
ctr[2] = mlk_rej_uniform(vec[2].coeffs, MLKEM_N, ctr[2], buf[2], buflen);
182+
ctr[3] = mlk_rej_uniform(vec[3].coeffs, MLKEM_N, ctr[3], buf[3], buflen);
187183
}
188184

189185
mlk_xof_x4_release(&statex);
190186

191187
/* Specification: Partially implements
192188
* [FIPS 203, Section 3.3, Destruction of intermediate values] */
193-
mlk_zeroize(buf0, sizeof(buf0));
194-
mlk_zeroize(buf1, sizeof(buf1));
195-
mlk_zeroize(buf2, sizeof(buf2));
196-
mlk_zeroize(buf3, sizeof(buf3));
189+
mlk_zeroize(buf, sizeof(buf));
197190
}
198191

199192
MLK_INTERNAL_API

mlkem/sampling.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,21 @@ void mlk_poly_cbd3(mlk_poly *r, const uint8_t buf[3 * MLKEM_N / 4]);
5555
* Description: Generate four polynomials using rejection sampling
5656
* on (pseudo-)uniformly random bytes sampled from a seed.
5757
*
58-
* Arguments: - mlk_poly *vec: Pointer to an array of 4 polynomials
59-
* to be sampled.
60-
* - uint8_t *seed[4]: Pointer to array of four pointers
61-
* pointing to the seed buffers of size
62-
* MLKEM_SYMBYTES + 2 each.
58+
* Arguments: - mlk_poly *vec:
59+
* Pointer to an array of 4 polynomials to be sampled.
60+
* - uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)]:
61+
* Pointer consecutive array of seed buffers of size
62+
* MLKEM_SYMBYTES + 2 each, plus padding for alignment.
6363
*
6464
* Specification: Implements [FIPS 203, Algorithm 7, SampleNTT]
6565
*
6666
**************************************************/
6767
MLK_INTERNAL_API
68-
void mlk_poly_rej_uniform_x4(mlk_poly *vec, uint8_t *seed[4])
68+
void mlk_poly_rej_uniform_x4(mlk_poly *vec,
69+
uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)])
6970
__contract__(
7071
requires(memory_no_alias(vec, sizeof(mlk_poly) * 4))
71-
requires(memory_no_alias(seed, sizeof(uint8_t*) * 4))
72-
requires(memory_no_alias(seed[0], MLKEM_SYMBYTES + 2))
73-
requires(memory_no_alias(seed[1], MLKEM_SYMBYTES + 2))
74-
requires(memory_no_alias(seed[2], MLKEM_SYMBYTES + 2))
75-
requires(memory_no_alias(seed[3], MLKEM_SYMBYTES + 2))
72+
requires(memory_no_alias(seed, 4 * MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)))
7673
assigns(memory_slice(vec, sizeof(mlk_poly) * 4))
7774
ensures(array_bound(vec[0].coeffs, 0, MLKEM_N, 0, MLKEM_Q))
7875
ensures(array_bound(vec[1].coeffs, 0, MLKEM_N, 0, MLKEM_Q))

mlkem/symmetric.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
mlk_shake256(OUT, (ETA) * MLKEM_N / 4, IN, MLKEM_SYMBYTES + 1)
3131
#define mlk_prf_eta1(OUT, IN) mlk_prf_eta(MLKEM_ETA1, OUT, IN)
3232
#define mlk_prf_eta2(OUT, IN) mlk_prf_eta(MLKEM_ETA2, OUT, IN)
33-
#define mlk_prf_eta1_x4(OUT0, OUT1, OUT2, OUT3, IN0, IN1, IN2, IN3) \
34-
mlk_shake256x4(OUT0, OUT1, OUT2, OUT3, (MLKEM_ETA1 * MLKEM_N / 4), IN0, IN1, \
35-
IN2, IN3, MLKEM_SYMBYTES + 1)
33+
#define mlk_prf_eta1_x4(OUT, IN) \
34+
mlk_shake256x4((OUT)[0], (OUT)[1], (OUT)[2], (OUT)[3], \
35+
(MLKEM_ETA1 * MLKEM_N / 4), (IN)[0], (IN)[1], (IN)[2], \
36+
(IN)[3], MLKEM_SYMBYTES + 1)
3637

3738
/* XOF function, FIPS 203 4.1 */
3839
#define mlk_xof_ctx mlk_shake128ctx
@@ -45,10 +46,12 @@
4546
#define mlk_xof_release(CTX) mlk_shake128_release((CTX))
4647

4748
#define mlk_xof_x4_init(CTX) mlk_shake128x4_init((CTX))
48-
#define mlk_xof_x4_absorb(CTX, IN0, IN1, IN2, IN3, INBYTES) \
49-
mlk_shake128x4_absorb_once((CTX), (IN0), (IN1), (IN2), (IN3), (INBYTES))
50-
#define mlk_xof_x4_squeezeblocks(BUF0, BUF1, BUF2, BUF3, NBLOCKS, CTX) \
51-
mlk_shake128x4_squeezeblocks((BUF0), (BUF1), (BUF2), (BUF3), (NBLOCKS), (CTX))
49+
#define mlk_xof_x4_absorb(CTX, IN, INBYTES) \
50+
mlk_shake128x4_absorb_once((CTX), (IN)[0], (IN)[1], (IN)[2], (IN)[3], \
51+
(INBYTES))
52+
#define mlk_xof_x4_squeezeblocks(BUF, NBLOCKS, CTX) \
53+
mlk_shake128x4_squeezeblocks((BUF)[0], (BUF)[1], (BUF)[2], (BUF)[3], \
54+
(NBLOCKS), (CTX))
5255
#define mlk_xof_x4_release(CTX) mlk_shake128x4_release((CTX))
5356

5457
#define MLK_XOF_RATE SHAKE128_RATE

mlkem/sys.h

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
#endif
110110

111111
#define MLK_DEFAULT_ALIGN 32
112+
#define MLK_ALIGN_UP(N) \
113+
((((N) + (MLK_DEFAULT_ALIGN - 1)) / MLK_DEFAULT_ALIGN) * MLK_DEFAULT_ALIGN)
112114
#if defined(__GNUC__)
113115
#define MLK_ALIGN __attribute__((aligned(MLK_DEFAULT_ALIGN)))
114116
#elif defined(_MSC_VER)

proofs/cbmc/poly_rej_uniform_x4/poly_rej_uniform_x4_harness.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
void harness(void)
99
{
1010
mlk_poly out[4];
11-
uint8_t *seed[4];
11+
uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)];
1212
mlk_poly_rej_uniform_x4(out, seed);
1313
}

0 commit comments

Comments
 (0)