big_int.h Source File - ClanLib SDK
Description: big_int.h Source File

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:
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}
clan::BigInt
Big Integer class.
Definition big_int.h:63
clan::BigInt::BigInt
BigInt(uint32_t value)
Constructs a big integer (initialised to value)
clan::BigInt::BigInt
BigInt(const BigInt &other)
Copy constructor.
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::BigInt::operator=
BigInt & operator=(const BigInt &other)
clan::BigInt::is_odd
bool is_odd() const
Returns a true if number is odd.
clan::BigInt::operator-=
BigInt operator-=(const BigInt &b)
Compute this -= b.
clan::BigInt::sqrmod
void sqrmod(const BigInt *m, BigInt *c) const
clan::BigInt::set
void set(int64_t d)
clan::BigInt::neg
void neg(BigInt *b) const
Compute b = -a. 'a' and 'b' may be identical.
clan::BigInt::sqr
void sqr(BigInt *b) const
clan::BigInt::sieve
void sieve(const uint32_t *primes, unsigned int num_primes, std::vector< unsigned char > &sieve)
clan::BigInt::operator+
BigInt operator+(const BigInt &b)
Compute result = this + b.
clan::BigInt::read_unsigned_octets
void read_unsigned_octets(const unsigned char *input_str, unsigned int input_length)
clan::BigInt::operator+=
BigInt operator+=(const BigInt &b)
Compute this += b.
clan::BigInt::zero
void zero()
clan::BigInt::abs
void abs(BigInt *b) const
Compute b = |a|. 'a' and 'b' may be identical.
clan::BigInt::BigInt
BigInt()
Constructs a big integer (initialised to zero)
clan::BigInt::cmp_d
int cmp_d(uint32_t d) const
Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d.
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::get
void get(uint32_t &d)
Gets a value.
clan::BigInt::make_prime
bool make_prime(unsigned int num_bits)
clan::BigInt::BigInt
BigInt(int32_t value)
Constructs a big integer (initialised to value)
clan::BigInt::unsigned_octet_size
int unsigned_octet_size() const
clan::BigInt::set
void set(int32_t d)
Sets a value.
clan::BigInt::get
void get(uint64_t &d)
clan::BigInt::operator%
BigInt operator%(const BigInt &b)
Compute result = this % b.
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::div_2
void div_2(BigInt *c) const
Compute c = a / 2, disregarding the remainder.
clan::BigInt::to_unsigned_octets
void to_unsigned_octets(unsigned char *output_str, unsigned int output_length) const
clan::BigInt::BigInt
BigInt(int64_t value)
Constructs a big integer (initialised to value)
clan::BigInt::random
void random()
Assigns a random value to a.
clan::BigInt::set
void set(uint64_t d)
clan::BigInt::set_bit
void set_bit(unsigned int bit_number, unsigned int value)
clan::BigInt::trailing_zeros
unsigned int trailing_zeros() const
clan::BigInt::exch
void exch(BigInt *mp2)
Exchange mp1 and mp2 without allocating any intermediate memory.
clan::BigInt::exptmod
void exptmod(const BigInt *b, const BigInt *m, BigInt *c) const
Compute c = (a ** b) mod m.
clan::BigInt::div
void div(uint32_t d, BigInt *q, BigInt *r) const
clan::BigInt::significant_bits
int significant_bits() const
clan::BigInt::operator/
BigInt operator/(const BigInt &b)
Compute result = this / b.
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::pprime
bool pprime(int nt) const
Performs nt iteration of the Miller-Rabin probabilistic primality test on a.
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::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::div
void div(const BigInt &b, BigInt *q, BigInt *r) const
Compute q = a / b and r = a mod b.
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::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::get
void get(int64_t &d)
clan::BigInt::~BigInt
~BigInt()
Destructor.
clan::BigInt::set
void set(uint32_t d)
clan::BigInt::get
void get(int32_t &d)
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::is_even
bool is_even() const
Returns a true if number is even.
clan::BigInt::BigInt
BigInt(uint64_t value)
Constructs a big integer (initialised to value)
clan::BigInt::cmp
int cmp(const BigInt *b) const
clan
Definition clanapp.h:36
clan::Key::y
@ y
clan::Key::c
@ c
clan::Key::r
@ r
clan::Key::m
@ m
clan::Key::q
@ q
clan::Key::d
@ d
clan::Key::b
@ b
clan::Key::x
@ x
clan::Key::g
@ g
clan::Key::w
@ w