-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathindcpa.c
More file actions
599 lines (539 loc) · 20.3 KB
/
indcpa.c
File metadata and controls
599 lines (539 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Copyright (c) The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
*/
/* References
* ==========
*
* - [FIPS203]
* FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
* National Institute of Standards and Technology
* https://csrc.nist.gov/pubs/fips/203/final
*
* - [REF]
* CRYSTALS-Kyber C reference implementation
* Bos, Ducas, Kiltz, Lepoint, Lyubashevsky, Schanck, Schwabe, Seiler, Stehlé
* https://github.com/pq-crystals/kyber/tree/main/ref
*/
#include <stdint.h>
#include "cbmc.h"
#include "debug.h"
#include "indcpa.h"
#include "poly.h"
#include "poly_k.h"
#include "randombytes.h"
#include "sampling.h"
#include "symmetric.h"
/* Parameter set namespacing
* This is to facilitate building multiple instances
* of mlkem-native (e.g. with varying parameter sets)
* within a single compilation unit. */
#define mlk_pack_pk MLK_ADD_PARAM_SET(mlk_pack_pk)
#define mlk_unpack_pk MLK_ADD_PARAM_SET(mlk_unpack_pk)
#define mlk_pack_sk MLK_ADD_PARAM_SET(mlk_pack_sk)
#define mlk_unpack_sk MLK_ADD_PARAM_SET(mlk_unpack_sk)
#define mlk_pack_ciphertext MLK_ADD_PARAM_SET(mlk_pack_ciphertext)
#define mlk_unpack_ciphertext MLK_ADD_PARAM_SET(mlk_unpack_ciphertext)
#define mlk_matvec_mul MLK_ADD_PARAM_SET(mlk_matvec_mul)
#define mlk_polymat_permute_bitrev_to_custom \
MLK_ADD_PARAM_SET(mlk_polymat_permute_bitrev_to_custom)
/* End of parameter set namespacing */
/*************************************************
* Name: mlk_pack_pk
*
* Description: Serialize the public key as concatenation of the
* serialized vector of polynomials pk
* and the public seed used to generate the matrix A.
*
* Arguments: uint8_t *r: pointer to the output serialized public key
* mlk_polyvec pk: pointer to the input public-key mlk_polyvec.
* Must have coefficients within [0,..,q-1].
* const uint8_t *seed: pointer to the input public seed
*
* Specification:
* Implements @[FIPS203, Algorithm 13 (K-PKE.KeyGen), L19]
*
**************************************************/
static void mlk_pack_pk(uint8_t r[MLKEM_INDCPA_PUBLICKEYBYTES],
const mlk_polyvec *pk,
const uint8_t seed[MLKEM_SYMBYTES])
{
mlk_assert_bound_2d(pk->vec, MLKEM_K, MLKEM_N, 0, MLKEM_Q);
mlk_polyvec_tobytes(r, pk);
mlk_memcpy(r + MLKEM_POLYVECBYTES, seed, MLKEM_SYMBYTES);
}
/*************************************************
* Name: mlk_unpack_pk
*
* Description: De-serialize public key from a byte array;
* approximate inverse of mlk_pack_pk
*
* Arguments: - mlk_polyvec pk: pointer to output public-key polynomial
* vector Coefficients will be normalized to [0,..,q-1].
* - uint8_t *seed: pointer to output seed to generate matrix A
* - const uint8_t *packedpk: pointer to input serialized public
* key.
*
* Specification:
* Implements @[FIPS203, Algorithm 14 (K-PKE.Encrypt), L2-3]
*
**************************************************/
static void mlk_unpack_pk(mlk_polyvec *pk, uint8_t seed[MLKEM_SYMBYTES],
const uint8_t packedpk[MLKEM_INDCPA_PUBLICKEYBYTES])
{
mlk_polyvec_frombytes(pk, packedpk);
mlk_memcpy(seed, packedpk + MLKEM_POLYVECBYTES, MLKEM_SYMBYTES);
/* NOTE: If a modulus check was conducted on the PK, we know at this
* point that the coefficients of `pk` are unsigned canonical. The
* specifications and proofs, however, do _not_ assume this, and instead
* work with the easily provable bound by MLKEM_UINT12_LIMIT. */
}
/*************************************************
* Name: mlk_pack_sk
*
* Description: Serialize the secret key
*
* Arguments: - uint8_t *r: pointer to output serialized secret key
* - mlk_polyvec sk: pointer to input vector of polynomials
* (secret key)
*
* Specification:
* Implements @[FIPS203, Algorithm 13 (K-PKE.KeyGen), L20]
*
**************************************************/
static void mlk_pack_sk(uint8_t r[MLKEM_INDCPA_SECRETKEYBYTES],
const mlk_polyvec *sk)
{
mlk_assert_bound_2d(sk->vec, MLKEM_K, MLKEM_N, 0, MLKEM_Q);
mlk_polyvec_tobytes(r, sk);
}
/*************************************************
* Name: mlk_unpack_sk
*
* Description: De-serialize the secret key; inverse of mlk_pack_sk
*
* Arguments: - mlk_polyvec sk: pointer to output vector of polynomials
* (secret key)
* - const uint8_t *packedsk: pointer to input serialized secret
* key
*
* Specification:
* Implements @[FIPS203, Algorithm 15 (K-PKE.Decrypt), L5]
*
**************************************************/
static void mlk_unpack_sk(mlk_polyvec *sk,
const uint8_t packedsk[MLKEM_INDCPA_SECRETKEYBYTES])
{
mlk_polyvec_frombytes(sk, packedsk);
}
/*************************************************
* Name: mlk_pack_ciphertext
*
* Description: Serialize the ciphertext as concatenation of the
* compressed and serialized vector of polynomials b
* and the compressed and serialized polynomial v
*
* Arguments: uint8_t *r: pointer to the output serialized ciphertext
* mlk_poly *pk: pointer to the input vector of polynomials b
* mlk_poly *v: pointer to the input polynomial v
*
* Specification:
* Implements @[FIPS203, Algorithm 14 (K-PKE.Encrypt), L22-23]
*
**************************************************/
static void mlk_pack_ciphertext(uint8_t r[MLKEM_INDCPA_BYTES],
const mlk_polyvec *b, mlk_poly *v)
{
mlk_polyvec_compress_du(r, b);
mlk_poly_compress_dv(r + MLKEM_POLYVECCOMPRESSEDBYTES_DU, v);
}
/*************************************************
* Name: mlk_unpack_ciphertext
*
* Description: De-serialize and decompress ciphertext from a byte array;
* approximate inverse of mlk_pack_ciphertext
*
* Arguments: - mlk_polyvec b: pointer to the output vector of polynomials b
* - mlk_poly *v: pointer to the output polynomial v
* - const uint8_t *c: pointer to the input serialized ciphertext
*
* Specification:
* Implements @[FIPS203, Algorithm 15 (K-PKE.Decrypt), L1-4]
*
**************************************************/
static void mlk_unpack_ciphertext(mlk_polyvec *b, mlk_poly *v,
const uint8_t c[MLKEM_INDCPA_BYTES])
{
mlk_polyvec_decompress_du(b, c);
mlk_poly_decompress_dv(v, c + MLKEM_POLYVECCOMPRESSEDBYTES_DU);
}
static void mlk_polymat_permute_bitrev_to_custom(mlk_polymat *a)
__contract__(
/* We don't specify that this should be a permutation, but only
* that it does not change the bound established at the end of mlk_gen_matrix. */
requires(memory_no_alias(a, sizeof(mlk_polymat)))
requires(forall(x, 0, MLKEM_K, forall(y, 0, MLKEM_K,
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
assigns(memory_slice(a, sizeof(mlk_polymat)))
ensures(forall(x, 0, MLKEM_K, forall(y, 0, MLKEM_K,
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
{
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
unsigned i, j;
for (i = 0; i < MLKEM_K; i++)
__loop__(
assigns(i, j, memory_slice(a, sizeof(mlk_polymat)))
invariant(i <= MLKEM_K)
invariant(forall(x2, 0, MLKEM_K, forall(y2, 0, MLKEM_K,
array_bound(a->vec[x2].vec[y2].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
{
for (j = 0; j < MLKEM_K; j++)
__loop__(
assigns(j, memory_slice(a, sizeof(mlk_polymat)))
invariant(i <= MLKEM_K)
invariant(j <= MLKEM_K)
invariant(forall(x3, 0, MLKEM_K, forall(y3, 0, MLKEM_K,
array_bound(a->vec[x3].vec[y3].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
)
{
mlk_poly_permute_bitrev_to_custom(a->vec[i].vec[j].coeffs);
}
}
#else /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
/* Nothing to do */
(void)a;
#endif /* !MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
}
/* Reference: `gen_matrix()` in the reference implementation @[REF].
* - We use a special subroutine to generate 4 polynomials
* at a time, to be able to leverage batched Keccak-f1600
* implementations. The reference implementation generates
* one matrix entry a time.
*
* Not static for benchmarking */
MLK_INTERNAL_API
void mlk_gen_matrix(mlk_polymat *a, const uint8_t seed[MLKEM_SYMBYTES],
int transposed)
{
unsigned i, j;
MLK_ALIGN uint8_t seed_ext[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)];
for (j = 0; j < 4; j++)
{
mlk_memcpy(seed_ext[j], seed, MLKEM_SYMBYTES);
}
#if !defined(MLK_CONFIG_SERIAL_FIPS202_ONLY)
/* Sample 4 matrix entries a time. */
for (i = 0; i < (MLKEM_K * MLKEM_K / 4) * 4; i += 4)
{
for (j = 0; j < 4; j++)
{
uint8_t x, y;
/* MLKEM_K <= 4, so the values fit in uint8_t. */
x = (uint8_t)((i + j) / MLKEM_K);
y = (uint8_t)((i + j) % MLKEM_K);
if (transposed)
{
seed_ext[j][MLKEM_SYMBYTES + 0] = x;
seed_ext[j][MLKEM_SYMBYTES + 1] = y;
}
else
{
seed_ext[j][MLKEM_SYMBYTES + 0] = y;
seed_ext[j][MLKEM_SYMBYTES + 1] = x;
}
}
mlk_poly_rej_uniform_x4(&a->vec[i / MLKEM_K].vec[i % MLKEM_K],
&a->vec[(i + 1) / MLKEM_K].vec[(i + 1) % MLKEM_K],
&a->vec[(i + 2) / MLKEM_K].vec[(i + 2) % MLKEM_K],
&a->vec[(i + 3) / MLKEM_K].vec[(i + 3) % MLKEM_K],
seed_ext);
}
#else /* !MLK_CONFIG_SERIAL_FIPS202_ONLY */
/* When using serial FIPS202, sample all entries individually. */
i = 0;
#endif /* MLK_CONFIG_SERIAL_FIPS202_ONLY */
/* For MLKEM_K == 3, sample the last entry individually.
* When MLK_CONFIG_SERIAL_FIPS202_ONLY is set, sample all entries
* individually. */
for (; i < MLKEM_K * MLKEM_K; i++)
{
uint8_t x, y;
/* MLKEM_K <= 4, so the values fit in uint8_t. */
x = (uint8_t)(i / MLKEM_K);
y = (uint8_t)(i % MLKEM_K);
if (transposed)
{
seed_ext[0][MLKEM_SYMBYTES + 0] = x;
seed_ext[0][MLKEM_SYMBYTES + 1] = y;
}
else
{
seed_ext[0][MLKEM_SYMBYTES + 0] = y;
seed_ext[0][MLKEM_SYMBYTES + 1] = x;
}
mlk_poly_rej_uniform(&a->vec[i / MLKEM_K].vec[i % MLKEM_K], seed_ext[0]);
}
mlk_assert(i == MLKEM_K * MLKEM_K);
/*
* The public matrix is generated in NTT domain. If the native backend
* uses a custom order in NTT domain, permute A accordingly.
*/
mlk_polymat_permute_bitrev_to_custom(a);
/* Specification: Partially implements
* @[FIPS203, Section 3.3, Destruction of intermediate values] */
mlk_zeroize(seed_ext, sizeof(seed_ext));
}
/*************************************************
* Name: mlk_matvec_mul
*
* Description: Computes matrix-vector product in NTT domain,
* via Montgomery multiplication.
*
* Arguments: - mlk_polyvec out: Pointer to output polynomial vector
* - mlk_polymat a: Input matrix. Must be in NTT domain
* and have coefficients of absolute value < 4096.
* - mlk_polyvec v: Input polynomial vector. Must be in NTT
* domain.
* - mlk_polyvec vc: Mulcache for v, computed via
* mlk_polyvec_mulcache_compute().
*
* Specification: Implements @[FIPS203, Section 2.4.7, Eq (2.12), (2.13)]
*
**************************************************/
static void mlk_matvec_mul(mlk_polyvec *out, const mlk_polymat *a,
const mlk_polyvec *v, const mlk_polyvec_mulcache *vc)
__contract__(
requires(memory_no_alias(out, sizeof(mlk_polyvec)))
requires(memory_no_alias(a, sizeof(mlk_polymat)))
requires(memory_no_alias(v, sizeof(mlk_polyvec)))
requires(memory_no_alias(vc, sizeof(mlk_polyvec_mulcache)))
requires(forall(k0, 0, MLKEM_K,
forall(k1, 0, MLKEM_K,
array_bound(a->vec[k0].vec[k1].coeffs, 0, MLKEM_N, 0, MLKEM_UINT12_LIMIT))))
assigns(memory_slice(out, sizeof(mlk_polyvec))))
{
unsigned i;
for (i = 0; i < MLKEM_K; i++)
__loop__(
assigns(i, memory_slice(out, sizeof(mlk_polyvec)))
invariant(i <= MLKEM_K))
{
mlk_polyvec_basemul_acc_montgomery_cached(&out->vec[i], &a->vec[i], v, vc);
}
}
/* Reference: `indcpa_keypair_derand()` in the reference implementation @[REF].
* - We use x4-batched versions of `poly_getnoise` to leverage
* batched x4-batched Keccak-f1600.
* - We use a different implementation of `gen_matrix()` which
* uses x4-batched Keccak-f1600 (see `mlk_gen_matrix()` above).
* - We use a mulcache to speed up matrix-vector multiplication.
* - We include buffer zeroization.
*/
MLK_INTERNAL_API
int mlk_indcpa_keypair_derand(uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
uint8_t sk[MLKEM_INDCPA_SECRETKEYBYTES],
const uint8_t coins[MLKEM_SYMBYTES])
{
typedef struct
{
MLK_ALIGN uint8_t buf[2 * MLKEM_SYMBYTES];
MLK_ALIGN uint8_t coins_with_domain_separator[MLKEM_SYMBYTES + 1];
mlk_polymat a;
mlk_polyvec e;
mlk_polyvec pkpv;
mlk_polyvec skpv;
mlk_polyvec_mulcache skpv_cache;
} workspace;
int ret = 0;
const uint8_t *publicseed;
const uint8_t *noiseseed;
MLK_ALLOC(ws, workspace, 1);
if (ws == NULL)
{
ret = MLK_ERR_OUT_OF_MEMORY;
goto cleanup;
}
publicseed = ws->buf;
noiseseed = ws->buf + MLKEM_SYMBYTES;
/* Concatenate coins with MLKEM_K for domain separation of security levels */
mlk_memcpy(ws->coins_with_domain_separator, coins, MLKEM_SYMBYTES);
ws->coins_with_domain_separator[MLKEM_SYMBYTES] = MLKEM_K;
mlk_hash_g(ws->buf, ws->coins_with_domain_separator, MLKEM_SYMBYTES + 1);
/*
* Declassify the public seed.
* Required to use it in conditional-branches in rejection sampling.
* This is needed because all output of randombytes is marked as secret
* (=undefined)
*/
MLK_CT_TESTING_DECLASSIFY(publicseed, MLKEM_SYMBYTES);
mlk_gen_matrix(&ws->a, publicseed, 0 /* no transpose */);
#if MLKEM_K == 2
mlk_poly_getnoise_eta1_4x(&ws->skpv.vec[0], &ws->skpv.vec[1], &ws->e.vec[0],
&ws->e.vec[1], noiseseed, 0, 1, 2, 3);
#elif MLKEM_K == 3
/*
* Only the first three output buffers are needed.
* The laster parameter is a dummy that's overwritten later.
*/
mlk_poly_getnoise_eta1_4x(&ws->skpv.vec[0], &ws->skpv.vec[1],
&ws->skpv.vec[2], NULL, noiseseed, 0, 1, 2,
0xFF /* irrelevant */);
/* Same here */
mlk_poly_getnoise_eta1_4x(&ws->e.vec[0], &ws->e.vec[1], &ws->e.vec[2], NULL,
noiseseed, 3, 4, 5, 0xFF /* irrelevant */);
#elif MLKEM_K == 4
mlk_poly_getnoise_eta1_4x(&ws->skpv.vec[0], &ws->skpv.vec[1],
&ws->skpv.vec[2], &ws->skpv.vec[3], noiseseed, 0, 1,
2, 3);
mlk_poly_getnoise_eta1_4x(&ws->e.vec[0], &ws->e.vec[1], &ws->e.vec[2],
&ws->e.vec[3], noiseseed, 4, 5, 6, 7);
#endif /* MLKEM_K == 4 */
mlk_polyvec_ntt(&ws->skpv);
mlk_polyvec_ntt(&ws->e);
mlk_polyvec_mulcache_compute(&ws->skpv_cache, &ws->skpv);
mlk_matvec_mul(&ws->pkpv, &ws->a, &ws->skpv, &ws->skpv_cache);
mlk_polyvec_tomont(&ws->pkpv);
mlk_polyvec_add(&ws->pkpv, &ws->e);
mlk_polyvec_reduce(&ws->pkpv);
mlk_polyvec_reduce(&ws->skpv);
mlk_pack_sk(sk, &ws->skpv);
mlk_pack_pk(pk, &ws->pkpv, publicseed);
cleanup:
/* Specification: Partially implements
* @[FIPS203, Section 3.3, Destruction of intermediate values] */
MLK_FREE(ws, workspace, 1);
return ret;
}
/* Reference: `indcpa_enc()` in the reference implementation @[REF].
* - We use x4-batched versions of `poly_getnoise` to leverage
* batched x4-batched Keccak-f1600.
* - We use a different implementation of `gen_matrix()` which
* uses x4-batched Keccak-f1600 (see `mlk_gen_matrix()` above).
* - We use a mulcache to speed up matrix-vector multiplication.
* - We include buffer zeroization.
*/
MLK_INTERNAL_API
int mlk_indcpa_enc(uint8_t c[MLKEM_INDCPA_BYTES],
const uint8_t m[MLKEM_INDCPA_MSGBYTES],
const uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
const uint8_t coins[MLKEM_SYMBYTES])
{
typedef struct
{
MLK_ALIGN uint8_t seed[MLKEM_SYMBYTES];
mlk_polymat at;
mlk_polyvec sp;
mlk_polyvec pkpv;
mlk_polyvec ep;
mlk_polyvec b;
mlk_poly v;
mlk_poly k;
mlk_poly epp;
mlk_polyvec_mulcache sp_cache;
} workspace;
int ret = 0;
MLK_ALLOC(ws, workspace, 1);
if (ws == NULL)
{
ret = MLK_ERR_OUT_OF_MEMORY;
goto cleanup;
}
mlk_unpack_pk(&ws->pkpv, ws->seed, pk);
mlk_poly_frommsg(&ws->k, m);
/*
* Declassify the public seed.
* Required to use it in conditional-branches in rejection sampling.
* This is needed because in re-encryption the publicseed originated from sk
* which is marked undefined.
*/
MLK_CT_TESTING_DECLASSIFY(ws->seed, MLKEM_SYMBYTES);
mlk_gen_matrix(&ws->at, ws->seed, 1 /* transpose */);
#if MLKEM_K == 2
mlk_poly_getnoise_eta1122_4x(&ws->sp.vec[0], &ws->sp.vec[1], &ws->ep.vec[0],
&ws->ep.vec[1], coins, 0, 1, 2, 3);
mlk_poly_getnoise_eta2(&ws->epp, coins, 4);
#elif MLKEM_K == 3
/*
* In this call, only the first three output buffers are needed.
* The last parameter is a dummy that's overwritten later.
*/
mlk_poly_getnoise_eta1_4x(&ws->sp.vec[0], &ws->sp.vec[1], &ws->sp.vec[2],
NULL, coins, 0, 1, 2, 0xFF /* irrelevant */);
/* The fourth output buffer in this call _is_ used. */
mlk_poly_getnoise_eta2_4x(&ws->ep.vec[0], &ws->ep.vec[1], &ws->ep.vec[2],
&ws->epp, coins, 3, 4, 5, 6);
#elif MLKEM_K == 4
mlk_poly_getnoise_eta1_4x(&ws->sp.vec[0], &ws->sp.vec[1], &ws->sp.vec[2],
&ws->sp.vec[3], coins, 0, 1, 2, 3);
mlk_poly_getnoise_eta2_4x(&ws->ep.vec[0], &ws->ep.vec[1], &ws->ep.vec[2],
&ws->ep.vec[3], coins, 4, 5, 6, 7);
mlk_poly_getnoise_eta2(&ws->epp, coins, 8);
#endif /* MLKEM_K == 4 */
mlk_polyvec_ntt(&ws->sp);
mlk_polyvec_mulcache_compute(&ws->sp_cache, &ws->sp);
mlk_matvec_mul(&ws->b, &ws->at, &ws->sp, &ws->sp_cache);
mlk_polyvec_basemul_acc_montgomery_cached(&ws->v, &ws->pkpv, &ws->sp,
&ws->sp_cache);
mlk_polyvec_invntt_tomont(&ws->b);
mlk_poly_invntt_tomont(&ws->v);
mlk_polyvec_add(&ws->b, &ws->ep);
mlk_poly_add(&ws->v, &ws->epp);
mlk_poly_add(&ws->v, &ws->k);
mlk_polyvec_reduce(&ws->b);
mlk_poly_reduce(&ws->v);
mlk_pack_ciphertext(c, &ws->b, &ws->v);
cleanup:
/* Specification: Partially implements
* @[FIPS203, Section 3.3, Destruction of intermediate values] */
MLK_FREE(ws, workspace, 1);
return ret;
}
/* Reference: `indcpa_dec()` in the reference implementation @[REF].
* - We use a mulcache for the scalar product.
* - We include buffer zeroization. */
MLK_INTERNAL_API
int mlk_indcpa_dec(uint8_t m[MLKEM_INDCPA_MSGBYTES],
const uint8_t c[MLKEM_INDCPA_BYTES],
const uint8_t sk[MLKEM_INDCPA_SECRETKEYBYTES])
{
typedef struct
{
mlk_polyvec b;
mlk_polyvec skpv;
mlk_poly v;
mlk_poly sb;
mlk_polyvec_mulcache b_cache;
} workspace;
int ret = 0;
MLK_ALLOC(ws, workspace, 1);
if (ws == NULL)
{
ret = MLK_ERR_OUT_OF_MEMORY;
goto cleanup;
}
mlk_unpack_ciphertext(&ws->b, &ws->v, c);
mlk_unpack_sk(&ws->skpv, sk);
mlk_polyvec_ntt(&ws->b);
mlk_polyvec_mulcache_compute(&ws->b_cache, &ws->b);
mlk_polyvec_basemul_acc_montgomery_cached(&ws->sb, &ws->skpv, &ws->b,
&ws->b_cache);
mlk_poly_invntt_tomont(&ws->sb);
mlk_poly_sub(&ws->v, &ws->sb);
mlk_poly_reduce(&ws->v);
mlk_poly_tomsg(m, &ws->v);
cleanup:
/* Specification: Partially implements
* @[FIPS203, Section 3.3, Destruction of intermediate values] */
MLK_FREE(ws, workspace, 1);
return ret;
}
/* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
* Don't modify by hand -- this is auto-generated by scripts/autogen. */
#undef mlk_pack_pk
#undef mlk_unpack_pk
#undef mlk_pack_sk
#undef mlk_unpack_sk
#undef mlk_pack_ciphertext
#undef mlk_unpack_ciphertext
#undef mlk_matvec_mul
#undef mlk_polymat_permute_bitrev_to_custom