-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBigInt.h
More file actions
211 lines (178 loc) · 5.12 KB
/
Copy pathBigInt.h
File metadata and controls
211 lines (178 loc) · 5.12 KB
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
#ifndef __BIGINT_H
#define __BIGINT_H
#include <inttypes.h>
#ifndef BIGINT_PRIMITIVE_SIZE
#define BIGINT_PRIMITIVE_SIZE 32
#endif
#if BIGINT_PRIMITIVE_SIZE == 64
// Values stored in 64-bit primitives
#define TWODIGITS uint64_t
#define SIGNEDTWODIGITS int64_t
#define DIGIT uint32_t
#define SIGNEDDIGIT int32_t
#define TWODIGITS_IS_UL 0
#define TWODIGITS_CONSTYPE uint64_t
#define DIGITBYTES 4
#define DIGITBITS 32
#define DIGITMASK 0xFFFFFFFF
#define DIGITHIGHBIT 0x80000000
#elif BIGINT_PRIMITIVE_SIZE == 32
// Values stored in 32-bit primitives
#define DIGIT uint16_t
#define SIGNEDDIGIT int16_t
#define TWODIGITS uint32_t
#define TWODIGITS_IS_UL 1
#define TWODIGITS_CONSTYPE unsigned long
#define SIGNEDTWODIGITS int32_t
#define DIGITBYTES 2
#define DIGITBITS 16
#define DIGITMASK 0xFFFF
#define DIGITHIGHBIT 0x8000
#else
// Values stored in 16-bit primitives
#define DIGIT uint8_t
#define SIGNEDDIGIT int8_t
#define TWODIGITS uint16_t
#define TWODIGITS_IS_UL 0
#define TWODIGITS_CONSTYPE unsigned long
#define SIGNEDTWODIGITS int16_t
#define DIGITBYTES 1
#define DIGITBITS 8
#define DIGITMASK 0xFF
#define DIGITHIGHBIT 0x80
#endif
#define PARTIALS 64
#define WINDOWSIZE 6
class BigInt
{
public: // constructors & destructors
BigInt();
BigInt(const BigInt&);
BigInt(unsigned char*, long); // defaults to positive
BigInt(unsigned char*, long, bool);
BigInt(long);
BigInt(TWODIGITS_CONSTYPE);
BigInt(const char*);
BigInt(const unsigned char*);
BigInt(long, DIGIT);
~BigInt();
public: // methods
// Assignment
bool operator=(const BigInt&);
bool operator=(const long);
bool operator=(char*);
bool copy_bytes(const unsigned char*, long); // defaults to positive
bool copy_bytes(const unsigned char*, long, bool);
bool use_value(DIGIT*, long); // defaults to positive
bool use_value(DIGIT*, long, bool);
void set_high_bit();
// Addition
const BigInt& operator++(); // prefix
const BigInt operator++(int); // postfix
BigInt operator+(const BigInt&) const;
friend BigInt operator+(long, const BigInt&);
bool operator+=(const BigInt&);
// Subtraction
const BigInt& operator--(); // prefix
const BigInt operator--(int); // postfix
BigInt operator-(const BigInt&) const;
friend BigInt operator-(long, const BigInt&);
bool operator-=(const BigInt&);
// Multiplication
BigInt operator*(const BigInt&) const;
friend BigInt operator*(long, const BigInt&);
bool operator*=(const BigInt&);
bool square();
bool squaremod(const BigInt&);
bool negate();
// Division
BigInt operator/(const BigInt&) const;
friend BigInt operator/(long, const BigInt&);
bool operator/=(const BigInt&);
// Modulation
BigInt operator%(const BigInt&) const;
friend BigInt operator%(long, const BigInt&);
bool operator%=(const BigInt&);
bool multmod(const BigInt&, const BigInt&);
// Exponentiation
BigInt exp(const BigInt&);
BigInt expmod(const BigInt&, const BigInt&) const;
// Multiplicative inverse
BigInt inv(const BigInt&) const;
// Multiplicative inverse
BigInt gcd(const BigInt&) const;
// Comparison
friend bool operator==(long, const BigInt&);
bool operator==(const BigInt&) const;
friend bool operator!=(long, const BigInt&);
bool operator!=(const BigInt&) const;
friend bool operator<(long, const BigInt&);
bool operator<(const BigInt&) const;
friend bool operator<=(long, const BigInt&);
bool operator<=(const BigInt&) const;
friend bool operator>(long, const BigInt&);
bool operator>(const BigInt&) const;
friend bool operator>=(long, const BigInt&);
bool operator>=(const BigInt&) const;
bool is_positive() const;
bool is_negative() const;
bool zero() const;
bool one() const;
bool negative_one() const;
bool odd() const;
bool even() const;
// Shifting
bool operator<<=(long);
bool operator>>=(long);
// Output
long byte_length() const;
unsigned char* byte_array_value() const;
void byte_array_value(unsigned char*) const;
unsigned char* byte_array_value(long) const;
void byte_array_value(unsigned char*, long) const;
long long_value() const;
unsigned long ul_value() const;
char* decimal_string_value() const;
// friend ostream& operator<<(ostream&, const BigInt&);
long MPint_length() const;
unsigned char* MPint_value() const;
void MPint_value(unsigned char*) const;
unsigned long num_bits() const;
private: // methods
// Assignment
bool set_value(long);
bool set_value(unsigned long);
bool set_value(const char*);
bool set_value(const unsigned char*);
bool set_zero();
bool copy_value(DIGIT*, long); // defaults to positive
bool copy_value(DIGIT*, long, bool);
// Addition
bool add_BigInt(const BigInt&);
bool add_digit(DIGIT);
// Subtraction
bool subtract_from_BigInt(const BigInt&);
void subtract_BigInt(const BigInt&);
void subtract_digit(DIGIT);
// Exponentiation
BigInt& get_partial (BigInt**, long, const BigInt&) const;
// Comparison
int value_compare(const BigInt&) const;
// Shifting
bool shift_left_one();
// Utilities
void complement_bytes(unsigned char*, long) const;
bool extend(long digits);
private: // member variables
DIGIT* value;
long msd, lsd;
bool negative;
};
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-file-offsets: ((substatement-open . 0))
* End:
*/