• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.1
/src/lib/math/bigint/big_ops2.cpp
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
BigInt& BigInt::add(const word y[], size_t y_words, Sign y_sign) {
12,939,743✔
17
   const size_t x_sw = sig_words();
12,939,743✔
18

19
   grow_to(std::max(x_sw, y_words) + 1);
22,160,446✔
20

21
   if(sign() == y_sign) {
12,939,743✔
22
      bigint_add2(mutable_data(), size() - 1, y, y_words);
11,907,495✔
23
   } else {
24
      const int32_t relative_size = bigint_cmp(data(), x_sw, y, y_words);
1,032,248✔
25

26
      if(relative_size >= 0) {
1,032,248✔
27
         // *this >= y
28
         bigint_sub2(mutable_data(), x_sw, y, y_words);
969,459✔
29
      } else {
30
         // *this < y
31
         bigint_sub2_rev(mutable_data(), y, y_words);
62,789✔
32
      }
33

34
      //this->sign_fixup(relative_size, y_sign);
35
      if(relative_size < 0)
1,032,248✔
36
         set_sign(y_sign);
62,789✔
37
      else if(relative_size == 0)
969,459✔
38
         set_sign(Positive);
302✔
39
   }
40

41
   return (*this);
12,939,743✔
42
}
43

44
BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
5,983,908✔
45
   if(this->is_negative() || s.is_negative() || mod.is_negative())
5,983,908✔
46
      throw Invalid_Argument("BigInt::mod_add expects all arguments are positive");
×
47

48
   BOTAN_DEBUG_ASSERT(*this < mod);
5,983,908✔
49
   BOTAN_DEBUG_ASSERT(s < mod);
5,983,908✔
50

51
   /*
52
   t + s or t + s - p == t - (p - s)
53

54
   So first compute ws = p - s
55

56
   Then compute t + s and t - ws
57

58
   If t - ws does not borrow, then that is the correct valued
59
   */
60

61
   const size_t mod_sw = mod.sig_words();
5,983,908✔
62
   BOTAN_ARG_CHECK(mod_sw > 0, "BigInt::mod_add modulus must be positive");
5,983,908✔
63

64
   this->grow_to(mod_sw);
5,983,908✔
65
   s.grow_to(mod_sw);
5,983,908✔
66

67
   // First mod_sw for p - s, 2*mod_sw for bigint_addsub workspace
68
   if(ws.size() < 3 * mod_sw)
5,983,908✔
69
      ws.resize(3 * mod_sw);
3,695,159✔
70

71
   word borrow = bigint_sub3(&ws[0], mod.data(), mod_sw, s.data(), mod_sw);
5,983,908✔
72
   BOTAN_DEBUG_ASSERT(borrow == 0);
5,983,908✔
73
   BOTAN_UNUSED(borrow);
5,983,908✔
74

75
   // Compute t - ws
76
   borrow = bigint_sub3(&ws[mod_sw], this->data(), mod_sw, &ws[0], mod_sw);
5,983,908✔
77

78
   // Compute t + s
79
   bigint_add3_nc(&ws[mod_sw * 2], this->data(), mod_sw, s.data(), mod_sw);
5,983,908✔
80

81
   CT::conditional_copy_mem(borrow, &ws[0], &ws[mod_sw * 2], &ws[mod_sw], mod_sw);
5,983,908✔
82
   set_words(&ws[0], mod_sw);
5,983,908✔
83

84
   return (*this);
5,983,908✔
85
}
86

87
BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
54,817,441✔
88
   if(this->is_negative() || s.is_negative() || mod.is_negative())
54,817,441✔
89
      throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive");
×
90

91
   // We are assuming in this function that *this and s are no more than mod_sw words long
92
   BOTAN_DEBUG_ASSERT(*this < mod);
54,817,441✔
93
   BOTAN_DEBUG_ASSERT(s < mod);
54,817,441✔
94

