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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 hits per line

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

90.85
/src/lib/math/bigint/bigint.cpp
1
/*
2
* BigInt Base
3
* (C) 1999-2011,2012,2014,2019 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/bigint.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/ct_utils.h>
13
#include <botan/internal/loadstor.h>
14
#include <botan/internal/mem_utils.h>
15
#include <botan/internal/mp_core.h>
16
#include <botan/internal/rounding.h>
17

18
namespace Botan {
19

20
BigInt::BigInt(uint64_t n) {
361,914✔
21
   if constexpr(sizeof(word) == 8) {
361,914✔
22
      m_data.set_word_at(0, static_cast<word>(n));
361,914✔
23
   } else {
24
      m_data.set_word_at(1, static_cast<word>(n >> 32));
25
      m_data.set_word_at(0, static_cast<word>(n));
26
   }
27
}
361,914✔
28

29
//static
30
BigInt BigInt::from_u64(uint64_t n) {
182,853✔
31
   return BigInt(n);
182,853✔
32
}
33

34
//static
35
BigInt BigInt::from_word(word n) {
461,521✔
36
   BigInt bn;
461,521✔
37
   bn.set_word_at(0, n);
461,521✔
38
   return bn;
461,521✔
39
}
×
40

41
//static
42
BigInt BigInt::from_s32(int32_t n) {
70,848✔
43
   if(n >= 0) {
70,848✔
44
      return BigInt::from_u64(static_cast<uint64_t>(n));
70,696✔
45
   } else {
46
      return -BigInt::from_u64(static_cast<uint64_t>(-n));
152✔
47
   }
48
}
49

50
//static
51
BigInt BigInt::with_capacity(size_t size) {
8,596,430✔
52
   BigInt bn;
8,596,430✔
53
   bn.grow_to(size);
8,596,430✔
54
   return bn;
8,596,430✔
55
}
×
56

57
/*
58
* Construct a BigInt from a string
59
*/
60
BigInt::BigInt(std::string_view str) {
46,607✔
61
   Base base = Decimal;
46,607✔
62
   size_t markers = 0;
46,607✔
63
   bool negative = false;
46,607✔
64

65
   if(!str.empty() && str[0] == '-') {
46,607✔
66
      markers += 1;
67
      negative = true;
68
   }
69

70
   if(str.length() > markers + 2 && str[markers] == '0' && str[markers + 1] == 'x') {
46,607✔
71
      markers += 2;
72
      base = Hexadecimal;
73
   }
74

75
   *this = decode(as_span_of_bytes(str).subspan(markers), base);
46,607✔
76

77
   if(negative) {
46,607✔
78
      set_sign(Negative);
904✔
79
   } else {
80
      set_sign(Positive);
46,155✔
81
   }
82
}
46,607✔
83

84
BigInt BigInt::from_string(std::string_view str) {
6✔
85
   return BigInt(str);
6✔
86
}
87

88
BigInt BigInt::from_bytes(std::span<const uint8_t> input) {
126,265✔
89
   BigInt r;
126,265✔
90
   r.assign_from_bytes(input);
126,265✔
91
   return r;
126,265✔
92
}
×
93

94
/*
95
* Construct a BigInt from an encoded BigInt
96
*/
97
BigInt::BigInt(const uint8_t input[], size_t length, Base base) {
×
98
   *this = decode(input, length, base);
×
99
}
×
100

101
//static
102
BigInt BigInt::from_bytes_with_max_bits(const uint8_t input[], size_t length, size_t max_bits) {
6,905✔
103
   const size_t input_bits = 8 * length;
6,905✔
104

105
   auto bn = BigInt::from_bytes(std::span{input, length});
6,905✔
106

107
   if(input_bits > max_bits) {
6,905✔
108
      const size_t bits_to_shift = input_bits - max_bits;
3,731✔
109

110
      bn >>= bits_to_shift;
3,731✔
111
   }
112

113
   return bn;
6,905✔
114
}
×
115

