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 
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 
170  BigInt operator + (uint32_t d);
171 
174  BigInt operator += (uint32_t d);
175 
178  BigInt operator - (uint32_t d);
179 
182  BigInt operator -= (uint32_t d);
183 
186  BigInt operator * (uint32_t d);
187 
190  BigInt operator *= (uint32_t d);
191 
194  BigInt operator / (uint32_t d);
195 
198  BigInt operator /= (uint32_t d);
199 
202  BigInt operator % (uint32_t d);
203 
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 
bool fermat(uint32_t w) const
Using w as a witness, try pseudo-primality testing based on Fermat's little theorem.
void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const
Compute c = (a ** b) mod m.
BigInt operator%(const BigInt &b)
Compute result = this % b.
void to_unsigned_octets(unsigned char *output_str, unsigned int output_length) const
BigInt(uint64_t value)
Constructs a big integer (initialised to value)
~BigInt()
Destructor.
int unsigned_octet_size() const
BigInt operator*=(const BigInt &b)
Compute this *= b.
bool is_odd() const
Returns a true if number is odd.
uint32_t mod_d(uint32_t d) const
Compute c = a (mod d). Result will always be 0 <= c < d.
unsigned int trailing_zeros() const
BigInt(int32_t value)
Constructs a big integer (initialised to value)
BigInt operator/(const BigInt &b)
Compute result = this / b.
void set(int32_t d)
Sets a value.
bool invmod(const BigInt *m, BigInt *c) const
Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
void exch(BigInt *mp2)
Exchange mp1 and mp2 without allocating any intermediate memory.
BigInt operator-(const BigInt &b)
Compute result = this - b.
int cmp(const BigInt *b) const
void div(const BigInt &b, BigInt *q, BigInt *r) const
Compute q = a / b and r = a mod b.
void get(int32_t &d)
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.
BigInt()
Constructs a big integer (initialised to zero)
int cmp_z() const
Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
void div_2(BigInt *c) const
Compute c = a / 2, disregarding the remainder.
Big Integer class.
Definition: big_int.h:62
int significant_bits() const
void div(uint32_t d, BigInt *q, BigInt *r) const
BigInt(uint32_t value)
Constructs a big integer (initialised to value)
void random()
Assigns a random value to a.
void get(uint32_t &d)
Gets a value.
BigInt operator%=(const BigInt &b)
Compute this %= b.
void set(uint64_t d)
void get(int64_t &d)
void mod(const BigInt *m, BigInt *c) const
Compute c = a (mod m). Result will always be 0 <= c < m.
Definition: clanapp.h:36
int cmp_d(uint32_t d) const
Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d.
void abs(BigInt *b) const
Compute b = |a|. 'a' and 'b' may be identical.
void set(uint32_t d)
void sqrmod(const BigInt *m, BigInt *c) const
BigInt operator+=(const BigInt &b)
Compute this += b.
void sieve(const uint32_t *primes, unsigned int num_primes, std::vector< unsigned char > &sieve)
BigInt operator/=(const BigInt &b)
Compute this /= b.
bool pprime(int nt) const
Performs nt iteration of the Miller-Rabin probabilistic primality test on a.
void set_bit(unsigned int bit_number, unsigned int value)
BigInt(int64_t value)
Constructs a big integer (initialised to value)
void sqr(BigInt *b) const
void read_unsigned_octets(const unsigned char *input_str, unsigned int input_length)
BigInt operator*(const BigInt &b)
Compute result = this * b.
bool is_even() const
Returns a true if number is even.
void neg(BigInt *b) const
Compute b = -a. 'a' and 'b' may be identical.
BigInt operator+(const BigInt &b)
Compute result = this + b.
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 ...
BigInt(const BigInt &other)
Copy constructor.
bool make_prime(unsigned int num_bits)
void set(int64_t d)
BigInt operator-=(const BigInt &b)
Compute this -= b.
BigInt & operator=(const BigInt &other)
void get(uint64_t &d)