From ea5598c0f4bc10fec05013ce548445b7eded9b2c Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Thu, 3 Jan 2019 09:34:54 -0500 Subject: Some cleanups in McEliece keygen Lots more of this needed in here --- src/lib/pubkey/mce/code_based_key_gen.cpp | 32 ++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/lib/pubkey/mce/code_based_key_gen.cpp b/src/lib/pubkey/mce/code_based_key_gen.cpp index 7ddc45655..97414418d 100644 --- a/src/lib/pubkey/mce/code_based_key_gen.cpp +++ b/src/lib/pubkey/mce/code_based_key_gen.cpp @@ -207,10 +207,8 @@ std::unique_ptr generate_R(std::vector &L, polyn_gf2m* g, s McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, uint32_t ext_deg, uint32_t code_length, uint32_t t) { - uint32_t i, j, k, l; - std::unique_ptr R; + const size_t codimension = t * ext_deg; - uint32_t codimension = t * ext_deg; if(code_length <= codimension) { throw Invalid_Argument("invalid McEliece parameters"); @@ -220,22 +218,26 @@ McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, uint32_t //pick the support......... std::vector L(code_length); - for(i=0;i(i); } randomize_support(L, rng); polyn_gf2m g(sp_field); // create as zero + bool success = false; + std::unique_ptr R; + do { // create a random irreducible polynomial g = polyn_gf2m (t, rng, sp_field); - try{ - R = generate_R(L,&g, sp_field, code_length, t); - success = true; - } + try + { + R = generate_R(L, &g, sp_field, code_length, t); + success = true; + } catch(const Invalid_State &) { } @@ -253,12 +255,12 @@ McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, uint32_t // std::vector H(bit_size_to_32bit_size(codimension) * code_length ); uint32_t* sk = H.data(); - for (i = 0; i < code_length; ++i) + for(size_t i = 0; i < code_length; ++i) { - for (l = 0; l < t; ++l) + for(size_t l = 0; l < t; ++l) { - k = (l * ext_deg) / 32; - j = (l * ext_deg) % 32; + const uint32_t k = (l * ext_deg) / 32; + const uint32_t j = (l * ext_deg) % 32; sk[k] ^= static_cast(F[i].get_coef(l)) << j; if (j + ext_deg > 32) { @@ -272,12 +274,12 @@ McEliece_PrivateKey generate_mceliece_key( RandomNumberGenerator & rng, uint32_t // inverse is needed std::vector Linv(code_length) ; - for (i = 0; i < code_length; ++i) + for (size_t i = 0; i != Linv.size(); ++i) { Linv[L[i]] = i; } std::vector pubmat (R->m_elem.size() * 4); - for(i = 0; i < R->m_elem.size(); i++) + for(size_t i = 0; i < R->m_elem.size(); i++) { store_le(R->m_elem[i], &pubmat[i*4]); } -- cgit v1.2.3