SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
big_integer_gmp.h
Go to the documentation of this file.
1 /*
2 SeComLib
3 Copyright 2012-2013 TU Delft, Information Security & Privacy Lab (http://isplab.tudelft.nl/)
4 
5 Contributors:
6 Inald Lagendijk (R.L.Lagendijk@TUDelft.nl)
7 Mihai Todor (todormihai@gmail.com)
8 Thijs Veugen (P.J.M.Veugen@tudelft.nl)
9 Zekeriya Erkin (z.erkin@tudelft.nl)
10 
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14 
15 http://www.apache.org/licenses/LICENSE-2.0
16 
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
29 #ifndef BIG_INTEGER_GMP_HEADER_GUARD
30 #define BIG_INTEGER_GMP_HEADER_GUARD
31 
32 #include "big_integer_base.h"
33 
34 //include C++ headers
35 #include <iostream>
36 #include <string>
37 #include <stdexcept>
38 
39 //include 3rd party library headers
40 #if defined(LIB_GMP)
41  #if defined(_WIN32)
42  #pragma warning(push)
43  #pragma warning(disable: 4127)//disable "warning C4127: conditional expression is constant"
44  #pragma warning(disable: 4146)//disable "warning C4146: unary minus operator applied to unsigned type, result still unsigned"
45  #include "gmp/gmp.h"
46  #pragma warning(pop)//restore warnings
47  #else
48  #include "gmp.h"
49  #endif
50 #elif defined(LIB_MPIR)
51  #if defined(_WIN32)
52  #pragma warning(push)
53  #pragma warning(disable: 4127)//disable "warning C4127: conditional expression is constant"
54  #include "mpir/mpir.h"
55  #pragma warning(pop)//restore warnings
56  #else
57  #include "mpir.h"
58  #endif
59 #endif
60 
61 namespace SeComLib {
62 namespace Core {
70  #define MILLER_RABIN_PRIMALITY_TEST_COUNT 10
71 
72  //forward-declare the GMP random provider wrapper
73  class RandomProviderGmp;
74 
78  class BigIntegerGmp {
79  public:
81  typedef mpz_t BigIntegerType;
82 
85 
87  static void Initialize (BigIntegerBase<BigIntegerGmp> &input);
91  static void Initialize (BigIntegerBase<BigIntegerGmp> &input, const long value);
93  static void Initialize (BigIntegerBase<BigIntegerGmp> &input, const unsigned long value);
95  static void Initialize (BigIntegerBase<BigIntegerGmp> &input, const double value, const BigIntegerBase<BigIntegerGmp> &scaling, const bool truncate = false);
97  static void Initialize (BigIntegerBase<BigIntegerGmp> &input, const double value, const unsigned int numberOfDigits, const bool truncate = false);
99  static void Initialize (BigIntegerBase<BigIntegerGmp> &input, const std::string &value, const int base = 0);
100 
102  static void Destroy (BigIntegerBase<BigIntegerGmp> &input);
103 
105  static void Set (BigIntegerBase<BigIntegerGmp> &input, const BigIntegerBase<BigIntegerGmp> &value);
107  static void Set (BigIntegerBase<BigIntegerGmp> &input, const long value);
109  static void Set (BigIntegerBase<BigIntegerGmp> &input, const unsigned long value);
110 
112  static void InvertSign (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input);
113 
117  static void Add (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const long rhs);
119  static void Add (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const unsigned long rhs);
120 
124  static void Subtract (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const long rhs);
126  static void Subtract (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const unsigned long rhs);
127 
131  static void Multiply (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const long rhs);
133  static void Multiply (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const unsigned long rhs);
134 
138  static void Divide (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const long rhs);
140  static void Divide (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &lhs, const unsigned long rhs);
141 
145  static void Modulo (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const long n);
147  static void Modulo (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const unsigned long n);
148 
150  static int Compare (const BigIntegerBase<BigIntegerGmp> &lhs, const BigIntegerBase<BigIntegerGmp> &rhs);
152  static int Compare (const BigIntegerBase<BigIntegerGmp> &lhs, const long rhs);
154  static int Compare (const BigIntegerBase<BigIntegerGmp> &lhs, const unsigned long rhs);
155 
157  static void RightShift (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const unsigned long numberOfBits);
159  static void LeftShift (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const unsigned long numberOfBits);
160 
162  static void InvertBits (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input);
163 
170 
174  static bool IsPrime (const BigIntegerBase<BigIntegerGmp> &input);
175 
177  static void SetBit (BigIntegerBase<BigIntegerGmp> &input, const size_t index);
179  static int GetBit (const BigIntegerBase<BigIntegerGmp> &input, const size_t index);
180 
182  static size_t GetSize (const BigIntegerBase<BigIntegerGmp> &input, const unsigned int base = 2);
183 
185  static void Abs (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input);
186 
188  static void Pow (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const BigIntegerBase<BigIntegerGmp> &power);
190  static void Pow (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const long power);
192  static void Pow (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const unsigned long power);
193 
197  static void PowModN (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const long power, const BigIntegerBase<BigIntegerGmp> &n);
199  static void PowModN (BigIntegerBase<BigIntegerGmp> &output, const BigIntegerBase<BigIntegerGmp> &input, const unsigned long power, const BigIntegerBase<BigIntegerGmp> &n);
200 
203 
206 
211 
213  static std::string ToString (const BigIntegerBase<BigIntegerGmp> &input, const unsigned int base = 2);
215  static unsigned long ToUnsignedLong (const BigIntegerBase<BigIntegerGmp> &input);
216  };
217 
218 
219 }//namespace Core
220 }//namespace SeComLib
221 
222 #endif//BIG_INTEGER_GMP_HEADER_GUARD
static void Modulo(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input, const BigIntegerBase< BigIntegerGmp > &n)
Computes input mod n.
static void Lcm(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Computes the least common multiple of lhs and rhs.
static void LeftShift(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input, const unsigned long numberOfBits)
Bitwise left shift input by numberOfBits.
Wrapper for the required GMP library random number specific functions.
static void Gcd(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Computes the greatest common divisor of lhs and rhs.
static void Swap(BigIntegerBase< BigIntegerGmp > &lhs, BigIntegerBase< BigIntegerGmp > &rhs)
Swaps lhs with rhs efficiently.
Definition of class BigIntegerBase.
RandomProviderGmp RandomGeneratorImpl
Generic alias required by BigIntegerBase to grant the GMP random provider wrapper access to its priva...
static void InvertModN(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input, const BigIntegerBase< BigIntegerGmp > &n)
Inverts input modulo n.
static void Destroy(BigIntegerBase< BigIntegerGmp > &input)
Destroys the underlying data from input.
static int GetBit(const BigIntegerBase< BigIntegerGmp > &input, const size_t index)
Returns the bit specified by index.
static void Subtract(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Subtracts rhs from lhs.
static void Pow(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input, const BigIntegerBase< BigIntegerGmp > &power)
Raises input to the specified power.
static void Initialize(BigIntegerBase< BigIntegerGmp > &input)
Initializes the underlying data from input.
static void BitwiseXor(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Computes lhs bitwise exclusive OR rhs.
static void BitwiseOr(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Computes lhs bitwise inclusive OR rhs.
static void InvertSign(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input)
Inverts the sign of input.
static void RightShift(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input, const unsigned long numberOfBits)
Bitwise right shift input by numberOfBits.
static void Abs(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input)
Computes the absolute value of input.
static size_t GetSize(const BigIntegerBase< BigIntegerGmp > &input, const unsigned int base=2)
Gets the length of the integer in the specified base.
static bool IsPrime(const BigIntegerBase< BigIntegerGmp > &input)
Tests if input is a prime number.
static void InvertBits(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input)
Computes one's complement of input.
static void Add(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Adds lhs and rhs.
static std::string ToString(const BigIntegerBase< BigIntegerGmp > &input, const unsigned int base=2)
Convert input to std::string in the specified base.
static void PowModN(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input, const BigIntegerBase< BigIntegerGmp > &power, const BigIntegerBase< BigIntegerGmp > &n)
Raises input to the specified power modulo n.
static void Multiply(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Multiplies lhs with rhs.
static void Divide(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Divides lhs by rhs.
mpz_t BigIntegerType
Generic alias required by BigIntegerBase to define the underlying data member.
static void Set(BigIntegerBase< BigIntegerGmp > &input, const BigIntegerBase< BigIntegerGmp > &value)
Sets input to the specified BigInteger value.
static void GetNextPrime(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &input)
Computes the first prime greater than the current instance.
static void BitwiseAnd(BigIntegerBase< BigIntegerGmp > &output, const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Computes lhs bitwise AND rhs.
Wrapper class for the most common functions related to the mpz_t datatype of the GMP library...
Template class which adds syntactic sugar to big integer operations.
static unsigned long ToUnsignedLong(const BigIntegerBase< BigIntegerGmp > &input)
Convert input to unsigned long.
static int Compare(const BigIntegerBase< BigIntegerGmp > &lhs, const BigIntegerBase< BigIntegerGmp > &rhs)
Compares lhs to rhs.
static void SetBit(BigIntegerBase< BigIntegerGmp > &input, const size_t index)
Sets a bit in input at the specified index.