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

randombit / botan / 17281389884

25 Aug 2025 03:59PM UTC coverage: 90.666% (-0.001%) from 90.667%
17281389884

push

github

geekrun
init

100200 of 110516 relevant lines covered (90.67%)

12215638.67 hits per line

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

91.18
/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_size, BigInt::Sign y_sign) {
3,961,176✔
20
   const size_t x_sw = x.sig_words();
3,961,176✔
21

22
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_size) + 1);
4,139,404✔
23

24
   if(x.sign() == y_sign) {
3,961,176✔
25
      word carry = bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_size);
3,504,809✔
26
      z.mutable_data()[std::max(x_sw, y_size)] += carry;
3,504,809✔
27
      z.set_sign(x.sign());
3,504,809✔
28
   } else {
29
      const int32_t relative_size = bigint_cmp(x.data(), x_sw, y, y_size);
456,367✔
30

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

50
   return z;
3,961,176✔
51
}
×
52

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

60
   BigInt z = BigInt::with_capacity(x.size() + y.size());
46,807✔
61

62
   if(x_sw == 1 && y_sw > 0) {
46,807✔
63
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
47,658✔
64
   } else if(y_sw == 1 && x_sw > 0) {
22,978✔
65
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
7,688✔
66
   } else if(x_sw > 0 && y_sw > 0) {
19,134✔
67
      secure_vector<word> workspace(z.size());
18,311✔
68

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

81
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
93,243✔
82

83
   return z;
46,807✔
84
}
×
85

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

92
   BigInt z = BigInt::with_capacity(x_sw + 1);
3,526,849✔
93

94
   if(x_sw > 0 && y > 0) {
3,526,849✔
95
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
3,417,755✔
96
      z.set_sign(x.sign());
3,417,755✔
97
   }
98

99
   return z;
3,526,849✔
100
}
×
101

102
/*
103
* Division Operator
104
*/
105
BigInt operator/(const BigInt& x, const BigInt& y) {
156,037✔
106
   if(y.sig_words() == 1 && y.is_positive()) {
158,480✔
107
      return x / y.word_at(0);
110,892✔
108
   }
109

110
   BigInt q;
100,591✔
111
   BigInt r;
100,591✔
112
   vartime_divide(x, y, q, r);
100,591✔
113
   return q;
100,591✔
114
}
100,591✔
115

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

124
   BigInt q;
56,042✔
125
   word r = 0;
56,042✔
126
   ct_divide_word(x, y, q, r);
56,042✔
127
   return q;
56,042✔
128
}
×
129

130
/*
131
* Modulo Operator
132
*/
133
BigInt operator%(const BigInt& n, const BigInt& mod) {
462,348✔
134
   if(mod.is_zero()) {
462,459✔
135
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
136
   }
137
   if(mod.is_negative()) {
462,348✔
138
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
139
   }
140
   if(n.is_positive() && mod.is_positive() && n < mod) {
893,507✔
141
      return n;
400✔
142
   }
143

144
   if(mod.sig_words() == 1) {
461,948✔
145
      return BigInt::from_word(n % mod.word_at(0));
482,818✔
146
   }
147

148
   BigInt q;
220,539✔
149
   BigInt r;
220,539✔
150
   vartime_divide(n, mod, q, r);
220,539✔
151
   return r;
220,539✔
152
}
220,539✔
153

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

162
   if(mod == 1) {
988,193✔
163
      return 0;
164
   }
165

166
   word remainder = 0;
988,179✔
167

168
   if(n.is_positive() && is_power_of_2(mod)) {
988,179✔
169
      remainder = (n.word_at(0) & (mod - 1));
380,638✔
170
   } else {
171
      const size_t sw = n.sig_words();
797,860✔
172
      for(size_t i = sw; i > 0; --i) {
7,346,916✔
173
         remainder = bigint_modop_vartime(remainder, n.word_at(i - 1), mod);
13,098,112✔
174
      }
175
   }
176

177
   if(remainder != 0 && n.sign() == BigInt::Negative) {
988,179✔
178
      return mod - remainder;
29,990✔
179
   }
180
   return remainder;
181
}
182

183
/*
184
* Left Shift Operator
185
*/
186
BigInt operator<<(const BigInt& x, size_t shift) {
324,352✔
187
   const size_t x_sw = x.sig_words();
324,352✔
188

189
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
324,352✔
190
   BigInt y = BigInt::with_capacity(new_size);
324,352✔
191
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
324,352✔
192
   y.set_sign(x.sign());
324,352✔
193
   return y;
324,352✔
194
}
×
195

196
/*
197
* Right Shift Operator
198
*/
199
BigInt operator>>(const BigInt& x, size_t shift) {
216,639✔
200
   const size_t shift_words = shift / WordInfo<word>::bits;
216,639✔
201
   const size_t x_sw = x.sig_words();
216,639✔
202

203
   if(shift_words >= x_sw) {
216,639✔
204
      return BigInt::zero();
2,062✔
205
   }
206

207
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
214,577✔
208
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
214,577✔
209

210
   if(x.is_negative() && y.is_zero()) {
214,602✔
211
      y.set_sign(BigInt::Positive);
3✔
212
   } else {
213
      y.set_sign(x.sign());
214,574✔
214
   }
215

216
   return y;
214,577✔
217
}
214,577✔
218

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