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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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) {
304,287✔
19
   set_sign(Positive);
304,287✔
20

21
   if(bitsize == 0) {
304,287✔
22
      clear();
×
23
   } else {
24
      secure_vector<uint8_t> array = rng.random_vec(round_up(bitsize, 8) / 8);
548,944✔
25

26
      // Always cut unwanted bits
27
      if(bitsize % 8)
304,286✔
28
         array[0] &= 0xFF >> (8 - (bitsize % 8));
244,656✔
29

30
      // Set the highest bit if wanted
31
      if(set_high_bit)
304,286✔
32
         array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0);
21,512✔
33

34
      binary_decode(array);
608,572✔
35
   }
304,286✔
36
}
304,286✔
37

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

45
   BigInt r;
160,618✔
46

47
   const size_t bits = max.bits();
160,618✔
48

49
   do {
238,037✔
50
      r.randomize(rng, bits, false);
238,037✔
51
   } while(r < min || r >= max);
476,068✔
52

53
   return r;
160,618✔
54
}
×
55

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