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

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

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

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

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

92.59
/src/lib/math/bigint/big_rand.cpp
1
/*
2
* BigInt Random Generation
3
* (C) 1999-2007 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/rng.h>
12
#include <botan/internal/rounding.h>
13

14
namespace Botan {
15

16
/*
17
* Randomize this number
18
*/
19
void BigInt::randomize(RandomNumberGenerator& rng, size_t bitsize, bool set_high_bit) {
497,468✔
20
   set_sign(Positive);
497,468✔
21

22
   if(bitsize == 0) {
497,468✔
23
      clear();
×
24
   } else {
25
      secure_vector<uint8_t> array = rng.random_vec(round_up(bitsize, 8) / 8);
497,468✔
26

27
      // Always cut unwanted bits
28
      if(bitsize % 8 > 0) {
497,467✔
29
         array[0] &= 0xFF >> (8 - (bitsize % 8));
375,494✔
30
      }
31

32
      // Set the highest bit if wanted
33
      if(set_high_bit) {
497,467✔
34
         array[0] |= 0x80 >> ((bitsize % 8) > 0 ? (8 - bitsize % 8) : 0);
25,421✔
35
      }
36

37
      assign_from_bytes(array);
497,467✔
38
   }
497,467✔
39
}
497,467✔
40

41
/*
42
* Generate a random integer within given range
43
*/
44
BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) {
434,155✔
45
   if(min.is_negative() || max.is_negative() || max <= min) {
868,310✔
46
      throw Invalid_Argument("BigInt::random_integer invalid range");
×
47
   }
48

49
   /*
50
   If min is > 1 then we generate a random number `r` in [0,max-min)
51
   and return min + r.
52

53
   This same logic could also be reasonably chosen for min == 1, but
54
   that breaks certain tests which expect stability of this function
55
   when generating within [1,n)
56
   */
57
   if(min > 1) {
434,155✔
58
      const BigInt diff = max - min;
213,759✔
59
      // This call is recursive, but will not recurse further
60
      return min + BigInt::random_integer(rng, BigInt::zero(), diff);
213,759✔
61
   }
213,759✔
62

63
   BOTAN_DEBUG_ASSERT(min <= 1);
220,396✔
64

65
   const size_t bits = max.bits();
220,396✔
66

67
   for(;;) {
84,579✔
68
      BigInt r;
304,975✔
69
      r.randomize(rng, bits, false);
304,975✔
70
      if(r >= min && r < max) {
609,950✔
71
         return r;
220,396✔
72
      }
73
   }
304,975✔
74
}
75

76
}  // 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