Botan 3.7.1
Crypto and TLS for C&
cmce_poly.h
Go to the documentation of this file.
1/*
2 * Classic McEliece Polynomials
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_POLY_H_
10#define BOTAN_CMCE_POLY_H_
11
12#include <botan/secmem.h>
13#include <botan/internal/cmce_gf.h>
14#include <botan/internal/loadstor.h>
15
16#include <optional>
17
18namespace Botan {
19
20/**
21 * @brief Representation of a Classic McEliece polynomial.
22 *
23 * This class represents a polynomial in the ring GF(q)[y]. E.g an example element of degree 2 could be:
24 * a = (z^3+1)y^2 + (z)y + (z^4+z^3)
25 * The degree of the polynomial is given by the size of the coefficient vector given to
26 * the constructor, even if the leading coefficient is zero. Coefficients are stored from
27 * lowest to highest monomial degree (coef_at(0) = (z^4+z^3) in the example above).
28 *
29 * This class is merely a container. The modulus and the operations with Polynomials (e.g. multiplication)
30 * is handled by the Classic_McEliece_Polynomial_Ring class.
31 */
33 public:
34 /**
35 * @brief Construct a polynomial given its coefficients.
36 *
37 * @param coef The coefficients of the polynomial. The first element is the coefficient of the lowest monomial.
38 */
39 Classic_McEliece_Polynomial(std::vector<Classic_McEliece_GF> coef) : m_coef(std::move(coef)) {}
40
41 /**
42 * @brief Evaluate the polynomial P(x) at a given point a, i.e., compute P(a).
43 */
44 Classic_McEliece_GF operator()(Classic_McEliece_GF a) const;
45
46 /**
47 * @brief Get the coefficient of the i-th monomial as a reference (from low to high degree).
48 */
49 Classic_McEliece_GF& coef_at(size_t i) { return m_coef.at(i); }
50
51 /**
52 * @brief Get the coefficient of the i-th monomial (from low to high degree).
53 */
54 const Classic_McEliece_GF& coef_at(size_t i) const { return m_coef.at(i); }
55
56 /**
57 * @brief Get the entire coefficients vector of the polynomial.
58 */
59 const std::vector<Classic_McEliece_GF>& coef() const { return m_coef; }
60
61 /**
62 * @brief Get the degree of the polynomial.
63 *
64 * Note that the degree is given by the size of the coefficient vector, even if the leading coefficient is zero.
65 */
66 size_t degree() const { return m_coef.size() + 1; }
67
68 void _const_time_poison() const { CT::poison(m_coef); }
69
70 void _const_time_unpoison() const { CT::unpoison(m_coef); }
71
72 private:
73 std::vector<Classic_McEliece_GF> m_coef;
74};
75
76/**
77 * @brief Representation of a minimal polynomial in GF(q)[y].
78 *
79 * It represents the monic irreducible degree-t polynomial of the goppa code.
80 */
82 public:
83 Classic_McEliece_Minimal_Polynomial(std::vector<Classic_McEliece_GF> coef) :
84 Classic_McEliece_Polynomial(std::move(coef)) {}
85
86 /**
87 * @brief Serialize the polynomial to bytes according to ISO Section 9.2.9.
88 */
89 secure_vector<uint8_t> serialize() const;
90
91 /**
92 * @brief Create a polynomial from bytes according to ISO Section 9.2.9.
93 */
94 static Classic_McEliece_Minimal_Polynomial from_bytes(std::span<const uint8_t> bytes, CmceGfMod poly_f);
95};
96
97/**
98 * @brief Represents the polynomial ring GF(q)[y]/F(y) where F(y) is the modulus polynomial in
99 * GF(q)[y] of degree t.
100 *
101 * This class contains a modulus polynomial F(y) and the GF(q) modulus f(z). It is used
102 * to create and operate with Classic_McEliece_Polynomials.
103 */
105 public:
106 /**
107 * @brief Represents a non-zero coefficient of the modulus F(y) (which is in GF(q)[y]).
108 *
109 * E.g. {.idx = 4, .coeff = (z+1)} represents the monomial (z+1)y^4.
110 */
115
116 /**
117 * @brief Construct a polynomial ring GF(q)[y]/F(y) by defining the polynomial modulus F(y),
118 * the GF(q) modulus f(z) and the degree of F(y).
119 *
120 * F(y) is given by a vector of Big_F_Coefficients, where each one represents a monomial of F(y).
121 * However, the highest monomial must not be specified, since it is always 1.
122 *
123 * @param poly_big_f_coef The non-zero coefficients of F(y) in GF(q)[y] WITHOUT the highest monomial.
124 * @param poly_f The modulus f(z) of GF(q).
125 * @param t The polynomial degree of the ring (and of F(y)).
126 */
127 Classic_McEliece_Polynomial_Ring(std::vector<Big_F_Coefficient> poly_big_f_coef, CmceGfMod poly_f, size_t t) :
128 m_position_map(std::move(poly_big_f_coef)), m_t(t), m_poly_f(poly_f) {}
129
130 CmceGfMod poly_f() const { return m_poly_f; }
131
132 /**
133 * @brief The degree of polynomials in this ring (and of F(y)).
134 */
135 size_t degree() const { return m_t; }
136
137 /**
138 * @returns a*b over GF(q)[y]/F(y).
139 */
141 const Classic_McEliece_Polynomial& b) const;
142
143 /**
144 * @brief Compute the minimal polynomial g of polynomial created from a @p seed.
145 *
146 * @param seed over the ring GF(q)[y] according to ISO Section 8.1 Step 3.
147 *
148 * @return g or std::nullopt if g has not full degree.
149 */
150 std::optional<Classic_McEliece_Minimal_Polynomial> compute_minimal_polynomial(
152
153 private:
154 // Creates a ring element from a coefficient vector
155 Classic_McEliece_Polynomial create_element_from_coef(const std::vector<CmceGfElem>& coeff_vec) const;
156
157 /**
158 * @brief Create a polynomial from bytes according to ISO Section 8.1 step 1 and 2.
159 */
160 Classic_McEliece_Polynomial create_element_from_bytes(std::span<const uint8_t> bytes) const;
161
162 /// Represents F(y) by storing the non-zero terms
163 std::vector<Big_F_Coefficient> m_position_map;
164
165 /// t in spec, i.e., degree of F(y)
166 size_t m_t;
167
168 // f(z) in spec
169 CmceGfMod m_poly_f;
170};
171
172bool operator==(const Classic_McEliece_Polynomial_Ring::Big_F_Coefficient& lhs,
173 const Classic_McEliece_Polynomial_Ring::Big_F_Coefficient& rhs);
174
175} // namespace Botan
176#endif
#define BOTAN_TEST_API
Definition api.h:39
Represents an element of the finite field GF(q) for q = 2^m.
Definition cmce_gf.h:30
Representation of a minimal polynomial in GF(q)[y].
Definition cmce_poly.h:81
Classic_McEliece_Minimal_Polynomial(std::vector< Classic_McEliece_GF > coef)
Definition cmce_poly.h:83
Represents the polynomial ring GF(q)[y]/F(y) where F(y) is the modulus polynomial in GF(q)[y] of degr...
Definition cmce_poly.h:104
size_t degree() const
The degree of polynomials in this ring (and of F(y)).
Definition cmce_poly.h:135
Classic_McEliece_Polynomial_Ring(std::vector< Big_F_Coefficient > poly_big_f_coef, CmceGfMod poly_f, size_t t)
Construct a polynomial ring GF(q)[y]/F(y) by defining the polynomial modulus F(y),...
Definition cmce_poly.h:127
Representation of a Classic McEliece polynomial.
Definition cmce_poly.h:32
const std::vector< Classic_McEliece_GF > & coef() const
Get the entire coefficients vector of the polynomial.
Definition cmce_poly.h:59
size_t degree() const
Get the degree of the polynomial.
Definition cmce_poly.h:66
const Classic_McEliece_GF & coef_at(size_t i) const
Get the coefficient of the i-th monomial (from low to high degree).
Definition cmce_poly.h:54
Classic_McEliece_Polynomial(std::vector< Classic_McEliece_GF > coef)
Construct a polynomial given its coefficients.
Definition cmce_poly.h:39
Classic_McEliece_GF & coef_at(size_t i)
Get the coefficient of the i-th monomial as a reference (from low to high degree).
Definition cmce_poly.h:49
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
Represents a non-zero coefficient of the modulus F(y) (which is in GF(q)[y]).
Definition cmce_poly.h:111