Botan 3.11.0
Crypto and TLS for C&
cmce_gf.h
Go to the documentation of this file.
1/*
2 * Classic McEliece GF arithmetic
3 * (C) 2023 Jack Lloyd
4 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 **/
8
9#ifndef BOTAN_CMCE_GF_H_
10#define BOTAN_CMCE_GF_H_
11
12#include <botan/assert.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/cmce_types.h>
15#include <botan/internal/ct_utils.h>
16
17namespace Botan {
18
19/**
20 * @brief Represents an element of the finite field GF(q) for q = 2^m.
21 *
22 * This class implements the finite field GF(q) for q = 2^m via the irreducible
23 * polynomial f(z) of degree m. The elements of GF(q) are represented as polynomials
24 * of degree m-1 with coefficients in GF(2). Each element and the modulus is
25 * represented by a uint16_t, where the i-th least significant bit corresponds to
26 * the coefficient of z^i. For example, the element (z^3 + z^2 + 1) is represented
27 * by the uint16_t 0b1101.
28 */
30 public:
31 /**
32 * @brief Creates an element of GF(q) from a uint16_t.
33 *
34 * Each element and the modulus is represented by a uint16_t, where the i-th least significant bit
35 * corresponds to the coefficient of z^i.
36 *
37 * @param elem The element as a uint16_t. Must be less than 2^m.
38 * @param modulus The modulus of GF(q).
39 */
41 BOTAN_DEBUG_ASSERT(elem <= (size_t(1) << log_q()) - 1);
42 }
43
44 /**
45 * @brief Get m.
46 *
47 * For a given irreducible polynomial @p modulus f(z) representing the modulus of a finite field GF(q) = GF(2^m),
48 * get the degree log_q of f(z) which corresponds to m.
49 *
50 * @param modulus The modulus of GF(q).
51 * @return size_t The degree log_q of the modulus (m for GF(2^m)).
52 */
53 static size_t log_q_from_mod(CmceGfMod modulus) { return floor_log2(modulus.get()); }
54
55 /**
56 * @brief Get m, the degree of the element's modulus.
57 *
58 * @return size_t The degree log_q of the modulus (m for GF(2^m)).
59 */
60 size_t log_q() const { return log_q_from_mod(m_modulus); }
61
62 /**
63 * @brief Get the GF(q) element as a GF_Elem.
64 *
65 * @return the element as a GF_Elem.
66 */
67 CmceGfElem elem() const { return m_elem; }
68
69 /**
70 * @brief Get the modulus f(z) of GF(q) as a GF_Mod.
71 *
72 * @return the modulus as a GF_Mod.
73 */
74 CmceGfMod modulus() const { return m_modulus; }
75
76 /**
77 * @brief Change the element to @p elem.
78 */
80 m_elem = elem & CmceGfElem((size_t(1) << log_q()) - 1);
81 return *this;
82 }
83
84 /**
85 * @brief Divide the element by @p other in GF(q). Constant time.
86 */
88 BOTAN_DEBUG_ASSERT(m_modulus == other.m_modulus);
89 return *this * other.inv();
90 }
91
92 /**
93 * @brief Add @p other to the element. Constant time.
94 */
96 BOTAN_DEBUG_ASSERT(m_modulus == other.m_modulus);
97 return Classic_McEliece_GF(m_elem ^ other.m_elem, m_modulus);
98 }
99
100 /**
101 * @brief Add @p other to the element. Constant time.
102 */
104 BOTAN_DEBUG_ASSERT(m_modulus == other.m_modulus);
105 m_elem ^= other.m_elem;
106 return *this;
107 }
108
109 /**
110 * @brief Multiply the element by @p other in GF(q). Constant time.
111 */
113 BOTAN_DEBUG_ASSERT(m_modulus == other.m_modulus);
114 *this = *this * other;
115 return *this;
116 }
117
118 /**
119 * @brief Multiply the element by @p other in GF(q). Constant time.
120 */
122
123 /**
124 * @brief Check if the element is equal to @p other. Modulus is ignored.
125 */
126 bool operator==(Classic_McEliece_GF other) const { return elem() == other.elem(); }
127
128 /**
129 * @brief Square the element. Constant time.
130 */
131 Classic_McEliece_GF square() const { return (*this) * (*this); }
132
133 /**
134 * @brief Invert the element. Constant time.
135 */
136 Classic_McEliece_GF inv() const;
137
138 /**
139 * @brief Check if the element is zero.
140 */
141 bool is_zero() const { return elem() == 0; }
142
143 private:
144 CmceGfElem m_elem;
145
146 CmceGfMod m_modulus;
147};
148
149/**
150 * @brief Constant time mask wrapper for GF(q) elements.
151 */
153 public:
154 template <std::unsigned_integral T>
155 static GF_Mask expand(T v) {
157 }
158
159 static GF_Mask expand(Classic_McEliece_GF v) { return expand(v.elem().get()); }
160
162
166
170
172
173 explicit GF_Mask(CT::Mask<uint16_t> underlying_mask) : m_mask(underlying_mask) {}
174
176 return Classic_McEliece_GF(CmceGfElem(m_mask.if_set_return(x.elem().get())), x.modulus());
177 }
178
180 return Classic_McEliece_GF(CmceGfElem(m_mask.select(x.elem().get(), y.elem().get())), x.modulus());
181 }
182
184 return Classic_McEliece_GF(CmceGfElem(m_mask.select(x.elem().get(), y.get())), x.modulus());
185 }
186
187 uint16_t select(uint16_t x, uint16_t y) const { return m_mask.select(x, y); }
188
190 m_mask &= o.m_mask;
191 return (*this);
192 }
193
194 bool as_bool() const { return m_mask.as_bool(); }
195
196 CT::Mask<uint16_t>& elem_mask() { return m_mask; }
197
198 private:
199 CT::Mask<uint16_t> m_mask;
200};
201
202} // namespace Botan
203#endif
#define BOTAN_TEST_API
Definition api.h:41
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:129
static constexpr Mask< T > is_lte(T x, T y)
Definition ct_utils.h:463
static constexpr Mask< T > set()
Definition ct_utils.h:382
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:392
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:442
static constexpr Mask< T > is_zero(T x)
Definition ct_utils.h:437
Represents an element of the finite field GF(q) for q = 2^m.
Definition cmce_gf.h:29
Classic_McEliece_GF inv() const
Invert the element. Constant time.
Definition cmce_gf.cpp:62
Classic_McEliece_GF(CmceGfElem elem, CmceGfMod modulus)
Creates an element of GF(q) from a uint16_t.
Definition cmce_gf.h:40
CmceGfElem elem() const
Get the GF(q) element as a GF_Elem.
Definition cmce_gf.h:67
Classic_McEliece_GF & operator=(const CmceGfElem elem)
Change the element to elem.
Definition cmce_gf.h:79
static size_t log_q_from_mod(CmceGfMod modulus)
Get m.
Definition cmce_gf.h:53
CmceGfMod modulus() const
Get the modulus f(z) of GF(q) as a GF_Mod.
Definition cmce_gf.h:74
bool is_zero() const
Check if the element is zero.
Definition cmce_gf.h:141
size_t log_q() const
Get m, the degree of the element's modulus.
Definition cmce_gf.h:60
Classic_McEliece_GF operator/(Classic_McEliece_GF other) const
Divide the element by other in GF(q). Constant time.
Definition cmce_gf.h:87
Classic_McEliece_GF operator+(Classic_McEliece_GF other) const
Add other to the element. Constant time.
Definition cmce_gf.h:95
bool operator==(Classic_McEliece_GF other) const
Check if the element is equal to other. Modulus is ignored.
Definition cmce_gf.h:126
Classic_McEliece_GF & operator+=(Classic_McEliece_GF other)
Add other to the element. Constant time.
Definition cmce_gf.h:103
Classic_McEliece_GF square() const
Square the element. Constant time.
Definition cmce_gf.h:131
Classic_McEliece_GF & operator*=(Classic_McEliece_GF other)
Multiply the element by other in GF(q). Constant time.
Definition cmce_gf.h:112
static GF_Mask expand(T v)
Definition cmce_gf.h:155
bool as_bool() const
Definition cmce_gf.h:194
uint16_t select(uint16_t x, uint16_t y) const
Definition cmce_gf.h:187
static GF_Mask is_lte(Classic_McEliece_GF a, Classic_McEliece_GF b)
Definition cmce_gf.h:163
static GF_Mask is_equal(Classic_McEliece_GF a, Classic_McEliece_GF b)
Definition cmce_gf.h:167
static GF_Mask set()
Definition cmce_gf.h:171
static GF_Mask is_zero(Classic_McEliece_GF v)
Definition cmce_gf.h:161
CT::Mask< uint16_t > & elem_mask()
Definition cmce_gf.h:196
Classic_McEliece_GF select(const Classic_McEliece_GF x, const Classic_McEliece_GF y) const
Definition cmce_gf.h:179
static GF_Mask expand(Classic_McEliece_GF v)
Definition cmce_gf.h:159
GF_Mask(CT::Mask< uint16_t > underlying_mask)
Definition cmce_gf.h:173
GF_Mask & operator&=(const GF_Mask &o)
Definition cmce_gf.h:189
Classic_McEliece_GF if_set_return(const Classic_McEliece_GF x) const
Definition cmce_gf.h:175
Classic_McEliece_GF select(const Classic_McEliece_GF x, CmceGfElem y) const
Definition cmce_gf.h:183
constexpr T & get() &
Definition strong_type.h:85
BOTAN_FORCE_INLINE constexpr T floor_log2(T n)
Definition bit_ops.h:134
Strong< uint16_t, struct CmceGfMod_ > CmceGfMod
Represents a GF(q) modulus.
Definition cmce_types.h:22
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:57
Strong< uint16_t, struct CmceGfElem_ > CmceGfElem
Represents a GF(q) element.
Definition cmce_types.h:19