28#ifndef LC_DILITHIUM_44_H
29#define LC_DILITHIUM_44_H
33#include "ext_headers.h"
51#define LC_DILITHIUM_MODE 2
53#define LC_DILITHIUM_SEEDBYTES 32
54#define LC_DILITHIUM_CRHBYTES 64
55#define LC_DILITHIUM_TRBYTES 64
56#define LC_DILITHIUM_RNDBYTES 32
57#define LC_DILITHIUM_N 256
58#define LC_DILITHIUM_Q 8380417
59#define LC_DILITHIUM_D 13
60#define LC_DILITHIUM_ROOT_OF_UNITY 1753
62#if LC_DILITHIUM_MODE == 2
63#define LC_DILITHIUM_LAMBDA 128
64#define LC_DILITHIUM_K 4
65#define LC_DILITHIUM_L 4
66#define LC_DILITHIUM_ETA 2
67#define LC_DILITHIUM_TAU 39
68#define LC_DILITHIUM_BETA 78
69#define LC_DILITHIUM_GAMMA1 (1 << 17)
70#define LC_DILITHIUM_GAMMA2 ((LC_DILITHIUM_Q - 1) / 88)
71#define LC_DILITHIUM_OMEGA 80
73#elif LC_DILITHIUM_MODE == 3
74#define LC_DILITHIUM_LAMBDA 192
75#define LC_DILITHIUM_K 6
76#define LC_DILITHIUM_L 5
77#define LC_DILITHIUM_ETA 4
78#define LC_DILITHIUM_TAU 49
79#define LC_DILITHIUM_BETA 196
80#define LC_DILITHIUM_GAMMA1 (1 << 19)
81#define LC_DILITHIUM_GAMMA2 ((LC_DILITHIUM_Q - 1) / 32)
82#define LC_DILITHIUM_OMEGA 55
84#elif LC_DILITHIUM_MODE == 5
85#define LC_DILITHIUM_LAMBDA 256
86#define LC_DILITHIUM_K 8
87#define LC_DILITHIUM_L 7
88#define LC_DILITHIUM_ETA 2
89#define LC_DILITHIUM_TAU 60
90#define LC_DILITHIUM_BETA 120
91#define LC_DILITHIUM_GAMMA1 (1 << 19)
92#define LC_DILITHIUM_GAMMA2 ((LC_DILITHIUM_Q - 1) / 32)
93#define LC_DILITHIUM_OMEGA 75
97#define LC_DILITHIUM_CTILDE_BYTES (LC_DILITHIUM_LAMBDA * 2 / 8)
98#define LC_DILITHIUM_POLYT1_PACKEDBYTES 320
99#define LC_DILITHIUM_POLYT0_PACKEDBYTES 416
100#define LC_DILITHIUM_POLYVECH_PACKEDBYTES (LC_DILITHIUM_OMEGA + LC_DILITHIUM_K)
102#if LC_DILITHIUM_GAMMA1 == (1 << 17)
103#define LC_DILITHIUM_POLYZ_PACKEDBYTES 576
104#elif LC_DILITHIUM_GAMMA1 == (1 << 19)
105#define LC_DILITHIUM_POLYZ_PACKEDBYTES 640
108#if LC_DILITHIUM_GAMMA2 == (LC_DILITHIUM_Q - 1) / 88
109#define LC_DILITHIUM_POLYW1_PACKEDBYTES 192
110#elif LC_DILITHIUM_GAMMA2 == (LC_DILITHIUM_Q - 1) / 32
111#define LC_DILITHIUM_POLYW1_PACKEDBYTES 128
114#if LC_DILITHIUM_ETA == 2
115#define LC_DILITHIUM_POLYETA_PACKEDBYTES 96
116#elif LC_DILITHIUM_ETA == 4
117#define LC_DILITHIUM_POLYETA_PACKEDBYTES 128
128#define LC_DILITHIUM_PUBLICKEYBYTES \
129 (LC_DILITHIUM_SEEDBYTES + \
130 LC_DILITHIUM_K * LC_DILITHIUM_POLYT1_PACKEDBYTES)
131#define LC_DILITHIUM_SECRETKEYBYTES \
132 (2 * LC_DILITHIUM_SEEDBYTES + LC_DILITHIUM_TRBYTES + \
133 LC_DILITHIUM_L * LC_DILITHIUM_POLYETA_PACKEDBYTES + \
134 LC_DILITHIUM_K * LC_DILITHIUM_POLYETA_PACKEDBYTES + \
135 LC_DILITHIUM_K * LC_DILITHIUM_POLYT0_PACKEDBYTES)
137#define LC_DILITHIUM_CRYPTO_BYTES \
138 (LC_DILITHIUM_CTILDE_BYTES + \
139 LC_DILITHIUM_L * LC_DILITHIUM_POLYZ_PACKEDBYTES + \
140 LC_DILITHIUM_POLYVECH_PACKEDBYTES)
148 uint8_t
sk[LC_DILITHIUM_SECRETKEYBYTES];
155 uint8_t
pk[LC_DILITHIUM_PUBLICKEYBYTES];
162 uint8_t
sig[LC_DILITHIUM_CRYPTO_BYTES];
171#ifndef LC_DILITHIUM_CTX_ON_STACK
174 uint8_t
shake_state[LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE)];
179#ifndef LC_DILITHIUM_CTX_ON_STACK
180#define LC_DILITHIUM_CTX_SIZE \
181 sizeof(struct lc_dilithium_ctx)
190#ifndef LC_DILITHIUM_CTX_ON_STACK
191#define LC_DILITHIUM_CTX_ON_STACK(name) \
192 _Pragma("GCC diagnostic push") _Pragma( \
193 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
195 name##_ctx_buf, LC_DILITHIUM_ED25519_CTX_SIZE, \
196 LC_HASH_COMMON_ALIGNMENT); \
197 struct lc_dilithium_ctx *name = \
198 (struct lc_dilithium_ctx *)name##_ctx_buf; \
199 LC_SHAKE_256_CTX((&(name)->dilithium_hash_ctx)); \
200 _Pragma("GCC diagnostic pop")
269 struct lc_rng_ctx *rng_ctx);
292 const uint8_t *seed,
size_t seedlen);
309 struct lc_rng_ctx *rng_ctx);
361 struct lc_rng_ctx *rng_ctx);
430#define LC_DILITHIUM_ED25519_SIG
431#ifdef LC_DILITHIUM_ED25519_SIG
433#include "lc_ed25519.h"
466#ifndef LC_DILITHIUM_ED25519_CTX_ON_STACK
475#ifndef LC_DILITHIUM_ED25519_CTX_ON_STACK
476#define LC_DILITHIUM_ED25519_CTX_SIZE \
477 sizeof(struct lc_dilithium_ed25519_ctx)
486#ifndef LC_DILITHIUM_ED25519_CTX_ON_STACK
487#define LC_DILITHIUM_ED25519_CTX_ON_STACK(name) \
488 _Pragma("GCC diagnostic push") _Pragma( \
489 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
491 name##_ctx_buf, LC_DILITHIUM_ED25519_CTX_SIZE, \
492 LC_HASH_COMMON_ALIGNMENT); \
493 struct lc_dilithium_ed25519_ctx *name = \
494 (struct lc_dilithium_ed25519_ctx *)name##_ctx_buf; \
495 LC_SHAKE_256_CTX((&(name)->dilithium_ctx.dilithium_hash_ctx)); \
496 LC_SHA512_CTX((&(name)->ed25519_hash_ctx)); \
497 _Pragma("GCC diagnostic pop")
544 struct lc_rng_ctx *rng_ctx);
560 const uint8_t *m,
size_t mlen,
562 struct lc_rng_ctx *rng_ctx);
576 struct lc_rng_ctx *rng_ctx);
590 const uint8_t *m,
size_t mlen,
613#ifndef LC_DILITHIUM_INTERNAL
614#undef LC_DILITHIUM_MODE
615#undef LC_DILITHIUM_SEEDBYTES
616#undef LC_DILITHIUM_CRHBYTES
617#undef LC_DILITHIUM_TRBYTES
618#undef LC_DILITHIUM_RNDBYTES
622#undef LC_DILITHIUM_ROOT_OF_UNITY
623#undef LC_DILITHIUM_LAMBDA
626#undef LC_DILITHIUM_ETA
627#undef LC_DILITHIUM_TAU
628#undef LC_DILITHIUM_BETA
629#undef LC_DILITHIUM_GAMMA1
630#undef LC_DILITHIUM_GAMMA2
631#undef LC_DILITHIUM_OMEGA
632#undef LC_DILITHIUM_CTILDE_BYTES
633#undef LC_DILITHIUM_POLYT1_PACKEDBYTES
634#undef LC_DILITHIUM_POLYT0_PACKEDBYTES
635#undef LC_DILITHIUM_POLYVECH_PACKEDBYTES
636#undef LC_DILITHIUM_POLYZ_PACKEDBYTES
637#undef LC_DILITHIUM_POLYW1_PACKEDBYTES
638#undef LC_DILITHIUM_POLYETA_PACKEDBYTES
639#undef LC_DILITHIUM_PUBLICKEYBYTES
640#undef LC_DILITHIUM_SECRETKEYBYTES
641#undef LC_DILITHIUM_CRYPTO_BYTES
static void lc_hash_zero(struct lc_hash_ctx *hash_ctx)
Zeroize Hash context allocated with either LC_HASH_CTX_ON_STACK or lc_hmac_alloc.
struct lc_ed25519_pk pk_ed25519
int lc_dilithium_44_ed25519_sign_update(struct lc_dilithium_ed25519_ctx *ctx, const uint8_t *m, size_t mlen)
int lc_dilithium_44_verify(const struct lc_dilithium_44_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_pk *pk)
Verifies signature in one shot.
struct lc_dilithium_44_sig sig
void lc_dilithium_44_ed25519_ctx_zero_free(struct lc_dilithium_ed25519_ctx *ctx)
Zeroize and free Dilithium-ED25519 stream context.
int lc_dilithium_44_keypair_from_seed(struct lc_dilithium_44_pk *pk, struct lc_dilithium_44_sk *sk, const uint8_t *seed, size_t seedlen)
Generates Dilithium public and private key from a given seed.
int lc_dilithium_44_keypair(struct lc_dilithium_44_pk *pk, struct lc_dilithium_44_sk *sk, struct lc_rng_ctx *rng_ctx)
Generates Dilithium public and private key.
int lc_dilithium_44_ed25519_sign_final(struct lc_dilithium_44_ed25519_sig *sig, struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
int lc_dilithium_44_ctx_alloc(struct lc_dilithium_ctx **ctx)
Allocate Dilithium stream context on heap.
int lc_dilithium_44_sign_final(struct lc_dilithium_44_sig *sig, struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature.
int lc_dilithium_44_sign(struct lc_dilithium_44_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature in one shot.
static LC_PURE unsigned int lc_dilithium_44_sk_size(void)
Return the size of the Dilithium secret key.
struct lc_ed25519_sk sk_ed25519
int lc_dilithium_44_ed25519_verify_init(struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_pk *pk)
int lc_dilithium_44_ed25519_verify_update(struct lc_dilithium_ed25519_ctx *ctx, const uint8_t *m, size_t mlen)
int lc_dilithium_44_verify_update(struct lc_dilithium_ctx *ctx, const uint8_t *m, size_t mlen)
Add more data to an already initialized signature state.
static void lc_dilithium_44_ed25519_ctx_zero(struct lc_dilithium_ed25519_ctx *ctx)
Zeroize Dilithium-ED25519 context allocated with LC_DILITHIUM_ED25519_CTX_ON_STACK lc_dilithium_ed255...
int lc_dilithium_44_ed25519_sign_init(struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_sk *sk)
int lc_dilithium_44_sign_update(struct lc_dilithium_ctx *ctx, const uint8_t *m, size_t mlen)
Add more data to an already initialized signature state.
uint8_t sk[LC_DILITHIUM_SECRETKEYBYTES]
int lc_dilithium_44_ed25519_ctx_alloc(struct lc_dilithium_ed25519_ctx **ctx)
Allocate Dilithium-ED25519 stream context on heap.
struct lc_dilithium_44_pk pk
static LC_PURE unsigned int lc_dilithium_44_pk_size(void)
Return the size of the Dilithium public key.
uint8_t sig[LC_DILITHIUM_CRYPTO_BYTES]
int lc_dilithium_44_ed25519_verify(const struct lc_dilithium_44_ed25519_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_ed25519_pk *pk)
Verifies signature in one shot.
struct lc_dilithium_44_sk sk
static LC_PURE unsigned int lc_dilithium_44_sig_size(void)
Return the size of the Dilithium signature.
int lc_dilithium_44_ed25519_sign(struct lc_dilithium_44_ed25519_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature in one shot.
int lc_dilithium_44_verify_final(const struct lc_dilithium_44_sig *sig, struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_pk *pk)
Verifies signature.
int lc_dilithium_44_verify_init(struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_pk *pk)
Initializes a signature verification operation.
int lc_dilithium_44_ed25519_keypair(struct lc_dilithium_44_ed25519_pk *pk, struct lc_dilithium_44_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
Generates Dilithium public and private key.
static void lc_dilithium_44_ctx_zero(struct lc_dilithium_ctx *ctx)
Zeroize Dilithium context allocated with LC_DILITHIUM_CTX_ON_STACK lc_dilithium_ed25519_alloc.
int lc_dilithium_44_sign_init(struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_sk *sk)
Initializes a signature operation.
void lc_dilithium_44_ctx_zero_free(struct lc_dilithium_ctx *ctx)
Zeroize and free Dilithium stream context.
struct lc_ed25519_sig sig_ed25519
int lc_dilithium_44_ed25519_verify_final(const struct lc_dilithium_44_ed25519_sig *sig, struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_pk *pk)
uint8_t pk[LC_DILITHIUM_PUBLICKEYBYTES]
struct lc_hash_ctx ed25519_hash_ctx
uint8_t shake_state[LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE)]
struct lc_hash_ctx dilithium_hash_ctx
struct lc_dilithium_ctx dilithium_ctx
uint8_t sha512_state[LC_SHA512_STATE_SIZE+LC_HASH_COMMON_ALIGNMENT]
Dilithium stream context.
Dilithium stream context.