FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
csbiginteger-cpp/src/BigIntegerGMP.cpp at master · NeoResearch/csbiginteger-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
NeoResearch
/
csbiginteger-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
csbiginteger-cpp
/
src
/
BigIntegerGMP.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
422 lines (345 loc) · 12.5 KB
Breadcrumbs
csbiginteger-cpp
/
src
/
BigIntegerGMP.cpp
Copy path
File metadata and controls
422 lines (345 loc) · 12.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
SPDX-License-Identifier: LGPL-3.0-or-later AND MIT
//
Copyright (C) 2020-2022 - csbiginteger-cpp project
#
include
<
csbiginteger/BigInteger.h
>
//
//
using gmp as internal computation method
//
TODO(igormcoelho): abandon this in favor of more portable libraries
//
sudo apt install libgmp (or libgmp-dev)
#
include
<
assert.h
>
#
include
<
gmp.h
>
#
include
<
gmpxx.h
>
//
c++
#
include
<
iostream
>
//
TODO(igormcoelho): remove
//
Ported from
//
-
//
https://referencesource.microsoft.com/#System.Numerics/System/Numerics/BigInteger.cs
//
using namespace std;
using
namespace
csbiginteger
;
//
NOLINT
//
input is raw little-endian format
mpz_class
csBigIntegerMPZparse
(cs_vbyte n);
//
string parse
mpz_class
csBigIntegerMPZparses
(std::string n,
int
base);
//
get bitstring from mpz bignum non-negative object
std::string
csBigIntegerGetBitsFromNonNegativeMPZ
(mpz_class big);
//
get big-endian bytearray from mpz bignum (positive or negative)
cs_vbyte
csBigIntegerGetBytesFromMPZ
(mpz_class big);
// ==================== END MPZ =======================
std::string
BigInteger::getEngine
() {
return
"
GMP
"
; }
const
BigInteger
BigInteger::error
() {
BigInteger big;
big.
_data
=
cs_vbyte
(
0
);
//
empty array is error
return
big;
}
BigInteger
BigInteger::Pow
(BigInteger value,
int
exponent) {
//
according to C# spec, only non-negative int32 values accepted here
if
(exponent <
0
)
return
BigInteger::Error
();
mpz_class big1 =
csBigIntegerMPZparse
(value.
ToByteArray
());
mpz_class r;
uint64_t
_exp = exponent;
mpz_pow_ui
(r.
get_mpz_t
(), big1.
get_mpz_t
(), _exp);
cs_vbyte vr =
csBigIntegerGetBytesFromMPZ
(r);
reverse
(vr.
begin
(), vr.
end
());
//
to little-endian
return
BigInteger
(vr);
}
//
default is base 10
//
allows base 2
//
if base 16, prefix '0x' indicates big-endian, otherwise is little-endian
BigInteger::BigInteger
(std::string str,
int
base) {
mpz_class a =
csBigIntegerMPZparses
(str, base);
_data =
csBigIntegerGetBytesFromMPZ
(a);
}
BigInteger::BigInteger
(
float
val) {
mpz_class a = val;
_data =
csBigIntegerGetBytesFromMPZ
(a);
}
cs_int32
BigInteger::toInt
()
const
{
cs_vbyte vb =
this
->
ToByteArray
();
//
little-endian
mpz_class a =
csBigIntegerMPZparse
(vb);
cs_int32 i = a.
get_ui
();
//
unsigned int
if
(a <
0
) i *= -
1
;
return
i;
}
cs_int64
BigInteger::toLong
()
const
{
cs_vbyte vb =
this
->
ToByteArray
();
//
little-endian
mpz_class a =
csBigIntegerMPZparse
(vb);
cs_int64 i = a.
get_si
();
//
signed long int
return
i;
}
bool
BigInteger::
operator
>(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
())
return
false
;
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
bool
r = (bThis > bOther);
//
result
return
r;
}
bool
BigInteger::
operator
<(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
()) {
return
false
;
}
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
bool
r = (bThis < bOther);
//
result
return
r;
}
//
----------------- arithmetic ---------------------
BigInteger BigInteger::
operator
+(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
())
return
Error
();
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis + bOther);
//
get big-endian
return
r;
}
BigInteger BigInteger::
operator
-(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
())
return
Error
();
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis - bOther);
//
get big-endian
return
r;
}
BigInteger BigInteger::
operator
*(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
())
return
Error
();
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis * bOther);
//
get big-endian
return
r;
}
BigInteger BigInteger::
operator
/(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
() || big2.
IsZero
())
return
Error
();
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis / bOther);
//
get big-endian
return
r;
}
BigInteger BigInteger::
operator
%(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
() || big2.
IsZero
())
return
Error
();
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
mpz_class bOther =
csBigIntegerMPZparse
(big2.
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis % bOther);
//
get big-endian
return
r;
}
BigInteger BigInteger::
operator
<<(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
())
return
Error
();
if
(big2 <
Zero
())
return
(*
this
) >> -big2;
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis << big2.
toInt
());
//
get big-endian
return
r;
}
BigInteger BigInteger::
operator
>>(
const
BigInteger& big2)
const
{
if
(
this
->
IsError
() || big2.
IsError
())
return
Error
();
if
(big2 <
Zero
())
return
(*
this
) << -big2;
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
BigInteger r;
//
result
r.
_data
=
csBigIntegerGetBytesFromMPZ
(bThis >> big2.
toInt
());
//
get big-endian
return
r;
}
// =================== BEGIN MPZ AGAIN =======================
std::string
BigInteger::toStringBase10
()
const
{
mpz_class bThis =
csBigIntegerMPZparse
(
this
->
ToByteArray
());
//
parse from little-endian
return
bThis.
get_str
(
10
);
}
//
assumes big >= 0
std::string
csBigIntegerGetBitsFromNonNegativeMPZ
(mpz_class big) {
std::string sbin;
while
(big >
0
) {
mpz_class rest = (big %
2
);
sbin.
insert
(
0
, (rest.
get_ui
() ==
0
?
std::string
(
"
0
"
) :
std::string
(
"
1
"
)));
big = big /
2
;
}
return
sbin;
}
cs_vbyte
csBigIntegerGetBytesFromMPZ
(mpz_class big) {
//
check if positive or negative
if
(big >=
0
) {
//
-------------------
//
positive conversion
//
-------------------
cs_vbyte v;
while
(big >
0
) {
mpz_class rest = (big %
256
);
v.
push_back
((cs_byte)rest.
get_ui
());
big = big /
256
;
}
//
added in little-endian format (backwards)
std::string leHex =
Helper::toHexString
(v);
if
(leHex.
length
() ==
0
) leHex =
"
00
"
;
//
check if became negative
if
(
Helper::checkNegativeBit
(leHex)) {
v.
push_back
(
0
);
//
guarantee non-negative
}
//
v is added backwards (little-endian), must reverse (to big-endian)
reverse
(v.
begin
(), v.
end
());
if
(v.
size
() ==
0
) v.
push_back
(
0x00
);
//
finished
return
v;
}
else
{
//
-------------------
//
negative conversion
//
-------------------
//
turn into positive
mpz_class x = big * (-
1
);
//
cout << "x positive " << x << endl;
//
========================
//
perform two's complement
//
========================
//
convert to binary
cs_vbyte vx =
csBigIntegerGetBytesFromMPZ
(x);
//
positive is easy to convert
//
cout << Helper::toHexString(vx) << endl;
mpz_class
bx
(
Helper::toHexString
(vx),
16
);
//
get binary representation
std::string y = bx.
get_str
(
2
);
//
cout << "numbits: " << y.length() << endl;
//
cout << "ybits: " << y << endl;
//
cout << "dbits: " << csBigIntegerGetBitsFromNonNegativeMPZ(x) << endl;
//
extra padding for limit cases (avoid overflow)
y.
insert
(
0
,
"
0
"
);
//
prepend
//
guarantee length must be at least 8, or add padding!
while
((y.
length
() <
8
) || (y.
length
() %
8
!=
0
)) {
y.
insert
(
0
,
"
0
"
);
//
prepend
}
//
invert bits
std::string y2 =
"
"
;
for
(
int
i =
0
; i < (
int
)y.
length
(); i++) y2 += (y[i] ==
'
0
'
?
'
1
'
:
'
0
'
);
//
cout << "rbits: " << y2 << endl;
//
go back to bigint
//
BigInteger by3(y2, 2); // recursive behavior
mpz_class
by3
(y2,
2
);
//
sum 1
mpz_class bby3 = by3 +
1
;
//
cout << "(~y)+1: " << bby3 << endl;
//
convert to binary again
std::string y4 =
csBigIntegerGetBitsFromNonNegativeMPZ
(bby3);
//
cout << "y4bits: " << y4 << endl;
//
padding
while
((y4.
length
() <
8
) || (y4.
length
() %
8
!=
0
)) {
y4.
insert
(
0
,
"
0
"
);
//
prepend
}
//
cout << "y4bits: " << y4 << endl;
//
get in bytes
cs_vbyte v =
Helper::BinToBytes
(y4);
//
cout << "v: " << Helper::toHexString(v) << endl;
//
convert to little-endian
reverse
(v.
begin
(), v.
end
());
//
to little-endian
//
will add extra ff to guarantee its negative, and simplify later (remove
//
extra ff)
v.
push_back
(
0xff
);
cs_vbyte vsimple = v;
while
((vsimple.
size
() >
1
) &&
(vsimple.
at
(((
int
)vsimple.
size
()) -
1
) ==
0xff
)) {
vsimple.
pop_back
();
std::string lehex =
Helper::toHexString
(vsimple);
if
(
Helper::checkNegativeBit
(lehex)) {
//
still negative! can keep removal
v = vsimple;
}
else
break
;
}
//
convert to big-endian
reverse
(v.
begin
(), v.
end
());
//
to big-endian
//
finished
return
v;
}
}
//
taken from csBigInteger.js
/*
Function: parse
Parse a string or little-endian byte array into a <BigInteger>.
- "0x" or "0X": *base* = 16
- "0b": *base* = 2 (?)
- else: *base* = 10
Parameters:
s - The string to parse.
base - Optional (default is 10)
Returns:
a <BigInteger> instance.
*/
//
expects vbyte on little-endian format (raw internal format)
mpz_class
csBigIntegerMPZparse
(cs_vbyte n) {
//
if (n.size() == 0)
//
return mpz_class(0); // never invoked
std::string s =
Helper::toHexString
(n);
//
verify if number is negative (most significant bit)
if
(
Helper::checkNegativeBit
(s)) {
//
is negative, must handle twos-complement
mpz_class
vint
(
Helper::revertHexString
(s),
16
);
//
as big-endian again
//
get bitstring
std::string rbitnum =
csBigIntegerGetBitsFromNonNegativeMPZ
(vint);
//
negate bits
std::stringstream y2;
for
(
int
i =
0
; i < (
int
)rbitnum.
length
(); i++)
y2 << (rbitnum[i] ==
'
0
'
?
'
1
'
:
'
0
'
);
//
to bigint again
mpz_class
by2
(y2.
str
(),
2
);
//
add one
mpz_class by2add = by2 +
1
;
//
get negative
mpz_class finalnum = by2add * (-
1
);
//
ensure it's negative (TODO(igormcoelho): remove)
assert
(finalnum <
0
);
//
must be negative
//
finished
return
finalnum;
}
else
{
//
positive is easy (in little-endian format)
std::string sbig =
Helper::revertHexString
(s);
//
build bignum in big-endian
return
mpz_class
(sbig,
16
);
//
big-endian again
}
}
mpz_class
csBigIntegerMPZparses
(std::string n,
int
base) {
if
(base ==
10
) {
//
if (n.length() == 0)
//
n = "0"; // not necessary
return
mpz_class
(n, base);
}
//
never used! internal function only
//
if (base == 2) {
//
if (n.length() == 0)
//
n = "0";
//
return mpz_class(n, base);
//
}
//
never used! internal function only
//
if (base != 16) {
//
cout << "UNSUPPORTED BASE! " << base << endl;
//
exit(1);
//
}
//
zero padding
while
(n.
length
() <
2
) n.
insert
(
0
,
"
0
"
);
//
return bytearray initialized
cs_vbyte vb;
//
prefix '0x' optional. input always big-endian
if
((n[
0
] ==
'
0
'
) && (n[
1
] ==
'
x
'
)) {
//
removing '0x'
n = n.
substr
(
2
, n.
length
() -
2
);
vb =
Helper::HexToBytes
(n);
}
else
{
vb =
Helper::HexToBytes
(n);
//
directly reading big-endian byte array
}
reverse
(vb.
begin
(), vb.
end
());
//
to little-endian
//
base 16
return
csBigIntegerMPZparse
(vb);
//
vb is little-endian byte array
}
Back
|
FazBrowse Home
|
New Git URL