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

randombit / botan / 16341960231

17 Jul 2025 09:53AM UTC coverage: 90.637% (+0.01%) from 90.627%
16341960231

Pull #4998

github

web-flow
Merge 3c2fa7646 into 421b766f1
Pull Request #4998: Enable and fix clang-tidy warning readability-isolate-declaration

99940 of 110264 relevant lines covered (90.64%)

12277154.52 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,003,892✔
20
   const size_t x_sw = x.sig_words();
4,003,892✔
21

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

24
   if(x.sign() == y_sign) {
4,003,892✔
25
      bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_words);
3,439,206✔
26
      z.set_sign(x.sign());
3,439,206✔
27
   } else {
28
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x._data(), x_sw, y, y_words);
564,686✔
29

30
      //z.sign_fixup(relative_size, y_sign);
31
      if(relative_size < 0) {
564,686✔
32
         z.set_sign(y_sign);
6,626✔
33
      } else if(relative_size == 0) {
558,060✔
34
         z.set_sign(BigInt::Positive);
110✔
35
      } else {
36
         z.set_sign(x.sign());
557,950✔
37
      }
38
   }
39

40
   return z;
4,003,892✔
41
}
×
42

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

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

52
   if(x_sw == 1 && y_sw > 0) {
46,520✔
53
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
47,588✔
54
   } else if(y_sw == 1 && x_sw > 0) {
22,726✔
55
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
7,688✔
56
   } else if(x_sw > 0 && y_sw > 0) {
18,882✔
57
      secure_vector<word> workspace(z.size());
18,088✔
58

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

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

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

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

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

84
   if(x_sw > 0 && y > 0) {
3,500,586✔
85
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
3,391,497✔
86
      z.set_sign(x.sign());
3,391,497✔
87
   }
88

89
   return z;
3,500,586✔
90
}
×
91

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

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

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

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

120
/*
121
* Modulo Operator
122
*/
123
BigInt operator%(const BigInt& n, const BigInt& mod) {
553,701✔
124
   if(mod.is_zero()) {
553,811✔
125
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
126
   }
127
   if(mod.is_negative()) {
553,701✔
128
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
129
   }
130
   if(n.is_positive() && mod.is_positive() && n < mod) {
1,076,437✔
131
      return n;
171,759✔
132
   }
133

134
   if(mod.sig_words() == 1) {
381,942✔
135
      return BigInt::from_word(n % mod.word_at(0));
363,190✔
136
   }
137

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

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

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

156
   word remainder = 0;
1,331,263✔
157

158
   if(is_power_of_2(mod)) {
1,331,263✔
159
      remainder = (n.word_at(0) & (mod - 1));
1,215,448✔
160
   } else {
161
      const size_t sw = n.sig_words();
723,539✔
162
      for(size_t i = sw; i > 0; --i) {
7,171,809✔
163
         remainder = bigint_modop_vartime(remainder, n.word_at(i - 1), mod);
12,896,540✔
164
      }
165
   }
166

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

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

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

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

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

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

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

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