big_int.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
54namespace clan
55{
58
59 class BigInt_Impl;
60
62 class BigInt
63 {
64 public:
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
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
159
163
167
171
175
179
183
187
191
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
252
253 private:
254 std::unique_ptr<BigInt_Impl> impl;
255 };
256
258}
Big Integer class.
Definition: big_int.h:63
BigInt(uint32_t value)
Constructs a big integer (initialised to value)
BigInt(const BigInt &other)
Copy constructor.
BigInt operator%=(const BigInt &b)
Compute this %= b.
void mod(const BigInt *m, BigInt *c) const
Compute c = a (mod m). Result will always be 0 <= c < m.
BigInt & operator=(const BigInt &other)
bool is_odd() const
Returns a true if number is odd.
BigInt operator-=(const BigInt &b)
Compute this -= b.
void sqrmod(const BigInt *m, BigInt *c) const
void set(int64_t d)
void neg(BigInt *b) const
Compute b = -a. 'a' and 'b' may be identical.
void sqr(BigInt *b) const
void sieve(const uint32_t *primes, unsigned int num_primes, std::vector< unsigned char > &sieve)
BigInt operator+(const BigInt &b)
Compute result = this + b.
void read_unsigned_octets(const unsigned char *input_str, unsigned int input_length)
BigInt operator+=(const BigInt &b)
Compute this += b.
void abs(BigInt *b) const
Compute b = |a|. 'a' and 'b' may be identical.
BigInt()
Constructs a big integer (initialised to zero)
int cmp_d(uint32_t d) const
Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d.
bool fermat(uint32_t w) const
Using w as a witness, try pseudo-primality testing based on Fermat's little theorem.
void get(uint32_t &d)
Gets a value.
bool make_prime(unsigned int num_bits)
BigInt(int32_t value)
Constructs a big integer (initialised to value)
int unsigned_octet_size() const
void set(int32_t d)
Sets a value.
void get(uint64_t &d)
BigInt operator%(const BigInt &b)
Compute result = this % b.
bool invmod(const BigInt *m, BigInt *c) const
Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
void div_2(BigInt *c) const
Compute c = a / 2, disregarding the remainder.
void to_unsigned_octets(unsigned char *output_str, unsigned int output_length) const
BigInt(int64_t value)
Constructs a big integer (initialised to value)
void random()
Assigns a random value to a.
void set(uint64_t d)
void set_bit(unsigned int bit_number, unsigned int value)
unsigned int trailing_zeros() const
void exch(BigInt *mp2)
Exchange mp1 and mp2 without allocating any intermediate memory.
void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const
Compute c = (a ** b) mod m.
void div(uint32_t d, BigInt *q, BigInt *r) const
int significant_bits() const
BigInt operator/(const BigInt &b)
Compute result = this / b.
BigInt operator*=(const BigInt &b)
Compute this *= b.
BigInt operator*(const BigInt &b)
Compute result = this * b.
bool pprime(int nt) const
Performs nt iteration of the Miller-Rabin probabilistic primality test on a.
int cmp_z() const
Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
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.
void div(const BigInt &b, BigInt *q, BigInt *r) const
Compute q = a / b and r = a mod b.
uint32_t mod_d(uint32_t d) const
Compute c = a (mod d). Result will always be 0 <= c < 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 ...
void get(int64_t &d)
~BigInt()
Destructor.
void set(uint32_t d)
void get(int32_t &d)
BigInt operator/=(const BigInt &b)
Compute this /= b.
BigInt operator-(const BigInt &b)
Compute result = this - b.
bool is_even() const
Returns a true if number is even.
BigInt(uint64_t value)
Constructs a big integer (initialised to value)
int cmp(const BigInt *b) const
Definition: clanapp.h:36