big_int.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Mark Page
27 ** Michael J. Fromberger
28 */
29
30 // This class is based on the original MPI library (not NSS, because of license restrictions) with some modifications.
31 // Some ideas and algorithms are from NSS (Netscape Security Suite). Where they have been used, the function contains a reference note
32 //
33 // Note, since September 2011, I believe the MPI homepage is now: http://spinning-yarns.org/michael/mpi/
34 // Note, since 2013, the MPI page no longer exists, however the internet archive has the details here: http://web.archive.org/web/20100426001741/http://spinning-yarns.org/michael/mpi/README
35 // The license is as follows
36 // This software was written by Michael J. Fromberger,
37 // http://www.dartmouth.edu/~sting/
38 //
39 // See the MPI home page at
40 // http://www.dartmouth.edu/~sting/mpi/
41 //
42 // This software is in the public domain. It is entirely free, and you
43 // may use it and/or redistribute it for whatever purpose you choose;
44 // however, as free software, it is provided without warranty of any
45 // kind, not even the implied warranty of merchantability or fitness for
46 // a particular purpose.
47
48
49 #pragma once
50
51 #include "../System/cl_platform.h"
52 #include <memory>
53
54 namespace clan
55 {
58 class BigInt_Impl;
59
61 class BigInt
62 {
65 public:
67 BigInt();
68
70 explicit BigInt(uint32_t value);
71
73 explicit BigInt(int32_t value);
74
76 explicit BigInt(uint64_t value);
77
79 explicit BigInt(int64_t value);
80
82 BigInt(const BigInt &other);
83
85 ~BigInt();
86
87 BigInt &operator=(const BigInt& other);
88
92
93 public:
94
98
99 public:
100 void read_unsigned_octets( const unsigned char *input_str, unsigned int input_length);
101
102 void zero();
103
104 bool make_prime(unsigned int num_bits);
105
107 int cmp_z() const;
108
109 void set_bit(unsigned int bit_number, unsigned int value);
110
111 int significant_bits() const;
112
113 void sieve(const uint32_t *primes, unsigned int num_primes, std::vector<unsigned char> &sieve);
114
116 uint32_t mod_d(uint32_t d) const;
117
119 void div_d(uint32_t d, BigInt *q, uint32_t *r) const;
120
126 bool fermat(uint32_t w) const;
127
132 bool pprime(int nt) const;
133
135 void set(int32_t d);
136 void set(uint32_t d);
137 void set(uint64_t d);
138 void set(int64_t d);
139
143 void get(uint32_t &d);
144 void get(uint64_t &d);
145 void get(int64_t &d);
146 void get(int32_t &d);
147
155 void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const;
156
158 void mod(const BigInt *m, BigInt *c) const;
159
165 void div(const BigInt &b, BigInt *q, BigInt *r) const;
166 void div(uint32_t d, BigInt *q, BigInt *r) const;
167
169 BigInt operator + (const BigInt& b);
170 BigInt operator + (uint32_t d);
171
173 BigInt operator += (const BigInt& b);
174 BigInt operator += (uint32_t d);
175
177 BigInt operator - (const BigInt& b);
178 BigInt operator - (uint32_t d);
179
181 BigInt operator -= (const BigInt& b);
182 BigInt operator -= (uint32_t d);
183
185 BigInt operator * (const BigInt& b);
186 BigInt operator * (uint32_t d);
187
189 BigInt operator *= (const BigInt& b);
190 BigInt operator *= (uint32_t d);
191
193 BigInt operator / (const BigInt& b);
194 BigInt operator / (uint32_t d);
195
197 BigInt operator /= (const BigInt& b);
198 BigInt operator /= (uint32_t d);
199
201 BigInt operator % (const BigInt& b);
202 BigInt operator % (uint32_t d);
203
205 BigInt operator %= (const BigInt& b);
206 BigInt operator %= (uint32_t d);
207
208 int cmp(const BigInt *b) const;
209
211 int cmp_d(uint32_t d) const;
212
214 void neg(BigInt *b) const;
215
216 unsigned int trailing_zeros() const;
217
218 void sqrmod(const BigInt *m, BigInt *c) const;
219 void sqr(BigInt *b) const;
220
229 void random();
230
235 void exch(BigInt *mp2);
236
241 bool invmod(const BigInt *m, BigInt *c) const;
242
247 void xgcd(const BigInt *b, BigInt *g, BigInt *x, BigInt *y) const;
248
250 void abs(BigInt *b) const;
251
253 bool is_even() const;
254
256 bool is_odd() const;
257
259 void div_2(BigInt *c) const;
260
261 void to_unsigned_octets( unsigned char *output_str, unsigned int output_length) const;
262
263 int unsigned_octet_size() const;
267 private:
268 std::unique_ptr<BigInt_Impl> impl;
269
271
272 };
273
274 }
275
277
clan::BigInt::div
void div(const BigInt &b, BigInt *q, BigInt *r) const
Compute q = a / b and r = a mod b.
clan
Definition: clanapp.h:35
clan::BigInt::operator=
BigInt & operator=(const BigInt &other)
clan::BigInt::cmp_d
int cmp_d(uint32_t d) const
Compare a <=> d. Returns <0 if a0 if a>d.
clan::BigInt::operator-
BigInt operator-(const BigInt &b)
Compute result = this - b.
clan::Key::q
clan::BigInt::sqr
void sqr(BigInt *b) const
clan::BigInt::operator+=
BigInt operator+=(const BigInt &b)
Compute this += b.
clan::BigInt::exptmod
void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const
Compute c = (a ** b) mod m.
clan::BigInt::set_bit
void set_bit(unsigned int bit_number, unsigned int value)
clan::BigInt::pprime
bool pprime(int nt) const
Performs nt iteration of the Miller-Rabin probabilistic primality test on a.
clan::BigInt::unsigned_octet_size
int unsigned_octet_size() const
clan::Key::x
clan::BigInt::make_prime
bool make_prime(unsigned int num_bits)
clan::Key::g
clan::BigInt::to_unsigned_octets
void to_unsigned_octets(unsigned char *output_str, unsigned int output_length) const
clan::BigInt::operator/
BigInt operator/(const BigInt &b)
Compute result = this / b.
clan::Key::d
clan::BigInt::is_odd
bool is_odd() const
Returns a true if number is odd.
clan::BigInt::mod_d
uint32_t mod_d(uint32_t d) const
Compute c = a (mod d). Result will always be 0 <= c < d.
clan::BigInt::set
void set(int32_t d)
Sets a value.
clan::BigInt::cmp_z
int cmp_z() const
Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
clan::Key::r
clan::BigInt::exch
void exch(BigInt *mp2)
Exchange mp1 and mp2 without allocating any intermediate memory.
clan::Key::m
clan::BigInt::div_d
void div_d(uint32_t d, BigInt *q, uint32_t *r) const
Compute the quotient q = a / d and remainder r = a mod d, for a single digit d. Respects the sign of ...
clan::BigInt::operator*=
BigInt operator*=(const BigInt &b)
Compute this *= b.
clan::BigInt::mod
void mod(const BigInt *m, BigInt *c) const
Compute c = a (mod m). Result will always be 0 <= c < m.
clan::Key::y
clan::BigInt::div_2
void div_2(BigInt *c) const
Compute c = a / 2, disregarding the remainder.
clan::BigInt::read_unsigned_octets
void read_unsigned_octets(const unsigned char *input_str, unsigned int input_length)
clan::BigInt::sqrmod
void sqrmod(const BigInt *m, BigInt *c) const
clan::BigInt::cmp
int cmp(const BigInt *b) const
clan::BigInt::is_even
bool is_even() const
Returns a true if number is even.
clan::BigInt::fermat
bool fermat(uint32_t w) const
Using w as a witness, try pseudo-primality testing based on Fermat's little theorem.
clan::BigInt::operator%=
BigInt operator%=(const BigInt &b)
Compute this %= b.
clan::BigInt::xgcd
void xgcd(const BigInt *b, BigInt *g, BigInt *x, BigInt *y) const
Compute g = (a, b) and values x and y satisfying Bezout's identity.
clan::BigInt::operator*
BigInt operator*(const BigInt &b)
Compute result = this * b.
clan::BigInt::significant_bits
int significant_bits() const
clan::BigInt::invmod
bool invmod(const BigInt *m, BigInt *c) const
Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
clan::BigInt::operator%
BigInt operator%(const BigInt &b)
Compute result = this % b.
clan::Key::b
clan::Key::c
clan::BigInt::trailing_zeros
unsigned int trailing_zeros() const
clan::BigInt::BigInt
BigInt()
Constructs a big integer (initialised to zero)
clan::BigInt::operator/=
BigInt operator/=(const BigInt &b)
Compute this /= b.
clan::BigInt::operator+
BigInt operator+(const BigInt &b)
Compute result = this + b.
clan::BigInt::random
void random()
Assigns a random value to a.
clan::BigInt::neg
void neg(BigInt *b) const
Compute b = -a. 'a' and 'b' may be identical.
clan::BigInt::abs
void abs(BigInt *b) const
Compute b = |a|. 'a' and 'b' may be identical.
clan::Key::w
clan::BigInt::operator-=
BigInt operator-=(const BigInt &b)
Compute this -= b.
clan::BigInt::~BigInt
~BigInt()
Destructor.
clan::BigInt
Big Integer class.
Definition: big_int.h:61
clan::BigInt::sieve
void sieve(const uint32_t *primes, unsigned int num_primes, std::vector< unsigned char > &sieve)
clan::BigInt::zero
void zero()