big_int.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 
59  class BigInt_Impl;
60 
62  class BigInt
63  {
64  public:
66  BigInt();
67 
69  explicit BigInt(uint32_t value);
70 
72  explicit BigInt(int32_t value);
73 
75  explicit BigInt(uint64_t value);
76 
78  explicit BigInt(int64_t value);
79 
81  BigInt(const BigInt &other);
82 
84  ~BigInt();
85 
86  BigInt &operator=(const BigInt& other);
87 
88  void read_unsigned_octets(const unsigned char *input_str, unsigned int input_length);
89 
90  void zero();
91 
92  bool make_prime(unsigned int num_bits);
93 
95  int cmp_z() const;
96 
97  void set_bit(unsigned int bit_number, unsigned int value);
98 
99  int significant_bits() const;
100 
101  void sieve(const uint32_t *primes, unsigned int num_primes, std::vector<unsigned char> &sieve);
102 
104  uint32_t mod_d(uint32_t d) const;
105 
107  void div_d(uint32_t d, BigInt *q, uint32_t *r) const;
108 
114  bool fermat(uint32_t w) const;
115 
120  bool pprime(int nt) const;
121 
123  void set(int32_t d);
124  void set(uint32_t d);
125  void set(uint64_t d);
126  void set(int64_t d);
127 
131  void get(uint32_t &d);
132  void get(uint64_t &d);
133  void get(int64_t &d);
134  void get(int32_t &d);
135 
143  void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const;
144 
146  void mod(const BigInt *m, BigInt *c) const;
147 
153  void div(const BigInt &b, BigInt *q, BigInt *r) const;
154  void div(uint32_t d, BigInt *q, BigInt *r) const;
155 
157  BigInt operator + (const BigInt& b);
158  BigInt operator + (uint32_t d);
159 
161  BigInt operator += (const BigInt& b);
162  BigInt operator += (uint32_t d);
163 
165  BigInt operator - (const BigInt& b);
166  BigInt operator - (uint32_t d);
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 
196  int cmp(const BigInt *b) const;
197 
199  int cmp_d(uint32_t d) const;
200 
202  void neg(BigInt *b) const;
203 
204  unsigned int trailing_zeros() const;
205 
206  void sqrmod(const BigInt *m, BigInt *c) const;
207  void sqr(BigInt *b) const;
208 
217  void random();
218 
223  void exch(BigInt *mp2);
224 
229  bool invmod(const BigInt *m, BigInt *c) const;
230 
235  void xgcd(const BigInt *b, BigInt *g, BigInt *x, BigInt *y) const;
236 
238  void abs(BigInt *b) const;
239 
241  bool is_even() const;
242 
244  bool is_odd() const;
245 
247  void div_2(BigInt *c) const;
248 
249  void to_unsigned_octets(unsigned char *output_str, unsigned int output_length) const;
250 
251  int unsigned_octet_size() const;
252 
253  private:
254  std::unique_ptr<BigInt_Impl> impl;
255  };
256 
258 }
BigInt operator*=(const BigInt &b)
Compute this *= b.
uint32_t mod_d(uint32_t d) const
Compute c = a (mod d). Result will always be 0 <= c < d.
Definition: clanapp.h:35
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 operator+=(const BigInt &b)
Compute this += b.
bool is_odd() const
Returns a true if number is odd.
void div(const BigInt &b, BigInt *q, BigInt *r) const
Compute q = a / b and r = a mod b.
int unsigned_octet_size() const
void sieve(const uint32_t *primes, unsigned int num_primes, std::vector< unsigned char > &sieve)
void exch(BigInt *mp2)
Exchange mp1 and mp2 without allocating any intermediate memory.
void abs(BigInt *b) const
Compute b = |a|. 'a' and 'b' may be identical.
BigInt operator%=(const BigInt &b)
Compute this %= b.
~BigInt()
Destructor.
void div_2(BigInt *c) const
Compute c = a / 2, disregarding the remainder.
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 operator%(const BigInt &b)
Compute result = this % b.
bool fermat(uint32_t w) const
Using w as a witness, try pseudo-primality testing based on Fermat's little theorem.
void random()
Assigns a random value to a.
BigInt operator-=(const BigInt &b)
Compute this -= b.
BigInt operator+(const BigInt &b)
Compute result = this + b.
int cmp_d(uint32_t d) const
Compare a <=> d. Returns <0 if a0 if a>d.
void set(int32_t d)
Sets a value.
void neg(BigInt *b) const
Compute b = -a. 'a' and 'b' may be identical.
void read_unsigned_octets(const unsigned char *input_str, unsigned int input_length)
void sqrmod(const BigInt *m, BigInt *c) const
BigInt & operator=(const BigInt &other)
BigInt operator/(const BigInt &b)
Compute result = this / b.
bool make_prime(unsigned int num_bits)
bool invmod(const BigInt *m, BigInt *c) const
Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
BigInt operator*(const BigInt &b)
Compute result = this * b.
void mod(const BigInt *m, BigInt *c) const
Compute c = a (mod m). Result will always be 0 <= c < m.
int cmp_z() const
Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
BigInt operator-(const BigInt &b)
Compute result = this - b.
void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const
Compute c = (a ** b) mod m.
BigInt operator/=(const BigInt &b)
Compute this /= b.
unsigned int trailing_zeros() const
int significant_bits() const
void set_bit(unsigned int bit_number, unsigned int value)
int cmp(const BigInt *b) const
void to_unsigned_octets(unsigned char *output_str, unsigned int output_length) const
bool pprime(int nt) const
Performs nt iteration of the Miller-Rabin probabilistic primality test on a.
bool is_even() const
Returns a true if number is even.
BigInt()
Constructs a big integer (initialised to zero)
Big Integer class.
Definition: big_int.h:62
void sqr(BigInt *b) const