Botan 3.7.1
Crypto and TLS for C&
cmce_poly.cpp
Go to the documentation of this file.
1/*
2 * Classic McEliece Polynomials
3 * Based on the public domain reference implementation by the designers
4 * (https://classic.mceliece.org/impl.html - released in Oct 2022 for NISTPQC-R4)
5 *
6 * (C) 2023 Jack Lloyd
7 * 2023,2024 Fabian Albert, Amos Treiber - Rohde & Schwarz Cybersecurity
8 *
9 * Botan is released under the Simplified BSD License (see license.txt)
10 **/
11
12#include <botan/internal/cmce_poly.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/stl_util.h>
15
16namespace Botan {
17
19 BOTAN_DEBUG_ASSERT(a.modulus() == coef_at(0).modulus());
20
22 for(auto it = m_coef.rbegin(); it != m_coef.rend(); ++it) {
23 r *= a;
24 r += *it;
25 }
26
27 return r;
28}
29
31 const Classic_McEliece_Polynomial& b) const {
32 std::vector<Classic_McEliece_GF> prod(m_t * 2 - 1, {CmceGfElem(0), m_poly_f});
33
34 for(size_t i = 0; i < m_t; ++i) {
35 for(size_t j = 0; j < m_t; ++j) {
36 prod.at(i + j) += (a.coef_at(i) * b.coef_at(j));
37 }
38 }
39
40 for(size_t i = (m_t - 1) * 2; i >= m_t; --i) {
41 for(auto& [idx, coef] : m_position_map) {
42 prod.at(i - m_t + idx) += coef * prod.at(i);
43 }
44 }
45
46 prod.erase(prod.begin() + m_t, prod.end());
47
48 return Classic_McEliece_Polynomial(std::move(prod));
49}
50
51Classic_McEliece_Polynomial Classic_McEliece_Polynomial_Ring::create_element_from_bytes(
52 std::span<const uint8_t> bytes) const {
53 BOTAN_ARG_CHECK(bytes.size() == m_t * 2, "Correct input size");
54 return create_element_from_coef(load_le<std::vector<CmceGfElem>>(bytes));
55}
56
57Classic_McEliece_Polynomial Classic_McEliece_Polynomial_Ring::create_element_from_coef(
58 const std::vector<CmceGfElem>& coeff_vec) const {
59 std::vector<Classic_McEliece_GF> coeff_vec_gf;
60 CmceGfElem coeff_mask = CmceGfElem((uint16_t(1) << Classic_McEliece_GF::log_q_from_mod(m_poly_f)) - 1);
61 std::transform(coeff_vec.begin(), coeff_vec.end(), std::back_inserter(coeff_vec_gf), [&](auto& coeff) {
62 return Classic_McEliece_GF(coeff & coeff_mask, m_poly_f);
63 });
64 return Classic_McEliece_Polynomial(coeff_vec_gf);
65}
66
71
72std::optional<Classic_McEliece_Minimal_Polynomial> Classic_McEliece_Polynomial_Ring::compute_minimal_polynomial(
74 auto polynomial = create_element_from_bytes(seed);
75 std::vector<Classic_McEliece_Polynomial> mat;
76
77 mat.push_back(create_element_from_coef(concat<std::vector<CmceGfElem>>(
78 std::vector<CmceGfElem>{CmceGfElem(1)}, std::vector<CmceGfElem>(degree() - 1, CmceGfElem(0)))));
79
80 mat.push_back(polynomial);
81
82 for(size_t j = 2; j <= degree(); ++j) {
83 mat.push_back(multiply(mat.at(j - 1), polynomial));
84 }
85
86 // Gaussian
87 for(size_t j = 0; j < degree(); ++j) {
88 for(size_t k = j + 1; k < degree(); ++k) {
89 auto cond = GF_Mask::is_zero(mat.at(j).coef_at(j));
90
91 for(size_t c = j; c < degree() + 1; ++c) {
92 mat.at(c).coef_at(j) += cond.if_set_return(mat.at(c).coef_at(k));
93 }
94 }
95
96 const bool is_zero_at_diagonal = mat.at(j).coef_at(j).is_zero();
97 CT::unpoison(is_zero_at_diagonal);
98 if(is_zero_at_diagonal) {
99 // Fail if not systematic. New rejection sampling iteration starts.
100 return std::nullopt;
101 }
102
103 auto inv = mat.at(j).coef_at(j).inv();
104
105 for(size_t c = j; c < degree() + 1; ++c) {
106 mat.at(c).coef_at(j) *= inv;
107 }
108
109 for(size_t k = 0; k < degree(); ++k) {
110 if(k != j) {
111 const auto t = mat.at(j).coef_at(k);
112
113 for(size_t c = j; c < degree() + 1; ++c) {
114 mat.at(c).coef_at(k) += mat.at(c).coef_at(j) * t;
115 }
116 }
117 }
118 }
119
120 auto minimal_poly_coeffs = mat.at(degree()).coef();
121 // Add coefficient 1 since polynomial is monic
122 minimal_poly_coeffs.emplace_back(CmceGfElem(1), poly_f());
123
124 return Classic_McEliece_Minimal_Polynomial(std::move(minimal_poly_coeffs));
125}
126
128 BOTAN_ASSERT_NOMSG(!coef().empty());
129 auto& all_coeffs = coef();
130 // Store all except coef for monomial x^t since polynomial is monic (ISO Spec Section 9.2.9)
131 auto coeffs_to_store = std::span(all_coeffs).first(all_coeffs.size() - 1);
132 secure_vector<uint8_t> bytes(sizeof(uint16_t) * coeffs_to_store.size());
133 BufferStuffer bytes_stuf(bytes);
134 for(auto& coef : coeffs_to_store) {
135 store_le(bytes_stuf.next<sizeof(CmceGfElem)>(), coef.elem().get());
136 }
137 BOTAN_ASSERT_NOMSG(bytes_stuf.full());
138 return bytes;
139}
140
142 CmceGfMod poly_f) {
143 BOTAN_ASSERT_NOMSG(bytes.size() % 2 == 0);
144 const auto coef_vec = load_le<std::vector<CmceGfElem>>(bytes);
145 std::vector<Classic_McEliece_GF> coeff_vec_gf;
146 std::transform(coef_vec.begin(), coef_vec.end(), std::back_inserter(coeff_vec_gf), [poly_f](auto& coeff) {
147 return Classic_McEliece_GF(coeff, poly_f);
148 });
149
150 coeff_vec_gf.emplace_back(CmceGfElem(1), poly_f); // x^t as polynomial is monic
151
152 return Classic_McEliece_Minimal_Polynomial(coeff_vec_gf);
153}
154
155} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
Helper class to ease in-place marshalling of concatenated fixed-length values.
Definition stl_util.h:142
constexpr std::span< uint8_t > next(size_t bytes)
Definition stl_util.h:150
constexpr bool full() const
Definition stl_util.h:187
Represents an element of the finite field GF(q) for q = 2^m.
Definition cmce_gf.h:30
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
Representation of a minimal polynomial in GF(q)[y].
Definition cmce_poly.h:81
secure_vector< uint8_t > serialize() const
Serialize the polynomial to bytes according to ISO Section 9.2.9.
static Classic_McEliece_Minimal_Polynomial from_bytes(std::span< const uint8_t > bytes, CmceGfMod poly_f)
Create a polynomial from bytes according to ISO Section 9.2.9.
Classic_McEliece_Minimal_Polynomial(std::vector< Classic_McEliece_GF > coef)
Definition cmce_poly.h:83
Classic_McEliece_Polynomial multiply(const Classic_McEliece_Polynomial &a, const Classic_McEliece_Polynomial &b) const
Definition cmce_poly.cpp:30
size_t degree() const
The degree of polynomials in this ring (and of F(y)).
Definition cmce_poly.h:135
std::optional< Classic_McEliece_Minimal_Polynomial > compute_minimal_polynomial(StrongSpan< const CmceIrreducibleBits > seed) const
Compute the minimal polynomial g of polynomial created from a seed.
Definition cmce_poly.cpp:72
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
Classic_McEliece_GF operator()(Classic_McEliece_GF a) const
Evaluate the polynomial P(x) at a given point a, i.e., compute P(a).
Definition cmce_poly.cpp:18
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
static GF_Mask is_zero(Classic_McEliece_GF v)
Definition cmce_gf.h:162
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64
Strong< uint16_t, struct CmceGfElem_ > CmceGfElem
Represents a GF(q) element.
Definition cmce_types.h:19
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:764
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:263
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:521
const SIMD_8x32 & b
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