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

randombit / botan / 13139534998

04 Feb 2025 02:46PM UTC coverage: 91.235% (-0.001%) from 91.236%
13139534998

push

github

web-flow
Merge pull request #4633 from Rohde-Schwarz/fix/unreachable_warning

FIX: unreachable code if XMD is not available

94189 of 103238 relevant lines covered (91.23%)

11530574.35 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()) {
249✔
17
      return 0;
31✔
18
   }
19
   Botan::BigInt u = mod, v = n;
67✔
20
   Botan::BigInt A = 1, B = 0, C = 0, D = 1;
67✔
21

22
   while(u.is_nonzero()) {
36,567✔
23
      const size_t u_zero_bits = Botan::low_zero_bits(u);
24,328✔
24
      u >>= u_zero_bits;
24,328✔
25
      for(size_t i = 0; i != u_zero_bits; ++i) {
48,928✔
26
         if(A.is_odd() || B.is_odd()) {
24,600✔
27
            A += n;
12,196✔
28
            B -= mod;
12,196✔
29
         }
30
         A >>= 1;
24,600✔
31
         B >>= 1;
24,600✔
32
      }
33

34
      const size_t v_zero_bits = Botan::low_zero_bits(v);
24,328✔
35
      v >>= v_zero_bits;
24,328✔
36
      for(size_t i = 0; i != v_zero_bits; ++i) {
48,710✔
37
         if(C.is_odd() || D.is_odd()) {
24,382✔
38
            C += n;
12,079✔
39
            D -= mod;
12,079✔
40
         }
41
         C >>= 1;
24,382✔
42
         D >>= 1;
24,382✔
43
      }
44

45
      if(u >= v) {
24,328✔
46
         u -= v;
12,172✔
47
         A -= C;
12,172✔
48
         B -= D;
12,172✔
49
      } else {
50
         v -= u;
12,156✔
51
         C -= A;
12,156✔
52
         D -= B;
12,156✔
53
      }
54
   }
55

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

60
   while(D.is_negative()) {
90✔
61
      D += mod;
43✔
62
   }
63
   while(D >= mod) {
47✔
64
      D -= mod;
×
65
   }
66

67
   return D;
47✔
68
}
402✔
69

70
}  // namespace
71

72
void fuzz(std::span<const uint8_t> in) {
100✔
73
   static const size_t max_bits = 4096;
100✔
74

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

79
   const Botan::BigInt x = Botan::BigInt::from_bytes(in.subspan(0, in.size() / 2));
100✔
80
   Botan::BigInt mod = Botan::BigInt::from_bytes(in.subspan(in.size() / 2, in.size() - in.size() / 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.to_hex_string() << "\n"
99✔
91
                                    << "Mod = " << mod.to_hex_string() << "\n"
92
                                    << "GCD(X,Mod) = " << gcd(x, mod).to_hex_string() << "\n"
93
                                    << "RefInv(X,Mod) = " << ref.to_hex_string() << "\n"
94
                                    << "LibInv(X,Mod)  = " << lib.to_hex_string() << "\n"
95
                                    << "RefCheck = " << ((x * ref) % mod).to_hex_string() << "\n"
96
                                    << "LibCheck  = " << ((x * lib) % mod).to_hex_string() << "\n");
97
   }
98
}
344✔
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