Coverage Report

Created: 2023-02-13 06:21

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