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

randombit / botan / 21780140538

07 Feb 2026 12:31PM UTC coverage: 90.065% (-0.009%) from 90.074%
21780140538

push

github

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

Further misc header reductions, forward declarations, etc

102236 of 113513 relevant lines covered (90.07%)

11421023.02 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) {
498,616✔
20
   set_sign(Positive);
498,616✔
21

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

27
      // Always cut unwanted bits
28
      if(bitsize % 8 > 0) {
498,615✔
29
         array[0] &= 0xFF >> (8 - (bitsize % 8));
373,883✔
30
      }
31

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

37
      assign_from_bytes(array);
498,615✔
38
   }
498,615✔
39
}
498,615✔
40

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

63
   BOTAN_DEBUG_ASSERT(min <= 1);
221,593✔
64

65
   const size_t bits = max.bits();
221,593✔
66

67
   for(;;) {
84,564✔
68
      BigInt r;
306,157✔
69
      r.randomize(rng, bits, false);
306,157✔
70
      if(r >= min && r < max) {
612,314✔
71
         return r;
221,593✔
72
      }
73
   }
306,157✔
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