11#include <botan/internal/gf2m_small_m.h>
13#include <botan/exceptn.h>
41std::vector<gf2m> gf_exp_table(
size_t deg,
gf2m prime_poly) {
44 std::vector<gf2m> tab((
static_cast<size_t>(1) << deg) + 1);
47 for(
size_t i = 1; i < tab.size(); ++i) {
48 const gf2m overflow = tab[i - 1] >> (deg - 1);
49 tab[i] = (tab[i - 1] << 1) ^ (overflow * prime_poly);
55const std::vector<gf2m>& exp_table(
size_t deg) {
59 throw Invalid_Argument(
"GF2m_Field does not support degree " + std::to_string(deg));
62 if(tabs[deg].empty()) {
63 tabs[deg] = gf_exp_table(deg, prim_poly[deg]);
69std::vector<gf2m> gf_log_table(
size_t deg,
const std::vector<gf2m>& exp) {
70 std::vector<gf2m> tab(
static_cast<size_t>(1) << deg);
72 tab[0] =
static_cast<gf2m>((
static_cast<gf2m>(1) << deg) - 1);
73 for(
size_t i = 0; i < tab.size(); ++i) {
74 tab[exp[i]] =
static_cast<gf2m>(i);
79const std::vector<gf2m>& log_table(
size_t deg) {
83 throw Invalid_Argument(
"GF2m_Field does not support degree " + std::to_string(deg));
86 if(tabs[deg].empty()) {
87 tabs[deg] = gf_log_table(deg, exp_table(deg));
97 mem[1] = to_enc & 0xFF;
98 return sizeof(to_enc);
103 result = mem[0] << 8;
109 m_gf_extension_degree(extdeg),
110 m_gf_multiplicative_order((1 << extdeg) - 1),
111 m_gf_log_table(log_table(m_gf_extension_degree)),
112 m_gf_exp_table(exp_table(m_gf_extension_degree)) {}
115 const int32_t sub_res =
static_cast<int32_t
>(
gf_log(x) -
static_cast<int32_t
>(
gf_log(y)));
116 const gf2m modq_res = _gf_modq_1(sub_res);
117 const int32_t div_res =
static_cast<int32_t
>(x) ?
static_cast<int32_t
>(
gf_exp(modq_res)) : 0;
118 return static_cast<gf2m>(div_res);
gf2m gf_div(gf2m x, gf2m y) const
GF2m_Field(size_t extdeg)
gf2m gf_exp(gf2m i) const
gf2m gf_log(gf2m i) const
gf2m decode_gf2m(const uint8_t *mem)
uint32_t encode_gf2m(gf2m to_enc, uint8_t *mem)