Coverage Report

Created: 2025-11-24 06:12

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
736k
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sign y_sign) {
20
736k
   const size_t x_sw = x.sig_words();
21
22
736k
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
23
24
736k
   if(x.sign() == y_sign) {
25
699k
      bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
26
699k
      z.set_sign(x.sign());
27
699k
   } else {
28
36.5k
      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.5k
      if(relative_size < 0) {
32
34.4k
         z.set_sign(y_sign);
33
34.4k
      } else if(relative_size == 0) {
34
491
         z.set_sign(BigInt::Positive);
35
1.64k
      } else {
36
1.64k
         z.set_sign(x.sign());
37
1.64k
      }
38
36.5k
   }
39
40
736k
   return z;
41
736k
}
42
43
/*
44
* Multiplication Operator
45
*/
46
190k
BigInt operator*(const BigInt& x, const BigInt& y) {
47
190k
   const size_t x_sw = x.sig_words();
48
190k
   const size_t y_sw = y.sig_words();
49
50
190k
   BigInt z = BigInt::with_capacity(x.size() + y.size());
51
52
190k
   if(x_sw == 1 && y_sw) {
53
8.56k
      bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
54
181k
   } else if(y_sw == 1 && x_sw) {
55
619
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
56
180k
   } else if(x_sw && y_sw) {
57
179k
      secure_vector<word> workspace(z.size());
58
59
179k
      bigint_mul(z.mutable_data(),
60
179k
                 z.size(),
61
179k
                 x.data(),
62
179k
                 x.size(),
63
179k
                 x_sw,
64
179k
                 y.data(),
65
179k
                 y.size(),
66
179k
                 y_sw,
67
179k
                 workspace.data(),
68
179k
                 workspace.size());
69
179k
   }
70
71
190k
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
72
73
190k
   return z;
74
190k
}
75
76
/*
77
* Multiplication Operator
78
*/
79
464k
BigInt operator*(const BigInt& x, word y) {
80
464k
   const size_t x_sw = x.sig_words();
81
82
464k
   BigInt z = BigInt::with_capacity(x_sw + 1);
83
84
464k
   if(x_sw && y) {
85
232k
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y);
86
232k
      z.set_sign(x.sign());
87
232k
   }
88
89
464k
   return z;
90
464k
}
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
581
      return n;
131
581
   }
132
133
12.4k
   if(mod.sig_words() == 1) {
134
0
      return BigInt::from_word(n % mod.word_at(0));
135
0
   }
136
137
12.4k
   BigInt q, r;
138
12.4k
   vartime_divide(n, mod, q, r);
139
12.4k
   return r;
140
12.4k
}
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.4k
BigInt operator<<(const BigInt& x, size_t shift) {
175
12.4k
   const size_t x_sw = x.sig_words();
176
177
12.4k
   const size_t new_size = x_sw + (shift + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS;
178
12.4k
   BigInt y = BigInt::with_capacity(new_size);
179
12.4k
   bigint_shl2(y.mutable_data(), x.data(), x_sw, shift);
180
12.4k
   y.set_sign(x.sign());
181
12.4k
   return y;
182
12.4k
}
183
184
/*
185
* Right Shift Operator
186
*/
187
1.98k
BigInt operator>>(const BigInt& x, size_t shift) {
188
1.98k
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
189
1.98k
   const size_t x_sw = x.sig_words();
190
191
1.98k
   if(shift_words >= x_sw) {
192
0
      return BigInt::zero();
193
0
   }
194
195
1.98k
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
196
1.98k
   bigint_shr2(y.mutable_data(), x.data(), x_sw, shift);
197
198
1.98k
   if(x.is_negative() && y.is_zero()) {
199
0
      y.set_sign(BigInt::Positive);
200
1.98k
   } else {
201
1.98k
      y.set_sign(x.sign());
202
1.98k
   }
203
204
1.98k
   return y;
205
1.98k
}
206
207
}  // namespace Botan