-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BigInteger.hpp
324 lines (264 loc) · 9.28 KB
/
BigInteger.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <gmp.h>
#include <vector>
#include <string>
#include <type_traits>
#include <stdexcept>
class BigInteger {
private:
mpz_t _Value;
public:
BigInteger() noexcept {
mpz_init(_Value);
}
template<typename __IntegerType>
BigInteger(__IntegerType SmallInteger) noexcept {
// __IntegerType must be a integer type, i.e. char, int, unsigned long...
static_assert(std::is_integral<__IntegerType>::value);
if constexpr (std::is_signed<__IntegerType>::value) {
mpz_init_set_sx(_Value, SmallInteger);
} else {
mpz_init_set_ux(_Value, SmallInteger);
}
}
BigInteger(bool IsNegative, const void* lpBuffer, size_t cbBuffer, bool UseLittleEndian) noexcept {
mpz_init(_Value);
mpz_import(_Value, cbBuffer, UseLittleEndian ? -1 : 1, sizeof(unsigned char), 0, 0, lpBuffer);
if (IsNegative)
mpz_neg(_Value, _Value);
}
BigInteger(bool IsNegative, const std::vector<uint8_t>& Buffer, bool UseLittleEndian) noexcept {
mpz_init(_Value);
mpz_import(_Value, Buffer.size(), UseLittleEndian ? -1 : 1, sizeof(unsigned char), 0, 0, Buffer.data());
if (IsNegative)
mpz_neg(_Value, _Value);
}
BigInteger(const char* lpszValue) noexcept {
mpz_init_set_str(_Value, lpszValue, 0);
}
BigInteger(const std::string& szValue) noexcept {
mpz_init_set_str(_Value, szValue.c_str(), 0);
}
BigInteger(const BigInteger& Other) noexcept {
mpz_init(_Value);
mpz_set(_Value, Other._Value);
}
BigInteger(BigInteger&& Other) noexcept {
mpz_init(_Value);
mpz_swap(_Value, Other._Value);
}
BigInteger& operator=(const BigInteger& Other) noexcept {
if (this != &Other) {
mpz_set(_Value, Other._Value);
}
return *this;
}
BigInteger& operator=(BigInteger&& Other) noexcept {
if (this != &Other) {
mpz_swap(_Value, Other._Value);
mpz_clear(Other._Value);
}
return *this;
}
template<typename __IntegerType>
BigInteger& operator=(__IntegerType SmallInteger) noexcept {
// __IntegerType must be a integer type, i.e. char, int, unsigned long...
static_assert(std::is_integral<__IntegerType>::value);
if constexpr (std::is_signed<__IntegerType>::value) {
mpz_set_sx(_Value, SmallInteger);
} else {
mpz_set_ux(_Value, SmallInteger);
}
return *this;
}
BigInteger& operator=(const char* lpszValue) noexcept {
mpz_init_set_str(_Value, lpszValue, 0);
return *this;
}
bool operator==(const BigInteger& Other) const noexcept {
return mpz_cmp(_Value, Other._Value) == 0;
}
bool operator!=(const BigInteger& Other) const noexcept {
return mpz_cmp(_Value, Other._Value) != 0;
}
bool operator<(const BigInteger& Other) const noexcept {
return mpz_cmp(_Value, Other._Value) < 0;
}
bool operator<=(const BigInteger& Other) const noexcept {
auto d = mpz_cmp(_Value, Other._Value);
return d < 0 || d == 0;
}
bool operator>(const BigInteger& Other) const noexcept {
return mpz_cmp(_Value, Other._Value) > 0;
}
bool operator>=(const BigInteger& Other) const noexcept {
auto d = mpz_cmp(_Value, Other._Value);
return d > 0 || d == 0;
}
BigInteger operator-() const noexcept {
BigInteger Result;
mpz_neg(Result._Value, _Value);
return Result;
}
BigInteger operator+(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_add(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator+=(const BigInteger& Other) noexcept {
mpz_add(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator-(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_sub(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator-=(const BigInteger& Other) noexcept {
mpz_sub(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator*(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_mul(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator*=(const BigInteger& Other) noexcept {
mpz_mul(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator/(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_fdiv_q(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator/=(const BigInteger& Other) noexcept {
mpz_fdiv_q(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator%(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_fdiv_r(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator%=(const BigInteger& Other) noexcept {
mpz_fdiv_r(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator~() const noexcept {
BigInteger Result;
mpz_com(Result._Value, _Value);
return Result;
}
BigInteger operator&(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_and(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator&=(const BigInteger& Other) noexcept {
mpz_and(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator|(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_ior(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator|=(const BigInteger& Other) noexcept {
mpz_ior(_Value, _Value, Other._Value);
return *this;
}
BigInteger operator^(const BigInteger& Other) const noexcept {
BigInteger Result;
mpz_xor(Result._Value, _Value, Other._Value);
return Result;
}
BigInteger& operator^=(const BigInteger& Other) noexcept {
mpz_xor(_Value, _Value, Other._Value);
return *this;
}
BigInteger& operator++() noexcept {
mpz_add_ui(_Value, _Value, 1);
return *this;
}
BigInteger operator++(int) noexcept {
BigInteger Result(*this);
mpz_add_ui(_Value, _Value, 1);
return Result;
}
BigInteger& operator--() noexcept {
mpz_sub_ui(_Value, _Value, 1);
return *this;
}
BigInteger operator--(int) noexcept {
BigInteger Result(*this);
mpz_sub_ui(_Value, _Value, 1);
return Result;
}
bool IsZero() const noexcept {
return mpz_sgn(_Value) == 0;
}
bool IsPositive() const noexcept {
return mpz_sgn(_Value) > 0;
}
bool IsNegative() const noexcept {
return mpz_sgn(_Value) < 0;
}
bool IsOne() const noexcept {
return mpz_cmp_si(_Value, 1) == 0;
}
BigInteger& Load(bool IsNegative, const void* lpBuffer, size_t cbBuffer, bool UseLittleEndian) noexcept {
mpz_import(_Value, cbBuffer, UseLittleEndian ? -1 : 1, sizeof(uint8_t), 0, 0, lpBuffer);
if (IsNegative)
mpz_neg(_Value, _Value);
return *this;
}
BigInteger& Load(bool IsNegative, const std::vector<uint8_t> Buffer, bool UseLittleEndian) noexcept {
mpz_import(_Value, Buffer.size(), UseLittleEndian ? -1 : 1, sizeof(uint8_t), 0, 0, Buffer.data());
if (IsNegative)
mpz_neg(_Value, _Value);
return *this;
}
void DumpAbs(void* lpBuffer, size_t cbBuffer, bool UseLittleEndian) const {
size_t bit_size = mpz_sizeinbase(_Value, 2);
size_t storage_size = (bit_size + 7) / 8;
if (cbBuffer >= storage_size) {
size_t bytes_written;
mpz_export(lpBuffer, &bytes_written, UseLittleEndian ? -1 : 1, sizeof(uint8_t), 0, 0, _Value);
memset(reinterpret_cast<unsigned char*>(lpBuffer) + bytes_written, 0, cbBuffer - bytes_written);
} else {
throw std::length_error("Insufficient buffer.");
}
}
std::vector<uint8_t> DumpAbs(bool UseLittleEndian) const noexcept {
size_t bit_size = mpz_sizeinbase(_Value, 2);
size_t storage_size = (bit_size + 7) / 8;
std::vector<uint8_t> bytes(storage_size);
mpz_export(bytes.data(), nullptr, UseLittleEndian ? -1 : 1, sizeof(uint8_t), 0, 0, _Value);
return bytes;
}
size_t BitLength() const noexcept {
return mpz_sizeinbase(_Value, 2);
}
bool TestBit(size_t i) const noexcept {
return mpz_tstbit(_Value, i) != 0;
}
void SetBit(size_t i) noexcept {
mpz_setbit(_Value, i);
}
std::string ToString(size_t Base, bool LowerCase) const {
if (2 <= Base && Base <= 10 + 26) {
int base = LowerCase ? static_cast<int>(Base) : -static_cast<int>(Base);
std::string s(mpz_sizeinbase(_Value, base) + 2, '\x00');
mpz_get_str(s.data(), base, _Value);
while (s.back() == '\x00') {
s.pop_back();
}
return s;
} else {
throw std::invalid_argument("Invalid base value.");
}
}
};