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

randombit / botan / 11367420381

16 Oct 2024 02:08PM UTC coverage: 91.119% (+0.002%) from 91.117%
11367420381

push

github

web-flow
Merge pull request #4383 from Rohde-Schwarz/fix/pk_api_sign_test

Fix pk_api_sign Test for SHAKE-only SLH-DSA

90998 of 99867 relevant lines covered (91.12%)

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

22
   while(u.is_nonzero()) {
44,862✔
23
      const size_t u_zero_bits = Botan::low_zero_bits(u);
29,868✔
24
      u >>= u_zero_bits;
29,868✔
25
      for(size_t i = 0; i != u_zero_bits; ++i) {
59,798✔
26
         if(A.is_odd() || B.is_odd()) {
29,930✔
27
            A += n;
14,960✔
28
            B -= mod;
14,960✔
29
         }
30
         A >>= 1;
29,930✔
31
         B >>= 1;
29,930✔
32
      }
33

34
      const size_t v_zero_bits = Botan::low_zero_bits(v);
29,868✔
35
      v >>= v_zero_bits;
29,868✔
36
      for(size_t i = 0; i != v_zero_bits; ++i) {
59,561✔
37
         if(C.is_odd() || D.is_odd()) {
29,693✔
38
            C += n;
14,952✔
39
            D -= mod;
14,952✔
40
         }
41
         C >>= 1;
29,693✔
42
         D >>= 1;
29,693✔
43
      }
44

45
      if(u >= v) {
29,868✔
46
         u -= v;
14,912✔
47
         A -= C;
14,912✔
48
         B -= D;
14,912✔
49
      } else {
50
         v -= u;
14,956✔
51
         C -= A;
14,956✔
52
         D -= B;
14,956✔
53
      }
54
   }
55

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

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

67
   return D;
57✔
68
}
492✔
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
}
370✔
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