SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
big_integer_base.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_BASE_HEADER_GUARD
30 #define BIG_INTEGER_BASE_HEADER_GUARD
31 
32 //include C++ headers
33 #include <iostream>
34 #include <string>
35 #include <stdexcept>
36 
37 namespace SeComLib {
38 namespace Core {
46  template <typename T_Impl>
49  friend T_Impl;
50 
52  friend typename T_Impl::RandomGeneratorImpl;
53 
54  public:
55  /* Constructors */
56 
58  BigIntegerBase ();
59 
62 
64  BigIntegerBase (const long input);
65 
67  BigIntegerBase (const unsigned long input);
68 
70  BigIntegerBase (const int input);
71 
73  BigIntegerBase (const unsigned int input);
74 
76  BigIntegerBase (const double input, const BigIntegerBase<T_Impl> &scaling, const bool truncate = false);
77 
79  BigIntegerBase (const double input, const unsigned int numberOfDigits, const bool truncate = false);
80 
82  BigIntegerBase (const std::string &input, const int base = 0);
83 
84  /* /Constructors */
85 
87  ~BigIntegerBase ();
88 
89  /* Operator overloading */
90 
94  BigIntegerBase<T_Impl> &operator= (const long input);
96  BigIntegerBase<T_Impl> &operator= (const unsigned long input);
98  BigIntegerBase<T_Impl> &operator= (const int input);
100  BigIntegerBase<T_Impl> &operator= (const unsigned int input);
101 
106 
115 
119  BigIntegerBase<T_Impl> operator+ (const long input) const;
121  BigIntegerBase<T_Impl> operator+ (const unsigned long input) const;
123  BigIntegerBase<T_Impl> operator+ (const int input) const;
125  BigIntegerBase<T_Impl> operator+ (const unsigned int input) const;
129  BigIntegerBase<T_Impl> &operator+= (const long input);
131  BigIntegerBase<T_Impl> &operator+= (const unsigned long input);
133  BigIntegerBase<T_Impl> &operator+= (const int input);
135  BigIntegerBase<T_Impl> &operator+= (const unsigned int input);
136 
140  BigIntegerBase<T_Impl> operator- (const long input) const;
142  BigIntegerBase<T_Impl> operator- (const unsigned long input) const;
144  BigIntegerBase<T_Impl> operator- (const int input) const;
146  BigIntegerBase<T_Impl> operator- (const unsigned int input) const;
150  BigIntegerBase<T_Impl> &operator-= (const long input);
152  BigIntegerBase<T_Impl> &operator-= (const unsigned long input);
154  BigIntegerBase<T_Impl> &operator-= (const int input);
156  BigIntegerBase<T_Impl> &operator-= (const unsigned int input);
157 
161  BigIntegerBase<T_Impl> operator* (const long input) const;
163  BigIntegerBase<T_Impl> operator* (const unsigned long input) const;
165  BigIntegerBase<T_Impl> operator* (const int input) const;
167  BigIntegerBase<T_Impl> operator* (const unsigned int input) const;
171  BigIntegerBase<T_Impl> &operator*= (const long input);
173  BigIntegerBase<T_Impl> &operator*= (const unsigned long input);
175  BigIntegerBase<T_Impl> &operator*= (const int input);
177  BigIntegerBase<T_Impl> &operator*= (const unsigned int input);
178 
182  BigIntegerBase<T_Impl> operator/ (const long input) const;
184  BigIntegerBase<T_Impl> operator/ (const unsigned long input) const;
186  BigIntegerBase<T_Impl> operator/ (const int input) const;
188  BigIntegerBase<T_Impl> operator/ (const unsigned int input) const;
192  BigIntegerBase<T_Impl> &operator/= (const long input);
194  BigIntegerBase<T_Impl> &operator/= (const unsigned long input);
196  BigIntegerBase<T_Impl> &operator/= (const int input);
198  BigIntegerBase<T_Impl> &operator/= (const unsigned int input);
199 
203  BigIntegerBase<T_Impl> operator% (const long input) const;
205  BigIntegerBase<T_Impl> operator% (const unsigned long input) const;
207  BigIntegerBase<T_Impl> operator% (const int input) const;
209  BigIntegerBase<T_Impl> operator% (const unsigned int input) const;
213  BigIntegerBase<T_Impl> &operator%= (const long input);
215  BigIntegerBase<T_Impl> &operator%= (const unsigned long input);
217  BigIntegerBase<T_Impl> &operator%= (const int input);
219  BigIntegerBase<T_Impl> &operator%= (const unsigned int input);
220 
222  bool operator== (const BigIntegerBase<T_Impl> &input) const;
224  bool operator== (const long input) const;
226  bool operator== (const unsigned long input) const;
228  bool operator== (const int input) const;
230  bool operator== (const unsigned int input) const;
231 
233  bool operator!= (const BigIntegerBase<T_Impl> &input) const;
235  bool operator!= (const long input) const;
237  bool operator!= (const unsigned long input) const;
239  bool operator!= (const int input) const;
241  bool operator!= (const unsigned int input) const;
242 
244  bool operator< (const BigIntegerBase<T_Impl> &input) const;
246  bool operator< (const long input) const;
248  bool operator< (const unsigned long input) const;
250  bool operator< (const int input) const;
252  bool operator< (const unsigned int input) const;
253 
255  bool operator<= (const BigIntegerBase<T_Impl> &input) const;
257  bool operator<= (const long input) const;
259  bool operator<= (const unsigned long input) const;
261  bool operator<= (const int input) const;
263  bool operator<= (const unsigned int input) const;
264 
266  bool operator>= (const BigIntegerBase<T_Impl> &input) const;
268  bool operator>= (const long input) const;
270  bool operator>= (const unsigned long input) const;
272  bool operator>= (const int input) const;
274  bool operator>= (const unsigned int input) const;
275 
277  bool operator> (const BigIntegerBase<T_Impl> &input) const;
279  bool operator> (const long input) const;
281  bool operator> (const unsigned long input) const;
283  bool operator> (const int input) const;
285  bool operator> (const unsigned int input) const;
286 
288  BigIntegerBase<T_Impl> operator>> (const unsigned long input) const;
290  BigIntegerBase<T_Impl> &operator>>= (const unsigned long input);
292  BigIntegerBase<T_Impl> operator<< (const unsigned long input) const;
294  BigIntegerBase<T_Impl> &operator<<= (const unsigned long input);
295 
298 
311 
312  /* /Operator overloading */
313 
314  /* Utility methods */
315 
319  bool IsPrime () const;
320 
322  BigIntegerBase<T_Impl> &SetBit (const size_t index);
324  int GetBit (const size_t index) const;
325 
327  size_t GetSize (const unsigned int base = 2) const;
328 
331 
334 
338  BigIntegerBase<T_Impl> &Pow (const long power);
340  BigIntegerBase<T_Impl> &Pow (const unsigned long power);
342  BigIntegerBase<T_Impl> &Pow (const int power);
344  BigIntegerBase<T_Impl> &Pow (const unsigned int power);
345 
349  BigIntegerBase<T_Impl> GetPow (const long power) const;
351  BigIntegerBase<T_Impl> GetPow (const unsigned long power) const;
353  BigIntegerBase<T_Impl> GetPow (const int power) const;
355  BigIntegerBase<T_Impl> GetPow (const unsigned int power) const;
356 
360  BigIntegerBase<T_Impl> &PowModN (const long power, const BigIntegerBase<T_Impl> &n);
362  BigIntegerBase<T_Impl> &PowModN (const unsigned long power, const BigIntegerBase<T_Impl> &n);
364  BigIntegerBase<T_Impl> &PowModN (const int power, const BigIntegerBase<T_Impl> &n);
366  BigIntegerBase<T_Impl> &PowModN (const unsigned int power, const BigIntegerBase<T_Impl> &n);
367 
371  BigIntegerBase<T_Impl> GetPowModN (const long power, const BigIntegerBase<T_Impl> &n) const;
373  BigIntegerBase<T_Impl> GetPowModN (const unsigned long power, const BigIntegerBase<T_Impl> &n) const;
375  BigIntegerBase<T_Impl> GetPowModN (const int power, const BigIntegerBase<T_Impl> &n) const;
377  BigIntegerBase<T_Impl> GetPowModN (const unsigned int power, const BigIntegerBase<T_Impl> &n) const;
378 
381 
384 
385  /* /Utility methods */
386 
387  /* Static utility methods */
388 
390  static void Swap (BigIntegerBase<T_Impl> &lhs, BigIntegerBase<T_Impl> &rhs);
391 
394 
397 
398  /* /Static utility methods */
399 
400  /* Conversion methods */
401 
403  std::string ToString (const unsigned int base = 2) const;
404 
406  unsigned long ToUnsignedLong () const;
407 
408  /* /Conversion methods */
409 
410  private:
412  typename T_Impl::BigIntegerType data;
413  };
414 
415  /* Binary non-member operators */
416 
418  template <typename T_Impl>
419  BigIntegerBase<T_Impl> operator+ (const long lhs, const BigIntegerBase<T_Impl> &rhs);
421  template <typename T_Impl>
422  BigIntegerBase<T_Impl> operator+ (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
424  template <typename T_Impl>
425  BigIntegerBase<T_Impl> operator+ (const int lhs, const BigIntegerBase<T_Impl> &rhs);
427  template <typename T_Impl>
428  BigIntegerBase<T_Impl> operator+ (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
429 
431  template <typename T_Impl>
432  BigIntegerBase<T_Impl> operator- (const long lhs, const BigIntegerBase<T_Impl> &rhs);
434  template <typename T_Impl>
435  BigIntegerBase<T_Impl> operator- (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
437  template <typename T_Impl>
438  BigIntegerBase<T_Impl> operator- (const int lhs, const BigIntegerBase<T_Impl> &rhs);
440  template <typename T_Impl>
441  BigIntegerBase<T_Impl> operator- (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
442 
444  template <typename T_Impl>
445  BigIntegerBase<T_Impl> operator* (const long lhs, const BigIntegerBase<T_Impl> &rhs);
447  template <typename T_Impl>
448  BigIntegerBase<T_Impl> operator* (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
450  template <typename T_Impl>
451  BigIntegerBase<T_Impl> operator* (const int lhs, const BigIntegerBase<T_Impl> &rhs);
453  template <typename T_Impl>
454  BigIntegerBase<T_Impl> operator* (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
455 
457  template <typename T_Impl>
458  BigIntegerBase<T_Impl> operator/ (const long lhs, const BigIntegerBase<T_Impl> &rhs);
460  template <typename T_Impl>
461  BigIntegerBase<T_Impl> operator/ (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
463  template <typename T_Impl>
464  BigIntegerBase<T_Impl> operator/ (const int lhs, const BigIntegerBase<T_Impl> &rhs);
466  template <typename T_Impl>
467  BigIntegerBase<T_Impl> operator/ (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
468 
470  template <typename T_Impl>
471  BigIntegerBase<T_Impl> operator% (const long lhs, const BigIntegerBase<T_Impl> &rhs);
473  template <typename T_Impl>
474  BigIntegerBase<T_Impl> operator% (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
476  template <typename T_Impl>
477  BigIntegerBase<T_Impl> operator% (const int lhs, const BigIntegerBase<T_Impl> &rhs);
479  template <typename T_Impl>
480  BigIntegerBase<T_Impl> operator% (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
481 
483  template <typename T_Impl>
484  bool operator== (const long lhs, const BigIntegerBase<T_Impl> &rhs);
486  template <typename T_Impl>
487  bool operator== (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
489  template <typename T_Impl>
490  bool operator== (const int lhs, const BigIntegerBase<T_Impl> &rhs);
492  template <typename T_Impl>
493  bool operator== (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
494 
496  template <typename T_Impl>
497  bool operator!= (const long lhs, const BigIntegerBase<T_Impl> &rhs);
499  template <typename T_Impl>
500  bool operator!= (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
502  template <typename T_Impl>
503  bool operator!= (const int lhs, const BigIntegerBase<T_Impl> &rhs);
505  template <typename T_Impl>
506  bool operator!= (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
507 
509  template <typename T_Impl>
510  bool operator< (const long lhs, const BigIntegerBase<T_Impl> &rhs);
512  template <typename T_Impl>
513  bool operator< (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
515  template <typename T_Impl>
516  bool operator< (const int lhs, const BigIntegerBase<T_Impl> &rhs);
518  template <typename T_Impl>
519  bool operator< (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
520 
522  template <typename T_Impl>
523  bool operator<= (const long lhs, const BigIntegerBase<T_Impl> &rhs);
525  template <typename T_Impl>
526  bool operator<= (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
528  template <typename T_Impl>
529  bool operator<= (const int lhs, const BigIntegerBase<T_Impl> &rhs);
531  template <typename T_Impl>
532  bool operator<= (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
533 
535  template <typename T_Impl>
536  bool operator>= (const long lhs, const BigIntegerBase<T_Impl> &rhs);
538  template <typename T_Impl>
539  bool operator>= (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
541  template <typename T_Impl>
542  bool operator>= (const int lhs, const BigIntegerBase<T_Impl> &rhs);
544  template <typename T_Impl>
545  bool operator>= (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
546 
548  template <typename T_Impl>
549  bool operator> (const long lhs, const BigIntegerBase<T_Impl> &rhs);
551  template <typename T_Impl>
552  bool operator> (const unsigned long lhs, const BigIntegerBase<T_Impl> &rhs);
554  template <typename T_Impl>
555  bool operator> (const int lhs, const BigIntegerBase<T_Impl> &rhs);
557  template <typename T_Impl>
558  bool operator> (const unsigned int lhs, const BigIntegerBase<T_Impl> &rhs);
559 
560  /* /Binary non-member operators */
561 
562 }//namespace Core
563 }//namespace SeComLib
564 
565 //Separate the implementation from the declaration of template methods
566 #include "big_integer_base.hpp"
567 
568 #endif//BIG_INTEGER_BASE_HEADER_GUARD
BigIntegerBase< T_Impl > & operator++()
Prefix increment unary operator.
BigIntegerBase< T_Impl > & operator--()
Prefix decrement unary operator.
friend T_Impl
The big number implementation requires access to the underlying data.
bool operator>=(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase greater than or equal to binary operator.
BigIntegerBase< T_Impl > & SetBit(const size_t index)
Sets a bit in the current instance at the specified index.
bool operator<(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase less than binary operator.
BigIntegerBase< T_Impl > operator*(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long multiplication binary operator
int GetBit(const size_t index) const
Returns the bit specified by index.
BigIntegerBase< T_Impl > GetNextPrime() const
Computes the first prime greater than the current instance.
bool operator>(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase greater than binary operator.
BigIntegerBase< T_Impl > operator~() const
One's complement unary operator.
std::string ToString(const unsigned int base=2) const
Convert to std::string in the specified base.
BigIntegerBase< T_Impl > operator>>(const unsigned long input) const
Bitwise right shift binary operator.
BigIntegerBase< T_Impl > GetPowModN(const BigIntegerBase< T_Impl > &power, const BigIntegerBase< T_Impl > &n) const
Computes the integer raised to the specified BigIntegerBase power modulo n.
BigIntegerBase< T_Impl > & operator-=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase subtraction & assignment binary operator.
BigIntegerBase< T_Impl > & operator|=(const BigIntegerBase< T_Impl > &input)
Bitwise inclusive OR & assignment binary operator.
bool operator==(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long equality binary operator
unsigned long ToUnsignedLong() const
Convert to unsigned long.
BigIntegerBase< T_Impl > & operator<<=(const unsigned long input)
Bitwise left shift & assignment binary operator.
BigIntegerBase< T_Impl > & operator&=(const BigIntegerBase< T_Impl > &input)
Bitwise AND & assignment binary operator.
BigIntegerBase< T_Impl > & operator>>=(const unsigned long input)
Bitwise right shift & assignment binary operator.
BigIntegerBase< T_Impl > operator&(const BigIntegerBase< T_Impl > &input) const
Bitwise AND binary operator.
bool operator>=(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long greater than or equal binary operator
BigIntegerBase< T_Impl > operator<<(const unsigned long input) const
Bitwise left shift binary operator.
BigIntegerBase< T_Impl > & Abs()
Sets the current instance to its absolute value.
BigIntegerBase< T_Impl > & operator/=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase division & assignment binary operator.
bool IsPrime() const
Tests if the current instance is a prime number.
BigIntegerBase< T_Impl > operator/(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase division binary operator.
size_t GetSize(const unsigned int base=2) const
Gets the length of the integer in the specified base.
BigIntegerBase< T_Impl > operator%(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long modulus binary operator
BigIntegerBase< T_Impl > operator-() const
Unary negation operator.
BigIntegerBase()
Default constructor.
BigIntegerBase< T_Impl > operator+() const
Unary plus operator.
static BigIntegerBase< T_Impl > Gcd(const BigIntegerBase< T_Impl > &lhs, const BigIntegerBase< T_Impl > &rhs)
Computes the greatest common divisor of lhs and rhs.
BigIntegerBase< T_Impl > & Pow(const BigIntegerBase< T_Impl > &power)
Raises the current instance to the specified BigIntegerBase power.
BigIntegerBase< T_Impl > operator|(const BigIntegerBase< T_Impl > &input) const
Bitwise inclusive OR binary operator.
BigIntegerBase< T_Impl > operator^(const BigIntegerBase< T_Impl > &input) const
Bitwise exclusive OR binary operator.
Implementation of class BigIntegerBase. To be included in big_integer_base.h.
T_Impl::BigIntegerType data
Implementation-defined big integer variable.
bool operator==(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase equality binary operator.
BigIntegerBase< T_Impl > GetPow(const BigIntegerBase< T_Impl > &power) const
Computes the integer raised to the specified BigIntegerBase power.
BigIntegerBase< T_Impl > & operator^=(const BigIntegerBase< T_Impl > &input)
Bitwise exclusive OR & assignment binary operator.
bool operator!=(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long inequality binary operator
BigIntegerBase< T_Impl > & PowModN(const BigIntegerBase< T_Impl > &power, const BigIntegerBase< T_Impl > &n)
Raises the current instance to the specified BigIntegerBase power modulo n.
BigIntegerBase< T_Impl > & operator*=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase multiplication & assignment binary operator.
BigIntegerBase< T_Impl > & operator+=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase addition & assignment binary operator.
BigIntegerBase< T_Impl > & operator=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase assignment operator.
BigIntegerBase< T_Impl > GetAbs() const
Computes the absolute value of the current instance.
Template class which adds syntactic sugar to big integer operations.
BigIntegerBase< T_Impl > operator+(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long addition binary operator
bool operator<=(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase less than or equal to binary operator.
static void Swap(BigIntegerBase< T_Impl > &lhs, BigIntegerBase< T_Impl > &rhs)
Swaps lhs with rhs efficiently.
BigIntegerBase< T_Impl > operator*(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase multiplication binary operator.
BigIntegerBase< T_Impl > operator-(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long subtraction binary operator
BigIntegerBase< T_Impl > GetInverseModN(const BigIntegerBase< T_Impl > &n) const
Computes the inverse modulo n.
BigIntegerBase< T_Impl > & operator%=(const BigIntegerBase< T_Impl > &input)
BigIntegerBase modulus & assignment binary operator.
bool operator!=(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase inequality binary operator.
BigIntegerBase< T_Impl > & InvertModN(const BigIntegerBase< T_Impl > &n)
Inverts the current instance modulo n.
static BigIntegerBase< T_Impl > Lcm(const BigIntegerBase< T_Impl > &lhs, const BigIntegerBase< T_Impl > &rhs)
Computes the least common multiple of lhs and rhs.
BigIntegerBase< T_Impl > operator/(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long division binary operator
bool operator>(const long lhs, const BigIntegerBase< T_Impl > &rhs)
long greater than binary operator
BigIntegerBase< T_Impl > operator%(const BigIntegerBase< T_Impl > &input) const
BigIntegerBase modulus binary operator.