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