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

randombit / botan / 16393801904

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

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

99942 of 110266 relevant lines covered (90.64%)

12231283.62 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,028,868✔
20
   const size_t x_sw = x.sig_words();
4,028,868✔
21

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

24
   if(x.sign() == y_sign) {
4,028,868✔
25
      bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_words);
3,461,863✔
26
      z.set_sign(x.sign());
3,461,863✔
27
   } else {
28
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x._data(), x_sw, y, y_words);
567,005✔
29

30
      //z.sign_fixup(relative_size, y_sign);
31
      if(relative_size < 0) {
567,005✔
32
         z.set_sign(y_sign);
6,577✔
33
      } else if(relative_size == 0) {
560,428✔
34
         z.set_sign(BigInt::Positive);
109✔
35
      } else {
36
         z.set_sign(x.sign());
560,319✔
37
      }
38
   }
39

40
   return z;
4,028,868✔
41
}
×
42

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

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

52
   if(x_sw == 1 && y_sw > 0) {
46,558✔
53
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
47,648✔
54
   } else if(y_sw == 1 && x_sw > 0) {
22,734✔
55
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
7,686✔
56
   } else if(x_sw > 0 && y_sw > 0) {
18,891✔
57
      secure_vector<word> workspace(z.size());
18,103✔
58

59
      bigint_mul(z.mutable_data(),
18,103✔
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,103✔
70

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

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

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

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

84
   if(x_sw > 0 && y > 0) {
3,495,319✔
85
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
3,386,230✔
86
      z.set_sign(x.sign());
3,386,230✔
87
   }
88

89
   return z;
3,495,319✔
90
}
×
91

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

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

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

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

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

134
   if(mod.sig_words() == 1) {
383,701✔
135
      return BigInt::from_word(n % mod.word_at(0));
364,492✔
136
   }
137

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

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

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

156
   word remainder = 0;
1,343,663✔
157

158
   if(is_power_of_2(mod)) {
1,343,663✔
159
      remainder = (n.word_at(0) & (mod - 1));
1,224,216✔
160
   } else {
161
      const size_t sw = n.sig_words();
731,555✔
162
      for(size_t i = sw; i > 0; --i) {
7,296,391✔
163
         remainder = bigint_modop_vartime(remainder, n.word_at(i - 1), mod);
13,129,672✔
164
      }
165
   }
166

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

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

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

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

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

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

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

206
   return y;
206,927✔
207
}
206,927✔
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