13#include <botan/internal/polyn_gf2m.h>
15#include <botan/exceptn.h>
17#include <botan/internal/code_based_util.h>
18#include <botan/internal/loadstor.h>
27 gf2m result = (a != 0);
34unsigned nlz_16bit(uint16_t x) {
60 int i =
static_cast<int>(this->m_coeff.size()) - 1;
62 uint32_t found_mask = 0;
63 uint32_t tracker_mask = 0xffff;
66 result |= i & found_mask & tracker_mask;
69 tracker_mask = tracker_mask & ~found_mask;
82 if(code_length == 0) {
83 throw Invalid_Argument(
"random_code_element() was supplied a code length of zero");
85 const unsigned nlz = nlz_16bit(code_length - 1);
86 const gf2m mask = (1 << (16 - nlz)) - 1;
90 while(result >= code_length) {
101 m_deg(-1), m_coeff(d + 1), m_sp_field(sp_field) {}
106void polyn_gf2m::realloc(uint32_t new_size) {
111 m_deg(-1), m_sp_field(sp_field) {
112 if(mem_len %
sizeof(
gf2m)) {
116 uint32_t size = (mem_len /
sizeof(this->m_coeff[0]));
119 for(uint32_t i = 0; i < size; i++) {
121 mem +=
sizeof(this->m_coeff[0]);
123 for(uint32_t i = 0; i < size; i++) {
124 if(this->m_coeff[i] >= (1 << sp_field->get_extension_degree())) {
136 const std::shared_ptr<GF2m_Field>& sp_field) :
137 m_sp_field(sp_field) {
138 const uint32_t polyn_size = degree + 1;
139 if(polyn_size * sp_field->get_extension_degree() > 8 * mem_byte_len) {
140 throw Decoding_Error(
"memory vector for polynomial has wrong size");
143 const gf2m ext_deg =
static_cast<gf2m>(this->m_sp_field->get_extension_degree());
144 for(uint32_t l = 0; l < polyn_size; l++) {
145 uint32_t k = (l * ext_deg) / 8;
147 uint32_t j = (l * ext_deg) % 8;
148 gf2m a = mem[k] >> j;
149 if(j + ext_deg > 8) {
150 a ^= mem[k + 1] << (8 - j);
152 if(j + ext_deg > 16) {
153 a ^= mem[k + 2] << (16 - j);
155 a &= ((1 << ext_deg) - 1);
156 (*this).set_coef(l, a);
163 clear_mem(this->m_coeff.data(), this->m_coeff.size());
168 int d =
static_cast<int>(this->m_coeff.size()) - 1;
169 while((d >= 0) && (this->m_coeff[d] == 0)) {
178gf2m eval_aux(
const gf2m* coeff,
gf2m a,
int d,
const std::shared_ptr<GF2m_Field>& sp_field) {
182 b = sp_field->gf_mul(b, a) ^ coeff[d];
193 return eval_aux(this->m_coeff.data(), a, this->m_deg, this->m_sp_field);
200 std::shared_ptr<GF2m_Field> m_sp_field = g.m_sp_field;
209 for(i = p_degree; d >= 0; --i, --d) {
211 gf2m lb = m_sp_field->gf_mul_rrn(la, p[i]);
213 p[j + d] ^= m_sp_field->gf_mul_zrz(lb, g[j]);
215 (*&p).set_coef(i, 0);
226 std::vector<polyn_gf2m> sq;
228 if(signed_deg <= 0) {
232 const uint32_t d =
static_cast<uint32_t
>(signed_deg);
233 uint32_t t = g.m_deg;
236 for(i = 0; i < t; ++i) {
239 for(i = 0; i < d / 2; ++i) {
240 sq[i].set_degree(2 * i);
246 copy_mem(sq[i].m_coeff.data() + 2, sq[i - 1].m_coeff.data(), d);
248 polyn_gf2m::remainder(sq[i], g);
258 std::shared_ptr<GF2m_Field> sp_field = this->m_sp_field;
262 for(i = 0; i < d / 2; ++i) {
263 (*&result).
set_coef(i * 2, sp_field->gf_square((*
this)[i]));
268 gf2m lpi = (*this)[i];
270 lpi = sp_field->gf_log(lpi);
271 gf2m la = sp_field->gf_mul_rrr(lpi, lpi);
272 for(
int j = 0; j < d; ++j) {
273 result[j] ^= sp_field->gf_mul_zrz(la, sq[i][j]);
279 result.set_degree(d - 1);
291 polyn_gf2m::remainder(p1, p2);
292 return polyn_gf2m::gcd_aux(p2, p1);
296polyn_gf2m polyn_gf2m::gcd(
const polyn_gf2m& p1,
const polyn_gf2m& p2) {
299 if(a.get_degree() < b.get_degree()) {
300 return polyn_gf2m(polyn_gf2m::gcd_aux(b, a));
302 return polyn_gf2m(polyn_gf2m::gcd_aux(a, b));
310 const size_t ext_deg = g.m_sp_field->get_extension_degree();
318 size_t result =
static_cast<size_t>(d);
319 for(
size_t i = 1; i <= (d / 2) * ext_deg; ++i) {
321 if((i % ext_deg) == 0) {
324 s = polyn_gf2m::gcd(g, r);
327 result = i / ext_deg;
343 if(this->m_coeff.size() < trgt_deg) {
346 for(uint32_t i = 0; i < this->m_coeff.size(); i++) {
347 this->m_coeff[i] |= patch_elem;
348 uint32_t equal = (i == trgt_deg);
350 patch_elem &= ~equal_mask;
360 std::shared_ptr<GF2m_Field> m_sp_field = g.m_sp_field;
393 while(dr >= break_deg) {
394 for(j = delta; j >= 0; --j) {
395 gf2m a = m_sp_field->gf_div(r0[dr + j], r1[dr]);
397 gf2m la = m_sp_field->gf_log(a);
399 for(i = 0; i <= du; ++i) {
400 u0[i + j] ^= m_sp_field->gf_mul_zrz(la, u1[i]);
403 for(i = 0; i <= dr; ++i) {
404 r0[i + j] ^= m_sp_field->gf_mul_zrz(la, r1[i]);
415 volatile gf2m fake_elem = 0x01;
416 volatile gf2m cond1 = 0;
417 volatile gf2m cond2 = 0;
428 cond1 = cond1 & cond2;
431 gf2m mask = generate_gf2m_mask(cond1);
432 fake_elem = fake_elem & mask;
437 volatile gf2m fake_elem = 0x00;
438 volatile uint32_t trgt_deg = 0;
472 int cond_u1 = m_sp_field->gf_mul(u0.m_coeff[1], m_sp_field->gf_inv(r0.m_coeff[0])) == 1;
475 int cond_u3 = u0.m_coeff[3] == 0;
477 cond_r &= (cond_u1 & cond_u3);
481 fake_elem = 1 & mask;
485 int cond_u1 = m_sp_field->gf_mul(u0.m_coeff[1], m_sp_field->gf_inv(r0.m_coeff[0])) == 1;
486 int cond_u3 = u0.m_coeff[3] == 0;
488 int cond_u5 = u0.m_coeff[5] == 0;
490 cond_r &= (cond_u1 & cond_u3 & cond_u5);
493 fake_elem = 1 & mask;
497 int cond_u1 = m_sp_field->gf_mul(u0[1], m_sp_field->gf_inv(r0[0])) == 1;
498 int cond_u3 = u0.m_coeff[3] == 0;
500 int cond_u5 = u0.m_coeff[5] == 0;
502 int cond_u7 = u0.m_coeff[7] == 0;
504 cond_r &= (cond_u1 & cond_u3 & cond_u5 & cond_u7);
507 fake_elem = 1 & mask;
521 while(r1[dr - delta] == 0) {
531 return std::make_pair(u1, r1);
535 m_deg(static_cast<int>(t)), m_coeff(t + 1), m_sp_field(sp_field) {
538 for(
size_t i = 0; i < t; ++i) {
550void polyn_gf2m::poly_shiftmod(
const polyn_gf2m& g) {
552 throw Invalid_Argument(
"shiftmod cannot be called on polynomials of degree 1 or less");
554 std::shared_ptr<GF2m_Field> field = g.m_sp_field;
557 gf2m a = field->gf_div(this->m_coeff[t - 1], g.m_coeff[t]);
558 for(
int i = t - 1; i > 0; --i) {
559 this->m_coeff[i] = this->m_coeff[i - 1] ^ this->m_sp_field->gf_mul(a, g.m_coeff[i]);
561 this->m_coeff[0] = field->gf_mul(a, g.m_coeff[0]);
567 uint32_t nb_polyn_sqrt_mat = 0;
568 std::shared_ptr<GF2m_Field> m_sp_field = g.m_sp_field;
569 std::vector<polyn_gf2m> result;
571 nb_polyn_sqrt_mat = t / 2;
580 for(i = 0; i < t * m_sp_field->get_extension_degree() - 1; ++i) {
590 for(i = 0; i < nb_polyn_sqrt_mat; ++i) {
596 for(i = 1; i < nb_polyn_sqrt_mat; i++) {
597 result[i] = result[i - 1];
598 result[i].poly_shiftmod(g);
599 result[i].get_degree();
611 std::shared_ptr<GF2m_Field> m_sp_field = generator.
get_sp_field();
613 std::vector<polyn_gf2m> result;
619 for(j = 0; j < n; j++) {
620 result.push_back(
polyn_gf2m(t - 1, m_sp_field));
622 (*&result[j]).set_coef(t - 1, 1);
623 for(i = t - 2; i >= 0; i--) {
624 (*&result[j]).set_coef(i, (generator)[i + 1] ^ m_sp_field->gf_mul(
lex_to_gray(support[j]), result[j][i + 1]));
626 a = ((generator)[0] ^ m_sp_field->gf_mul(
lex_to_gray(support[j]), result[j][0]));
627 for(i = 0; i < t; i++) {
628 (*&result[j]).set_coef(i, m_sp_field->gf_div(result[j][i], a));
635 m_sp_field(sp_field) {
636 if(encoded.size() % 2) {
637 throw Decoding_Error(
"encoded polynomial has odd length");
639 for(uint32_t i = 0; i < encoded.size(); i += 2) {
640 gf2m el = (encoded[i] << 8) | encoded[i + 1];
641 m_coeff.push_back(el);
655 uint32_t len = m_deg + 1;
656 for(
unsigned i = 0; i < len; i++) {
665 std::swap(this->m_deg, other.m_deg);
666 std::swap(this->m_sp_field, other.m_sp_field);
667 std::swap(this->m_coeff, other.m_coeff);
671 return m_deg == other.m_deg && m_coeff == other.m_coeff;
#define BOTAN_ASSERT(expr, assertion_made)
void randomize(std::span< uint8_t > output)
secure_vector< uint8_t > encode() 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 set_coef(size_t i, gf2m v)
polyn_gf2m(const std::shared_ptr< GF2m_Field > &sp_field)
void swap(polyn_gf2m &other) noexcept
gf2m get_lead_coef() const
void patchup_deg_secure(uint32_t trgt_deg, gf2m patch_elem)
static std::vector< polyn_gf2m > sqmod_init(const polyn_gf2m &g)
static std::vector< polyn_gf2m > sqrt_mod_init(const polyn_gf2m &g)
int calc_degree_secure() const
bool operator==(const polyn_gf2m &other) const
polyn_gf2m sqmod(const std::vector< polyn_gf2m > &sq, int d)
static size_t degppf(const polyn_gf2m &g)
constexpr uint8_t get_byte(T input)
gf2m lex_to_gray(gf2m lex)
constexpr void copy_mem(T *out, const T *in, size_t n)
uint16_t expand_mask_16bit(T tst)
gf2m random_code_element(uint16_t code_length, RandomNumberGenerator &rng)
std::vector< polyn_gf2m > syndrome_init(const polyn_gf2m &generator, const std::vector< gf2m > &support, int n)
gf2m random_gf2m(RandomNumberGenerator &rng)
std::vector< T, secure_allocator< T > > secure_vector
gf2m decode_gf2m(const uint8_t *mem)
constexpr void clear_mem(T *ptr, size_t n)
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)