116
/*
117
* Construct a BigInt from an encoded BigInt
118
*/
119
BigInt::BigInt(RandomNumberGenerator& rng, size_t bits, bool set_high_bit) {
88,562✔
120
   randomize(rng, bits, set_high_bit);
88,562✔
121
}
88,562✔
122

123
uint8_t BigInt::byte_at(size_t n) const {
346,238✔
124
   return get_byte_var(sizeof(word) - (n % sizeof(word)) - 1, word_at(n / sizeof(word)));
346,238✔
125
}
126

127
int32_t BigInt::cmp_word(word other) const {
3,150,934✔
128
   if(is_negative()) {
3,150,934✔
129
      return -1;  // other is positive ...
130
   }
131

132
   const size_t sw = this->sig_words();
3,085,436✔
133
   if(sw > 1) {
3,085,436✔
134
      return 1;  // must be larger since other is just one word ...
135
   }
136

137
   return bigint_cmp(this->_data(), sw, &other, 1);
2,402,463✔
138
}
139

140
/*
141
* Comparison Function
142
*/
143
int32_t BigInt::cmp(const BigInt& other, bool check_signs) const {
1,010,974✔
144
   if(check_signs) {
1,010,974✔
145
      if(other.is_positive() && this->is_negative()) {
1,010,847✔
146
         return -1;
147
      }
148

149
      if(other.is_negative() && this->is_positive()) {
1,010,789✔
150
         return 1;
151
      }
152

153
      if(other.is_negative() && this->is_negative()) {
1,010,703✔
154
         return (-bigint_cmp(this->_data(), this->size(), other._data(), other.size()));
66✔
155
      }
156
   }
157

158
   return bigint_cmp(this->_data(), this->size(), other._data(), other.size());
1,010,764✔
159
}
160

161
bool BigInt::is_equal(const BigInt& other) const {
586,409✔
162
   if(this->sign() != other.sign()) {
586,409✔
163
      return false;
164
   }
165

166
   return bigint_ct_is_eq(this->_data(), this->sig_words(), other._data(), other.sig_words()).as_bool();
1,100,003✔
167
}
168

169
bool BigInt::is_less_than(const BigInt& other) const {
1,523,312✔
170
   if(this->is_negative() && other.is_positive()) {
1,523,312✔
171
      return true;
172
   }
173

174
   if(this->is_positive() && other.is_negative()) {
1,523,239✔
175
      return false;
176
   }
177

178
   if(other.is_negative() && this->is_negative()) {
1,523,169✔
179
      return bigint_ct_is_lt(other._data(), other.sig_words(), this->_data(), this->sig_words()).as_bool();
65✔
180
   }
181

182
   return bigint_ct_is_lt(this->_data(), this->sig_words(), other._data(), other.sig_words()).as_bool();
2,282,297✔
183
}
184

185
void BigInt::encode_words(word out[], size_t size) const {
283,534✔
186
   const size_t words = sig_words();
283,534✔
187

188
   if(words > size) {
283,534✔
189
      throw Encoding_Error("BigInt::encode_words value too large to encode");
×
190
   }
191

192
   clear_mem(out, size);
283,534✔
193
   copy_mem(out, _data(), words);
283,534✔
194
}
283,534✔
195

196
void BigInt::Data::set_to_zero() {
884,808✔
197
   m_reg.resize(m_reg.capacity());
884,808✔
198
   clear_mem(m_reg.data(), m_reg.size());
884,808✔
199
   m_sig_words = 0;
884,808✔
200
}
884,808✔
201

202
void BigInt::Data::mask_bits(size_t n) {
36,371✔
203
   if(n == 0) {
36,371✔
204
      return set_to_zero();
×
205
   }
206

207
   const size_t top_word = n / WordInfo<word>::bits;
36,371✔
208

209
   if(top_word < size()) {
36,371✔
210
      const word mask = (static_cast<word>(1) << (n % WordInfo<word>::bits)) - 1;
34,321✔
211
      const size_t len = size() - (top_word + 1);
34,321✔
212
      if(len > 0) {
34,321✔
213
         clear_mem(&m_reg[top_word + 1], len);
34,321✔
214
      }
215
      m_reg[top_word] &= mask;
34,321✔
216
      invalidate_sig_words();
34,321✔
217
   }
218
}
219

