Coverage Report

Created: 2026-07-25 06:49

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
80.4k
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sign y_sign) {
20
80.4k
   const size_t x_sw = x.sig_words();
21
22
80.4k
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
23
24
80.4k
   if(x.sign() == y_sign) {
25
44.7k
      bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_words);
26
44.7k
      z.set_sign(x.sign());
27
44.7k
   } else {
28
35.7k
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x._data(), x_sw, y, y_words);
29
30
      //z.sign_fixup(relative_size, y_sign);
31
35.7k
      if(relative_size < 0) {
32
5.65k
         z.set_sign(y_sign);
33
30.0k
      } else if(relative_size == 0) {
34
68
         z.set_sign(BigInt::Positive);
35
30.0k
      } else {
36
30.0k
         z.set_sign(x.sign());
37
30.0k
      }
38
35.7k
   }
39
40
80.4k
   return z;
41
80.4k
}
42
43
/*
44
* Multiplication Operator
45
*/
46
2.24k
BigInt operator*(const BigInt& x, const BigInt& y) {
47
2.24k
   const size_t x_sw = x.sig_words();
48
2.24k
   const size_t y_sw = y.sig_words();
49
50
2.24k
   BigInt z = BigInt::with_capacity(x.size() + y.size());
51
52
2.24k
   if(x_sw == 1 && y_sw) {
53
479
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
54
1.76k
   } else if(y_sw == 1 && x_sw) {
55
67
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
56
1.69k
   } else if(x_sw && y_sw) {
57
1.63k
      secure_vector<word> workspace(z.size());
58
59
1.63k
      bigint_mul(z.mutable_data(),
60
1.63k
                 z.size(),
61
1.63k
                 x._data(),
62
1.63k
                 x.size(),
63
1.63k
                 x_sw,
64
1.63k
                 y._data(),
65
1.63k
                 y.size(),
66
1.63k
                 y_sw,
67
1.63k
                 workspace.data(),
68
1.63k
                 workspace.size());
69
1.63k
   }
70
71
2.24k
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
72
73
2.24k
   return z;
74
2.24k
}
75
76
/*
77
* Multiplication Operator
78
*/
79
78.8k
BigInt operator*(const BigInt& x, word y) {
80
78.8k
   const size_t x_sw = x.sig_words();
81
82
78.8k
   BigInt z = BigInt::with_capacity(x_sw + 1);
83
84
78.8k
   if(x_sw && y) {
85
78.6k
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
86
78.6k
      z.set_sign(x.sign());
87
78.6k
   }
88
89
78.8k
   return z;
90
78.8k
}
91
92
/*
93
* Division Operator
94
*/
95
1.56k
BigInt operator/(const BigInt& x, const BigInt& y) {
96
1.56k
   if(y.sig_words() == 1) {
97
144
      return x / y.word_at(0);
98
144
   }
99
100
1.42k
   BigInt q, r;
101
1.42k
   vartime_divide(x, y, q, r);
102
1.42k
   return q;
103
1.56k
}
104
105
/*
106
* Division Operator
107
*/
108
64.3k
BigInt operator/(const BigInt& x, word y) {
109
64.3k
   if(y == 0) {
110
0
      throw Invalid_Argument("BigInt::operator/ divide by zero");
111
0
   }
112
113
64.3k
   BigInt q;
114
64.3k
   word r;
115
64.3k
   ct_divide_word(x, y, q, r);
116
64.3k
   return q;
117
64.3k
}
118
119
/*
120
* Modulo Operator
121
*/
122
64.5k
BigInt operator%(const BigInt& n, const BigInt& mod) {
123
64.5k
   if(mod.is_zero()) {
124
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
125
0
   }
126
64.5k
   if(mod.is_negative()) {
127
0
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
128
0
   }
129
64.5k
   if(n.is_positive() && mod.is_positive() && n < mod) {
130
617
      return n;
131
617
   }
132
133
63.9k
   if(mod.sig_words() == 1) {
134
5.87k
      return BigInt::from_word(n % mod.word_at(0));
135
5.87k
   }
136
137
58.0k
   BigInt q, r;
138
58.0k
   vartime_divide(n, mod, q, r);
139
58.0k
   return r;
140
63.9k
}
141
142
/*
143
* Modulo Operator
144
*/
145
152k
word operator%(const BigInt& n, word mod) {
146
152k
   if(mod == 0) {
147
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
148
0
   }
149
150
152k
   if(mod == 1) {
151
0
      return 0;
152
0
   }
153
154
152k
   word remainder = 0;
155
156
152k
   if(is_power_of_2(mod)) {
157
146k
      remainder = (n.word_at(0) & (mod - 1));
158
146k
   } else {
159
5.87k
      const size_t sw = n.sig_words();
160
12.9k
      for(size_t i = sw; i > 0; --i) {
161
7.05k
         remainder = bigint_modop_vartime(remainder, n.word_at(i - 1), mod);
162
7.05k
      }
163
5.87k
   }
164
165
152k
   if(remainder && n.sign() == BigInt::Negative) {
166
0
      return mod - remainder;
167
0
   }
168
152k
   return remainder;
169
152k
}
170
171
/*
172
* Left Shift Operator
173
*/
174
59.5k
BigInt operator<<(const BigInt& x, size_t shift) {
175
59.5k
   const size_t x_sw = x.sig_words();
176
177
59.5k
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
178
59.5k
   BigInt y = BigInt::with_capacity(new_size);
179
59.5k
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
180
59.5k
   y.set_sign(x.sign());
181
59.5k
   return y;
182
59.5k
}
183
184
/*
185
* Right Shift Operator
186
*/
187
264
BigInt operator>>(const BigInt& x, size_t shift) {
188
264
   const size_t shift_words = shift / WordInfo<word>::bits;
189
264
   const size_t x_sw = x.sig_words();
190
191
264
   if(shift_words >= x_sw) {
192
58
      return BigInt::zero();
193
58
   }
194
195
206
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
196
206
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
197
198
206
   if(x.is_negative() && y.is_zero()) {
199
0
      y.set_sign(BigInt::Positive);
200
206
   } else {
201
206
      y.set_sign(x.sign());
202
206
   }
203
204
206
   return y;
205
264
}
206
207
}  // namespace Botan