Botan 3.9.0
Crypto and TLS for C&
polyn_gf2m.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_POLYN_GF2M_H_
13#define BOTAN_POLYN_GF2M_H_
14
15#include <botan/secmem.h>
16#include <memory>
17#include <utility>
18
19namespace Botan {
20
21typedef uint16_t gf2m;
22
23class GF2m_Field;
24
26
28 public:
29 /**
30 * create a zero polynomial:
31 */
32 explicit polyn_gf2m(const std::shared_ptr<GF2m_Field>& sp_field);
33
34 polyn_gf2m() : m_deg(-1) {}
35
36 polyn_gf2m(const secure_vector<uint8_t>& encoded, const std::shared_ptr<GF2m_Field>& sp_field);
37
38 ~polyn_gf2m() = default;
39
40 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(const polyn_gf2m& other);
48
49 /**
50 * random irreducible polynomial of degree t
51 */
52 polyn_gf2m(size_t t, RandomNumberGenerator& rng, const std::shared_ptr<GF2m_Field>& sp_field);
53
54 /** decode a polynomial from memory: **/
55 polyn_gf2m(const uint8_t* mem, uint32_t mem_len, const std::shared_ptr<GF2m_Field>& sp_field);
56
57 /**
58 * create a polynomial from memory area (encoded)
59 */
60 polyn_gf2m(int degree, const uint8_t* mem, size_t mem_byte_len, const std::shared_ptr<GF2m_Field>& sp_field);
61
62 bool operator==(const polyn_gf2m& other) const;
63
64 bool operator!=(const polyn_gf2m& other) const { return !(*this == other); }
65
66 polyn_gf2m(polyn_gf2m&& other) noexcept { this->swap(other); }
67
68 polyn_gf2m& operator=(polyn_gf2m&& other) noexcept {
69 if(this != &other) {
70 this->swap(other);
71 }
72 return *this;
73 }
74
75 void swap(polyn_gf2m& other) noexcept;
76
78
79 std::shared_ptr<GF2m_Field> get_sp_field() const { return m_sp_field; }
80
81 gf2m& operator[](size_t i) { return m_coeff[i]; }
82
83 gf2m operator[](size_t i) const { return m_coeff[i]; }
84
85 gf2m get_lead_coef() const { return m_coeff[m_deg]; }
86
87 gf2m get_coef(size_t i) const { return m_coeff[i]; }
88
89 inline void set_coef(size_t i, gf2m v) { m_coeff[i] = v; }
90
91 inline void add_to_coef(size_t i, gf2m v) { m_coeff[i] ^= v; }
92
93 void encode(uint32_t min_numo_coeffs, uint8_t* mem, uint32_t mem_len) const;
94
95 int get_degree() const;
96
97 /**
98 * determine the degree in a timing secure manner. the timing of this function
99 * only depends on the number of allocated coefficients, not on the actual
100 * degree
101 */
102 int calc_degree_secure() const;
103
104 static size_t degppf(const polyn_gf2m& g);
105
106 static std::vector<polyn_gf2m> sqmod_init(const polyn_gf2m& g);
107
108 static std::vector<polyn_gf2m> sqrt_mod_init(const polyn_gf2m& g);
109
110 polyn_gf2m sqmod(const std::vector<polyn_gf2m>& sq, int d);
111 void set_to_zero();
112 gf2m eval(gf2m a);
113
114 static std::pair<polyn_gf2m, polyn_gf2m> eea_with_coefficients(const polyn_gf2m& p,
115 const polyn_gf2m& g,
116 int break_deg);
117
118 void patchup_deg_secure(uint32_t trgt_deg, gf2m patch_elem);
119
120 private:
121 void set_degree(int d) { m_deg = d; }
122
123 void poly_shiftmod(const polyn_gf2m& g);
124 void realloc(uint32_t new_size);
125 static polyn_gf2m gcd(const polyn_gf2m& p1, const polyn_gf2m& p2);
126
127 /**
128 * destructive:
129 */
130 static void remainder(polyn_gf2m& p, const polyn_gf2m& g);
131
132 static polyn_gf2m gcd_aux(polyn_gf2m& p1, polyn_gf2m& p2);
133
134 private:
135 int m_deg = -1;
136 secure_vector<gf2m> m_coeff;
137 std::shared_ptr<GF2m_Field> m_sp_field;
138};
139
140gf2m random_gf2m(RandomNumberGenerator& rng);
141gf2m random_code_element(uint16_t code_length, RandomNumberGenerator& rng);
142
143std::vector<polyn_gf2m> syndrome_init(const polyn_gf2m& generator, const std::vector<gf2m>& support, int n);
144
145/**
146* Find the roots of a polynomial over GF(2^m) using the method by Federenko et al.
147*/
148secure_vector<gf2m> find_roots_gf2m_decomp(const polyn_gf2m& polyn, size_t code_length);
149
150} // namespace Botan
151
152#endif
polyn_gf2m(const polyn_gf2m &other)
secure_vector< uint8_t > encode() const
int get_degree() const
std::shared_ptr< GF2m_Field > get_sp_field() const
Definition polyn_gf2m.h:79
bool operator!=(const polyn_gf2m &other) const
Definition polyn_gf2m.h:64
static std::pair< polyn_gf2m, polyn_gf2m > eea_with_coefficients(const polyn_gf2m &p, const polyn_gf2m &g, int break_deg)
~polyn_gf2m()=default
void set_coef(size_t i, gf2m v)
Definition polyn_gf2m.h:89
polyn_gf2m(const std::shared_ptr< GF2m_Field > &sp_field)
gf2m get_coef(size_t i) const
Definition polyn_gf2m.h:87
void swap(polyn_gf2m &other) noexcept
gf2m get_lead_coef() const
Definition polyn_gf2m.h:85
void patchup_deg_secure(uint32_t trgt_deg, gf2m patch_elem)
polyn_gf2m & operator=(const polyn_gf2m &)=default
static std::vector< polyn_gf2m > sqmod_init(const polyn_gf2m &g)
static std::vector< polyn_gf2m > sqrt_mod_init(const polyn_gf2m &g)
void encode(uint32_t min_numo_coeffs, uint8_t *mem, uint32_t mem_len) const
polyn_gf2m & operator=(polyn_gf2m &&other) noexcept
Definition polyn_gf2m.h:68
gf2m operator[](size_t i) const
Definition polyn_gf2m.h:83
void add_to_coef(size_t i, gf2m v)
Definition polyn_gf2m.h:91
int calc_degree_secure() const
bool operator==(const polyn_gf2m &other) const
polyn_gf2m(polyn_gf2m &&other) noexcept
Definition polyn_gf2m.h:66
gf2m & operator[](size_t i)
Definition polyn_gf2m.h:81
polyn_gf2m sqmod(const std::vector< polyn_gf2m > &sq, int d)
gf2m eval(gf2m a)
static size_t degppf(const polyn_gf2m &g)
gf2m random_code_element(uint16_t code_length, RandomNumberGenerator &rng)
std::vector< polyn_gf2m > syndrome_init(const polyn_gf2m &generator, const std::vector< gf2m > &support, int n)
gf2m random_gf2m(RandomNumberGenerator &rng)
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:193
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)
uint16_t gf2m