Coverage Report

Created: 2026-06-16 07:05

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