9 #include <botan/polyn_gf2m.h>
10 #include <botan/internal/bit_ops.h>
11 #include <botan/internal/code_based_util.h>
12 #include <botan/exceptn.h>
18 void 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 &= cond_mask;
27 for(
size_t i = 0; i < res_root_arr_len; i++)
29 gf2m masked_patch_elem = (patch_elem++) & cond_mask;
30 res_root_arr[i] ^= masked_patch_elem++;
34 class gf2m_decomp_rootfind_state
37 gf2m_decomp_rootfind_state(
const polyn_gf2m & p_polyn,
size_t code_length);
39 void calc_LiK(
const polyn_gf2m & sigma);
40 gf2m calc_Fxj_j_neq_0(
const polyn_gf2m & sigma,
gf2m j_gray);
42 void calc_Ai_zero(
const polyn_gf2m & sigma);
43 secure_vector<gf2m> find_roots(
const polyn_gf2m & sigma);
60 gf2m brootf_decomp_gray_to_lex(
gf2m gray)
62 static_assert(
sizeof(
gf2m) == 2,
"Expected size");
63 gf2m result = gray ^ (gray>>8);
64 result ^= (result >> 4);
65 result ^= (result >> 2);
66 result ^= (result >> 1);
74 uint32_t brootf_decomp_calc_sum_limit(uint32_t t)
87 gf2m_decomp_rootfind_state::gf2m_decomp_rootfind_state(
const polyn_gf2m & polyn,
size_t code_length) :
92 std::shared_ptr<GF2m_Field> sp_field = polyn.get_sp_field();
93 int deg_sigma = polyn.get_degree();
96 throw Internal_Error(
"Unexpected degree in gf2m_decomp_rootfind_state");
99 coeff_3 = polyn.get_coef( 3);
100 coeff_head = polyn.get_coef( deg_sigma);
103 this->
m_sigma_3_l = sp_field->gf_l_from_n(coeff_3);
109 this->
m_sigma_3_l = sp_field->gf_l_from_n(coeff_head);
118 void gf2m_decomp_rootfind_state::calc_Ai_zero(
const polyn_gf2m & sigma)
126 this->
m_Aij[i] = sigma.get_coef(5*i);
132 void gf2m_decomp_rootfind_state::calc_next_Aij()
140 gf2m diff, new_j_gray;
141 uint32_t Lik_pos_base;
151 else if(this->
m_j & 2)
155 else if( this->
m_j & 4)
157 Lik_pos_base = this->m_outer_summands * 2;
159 else if( this->
m_j & 8)
161 Lik_pos_base = this->m_outer_summands * 3;
163 else if( this->
m_j & 16)
165 Lik_pos_base = this->m_outer_summands * 4;
171 while(((static_cast<gf2m>(1) << delta_offs) & diff) == 0)
183 this->
m_Aij[i] ^= this->
m_Lik[Lik_pos_base + i];
188 void gf2m_decomp_rootfind_state::calc_LiK(
const polyn_gf2m & sigma)
190 std::shared_ptr<GF2m_Field> sp_field = sigma.get_sp_field();
192 d = sigma.get_degree();
193 for(k = 0; k < sp_field->get_extension_degree(); k++)
196 gf2m alpha_l_k_tt2_ttj[4];
197 alpha_l_k_tt2_ttj[0] = sp_field->gf_l_from_n(static_cast<gf2m>(1) << k);
198 alpha_l_k_tt2_ttj[1] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[0], alpha_l_k_tt2_ttj[0]);
199 alpha_l_k_tt2_ttj[2] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[1],alpha_l_k_tt2_ttj[1] );
201 alpha_l_k_tt2_ttj[3] = sp_field->gf_mul_rrr(alpha_l_k_tt2_ttj[2], alpha_l_k_tt2_ttj[2]);
205 uint32_t five_i = 5*i;
206 uint32_t Lik_pos = Lik_pos_base + i;
207 this->
m_Lik[Lik_pos] = 0;
208 for(j = 0; j <= 3; j++)
211 uint32_t f_ind = five_i + (
static_cast<uint32_t
>(1) << j);
216 f = sigma.get_coef( f_ind);
218 x = sp_field->gf_mul_zrz(alpha_l_k_tt2_ttj[j], f);
219 this->
m_Lik[Lik_pos] ^= x;
225 gf2m gf2m_decomp_rootfind_state::calc_Fxj_j_neq_0(
const polyn_gf2m & sigma,
gf2m j_gray)
230 std::shared_ptr<GF2m_Field> sp_field = sigma.get_sp_field();
231 const gf2m jl_gray = sp_field->gf_l_from_n(j_gray);
232 gf2m xl_j_tt_5 = sp_field->gf_square_rr(jl_gray);
233 gf2m xl_gray_tt_3 = sp_field->gf_mul_rrr(xl_j_tt_5, jl_gray);
234 xl_j_tt_5 = sp_field->gf_mul_rrr(xl_j_tt_5, xl_gray_tt_3);
237 sum = sp_field->gf_mul_nrr(xl_gray_tt_3, this->
m_sigma_3_l);
243 sum ^= this->
m_Aij[0];
246 if(this->m_outer_summands > 1)
249 x = sp_field->gf_mul_zrz(xl_j_tt_5, this->
m_Aij[1]);
253 gf2m xl_j_tt_5i = xl_j_tt_5;
258 xl_j_tt_5i = sp_field->gf_mul_rrr(xl_j_tt_5i, xl_j_tt_5);
260 x = sp_field->gf_mul_zrz(xl_j_tt_5i, this->
m_Aij[i]);
266 secure_vector<gf2m> gf2m_decomp_rootfind_state::find_roots(
const polyn_gf2m & sigma)
268 const int sigma_degree = sigma.get_degree();
270 secure_vector<gf2m> result(sigma_degree);
271 uint32_t root_pos = 0;
273 this->calc_Ai_zero(sigma);
274 this->calc_LiK(sigma);
281 eval_result = sigma.get_coef(0);
285 eval_result = this->calc_Fxj_j_neq_0(sigma, this->
m_j_gray);
298 this->calc_next_Aij();
302 patch_root_array(result.data(), result.size(), root_pos);
310 gf2m_decomp_rootfind_state state(polyn, code_length);
311 return state.find_roots(polyn);
secure_vector< gf2m > m_Aij
secure_vector< gf2m > m_Lik
#define BOTAN_ASSERT(expr, assertion_made)
std::vector< T, secure_allocator< T >> secure_vector
gf2m lex_to_gray(gf2m lex)
uint16_t expand_mask_16bit(T tst)
gf2m m_sigma_3_neq_0_mask
uint32_t m_outer_summands
secure_vector< gf2m > find_roots_gf2m_decomp(const polyn_gf2m &polyn, size_t code_length)