9#include <botan/internal/polyn_gf2m.h>
10#include <botan/internal/bit_ops.h>
11#include <botan/internal/code_based_util.h>
12#include <botan/exceptn.h>
18void patch_root_array(
gf2m res_root_arr[],
19 size_t res_root_arr_len,
22 volatile gf2m patch_elem = 0x01;
23 volatile gf2m cond_mask = (root_pos == res_root_arr_len);
25 cond_mask = ~cond_mask;
26 patch_elem = patch_elem & cond_mask;
27 for(
size_t i = 0; i < res_root_arr_len; i++)
29 patch_elem = patch_elem + 1;
30 gf2m masked_patch_elem = patch_elem & cond_mask;
31 res_root_arr[i] ^= masked_patch_elem++;
35class gf2m_decomp_rootfind_state
38 gf2m_decomp_rootfind_state(
const polyn_gf2m & p_polyn,
size_t code_length);
40 void calc_LiK(
const polyn_gf2m &
sigma);
41 gf2m calc_Fxj_j_neq_0(
const polyn_gf2m &
sigma,
gf2m j_gray);
43 void calc_Ai_zero(
const polyn_gf2m &
sigma);
44 secure_vector<gf2m> find_roots(
const polyn_gf2m &
sigma);
48 secure_vector<gf2m> m_Lik;
49 secure_vector<gf2m> m_Aij;
50 uint32_t m_outer_summands;
54 gf2m m_sigma_3_neq_0_mask;
60uint32_t brootf_decomp_calc_sum_limit(uint32_t t)
73gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(
const polyn_gf2m & polyn,
size_t code_length) :
74 m_code_length(code_length), m_j(0), m_j_gray(0)
78 std::shared_ptr<GF2m_Field> sp_field = polyn.get_sp_field();
79 int deg_sigma = polyn.get_degree();
82 throw Internal_Error(
"Unexpected degree in gf2m_decomp_rootfind_state");
85 coeff_3 = polyn.get_coef( 3);
86 coeff_head = polyn.get_coef( deg_sigma);
89 this->m_sigma_3_l = sp_field->gf_l_from_n(coeff_3);
90 this->m_sigma_3_neq_0_mask = 0xFFFF;
95 this->m_sigma_3_l = sp_field->gf_l_from_n(coeff_head);
96 this->m_sigma_3_neq_0_mask = 0 ;
99 this->m_outer_summands = 1 + brootf_decomp_calc_sum_limit(deg_sigma);
100 this->m_Lik.resize(this->m_outer_summands * sp_field->get_extension_degree());
101 this->m_Aij.resize(this->m_outer_summands);
104void gf2m_decomp_rootfind_state::calc_Ai_zero(
const polyn_gf2m & sigma)
110 for(i = 0; i < this->m_outer_summands; i++)
112 this->m_Aij[i] =
sigma.get_coef(5*i);
118void gf2m_decomp_rootfind_state::calc_next_Aij()
126 gf2m diff, new_j_gray;
127 uint32_t Lik_pos_base;
137 else if(this->m_j & 2)
139 Lik_pos_base = this->m_outer_summands;
141 else if( this->m_j & 4)
143 Lik_pos_base = this->m_outer_summands * 2;
145 else if( this->m_j & 8)
147 Lik_pos_base = this->m_outer_summands * 3;
149 else if( this->m_j & 16)
151 Lik_pos_base = this->m_outer_summands * 4;
156 diff = this->m_j_gray ^ new_j_gray;
157 while(((
static_cast<gf2m>(1) << delta_offs) & diff) == 0)
162 Lik_pos_base = delta_offs * this->m_outer_summands;
164 this->m_j_gray = new_j_gray;
167 for(; i < this->m_outer_summands; i++)
169 this->m_Aij[i] ^= this->m_Lik[Lik_pos_base + i];
174void gf2m_decomp_rootfind_state::calc_LiK(
const polyn_gf2m & sigma)
176 std::shared_ptr<GF2m_Field> sp_field =
sigma.get_sp_field();
178 d =
sigma.get_degree();
179 for(k = 0; k < sp_field->get_extension_degree(); k++)
181 uint32_t Lik_pos_base = k * this->m_outer_summands;
182 gf2m alpha_l_k_tt2_ttj[4];
183 alpha_l_k_tt2_ttj[0] = sp_field->gf_l_from_n(
static_cast<gf2m>(1) << k);
184 alpha_l_k_tt2_ttj[1] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[0], alpha_l_k_tt2_ttj[0]);
185 alpha_l_k_tt2_ttj[2] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[1],alpha_l_k_tt2_ttj[1] );
187 alpha_l_k_tt2_ttj[3] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[2], alpha_l_k_tt2_ttj[2]);
188 for(i = 0; i < this->m_outer_summands; i++)
191 uint32_t five_i = 5*i;
192 uint32_t Lik_pos = Lik_pos_base + i;
193 this->m_Lik[Lik_pos] = 0;
194 for(j = 0; j <= 3; j++)
197 uint32_t f_ind = five_i + (
static_cast<uint32_t
>(1) << j);
202 f =
sigma.get_coef( f_ind);
204 x = sp_field->gf_mul_zrz(alpha_l_k_tt2_ttj[j], f);
205 this->m_Lik[Lik_pos] ^= x;
211gf2m gf2m_decomp_rootfind_state::calc_Fxj_j_neq_0(
const polyn_gf2m & sigma, gf2m j_gray)
216 std::shared_ptr<GF2m_Field> sp_field =
sigma.get_sp_field();
217 const gf2m jl_gray = sp_field->gf_l_from_n(j_gray);
218 gf2m xl_j_tt_5 = sp_field->gf_square_rr(jl_gray);
219 gf2m xl_gray_tt_3 = sp_field->gf_mul_rrr(xl_j_tt_5, jl_gray);
220 xl_j_tt_5 = sp_field->gf_mul_rrr(xl_j_tt_5, xl_gray_tt_3);
223 sum = sp_field->gf_mul_nrr(xl_gray_tt_3, this->m_sigma_3_l);
224 sum &= this->m_sigma_3_neq_0_mask;
229 sum ^= this->m_Aij[0];
232 if(this->m_outer_summands > 1)
235 x = sp_field->gf_mul_zrz(xl_j_tt_5, this->m_Aij[1]);
239 gf2m xl_j_tt_5i = xl_j_tt_5;
241 for(i = 2; i < this->m_outer_summands; i++)
244 xl_j_tt_5i = sp_field->gf_mul_rrr(xl_j_tt_5i, xl_j_tt_5);
246 x = sp_field->gf_mul_zrz(xl_j_tt_5i, this->m_Aij[i]);
252secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(
const polyn_gf2m & sigma)
254 const int sigma_degree =
sigma.get_degree();
256 secure_vector<gf2m> result(sigma_degree);
257 uint32_t root_pos = 0;
259 this->calc_Ai_zero(sigma);
260 this->calc_LiK(sigma);
265 if(this->m_j_gray == 0)
267 eval_result =
sigma.get_coef(0);
271 eval_result = this->calc_Fxj_j_neq_0(sigma, this->m_j_gray);
276 result[root_pos] = this->m_j_gray;
280 if(this->m_j +
static_cast<uint32_t
>(1) == m_code_length)
284 this->calc_next_Aij();
288 patch_root_array(result.data(), result.size(), root_pos);
296 gf2m_decomp_rootfind_state state(polyn, code_length);
297 return state.find_roots(polyn);
#define BOTAN_ASSERT(expr, assertion_made)
gf2m lex_to_gray(gf2m lex)
uint16_t expand_mask_16bit(T tst)
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)
std::vector< T, secure_allocator< T > > secure_vector