Coverage Report

Created: 2026-10-10 06:25

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
97.8k
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_size, BigInt::Sign y_sign) {
20
97.8k
   const size_t x_sw = x.sig_words();
21
22
97.8k
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_size) + 1);
23
24
97.8k
   if(x.sign() == y_sign) {
25
69.8k
      word carry = bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_size);
26
69.8k
      z.mutable_data()[std::max(x_sw, y_size)] += carry;
27
69.8k
      z.set_sign(x.sign());
28
69.8k
   } else {
29
28.0k
      const int32_t relative_size = bigint_cmp(x.data(), x_sw, y, y_size);
30
31
28.0k
      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
28.0k
      } else if(relative_size == 0) {
37
         // Positive zero (nothing to do in this case)
38
28.0k
      } 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
28.0k
         y_size = std::min(x_sw, y_size);
45
28.0k
         bigint_sub3(z.mutable_data(), x.data(), x_sw, y, y_size);
46
28.0k
         z.set_sign(x.sign());
47
28.0k
      }
48
28.0k
   }
49
50
97.8k
   return z;
51
97.8k
}
52
53
/*
54
* Multiplication Operator
55
*/
56
591
BigInt operator*(const BigInt& x, const BigInt& y) {
57
591
   const size_t x_sw = x.sig_words();
58
591
   const size_t y_sw = y.sig_words();
59
60
591
   BigInt z = BigInt::with_capacity(x.size() + y.size());
61
62
591
   if(x_sw == 1 && y_sw > 0) {
63
41
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
64
550
   } else if(y_sw == 1 && x_sw > 0) {
65
331
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
66
331
   } else if(x_sw > 0 && y_sw > 0) {
67
219
      secure_vector<word> workspace(z.size());
68
69
219
      bigint_mul(z.mutable_data(),
70
219
                 z.size(),
71
219
                 x._data(),
72
219
                 x.size(),
73
219
                 x_sw,
74
219
                 y._data(),
75
219
                 y.size(),
76
219
                 y_sw,
77
219
                 workspace.data(),
78
219
                 workspace.size());
79
219
   }
80
81
591
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
82
83
591
   return z;
84
591
}
85
86
/*
87
* Multiplication Operator
88
*/
89
341
BigInt operator*(const BigInt& x, word y) {
90
341
   const size_t x_sw = x.sig_words();
91
92
341
   BigInt z = BigInt::with_capacity(x_sw + 1);
93
94
341
   if(x_sw > 0 && y > 0) {
95
341
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
96
341
      z.set_sign(x.sign());
97
341
   }
98
99
341
   return z;
100
341
}
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
358
BigInt operator%(const BigInt& n, const BigInt& mod) {
134
358
   if(mod.is_zero()) {
135
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
136
0
   }
137
358
   if(mod.is_negative()) {
138
0
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
139
0
   }
140
358
   if(n.is_positive() && mod.is_positive() && n < mod) {
141
0
      return n;
142
0
   }
143
144
358
   if(mod.sig_words() == 1) {
145
280
      return BigInt::from_word(n % mod.word_at(0));
146
280
   }
147
148
78
   BigInt q;
149
78
   BigInt r;
150
78
   vartime_divide(n, mod, q, r);
151
78
   return r;
152
358
}
153
154
/*
155
* Modulo Operator
156
*/
157
585
word operator%(const BigInt& n, word mod) {
158
585
   if(mod == 0) {
159
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
160
0
   }
161
162
585
   if(mod == 1) {
163
0
      return 0;
164
0
   }
165
166
585
   word remainder = 0;
167
168
585
   if(n.is_positive() && is_power_of_2(mod)) {
169
305
      remainder = (n.word_at(0) & (mod - 1));
170
305
   } else {
171
280
      divide_precomp redc_mod(mod);
172
280
      const size_t sw = n.sig_words();
173
764
      for(size_t i = sw; i > 0; --i) {
174
484
         remainder = redc_mod.vartime_mod_2to1(remainder, n.word_at(i - 1));
175
484
      }
176
280
   }
177
178
585
   if(remainder != 0 && n.sign() == BigInt::Negative) {
179
0
      return mod - remainder;
180
0
   }
181
585
   return remainder;
182
585
}
183
184
/*
185
* Left Shift Operator
186
*/
187
189
BigInt operator<<(const BigInt& x, size_t shift) {
188
189
   if(x.is_zero()) {
189
0
      return BigInt::zero();
190
0
   }
191
192
189
   const size_t x_sw = x.sig_words();
193
194
189
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
195
189
   BigInt y = BigInt::with_capacity(new_size);
196
189
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
197
189
   y.set_sign(x.sign());
198
189
   return y;
199
189
}
200
201
/*
202
* Right Shift Operator
203
*/
204
13.5k
BigInt operator>>(const BigInt& x, size_t shift) {
205
13.5k
   const size_t shift_words = shift / WordInfo<word>::bits;
206
13.5k
   const size_t x_sw = x.sig_words();
207
208
13.5k
   if(shift_words >= x_sw) {
209
0
      return BigInt::zero();
210
0
   }
211
212
13.5k
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
213
13.5k
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
214
215
13.5k
   if(x.is_negative() && y.is_zero()) {
216
0
      y.set_sign(BigInt::Positive);
217
13.5k
   } else {
218
13.5k
      y.set_sign(x.sign());
219
13.5k
   }
220
221
13.5k
   return y;
222
13.5k
}
223
224
}  // namespace Botan