Coverage Report

Created: 2026-01-16 06:49

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