Coverage Report

Created: 2022-06-23 06:44

/src/botan/build/include/botan/internal/gf2m_small_m.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_GF2M_SMALL_M_H_
13
#define BOTAN_GF2M_SMALL_M_H_
14
15
#include <botan/types.h>
16
#include <vector>
17
18
namespace Botan {
19
20
typedef uint16_t gf2m;
21
22
/**
23
* GF(2^m) field for m = [2...16]
24
*/
25
class BOTAN_TEST_API GF2m_Field
26
   {
27
   public:
28
      explicit GF2m_Field(size_t extdeg);
29
30
      gf2m gf_mul(gf2m x, gf2m y) const
31
0
         {
32
0
         return ((x) ? gf_mul_fast(x, y) : 0);
33
0
         }
34
35
      gf2m gf_square(gf2m x) const
36
0
         {
37
0
         return ((x) ? gf_exp(_gf_modq_1(gf_log(x) << 1)) : 0);
38
0
         }
39
40
      gf2m square_rr(gf2m x) const
41
0
         {
42
0
         return _gf_modq_1(x << 1);
43
0
         }
44
45
      gf2m gf_mul_fast(gf2m x, gf2m y) const
46
0
         {
47
0
         return ((y) ? gf_exp(_gf_modq_1(gf_log(x) + gf_log(y))) : 0);
48
0
         }
49
50
      /*
51
      naming convention of GF(2^m) field operations:
52
        l logarithmic, unreduced
53
        r logarithmic, reduced
54
        n normal, non-zero
55
        z normal, might be zero
56
      */
57
58
      gf2m gf_mul_lll(gf2m a, gf2m b) const
59
0
         {
60
0
         return (a + b);
61
0
         }
62
63
      gf2m gf_mul_rrr(gf2m a, gf2m b) const
64
0
         {
65
0
         return (_gf_modq_1(gf_mul_lll(a, b)));
66
0
         }
67
68
      gf2m gf_mul_nrr(gf2m a, gf2m b) const
69
0
         {
70
0
         return (gf_exp(gf_mul_rrr(a, b)));
71
0
         }
72
73
      gf2m gf_mul_rrn(gf2m a, gf2m y) const
74
0
         {
75
0
         return _gf_modq_1(gf_mul_lll(a, gf_log(y)));
76
0
         }
77
78
      gf2m gf_mul_rnr(gf2m y, gf2m a) const
79
0
         {
80
0
         return gf_mul_rrn(a, y);
81
0
         }
82
83
      gf2m gf_mul_lnn(gf2m x, gf2m y) const
84
0
         {
85
0
         return (gf_log(x) + gf_log(y));
86
0
         }
87
88
      gf2m gf_mul_rnn(gf2m x, gf2m y) const
89
0
         {
90
0
         return _gf_modq_1(gf_mul_lnn(x, y));
91
0
         }
92
93
      gf2m gf_mul_nrn(gf2m a, gf2m y) const
94
0
         {
95
0
         return gf_exp(_gf_modq_1((a) + gf_log(y)));
96
0
         }
97
98
      /**
99
      * zero operand allowed
100
      */
101
      gf2m gf_mul_zrz(gf2m a, gf2m y) const
102
0
         {
103
0
         return ( (y == 0) ? 0 : gf_mul_nrn(a, y) );
104
0
         }
105
106
      gf2m gf_mul_zzr(gf2m a, gf2m y) const
107
0
         {
108
0
         return gf_mul_zrz(y, a);
109
0
         }
110
111
      /**
112
      * non-zero operand
113
      */
114
      gf2m gf_mul_nnr(gf2m y, gf2m a) const
115
0
         {
116
0
         return gf_mul_nrn(a, y);
117
0
         }
118
119
      gf2m gf_sqrt(gf2m x) const
120
0
         {
121
0
         return ((x) ? gf_exp(_gf_modq_1(gf_log(x) << (get_extension_degree()-1))) : 0);
122
0
         }
123
124
      gf2m gf_div_rnn(gf2m x, gf2m y) const
125
0
         {
126
0
         return _gf_modq_1(gf_log(x) - gf_log(y));
127
0
         }
128
129
      gf2m gf_div_rnr(gf2m x, gf2m b) const
130
0
         {
131
0
         return _gf_modq_1(gf_log(x) - b);
132
0
         }
133
134
      gf2m gf_div_nrr(gf2m a, gf2m b) const
135
0
         {
136
0
         return gf_exp(_gf_modq_1(a - b));
137
0
         }
138
139
      gf2m gf_div_zzr(gf2m x, gf2m b) const
140
0
         {
141
0
         return ((x) ? gf_exp(_gf_modq_1(gf_log(x) - b)) : 0);
142
0
         }
143
144
      gf2m gf_inv(gf2m x) const
145
0
         {
146
0
         return gf_exp(gf_ord() - gf_log(x));
147
0
         }
148
149
      gf2m gf_inv_rn(gf2m x) const
150
0
         {
151
0
         return (gf_ord() - gf_log(x));
152
0
         }
153
154
      gf2m gf_square_ln(gf2m x) const
155
0
         {
156
0
         return gf_log(x) << 1;
157
0
         }
158
159
      gf2m gf_square_rr(gf2m a) const
160
0
         {
161
0
         return a << 1;
162
0
         }
163
164
      gf2m gf_l_from_n(gf2m x) const
165
0
         {
166
0
         return gf_log(x);
167
0
         }
168
169
      gf2m gf_div(gf2m x, gf2m y) const;
170
171
      gf2m gf_exp(gf2m i) const
172
0
         {
173
0
         return m_gf_exp_table.at(i); /* alpha^i */
174
0
         }
175
176
      gf2m gf_log(gf2m i) const
177
0
         {
178
0
         return m_gf_log_table.at(i); /* return i when x=alpha^i */
179
0
         }
180
181
      gf2m gf_ord() const
182
0
         {
183
0
         return m_gf_multiplicative_order;
184
0
         }
185
186
      size_t get_extension_degree() const
187
0
         {
188
0
         return m_gf_extension_degree;
189
0
         }
190
191
      gf2m get_cardinality() const
192
0
         {
193
0
         return static_cast<gf2m>(1 << get_extension_degree());
194
0
         }
195
196
   private:
197
      gf2m _gf_modq_1(int32_t d) const
198
0
         {
199
         /* residual modulo q-1
200
         when -q < d < 0, we get (q-1+d)
201
         when 0 <= d < q, we get (d)
202
         when q <= d < 2q-1, we get (d-q+1)
203
         */
204
0
         return static_cast<gf2m>(((d) & gf_ord()) + ((d) >> get_extension_degree()));
205
0
         }
206
207
      const size_t m_gf_extension_degree;
208
      const gf2m m_gf_multiplicative_order;
209
      const std::vector<gf2m>& m_gf_log_table;
210
      const std::vector<gf2m>& m_gf_exp_table;
211
   };
212
213
uint32_t encode_gf2m(gf2m to_enc, uint8_t* mem);
214
215
gf2m decode_gf2m(const uint8_t* mem);
216
217
}
218
219
#endif