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

randombit / botan / 16242898793

12 Jul 2025 11:06PM UTC coverage: 90.571% (-0.002%) from 90.573%
16242898793

push

github

web-flow
Merge pull request #4972 from randombit/jack/fix-clang-tidy-readability-container-data-pointer

Enable and fix clang-tidy warning readability-container-data-pointer

99095 of 109411 relevant lines covered (90.57%)

12442350.4 hits per line

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

88.97
/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) {
5,501,013✔
17
   const size_t x_sw = sig_words();
5,501,013✔
18

19
   grow_to(std::max(x_sw, y_words) + 1);
5,570,881✔
20

21
   if(sign() == y_sign) {
5,501,013✔
22
      bigint_add2(mutable_data(), size() - 1, y, y_words);
1,873,247✔
23
   } else {
24
      const int32_t relative_size = bigint_cmp(_data(), x_sw, y, y_words);
3,627,766✔
25

26
      if(relative_size >= 0) {
3,627,766✔
27
         // *this >= y
28
         bigint_sub2(mutable_data(), x_sw, y, y_words);
3,566,059✔
29
      } else {
30
         // *this < y
31
         bigint_sub2_rev(mutable_data(), y, y_words);
61,707✔
32
      }
33

34
      //this->sign_fixup(relative_size, y_sign);
35
      if(relative_size < 0) {
3,627,766✔
36
         set_sign(y_sign);
61,707✔
37
      } else if(relative_size == 0) {
3,566,059✔
38
         set_sign(Positive);
53,157✔
39
      }
40
   }
41

42
   return (*this);
5,501,013✔
43
}
44

45
BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
1,118,799✔
46
   if(this->is_negative() || s.is_negative() || mod.is_negative()) {
1,118,799✔
47
      throw Invalid_Argument("BigInt::mod_add expects all arguments are positive");
×
48
   }
49

50
   BOTAN_DEBUG_ASSERT(*this < mod);
1,118,799✔
51
   BOTAN_DEBUG_ASSERT(s < mod);
1,118,799✔
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
   const size_t mod_sw = mod.sig_words();
1,118,799✔
64
   BOTAN_ARG_CHECK(mod_sw > 0, "BigInt::mod_add modulus must be positive");
1,118,799✔
65

66
   this->grow_to(mod_sw);
1,118,799✔
67
   s.grow_to(mod_sw);
1,118,799✔
68

69
   // First mod_sw for p - s, 2*mod_sw for bigint_addsub workspace
70
   if(ws.size() < 3 * mod_sw) {
1,118,799✔
71
      ws.resize(3 * mod_sw);
615,578✔
72
   }
73

74
   // NOLINTBEGIN(readability-container-data-pointer)
75

76
   word borrow = bigint_sub3(&ws[0], mod._data(), mod_sw, s._data(), mod_sw);
1,118,799✔
77
   BOTAN_DEBUG_ASSERT(borrow == 0);
1,118,799✔
78
   BOTAN_UNUSED(borrow);
1,118,799✔
79

80
   // Compute t - ws
81
   borrow = bigint_sub3(&ws[mod_sw], this->_data(), mod_sw, &ws[0], mod_sw);
1,118,799✔
82

83
   // Compute t + s
84
   bigint_add3_nc(&ws[mod_sw * 2], this->_data(), mod_sw, s._data(), mod_sw);
1,118,799✔
85

86
   CT::conditional_copy_mem(borrow, &ws[0], &ws[mod_sw * 2], &ws[mod_sw], mod_sw);
1,118,799✔
87
   set_words(&ws[0], mod_sw);
1,118,799✔
88

89
   // NOLINTEND(readability-container-data-pointer)
90

91
   return (*this);
1,118,799✔
92
}
93

94
BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) {
15,889,793✔
95
   if(this->is_negative() || s.is_negative() || mod.is_negative()) {
15,889,793✔
96
      throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive");
×
97
   }
98

99
   // We are assuming in this function that *this and s are no more than mod_sw words long
100
   BOTAN_DEBUG_ASSERT(*this < mod);
15,889,793✔
101
   BOTAN_DEBUG_ASSERT(s < mod);
15,889,793✔
102

103
   const size_t mod_sw = mod.sig_words();
15,889,793✔
104

105
   this->grow_to(mod_sw);
15,889,793✔
106
   s.grow_to(mod_sw);
15,889,793✔
107

108
   if(ws.size() < mod_sw) {
15,889,793✔
109
      ws.resize(mod_sw);
5,999✔
110
   }
111

112
   bigint_mod_sub(mutable_data(), s._data(), mod._data(), mod_sw, ws.data());
15,889,793✔
113

114
   return (*this);
15,889,793✔
115
}
116

117
BigInt& BigInt::mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws) {
6,918,676✔
118
   BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive");
6,918,676✔
119
   BOTAN_ARG_CHECK(y < 16, "y too large");
6,918,676✔
120

121
   BOTAN_DEBUG_ASSERT(*this < mod);
6,918,676✔
122

123
   *this *= static_cast<word>(y);
6,918,676✔
124
   this->reduce_below(mod, ws);
6,918,676✔
125
   return (*this);
6,918,676✔
126
}
127

128
BigInt& BigInt::rev_sub(const word y[], size_t y_sw, secure_vector<word>& ws) {
×
129
   if(this->sign() != BigInt::Positive) {
×
130
      throw Invalid_State("BigInt::sub_rev requires this is positive");
×
131
   }
132

133
   const size_t x_sw = this->sig_words();
×
134

135
   ws.resize(std::max(x_sw, y_sw));
×
136
   clear_mem(ws.data(), ws.size());
×
137

138
   const int32_t relative_size = bigint_sub_abs(ws.data(), _data(), x_sw, y, y_sw);
×
139

140
   this->cond_flip_sign(relative_size > 0);
×
141
   this->swap_reg(ws);
×
142

143
   return (*this);
×
144
}
145

