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

randombit / botan / 5112871155

29 May 2023 02:26PM UTC coverage: 92.217% (+0.006%) from 92.211%
5112871155

push

github

randombit
Use combined namespace in this header

75578 of 81957 relevant lines covered (92.22%)

11942277.05 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
   if(n.is_even() && mod.is_even())
231✔
16
      return 0;
19✔
17
   Botan::BigInt u = mod, v = n;
79✔
18
   Botan::BigInt A = 1, B = 0, C = 0, D = 1;
79✔
19

20
   while(u.is_nonzero()) {
44,533✔
21
      const size_t u_zero_bits = Botan::low_zero_bits(u);
29,578✔
22
      u >>= u_zero_bits;
29,578✔
23
      for(size_t i = 0; i != u_zero_bits; ++i) {
59,191✔
24
         if(A.is_odd() || B.is_odd()) {
76,845✔
25
            A += n;
14,788✔
26
            B -= mod;
14,788✔
27
         }
28
         A >>= 1;
29,613✔
29
         B >>= 1;
29,613✔
30
      }
31

32
      const size_t v_zero_bits = Botan::low_zero_bits(v);
29,578✔
33
      v >>= v_zero_bits;
29,578✔
34
      for(size_t i = 0; i != v_zero_bits; ++i) {
58,656✔
35
         if(C.is_odd() || D.is_odd()) {
75,478✔
36
            C += n;
14,553✔
37
            D -= mod;
14,553✔
38
         }
39
         C >>= 1;
29,078✔
40
         D >>= 1;
29,078✔
41
      }
42

43
      if(u >= v) {
29,578✔
44
         u -= v;
14,876✔
45
         A -= C;
14,876✔
46
         B -= D;
14,876✔
47
      } else {
48
         v -= u;
14,702✔
49
         C -= A;
14,702✔
50
         D -= B;
14,702✔
51
      }
52
   }
53

54
   if(v != 1)
79✔
55
      return 0;  // no modular inverse
15✔
56

57
   while(D.is_negative())
114✔
58
      D += mod;
50✔
59
   while(D >= mod)
64✔
60
      D -= mod;
×
61

62
   return D;
79✔
63
}
494✔
64

65
}  // namespace
66

67
void fuzz(const uint8_t in[], size_t len) {
100✔
68
   static const size_t max_bits = 4096;
100✔
69

70
   if(len > 2 * max_bits / 8)
100✔
71
      return;
1✔
72

73
   const Botan::BigInt x = Botan::BigInt::decode(in, len / 2);
100✔
74
   Botan::BigInt mod = Botan::BigInt::decode(in + len / 2, len - len / 2);
100✔
75

76
   if(mod < 2)
100✔
77
      return;
1✔
78

79
   const Botan::BigInt lib = Botan::inverse_mod(x, mod);
99✔
80
   const Botan::BigInt ref = ref_inverse_mod(x, mod);
99✔
81

82
   if(ref != lib) {
99✔
83
      FUZZER_WRITE_AND_CRASH("X = " << x << "\n"
99✔
84
                                    << "Mod = " << mod << "\n"
85
                                    << "GCD(X,Mod) = " << gcd(x, mod) << "\n"
86
                                    << "RefInv(X,Mod) = " << ref << "\n"
87
                                    << "LibInv(X,Mod)  = " << lib << "\n"
88
                                    << "RefCheck = " << (x * ref) % mod << "\n"
89
                                    << "LibCheck  = " << (x * lib) % mod << "\n");
90
   }
91
}
369✔
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