• 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

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,689,495✔
20
   const size_t x_sw = x.sig_words();
3,689,495✔
21

22
   BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
4,049,760✔
23

24
   if(x.sign() == y_sign) {
3,689,495✔
25
      bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
2,666,318✔
26
      z.set_sign(x.sign());
2,666,318✔
27
   } else {
28
      const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x.data(), x_sw, y, y_words);
1,023,177✔
29

30
      //z.sign_fixup(relative_size, y_sign);
31
      if(relative_size < 0)
1,023,177✔
32
         z.set_sign(y_sign);
401,226✔
33
      else if(relative_size == 0)
621,951✔
34
         z.set_sign(BigInt::Positive);
462✔
35
      else
36
         z.set_sign(x.sign());
621,489✔
37
   }
38

39
   return z;
3,689,495✔
40
}
×
41

42
/*
43
* Multiplication Operator
44
*/
45
BigInt operator*(const BigInt& x, const BigInt& y) {
3,100,529✔
46
   const size_t x_sw = x.sig_words();
3,100,529✔
47
   const size_t y_sw = y.sig_words();
3,100,529✔
48

49
   BigInt z = BigInt::with_capacity(x.size() + y.size());
3,100,529✔
50

51
   if(x_sw == 1 && y_sw)
3,100,529✔
52
      bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
4,093,740✔
53
   else if(y_sw == 1 && x_sw)
1,053,659✔
54
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
837,822✔
55
   else if(x_sw && y_sw) {
634,748✔
56
      secure_vector<word> workspace(z.size());
592,404✔
57

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

70
   z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());
5,299,173✔
71

72
   return z;
3,100,529✔
73
}
×
74

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

81
   BigInt z = BigInt::with_capacity(x_sw + 1);
1,843,824✔
82

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

88
   return z;
1,843,824✔
89
}
×
90

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

99
   BigInt q, r;
796✔
100
   vartime_divide(x, y, q, r);
796✔
101
   return q;
796✔
102
}
4,094✔
103

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

111
   BigInt q;
928,991✔
112
   word r;
928,991✔
113
   ct_divide_word(x, y, q, r);
928,991✔
114
   return q;
928,991✔
115
}
×
116

117
/*
118
* Modulo Operator
119
*/
120
BigInt operator%(const BigInt& n, const BigInt& mod) {
1,128,432✔
121
   if(mod.is_zero())
1,128,508✔
122
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
123
   if(mod.is_negative())
1,128,432✔
124
      throw Invalid_Argument("BigInt::operator% modulus must be > 0");
×
125
   if(n.is_positive() && mod.is_positive() && n < mod)
2,225,788✔
126
      return n;
189,434✔
127

128
   if(mod.sig_words() == 1) {
938,998✔
129
      return BigInt::from_word(n % mod.word_at(0));
577,358✔
130
   }
131

132
   BigInt q, r;
650,319✔
133
   vartime_divide(n, mod, q, r);
650,319✔
134
   return r;
650,319✔
135
}
1,778,751✔
136

137
/*
138
* Modulo Operator
139
*/
140
word operator%(const BigInt& n, word mod) {
2,767,071✔
141
   if(mod == 0)
2,767,071✔
142
      throw Invalid_Argument("BigInt::operator% divide by zero");
×
143

144
   if(mod == 1)
2,767,071✔
145
      return 0;
146

147
   word remainder = 0;
2,766,910✔
148

149
   if(is_power_of_2(mod)) {
2,766,910✔
150
      remainder = (n.word_at(0) & (mod - 1));
3,958,360✔
151
   } else {
152
      const size_t sw = n.sig_words();
787,730✔
153
      for(size_t i = sw; i > 0; --i) {
8,049,514✔
154
         remainder = bigint_modop(remainder, n.word_at(i - 1), mod);
14,523,568✔
155
      }
156
   }
157

158
   if(remainder && n.sign() == BigInt::Negative)
2,766,910✔
159
      return mod - remainder;
29,958✔
160
   return remainder;
161
}
162

163
/*
164
* Left Shift Operator
165
*/
166
BigInt operator<<(const BigInt& x, size_t shift) {
654,237✔
167
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS, shift_bits = shift % BOTAN_MP_WORD_BITS;
654,237✔
168

169
   const size_t x_sw = x.sig_words();
654,237✔
170

171
   BigInt y = BigInt::with_capacity(x_sw + shift_words + (shift_bits ? 1 : 0));
1,307,655✔
172
   bigint_shl2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);
654,237✔
173
   y.set_sign(x.sign());
654,237✔
174
   return y;
654,237✔
175
}
×
176

177
/*
178
* Right Shift Operator
179
*/
180
BigInt operator>>(const BigInt& x, size_t shift) {
191,964✔
181
   const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
191,964✔
182
   const size_t shift_bits = shift % BOTAN_MP_WORD_BITS;
191,964✔
183
   const size_t x_sw = x.sig_words();
191,964✔
184

185
   if(shift_words >= x_sw)
191,964✔
186
      return BigInt::zero();
2,062✔
187

188
   BigInt y = BigInt::with_capacity(x_sw - shift_words);
189,902✔
189
   bigint_shr2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);
189,902✔
190

191
   if(x.is_negative() && y.is_zero())
189,927✔
192
      y.set_sign(BigInt::Positive);
3✔
193
   else
194
      y.set_sign(x.sign());
189,899✔
195

196
   return y;
189,902✔
197
}
191,964✔
198

199
}
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