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

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

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

92.38
/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/exceptn.h>
12
#include <botan/internal/bit_ops.h>
13
#include <botan/internal/divide.h>
14
#include <botan/internal/mp_core.h>
15
#include <algorithm>
16

17
namespace Botan {
18

19
//static
20
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_size, BigInt::Sign y_sign) {
3,093,849✔
21
   const size_t x_sw = x.sig_words();
3,093,849✔
22

23
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_size) + 1);
3,250,718✔
24

25
   if(x.sign() == y_sign) {
3,093,849✔
26
      const word carry = bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_size);
2,673,840✔
27
      z.mutable_data()[std::max(x_sw, y_size)] += carry;
2,673,840✔
28
      z.set_sign(x.sign());
2,673,840✔
29
   } else {
30
      const int32_t relative_size = bigint_cmp(x.data(), x_sw, y, y_size);
420,009✔
31

32
      if(relative_size < 0) {
420,009✔
33
         // x < y so z = abs(y - x)
34
         // NOLINTNEXTLINE(*-suspicious-call-argument) intentionally swapping x and y here
35
         bigint_sub3(z.mutable_data(), y, y_size, x.data(), x_sw);
6,812✔
36
         z.set_sign(y_sign);
6,812✔
37
      } else if(relative_size == 0) {
413,197✔
38
         // Positive zero (nothing to do in this case)
39
      } else {
40
         /*
41
         * We know at this point that x >= y so if y_size is larger than
42
         * x_sw, we are guaranteed they are just leading zeros which can
43
         * be ignored
44
         */
45
         y_size = std::min(x_sw, y_size);
413,085✔
46
         bigint_sub3(z.mutable_data(), x.data(), x_sw, y, y_size);
413,085✔
47
         z.set_sign(x.sign());
413,085✔
48
      }
49
   }
50

51
   return z;
3,093,849✔
52
}
×
53

54
/*
55
* Multiplication Operator
56
*/
57
BigInt operator*(const BigInt& x, const BigInt& y) {
42,685✔
58
   const size_t x_sw = x.sig_words();
42,685✔
59
   const size_t y_sw = y.sig_words();
42,685✔
60

61
   BigInt z = BigInt::with_capacity(x.size() + y.size());
42,685✔
62

63
   if(x_sw == 1 && y_sw > 0) {
42,685✔
64
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
39,560✔
65
   } else if(y_sw == 1 && x_sw > 0) {
22,905✔
66
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
7,690✔
67
   } else if(x_sw > 0 && y_sw > 0) {
19,060✔
68
      secure_vector<word> workspace(z.size());
18,269✔
69

70
      bigint_mul(z.mutable_data(),
18,269✔
71
                 z.size(),
72
                 x._data(),
73
                 x.size(),
74
                 x_sw,
75
                 y._data(),
76
                 y.size(),
77
                 y_sw,
78
                 workspace.data(),
79
                 workspace.size());
80
   }
18,269✔
81

82
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
85,005✔
83

84
   return z;
42,685✔
85
}
×
86

87
/*
88
* Multiplication Operator
89
*/
90
BigInt operator*(const BigInt& x, word y) {
3,685,924✔
91
   const size_t x_sw = x.sig_words();
3,685,924✔
92

93
   BigInt z = BigInt::with_capacity(x_sw + 1);
3,685,924✔
94

95
   if(x_sw > 0 && y > 0) {
3,685,924✔
96
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
3,685,924✔
97
      z.set_sign(x.sign());
3,685,924✔
98
   }
99

100
   return z;
3,685,924✔
101
}
×
102

103
/*
104
* Division Operator
105
*/
106
BigInt operator/(const BigInt& x, const BigInt& y) {
70,706✔
107
   if(y.sig_words() == 1 && y.is_positive()) {
73,147✔
108
      return x / y.word_at(0);
10,524✔
109
   }
110

111
   BigInt q;
65,444✔
112
   BigInt r;
65,444✔
113
   vartime_divide(x, y, q, r);
65,444✔
114
   return q;
65,444✔
115
}
65,444✔
116

117
/*
118
* Division Operator
119
*/
120
BigInt operator/(const BigInt& x, word y) {
5,845✔
121
   if(y == 0) {
5,845✔
122
      throw Invalid_Argument("BigInt::operator/ divide by zero");
×
123
   }
124

125
   BigInt q;
5,845✔
126
   word r = 0;
5,845✔
127
   ct_divide_word(x, y, q, r);
5,845✔
128
   return q;
5,845✔
129
}
×
130

131
/*
132
* Modulo Operator
133
*/
134
BigInt operator%(const BigInt& n, const BigInt& mod) {
455,919✔
135
   if(mod.is_zero()) {
456,031✔
136
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
137
   }
138
   if(mod.is_negative()) {
455,919✔
139
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
140
   }
141
   if(n.is_positive() && mod.is_positive() && n < mod) {
881,135✔
142
      return n;
390✔
143
   }
144

145
   if(mod.sig_words() == 1) {
455,529✔
146
      return BigInt::from_word(n % mod.word_at(0));
479,948✔
147
   }
148

149
   BigInt q;
215,555✔
150
   BigInt r;
215,555✔
151
   vartime_divide(n, mod, q, r);
215,555✔
152
   return r;
215,555✔
153
}
215,555✔
154

155
/*
156
* Modulo Operator
157
*/
158
word operator%(const BigInt& n, word mod) {
520,763✔
159
   if(mod == 0) {
520,763✔
160
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
161
   }
162

163
   if(mod == 1) {
520,763✔
164
      return 0;
165
   }
166

167
   word remainder = 0;
520,751✔
168

169
   if(n.is_positive() && is_power_of_2(mod)) {
520,751✔
170
      remainder = (n.word_at(0) & (mod - 1));
379,348✔
171
   } else {
172
      const divide_precomp redc_mod(mod);
331,077✔
173
      const size_t sw = n.sig_words();
331,077✔
174
      for(size_t i = sw; i > 0; --i) {
675,537✔
175
         remainder = redc_mod.vartime_mod_2to1(remainder, n.word_at(i - 1));
688,920✔
176
      }
177
   }
178

179
   if(remainder != 0 && n.sign() == BigInt::Negative) {
520,751✔
180
      return mod - remainder;
29,982✔
181
   }
182
   return remainder;
183
}
184

185
/*
186
* Left Shift Operator
187
*/
188
BigInt operator<<(const BigInt& x, size_t shift) {
439,473✔
189
   if(x.is_zero()) {
841,993✔
190
      return BigInt::zero();
2✔
191
   }
192

193
   const size_t x_sw = x.sig_words();
439,471✔
194

195
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
439,471✔
196
   BigInt y = BigInt::with_capacity(new_size);
439,471✔
197
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
439,471✔
198
   y.set_sign(x.sign());
439,471✔
199
   return y;
439,471✔
200
}
439,471✔
201

202
/*
203
* Right Shift Operator
204
*/
205
BigInt operator>>(const BigInt& x, size_t shift) {
199,521✔
206
   const size_t shift_words = shift / WordInfo<word>::bits;
199,521✔
207
   const size_t x_sw = x.sig_words();
199,521✔
208

209
   if(shift_words >= x_sw) {
199,521✔
210
      return BigInt::zero();
2,063✔
211
   }
212

213
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
197,458✔
214
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
197,458✔
215

216
   if(x.is_negative() && y.is_zero()) {
197,483✔
217
      y.set_sign(BigInt::Positive);
3✔
218
   } else {
219
      y.set_sign(x.sign());
197,455✔
220
   }
221

222
   return y;
197,458✔
223
}
197,458✔
224

225
}  // 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