220
size_t BigInt::Data::calc_sig_words() const {
42,515,273✔
221
   const size_t sz = m_reg.size();
42,515,273✔
222
   size_t sig = sz;
42,515,273✔
223

224
   word sub = 1;
42,515,273✔
225

226
   for(size_t i = 0; i != sz; ++i) {
1,144,617,880✔
227
      const word w = m_reg[sz - i - 1];
1,102,102,607✔
228
      sub &= ct_is_zero(w);
1,102,102,607✔
229
      sig -= sub;
1,102,102,607✔
230
   }
231

232
   /*
233
   * This depends on the data so is poisoned, but unpoison it here as
234
   * later conditionals are made on the size.
235
   */
236
   CT::unpoison(sig);
42,515,273✔
237

238
   return sig;
42,515,273✔
239
}
240

241
/*
242
* Return bits {offset...offset+length}
243
*/
244
uint32_t BigInt::get_substring(size_t offset, size_t length) const {
11,474,948✔
245
   if(length == 0 || length > 32) {
11,474,948✔
246
      throw Invalid_Argument("BigInt::get_substring invalid substring length");
×
247
   }
248

249
   const uint32_t mask = 0xFFFFFFFF >> (32 - length);
11,474,948✔
250

251
   const size_t word_offset = offset / WordInfo<word>::bits;
11,474,948✔
252
   const size_t wshift = (offset % WordInfo<word>::bits);
11,474,948✔
253

254
   /*
255
   * The substring is contained within one or at most two words. The
256
   * offset and length are not secret, so we can perform conditional
257
   * operations on those values.
258
   */
259
   const word w0 = word_at(word_offset);
11,474,948✔
260

261
   if(wshift == 0 || (offset + length) / WordInfo<word>::bits == word_offset) {
11,474,948✔
262
      return static_cast<uint32_t>(w0 >> wshift) & mask;
10,918,951✔
263
   } else {
264
      const word w1 = word_at(word_offset + 1);
555,997✔
265
      return static_cast<uint32_t>((w0 >> wshift) | (w1 << (WordInfo<word>::bits - wshift))) & mask;
555,997✔
266
   }
267
}
268

269
/*
270
* Convert this number to a uint32_t, if possible
271
*/
272
uint32_t BigInt::to_u32bit() const {
34,952✔
273
   if(is_negative()) {
34,952✔
274
      throw Encoding_Error("BigInt::to_u32bit: Number is negative");
×
275
   }
276
   if(bits() > 32) {
34,952✔
277
      throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
15✔
278
   }
279

280
   uint32_t out = 0;
281
   for(size_t i = 0; i != 4; ++i) {
174,685✔
282
      out = (out << 8) | byte_at(3 - i);
139,748✔
283
   }
284
   return out;
34,937✔
285
}
286

287
/*
288
* Clear bit number n
289
*/
290
void BigInt::clear_bit(size_t n) {
71✔
291
   const size_t which = n / WordInfo<word>::bits;
71✔
292

293
   if(which < size()) {
71✔
294
      const word mask = ~(static_cast<word>(1) << (n % WordInfo<word>::bits));
71✔
295
      m_data.set_word_at(which, word_at(which) & mask);
71✔
296
   }
297
}
71✔
298

299
size_t BigInt::bytes() const {
356,951✔
300
   return round_up(bits(), 8) / 8;
356,951✔
301
}
302

303
size_t BigInt::top_bits_free() const {
2,120,110✔
304
   const size_t words = sig_words();
2,120,110✔
305

306
   const word top_word = word_at(words - 1);
2,120,110✔
307
   const size_t bits_used = high_bit(CT::value_barrier(top_word));
2,120,110✔
308
   CT::unpoison(bits_used);
2,120,110✔
309
   return WordInfo<word>::bits - bits_used;
2,120,110✔
310
}
311

