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

randombit / botan / 13133892967

04 Feb 2025 10:56AM UTC coverage: 91.251% (+0.02%) from 91.236%
13133892967

Pull #4554

github

web-flow
Merge 336a22e28 into 6711c7c93
Pull Request #4554: Add generic pcurves for application specific curves

95115 of 104235 relevant lines covered (91.25%)

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

22
   while(u.is_nonzero()) {
41,761✔
23
      const size_t u_zero_bits = Botan::low_zero_bits(u);
27,691✔
24
      u >>= u_zero_bits;
27,691✔
25
      for(size_t i = 0; i != u_zero_bits; ++i) {
55,607✔
26
         if(A.is_odd() || B.is_odd()) {
27,916✔
27
            A += n;
13,934✔
28
            B -= mod;
13,934✔
29
         }
30
         A >>= 1;
27,916✔
31
         B >>= 1;
27,916✔
32
      }
33

34
      const size_t v_zero_bits = Botan::low_zero_bits(v);
27,691✔
35
      v >>= v_zero_bits;
27,691✔
36
      for(size_t i = 0; i != v_zero_bits; ++i) {
55,291✔
37
         if(C.is_odd() || D.is_odd()) {
27,600✔
38
            C += n;
13,809✔
39
            D -= mod;
13,809✔
40
         }
41
         C >>= 1;
27,600✔
42
         D >>= 1;
27,600✔
43
      }
44

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

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

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

67
   return D;
61✔
68
}
444✔
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
}
358✔
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