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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

90.82
/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_words, BigInt::Sign y_sign) {
3,381,967✔
20
   const size_t x_sw = x.sig_words();
3,381,967✔
21

22
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
3,656,852✔
23

24
   if(x.sign() == y_sign) {
3,381,967✔
25
      bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
2,427,778✔
26
      z.set_sign(x.sign());
2,427,778✔
27
   } else {
28
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x.data(), x_sw, y, y_words);
954,189✔
29

30
      //z.sign_fixup(relative_size, y_sign);
31
      if(relative_size < 0) {
954,189✔
32
         z.set_sign(y_sign);
341,598✔
33
      } else if(relative_size == 0) {
612,591✔
34
         z.set_sign(BigInt::Positive);
450✔
35
      } else {
36
         z.set_sign(x.sign());
612,141✔
37
      }
38
   }
39

40
   return z;
3,381,967✔
41
}
×
42

43
/*
44
* Multiplication Operator
45
*/
46
BigInt operator*(const BigInt& x, const BigInt& y) {
2,794,280✔
47
   const size_t x_sw = x.sig_words();
2,794,280✔
48
   const size_t y_sw = y.sig_words();
2,794,280✔
49

50
   BigInt z = BigInt::with_capacity(x.size() + y.size());
2,794,280✔
51

52
   if(x_sw == 1 && y_sw) {
2,794,280✔
53
      bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
3,891,290✔
54
   } else if(y_sw == 1 && x_sw) {
848,635✔
55
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
638,294✔
56
   } else if(x_sw && y_sw) {
529,488✔
57
      secure_vector<word> workspace(z.size());
487,513✔
58

59
      bigint_mul(z.mutable_data(),
487,513✔
60
                 z.size(),
61
                 x.data(),
62
                 x.size(),
63
                 x_sw,
64
                 y.data(),
65
                 y.size(),
66
                 y_sw,
67
                 workspace.data(),
68
                 workspace.size());
69
   }
487,513✔
70

71
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
4,812,436✔
72

73
   return z;
2,794,280✔
74
}
×
75

76
/*
77
* Multiplication Operator
78
*/
79
BigInt operator*(const BigInt& x, word y) {
1,843,809✔
80
   const size_t x_sw = x.sig_words();
1,843,809✔
81

82
   BigInt z = BigInt::with_capacity(x_sw + 1);
1,843,809✔
83

84
   if(x_sw && y) {
1,843,809✔
85
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y);
922,734✔
86
      z.set_sign(x.sign());
922,734✔
87
   }
88

89
   return z;
1,843,809✔
90
}
×
91

92
/*
93
* Division Operator
94
*/
95
BigInt operator/(const BigInt& x, const BigInt& y) {
3,305✔
96
   if(y.sig_words() == 1) {
5,766✔
97
      return x / y.word_at(0);
5,004✔
98
   }
99

100
   BigInt q, r;
803✔
101
   vartime_divide(x, y, q, r);
803✔
102
   return q;
803✔
103
}
4,108✔
104

105
/*
106
* Division Operator
107
*/
108
BigInt operator/(const BigInt& x, word y) {
929,534✔
109
   if(y == 0) {
929,534✔
110
      throw Invalid_Argument("BigInt::operator/ divide by zero");
×
111
   }
112

113
   BigInt q;
929,534✔
114
   word r;
929,534✔
115
   ct_divide_word(x, y, q, r);
929,534✔
116
   return q;
929,534✔
117
}
×
118

119
/*
120
* Modulo Operator
121
*/
122
BigInt operator%(const BigInt& n, const BigInt& mod) {
1,128,969✔
123
   if(mod.is_zero()) {
1,129,045✔
124
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
125
   }
126
   if(mod.is_negative()) {
1,128,969✔
127
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
128
   }
129
   if(n.is_positive() && mod.is_positive() && n < mod) {
2,226,976✔
130
      return n;
189,024✔
131
   }
132

133
   if(mod.sig_words() == 1) {
939,945✔
134
      return BigInt::from_word(n % mod.word_at(0));
577,434✔
135
   }
136

137
   BigInt q, r;
651,228✔
138
   vartime_divide(n, mod, q, r);
651,228✔
139
   return r;
651,228✔
140
}
1,780,197✔
141

142
/*
143
* Modulo Operator
144
*/
145
word operator%(const BigInt& n, word mod) {
2,670,455✔
146
   if(mod == 0) {
2,670,455✔
147
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
148
   }
149

150
   if(mod == 1) {
2,670,455✔
151
      return 0;
152
   }
153

154
   word remainder = 0;
2,670,312✔
155

156
   if(is_power_of_2(mod)) {
2,670,312✔
157
      remainder = (n.word_at(0) & (mod - 1));
3,961,946✔
158
   } else {
159
      const size_t sw = n.sig_words();
689,339✔
160
      for(size_t i = sw; i > 0; --i) {
6,372,238✔
161
         remainder = bigint_modop(remainder, n.word_at(i - 1), mod);
11,365,798✔
162
      }
163
   }
164

165
   if(remainder && n.sign() == BigInt::Negative) {
2,670,312✔
166
      return mod - remainder;
29,960✔
167
   }
168
   return remainder;
169
}
170

171
/*
172
* Left Shift Operator
173
*/
174
BigInt operator<<(const BigInt& x, size_t shift) {
654,970✔
175
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS, shift_bits = shift % BOTAN_MP_WORD_BITS;
654,970✔
176

177
   const size_t x_sw = x.sig_words();
654,970✔
178

179
   BigInt y = BigInt::with_capacity(x_sw + shift_words + (shift_bits ? 1 : 0));
1,309,222✔
180
   bigint_shl2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);
654,970✔
181
   y.set_sign(x.sign());
654,970✔
182
   return y;
654,970✔
183
}
×
184

185
/*
186
* Right Shift Operator
187
*/
188
BigInt operator>>(const BigInt& x, size_t shift) {
186,684✔
189
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
186,684✔
190
   const size_t shift_bits = shift % BOTAN_MP_WORD_BITS;
186,684✔
191
   const size_t x_sw = x.sig_words();
186,684✔
192

193
   if(shift_words >= x_sw) {
186,684✔
194
      return BigInt::zero();
2,062✔
195
   }
196

197
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
184,622✔
198
   bigint_shr2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);
184,622✔
199

200
   if(x.is_negative() && y.is_zero()) {
184,647✔
201
      y.set_sign(BigInt::Positive);
3✔
202
   } else {
203
      y.set_sign(x.sign());
184,619✔
204
   }
205

206
   return y;
184,622✔
207
}
186,684✔
208

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