Coverage Report

Created: 2024-06-28 06:39

/src/botan/src/lib/math/bigint/big_ops2.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 1999-2007,2018 Jack Lloyd
3
*     2016 Matthias Gierlings
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/bigint.h>
9
10
#include <botan/internal/bit_ops.h>
11
#include <botan/internal/mp_core.h>
12
#include <algorithm>
13
14
namespace Botan {
15
16
11.9M
BigInt& BigInt::add(const word y[], size_t y_words, Sign y_sign) {
17
11.9M
   const size_t x_sw = sig_words();
18
19
11.9M
   grow_to(std::max(x_sw, y_words) + 1);
20
21
11.9M
   if(sign() == y_sign) {
22
11.8M
      bigint_add2(mutable_data(), size() - 1, y, y_words);
23
11.8M
   } else {
24
144k
      const int32_t relative_size = bigint_cmp(_data(), x_sw, y, y_words);
25
26
144k
      if(relative_size >= 0) {
27
         // *this >= y
28
135k
         bigint_sub2(mutable_data(), x_sw, y, y_words);
29
135k
      } else {
30
         // *this < y
31
8.28k
         bigint_sub2_rev(mutable_data(), y, y_words);
32
8.28k
      }
33
34
      //this->sign_fixup(relative_size, y_sign);
35
144k
      if(relative_size < 0) {
36
8.28k
         set_sign(y_sign);
37
135k
      } else if(relative_size == 0) {
38
229
         set_sign(Positive);
39
229
      }
40
144k
   }
41
42
11.9M
   return (*this);
43
11.9M
}
44
45
1.00M
BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
46
1.00M
   if(this->is_negative() || s.is_negative() || mod.is_negative()) {
47
0
      throw Invalid_Argument("BigInt::mod_add expects all arguments are positive");
48
0
   }
49
50
1.00M
   BOTAN_DEBUG_ASSERT(*this < mod);
51
1.00M
   BOTAN_DEBUG_ASSERT(s < mod);
52
53
   /*
54
   t + s or t + s - p == t - (p - s)
55
56
   So first compute ws = p - s
57
58
   Then compute t + s and t - ws
59
60
   If t - ws does not borrow, then that is the correct valued
61
   */
62
63
1.00M
   const size_t mod_sw = mod.sig_words();
64
1.00M
   BOTAN_ARG_CHECK(mod_sw > 0, "BigInt::mod_add modulus must be positive");
65
66
1.00M
   this->grow_to(mod_sw);
67
1.00M
   s.grow_to(mod_sw);
68
69
   // First mod_sw for p - s, 2*mod_sw for bigint_addsub workspace
70
1.00M
   if(ws.size() < 3 * mod_sw) {
71
643k
      ws.resize(3 * mod_sw);
72
643k
   }
73
74
1.00M
   word borrow = bigint_sub3(&ws[0], mod._data(), mod_sw, s._data(), mod_sw);
75
1.00M
   BOTAN_DEBUG_ASSERT(borrow == 0);
76
1.00M
   BOTAN_UNUSED(borrow);
77
78
   // Compute t - ws
79
1.00M
   borrow = bigint_sub3(&ws[mod_sw], this->_data(), mod_sw, &ws[0], mod_sw);
80
81
   // Compute t + s
82
1.00M
   bigint_add3_nc(&ws[mod_sw * 2], this->_data(), mod_sw, s._data(), mod_sw);
83
84
1.00M
   CT::conditional_copy_mem(borrow, &ws[0], &ws[mod_sw * 2], &ws[mod_sw], mod_sw);
85
1.00M
   set_words(&ws[0], mod_sw);
86
87
1.00M
   return (*this);
88
1.00M
}
89
90
11.8M
BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
91
11.8M
   if(this->is_negative() || s.is_negative() || mod.is_negative()) {
92
0
      throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive");
93
0
   }
94
95
   // We are assuming in this function that *this and s are no more than mod_sw words long
96
11.8M
   BOTAN_DEBUG_ASSERT(*this < mod);
97
11.8M
   BOTAN_DEBUG_ASSERT(s < mod);
98
99
11.8M
   const size_t mod_sw = mod.sig_words();
100
101
11.8M
   this->grow_to(mod_sw);
102
11.8M
   s.grow_to(mod_sw);
103
104
11.8M
   if(ws.size() < mod_sw) {
105
46
      ws.resize(mod_sw);
106
46
   }
107
108
11.8M
   if(mod_sw == 4) {
109
2.87M
      bigint_mod_sub_n<4>(mutable_data(), s._data(), mod._data(), ws.data());
110
9.02M
   } else if(mod_sw == 6) {
111
1.48M
      bigint_mod_sub_n<6>(mutable_data(), s._data(), mod._data(), ws.data());
112
7.54M
   } else {
113
7.54M
      bigint_mod_sub(mutable_data(), s._data(), mod._data(), mod_sw, ws.data());
114
7.54M
   }
115
116
11.8M
   return (*this);
