12#include <botan/internal/cmce_poly.h>
14#include <botan/internal/buffer_stuffer.h>
15#include <botan/internal/concat_util.h>
16#include <botan/internal/ct_utils.h>
27 for(
auto it = m_coef.rbegin(); it != m_coef.rend(); ++it) {
37 std::vector<Classic_McEliece_GF> prod(m_t * 2 - 1, {
CmceGfElem(0), m_poly_f});
39 for(
size_t i = 0; i < m_t; ++i) {
40 for(
size_t j = 0; j < m_t; ++j) {
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);
51 prod.erase(prod.begin() + m_t, prod.end());
57 std::span<const uint8_t> bytes)
const {
59 return create_element_from_coef(
load_le<std::vector<CmceGfElem>>(bytes));
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;
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);
69 return Classic_McEliece_Polynomial(coeff_vec_gf);
79 auto polynomial = create_element_from_bytes(seed);
80 std::vector<Classic_McEliece_Polynomial> mat;
82 mat.push_back(create_element_from_coef(
concat<std::vector<CmceGfElem>>(
85 mat.push_back(polynomial);
87 for(
size_t j = 2; j <=
degree(); ++j) {
88 mat.push_back(
multiply(mat.at(j - 1), polynomial));
92 for(
size_t j = 0; j <
degree(); ++j) {
93 for(
size_t k = j + 1; k <
degree(); ++k) {
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));
101 const bool is_zero_at_diagonal = mat.at(j).coef_at(j).is_zero();
103 if(is_zero_at_diagonal) {
108 auto inv = mat.at(j).coef_at(j).inv();
110 for(
size_t c = j; c <
degree() + 1; ++c) {
111 mat.at(c).coef_at(j) *= inv;
114 for(
size_t k = 0; k <
degree(); ++k) {
116 const auto t = mat.at(j).coef_at(k);
118 for(
size_t c = j; c <
degree() + 1; ++c) {
119 mat.at(c).coef_at(k) += mat.at(c).coef_at(j) * t;
125 auto minimal_poly_coeffs = mat.at(
degree()).coef();
134 const auto& all_coeffs =
coef();
136 auto coeffs_to_store = std::span(all_coeffs).first(all_coeffs.size() - 1);
139 for(
const auto&
coef : coeffs_to_store) {
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);
155 coeff_vec_gf.emplace_back(
CmceGfElem(1), poly_f);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
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.
static size_t log_q_from_mod(CmceGfMod modulus)
Get m.
CmceGfMod modulus() const
Get the modulus f(z) of GF(q) as a GF_Mod.
Representation of a minimal polynomial in GF(q)[y].
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)
Classic_McEliece_Polynomial multiply(const Classic_McEliece_Polynomial &a, const Classic_McEliece_Polynomial &b) const
size_t degree() const
The degree of polynomials in this ring (and of F(y)).
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.
Representation of a Classic McEliece polynomial.
const std::vector< Classic_McEliece_GF > & coef() const
Get the entire coefficients vector of the polynomial.
Classic_McEliece_GF operator()(Classic_McEliece_GF a) const
Evaluate the polynomial P(x) at a given point a, i.e., compute P(a).
Classic_McEliece_GF & coef_at(size_t i)
Get the coefficient of the i-th monomial as a reference (from low to high degree).
static GF_Mask is_zero(Classic_McEliece_GF v)
constexpr void unpoison(const T *p, size_t n)
Strong< uint16_t, struct CmceGfMod_ > CmceGfMod
Represents a GF(q) modulus.
Strong< uint16_t, struct CmceGfElem_ > CmceGfElem
Represents a GF(q) element.
constexpr auto store_le(ParamTs &&... params)
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
constexpr auto concat(Rs &&... ranges)
constexpr auto load_le(ParamTs &&... params)
std::vector< T, secure_allocator< T > > secure_vector
Represents a non-zero coefficient of the modulus F(y) (which is in GF(q)[y]).
Classic_McEliece_GF coeff