• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.49
/src/lib/pubkey/mce/code_based_key_gen.cpp
1
/*
2
 * (C) Copyright Projet SECRET, INRIA, Rocquencourt
3
 * (C) Bhaskar Biswas and  Nicolas Sendrier
4
 *
5
 * (C) 2014 cryptosource GmbH
6
 * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
7
 * (C) 2015 Jack Lloyd
8
 *
9
 * Botan is released under the Simplified BSD License (see license.txt)
10
 *
11
 */
12

13
#include <botan/mceliece.h>
14

15
#include <botan/internal/code_based_util.h>
16
#include <botan/internal/loadstor.h>
17
#include <botan/internal/mce_internal.h>
18
#include <botan/internal/polyn_gf2m.h>
19

20
namespace Botan {
21

22
namespace {
23

24
class binary_matrix final {
196✔
25
   public:
26
      binary_matrix(size_t m_rown, size_t m_coln);
27

28
      void row_xor(size_t a, size_t b);
29
      secure_vector<size_t> row_reduced_echelon_form();
30

31
      /**
32
      * return the coefficient out of F_2
33
      */
34
      uint32_t coef(size_t i, size_t j) { return (m_elem[(i)*m_rwdcnt + (j) / 32] >> (j % 32)) & 1; }
99,912,736✔
35

36
      void set_coef_to_one(size_t i, size_t j) {
65,136,545✔
37
         m_elem[(i)*m_rwdcnt + (j) / 32] |= (static_cast<uint32_t>(1) << ((j) % 32));
65,136,545✔
38
      }
65,136,545✔
39

40
      void toggle_coeff(size_t i, size_t j) {
49,958,370✔
41
         m_elem[(i)*m_rwdcnt + (j) / 32] ^= (static_cast<uint32_t>(1) << ((j) % 32));
49,958,370✔
42
      }
49,958,370✔
43

44
      size_t rows() const { return m_rown; }
123,512✔
45

46
      size_t columns() const { return m_coln; }
100,036,150✔
47

48
      const std::vector<uint32_t>& elem() const { return m_elem; }
3,182,601✔
49

50
   private:
51
      size_t m_rown;    // number of rows.
52
      size_t m_coln;    // number of columns.
53
      size_t m_rwdcnt;  // number of words in a row
54
      std::vector<uint32_t> m_elem;
55
};
56

57
binary_matrix::binary_matrix(size_t rown, size_t coln) {
196✔
58
   m_coln = coln;
196✔
59
   m_rown = rown;
196✔
60
   m_rwdcnt = 1 + ((m_coln - 1) / 32);
196✔
61
   m_elem = std::vector<uint32_t>(m_rown * m_rwdcnt);
196✔
62
}
196✔
63

64
void binary_matrix::row_xor(size_t a, size_t b) {
65
   for(size_t i = 0; i != m_rwdcnt; i++) {
2,349,803,369✔
66
      m_elem[a * m_rwdcnt + i] ^= m_elem[b * m_rwdcnt + i];
2,334,625,715✔
67
   }
68
}
69

70
//the matrix is reduced from LSB...(from right)
71
secure_vector<size_t> binary_matrix::row_reduced_echelon_form() {
98✔
72
   secure_vector<size_t> perm(m_coln);
98✔
73
   for(size_t i = 0; i != m_coln; i++) {
162,706✔
74
      perm[i] = i;  // initialize permutation.
162,608✔
75
   }
76

77
   uint32_t failcnt = 0;
98✔
78

79
   size_t max = m_coln - 1;
98✔
80
   for(size_t i = 0; i != m_rown; i++, max--) {
39,445✔
81
      bool found_row = false;
82

83
      for(size_t j = i; !found_row && j != m_rown; j++) {
117,726✔
84
         if(coef(j, max)) {
78,379✔
85
            if(i != j)  //not needed as ith row is 0 and jth row is 1.
39,194✔
86
            {
87
               row_xor(i, j);  //xor to the row.(swap)?
78,379✔
88
            }
89

90
            found_row = true;
91
         }
92
      }
93

94
      //if no row with a 1 found then swap last column and the column with no 1 down.
95
      if(!found_row) {
39,347✔
96
         perm[m_coln - m_rown - 1 - failcnt] = static_cast<int>(max);
153✔
97
         failcnt++;
153✔
98
         if(!max) {
153✔
99
            perm.clear();
×
100
         }
101
         i--;
153✔
102
      } else {
103
         perm[i + m_coln - m_rown] = max;
39,194✔
104
         for(size_t j = i + 1; j < m_rown; j++)  //fill the column downwards with 0's
15,199,501✔
105
         {
106
            if(coef(j, max)) {
15,160,307✔
107
               row_xor(j, i);  //check the arg. order.
15,160,307✔
108
            }
109
         }
110

111
         //fill the column with 0's upwards too.
112
         for(size_t j = i; j != 0; --j) {
15,199,501✔
113
            if(coef(j - 1, max)) {
15,160,307✔
114
               row_xor(j - 1, i);
15,199,501✔
115
            }
116
         }
117
      }
118
   }  //end for(i)
119
   return perm;
98✔
120
}
121

122
void randomize_support(std::vector<gf2m>& L, RandomNumberGenerator& rng) {
98✔
123
   for(size_t i = 0; i != L.size(); ++i) {
162,706✔
124
      gf2m rnd = random_gf2m(rng);
162,608✔
125

126
      // no rejection sampling, but for useful code-based parameters with n <= 13 this seem tolerable
127
      std::swap(L[i], L[rnd % L.size()]);
162,608✔
128
   }
129
}
98✔
130

131
std::unique_ptr<binary_matrix> generate_R(
98✔
132
   std::vector<gf2m>& L, polyn_gf2m* g, const GF2m_Field& sp_field, size_t code_length, size_t t) {
133
   //L- Support
134
   //t- Number of errors
135
   //n- Length of the Goppa code
136
   //m- The extension degree of the GF
137
   //g- The generator polynomial.
138

139
   const size_t r = t * sp_field.get_extension_degree();
98✔
140

141
   binary_matrix H(r, code_length);
98✔
142

143
   for(size_t i = 0; i != code_length; i++) {
162,706✔
144
      gf2m x = g->eval(lex_to_gray(L[i]));  //evaluate the polynomial at the point L[i].
162,608✔
145
      x = sp_field.gf_inv(x);
162,608✔
146
      gf2m y = x;
147
      for(size_t j = 0; j < t; j++) {
10,755,904✔
148
         for(size_t k = 0; k < sp_field.get_extension_degree(); k++) {
140,865,840✔
149
            if(y & (1 << k)) {
130,272,544✔
150
               //the co-eff. are set in 2^0,...,2^11 ; 2^0,...,2^11 format along the rows/cols?
151
               H.set_coef_to_one(j * sp_field.get_extension_degree() + k, i);
130,272,544✔
152
            }
153
         }
154
         y = sp_field.gf_mul(y, lex_to_gray(L[i]));
21,183,184✔
155
      }
156
   }  //The H matrix is fed.
157

158
   secure_vector<size_t> perm = H.row_reduced_echelon_form();
98✔
159
   if(perm.empty()) {
98✔
160
      throw Invalid_State("McEliece keygen failed - could not bring matrix to row reduced echelon form");
×
161
   }
162

163
   auto result = std::make_unique<binary_matrix>(code_length - r, r);
98✔
164
   for(size_t i = 0; i < result->rows(); ++i) {
123,512✔
165
      for(size_t j = 0; j < result->columns(); ++j) {
100,036,150✔
166
         if(H.coef(j, perm[i])) {
99,912,736✔
167
            result->toggle_coeff(i, j);
99,912,736✔
168
         }
169
      }
170
   }
171

172
   std::vector<gf2m> Laux(code_length);
98✔
173
   for(size_t i = 0; i < code_length; ++i) {
162,706✔
174
      Laux[i] = L[perm[i]];
162,608✔
175
   }
176

177
   for(size_t i = 0; i < code_length; ++i) {
162,706✔
178
      L[i] = Laux[i];
162,608✔
179
   }
180
   return result;
98✔
181
}
294✔
182
}
183

184
McEliece_PrivateKey generate_mceliece_key(RandomNumberGenerator& rng, size_t ext_deg, size_t code_length, size_t t) {
98✔
185
   const size_t codimension = t * ext_deg;
98✔
186

187
   if(code_length <= codimension) {
98✔
188
      throw Invalid_Argument("invalid McEliece parameters");
×
189
   }
190

191
   auto sp_field = std::make_shared<GF2m_Field>(ext_deg);
98✔
192

193
   //pick the support.........
194
   std::vector<gf2m> L(code_length);
98✔
195

196
   for(size_t i = 0; i != L.size(); i++) {
162,706✔
197
      L[i] = static_cast<gf2m>(i);
162,608✔
198
   }
199
   randomize_support(L, rng);
98✔
200
   polyn_gf2m g(sp_field);  // create as zero
98✔
201

202
   bool success = false;
98✔
203
   std::unique_ptr<binary_matrix> R;
98✔
204

205
   do {
98✔
206
      // create a random irreducible polynomial
207
      g = polyn_gf2m(t, rng, sp_field);
196✔
208

209
      try {
98✔
210
         R = generate_R(L, &g, *sp_field, code_length, t);
98✔
211
         success = true;
98✔
212
      } catch(const Invalid_State&) {}
×
213
   } while(!success);
98✔
214

215
   std::vector<polyn_gf2m> sqrtmod = polyn_gf2m::sqrt_mod_init(g);
98✔
216
   std::vector<polyn_gf2m> F = syndrome_init(g, L, static_cast<int>(code_length));
98✔
217

218
   // Each F[i] is the (precomputed) syndrome of the error vector with
219
   // a single '1' in i-th position.
220
   // We do not store the F[i] as polynomials of degree t , but
221
   // as binary vectors of length ext_deg * t (this will
222
   // speed up the syndrome computation)
223
   //
224
   std::vector<uint32_t> H(bit_size_to_32bit_size(codimension) * code_length);
98✔
225
   uint32_t* sk = H.data();
98✔
226
   for(size_t i = 0; i < code_length; ++i) {
162,706✔
227
      for(size_t l = 0; l < t; ++l) {
10,755,904✔
228
         const size_t k = (l * ext_deg) / 32;
10,593,296✔
229
         const uint8_t j = (l * ext_deg) % 32;
10,593,296✔
230
         sk[k] ^= static_cast<uint32_t>(F[i].get_coef(l)) << j;
10,593,296✔
231
         if(j + ext_deg > 32) {
10,593,296✔
232
            sk[k + 1] ^= F[i].get_coef(l) >> (32 - j);
3,599,680✔
233
         }
234
      }
235
      sk += bit_size_to_32bit_size(codimension);
162,608✔
236
   }
237

238
   // We need the support L for decoding (decryption). In fact the
239
   // inverse is needed
240

241
   std::vector<gf2m> Linv(code_length);
98✔
242
   for(size_t i = 0; i != Linv.size(); ++i) {
162,706✔
243
      Linv[L[i]] = static_cast<gf2m>(i);
162,608✔
244
   }
245
   std::vector<uint8_t> pubmat(R->elem().size() * 4);
98✔
246
   for(size_t i = 0; i < R->elem().size(); i++) {
3,182,503✔
247
      store_le(R->elem()[i], &pubmat[i * 4]);
3,182,405✔
248
   }
249

250
   return McEliece_PrivateKey(g, H, sqrtmod, Linv, pubmat);
98✔
251
}
490✔
252

253
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc