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

randombit / botan / 16511306696

25 Jul 2025 01:07AM UTC coverage: 90.698% (+0.005%) from 90.693%
16511306696

push

github

web-flow
Merge pull request #5017 from randombit/jack/mp-cleanup

Clean up low level mp interfaces

99956 of 110208 relevant lines covered (90.7%)

12233642.11 hits per line

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

91.18
/src/lib/math/bigint/big_ops3.cpp
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
BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_size, BigInt::Sign y_sign) {
4,225,856✔
20
   const size_t x_sw = x.sig_words();
4,225,856✔
21

22
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_size) + 1);
4,404,780✔
23

24
   if(x.sign() == y_sign) {
4,225,856✔
25
      word carry = bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_size);
3,645,235✔
26
      z.mutable_data()[std::max(x_sw, y_size)] += carry;
3,645,235✔
27
      z.set_sign(x.sign());
3,645,235✔
28
   } else {
29
      const int32_t relative_size = bigint_cmp(x.data(), x_sw, y, y_size);
580,621✔
30

31
      if(relative_size < 0) {
580,621✔
32
         // x < y so z = abs(y - x)
33
         // NOLINTNEXTLINE(*-suspicious-call-argument) intentionally swapping x and y here
34
         bigint_sub3(z.mutable_data(), y, y_size, x.data(), x_sw);
7,321✔
35
         z.set_sign(y_sign);
7,321✔
36
      } else if(relative_size == 0) {
573,300✔
37
         // Positive zero (nothing to do in this case)
38
      } else {
39
         /*
40
         * We know at this point that x >= y so if y_size is larger than
41
         * x_sw, we are guaranteed they are just leading zeros which can
42
         * be ignored
43
         */
44
         y_size = std::min(x_sw, y_size);
573,180✔
45
         bigint_sub3(z.mutable_data(), x.data(), x_sw, y, y_size);
573,180✔
46
         z.set_sign(x.sign());
573,180✔
47
      }
48
   }
49

50
   return z;
4,225,856✔
51
}
×
52

53
/*
54
* Multiplication Operator
55
*/
56
BigInt operator*(const BigInt& x, const BigInt& y) {
46,838✔
57
   const size_t x_sw = x.sig_words();
46,838✔
58
   const size_t y_sw = y.sig_words();
46,838✔
59

60
   BigInt z = BigInt::with_capacity(x.size() + y.size());
46,838✔
61

62
   if(x_sw == 1 && y_sw > 0) {
46,838✔
63
      bigint_linmul3(z.mutable_data(), y._data(), y_sw, x.word_at(0));
47,764✔
64
   } else if(y_sw == 1 && x_sw > 0) {
22,956✔
65
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y.word_at(0));
7,686✔
66
   } else if(x_sw > 0 && y_sw > 0) {
19,113✔
67
      secure_vector<word> workspace(z.size());
18,314✔
68

69
      bigint_mul(z.mutable_data(),
18,314✔
70
                 z.size(),
71
                 x._data(),
72
                 x.size(),
73
                 x_sw,
74
                 y._data(),
75
                 y.size(),
76
                 y_sw,
77
                 workspace.data(),
78
                 workspace.size());
79
   }
18,314✔
80

81
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
93,306✔
82

83
   return z;
46,838✔
84
}
×
85

86
/*
87
* Multiplication Operator
88
*/
89
BigInt operator*(const BigInt& x, word y) {
3,488,309✔
90
   const size_t x_sw = x.sig_words();
3,488,309✔
91

92
   BigInt z = BigInt::with_capacity(x_sw + 1);
3,488,309✔
93

94
   if(x_sw > 0 && y > 0) {
3,488,309✔
95
      bigint_linmul3(z.mutable_data(), x._data(), x_sw, y);
3,379,215✔
96
      z.set_sign(x.sign());
3,379,215✔
97
   }
98

99
   return z;
3,488,309✔
100
}
×
101

102
/*
103
* Division Operator
104
*/
105
BigInt operator/(const BigInt& x, const BigInt& y) {
156,007✔
106
   if(y.sig_words() == 1) {
158,449✔
107
      return x / y.word_at(0);
110,878✔
108
   }
109

110
   BigInt q;
100,568✔
111
   BigInt r;
100,568✔
112
   vartime_divide(x, y, q, r);
100,568✔
113
   return q;
100,568✔
114
}
100,568✔
115

116
/*
117
* Division Operator
118
*/
119
BigInt operator/(const BigInt& x, word y) {
381,005✔
120
   if(y == 0) {
381,005✔
121
      throw Invalid_Argument("BigInt::operator/ divide by zero");
×
122
   }
123

124
   BigInt q;
381,005✔
125
   word r = 0;
381,005✔
126
   ct_divide_word(x, y, q, r);
381,005✔
127
   return q;
381,005✔
128
}
×
129

130
/*
131
* Modulo Operator
132
*/
133
BigInt operator%(const BigInt& n, const BigInt& mod) {
553,362✔
134
   if(mod.is_zero()) {
553,472✔
135
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
136
   }
137
   if(mod.is_negative()) {
553,362✔
138
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
139
   }
140
   if(n.is_positive() && mod.is_positive() && n < mod) {
1,075,536✔
141
      return n;
172,497✔
142
   }
143

144
   if(mod.sig_words() == 1) {
380,865✔
145
      return BigInt::from_word(n % mod.word_at(0));
363,976✔
146
   }
147

148
   BigInt q;
198,877✔
149
   BigInt r;
198,877✔
150
   vartime_divide(n, mod, q, r);
198,877✔
151
   return r;
198,877✔
152
}
198,877✔
153

154
/*
155
* Modulo Operator
156
*/
157
word operator%(const BigInt& n, word mod) {
1,394,491✔
158
   if(mod == 0) {
1,394,491✔
159
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
160
   }
161

162
   if(mod == 1) {
1,394,491✔
163
      return 0;
164
   }
165

166
   word remainder = 0;
1,394,480✔
167

168
   if(is_power_of_2(mod)) {
1,394,480✔
169
      remainder = (n.word_at(0) & (mod - 1));
1,212,326✔
170
   } else {
171
      const size_t sw = n.sig_words();
788,317✔
172
      for(size_t i = sw; i > 0; --i) {
8,083,026✔
173
         remainder = bigint_modop_vartime(remainder, n.word_at(i - 1), mod);
14,589,418✔
174
      }
175
   }
176

177
   if(remainder != 0 && n.sign() == BigInt::Negative) {
1,394,480✔
178
      return mod - remainder;
29,959✔
179
   }
180
   return remainder;
181
}
182

183
/*
184
* Left Shift Operator
185
*/
186
BigInt operator<<(const BigInt& x, size_t shift) {
302,705✔
187
   const size_t x_sw = x.sig_words();
302,705✔
188

189
   const size_t new_size = x_sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits;
302,705✔
190
   BigInt y = BigInt::with_capacity(new_size);
302,705✔
191
   bigint_shl2(y.mutable_data(), x._data(), x_sw, shift);
302,705✔
192
   y.set_sign(x.sign());
302,705✔
193
   return y;
302,705✔
194
}
×
195

196
/*
197
* Right Shift Operator
198
*/
199
BigInt operator>>(const BigInt& x, size_t shift) {
216,043✔
200
   const size_t shift_words = shift / WordInfo<word>::bits;
216,043✔
201
   const size_t x_sw = x.sig_words();
216,043✔
202

203
   if(shift_words >= x_sw) {
216,043✔
204
      return BigInt::zero();
2,062✔
205
   }
206

207
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
213,981✔
208
   bigint_shr2(y.mutable_data(), x._data(), x_sw, shift);
213,981✔
209

210
   if(x.is_negative() && y.is_zero()) {
214,006✔
211
      y.set_sign(BigInt::Positive);
3✔
212
   } else {
213
      y.set_sign(x.sign());
213,978✔
214
   }
215

216
   return y;
213,981✔
217
}
213,981✔
218

219
}  // 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