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

randombit / botan / 21782759586

07 Feb 2026 12:31PM UTC coverage: 90.073% (-0.001%) from 90.074%
21782759586

push

github

web-flow
Merge pull request #5289 from randombit/jack/header-patrol-2

Further misc header reductions, forward declarations, etc

102244 of 113513 relevant lines covered (90.07%)

11458499.72 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) {
500,892✔
20
   set_sign(Positive);
500,892✔
21

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

27
      // Always cut unwanted bits
28
      if(bitsize % 8 > 0) {
500,891✔
29
         array[0] &= 0xFF >> (8 - (bitsize % 8));
374,684✔
30
      }
31

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

37
      assign_from_bytes(array);
500,891✔
38
   }
500,891✔
39
}
500,891✔
40

41
/*
42
* Generate a random integer within given range
43
*/
44
BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) {
439,427✔
45
   if(min.is_negative() || max.is_negative() || max <= min) {
878,854✔
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) {
439,427✔
58
      const BigInt diff = max - min;
216,394✔
59
      // This call is recursive, but will not recurse further
60
      return min + BigInt::random_integer(rng, BigInt::zero(), diff);
216,394✔
61
   }
216,394✔
62

63
   BOTAN_DEBUG_ASSERT(min <= 1);
223,033✔
64

65
   const size_t bits = max.bits();
223,033✔
66

67
   for(;;) {
85,270✔
68
      BigInt r;
308,303✔
69
      r.randomize(rng, bits, false);
308,303✔
70
      if(r >= min && r < max) {
616,606✔
71
         return r;
223,033✔
72
      }
73
   }
308,303✔
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