Botan 3.4.0
Crypto and TLS for C&
polyn_gf2m.h
Go to the documentation of this file.
1/*
2 * (C) Copyright Projet SECRET, INRIA, Rocquencourt
3 * (C) Bhaskar Biswas and Nicolas Sendrier
4 *
5 * (C) 2014 cryptosource GmbH
6 * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
7 *
8 * Botan is released under the Simplified BSD License (see license.txt)
9 *
10 */
11
12#ifndef BOTAN_POLYN_GF2M_H_
13#define BOTAN_POLYN_GF2M_H_
14
15#include <botan/secmem.h>
16#include <utility>
17
18namespace Botan {
19
20typedef uint16_t gf2m;
21
22class GF2m_Field;
23
24class RandomNumberGenerator;
25
27 public:
28 /**
29 * create a zero polynomial:
30 */
31 explicit polyn_gf2m(const std::shared_ptr<GF2m_Field>& sp_field);
32
33 polyn_gf2m() : m_deg(-1) {}
34
35 polyn_gf2m(const secure_vector<uint8_t>& encoded, const std::shared_ptr<GF2m_Field>& sp_field);
36
37 polyn_gf2m& operator=(const polyn_gf2m&) = default;
38
39 /**
40 * create zero polynomial with reservation of space for a degree d polynomial
41 */
42 polyn_gf2m(int d, const std::shared_ptr<GF2m_Field>& sp_field);
43
44 polyn_gf2m(const polyn_gf2m& other);
45
46 /**
47 * random irreducible polynomial of degree t
48 */
49 polyn_gf2m(size_t t, RandomNumberGenerator& rng, const std::shared_ptr<GF2m_Field>& sp_field);
50
51 /** decode a polynomial from memory: **/
52 polyn_gf2m(const uint8_t* mem, uint32_t mem_len, const std::shared_ptr<GF2m_Field>& sp_field);
53
54 /**
55 * create a polynomial from memory area (encoded)
56 */
57 polyn_gf2m(int degree, const uint8_t* mem, size_t mem_byte_len, const std::shared_ptr<GF2m_Field>& sp_field);
58
59 bool operator==(const polyn_gf2m& other) const;
60
61 bool operator!=(const polyn_gf2m& other) const { return !(*this == other); }
62
63 polyn_gf2m(polyn_gf2m&& other) noexcept { this->swap(other); }
64
65 polyn_gf2m& operator=(polyn_gf2m&& other) noexcept {
66 if(this != &other) {
67 this->swap(other);
68 }
69 return *this;
70 }
71
72 void swap(polyn_gf2m& other);
73
75
76 std::shared_ptr<GF2m_Field> get_sp_field() const { return m_sp_field; }
77
78 gf2m& operator[](size_t i) { return m_coeff[i]; }
79
80 gf2m operator[](size_t i) const { return m_coeff[i]; }
81
82 gf2m get_lead_coef() const { return m_coeff[m_deg]; }
83
84 gf2m get_coef(size_t i) const { return m_coeff[i]; }
85
86 inline void set_coef(size_t i, gf2m v) { m_coeff[i] = v; }
87
88 inline void add_to_coef(size_t i, gf2m v) { m_coeff[i] ^= v; }
89
90 void encode(uint32_t min_numo_coeffs, uint8_t* mem, uint32_t mem_len) const;
91
92 int get_degree() const;
93
94 /**
95 * determine the degree in a timing secure manner. the timing of this function
96 * only depends on the number of allocated coefficients, not on the actual
97 * degree
98 */
99 int calc_degree_secure() const;
100
101 static size_t degppf(const polyn_gf2m& g);
102
103 static std::vector<polyn_gf2m> sqmod_init(const polyn_gf2m& g);
104
105 static std::vector<polyn_gf2m> sqrt_mod_init(const polyn_gf2m& g);
106
107 polyn_gf2m sqmod(const std::vector<polyn_gf2m>& sq, int d);
108 void set_to_zero();
109 gf2m eval(gf2m a);
110
111 static std::pair<polyn_gf2m, polyn_gf2m> eea_with_coefficients(const polyn_gf2m& p,
112 const polyn_gf2m& g,
113 int break_deg);
114
115 void patchup_deg_secure(uint32_t trgt_deg, gf2m patch_elem);
116
117 private:
118 void set_degree(int d) { m_deg = d; }
119
120 void poly_shiftmod(const polyn_gf2m& g);
121 void realloc(uint32_t new_size);
122 static polyn_gf2m gcd(const polyn_gf2m& p1, const polyn_gf2m& p2);
123
124 /**
125 * destructive:
126 */
127 static void remainder(polyn_gf2m& p, const polyn_gf2m& g);
128
129 static polyn_gf2m gcd_aux(polyn_gf2m& p1, polyn_gf2m& p2);
130
131 private:
132 int m_deg;
133 secure_vector<gf2m> m_coeff;
134 std::shared_ptr<GF2m_Field> m_sp_field;
135};
136
137gf2m random_gf2m(RandomNumberGenerator& rng);
138gf2m random_code_element(uint16_t code_length, RandomNumberGenerator& rng);
139
140std::vector<polyn_gf2m> syndrome_init(const polyn_gf2m& generator, const std::vector<gf2m>& support, int n);
141
142/**
143* Find the roots of a polynomial over GF(2^m) using the method by Federenko et al.
144*/
145secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m& polyn, size_t code_length);
146
147} // namespace Botan
148
149#endif
polyn_gf2m(const polyn_gf2m &other)
secure_vector< uint8_t > encode() const
int get_degree() const
std::shared_ptr< GF2m_Field > get_sp_field() const
Definition polyn_gf2m.h:76
bool operator!=(const polyn_gf2m &other) const
Definition polyn_gf2m.h:61
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)
Definition polyn_gf2m.h:86
gf2m get_coef(size_t i) const
Definition polyn_gf2m.h:84
gf2m get_lead_coef() const
Definition polyn_gf2m.h:82
void patchup_deg_secure(uint32_t trgt_deg, gf2m patch_elem)
polyn_gf2m & operator=(const polyn_gf2m &)=default
static std::vector< polyn_gf2m > sqmod_init(const polyn_gf2m &g)
void swap(polyn_gf2m &other)
static std::vector< polyn_gf2m > sqrt_mod_init(const polyn_gf2m &g)
void encode(uint32_t min_numo_coeffs, uint8_t *mem, uint32_t mem_len) const
polyn_gf2m & operator=(polyn_gf2m &&other) noexcept
Definition polyn_gf2m.h:65
gf2m operator[](size_t i) const
Definition polyn_gf2m.h:80
void add_to_coef(size_t i, gf2m v)
Definition polyn_gf2m.h:88
int calc_degree_secure() const
bool operator==(const polyn_gf2m &other) const
polyn_gf2m(polyn_gf2m &&other) noexcept
Definition polyn_gf2m.h:63
gf2m & operator[](size_t i)
Definition polyn_gf2m.h:78
polyn_gf2m sqmod(const std::vector< polyn_gf2m > &sq, int d)
gf2m eval(gf2m a)
static size_t degppf(const polyn_gf2m &g)
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
Definition secmem.h:61
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)
uint16_t gf2m