Coverage Report

Created: 2021-05-04 09:02

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