Botan 3.5.0
Crypto and TLS for C&
gf2m_small_m.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_GF2M_SMALL_M_H_
13#define BOTAN_GF2M_SMALL_M_H_
14
15#include <botan/types.h>
16#include <vector>
17
18namespace Botan {
19
20typedef uint16_t gf2m;
21
22/**
23* GF(2^m) field for m = [2...16]
24*/
26 public:
27 explicit GF2m_Field(size_t extdeg);
28
29 gf2m gf_mul(gf2m x, gf2m y) const { return ((x) ? gf_mul_fast(x, y) : 0); }
30
31 gf2m gf_square(gf2m x) const { return ((x) ? gf_exp(_gf_modq_1(gf_log(x) << 1)) : 0); }
32
33 gf2m square_rr(gf2m x) const { return _gf_modq_1(x << 1); }
34
35 gf2m gf_mul_fast(gf2m x, gf2m y) const { return ((y) ? gf_exp(_gf_modq_1(gf_log(x) + gf_log(y))) : 0); }
36
37 /*
38 naming convention of GF(2^m) field operations:
39 l logarithmic, unreduced
40 r logarithmic, reduced
41 n normal, non-zero
42 z normal, might be zero
43 */
44
45 gf2m gf_mul_lll(gf2m a, gf2m b) const { return (a + b); }
46
47 gf2m gf_mul_rrr(gf2m a, gf2m b) const { return (_gf_modq_1(gf_mul_lll(a, b))); }
48
49 gf2m gf_mul_nrr(gf2m a, gf2m b) const { return (gf_exp(gf_mul_rrr(a, b))); }
50
51 gf2m gf_mul_rrn(gf2m a, gf2m y) const { return _gf_modq_1(gf_mul_lll(a, gf_log(y))); }
52
53 gf2m gf_mul_rnr(gf2m y, gf2m a) const { return gf_mul_rrn(a, y); }
54
55 gf2m gf_mul_lnn(gf2m x, gf2m y) const { return (gf_log(x) + gf_log(y)); }
56
57 gf2m gf_mul_rnn(gf2m x, gf2m y) const { return _gf_modq_1(gf_mul_lnn(x, y)); }
58
59 gf2m gf_mul_nrn(gf2m a, gf2m y) const { return gf_exp(_gf_modq_1((a) + gf_log(y))); }
60
61 /**
62 * zero operand allowed
63 */
64 gf2m gf_mul_zrz(gf2m a, gf2m y) const { return ((y == 0) ? 0 : gf_mul_nrn(a, y)); }
65
66 gf2m gf_mul_zzr(gf2m a, gf2m y) const { return gf_mul_zrz(y, a); }
67
68 /**
69 * non-zero operand
70 */
71 gf2m gf_mul_nnr(gf2m y, gf2m a) const { return gf_mul_nrn(a, y); }
72
73 gf2m gf_sqrt(gf2m x) const { return ((x) ? gf_exp(_gf_modq_1(gf_log(x) << (get_extension_degree() - 1))) : 0); }
74
75 gf2m gf_div_rnn(gf2m x, gf2m y) const { return _gf_modq_1(gf_log(x) - gf_log(y)); }
76
77 gf2m gf_div_rnr(gf2m x, gf2m b) const { return _gf_modq_1(gf_log(x) - b); }
78
79 gf2m gf_div_nrr(gf2m a, gf2m b) const { return gf_exp(_gf_modq_1(a - b)); }
80
81 gf2m gf_div_zzr(gf2m x, gf2m b) const { return ((x) ? gf_exp(_gf_modq_1(gf_log(x) - b)) : 0); }
82
83 gf2m gf_inv(gf2m x) const { return gf_exp(gf_ord() - gf_log(x)); }
84
85 gf2m gf_inv_rn(gf2m x) const { return (gf_ord() - gf_log(x)); }
86
87 gf2m gf_square_ln(gf2m x) const { return gf_log(x) << 1; }
88
89 gf2m gf_square_rr(gf2m a) const { return a << 1; }
90
91 gf2m gf_l_from_n(gf2m x) const { return gf_log(x); }
92
93 gf2m gf_div(gf2m x, gf2m y) const;
94
95 gf2m gf_exp(gf2m i) const { return m_gf_exp_table.at(i); /* alpha^i */ }
96
97 gf2m gf_log(gf2m i) const { return m_gf_log_table.at(i); /* return i when x=alpha^i */ }
98
99 gf2m gf_ord() const { return m_gf_multiplicative_order; }
100
101 size_t get_extension_degree() const { return m_gf_extension_degree; }
102
103 gf2m get_cardinality() const { return static_cast<gf2m>(1 << get_extension_degree()); }
104
105 private:
106 gf2m _gf_modq_1(int32_t d) const {
107 /* residual modulo q-1
108 when -q < d < 0, we get (q-1+d)
109 when 0 <= d < q, we get (d)
110 when q <= d < 2q-1, we get (d-q+1)
111 */
112 return static_cast<gf2m>(((d)&gf_ord()) + ((d) >> get_extension_degree()));
113 }
114
115 const size_t m_gf_extension_degree;
116 const gf2m m_gf_multiplicative_order;
117 const std::vector<gf2m>& m_gf_log_table;
118 const std::vector<gf2m>& m_gf_exp_table;
119};
120
121uint32_t encode_gf2m(gf2m to_enc, uint8_t* mem);
122
123gf2m decode_gf2m(const uint8_t* mem);
124
125} // namespace Botan
126
127#endif
gf2m gf_mul_lnn(gf2m x, gf2m y) const
gf2m gf_square_rr(gf2m a) const
gf2m gf_mul_zrz(gf2m a, gf2m y) const
gf2m gf_l_from_n(gf2m x) const
gf2m gf_mul(gf2m x, gf2m y) const
gf2m gf_square_ln(gf2m x) const
gf2m gf_mul_rrr(gf2m a, gf2m b) const
gf2m gf_exp(gf2m i) const
gf2m gf_mul_rnn(gf2m x, gf2m y) const
gf2m square_rr(gf2m x) const
gf2m gf_mul_nrn(gf2m a, gf2m y) const
gf2m gf_div_rnr(gf2m x, gf2m b) const
gf2m gf_mul_rrn(gf2m a, gf2m y) const
gf2m gf_div_zzr(gf2m x, gf2m b) const
gf2m gf_div_rnn(gf2m x, gf2m y) const
gf2m gf_inv(gf2m x) const
gf2m gf_square(gf2m x) const
gf2m gf_sqrt(gf2m x) const
gf2m gf_mul_fast(gf2m x, gf2m y) const
gf2m gf_div_nrr(gf2m a, gf2m b) const
gf2m gf_inv_rn(gf2m x) const
gf2m get_cardinality() const
gf2m gf_mul_nnr(gf2m y, gf2m a) const
gf2m gf_mul_lll(gf2m a, gf2m b) const
gf2m gf_ord() const
gf2m gf_mul_nrr(gf2m a, gf2m b) const
gf2m gf_log(gf2m i) const
size_t get_extension_degree() const
gf2m gf_mul_rnr(gf2m y, gf2m a) const
gf2m gf_mul_zzr(gf2m a, gf2m y) const
#define BOTAN_TEST_API
Definition compiler.h:51
gf2m decode_gf2m(const uint8_t *mem)
uint32_t encode_gf2m(gf2m to_enc, uint8_t *mem)
uint16_t gf2m