/src/botan/build/include/botan/polyn_gf2m.h
Line | Count | Source (jump to first uncovered line) |
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 <botan/gf2m_small_m.h> |
17 | | #include <utility> |
18 | | #include <string> |
19 | | |
20 | | //BOTAN_FUTURE_INTERNAL_HEADER(polyn_gf2m.h) |
21 | | |
22 | | namespace Botan { |
23 | | |
24 | | class RandomNumberGenerator; |
25 | | |
26 | | class polyn_gf2m |
27 | | { |
28 | | public: |
29 | | /** |
30 | | * create a zero polynomial: |
31 | | */ |
32 | | explicit polyn_gf2m( std::shared_ptr<GF2m_Field> sp_field ); |
33 | | |
34 | | polyn_gf2m() |
35 | 0 | :m_deg(-1) {} |
36 | | |
37 | | polyn_gf2m(const secure_vector<uint8_t>& encoded, std::shared_ptr<GF2m_Field> sp_field ); |
38 | | |
39 | 0 | polyn_gf2m& operator=(const polyn_gf2m&) = default; |
40 | | |
41 | | bool operator==(const polyn_gf2m & other) const ; |
42 | | |
43 | 0 | bool operator!=(const polyn_gf2m & other) const { return !(*this == other); } |
44 | | |
45 | | polyn_gf2m(polyn_gf2m&& other) |
46 | 0 | { |
47 | 0 | this->swap(other); |
48 | 0 | } |
49 | | |
50 | | polyn_gf2m & operator=(polyn_gf2m&& other) |
51 | 0 | { |
52 | 0 | if(this != &other) |
53 | 0 | { |
54 | 0 | this->swap(other); |
55 | 0 | } |
56 | 0 | return *this; |
57 | 0 | } |
58 | | |
59 | | void swap(polyn_gf2m& other); |
60 | | |
61 | | secure_vector<uint8_t> encode() const; |
62 | | /** |
63 | | * create zero polynomial with reservation of space for a degree d polynomial |
64 | | */ |
65 | | polyn_gf2m(int d, std::shared_ptr<GF2m_Field> sp_field); |
66 | | |
67 | | polyn_gf2m(polyn_gf2m const& other); |
68 | | /** |
69 | | * create zero polynomial with allocated size determined by specified degree d: |
70 | | */ |
71 | | |
72 | | /** |
73 | | * random irreducible polynomial of degree t |
74 | | */ |
75 | | polyn_gf2m(int t, RandomNumberGenerator& rng, std::shared_ptr<GF2m_Field> sp_field); |
76 | | |
77 | | std::shared_ptr<GF2m_Field> get_sp_field() const |
78 | 0 | { return m_sp_field; } |
79 | | |
80 | 0 | gf2m& operator[](size_t i) { return coeff[i]; } |
81 | | |
82 | 0 | gf2m operator[](size_t i) const { return coeff[i]; } |
83 | | |
84 | 0 | gf2m get_lead_coef() const { return coeff[m_deg]; } |
85 | | |
86 | 0 | gf2m get_coef(uint32_t i) const { return coeff[i]; } |
87 | | |
88 | | inline void set_coef(uint32_t i, gf2m v) |
89 | 0 | { |
90 | 0 | coeff[i] = v; |
91 | 0 | } |
92 | | |
93 | | inline void add_to_coef(uint32_t i, gf2m v) |
94 | 0 | { |
95 | 0 | coeff[i] = coeff[i] ^ v; |
96 | 0 | } |
97 | | |
98 | | std::string to_string() const; |
99 | | |
100 | | /** decode a polynomial from memory: **/ |
101 | | polyn_gf2m(const uint8_t* mem, uint32_t mem_len, std::shared_ptr<GF2m_Field> sp_field); |
102 | | // remove one! ^v! |
103 | | /** |
104 | | * create a polynomial from memory area (encoded) |
105 | | */ |
106 | | polyn_gf2m(int degree, const unsigned char* mem, uint32_t mem_byte_len, std::shared_ptr<GF2m_Field> sp_field); |
107 | | |
108 | | void encode(uint32_t min_numo_coeffs, uint8_t* mem, uint32_t mem_len) const; |
109 | | |
110 | | int get_degree() const; |
111 | | |
112 | | /** |
113 | | * determine the degree in a timing secure manner. the timing of this function |
114 | | * only depends on the number of allocated coefficients, not on the actual |
115 | | * degree |
116 | | */ |
117 | | int calc_degree_secure() const; |
118 | | |
119 | | void degppf(const polyn_gf2m & g, int* p_result); |
120 | | |
121 | | static std::vector<polyn_gf2m> sqmod_init(const polyn_gf2m & g); |
122 | | |
123 | | static std::vector<polyn_gf2m> sqrt_mod_init(const polyn_gf2m & g); |
124 | | |
125 | | |
126 | | polyn_gf2m sqmod(const std::vector<polyn_gf2m> & sq, int d); |
127 | | void set_to_zero(); |
128 | | gf2m eval(gf2m a); |
129 | | |
130 | | static std::pair<polyn_gf2m, polyn_gf2m> eea_with_coefficients(const polyn_gf2m & p, |
131 | | const polyn_gf2m & g, |
132 | | int break_deg); |
133 | | |
134 | | void patchup_deg_secure( uint32_t trgt_deg, volatile gf2m patch_elem); |
135 | | |
136 | | private: |
137 | | |
138 | 0 | void set_degree(int d) { m_deg = d; } |
139 | | |
140 | | void poly_shiftmod( const polyn_gf2m & g); |
141 | | void realloc(uint32_t new_size); |
142 | | static polyn_gf2m gcd(polyn_gf2m const& p1, polyn_gf2m const& p2); |
143 | | |
144 | | /** |
145 | | * destructive: |
146 | | */ |
147 | | static void remainder(polyn_gf2m & p, const polyn_gf2m & g); |
148 | | |
149 | | static polyn_gf2m gcd_aux(polyn_gf2m& p1, polyn_gf2m& p2); |
150 | | public: |
151 | | // public member variable: |
152 | | int m_deg; |
153 | | |
154 | | // public member variable: |
155 | | secure_vector<gf2m> coeff; |
156 | | |
157 | | // public member variable: |
158 | | std::shared_ptr<GF2m_Field> m_sp_field; |
159 | | }; |
160 | | |
161 | | gf2m random_gf2m(RandomNumberGenerator& rng); |
162 | | gf2m random_code_element(unsigned code_length, RandomNumberGenerator& rng); |
163 | | |
164 | | std::vector<polyn_gf2m> syndrome_init(polyn_gf2m const& generator, std::vector<gf2m> const& support, int n); |
165 | | |
166 | | /** |
167 | | * Find the roots of a polynomial over GF(2^m) using the method by Federenko |
168 | | * et al. |
169 | | */ |
170 | | secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m & polyn, uint32_t code_length); |
171 | | |
172 | | } |
173 | | |
174 | | #endif |