12#include <botan/internal/mce_internal.h>
13#include <botan/internal/code_based_util.h>
19void matrix_arr_mul(std::vector<uint32_t> matrix,
22 const uint8_t input_vec[],
23 uint32_t output_vec[],
24 size_t output_vec_len)
26 for(
size_t j = 0; j < numo_rows; j++)
28 if((input_vec[j / 8] >> (j % 8)) & 1)
30 for(
size_t i = 0; i < output_vec_len; i++)
32 output_vec[i] ^= matrix[j * (words_per_row) + i];
41secure_vector<gf2m> goppa_decode(
const polyn_gf2m & syndrom_polyn,
43 const std::vector<polyn_gf2m> & sqrtmod,
44 const std::vector<gf2m> & Linv)
46 const size_t code_length = Linv.size();
48 uint32_t t = g.get_degree();
50 std::shared_ptr<GF2m_Field> sp_field = g.get_sp_field();
53 polyn_gf2m & h = h_aux.first;
54 polyn_gf2m & aux = h_aux.second;
55 a = sp_field->gf_inv(aux.get_coef(0));
56 gf2m log_a = sp_field->gf_log(a);
57 for(
int i = 0; i <= h.get_degree(); ++i)
59 h.set_coef(i,sp_field->gf_mul_zrz(log_a,h.get_coef(i)));
65 polyn_gf2m S(t - 1, g.get_sp_field());
67 for(uint32_t i=0;i<t;i++)
69 a = sp_field->gf_sqrt(h.get_coef(i));
73 for(uint32_t j=0;j<t;j++)
75 S.add_to_coef( j, sp_field->gf_mul(a, sqrtmod[i/2].get_coef(j)));
80 S.add_to_coef( i/2, a);
88 polyn_gf2m & u = v_u.second;
89 polyn_gf2m & v = v_u.first;
92 polyn_gf2m
sigma ( t , g.get_sp_field());
94 const int u_deg = u.get_degree();
96 for(
int i = 0; i <= u_deg; ++i)
98 sigma.set_coef(2*i, sp_field->gf_square(u.get_coef(i)));
101 const int v_deg = v.get_degree();
103 for(
int i = 0; i <= v_deg; ++i)
105 sigma.set_coef(2*i+1, sp_field->gf_square(v.get_coef(i)));
109 size_t d = res.size();
111 secure_vector<gf2m> result(d);
112 for(uint32_t i = 0; i < d; ++i)
114 gf2m current = res[i];
119 if(tmp >= code_length)
121 result[i] =
static_cast<gf2m>(i);
123 result[i] = Linv[tmp];
135 mceliece_decrypt(plaintext_out, error_mask_out, ciphertext.data(), ciphertext.size(), key);
141 const uint8_t ciphertext[],
142 size_t ciphertext_len,
150 for(
auto&& pos : error_pos)
152 if(pos > code_length)
156 result[pos / 8] |= (1 << (pos % 8));
168 const uint8_t *ciphertext,
size_t ciphertext_len,
176 const unsigned unused_pt_bits = dimension % 8;
177 const uint8_t unused_pt_bits_mask = (1 << unused_pt_bits) - 1;
195 syndrome_vec.data(), syndrome_vec.size());
198 const size_t syndrome_byte_vec_size = syndrome_byte_vec.size();
199 for(
size_t i = 0; i < syndrome_byte_vec_size; i++)
201 syndrome_byte_vec[i] =
static_cast<uint8_t
>(syndrome_vec[i/4] >> (8 * (i % 4)));
209 const size_t nb_err = error_pos.size();
212 copy_mem(cleartext.data(), ciphertext, cleartext_len);
214 for(
size_t i = 0; i < nb_err; i++)
216 gf2m current = error_pos[i];
218 if(current >= cleartext_len * 8)
223 cleartext[current / 8] ^= (1 << (current % 8));
228 cleartext[cleartext_len - 1] &= unused_pt_bits_mask;
#define BOTAN_ASSERT(expr, assertion_made)
std::vector< gf2m > const & get_Linv() const
size_t get_codimension() const
size_t get_dimension() const
std::vector< polyn_gf2m > const & get_sqrtmod() const
polyn_gf2m const & get_goppa_polyn() const
std::vector< uint32_t > const & get_H_coeffs() const
size_t get_message_word_bit_length() const
size_t get_code_length() const
std::shared_ptr< GF2m_Field > get_sp_field() const
static std::pair< polyn_gf2m, polyn_gf2m > eea_with_coefficients(const polyn_gf2m &p, const polyn_gf2m &g, int break_deg)
void mceliece_decrypt(secure_vector< uint8_t > &plaintext_out, secure_vector< uint8_t > &error_mask_out, const secure_vector< uint8_t > &ciphertext, const McEliece_PrivateKey &key)
constexpr void copy_mem(T *out, const T *in, size_t n)
gf2m gray_to_lex(gf2m gray)
size_t bit_size_to_32bit_size(size_t bit_size)
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)
size_t bit_size_to_byte_size(size_t bit_size)
std::vector< T, secure_allocator< T > > secure_vector