Botan 3.10.0
Crypto and TLS for C&
kyber_constants.h
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#ifndef BOTAN_KYBER_CONSTANTS_H_
13#define BOTAN_KYBER_CONSTANTS_H_
14
15#include <botan/kyber.h>
16
17namespace Botan {
18
21
22class KyberConstants final {
23 public:
24 /// base data type for most calculations
25 using T = int16_t;
26
27 /// number of coefficients in a polynomial
28 static constexpr T N = 256;
29
30 /// modulus
31 static constexpr T Q = 3329;
32
33 /// as specified in FIPS 203 (see Algorithm 10 (NTT^-1), f = 128^-1 mod Q)
34 static constexpr T F = 3303;
35
36 /// the primitive 256-th root of unity modulo Q (see FIPS 203 Section 4.3)
37 static constexpr T ROOT_OF_UNITY = 17;
38
39 /// degree of the NTT polynomials
40 static constexpr size_t NTT_Degree = 128;
41
42 public:
43 static constexpr size_t SEED_BYTES = 32;
44 static constexpr size_t PUBLIC_KEY_HASH_BYTES = 32;
45 static constexpr size_t SHARED_KEY_BYTES = 32;
46
47 /// sampling limit for SampleNTT (in bytes), see FIPS 204, Apx B
48 static constexpr uint16_t SAMPLE_NTT_POLY_FROM_XOF_BOUND = 280 * 3 /* XOF bytes per while iteration */;
49
50 public:
51 // NOLINTBEGIN(*-use-enum-class)
52
53 enum KyberEta : uint8_t { _2 = 2, _3 = 3 };
54
55 enum KyberDu : uint8_t { _10 = 10, _11 = 11 };
56
57 enum KyberDv : uint8_t { _4 = 4, _5 = 5 };
58
59 enum KyberStrength : uint16_t { _128 = 128, _192 = 192, _256 = 256 };
60
61 // NOLINTEND(*-use-enum-class)
62
63 public:
64 /* NOLINT(*-explicit-conversions) */ KyberConstants(KyberMode mode);
65
67
68 KyberConstants(const KyberConstants& other) : KyberConstants(other.m_mode) {}
69
70 KyberConstants(KyberConstants&& other) = default;
71 KyberConstants& operator=(const KyberConstants& other) = delete;
73
74 KyberMode mode() const { return m_mode; }
75
76 /// @returns one of {512, 768, 1024}
77 size_t canonical_parameter_set_identifier() const { return k() * N; }
78
79 /// \name Foundational constants
80 /// @{
81
82 uint8_t k() const { return m_k; }
83
84 KyberEta eta1() const { return m_eta1; }
85
86 constexpr KyberEta eta2() const { return KyberEta::_2; }
87
88 KyberDu d_u() const { return m_du; }
89
90 KyberDv d_v() const { return m_dv; }
91
92 KyberStrength estimated_strength() const { return m_nist_strength; }
93
94 /// @}
95
96 /// \name Sizes of encoded data structures
97 /// @{
98
99 /// byte length of an encoded polynomial vector
100 size_t polynomial_vector_bytes() const { return m_polynomial_vector_bytes; }
101
102 /// byte length of an encoded compressed polynomial vector
103 size_t polynomial_vector_compressed_bytes() const { return m_polynomial_vector_compressed_bytes; }
104
105 /// byte length of an encoded compressed polynomial
106 size_t polynomial_compressed_bytes() const { return m_polynomial_compressed_bytes; }
107
108 /// byte length of an encoded ciphertext
110
111 /// byte length of the shared key
112 constexpr size_t shared_key_bytes() const { return SHARED_KEY_BYTES; }
113
114 /// byte length of an encoded public key
116
117 /// byte length of a private key with expanded encoding as defined
118 // in FIPS 203
119 size_t expanded_private_key_bytes() const { return m_expanded_private_key_bytes; }
120
121 /// byte length of an private key encoded as the seed: d || z
122 size_t seed_private_key_bytes() const { return m_seed_private_key_bytes; }
123
124 /// @}
125
126 Kyber_Symmetric_Primitives& symmetric_primitives() const { return *m_symmetric_primitives; }
127
128 private:
129 KyberMode m_mode;
130
131 KyberStrength m_nist_strength;
132 KyberEta m_eta1;
133 KyberDu m_du;
134 KyberDv m_dv;
135 uint8_t m_k;
136
137 uint32_t m_polynomial_vector_bytes;
138 uint32_t m_polynomial_vector_compressed_bytes;
139 uint32_t m_polynomial_compressed_bytes;
140
141 uint32_t m_expanded_private_key_bytes;
142 uint32_t m_seed_private_key_bytes;
143
144 std::unique_ptr<Kyber_Symmetric_Primitives> m_symmetric_primitives;
145};
146
147} // namespace Botan
148
149#endif
size_t polynomial_vector_compressed_bytes() const
byte length of an encoded compressed polynomial vector
static constexpr T N
number of coefficients in a polynomial
constexpr KyberEta eta2() const
size_t polynomial_compressed_bytes() const
byte length of an encoded compressed polynomial
static constexpr T Q
modulus
KyberConstants & operator=(const KyberConstants &other)=delete
static constexpr size_t SEED_BYTES
static constexpr T ROOT_OF_UNITY
the primitive 256-th root of unity modulo Q (see FIPS 203 Section 4.3)
size_t public_key_bytes() const
byte length of an encoded public key
KyberConstants & operator=(KyberConstants &&other)=default
size_t seed_private_key_bytes() const
byte length of an private key encoded as the seed: d || z
static constexpr T F
as specified in FIPS 203 (see Algorithm 10 (NTT^-1), f = 128^-1 mod Q)
static constexpr uint16_t SAMPLE_NTT_POLY_FROM_XOF_BOUND
sampling limit for SampleNTT (in bytes), see FIPS 204, Apx B
KyberConstants(KyberMode mode)
size_t polynomial_vector_bytes() const
byte length of an encoded polynomial vector
constexpr size_t shared_key_bytes() const
byte length of the shared key
size_t ciphertext_bytes() const
byte length of an encoded ciphertext
static constexpr size_t SHARED_KEY_BYTES
static constexpr size_t NTT_Degree
degree of the NTT polynomials
KyberStrength estimated_strength() const
size_t expanded_private_key_bytes() const
byte length of a private key with expanded encoding as defined
KyberConstants(KyberConstants &&other)=default
KyberConstants(const KyberConstants &other)
KyberMode mode() const
KyberEta eta1() const
Kyber_Symmetric_Primitives & symmetric_primitives() const
int16_t T
base data type for most calculations
size_t canonical_parameter_set_identifier() const
static constexpr size_t PUBLIC_KEY_HASH_BYTES