Botan 3.6.1
Crypto and TLS for C&
kyber_constants.cpp
Go to the documentation of this file.
1/*
2 * Crystals Kyber Constants
3 *
4 * (C) 2021-2024 Jack Lloyd
5 * (C) 2021-2022 Manuel Glaser and Michael Boric, Rohde & Schwarz Cybersecurity
6 * (C) 2021-2022 René Meusel and Hannes Rantzsch, neXenio GmbH
7 * (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
8 *
9 * Botan is released under the Simplified BSD License (see license.txt)
10 */
11
12#include <botan/internal/kyber_constants.h>
13
14#include <botan/internal/pqcrystals_helpers.h>
15
16#if defined(BOTAN_HAS_KYBER)
17 #include <botan/internal/kyber_modern.h>
18#endif
19
20#if defined(BOTAN_HAS_KYBER_90S)
21 #include <botan/internal/kyber_90s.h>
22#endif
23
24#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
25 #include <botan/internal/kyber_round3_impl.h>
26#endif
27
28#if defined(BOTAN_HAS_ML_KEM)
29 #include <botan/internal/ml_kem_impl.h>
30#endif
31
32namespace Botan {
33
35 switch(mode.mode()) {
36 case KyberMode::Kyber512_R3:
37 case KyberMode::Kyber512_90s:
38 case KyberMode::ML_KEM_512:
39 m_nist_strength = KyberStrength::_128;
40 m_k = 2;
41 m_eta1 = KyberEta::_3;
42 m_du = KyberDu::_10;
43 m_dv = KyberDv::_4;
44 break;
45
46 case KyberMode::Kyber768_R3:
47 case KyberMode::Kyber768_90s:
48 case KyberMode::ML_KEM_768:
49 m_nist_strength = KyberStrength::_192;
50 m_k = 3;
51 m_eta1 = KyberEta::_2;
52 m_du = KyberDu::_10;
53 m_dv = KyberDv::_4;
54 break;
55
56 case KyberMode::Kyber1024_R3:
57 case KyberMode::Kyber1024_90s:
58 case KyberMode::ML_KEM_1024:
59 m_nist_strength = KyberStrength::_256;
60 m_k = 4;
61 m_eta1 = KyberEta::_2;
62 m_du = KyberDu::_11;
63 m_dv = KyberDv::_5;
64 break;
65
66 default:
67 BOTAN_ASSERT_UNREACHABLE();
68 }
69
71 if(mode.is_kyber_round3() && mode.is_90s()) {
72 m_symmetric_primitives = std::make_unique<Kyber_90s_Symmetric_Primitives>();
73 m_keypair_codec = std::make_unique<Kyber_Expanded_Keypair_Codec>();
74 }
75#endif
76
77#ifdef BOTAN_HAS_KYBER
79 m_symmetric_primitives = std::make_unique<Kyber_Modern_Symmetric_Primitives>();
80 m_keypair_codec = std::make_unique<Kyber_Expanded_Keypair_Codec>();
81 }
82#endif
83
85 if(mode.is_ml_kem()) {
86 m_symmetric_primitives = std::make_unique<ML_KEM_Symmetric_Primitives>();
87 m_keypair_codec = std::make_unique<ML_KEM_Expanding_Keypair_Codec>();
88 }
89#endif
90
91 static_assert(N % 8 == 0);
92 m_polynomial_vector_bytes = (bitlen(Q) * (N / 8)) * k();
93 m_polynomial_vector_compressed_bytes = d_u() * k() * (N / 8);
94 m_polynomial_compressed_bytes = d_v() * (N / 8);
95 m_private_key_bytes = static_cast<uint32_t>([this] {
96 if(m_mode.is_ml_kem()) {
97 // ML-KEM's private keys are simply expanded from their seeds.
98 return 2 * SEED_BYTES;
99 } else {
100 return m_polynomial_vector_bytes + public_key_bytes() + PUBLIC_KEY_HASH_BYTES + SEED_BYTES;
101 }
102 }());
103
104 if(!m_symmetric_primitives) {
105 throw Not_Implemented("requested Kyber mode is not enabled in this build");
106 }
107}
108
110
111} // namespace Botan
static constexpr T N
number of coefficients in a polynomial
static constexpr T Q
modulus
static constexpr size_t SEED_BYTES
size_t public_key_bytes() const
byte length of an encoded public key
KyberConstants(KyberMode mode)
KyberMode mode() const
static constexpr size_t PUBLIC_KEY_HASH_BYTES
bool is_kyber_round3() const
Definition kyber.cpp:136
bool is_modern() const
Definition kyber.cpp:128
bool is_90s() const
Definition kyber.cpp:124
bool is_ml_kem() const
Definition kyber.cpp:132
Mode mode() const
Definition kyber.h:62
#define BOTAN_HAS_KYBER_90S
Definition build.h:284
#define BOTAN_HAS_ML_KEM
Definition build.h:297
#define BOTAN_HAS_KYBER
Definition build.h:283
constexpr auto bitlen(size_t x)