146
/*
147
* Multiplication Operator
148
*/
149
BigInt& BigInt::operator*=(const BigInt& y) {
525✔
150
   secure_vector<word> ws;
525✔
151
   return this->mul(y, ws);
525✔
152
}
525✔
153

154
BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws) {
9,237✔
155
   const size_t x_sw = sig_words();
9,237✔
156
   const size_t y_sw = y.sig_words();
9,237✔
157
   set_sign((sign() == y.sign()) ? Positive : Negative);
9,307✔
158

159
   if(x_sw == 0 || y_sw == 0) {
9,237✔
160
      clear();
32✔
161
      set_sign(Positive);
32✔
162
   } else if(x_sw == 1 && y_sw > 0) {
9,205✔
163
      grow_to(y_sw + 1);
328✔
164
      bigint_linmul3(mutable_data(), y._data(), y_sw, word_at(0));
656✔
165
   } else if(y_sw == 1 && x_sw > 0) {
8,877✔
166
      word carry = bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
42✔
167
      set_word_at(x_sw, carry);
21✔
168
   } else {
169
      const size_t new_size = x_sw + y_sw + 1;
8,856✔
170
      if(ws.size() < new_size) {
8,856✔
171
         ws.resize(new_size);
8,856✔
172
      }
173
      secure_vector<word> z_reg(new_size);
8,856✔
174

175
      bigint_mul(z_reg.data(), z_reg.size(), _data(), size(), x_sw, y._data(), y.size(), y_sw, ws.data(), ws.size());
8,856✔
176

177
      this->swap_reg(z_reg);
8,856✔
178
   }
8,856✔
179

180
   return (*this);
9,237✔
181
}
182

183
BigInt& BigInt::square(secure_vector<word>& ws) {
9,733✔
184
   const size_t sw = sig_words();
9,733✔
185

186
   secure_vector<word> z(2 * sw);
9,733✔
187
   ws.resize(z.size());
9,733✔
188

189
   bigint_sqr(z.data(), z.size(), _data(), size(), sw, ws.data(), ws.size());
9,733✔
190

191
   swap_reg(z);
9,733✔
192
   set_sign(BigInt::Positive);
9,733✔
193

194
   return (*this);
9,733✔
195
}
9,733✔
196

197
BigInt& BigInt::operator*=(word y) {
7,297,805✔
198
   if(y == 0) {
7,297,805✔
199
      clear();
×
200
      set_sign(Positive);
×
201
   }
202

203
   const word carry = bigint_linmul2(mutable_data(), size(), y);
7,297,805✔
204
   set_word_at(size(), carry);
7,297,805✔
205

206
   return (*this);
7,297,805✔
207
}
208

209
/*
210
* Division Operator
211
*/
212
BigInt& BigInt::operator/=(const BigInt& y) {
929✔
213
   if(y.sig_words() == 1 && is_power_of_2(y.word_at(0))) {
1,620✔
214
      (*this) >>= (y.bits() - 1);
85✔
215
   } else {
216
      (*this) = (*this) / y;
844✔
217
   }
218
   return (*this);
929✔
219
}
220

221
/*
222
* Modulo Operator
223
*/
224
BigInt& BigInt::operator%=(const BigInt& mod) {
327,511✔
225
   return (*this = (*this) % mod);
327,511✔
226
}
227

228
/*
229
* Modulo Operator
230
*/
231
word BigInt::operator%=(word mod) {
33✔
232
   if(mod == 0) {
33✔
233
      throw Invalid_Argument("BigInt::operator%= divide by zero");
×
234
   }
235

236
   word remainder = 0;
33✔
237

238
   if(is_power_of_2(mod)) {
33✔
239
      remainder = (word_at(0) & (mod - 1));
10✔
240
   } else {
241
      const size_t sw = sig_words();
28✔
242
      for(size_t i = sw; i > 0; --i) {
79✔
243
         remainder = bigint_modop_vartime(remainder, word_at(i - 1), mod);
102✔
244
      }
245
   }
246

247
   if(remainder != 0 && sign() == BigInt::Negative) {
33✔
248
      remainder = mod - remainder;
12✔
249
   }
250

251
   m_data.set_to_zero();
33✔
252
   m_data.set_word_at(0, remainder);
33✔
253
   set_sign(BigInt::Positive);
33✔
254
   return remainder;
33✔
255
}
256

257
/*
258
* Left Shift Operator
259
*/
260
BigInt& BigInt::operator<<=(size_t shift) {
12,137,957✔
261
   const size_t sw = sig_words();
12,137,957✔
262
   const size_t new_size = sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
12,137,957✔
263

264
   m_data.grow_to(new_size);
12,137,957✔
265

266
   bigint_shl1(m_data.mutable_data(), new_size, sw, shift);
12,137,957✔
267

268
   return (*this);
12,137,957✔
269
}
270

271
/*
272
* Right Shift Operator
273
*/
274
BigInt& BigInt::operator>>=(size_t shift) {
7,507,030✔
275
   bigint_shr1(m_data.mutable_data(), m_data.size(), shift);
7,507,030✔
276

277
   if(is_negative() && is_zero()) {
7,605,352✔
278
      set_sign(Positive);
8✔
279
   }
280

281
   return (*this);
7,507,030✔
282
}
283

284
}  // namespace Botan
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