95
   const size_t mod_sw = mod.sig_words();
54,817,441✔
96

97
   this->grow_to(mod_sw);
54,817,441✔
98
   s.grow_to(mod_sw);
54,817,441✔
99

100
   if(ws.size() < mod_sw)
54,817,441✔
101
      ws.resize(mod_sw);
×
102

103
   if(mod_sw == 4)
54,817,441✔
104
      bigint_mod_sub_n<4>(mutable_data(), s.data(), mod.data(), ws.data());
24,202,483✔
105
   else if(mod_sw == 6)
30,614,958✔
106
      bigint_mod_sub_n<6>(mutable_data(), s.data(), mod.data(), ws.data());
16,060,248✔
107
   else
108
      bigint_mod_sub(mutable_data(), s.data(), mod.data(), mod_sw, ws.data());
14,554,710✔
109

110
   return (*this);
54,817,441✔
111
}
112

113
BigInt& BigInt::mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws) {
24,420,796✔
114
   BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive");
24,420,796✔
115
   BOTAN_ARG_CHECK(y < 16, "y too large");
24,420,796✔
116

117
   BOTAN_DEBUG_ASSERT(*this < mod);
24,420,796✔
118

119
   *this *= static_cast<word>(y);
24,420,796✔
120
   this->reduce_below(mod, ws);
24,420,796✔
121
   return (*this);
24,420,796✔
122
}
123

124
BigInt& BigInt::rev_sub(const word y[], size_t y_sw, secure_vector<word>& ws) {
9,203,191✔
125
   if(this->sign() != BigInt::Positive)
9,203,191✔
126
      throw Invalid_State("BigInt::sub_rev requires this is positive");
×
127

128
   const size_t x_sw = this->sig_words();
9,203,191✔
129

130
   ws.resize(std::max(x_sw, y_sw));
12,412,826✔
131
   clear_mem(ws.data(), ws.size());
9,203,191✔
132

133
   const int32_t relative_size = bigint_sub_abs(ws.data(), data(), x_sw, y, y_sw);
9,203,191✔
134

135
   this->cond_flip_sign(relative_size > 0);
9,203,191✔
136
   this->swap_reg(ws);
9,203,191✔
137

138
   return (*this);
9,203,191✔
139
}
140

141
/*
142
* Multiplication Operator
143
*/
144
BigInt& BigInt::operator*=(const BigInt& y) {
612✔
145
   secure_vector<word> ws;
612✔
146
   return this->mul(y, ws);
612✔
147
}
612✔
148

149
BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws) {
18,411,531✔
150
   const size_t x_sw = sig_words();
18,411,531✔
151
   const size_t y_sw = y.sig_words();
18,411,531✔
152
   set_sign((sign() == y.sign()) ? Positive : Negative);
18,411,601✔
153

154
   if(x_sw == 0 || y_sw == 0) {
18,411,531✔
155
      clear();
3,047,255✔
156
      set_sign(Positive);
3,047,255✔
157
   } else if(x_sw == 1 && y_sw) {
15,364,276✔
158
      grow_to(y_sw + 1);
9,444,661✔
159
      bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
18,889,322✔
160
   } else if(y_sw == 1 && x_sw) {
5,919,615✔
161
      word carry = bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
54✔
162
      set_word_at(x_sw, carry);
27✔
163
   } else {
164
      const size_t new_size = x_sw + y_sw + 1;
5,919,588✔
165
      ws.resize(new_size);
5,919,588✔
166
      secure_vector<word> z_reg(new_size);
5,919,588✔
167

168
      bigint_mul(z_reg.data(), z_reg.size(), data(), size(), x_sw, y.data(), y.size(), y_sw, ws.data(), ws.size());
5,919,588✔
169

170
      this->swap_reg(z_reg);
5,919,588✔
171
   }
5,919,588✔
172

173
   return (*this);
18,411,531✔
174
}
175