312
size_t BigInt::bits() const {
1,698,959✔
313
   const size_t words = sig_words();
1,698,959✔
314

315
   if(words == 0) {
1,698,959✔
316
      return 0;
317
   }
318

319
   const size_t full_words = (words - 1) * WordInfo<word>::bits;
1,679,577✔
320
   const size_t top_bits = WordInfo<word>::bits - top_bits_free();
1,679,577✔
321

322
   return full_words + top_bits;
1,679,577✔
323
}
324

325
/*
326
* Return the negation of this number
327
*/
328
BigInt BigInt::operator-() const {
163✔
329
   BigInt x = (*this);
163✔
330
   x.flip_sign();
163✔
331
   return x;
163✔
332
}
×
333

334
size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws) {
7,341,781✔
335
   if(p.is_negative() || this->is_negative()) {
7,341,781✔
336
      throw Invalid_Argument("BigInt::reduce_below both values must be positive");
×
337
   }
338

339
   const size_t p_words = p.sig_words();
7,341,781✔
340

341
   if(size() < p_words + 1) {
7,341,781✔
342
      grow_to(p_words + 1);
28,974✔
343
   }
344

345
   if(ws.size() < p_words + 1) {
7,341,781✔
346
      ws.resize(p_words + 1);
440,533✔
347
   }
348

349
   clear_mem(ws.data(), ws.size());
7,341,781✔
350

351
   size_t reductions = 0;
7,341,781✔
352

353
   for(;;) {
11,222,529✔
354
      const word borrow = bigint_sub3(ws.data(), _data(), p_words + 1, p._data(), p_words);
18,564,310✔
355
      if(borrow > 0) {
18,564,310✔
356
         break;
357
      }
358

359
      ++reductions;
11,222,529✔
360
      swap_reg(ws);
11,222,529✔
361
   }
362

363
   return reductions;
7,341,781✔
364
}
365

366
void BigInt::ct_reduce_below(const BigInt& mod, secure_vector<word>& ws, size_t bound) {
×
367
   if(mod.is_negative() || this->is_negative()) {
×
368
      throw Invalid_Argument("BigInt::ct_reduce_below both values must be positive");
×
369
   }
370

371
   const size_t mod_words = mod.sig_words();
×
372

373
   grow_to(mod_words);
×
374

375
   const size_t sz = size();
×
376

377
   ws.resize(sz);
×
378

379
   clear_mem(ws.data(), sz);
×
380

381
   for(size_t i = 0; i != bound; ++i) {
×
382
      const word borrow = bigint_sub3(ws.data(), _data(), sz, mod._data(), mod_words);
×
383

384
      CT::Mask<word>::is_zero(borrow).select_n(mutable_data(), ws.data(), _data(), sz);
×
385
   }
386
}
×
387

388
/*
389
* Return the absolute value of this number
390
*/
391
BigInt BigInt::abs() const {
867✔
392
   BigInt x = (*this);
867✔
393
   x.set_sign(Positive);
867✔
394
   return x;
867✔
395
}
396

397
/*
398
* Encode this number into bytes
399
*/
400
void BigInt::serialize_to(std::span<uint8_t> output) const {
216,888✔
401
   BOTAN_ARG_CHECK(this->bytes() <= output.size(), "Insufficient output space");
216,888✔
402

403
   this->binary_encode(output.data(), output.size());
216,660✔
404
}
216,660✔
405

406
/*
407
* Encode this number into bytes
408
*/
409
void BigInt::binary_encode(uint8_t output[], size_t len) const {
216,665✔
410
   const size_t full_words = len / sizeof(word);
216,665✔
411
   const size_t extra_bytes = len % sizeof(word);
216,665✔
412

413
   for(size_t i = 0; i != full_words; ++i) {
1,976,667✔
414
      const word w = word_at(i);
1,760,002✔
415
      store_be(w, output + (len - (i + 1) * sizeof(word)));
1,760,002✔
416
   }
417

418
   if(extra_bytes > 0) {
216,665✔
419
      const word w = word_at(full_words);
125,187✔
420

421
      for(size_t i = 0; i != extra_bytes; ++i) {
571,555✔
422
         output[extra_bytes - i - 1] = get_byte_var(sizeof(word) - i - 1, w);
446,368✔
423
      }
424
   }
425
}
216,665✔
426

427
/*
428
* Set this number to the value in buf
429
*/
430
void BigInt::assign_from_bytes(std::span<const uint8_t> bytes) {
882,097✔
431
   clear();
882,097✔
432

433
   const size_t length = bytes.size();
882,097✔
434
   const size_t full_words = length / sizeof(word);
882,097✔
435
   const size_t extra_bytes = length % sizeof(word);
882,097✔
436

437
   secure_vector<word> reg((round_up(full_words + (extra_bytes > 0 ? 1 : 0), 8)));
1,108,305✔
438

439
   for(size_t i = 0; i != full_words; ++i) {
8,868,804✔
440
      reg[i] = load_be<word>(bytes.last<sizeof(word)>());
7,986,707✔
441
      bytes = bytes.first(bytes.size() - sizeof(word));
7,986,707✔
442
   }
443

444
   if(!bytes.empty()) {
882,097✔
445
      BOTAN_ASSERT_NOMSG(extra_bytes == bytes.size());
655,889✔
446
      std::array<uint8_t, sizeof(word)> last_partial_word = {0};
655,889✔
447
      copy_mem(std::span{last_partial_word}.last(extra_bytes), bytes);
655,889✔
448
      reg[full_words] = load_be<word>(last_partial_word);
655,889✔
449
   }
450

451
   m_data.swap(reg);
882,097✔
452
}
882,097✔
453

454
void BigInt::ct_cond_add(bool predicate, const BigInt& value) {
2,353,577✔
455
   if(this->is_negative() || value.is_negative()) {
2,353,577✔
456
      throw Invalid_Argument("BigInt::ct_cond_add requires both values to be positive");
×
457
   }
458
   const size_t v_words = value.sig_words();
2,353,577✔
459

460
   this->grow_to(1 + v_words);
2,353,577✔
461

462
   const auto mask = CT::Mask<word>::expand(static_cast<word>(predicate)).value();
2,353,577✔
463

464
   word carry = 0;
2,353,577✔
465

466
   word* x = this->mutable_data();
2,353,577✔
467
   const word* y = value._data();
2,353,577✔
468

469
   for(size_t i = 0; i != v_words; ++i) {
11,379,126✔
470
      x[i] = word_add(x[i], y[i] & mask, &carry);
9,025,549✔
471
   }
472

473
   for(size_t i = v_words; i != size(); ++i) {
20,794,809✔
474
      x[i] = word_add(x[i], static_cast<word>(0), &carry);
18,441,232✔
475
   }
476
}
2,353,577✔
477

478
void BigInt::ct_shift_left(size_t shift) {
30,665✔
479
   auto shl_bit = [](const BigInt& a, BigInt& result) {
3,830,336✔
480
      BOTAN_DEBUG_ASSERT(a.size() + 1 == result.size());
3,799,671✔
481
      bigint_shl2(result.mutable_data(), a._data(), a.size(), 1);
3,799,671✔
482
      // shl2 may have shifted a bit into the next word, which must be dropped
483
      clear_mem(result.mutable_data() + result.size() - 1, 1);
3,799,671✔
484
   };
3,799,671✔
485

486
   auto shl_word = [](const BigInt& a, BigInt& result) {
3,830,336✔
487
      // the most significant word is not copied, aka. shifted out
488
      bigint_shl2(result.mutable_data(), a._data(), a.size() - 1 /* ignore msw */, WordInfo<word>::bits);
3,799,671✔
489
      // we left-shifted by a full word, the least significant word must be zero'ed
490
      clear_mem(result.mutable_data(), 1);
3,799,671✔
491
   };
3,799,671✔
492

493
   BOTAN_ASSERT_NOMSG(size() > 0);
30,665✔
494

495
   constexpr size_t bits_in_word = sizeof(word) * 8;
30,665✔
496
   const size_t word_shift = shift >> ceil_log2(bits_in_word);             // shift / bits_in_word
30,665✔
497
   const size_t bit_shift = shift & ((1 << ceil_log2(bits_in_word)) - 1);  // shift % bits_in_word
30,665✔
498
   const size_t iterations = std::max(size(), bits_in_word) - 1;           // uint64_t i; i << 64 is undefined behaviour
30,665✔
499

500
   // In every iteration, shift one bit and one word to the left and use the
501
   // shift results only when they are within the shift range.
502
   BigInt tmp;
30,665✔
503
   tmp.resize(size() + 1 /* to hold the shifted-out word */);
30,665✔
504
   for(size_t i = 0; i < iterations; ++i) {
3,830,336✔
505
      shl_bit(*this, tmp);
3,799,671✔
506
      ct_cond_assign(i < bit_shift, tmp);
3,799,671✔
507
      shl_word(*this, tmp);
3,799,671✔
508
      ct_cond_assign(i < word_shift, tmp);
3,799,671✔
509
   }
510
}
30,665✔
511

512
void BigInt::ct_cond_swap(bool predicate, BigInt& other) {
11,240,215✔
513
   const size_t max_words = std::max(size(), other.size());
11,240,215✔
514
   grow_to(max_words);
11,240,215✔
515
   other.grow_to(max_words);
11,240,215✔
516

517
   bigint_cnd_swap(static_cast<word>(predicate), this->mutable_data(), other.mutable_data(), max_words);
11,240,215✔
518
}
11,240,215✔
519

520
void BigInt::cond_flip_sign(bool predicate) {
32,093,875✔
521
   // This code is assuming Negative == 0, Positive == 1
522

523
   const auto mask = CT::Mask<uint8_t>::expand_bool(predicate);
32,093,875✔
524

525
   const uint8_t current_sign = static_cast<uint8_t>(sign());
32,093,875✔
526

527
   const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
32,093,875✔
528

529
   set_sign(static_cast<Sign>(new_sign));
32,093,875✔
530
}
32,093,875✔
531

532
void BigInt::ct_cond_assign(bool predicate, const BigInt& other) {
31,760,326✔
533
   const size_t t_words = size();
31,760,326✔
534
   const size_t o_words = other.size();
31,760,326✔
535

536
   if(o_words < t_words) {
31,760,326✔
537
      grow_to(o_words);
365✔
538
   }
539

540
   const size_t r_words = std::max(t_words, o_words);
31,760,326✔
541

542
   const auto mask = CT::Mask<word>::expand_bool(predicate);
31,760,326✔
543

544
   for(size_t i = 0; i != r_words; ++i) {
2,147,483,647✔
545
      const word o_word = other.word_at(i);
2,147,483,647✔
546
      const word t_word = this->word_at(i);
2,147,483,647✔
547
      this->set_word_at(i, mask.select(o_word, t_word));
2,147,483,647✔
548
   }
549

550
   const auto same_sign = CT::Mask<word>::is_equal(sign(), other.sign()).as_choice();
31,760,326✔
551
   cond_flip_sign((mask.as_choice() && !same_sign).as_bool());
31,760,326✔
552
}
31,760,326✔
553

554
void BigInt::_const_time_poison() const {
57,366✔
555
   CT::poison(m_data.const_data(), m_data.size());
57,366✔
556
}
57,366✔
557

558
void BigInt::_const_time_unpoison() const {
24,483,808✔
559
   CT::unpoison(m_data.const_data(), m_data.size());
24,483,808✔
560
}
24,483,808✔
561

562
}  // namespace Botan
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

© 2026 Coveralls, Inc