Coverage Report

Created: 2026-10-28 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/math/bigint/big_ops3.cpp
Line
Count
Source
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
90.0k
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_size, BigInt::Sign y_sign) {
20
90.0k
   const size_t x_sw = x.sig_words();
21
22
90.0k
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_size) + 1);
23
24
90.0k
   if(x.sign() == y_sign) {
25
64.1k
      word carry = bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_size);
26
64.1k
      z.mutable_data()[std::max(x_sw, y_size)] += carry;
27
64.1k
      z.set_sign(x.sign());
28
64.1k
   } else {
29
25.9k
      const int32_t relative_size = bigint_cmp(x.data(), x_sw, y, y_size);
30
31
25.9k
      if(relative_size < 0) {
32
         // x < y so z = abs(y - x)
33
         // NOLINTNEXTLINE(*-suspicious-call-argument) intentionally swapping x and y here
34
21
         bigint_sub3(z.mutable_data(), y, y_size, x.data(), x_sw);
35
21
         z.set_sign(y_sign);
36
25.8k
      } else if(relative_size == 0) {
37
         // Positive zero (nothing to do in this case)
38
25.8k
      } 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
25.8k
         y_size = std::min(x_sw, y_size);
45
25.8k
         bigint_sub3(z.mutable_data(), x.data(), x_sw, y, y_size);
46
25.8k
         z.set_sign(x.sign());
47
25.8k
      }
48
25.9k
   }
49
50
90.0k
   return z;
51
90.0k
}
52
53
/*
54
* Multiplication Operator
55
*/
56
569
BigInt operator*(const BigInt& x, const BigInt& y) {
57
569
   const size_t x_sw = x.sig_words();
58
569
   const size_t y_sw = y.sig_words();
59
60
569
   BigInt z = BigInt::with_capacity(x.size() + y.size());
61
62
569
   if(x_sw == 1 && y_sw > 0) {
63
35
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
64
534
   } else if(y_sw == 1 && x_sw > 0) {
65
322
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
66
322
   } else if(x_sw > 0 && y_sw > 0) {
67
212
      secure_vector<word> workspace(z.size());
68
69
212
      bigint_mul(z.mutable_data(),
70
212
                 z.size(),
71
212
                 x._data(),
72
212
                 x.size(),
73
212
                 x_sw,
74
212
                 y._data(),
75
212
                 y.size(),
76
212
                 y_sw,
77
212
                 workspace.data(),
78
212
                 workspace.size());
79
212
   }
80
81
569
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
82
83
569
   return z;
84
569
}
85
86
/*
87
* Multiplication Operator
88
*/
89
323
BigInt operator*(const BigInt& x, word y) {
90
323
   const size_t x_sw = x.sig_words();
91
92
323
   BigInt z = BigInt::with_capacity(x_sw + 1);
93
94
323
   if(x_sw > 0 && y > 0) {
95
323
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
96
323
      z.set_sign(x.sign());
97
323
   }
98
99
323
   return z;
100
323
}
101
102
/*
103
* Division Operator
104
*/
105
0
BigInt operator/(const BigInt& x, const BigInt& y) {
106
0
   if(y.sig_words() == 1 && y.is_positive()) {
107
0
      return x / y.word_at(0);
108
0
   }
109
110
0
   BigInt q;
111
0
   BigInt r;
112
0
   vartime_divide(x, y, q, r);
113
0
   return q;
114
0
}
115
116
/*
117
* Division Operator
118
*/
119
42
BigInt operator/(const BigInt& x, word y) {
120
42
   if(y == 0) {
121
0
      throw Invalid_Argument("BigInt::operator/ divide by zero");
122
0
   }
123
124
42
   BigInt q;
125
42
   word r = 0;
126
42
   ct_divide_word(x, y, q, r);
127
42
   return q;
128
42
}
129
130
/*
131
* Modulo Operator
132
*/
133
315
BigInt operator%(const BigInt& n, const BigInt& mod) {
134
315
   if(mod.is_zero()) {
135
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
136
0
   }
137
315
   if(mod.is_negative()) {
138
0
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
139
0
   }
140
315
   if(n.is_positive() && mod.is_positive() && n < mod) {
141
0
      return n;
142
0
   }
143
144
315
   if(mod.sig_words() == 1) {
145
249
      return BigInt::from_word(n % mod.word_at(0));
146
249
   }
147
148
66
   BigInt q;
149
66
   BigInt r;
150
66
   vartime_divide(n, mod, q, r);
151
66
   return r;
152
315
}
153
154
/*
155
* Modulo Operator
156
*/
157
510
word operator%(const BigInt& n, word mod) {
158
510
   if(mod == 0) {
159
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
160
0
   }
161
162
510
   if(mod == 1) {
163
0
      return 0;
164
0
   }
165
166
510
   word remainder = 0;
167
168
510
   if(n.is_positive() && is_power_of_2(mod)) {
169
261
      remainder = (n.word_at(0) & (mod - 1));
170
261
   } else {
171
249
      divide_precomp redc_mod(mod);
172
249
      const size_t sw = n.sig_words();
173
680
      for(size_t i = sw; i > 0; --i) {
174
431
         remainder = redc_mod.vartime_mod_2to1(remainder, n.word_at(i - 1));
175
431
      }
176
249
   }
177
178
510
   if(remainder != 0 && n.sign() == BigInt::Negative) {
179
0
      return mod - remainder;
180
0
   }
181
510
   return remainder;
182
510
}
183
184
/*
185
* Left Shift Operator
186
*/
187
171
BigInt operator<<(const BigInt& x, size_t shift) {
188
171
   if(x.is_zero()) {
189
0
      return BigInt::zero();
190
0
   }
191
192
171
   const size_t x_sw = x.sig_words();
193
194
171
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
195
171
   BigInt y = BigInt::with_capacity(new_size);
196
171
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
197
171
   y.set_sign(x.sign());
198
171
   return y;
199
171
}
200
201
/*
202
* Right Shift Operator
203
*/
204
12.4k
BigInt operator>>(const BigInt& x, size_t shift) {
205
12.4k
   const size_t shift_words = shift / WordInfo<word>::bits;
206
12.4k
   const size_t x_sw = x.sig_words();
207
208
12.4k
   if(shift_words >= x_sw) {
209
0
      return BigInt::zero();
210
0
   }
211
212
12.4k
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
213
12.4k
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
214
215
12.4k
   if(x.is_negative() && y.is_zero()) {
216
0
      y.set_sign(BigInt::Positive);
217
12.4k
   } else {
218
12.4k
      y.set_sign(x.sign());
219
12.4k
   }
220
221
12.4k
   return y;
222
12.4k
}
223
224
}  // namespace Botan