Coverage Report

Created: 2022-05-14 06:06

/src/botan/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
#include <botan/internal/divide.h>
11
#include <botan/internal/mp_core.h>
12
#include <botan/internal/bit_ops.h>
13
#include <algorithm>
14
15
namespace Botan {
16
17
//static
18
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sign y_sign)
19
3.85M
   {
20
3.85M
   const size_t x_sw = x.sig_words();
21
22
3.85M
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
23
24
3.85M
   if(x.sign() == y_sign)
25
1.96M
      {
26
1.96M
      bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
27
1.96M
      z.set_sign(x.sign());
28
1.96M
      }
29
1.88M
   else
30
1.88M
      {
31
1.88M
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x.data(), x_sw, y, y_words);
32
33
      //z.sign_fixup(relative_size, y_sign);
34
1.88M
      if(relative_size < 0)
35
226k
         z.set_sign(y_sign);
36
1.66M
      else if(relative_size == 0)
37
647
         z.set_sign(BigInt::Positive);
38
1.66M
      else
39
1.66M
         z.set_sign(x.sign());
40
1.88M
      }
41
42
3.85M
   return z;
43
3.85M
   }
44
45
/*
46
* Multiplication Operator
47
*/
48
BigInt operator*(const BigInt& x, const BigInt& y)
49
2.04M
   {
50
2.04M
   const size_t x_sw = x.sig_words();
51
2.04M
   const size_t y_sw = y.sig_words();
52
53
2.04M
   BigInt z = BigInt::with_capacity(x.size() + y.size());
54
55
2.04M
   if(x_sw == 1 && y_sw)
56
470k
      bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
57
1.57M
   else if(y_sw == 1 && x_sw)
58
387k
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
59
1.18M
   else if(x_sw && y_sw)
60
1.12M
      {
61
1.12M
      secure_vector<word> workspace(z.size());
62
63
1.12M
      bigint_mul(z.mutable_data(), z.size(),
64
1.12M
                 x.data(), x.size(), x_sw,
65
1.12M
                 y.data(), y.size(), y_sw,
66
1.12M
                 workspace.data(), workspace.size());
67
1.12M
      }
68
69
2.04M
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
70
71
2.04M
   return z;
72
2.04M
   }
73
74
/*
75
* Multiplication Operator
76
*/
77
BigInt operator*(const BigInt& x, word y)
78
6.61M
   {
79
6.61M
   const size_t x_sw = x.sig_words();
80
81
6.61M
   BigInt z = BigInt::with_capacity(x_sw + 1);
82
83
6.61M
   if(x_sw && y)
84
3.30M
      {
85
3.30M
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y);
86
3.30M
      z.set_sign(x.sign());
87
3.30M
      }
88
89
6.61M
   return z;
90
6.61M
   }
91
92
/*
93
* Division Operator
94
*/
95
BigInt operator/(const BigInt& x, const BigInt& y)
96
613
   {
97
613
   if(y.sig_words() == 1)
98
248
      {
99
248
      return x / y.word_at(0);
100
248
      }
101
102
365
   BigInt q, r;
103
365
   vartime_divide(x, y, q, r);
104
365
   return q;
105
613
   }
106
107
/*
108
* Division Operator
109
*/
110
BigInt operator/(const BigInt& x, word y)
111
3.50M
   {
112
3.50M
   if(y == 0)
113
0
      throw Invalid_Argument("BigInt::operator/ divide by zero");
114
115
3.50M
   BigInt q;
116
3.50M
   word r;
117
3.50M
   ct_divide_word(x, y, q, r);
118
3.50M
   return q;
119
3.50M
   }
120
121
/*
122
* Modulo Operator
123
*/
124
BigInt operator%(const BigInt& n, const BigInt& mod)
125
3.65M
   {
126
3.65M
   if(mod.is_zero())
127
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
128
3.65M
   if(mod.is_negative())
129
0
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
130
3.65M
   if(n.is_positive() && mod.is_positive() && n < mod)
131
99.5k
      return n;
132
133
3.55M
   if(mod.sig_words() == 1)
134
625k
      {
135
625k
      return BigInt::from_word(n % mod.word_at(0));
136
625k
      }
137
138
2.92M
   BigInt q, r;
139
2.92M
   vartime_divide(n, mod, q, r);
140
2.92M
   return r;
141
3.55M
   }
142
143
/*
144
* Modulo Operator
145
*/
146
word operator%(const BigInt& n, word mod)
147
8.63M
   {
148
8.63M
   if(mod == 0)
149
0
      throw Invalid_Argument("BigInt::operator% divide by zero");
150
151
8.63M
   if(mod == 1)
152
729
      return 0;
153
154
8.63M
   word remainder = 0;
155
156
8.63M
   if(is_power_of_2(mod))
157
8.01M
      {
158
8.01M
      remainder = (n.word_at(0) & (mod - 1));
159
8.01M
      }
160
624k
   else
161
624k
      {
162
624k
      const size_t sw = n.sig_words();
163
1.32M
      for(size_t i = sw; i > 0; --i)
164
702k
         {
165
702k
         remainder = bigint_modop(remainder, n.word_at(i-1), mod);
166
702k
         }
167
624k
      }
168
169
8.63M
   if(remainder && n.sign() == BigInt::Negative)
170
494
      return mod - remainder;
171
8.63M
   return remainder;
172
8.63M
   }
173
174
/*
175
* Left Shift Operator
176
*/
177
BigInt operator<<(const BigInt& x, size_t shift)
178
2.93M
   {
179
2.93M
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS,
180
2.93M
                shift_bits  = shift % BOTAN_MP_WORD_BITS;
181
182
2.93M
   const size_t x_sw = x.sig_words();
183
184
2.93M
   BigInt y = BigInt::with_capacity(x_sw + shift_words + (shift_bits ? 1 : 0));
185
2.93M
   bigint_shl2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);
186
2.93M
   y.set_sign(x.sign());
187
2.93M
   return y;
188
2.93M
   }
189
190
/*
191
* Right Shift Operator
192
*/
193
BigInt operator>>(const BigInt& x, size_t shift)
194
22.9k
   {
195
22.9k
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
196
22.9k
   const size_t shift_bits  = shift % BOTAN_MP_WORD_BITS;
197
22.9k
   const size_t x_sw = x.sig_words();
198
199
22.9k
   if(shift_words >= x_sw)
200
0
      return BigInt::zero();
201
202
22.9k
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
203
22.9k
   bigint_shr2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);
204
205
22.9k
   if(x.is_negative() && y.is_zero())
206
0
      y.set_sign(BigInt::Positive);
207
22.9k
   else
208
22.9k
      y.set_sign(x.sign());
209
210
22.9k
   return y;
211
22.9k
   }
212
213
}