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

randombit / botan / 16399247596

19 Jul 2025 11:30PM UTC coverage: 90.635% (-0.07%) from 90.708%
16399247596

push

github

web-flow
Merge pull request #4998 from randombit/jack/fix-clang-tidy-readability-isolate-declaration

Enable and fix clang-tidy warning readability-isolate-declaration

99940 of 110266 relevant lines covered (90.64%)

12278552.1 hits per line

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

90.91
/src/lib/math/bigint/big_ops3.cpp
1
/*
2
* BigInt Binary Operators
3
* (C) 1999-2007,2018 Jack Lloyd
4
*     2016 Matthias Gierlings
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/bigint.h>
10

11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/divide.h>
13
#include <botan/internal/mp_core.h>
14
#include <algorithm>
15

16
namespace Botan {
17

18
//static
19
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sign y_sign) {
4,076,635✔
20
   const size_t x_sw = x.sig_words();
4,076,635✔
21

22
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
4,249,239✔
23

24
   if(x.sign() == y_sign) {
4,076,635✔
25
      bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_words);
3,505,976✔
26
      z.set_sign(x.sign());
3,505,976✔
27
   } else {
28
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x._data(), x_sw, y, y_words);
570,659✔
29

30
      //z.sign_fixup(relative_size, y_sign);
31
      if(relative_size < 0) {
570,659✔
32
         z.set_sign(y_sign);
6,911✔
33
      } else if(relative_size == 0) {
563,748✔
34
         z.set_sign(BigInt::Positive);
114✔
35
      } else {
36
         z.set_sign(x.sign());
563,634✔
37
      }
38
   }
39

40
   return z;
4,076,635✔
41
}
×
42

43
/*
44
* Multiplication Operator
45
*/
46
BigInt operator*(const BigInt& x, const BigInt& y) {
46,512✔
47
   const size_t x_sw = x.sig_words();
46,512✔
48
   const size_t y_sw = y.sig_words();
46,512✔
49

50
   BigInt z = BigInt::with_capacity(x.size() + y.size());
46,512✔
51

52
   if(x_sw == 1 && y_sw > 0) {
46,512✔
53
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
47,650✔
54
   } else if(y_sw == 1 && x_sw > 0) {
22,687✔
55
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
7,690✔
56
   } else if(x_sw > 0 && y_sw > 0) {
18,842✔
57
      secure_vector<word> workspace(z.size());
18,078✔
58

59
      bigint_mul(z.mutable_data(),
18,078✔
60
                 z.size(),
61
                 x._data(),
62
                 x.size(),
63
                 x_sw,
64
                 y._data(),
65
                 y.size(),
66
                 y_sw,
67
                 workspace.data(),
68
                 workspace.size());
69
   }
18,078✔
70

71
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
92,653✔
72

73
   return z;
46,512✔
74
}
×
75

76
/*
77
* Multiplication Operator
78
*/
79
BigInt operator*(const BigInt& x, word y) {
3,504,355✔
80
   const size_t x_sw = x.sig_words();
3,504,355✔
81

82
   BigInt z = BigInt::with_capacity(x_sw + 1);
3,504,355✔
83

84
   if(x_sw > 0 && y > 0) {
3,504,355✔
85
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
3,395,266✔
86
      z.set_sign(x.sign());
3,395,266✔
87
   }
88

89
   return z;
3,504,355✔
90
}
×
91

92
/*
93
* Division Operator
94
*/
95
BigInt operator/(const BigInt& x, const BigInt& y) {
155,996✔
96
   if(y.sig_words() == 1) {
158,438✔
97
      return x / y.word_at(0);
110,882✔
98
   }
99

100
   BigInt q;
100,555✔
101
   BigInt r;
100,555✔
102
   vartime_divide(x, y, q, r);
100,555✔
103
   return q;
100,555✔
104
}
100,555✔
105

106
/*
107
* Division Operator
108
*/
109
BigInt operator/(const BigInt& x, word y) {
383,677✔
110
   if(y == 0) {
383,677✔
111
      throw Invalid_Argument("BigInt::operator/ divide by zero");
×
112
   }
113

114
   BigInt q;
383,677✔
115
   word r = 0;
383,677✔
116
   ct_divide_word(x, y, q, r);
383,677✔
117
   return q;
383,677✔
118
}
×
119

120
/*
121
* Modulo Operator
122
*/
123
BigInt operator%(const BigInt& n, const BigInt& mod) {
556,362✔
124
   if(mod.is_zero()) {
556,472✔
125
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
126
   }
127
   if(mod.is_negative()) {
556,362✔
128
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
129
   }
130
   if(n.is_positive() && mod.is_positive() && n < mod) {
1,081,692✔
131
      return n;
172,017✔
132
   }
133

134
   if(mod.sig_words() == 1) {
384,345✔
135
      return BigInt::from_word(n % mod.word_at(0));
365,238✔
136
   }
137

138
   BigInt q;
201,726✔
139
   BigInt r;
201,726✔
140
   vartime_divide(n, mod, q, r);
201,726✔
141
   return r;
201,726✔
142
}
201,726✔
143

144
/*
145
* Modulo Operator
146
*/
147
word operator%(const BigInt& n, word mod) {
1,358,659✔
148
   if(mod == 0) {
1,358,659✔
149
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
150
   }
151

152
   if(mod == 1) {
1,358,659✔
153
      return 0;
154
   }
155

156
   word remainder = 0;
1,358,645✔
157

158
   if(is_power_of_2(mod)) {
1,358,645✔
159
      remainder = (n.word_at(0) & (mod - 1));
1,225,514✔
160
   } else {
161
      const size_t sw = n.sig_words();
745,888✔
162
      for(size_t i = sw; i > 0; --i) {
7,540,444✔
163
         remainder = bigint_modop_vartime(remainder, n.word_at(i - 1), mod);
13,589,112✔
164
      }
165
   }
166

167
   if(remainder != 0 && n.sign() == BigInt::Negative) {
1,358,645✔
168
      return mod - remainder;
29,949✔
169
   }
170
   return remainder;
171
}
172

173
/*
174
* Left Shift Operator
175
*/
176
BigInt operator<<(const BigInt& x, size_t shift) {
305,521✔
177
   const size_t x_sw = x.sig_words();
305,521✔
178

179
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
305,521✔
180
   BigInt y = BigInt::with_capacity(new_size);
305,521✔
181
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
305,521✔
182
   y.set_sign(x.sign());
305,521✔
183
   return y;
305,521✔
184
}
×
185

186
/*
187
* Right Shift Operator
188
*/
189
BigInt operator>>(const BigInt& x, size_t shift) {
210,175✔
190
   const size_t shift_words = shift / WordInfo<word>::bits;
210,175✔
191
   const size_t x_sw = x.sig_words();
210,175✔
192

193
   if(shift_words >= x_sw) {
210,175✔
194
      return BigInt::zero();
2,062✔
195
   }
196

197
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
208,113✔
198
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
208,113✔
199

200
   if(x.is_negative() && y.is_zero()) {
208,138✔
201
      y.set_sign(BigInt::Positive);
3✔
202
   } else {
203
      y.set_sign(x.sign());
208,110✔
204
   }
205

206
   return y;
208,113✔
207
}
208,113✔
208

209
}  // namespace Botan
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