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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

86.36
/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/rng.h>
11
#include <botan/internal/rounding.h>
12

13
namespace Botan {
14

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

21
   if(bitsize == 0) {
300,208✔
22
      clear();
×
23
   } else {
24
      secure_vector<uint8_t> array = rng.random_vec(round_up(bitsize, 8) / 8);
543,952✔
25

26
      // Always cut unwanted bits
27
      if(bitsize % 8)
300,207✔
28
         array[0] &= 0xFF >> (8 - (bitsize % 8));
243,743✔
29

30
      // Set the highest bit if wanted
31
      if(set_high_bit)
300,207✔
32
         array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0);
22,066✔
33

34
      binary_decode(array);
600,414✔
35
   }
300,207✔
36
}
300,207✔
37

38
/*
39
* Generate a random integer within given range
40
*/
41
BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) {
156,477✔
42
   if(min.is_negative() || max.is_negative() || max <= min)
312,954✔
43
      throw Invalid_Argument("BigInt::random_integer invalid range");
×
44

45
   BigInt r;
156,477✔
46

47
   const size_t bits = max.bits();
156,477✔
48

49
   do {
233,724✔
50
      r.randomize(rng, bits, false);
233,724✔
51
   } while(r < min || r >= max);
467,445✔
52

53
   return r;
156,477✔
54
}
×
55

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