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

randombit / botan / 5356685938

23 Jun 2023 01:39PM UTC coverage: 91.746% (+0.02%) from 91.728%
5356685938

push

github

randombit
Merge GH #3595 Apply clang-tidy more universally

Previously clang-tidy ruleset disabled certain rules for the cli and
tests. Remove these exceptions, and fix the relevant warnings.

Also fix compile_commands.json which had previously not provided information for
the examples, BoGo shim, or fuzzers. As a result, clang-tidy was effectively
blind to them. Fix various clang-tidy findings in these files.

Additionally fix clang-tidy warnings that were in Boost or Sqlite3 specific
code, which had been accidentally omitted in past checks.

Modify the nightly clang-tidy run to additionally check the examples, fuzzers,
shim, and Sqlite3/Boost specific code.

78183 of 85217 relevant lines covered (91.75%)

12364366.11 hits per line

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

98.08
/src/fuzzer/invert.cpp
1
/*
2
* (C) 2015,2016,2020 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
#include "fuzzers.h"
7

8
#include <botan/numthry.h>
9

10
namespace {
11

12
Botan::BigInt ref_inverse_mod(const Botan::BigInt& n, const Botan::BigInt& mod) {
99✔
13
   if(n == 0 || mod < 2) {
197✔
14
      return 0;
1✔
15
   }
16
   if(n.is_even() && mod.is_even()) {
251✔
17
      return 0;
28✔
18
   }
19
   Botan::BigInt u = mod, v = n;
70✔
20
   Botan::BigInt A = 1, B = 0, C = 0, D = 1;
70✔
21

22
   while(u.is_nonzero()) {
40,713✔
23
      const size_t u_zero_bits = Botan::low_zero_bits(u);
27,025✔
24
      u >>= u_zero_bits;
27,025✔
25
      for(size_t i = 0; i != u_zero_bits; ++i) {
54,154✔
26
         if(A.is_odd() || B.is_odd()) {
72,773✔
27
            A += n;
13,664✔
28
            B -= mod;
13,664✔
29
         }
30
         A >>= 1;
27,129✔
31
         B >>= 1;
27,129✔
32
      }
33

34
      const size_t v_zero_bits = Botan::low_zero_bits(v);
27,025✔
35
      v >>= v_zero_bits;
27,025✔
36
      for(size_t i = 0; i != v_zero_bits; ++i) {
53,736✔
37
         if(C.is_odd() || D.is_odd()) {
71,807✔
38
            C += n;
13,375✔
39
            D -= mod;
13,375✔
40
         }
41
         C >>= 1;
26,711✔
42
         D >>= 1;
26,711✔
43
      }
44

45
      if(u >= v) {
27,025✔
46
         u -= v;
13,618✔
47
         A -= C;
13,618✔
48
         B -= D;
13,618✔
49
      } else {
50
         v -= u;
13,407✔
51
         C -= A;
13,407✔
52
         D -= B;
13,407✔
53
      }
54
   }
55

56
   if(v != 1) {
70✔
57
      return 0;  // no modular inverse
13✔
58
   }
59

60
   while(D.is_negative()) {
103✔
61
      D += mod;
46✔
62
   }
63
   while(D >= mod) {
57✔
64
      D -= mod;
×
65
   }
66

67
   return D;
70✔
68
}
449✔
69

70
}  // namespace
71

72
void fuzz(const uint8_t in[], size_t len) {
100✔
73
   static const size_t max_bits = 4096;
100✔
74

75
   if(len > 2 * max_bits / 8) {
100✔
76
      return;
1✔
77
   }
78

79
   const Botan::BigInt x = Botan::BigInt::decode(in, len / 2);
100✔
80
   Botan::BigInt mod = Botan::BigInt::decode(in + len / 2, len - len / 2);
100✔
81

82
   if(mod < 2) {
100✔
83
      return;
1✔
84
   }
85

86
   const Botan::BigInt lib = Botan::inverse_mod(x, mod);
99✔
87
   const Botan::BigInt ref = ref_inverse_mod(x, mod);
99✔
88

89
   if(ref != lib) {
99✔
90
      FUZZER_WRITE_AND_CRASH("X = " << x << "\n"
99✔
91
                                    << "Mod = " << mod << "\n"
92
                                    << "GCD(X,Mod) = " << gcd(x, mod) << "\n"
93
                                    << "RefInv(X,Mod) = " << ref << "\n"
94
                                    << "LibInv(X,Mod)  = " << lib << "\n"
95
                                    << "RefCheck = " << (x * ref) % mod << "\n"
96
                                    << "LibCheck  = " << (x * lib) % mod << "\n");
97
   }
98
}
365✔
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

© 2025 Coveralls, Inc