Botan 3.11.0
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
14#include <botan/internal/buffer_stuffer.h>
15#include <botan/internal/concat_util.h>
16#include <botan/internal/ct_utils.h>
17#include <algorithm>
18
19namespace Botan {
20
22 BOTAN_DEBUG_ASSERT(a.modulus() == coef_at(0).modulus());
23
25 // TODO(Botan4) use std::ranges::reverse_view here once available (need newer Clang)
26 // NOLINTNEXTLINE(modernize-loop-convert)
27 for(auto it = m_coef.rbegin(); it != m_coef.rend(); ++it) {
28 r *= a;
29 r += *it;
30 }
31
32 return r;
33}
34
36 const Classic_McEliece_Polynomial& b) const {
37 std::vector<Classic_McEliece_GF> prod(m_t * 2 - 1, {CmceGfElem(0), m_poly_f});
38
39 for(size_t i = 0; i < m_t; ++i) {
40 for(size_t j = 0; j < m_t; ++j) {
41 prod.at(i + j) += (a.coef_at(i) * b.coef_at(j));
42 }
43 }
44
45 for(size_t i = (m_t - 1) * 2; i >= m_t; --i) {
46 for(const auto& [idx, coef] : m_position_map) {
47 prod.at(i - m_t + idx) += coef * prod.at(i);
48 }
49 }
50
51 prod.erase(prod.begin() + m_t, prod.end());
52
53 return Classic_McEliece_Polynomial(std::move(prod));
54}
55
56Classic_McEliece_Polynomial Classic_McEliece_Polynomial_Ring::create_element_from_bytes(
57 std::span<const uint8_t> bytes) const {
58 BOTAN_ARG_CHECK(bytes.size() == m_t * 2, "Correct input size");
59 return create_element_from_coef(load_le<std::vector<CmceGfElem>>(bytes));
60}
61
62Classic_McEliece_Polynomial Classic_McEliece_Polynomial_Ring::create_element_from_coef(
63 const std::vector<CmceGfElem>& coeff_vec) const {
64 std::vector<Classic_McEliece_GF> coeff_vec_gf;
65 CmceGfElem coeff_mask = CmceGfElem((uint16_t(1) << Classic_McEliece_GF::log_q_from_mod(m_poly_f)) - 1);
66 std::transform(coeff_vec.begin(), coeff_vec.end(), std::back_inserter(coeff_vec_gf), [&](auto& coeff) {
67 return Classic_McEliece_GF(coeff & coeff_mask, m_poly_f);
68 });
69 return Classic_McEliece_Polynomial(coeff_vec_gf);
70}
71
76
77std::optional<Classic_McEliece_Minimal_Polynomial> Classic_McEliece_Polynomial_Ring::compute_minimal_polynomial(
79 auto polynomial = create_element_from_bytes(seed);
80 std::vector<Classic_McEliece_Polynomial> mat;
81
82 mat.push_back(create_element_from_coef(concat<std::vector<CmceGfElem>>(
83 std::vector<CmceGfElem>{CmceGfElem(1)}, std::vector<CmceGfElem>(degree() - 1, CmceGfElem(0)))));
84
85 mat.push_back(polynomial);
86
87 for(size_t j = 2; j <= degree(); ++j) {
88 mat.push_back(multiply(mat.at(j - 1), polynomial));
89 }
90
91 // Gaussian
92 for(size_t j = 0; j < degree(); ++j) {
93 for(size_t k = j + 1; k < degree(); ++k) {
94 auto cond = GF_Mask::is_zero(mat.at(j).coef_at(j));
95
96 for(size_t c = j; c < degree() + 1; ++c) {
97 mat.at(c).coef_at(j) += cond.if_set_return(mat.at(c).coef_at(k));
98 }
99 }
100
101 const bool is_zero_at_diagonal = mat.at(j).coef_at(j).is_zero();
102 CT::unpoison(is_zero_at_diagonal);
103 if(is_zero_at_diagonal) {
104 // Fail if not systematic. New rejection sampling iteration starts.
105 return std::nullopt;
106 }
107
108 auto inv = mat.at(j).coef_at(j).inv();
109
110 for(size_t c = j; c < degree() + 1; ++c) {
111 mat.at(c).coef_at(j) *= inv;
112 }
113
114 for(size_t k = 0; k < degree(); ++k) {
115 if(k != j) {
116 const auto t = mat.at(j).coef_at(k);
117
118 for(size_t c = j; c < degree() + 1; ++c) {
119 mat.at(c).coef_at(k) += mat.at(c).coef_at(j) * t;
120 }
121 }
122 }
123 }
124
125 auto minimal_poly_coeffs = mat.at(degree()).coef();
126 // Add coefficient 1 since polynomial is monic
127 minimal_poly_coeffs.emplace_back(CmceGfElem(1), poly_f());
128
129 return Classic_McEliece_Minimal_Polynomial(std::move(minimal_poly_coeffs));
130}
131
133 BOTAN_ASSERT_NOMSG(!coef().empty());
134 const auto& all_coeffs = coef();
135 // Store all except coef for monomial x^t since polynomial is monic (ISO Spec Section 9.2.9)
136 auto coeffs_to_store = std::span(all_coeffs).first(all_coeffs.size() - 1);
137 secure_vector<uint8_t> bytes(sizeof(uint16_t) * coeffs_to_store.size());
138 BufferStuffer bytes_stuf(bytes);
139 for(const auto& coef : coeffs_to_store) {
140 store_le(bytes_stuf.next<sizeof(CmceGfElem)>(), coef.elem().get());
141 }
142 BOTAN_ASSERT_NOMSG(bytes_stuf.full());
143 return bytes;
144}
145
147 CmceGfMod poly_f) {
148 BOTAN_ASSERT_NOMSG(bytes.size() % 2 == 0);
149 const auto coef_vec = load_le<std::vector<CmceGfElem>>(bytes);
150 std::vector<Classic_McEliece_GF> coeff_vec_gf;
151 std::transform(coef_vec.begin(), coef_vec.end(), std::back_inserter(coeff_vec_gf), [poly_f](auto& coeff) {
152 return Classic_McEliece_GF(coeff, poly_f);
153 });
154
155 coeff_vec_gf.emplace_back(CmceGfElem(1), poly_f); // x^t as polynomial is monic
156
157 return Classic_McEliece_Minimal_Polynomial(coeff_vec_gf);
158}
159
160} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:129
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr std::span< uint8_t > next(size_t bytes)
constexpr bool full() const
Represents an element of the finite field GF(q) for q = 2^m.
Definition cmce_gf.h:29
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
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:35
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:77
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:21
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:161
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
Strong< uint16_t, struct CmceGfMod_ > CmceGfMod
Represents a GF(q) modulus.
Definition cmce_types.h:22
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:736
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:53
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:90
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:495
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
Represents a non-zero coefficient of the modulus F(y) (which is in GF(q)[y]).
Definition cmce_poly.h:111