Debian Codegraph @

cipher/dilithium.c

raw
1 /* dilithium.c - the Dilithium (main part)
2 * Copyright (C) 2025 g10 Code GmbH
3 *
4 * This file was modified for use by Libgcrypt.
5 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 *
20 * You can also use this file under the same licence of original code.
21 * SPDX-License-Identifier: CC0 OR Apache-2.0
22 *
23 */
24 /*
25 Original code from:
26
27 Repository: https://github.com/pq-crystals/dilithium.git
28 Branch: master
29 Commit: 444cdcc84eb36b66fe27b3a2529ee48f6d8150c2
30
31 Licence:
32 Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/);
33 or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html).
34
35 Authors:
36 Léo Ducas
37 Eike Kiltz
38 Tancrède Lepoint
39 Vadim Lyubashevsky
40 Gregor Seiler
41 Peter Schwabe
42 Damien Stehlé
43
44 Dilithium Home: https://github.com/pq-crystals/dilithium.git
45 */
46 /*
47 * This implementation consists of four files: dilithium.h (header),
48 * dilithium.c (this), dilithium-common.c (common part), and
49 * dilithium-dep.c (DILITHIUM_MODE dependent part).
50 *
51 * It is for inclusion in libgcrypt library. Also, standalone use of
52 * the implementation is possible. With DILITHIUM_MODE defined, it
53 * can offer the variant of that DILITHIUM_MODE specified. Otherwise,
54 * three variants are offered.
55 *
56 * From original code, following modification was made.
57 *
58 * - C++ style comments are changed to C-style.
59 *
60 * - No use of DILITHIUM_NAMESPACE and FIPS202_NAMESPACE. Don't export
61 * internal symbols.
62 *
63 * - Different external API for shake128 and shake256, having _close.
64 *
65 * - Add crypto_sign_keypair_internal function.
66 */
67
68 #ifdef HAVE_CONFIG_H
69 #include <config.h>
70 #endif
71
72 #include <stddef.h>
73 #include <stdint.h>
74 #include <string.h>
75
76 #ifdef _GCRYPT_IN_LIBGCRYPT
77 #include <stdarg.h>
78 #include <gpg-error.h>
79
80 #include "types.h"
81 #include "g10lib.h"
82 #include "gcrypt-int.h"
83 #include "const-time.h"
84
85 /* With glue code, we only use the "_internal" API of Dilithium. */
86 #define DILITHIUM_INTERNAL_API_ONLY 1
87
88 #include "dilithium.h"
89
90 static int crypto_sign_keypair_internal_2 (uint8_t *pk, uint8_t *sk,
91 const uint8_t seed[SEEDBYTES]);
92 static int crypto_sign_keypair_internal_3 (uint8_t *pk, uint8_t *sk,
93 const uint8_t seed[SEEDBYTES]);
94 static int crypto_sign_keypair_internal_5 (uint8_t *pk, uint8_t *sk,
95 const uint8_t seed[SEEDBYTES]);
96 static int crypto_sign_signature_internal_2 (uint8_t *sig, size_t *siglen,
97 const uint8_t *m, size_t mlen,
98 const uint8_t *pre, size_t prelen,
99 const uint8_t rnd[RNDBYTES],
100 const uint8_t *sk);
101 static int crypto_sign_signature_internal_3 (uint8_t *sig, size_t *siglen,
102 const uint8_t *m, size_t mlen,
103 const uint8_t *pre, size_t prelen,
104 const uint8_t rnd[RNDBYTES],
105 const uint8_t *sk);
106 static int crypto_sign_signature_internal_5 (uint8_t *sig, size_t *siglen,
107 const uint8_t *m, size_t mlen,
108 const uint8_t *pre, size_t prelen,
109 const uint8_t rnd[RNDBYTES],
110 const uint8_t *sk);
111 static int crypto_sign_verify_internal_2 (const uint8_t *sig, size_t siglen,
112 const uint8_t *m, size_t mlen,
113 const uint8_t *pre, size_t prelen,
114 const uint8_t *pk);
115 static int crypto_sign_verify_internal_3 (const uint8_t *sig, size_t siglen,
116 const uint8_t *m, size_t mlen,
117 const uint8_t *pre, size_t prelen,
118 const uint8_t *pk);
119 static int crypto_sign_verify_internal_5 (const uint8_t *sig, size_t siglen,
120 const uint8_t *m, size_t mlen,
121 const uint8_t *pre, size_t prelen,
122 const uint8_t *pk);
123
124 gpg_err_code_t
125 dilithium_keypair (int algo, uint8_t *pk, uint8_t *sk,
126 const uint8_t seed[SEEDBYTES])
127 {
128 int r;
129
130 switch (algo)
131 {
132 case GCRY_MLDSA44:
133 r = crypto_sign_keypair_internal_2 (pk, sk, seed);
134 break;
135 case GCRY_MLDSA65:
136 default:
137 r = crypto_sign_keypair_internal_3 (pk, sk, seed);
138 break;
139 case GCRY_MLDSA87:
140 r = crypto_sign_keypair_internal_5 (pk, sk, seed);
141 break;
142 }
143
144 if (r < 0)
145 return GPG_ERR_INTERNAL;
146
147 return 0;
148 }
149
150 gpg_err_code_t
151 dilithium_sign (int algo, uint8_t *sig, size_t siglen,
152 const uint8_t *m, size_t mlen,
153 const uint8_t *ctx, size_t ctxlen,
154 const uint8_t *sk, const uint8_t rnd[RNDBYTES])
155 {
156 size_t i;
157 uint8_t pre[257];
158 size_t prelen;
159 int r;
160
161 if (ctx == NULL)
162 {
163 if (ctxlen == -1)
164 prelen = 0;
165 else
166 return GPG_ERR_INV_DATA;
167 }
168 else if (ctxlen > 255)
169 return GPG_ERR_INV_DATA;
170 else
171 {
172 /* Prepare pre = (0, ctxlen, ctx) */
173 pre[0] = 0;
174 pre[1] = ctxlen;
175 for(i = 0; i < ctxlen; i++)
176 pre[2 + i] = ctx[i];
177 prelen = 2 + ctxlen;
178 }
179
180 /*
181 * Note that the second argument of the upstream routine is the
182 * pointer to output length of signature. It assumes the first
183 * argument (pointer to output signature) should have correct (or
184 * more) length, beforehand.
185 *
186 * Before calling the routine, we should check the length.
187 */
188 switch (algo)
189 {
190 case GCRY_MLDSA44:
191 if (siglen != CRYPTO_BYTES_2)
192 return GPG_ERR_INV_DATA;
193 r = crypto_sign_signature_internal_2 (sig, &siglen, m, mlen,
194 pre, prelen, rnd, sk);
195 break;
196 case GCRY_MLDSA65:
197 default:
198 if (siglen != CRYPTO_BYTES_3)
199 return GPG_ERR_INV_DATA;
200 r = crypto_sign_signature_internal_3 (sig, &siglen, m, mlen,
201 pre, prelen, rnd, sk);
202 break;
203 case GCRY_MLDSA87:
204 if (siglen != CRYPTO_BYTES_5)
205 return GPG_ERR_INV_DATA;
206 r = crypto_sign_signature_internal_5 (sig, &siglen, m, mlen,
207 pre, prelen, rnd, sk);
208 break;
209 }
210
211 if (r < 0)
212 return GPG_ERR_INTERNAL;
213
214 return 0;
215 }
216
217 gpg_err_code_t
218 dilithium_verify (int algo, const uint8_t *sig, size_t siglen,
219 const uint8_t *m, size_t mlen,
220 const uint8_t *ctx, size_t ctxlen,
221 const uint8_t *pk)
222 {
223 size_t i;
224 uint8_t pre[257];
225 size_t prelen;
226 int r;
227
228 if (ctx == NULL)
229 {
230 if (ctxlen == -1)
231 prelen = 0;
232 else
233 return GPG_ERR_INV_DATA;
234 }
235 else if (ctxlen > 255)
236 return GPG_ERR_INV_DATA;
237 else
238 {
239 /* Prepare pre = (0, ctxlen, ctx) */
240 pre[0] = 0;
241 pre[1] = ctxlen;
242 for(i = 0; i < ctxlen; i++)
243 pre[2 + i] = ctx[i];
244 prelen = 2 + ctxlen;
245 }
246
247 switch (algo)
248 {
249 case GCRY_MLDSA44:
250 r = crypto_sign_verify_internal_2 (sig, siglen, m, mlen,
251 pre, prelen, pk);
252 break;
253 case GCRY_MLDSA65:
254 default:
255 r = crypto_sign_verify_internal_3 (sig, siglen, m, mlen,
256 pre, prelen, pk);
257 break;
258 case GCRY_MLDSA87:
259 r = crypto_sign_verify_internal_5 (sig, siglen, m, mlen,
260 pre, prelen, pk);
261 break;
262 }
263
264 if (r < 0)
265 return GPG_ERR_BAD_SIGNATURE;
266
267 return 0;
268 }
269
270 typedef struct {
271 gcry_md_hd_t h;
272 } keccak_state;
273
274 static void
275 shake128_init (keccak_state *state)
276 {
277 gcry_err_code_t ec;
278
279 ec = _gcry_md_open (&state->h, GCRY_MD_SHAKE128, 0);
280 if (ec)
281 log_fatal ("internal md_open failed: %d\n", ec);
282 }
283
284 static void
285 shake128_absorb (keccak_state *state, const uint8_t *in, size_t inlen)
286 {
287 _gcry_md_write (state->h, in, inlen);
288 }
289
290 static void
291 shake128_finalize (keccak_state *state)
292 {
293 (void)state;
294 }
295
296 static void
297 shake128_squeeze (uint8_t *out, size_t outlen, keccak_state *state)
298 {
299 _gcry_md_extract (state->h, GCRY_MD_SHAKE128, out, outlen);
300 }
301
302 static void
303 shake128_close (keccak_state *state)
304 {
305 _gcry_md_close (state->h);
306 }
307
308 static void
309 shake256_init (keccak_state *state)
310 {
311 gcry_err_code_t ec;
312
313 ec = _gcry_md_open (&state->h, GCRY_MD_SHAKE256, 0);
314 if (ec)
315 log_fatal ("internal md_open failed: %d\n", ec);
316 }
317
318 static void
319 shake256_absorb (keccak_state *state, const uint8_t *in, size_t inlen)
320 {
321 _gcry_md_write (state->h, in, inlen);
322 }
323
324 static void
325 shake256_finalize (keccak_state *state)
326 {
327 (void)state;
328 }
329
330 static void
331 shake256_squeeze (uint8_t *out, size_t outlen, keccak_state *state)
332 {
333 _gcry_md_extract (state->h, GCRY_MD_SHAKE256, out, outlen);
334 }
335
336 static void
337 shake256_close (keccak_state *state)
338 {
339 _gcry_md_close (state->h);
340 }
341
342 static void
343 shake256 (uint8_t *out, size_t outlen, const uint8_t *in,
344 size_t inlen)
345 {
346 gcry_buffer_t iov[1];
347
348 iov[0].size = 0;
349 iov[0].data = (uint8_t *)in;
350 iov[0].off = 0;
351 iov[0].len = inlen;
352
353 _gcry_md_hash_buffers_extract (GCRY_MD_SHAKE256, 0, out, outlen,
354 iov, 1);
355 }
356 #else
357 #include "dilithium.h"
358
359 #define DILITHIUM_RANDOMIZED_SIGNING
360
361 /*************** dilithium/ref/randombytes.h */
362 void randombytes (uint8_t *out, size_t outlen);
363
364 /*************** dilithium/ref/fips202.h */
365 typedef struct {
366 uint64_t s[25];
367 unsigned int pos;
368 } keccak_state;
369
370 void shake128_init(keccak_state *state);
371 void shake128_absorb(keccak_state *state, const uint8_t *in, size_t inlen);
372 void shake128_finalize(keccak_state *state);
373 void shake128_squeeze(uint8_t *out, size_t outlen, keccak_state *state);
374
375 void shake256_init(keccak_state *state);
376 void shake256_absorb(keccak_state *state, const uint8_t *in, size_t inlen);
377 void shake256_finalize(keccak_state *state);
378 void shake256_squeeze(uint8_t *out, size_t outlen, keccak_state *state);
379 void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen);
380 #endif /* _GCRYPT_IN_LIBGCRYPT */
381
382 /*************** dilithium/ref/fips202.h */
383 #define SHAKE128_RATE 168
384 #define SHAKE256_RATE 136
385 #define SHA3_256_RATE 136
386
387 /*************** dilithium/ref/params.h */
388 #define CRHBYTES 64
389 #define TRBYTES 64
390 #define N 256
391 #define Q 8380417
392 #define D 13
393 #define ROOT_OF_UNITY 1753
394
395 #define POLYT1_PACKEDBYTES 320
396 #define POLYT0_PACKEDBYTES 416
397 #define POLYVECH_PACKEDBYTES (OMEGA + K)
398
399 /* DILITHIUM_MODE dependent values */
400 #define ETA2 2
401 #define ETA4 4
402 #define GAMMA1_17 (1 << 17)
403 #define GAMMA1_19 (1 << 19)
404 #define GAMMA2_32 ((Q-1)/32)
405 #define GAMMA2_88 ((Q-1)/88)
406 #define POLYZ_PACKEDBYTES_17 576
407 #define POLYZ_PACKEDBYTES_19 640
408 #define POLYW1_PACKEDBYTES_88 192
409 #define POLYW1_PACKEDBYTES_32 128
410 #define POLYETA_PACKEDBYTES_2 96
411 #define POLYETA_PACKEDBYTES_4 128
412
413 /*************** dilithium/ref/poly.h */
414 typedef struct {
415 int32_t coeffs[N];
416 } poly;
417
418 static void poly_reduce(poly *a);
419 static void poly_caddq(poly *a);
420
421 static void poly_add(poly *c, const poly *a, const poly *b);
422 static void poly_sub(poly *c, const poly *a, const poly *b);
423 static void poly_shiftl(poly *a);
424
425 static void poly_ntt(poly *a);
426 static void poly_invntt_tomont(poly *a);
427 static void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b);
428
429 static void poly_power2round(poly *a1, poly *a0, const poly *a);
430
431 static int poly_chknorm(const poly *a, int32_t B);
432 static void poly_uniform(poly *a,
433 const uint8_t seed[SEEDBYTES],
434 uint16_t nonce);
435
436 static void polyt1_pack(uint8_t *r, const poly *a);
437 static void polyt1_unpack(poly *r, const uint8_t *a);
438
439 static void polyt0_pack(uint8_t *r, const poly *a);
440 static void polyt0_unpack(poly *r, const uint8_t *a);
441
442 /*************** dilithium/ref/reduce.h */
443 #define MONT -4186625 /* 2^32 % Q */
444 #define QINV 58728449 /* q^(-1) mod 2^32 */
445
446 static int32_t montgomery_reduce(int64_t a);
447
448 static int32_t reduce32(int32_t a);
449
450 static int32_t caddq(int32_t a);
451
452 /*************** dilithium/ref/rounding.h */
453 static int32_t power2round(int32_t *a0, int32_t a);
454
455 /*************** dilithium/ref/symmetric.h */
456 typedef keccak_state stream128_state;
457 typedef keccak_state stream256_state;
458
459 static void dilithium_shake128_stream_init(keccak_state *state,
460 const uint8_t seed[SEEDBYTES],
461 uint16_t nonce);
462
463 static void dilithium_shake256_stream_init(keccak_state *state,
464 const uint8_t seed[CRHBYTES],
465 uint16_t nonce);
466
467 #define STREAM128_BLOCKBYTES SHAKE128_RATE
468 #define STREAM256_BLOCKBYTES SHAKE256_RATE
469
470 #define stream128_init(STATE, SEED, NONCE) \
471 dilithium_shake128_stream_init(STATE, SEED, NONCE)
472 #define stream128_squeezeblocks(OUT, OUTBLOCKS, STATE) \
473 shake128_squeezeblocks(OUT, OUTBLOCKS, STATE)
474 #define stream256_init(STATE, SEED, NONCE) \
475 dilithium_shake256_stream_init(STATE, SEED, NONCE)
476 #define stream256_squeezeblocks(OUT, OUTBLOCKS, STATE) \
477 shake256_squeezeblocks(OUT, OUTBLOCKS, STATE)
478
479 /*************** dilithium/ref/symmetric-shake.c */
480
481 void dilithium_shake128_stream_init(keccak_state *state, const uint8_t seed[SEEDBYTES], uint16_t nonce)
482 {
483 uint8_t t[2];
484 t[0] = nonce;
485 t[1] = nonce >> 8;
486
487 shake128_init(state);
488 shake128_absorb(state, seed, SEEDBYTES);
489 shake128_absorb(state, t, 2);
490 shake128_finalize(state);
491 }
492
493 void dilithium_shake256_stream_init(keccak_state *state, const uint8_t seed[CRHBYTES], uint16_t nonce)
494 {
495 uint8_t t[2];
496 t[0] = nonce;
497 t[1] = nonce >> 8;
498
499 shake256_init(state);
500 shake256_absorb(state, seed, CRHBYTES);
501 shake256_absorb(state, t, 2);
502 shake256_finalize(state);
503 }
504
505 /* Glue code */
506 #define shake128_squeezeblocks(OUT, OUTBLOCKS, STATE) \
507 shake128_squeeze(OUT, SHAKE128_RATE*OUTBLOCKS, STATE)
508 #define stream128_close(STATE) shake128_close(STATE)
509 #define shake256_squeezeblocks(OUT, OUTBLOCKS, STATE) \
510 shake256_squeeze(OUT, SHAKE256_RATE*OUTBLOCKS, STATE)
511 #define stream256_close(STATE) shake256_close(STATE)
512 #ifndef _GCRYPT_IN_LIBGCRYPT
513 static void shake256_close (keccak_state *state) { (void)state; }
514 static void shake128_close (keccak_state *state) { (void)state; }
515 #endif
516
517 #include "dilithium-common.c"
518
519 #ifdef DILITHIUM_MODE
520
521 # if DILITHIUM_MODE == 2
522 # define CRYPTO_ALGNAME "Dilithium2"
523 # define K 4
524 # define L 4
525 # define ETA 2
526 # define TAU 39
527 # define BETA 78
528 # define GAMMA1 (1 << 17)
529 # define GAMMA2 ((Q-1)/88)
530 # define OMEGA 80
531 # define CTILDEBYTES 32
532
533 # define poly_decompose poly_decompose_88
534 # define poly_make_hint poly_make_hint_88
535 # define poly_use_hint poly_use_hint_88
536 # define poly_uniform_eta poly_uniform_eta_2
537 # define poly_uniform_gamma1 poly_uniform_gamma1_17
538 # define polyz_pack polyz_pack_17
539 # define polyz_unpack polyz_unpack_17
540 # define polyeta_pack polyeta_pack_2
541 # define polyeta_unpack polyeta_unpack_2
542 # define polyw1_pack polyw1_pack_88
543
544 # elif DILITHIUM_MODE == 3
545 # define CRYPTO_ALGNAME "Dilithium3"
546 # define K 6
547 # define L 5
548 # define ETA 4
549 # define TAU 49
550 # define BETA 196
551 # define GAMMA1 (1 << 19)
552 # define GAMMA2 ((Q-1)/32)
553 # define OMEGA 55
554 # define CTILDEBYTES 48
555
556 # define poly_decompose poly_decompose_32
557 # define poly_make_hint poly_make_hint_32
558 # define poly_use_hint poly_use_hint_32
559 # define poly_uniform_eta poly_uniform_eta_4
560 # define poly_uniform_gamma1 poly_uniform_gamma1_19
561 # define polyz_pack polyz_pack_19
562 # define polyz_unpack polyz_unpack_19
563 # define polyeta_pack polyeta_pack_4
564 # define polyeta_unpack polyeta_unpack_4
565 # define polyw1_pack polyw1_pack_32
566
567 # elif DILITHIUM_MODE == 5
568 # define CRYPTO_ALGNAME "Dilithium5"
569 # define K 8
570 # define L 7
571 # define ETA 2
572 # define TAU 60
573 # define BETA 120
574 # define GAMMA1 (1 << 19)
575 # define GAMMA2 ((Q-1)/32)
576 # define OMEGA 75
577 # define CTILDEBYTES 64
578
579 # define poly_decompose poly_decompose_32
580 # define poly_make_hint poly_make_hint_32
581 # define poly_use_hint poly_use_hint_32
582 # define poly_uniform_eta poly_uniform_eta_2
583 # define poly_uniform_gamma1 poly_uniform_gamma1_19
584 # define polyz_pack polyz_pack_19
585 # define polyz_unpack polyz_unpack_19
586 # define polyeta_pack polyeta_pack_2
587 # define polyeta_unpack polyeta_unpack_2
588 # define polyw1_pack polyw1_pack_32
589
590 # endif
591
592 # if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2
593 # define POLYZ_PACKEDBYTES POLYZ_PACKEDBYTES_17
594 # endif
595 # if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5
596 # define POLYZ_PACKEDBYTES POLYZ_PACKEDBYTES_19
597 # endif
598
599 # if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2
600 # define POLYW1_PACKEDBYTES POLYW1_PACKEDBYTES_88
601 # endif
602 # if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5
603 # define POLYW1_PACKEDBYTES POLYW1_PACKEDBYTES_32
604 # endif
605
606 # if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 || DILITHIUM_MODE == 5
607 # define POLYETA_PACKEDBYTES POLYETA_PACKEDBYTES_2
608 # endif
609 # if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3
610 # define POLYETA_PACKEDBYTES POLYETA_PACKEDBYTES_4
611 # endif
612
613 # include "dilithium-dep.c"
614 #else
615
616 # define CRYPTO_ALGNAME "Dilithium"
617
618 # define VARIANT2(name) name ## _2
619 # define VARIANT3(name) name ## _3
620 # define VARIANT5(name) name ## _5
621
622 # define DILITHIUM_MODE 2
623
624 # define CRYPTO_PUBLICKEYBYTES CRYPTO_PUBLICKEYBYTES_2
625 # define CRYPTO_SECRETKEYBYTES CRYPTO_SECRETKEYBYTES_2
626 # define CRYPTO_BYTES CRYPTO_BYTES_2
627 # define POLYZ_PACKEDBYTES POLYZ_PACKEDBYTES_17
628 # define POLYW1_PACKEDBYTES POLYW1_PACKEDBYTES_88
629 # define POLYETA_PACKEDBYTES POLYETA_PACKEDBYTES_2
630
631 # define K 4
632 # define L 4
633 # define ETA 2
634 # define TAU 39
635 # define BETA 78
636 # define GAMMA1 (1 << 17)
637 # define GAMMA2 ((Q-1)/88)
638 # define OMEGA 80
639 # define CTILDEBYTES 32
640
641 # define poly_decompose poly_decompose_88
642 # define poly_make_hint poly_make_hint_88
643 # define poly_use_hint poly_use_hint_88
644 # define poly_uniform_eta poly_uniform_eta_2
645 # define poly_uniform_gamma1 poly_uniform_gamma1_17
646 # define polyz_pack polyz_pack_17
647 # define polyz_unpack polyz_unpack_17
648 # define polyeta_pack polyeta_pack_2
649 # define polyeta_unpack polyeta_unpack_2
650 # define polyw1_pack polyw1_pack_88
651
652 # define polyvecl VARIANT2(polyvecl)
653 # define polyveck VARIANT2(polyveck)
654 # define pack_pk VARIANT2(pack_pk)
655 # define unpack_pk VARIANT2(unpack_pk)
656 # define pack_sk VARIANT2(pack_sk)
657 # define unpack_sk VARIANT2(unpack_sk)
658 # define pack_sig VARIANT2(pack_sig)
659 # define unpack_sig VARIANT2(unpack_sig)
660 # define poly_challenge VARIANT2(poly_challenge)
661 # define polyvec_matrix_expand VARIANT2(polyvec_matrix_expand)
662 # define polyvec_matrix_pointwise_montgomery VARIANT2(polyvec_matrix_pointwise_montgomery)
663 # define polyveck_power2round VARIANT2(polyvec_power2round)
664 # define polyveck_make_hint VARIANT2(polyvec_make_hint)
665 # define polyveck_use_hint VARIANT2(polyvec_use_hint)
666 # define polyvecl_uniform_eta VARIANT2(polyvecl_uniform_eta)
667 # define polyvecl_uniform_gamma1 VARIANT2(polyvecl_uniform_gamma1)
668 # define polyvecl_reduce VARIANT2(polyvecl_reduce)
669 # define polyvecl_add VARIANT2(polyvecl_add)
670 # define polyvecl_ntt VARIANT2(polyvecl_ntt)
671 # define polyvecl_invntt_tomont VARIANT2(polyvecl_invntt_tomont)
672 # define polyvecl_pointwise_poly_montgomery VARIANT2(polyvecl_pointwise_poly_montgomery)
673 # define polyvecl_pointwise_acc_montgomery VARIANT2(polyvecl_pointwise_acc_montgomery)
674 # define polyvecl_chknorm VARIANT2(polyvecl_chknorm)
675 # define polyveck_uniform_eta VARIANT2(polyveck_uniform_eta)
676 # define polyveck_reduce VARIANT2(polyveck_reduce)
677 # define polyveck_caddq VARIANT2(polyveck_caddq)
678 # define polyveck_add VARIANT2(polyveck_add)
679 # define polyveck_sub VARIANT2(polyveck_sub)
680 # define polyveck_shiftl VARIANT2(polyveck_shiftl)
681 # define polyveck_ntt VARIANT2(polyveck_ntt)
682 # define polyveck_invntt_tomont VARIANT2(polyveck_invntt_tomont)
683 # define polyveck_pointwise_poly_montgomery VARIANT2(polyveck_pointwise_poly_montgomery)
684 # define polyveck_chknorm VARIANT2(polyveck_chknorm)
685 # define polyveck_pack_w1 VARIANT2(polyveck_pack_w1)
686 # define polyveck_decompose VARIANT2(polyveck_decompose)
687 # define crypto_sign_keypair VARIANT2(crypto_sign_keypair)
688 # define crypto_sign_keypair_internal VARIANT2(crypto_sign_keypair_internal)
689 # define crypto_sign_signature_internal VARIANT2(crypto_sign_signature_internal)
690 # define crypto_sign_signature VARIANT2(crypto_sign_signature)
691 # define crypto_sign VARIANT2(crypto_sign)
692 # define crypto_sign_verify_internal VARIANT2(crypto_sign_verify_internal)
693 # define crypto_sign_verify VARIANT2(crypto_sign_verify)
694 # define crypto_sign_open VARIANT2(crypto_sign_open)
695
696 # include "dilithium-dep.c"
697
698 # define DILITHIUM_MODE 3
699
700 # define CRYPTO_PUBLICKEYBYTES CRYPTO_PUBLICKEYBYTES_3
701 # define CRYPTO_SECRETKEYBYTES CRYPTO_SECRETKEYBYTES_3
702 # define CRYPTO_BYTES CRYPTO_BYTES_3
703 # define POLYZ_PACKEDBYTES POLYZ_PACKEDBYTES_19
704 # define POLYW1_PACKEDBYTES POLYW1_PACKEDBYTES_32
705 # define POLYETA_PACKEDBYTES POLYETA_PACKEDBYTES_4
706
707 # define K 6
708 # define L 5
709 # define ETA 4
710 # define TAU 49
711 # define BETA 196
712 # define GAMMA1 (1 << 19)
713 # define GAMMA2 ((Q-1)/32)
714 # define OMEGA 55
715 # define CTILDEBYTES 48
716
717 # define poly_decompose poly_decompose_32
718 # define poly_make_hint poly_make_hint_32
719 # define poly_use_hint poly_use_hint_32
720 # define poly_uniform_eta poly_uniform_eta_4
721 # define poly_uniform_gamma1 poly_uniform_gamma1_19
722 # define polyz_pack polyz_pack_19
723 # define polyz_unpack polyz_unpack_19
724 # define polyeta_pack polyeta_pack_4
725 # define polyeta_unpack polyeta_unpack_4
726 # define polyw1_pack polyw1_pack_32
727
728 # define polyvecl VARIANT3(polyvecl)
729 # define polyveck VARIANT3(polyveck)
730 # define pack_pk VARIANT3(pack_pk)
731 # define unpack_pk VARIANT3(unpack_pk)
732 # define pack_sk VARIANT3(pack_sk)
733 # define unpack_sk VARIANT3(unpack_sk)
734 # define pack_sig VARIANT3(pack_sig)
735 # define unpack_sig VARIANT3(unpack_sig)
736 # define poly_challenge VARIANT3(poly_challenge)
737 # define polyvec_matrix_expand VARIANT3(polyvec_matrix_expand)
738 # define polyvec_matrix_pointwise_montgomery VARIANT3(polyvec_matrix_pointwise_montgomery)
739 # define polyveck_power2round VARIANT3(polyvec_power2round)
740 # define polyveck_make_hint VARIANT3(polyvec_make_hint)
741 # define polyveck_use_hint VARIANT3(polyvec_use_hint)
742 # define polyvecl_uniform_eta VARIANT3(polyvecl_uniform_eta)
743 # define polyvecl_uniform_gamma1 VARIANT3(polyvecl_uniform_gamma1)
744 # define polyvecl_reduce VARIANT3(polyvecl_reduce)
745 # define polyvecl_add VARIANT3(polyvecl_add)
746 # define polyvecl_ntt VARIANT3(polyvecl_ntt)
747 # define polyvecl_invntt_tomont VARIANT3(polyvecl_invntt_tomont)
748 # define polyvecl_pointwise_poly_montgomery VARIANT3(polyvecl_pointwise_poly_montgomery)
749 # define polyvecl_pointwise_acc_montgomery VARIANT3(polyvecl_pointwise_acc_montgomery)
750 # define polyvecl_chknorm VARIANT3(polyvecl_chknorm)
751 # define polyveck_uniform_eta VARIANT3(polyveck_uniform_eta)
752 # define polyveck_reduce VARIANT3(polyveck_reduce)
753 # define polyveck_caddq VARIANT3(polyveck_caddq)
754 # define polyveck_add VARIANT3(polyveck_add)
755 # define polyveck_sub VARIANT3(polyveck_sub)
756 # define polyveck_shiftl VARIANT3(polyveck_shiftl)
757 # define polyveck_ntt VARIANT3(polyveck_ntt)
758 # define polyveck_invntt_tomont VARIANT3(polyveck_invntt_tomont)
759 # define polyveck_pointwise_poly_montgomery VARIANT3(polyveck_pointwise_poly_montgomery)
760 # define polyveck_chknorm VARIANT3(polyveck_chknorm)
761 # define polyveck_pack_w1 VARIANT3(polyveck_pack_w1)
762 # define polyveck_decompose VARIANT3(polyveck_decompose)
763 # define crypto_sign_keypair VARIANT3(crypto_sign_keypair)
764 # define crypto_sign_keypair_internal VARIANT3(crypto_sign_keypair_internal)
765 # define crypto_sign_signature_internal VARIANT3(crypto_sign_signature_internal)
766 # define crypto_sign_signature VARIANT3(crypto_sign_signature)
767 # define crypto_sign VARIANT3(crypto_sign)
768 # define crypto_sign_verify_internal VARIANT3(crypto_sign_verify_internal)
769 # define crypto_sign_verify VARIANT3(crypto_sign_verify)
770 # define crypto_sign_open VARIANT3(crypto_sign_open)
771
772 # include "dilithium-dep.c"
773
774 # define DILITHIUM_MODE 5
775
776 # define CRYPTO_PUBLICKEYBYTES CRYPTO_PUBLICKEYBYTES_5
777 # define CRYPTO_SECRETKEYBYTES CRYPTO_SECRETKEYBYTES_5
778 # define CRYPTO_BYTES CRYPTO_BYTES_5
779 # define POLYZ_PACKEDBYTES POLYZ_PACKEDBYTES_19
780 # define POLYW1_PACKEDBYTES POLYW1_PACKEDBYTES_32
781 # define POLYETA_PACKEDBYTES POLYETA_PACKEDBYTES_2
782
783 # define K 8
784 # define L 7
785 # define ETA 2
786 # define TAU 60
787 # define BETA 120
788 # define GAMMA1 (1 << 19)
789 # define GAMMA2 ((Q-1)/32)
790 # define OMEGA 75
791 # define CTILDEBYTES 64
792
793 # define poly_decompose poly_decompose_32
794 # define poly_make_hint poly_make_hint_32
795 # define poly_use_hint poly_use_hint_32
796 # define poly_uniform_eta poly_uniform_eta_2
797 # define poly_uniform_gamma1 poly_uniform_gamma1_19
798 # define polyz_pack polyz_pack_19
799 # define polyz_unpack polyz_unpack_19
800 # define polyeta_pack polyeta_pack_2
801 # define polyeta_unpack polyeta_unpack_2
802 # define polyw1_pack polyw1_pack_32
803
804 # define polyvecl VARIANT5(polyvecl)
805 # define polyveck VARIANT5(polyveck)
806 # define pack_pk VARIANT5(pack_pk)
807 # define unpack_pk VARIANT5(unpack_pk)
808 # define pack_sk VARIANT5(pack_sk)
809 # define unpack_sk VARIANT5(unpack_sk)
810 # define pack_sig VARIANT5(pack_sig)
811 # define unpack_sig VARIANT5(unpack_sig)
812 # define poly_challenge VARIANT5(poly_challenge)
813 # define polyvec_matrix_expand VARIANT5(polyvec_matrix_expand)
814 # define polyvec_matrix_pointwise_montgomery VARIANT5(polyvec_matrix_pointwise_montgomery)
815 # define polyveck_power2round VARIANT5(polyvec_power2round)
816 # define polyveck_make_hint VARIANT5(polyvec_make_hint)
817 # define polyveck_use_hint VARIANT5(polyvec_use_hint)
818 # define polyvecl_uniform_eta VARIANT5(polyvecl_uniform_eta)
819 # define polyvecl_uniform_gamma1 VARIANT5(polyvecl_uniform_gamma1)
820 # define polyvecl_reduce VARIANT5(polyvecl_reduce)
821 # define polyvecl_add VARIANT5(polyvecl_add)
822 # define polyvecl_ntt VARIANT5(polyvecl_ntt)
823 # define polyvecl_invntt_tomont VARIANT5(polyvecl_invntt_tomont)
824 # define polyvecl_pointwise_poly_montgomery VARIANT5(polyvecl_pointwise_poly_montgomery)
825 # define polyvecl_pointwise_acc_montgomery VARIANT5(polyvecl_pointwise_acc_montgomery)
826 # define polyvecl_chknorm VARIANT5(polyvecl_chknorm)
827 # define polyveck_uniform_eta VARIANT5(polyveck_uniform_eta)
828 # define polyveck_reduce VARIANT5(polyveck_reduce)
829 # define polyveck_caddq VARIANT5(polyveck_caddq)
830 # define polyveck_add VARIANT5(polyveck_add)
831 # define polyveck_sub VARIANT5(polyveck_sub)
832 # define polyveck_shiftl VARIANT5(polyveck_shiftl)
833 # define polyveck_ntt VARIANT5(polyveck_ntt)
834 # define polyveck_invntt_tomont VARIANT5(polyveck_invntt_tomont)
835 # define polyveck_pointwise_poly_montgomery VARIANT5(polyveck_pointwise_poly_montgomery)
836 # define polyveck_chknorm VARIANT5(polyveck_chknorm)
837 # define polyveck_pack_w1 VARIANT5(polyveck_pack_w1)
838 # define polyveck_decompose VARIANT5(polyveck_decompose)
839 # define crypto_sign_keypair VARIANT5(crypto_sign_keypair)
840 # define crypto_sign_keypair_internal VARIANT5(crypto_sign_keypair_internal)
841 # define crypto_sign_signature_internal VARIANT5(crypto_sign_signature_internal)
842 # define crypto_sign_signature VARIANT5(crypto_sign_signature)
843 # define crypto_sign VARIANT5(crypto_sign)
844 # define crypto_sign_verify_internal VARIANT5(crypto_sign_verify_internal)
845 # define crypto_sign_verify VARIANT5(crypto_sign_verify)
846 # define crypto_sign_open VARIANT5(crypto_sign_open)
847
848 # include "dilithium-dep.c"
849
850 #endif