9#include <botan/internal/polyn_gf2m.h>
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <botan/internal/code_based_util.h>
19void patch_root_array(
gf2m res_root_arr[],
size_t res_root_arr_len,
size_t root_pos) {
20 volatile gf2m patch_elem = 0x01;
21 volatile gf2m cond_mask =
static_cast<gf2m>(root_pos == res_root_arr_len);
23 cond_mask = ~cond_mask;
24 patch_elem = patch_elem & cond_mask;
25 for(
size_t i = 0; i < res_root_arr_len; i++) {
26 patch_elem = patch_elem + 1;
27 gf2m masked_patch_elem = patch_elem & cond_mask;
28 res_root_arr[i] ^= masked_patch_elem++;
32class gf2m_decomp_rootfind_state {
34 gf2m_decomp_rootfind_state(
const polyn_gf2m& p_polyn,
size_t code_length);
36 void calc_LiK(
const polyn_gf2m&
sigma);
39 void calc_Ai_zero(
const polyn_gf2m&
sigma);
46 uint32_t m_outer_summands;
50 gf2m m_sigma_3_neq_0_mask;
56uint32_t brootf_decomp_calc_sum_limit(uint32_t t) {
60 uint32_t result = t - 4;
66gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(
const polyn_gf2m& polyn,
size_t code_length) :
67 m_code_length(code_length), m_j(0), m_j_gray(0) {
68 std::shared_ptr<GF2m_Field> sp_field = polyn.get_sp_field();
69 int deg_sigma = polyn.get_degree();
71 throw Internal_Error(
"Unexpected degree in gf2m_decomp_rootfind_state");
74 gf2m coeff_3 = polyn.get_coef(3);
75 gf2m coeff_head = polyn.get_coef(deg_sigma);
77 this->m_sigma_3_l = sp_field->gf_l_from_n(coeff_3);
78 this->m_sigma_3_neq_0_mask = 0xFFFF;
81 this->m_sigma_3_l = sp_field->gf_l_from_n(coeff_head);
82 this->m_sigma_3_neq_0_mask = 0;
85 this->m_outer_summands = 1 + brootf_decomp_calc_sum_limit(deg_sigma);
86 this->m_Lik.resize(this->m_outer_summands * sp_field->get_extension_degree());
87 this->m_Aij.resize(this->m_outer_summands);
90void gf2m_decomp_rootfind_state::calc_Ai_zero(
const polyn_gf2m& sigma) {
94 for(uint32_t i = 0; i < this->m_outer_summands; i++) {
95 this->m_Aij[i] =
sigma.get_coef(5 * i);
101void gf2m_decomp_rootfind_state::calc_next_Aij() {
107 uint32_t Lik_pos_base = 0;
113 if((this->m_j & 1) != 0) {
116 }
else if((this->m_j & 2) != 0) {
118 Lik_pos_base = this->m_outer_summands;
119 }
else if((this->m_j & 4) != 0) {
121 Lik_pos_base = this->m_outer_summands * 2;
122 }
else if((this->m_j & 8) != 0) {
124 Lik_pos_base = this->m_outer_summands * 3;
125 }
else if((this->m_j & 16) != 0) {
126 Lik_pos_base = this->m_outer_summands * 4;
129 gf2m diff = this->m_j_gray ^ new_j_gray;
130 while(((
static_cast<gf2m>(1) << delta_offs) & diff) == 0) {
133 Lik_pos_base = delta_offs * this->m_outer_summands;
135 this->m_j_gray = new_j_gray;
137 for(uint32_t i = 0; i < this->m_outer_summands; i++) {
138 this->m_Aij[i] ^= this->m_Lik[Lik_pos_base + i];
142void gf2m_decomp_rootfind_state::calc_LiK(
const polyn_gf2m& sigma) {
143 std::shared_ptr<GF2m_Field> sp_field =
sigma.get_sp_field();
144 uint32_t d =
sigma.get_degree();
145 for(uint32_t k = 0; k < sp_field->get_extension_degree(); k++) {
146 uint32_t Lik_pos_base = k * this->m_outer_summands;
147 gf2m alpha_l_k_tt2_ttj[4];
148 alpha_l_k_tt2_ttj[0] = sp_field->gf_l_from_n(
static_cast<gf2m>(1) << k);
149 alpha_l_k_tt2_ttj[1] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[0], alpha_l_k_tt2_ttj[0]);
150 alpha_l_k_tt2_ttj[2] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[1], alpha_l_k_tt2_ttj[1]);
152 alpha_l_k_tt2_ttj[3] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[2], alpha_l_k_tt2_ttj[2]);
153 for(uint32_t i = 0; i < this->m_outer_summands; i++) {
154 const uint32_t five_i = 5 * i;
155 const uint32_t Lik_pos = Lik_pos_base + i;
156 this->m_Lik[Lik_pos] = 0;
157 for(
size_t j = 0; j <= 3; j++) {
158 uint32_t f_ind = five_i + (
static_cast<uint32_t
>(1) << j);
164 gf2m x = sp_field->gf_mul_zrz(alpha_l_k_tt2_ttj[j], f);
165 this->m_Lik[Lik_pos] ^= x;
171gf2m gf2m_decomp_rootfind_state::calc_Fxj_j_neq_0(
const polyn_gf2m& sigma, gf2m j_gray) {
174 std::shared_ptr<GF2m_Field> sp_field =
sigma.get_sp_field();
175 const gf2m jl_gray = sp_field->gf_l_from_n(j_gray);
176 gf2m xl_j_tt_5 = sp_field->gf_square_rr(jl_gray);
177 gf2m xl_gray_tt_3 = sp_field->gf_mul_rrr(xl_j_tt_5, jl_gray);
178 xl_j_tt_5 = sp_field->gf_mul_rrr(xl_j_tt_5, xl_gray_tt_3);
180 sum = sp_field->gf_mul_nrr(xl_gray_tt_3, this->m_sigma_3_l);
181 sum &= this->m_sigma_3_neq_0_mask;
186 sum ^= this->m_Aij[0];
189 if(this->m_outer_summands > 1) {
190 gf2m x = sp_field->gf_mul_zrz(xl_j_tt_5, this->m_Aij[1]);
194 gf2m xl_j_tt_5i = xl_j_tt_5;
196 for(uint32_t i = 2; i < this->m_outer_summands; i++) {
197 xl_j_tt_5i = sp_field->gf_mul_rrr(xl_j_tt_5i, xl_j_tt_5);
199 gf2m x = sp_field->gf_mul_zrz(xl_j_tt_5i, this->m_Aij[i]);
205secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(
const polyn_gf2m& sigma) {
206 const int sigma_degree =
sigma.get_degree();
208 secure_vector<gf2m> result(sigma_degree);
209 uint32_t root_pos = 0;
211 this->calc_Ai_zero(sigma);
212 this->calc_LiK(sigma);
214 gf2m eval_result = 0;
216 if(this->m_j_gray == 0) {
217 eval_result =
sigma.get_coef(0);
219 eval_result = this->calc_Fxj_j_neq_0(sigma, this->m_j_gray);
222 if(eval_result == 0) {
223 result[root_pos] = this->m_j_gray;
227 if(this->m_j +
static_cast<uint32_t
>(1) == m_code_length) {
230 this->calc_next_Aij();
234 patch_root_array(result.data(), result.size(), root_pos);
241 gf2m_decomp_rootfind_state state(polyn, code_length);
242 return state.find_roots(polyn);
#define BOTAN_ASSERT(expr, assertion_made)
gf2m lex_to_gray(gf2m lex)
uint16_t expand_mask_16bit(T tst)
BOTAN_FORCE_INLINE constexpr T sigma(T x)
std::vector< T, secure_allocator< T > > secure_vector
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)