117
11.8M
}
118
119
4.13M
BigInt& BigInt::mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws) {
120
4.13M
   BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive");
121
4.13M
   BOTAN_ARG_CHECK(y < 16, "y too large");
122
123
4.13M
   BOTAN_DEBUG_ASSERT(*this < mod);
124
125
4.13M
   *this *= static_cast<word>(y);
126
4.13M
   this->reduce_below(mod, ws);
127
4.13M
   return (*this);
128
4.13M
}
129
130
3.50M
BigInt& BigInt::rev_sub(const word y[], size_t y_sw, secure_vector<word>& ws) {
131
3.50M
   if(this->sign() != BigInt::Positive) {
132
0
      throw Invalid_State("BigInt::sub_rev requires this is positive");
133
0
   }
134
135
3.50M
   const size_t x_sw = this->sig_words();
136
137
3.50M
   ws.resize(std::max(x_sw, y_sw));
138
3.50M
   clear_mem(ws.data(), ws.size());
139
140
3.50M
   const int32_t relative_size = bigint_sub_abs(ws.data(), _data(), x_sw, y, y_sw);
141
142
3.50M
   this->cond_flip_sign(relative_size > 0);
143
3.50M
   this->swap_reg(ws);
144
145
3.50M
   return (*this);
146
3.50M
}
147
148
/*
149
* Multiplication Operator
150
*/
151
159
BigInt& BigInt::operator*=(const BigInt& y) {
152
159
   secure_vector<word> ws;
153
159
   return this->mul(y, ws);
154
159
}
155
156
7.00M
BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws) {
157
7.00M
   const size_t x_sw = sig_words();
158
7.00M
   const size_t y_sw = y.sig_words();
159
7.00M
   set_sign((sign() == y.sign()) ? Positive : Negative);
160
161
7.00M
   if(x_sw == 0 || y_sw == 0) {
162
968k
      clear();
163
968k
      set_sign(Positive);
164
6.04M
   } else if(x_sw == 1 && y_sw) {
165
2.78M
      grow_to(y_sw + 1);
166
2.78M
      bigint_linmul3(mutable_data(), y._data(), y_sw, word_at(0));
167
3.25M
   } else if(y_sw == 1 && x_sw) {
168
25
      word carry = bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
169
25
      set_word_at(x_sw, carry);
170
3.25M
   } else {
171
3.25M
      const size_t new_size = x_sw + y_sw + 1;
172
3.25M
      ws.resize(new_size);
173
3.25M
      secure_vector<word> z_reg(new_size);
174
175
3.25M
      bigint_mul(z_reg.data(), z_reg.size(), _data(), size(), x_sw, y._data(), y.size(), y_sw, ws.data(), ws.size());
176
177
3.25M
      this->swap_reg(z_reg);
178
3.25M
   }
179
180
7.00M
   return (*this);
181
7.00M
}
182
183
1.19M
BigInt& BigInt::square(secure_vector<word>& ws) {
184
1.19M
   const size_t sw = sig_words();
185
186
1.19M
   secure_vector<word> z(2 * sw);
187
1.19M
   ws.resize(z.size());
188
189
1.19M
   bigint_sqr(z.data(), z.size(), _data(), size(), sw, ws.data(), ws.size());
190
191
1.19M
   swap_reg(z);
192
1.19M
   set_sign(BigInt::Positive);
193
194
1.19M
   return (*this);
195
1.19M
}
196
197
28.3M
BigInt& BigInt::operator*=(word y) {
198
28.3M
   if(y == 0) {
199
0
      clear();
200
0
      set_sign(Positive);
201
0
   }
202
203
28.3M
   const word carry = bigint_linmul2(mutable_data(), size(), y);
204
28.3M
   set_word_at(size(), carry);
205
206
28.3M
   return (*this);
207
28.3M
}
208
209
/*
210
* Division Operator
211
*/
212
56
BigInt& BigInt::operator/=(const BigInt& y) {
213
56
   if(y.sig_words() == 1 && is_power_of_2(y.word_at(0))) {
214
21
      (*this) >>= (y.bits() - 1);
215
35
   } else {
216
35
      (*this) = (*this) / y;
217
35
   }
218
56
   return (*this);
219
56
}
220
221
/*
222
* Modulo Operator
223
*/
224
136k
BigInt& BigInt::operator%=(const BigInt& mod) {
225
136k
   return (*this = (*this) % mod);
226
136k
}
227
228
/*
229
* Modulo Operator
230
*/
231
175
word BigInt::operator%=(word mod) {
232
175
   if(mod == 0) {
233
25
      throw Invalid_Argument("BigInt::operator%= divide by zero");
234
25
   }
235
236
150
   word remainder = 0;
237
238
150
   if(is_power_of_2(mod)) {
239
40
      remainder = (word_at(0) & (mod - 1));
240
110
   } else {
241
110
      const size_t sw = sig_words();
242
473
      for(size_t i = sw; i > 0; --i) {
243
363
         remainder = bigint_modop_vartime(remainder, word_at(i - 1), mod);
244
363
      }
245
110
   }
246
247
150
   if(remainder && sign() == BigInt::Negative) {
248
0
      remainder = mod - remainder;
249
0
   }
250
251
150
   m_data.set_to_zero();
252
150
   m_data.set_word_at(0, remainder);
253
150
   set_sign(BigInt::Positive);
254
150
   return remainder;
255
175
}
256
257
/*
258
* Left Shift Operator
259
*/
260
220k
BigInt& BigInt::operator<<=(size_t shift) {
261
220k
   const size_t sw = sig_words();
262
220k
   const size_t new_size = sw + (shift + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS;
263
264
220k
   m_data.grow_to(new_size);
265
266
220k
   bigint_shl1(m_data.mutable_data(), new_size, sw, shift);
267
268
220k
   return (*this);
269
220k
}
270
271
/*
272
* Right Shift Operator
273
*/
274
8.58M
BigInt& BigInt::operator>>=(size_t shift) {
275
8.58M
   bigint_shr1(m_data.mutable_data(), m_data.size(), shift);
276
277
8.58M
   if(is_negative() && is_zero()) {
278
0
      set_sign(Positive);
279
0
   }
280
281
8.58M
   return (*this);
282
8.58M
}
283
284
}  // namespace Botan