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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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) {
297,413✔
19
   set_sign(Positive);
297,413✔
20

21
   if(bitsize == 0) {
297,413✔
22
      clear();
×
23
   } else {
24
      secure_vector<uint8_t> array = rng.random_vec(round_up(bitsize, 8) / 8);
542,080✔
25

26
      // Always cut unwanted bits
27
      if(bitsize % 8) {
297,412✔
28
         array[0] &= 0xFF >> (8 - (bitsize % 8));
244,666✔
29
      }
30

31
      // Set the highest bit if wanted
32
      if(set_high_bit) {
297,412✔
33
         array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0);
19,880✔
34
      }
35

36
      binary_decode(array);
594,824✔
37
   }
297,412✔
38
}
297,412✔
39

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

48
   BigInt r;
155,362✔
49

50
   const size_t bits = max.bits();
155,362✔
51

52
   do {
232,955✔
53
      r.randomize(rng, bits, false);
232,955✔
54
   } while(r < min || r >= max);
465,908✔
55

56
   return r;
155,362✔
57
}
×
58

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