Coverage Report

Created: 2025-08-29 06:23

/src/Botan-3.4.0/src/lib/math/bigint/big_ops3.cpp
Line
Count
Source (jump to first uncovered line)
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
634k
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sign y_sign) {
20
634k
   const size_t x_sw = x.sig_words();
21
22
634k
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
23
24
634k
   if(x.sign() == y_sign) {
25
597k
      bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
26
597k
      z.set_sign(x.sign());
27
597k
   } else {
28
36.2k
      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
36.2k
      if(relative_size < 0) {
32
34.7k
         z.set_sign(y_sign);
33
34.7k
      } else if(relative_size == 0) {
34
517
         z.set_sign(BigInt::Positive);
35
1.02k
      } else {
36
1.02k
         z.set_sign(x.sign());
37
1.02k
      }
38
36.2k
   }
39
40
634k
   return z;
41
634k
}
42
43
/*
44
* Multiplication Operator
45
*/
46
192k
BigInt operator*(const BigInt& x, const BigInt& y) {
47
192k
   const size_t x_sw = x.sig_words();
48
192k
   const size_t y_sw = y.sig_words();
49
50
192k
   BigInt z = BigInt::with_capacity(x.size() + y.size());
51
52
192k
   if(x_sw == 1 && y_sw) {
53
7.42k
      bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
54
185k
   } else if(y_sw == 1 && x_sw) {
55
779
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
56
184k
   } else if(x_sw && y_sw) {
57
183k
      secure_vector<word> workspace(z.size());
58
59
183k
      bigint_mul(z.mutable_data(),
60
183k
                 z.size(),
61
183k
                 x.data(),
62
183k
                 x.size(),
63
183k
                 x_sw,
64
183k
                 y.data(),
65
183k
                 y.size(),
66
183k
                 y_sw,
67
183k
                 workspace.data(),
68
183k
                 workspace.size());
69
183k
   }
70
71
192k
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
72
73
192k
   return z;
74
192k
}
75
76
/*
77
* Multiplication Operator
78
*/
79
472k
BigInt operator*(const BigInt& x, word y) {
80
472k
   const size_t x_sw = x.sig_words();
81
82
472k
   BigInt z = BigInt::with_capacity(x_sw + 1);
83
84
472k
   if(x_sw && y) {
85
236k
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y);
86
236k
      z.set_sign(x.sign());
87
236k
   }
88
89
472k
   return z;
90
472k
}
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
13.0k
BigInt operator%(const BigInt& n, const BigInt& mod) {
123
13.0k
   if(mod.is_zero()) {
124
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
125
0
   }
126
13.0k
   if(mod.is_negative()) {
127
0
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
128
0
   }
129
13.0k
   if(n.is_positive() && mod.is_positive() && n < mod) {
130
326
      return n;
131
326
   }
132
133
12.7k
   if(mod.sig_words() == 1) {
134
0
      return BigInt::from_word(n % mod.word_at(0));
135
0
   }
136
137
12.7k
   BigInt q, r;
138
12.7k
   vartime_divide(n, mod, q, r);
139
12.7k
   return r;
140
12.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
12.7k
BigInt operator<<(const BigInt& x, size_t shift) {
175
12.7k
   const size_t x_sw = x.sig_words();
176
177
12.7k
   const size_t new_size = x_sw + (shift + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS;
178
12.7k
   BigInt y = BigInt::with_capacity(new_size);
179
12.7k
   bigint_shl2(y.mutable_data(), x.data(), x_sw, shift);
180
12.7k
   y.set_sign(x.sign());
181
12.7k
   return y;
182
12.7k
}
183
184
/*
185
* Right Shift Operator
186
*/
187
1.25k
BigInt operator>>(const BigInt& x, size_t shift) {
188
1.25k
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
189
1.25k
   const size_t x_sw = x.sig_words();
190
191
1.25k
   if(shift_words >= x_sw) {
192
0
      return BigInt::zero();
193
0
   }
194
195
1.25k
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
196
1.25k
   bigint_shr2(y.mutable_data(), x.data(), x_sw, shift);
197
198
1.25k
   if(x.is_negative() && y.is_zero()) {
199
0
      y.set_sign(BigInt::Positive);
200
1.25k
   } else {
201
1.25k
      y.set_sign(x.sign());
202
1.25k
   }
203
204
1.25k
   return y;
205
1.25k
}
206
207
}  // namespace Botan