176
BigInt& BigInt::square(secure_vector<word>& ws) {
3,176,097✔
177
   const size_t sw = sig_words();
3,176,097✔
178

179
   secure_vector<word> z(2 * sw);
3,176,097✔
180
   ws.resize(z.size());
3,176,097✔
181

182
   bigint_sqr(z.data(), z.size(), data(), size(), sw, ws.data(), ws.size());
3,176,097✔
183

184
   swap_reg(z);
3,176,097✔
185
   set_sign(BigInt::Positive);
3,176,097✔
186

187
   return (*this);
3,176,097✔
188
}
3,176,097✔
189

190
BigInt& BigInt::operator*=(word y) {
176,289,691✔
191
   if(y == 0) {
176,289,691✔
192
      clear();
×
193
      set_sign(Positive);
×
194
   }
195

196
   const word carry = bigint_linmul2(mutable_data(), size(), y);
176,289,691✔
197
   set_word_at(size(), carry);
176,289,691✔
198

199
   return (*this);
176,289,691✔
200
}
201

202
/*
203
* Division Operator
204
*/
205
BigInt& BigInt::operator/=(const BigInt& y) {
929✔
206
   if(y.sig_words() == 1 && is_power_of_2(y.word_at(0)))
1,622✔
207
      (*this) >>= (y.bits() - 1);
85✔
208
   else
209
      (*this) = (*this) / y;
1,688✔
210
   return (*this);
929✔
211
}
212

213
/*
214
* Modulo Operator
215
*/
216
BigInt& BigInt::operator%=(const BigInt& mod) { return (*this = (*this) % mod); }
926,309✔
217

218
/*
219
* Modulo Operator
220
*/
221
word BigInt::operator%=(word mod) {
30✔
222
   if(mod == 0)
30✔
223
      throw Invalid_Argument("BigInt::operator%= divide by zero");
×
224

225
   word remainder = 0;
30✔
226

227
   if(is_power_of_2(mod)) {
30✔
228
      remainder = (word_at(0) & (mod - 1));
8✔
229
   } else {
230
      const size_t sw = sig_words();
26✔
231
      for(size_t i = sw; i > 0; --i)
75✔
232
         remainder = bigint_modop(remainder, word_at(i - 1), mod);
98✔
233
   }
234

235
   if(remainder && sign() == BigInt::Negative)
30✔
236
      remainder = mod - remainder;
12✔
237

238
   m_data.set_to_zero();
30✔
239
   m_data.set_word_at(0, remainder);
30✔
240
   set_sign(BigInt::Positive);
30✔
241
   return remainder;
30✔
242
}
243

244
/*
245
* Left Shift Operator
246
*/
247
BigInt& BigInt::operator<<=(size_t shift) {
1,735,844✔
248
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
1,735,844✔
249
   const size_t shift_bits = shift % BOTAN_MP_WORD_BITS;
1,735,844✔
250
   const size_t size = sig_words();
1,735,844✔
251

252
   const size_t bits_free = top_bits_free();
1,735,844✔
253

254
   const size_t new_size = size + shift_words + (bits_free < shift_bits);
1,735,844✔
255

256
   m_data.grow_to(new_size);
1,735,844✔
257

258
   bigint_shl1(m_data.mutable_data(), new_size, size, shift_words, shift_bits);
1,735,844✔
259

260
   return (*this);
1,735,844✔
261
}
262

263
/*
264
* Right Shift Operator
265
*/
266
BigInt& BigInt::operator>>=(size_t shift) {
29,409,764✔
267
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
29,409,764✔
268
   const size_t shift_bits = shift % BOTAN_MP_WORD_BITS;
29,409,764✔
269

270
   bigint_shr1(m_data.mutable_data(), m_data.size(), shift_words, shift_bits);
29,409,764✔
271

272
   if(is_negative() && is_zero())
29,503,201✔
273
      set_sign(Positive);
8✔
274

275
   return (*this);
29,409,764✔
276
}
277

278
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc