Security Scol plugin
integer.h
Go to the documentation of this file.
1// integer.h - originally written and placed in the public domain by Wei Dai
2
15
16#ifndef CRYPTOPP_INTEGER_H
17#define CRYPTOPP_INTEGER_H
18
19#include "cryptlib.h"
20#include "secblock.h"
21#include "stdcpp.h"
22
23#include <iosfwd>
24
25NAMESPACE_BEGIN(CryptoPP)
26
27
33
34// Always align, http://github.com/weidai11/cryptopp/issues/256
36
49class CRYPTOPP_DLL Integer : private InitializeInteger, public ASN1Object
50{
51public:
53
54
55 class DivideByZero : public Exception
56 {
57 public:
58 DivideByZero() : Exception(OTHER_ERROR, "Integer: division by zero") {}
59 };
60
64 {
65 public:
66 RandomNumberNotFound() : Exception(OTHER_ERROR, "Integer: no integer satisfies the given parameters") {}
67 };
68
73 enum Sign {
75 POSITIVE=0,
77 NEGATIVE=1};
78
87 SIGNED};
88
95 PRIME};
97
99
100
101 Integer();
102
104 Integer(const Integer& t);
105
107 Integer(signed long value);
108
112 Integer(Sign sign, lword value);
113
118 Integer(Sign sign, word highWord, word lowWord);
119
128 explicit Integer(const char *str, ByteOrder order = BIG_ENDIAN_ORDER);
129
138 explicit Integer(const wchar_t *str, ByteOrder order = BIG_ENDIAN_ORDER);
139
147 Integer(const byte *encodedInteger, size_t byteCount, Signedness sign=UNSIGNED, ByteOrder order = BIG_ENDIAN_ORDER);
148
156 Integer(BufferedTransformation &bt, size_t byteCount, Signedness sign=UNSIGNED, ByteOrder order = BIG_ENDIAN_ORDER);
157
160 explicit Integer(BufferedTransformation &bt);
161
166 Integer(RandomNumberGenerator &rng, size_t bitCount);
167
171 static const Integer & CRYPTOPP_API Zero();
175 static const Integer & CRYPTOPP_API One();
179 static const Integer & CRYPTOPP_API Two();
180
197 Integer(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType=ANY, const Integer &equiv=Zero(), const Integer &mod=One());
198
202 static Integer CRYPTOPP_API Power2(size_t e);
204
206
207
210 size_t MinEncodedSize(Signedness sign=UNSIGNED) const;
211
219 void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const;
220
228 void Encode(BufferedTransformation &bt, size_t outputLen, Signedness sign=UNSIGNED) const;
229
234 void DEREncode(BufferedTransformation &bt) const;
235
239 void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const;
240
247 size_t OpenPGPEncode(byte *output, size_t bufferSize) const;
248
254 size_t OpenPGPEncode(BufferedTransformation &bt) const;
255
260 void Decode(const byte *input, size_t inputLen, Signedness sign=UNSIGNED);
261
267 void Decode(BufferedTransformation &bt, size_t inputLen, Signedness sign=UNSIGNED);
268
272 void BERDecode(const byte *input, size_t inputLen);
273
277
281 void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length);
282
285 {
286 public:
287 OpenPGPDecodeErr() : Exception(INVALID_DATA_FORMAT, "OpenPGP decode error") {}
288 };
289
293 void OpenPGPDecode(const byte *input, size_t inputLen);
296 void OpenPGPDecode(BufferedTransformation &bt);
298
300
301
304 bool IsConvertableToLong() const;
308 signed long ConvertToLong() const;
309
313 unsigned int BitCount() const;
317 unsigned int ByteCount() const;
321 unsigned int WordCount() const;
322
325 bool GetBit(size_t i) const;
328 byte GetByte(size_t i) const;
331 lword GetBits(size_t i, size_t n) const;
332
335 bool IsZero() const {return !*this;}
338 bool NotZero() const {return !IsZero();}
341 bool IsNegative() const {return sign == NEGATIVE;}
344 bool NotNegative() const {return !IsNegative();}
347 bool IsPositive() const {return NotNegative() && NotZero();}
350 bool NotPositive() const {return !IsPositive();}
353 bool IsEven() const {return GetBit(0) == 0;}
356 bool IsOdd() const {return GetBit(0) == 1;}
358
360
361
364 Integer& operator=(const Integer& t);
368 Integer& operator+=(const Integer& t);
372 Integer& operator-=(const Integer& t);
377 Integer& operator*=(const Integer& t) {return *this = Times(t);}
381 Integer& operator/=(const Integer& t) {return *this = DividedBy(t);}
386 Integer& operator%=(const Integer& t) {return *this = Modulo(t);}
390 Integer& operator/=(word t) {return *this = DividedBy(t);}
395 Integer& operator%=(word t) {return *this = Integer(POSITIVE, 0, Modulo(t));}
396
400 Integer& operator<<=(size_t n);
404 Integer& operator>>=(size_t n);
405
417 Integer& operator&=(const Integer& t);
429 Integer& operator|=(const Integer& t);
441 Integer& operator^=(const Integer& t);
442
447 void Randomize(RandomNumberGenerator &rng, size_t bitCount);
448
454 void Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max);
455
472 bool Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType, const Integer &equiv=Zero(), const Integer &mod=One());
473
490 bool GenerateRandomNoThrow(RandomNumberGenerator &rng, const NameValuePairs &params = g_nullNameValuePairs);
491
508 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params = g_nullNameValuePairs)
509 {
510 if (!GenerateRandomNoThrow(rng, params))
511 throw RandomNumberNotFound();
512 }
513
516 void SetBit(size_t n, bool value=1);
517
520 void SetByte(size_t n, byte value);
521
523 void Negate();
524
526 void SetPositive() {sign = POSITIVE;}
527
529 void SetNegative() {if (!!(*this)) sign = NEGATIVE;}
530
532 void swap(Integer &a);
534
536
537
538 bool operator!() const;
540 Integer operator+() const {return *this;}
542 Integer operator-() const;
544 Integer& operator++();
546 Integer& operator--();
548 Integer operator++(int) {Integer temp = *this; ++*this; return temp;}
550 Integer operator--(int) {Integer temp = *this; --*this; return temp;}
552
554
555
560 int Compare(const Integer& a) const;
561
563 Integer Plus(const Integer &b) const;
565 Integer Minus(const Integer &b) const;
568 Integer Times(const Integer &b) const;
570 Integer DividedBy(const Integer &b) const;
573 Integer Modulo(const Integer &b) const;
575 Integer DividedBy(word b) const;
578 word Modulo(word b) const;
579
591 Integer And(const Integer& t) const;
592
604 Integer Or(const Integer& t) const;
605
617 Integer Xor(const Integer& t) const;
618
620 Integer operator>>(size_t n) const {return Integer(*this)>>=n;}
622 Integer operator<<(size_t n) const {return Integer(*this)<<=n;}
624
626
627
628 Integer AbsoluteValue() const;
630 Integer Doubled() const {return Plus(*this);}
633 Integer Squared() const {return Times(*this);}
636 Integer SquareRoot() const;
638 bool IsSquare() const;
639
642 bool IsUnit() const;
645 Integer MultiplicativeInverse() const;
646
653 static void CRYPTOPP_API Divide(Integer &r, Integer &q, const Integer &a, const Integer &d);
654
662 static void CRYPTOPP_API Divide(word &r, Integer &q, const Integer &a, word d);
663
672 static void CRYPTOPP_API DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, unsigned int n);
673
678 static Integer CRYPTOPP_API Gcd(const Integer &a, const Integer &n);
679
686 Integer InverseMod(const Integer &n) const;
687
694 word InverseMod(word n) const;
696
698
699
703 friend CRYPTOPP_DLL std::istream& CRYPTOPP_API operator>>(std::istream& in, Integer &a);
704
715 friend CRYPTOPP_DLL std::ostream& CRYPTOPP_API operator<<(std::ostream& out, const Integer &a);
717
723 CRYPTOPP_DLL friend Integer CRYPTOPP_API a_times_b_mod_c(const Integer &x, const Integer& y, const Integer& m);
729 CRYPTOPP_DLL friend Integer CRYPTOPP_API a_exp_b_mod_c(const Integer &x, const Integer& e, const Integer& m);
730
731protected:
732
733 // http://github.com/weidai11/cryptopp/issues/602
734 Integer InverseModNext(const Integer &n) const;
735
736private:
737
738 Integer(word value, size_t length);
739 int PositiveCompare(const Integer &t) const;
740
741 IntegerSecBlock reg;
742 Sign sign;
743
744#ifndef CRYPTOPP_DOXYGEN_PROCESSING
745 friend class ModularArithmetic;
746 friend class MontgomeryRepresentation;
747 friend class HalfMontgomeryRepresentation;
748
749 friend void PositiveAdd(Integer &sum, const Integer &a, const Integer &b);
750 friend void PositiveSubtract(Integer &diff, const Integer &a, const Integer &b);
751 friend void PositiveMultiply(Integer &product, const Integer &a, const Integer &b);
752 friend void PositiveDivide(Integer &remainder, Integer &quotient, const Integer &dividend, const Integer &divisor);
753#endif
754};
755
757inline bool operator==(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)==0;}
759inline bool operator!=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)!=0;}
761inline bool operator> (const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)> 0;}
763inline bool operator>=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)>=0;}
765inline bool operator< (const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)< 0;}
767inline bool operator<=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)<=0;}
769inline CryptoPP::Integer operator+(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Plus(b);}
771inline CryptoPP::Integer operator-(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Minus(b);}
774inline CryptoPP::Integer operator*(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Times(b);}
776inline CryptoPP::Integer operator/(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.DividedBy(b);}
779inline CryptoPP::Integer operator%(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Modulo(b);}
781inline CryptoPP::Integer operator/(const CryptoPP::Integer &a, CryptoPP::word b) {return a.DividedBy(b);}
784inline CryptoPP::word operator%(const CryptoPP::Integer &a, CryptoPP::word b) {return a.Modulo(b);}
785
798inline CryptoPP::Integer operator&(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.And(b);}
799
812inline CryptoPP::Integer operator|(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Or(b);}
813
826inline CryptoPP::Integer operator^(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Xor(b);}
827
828NAMESPACE_END
829
830#ifndef __BORLANDC__
831NAMESPACE_BEGIN(std)
832inline void swap(CryptoPP::Integer &a, CryptoPP::Integer &b)
833{
834 a.swap(b);
835}
836NAMESPACE_END
837#endif
838
839#endif
Interface for encoding and decoding ASN1 objects.
Definition cryptlib.h:3284
virtual void DEREncode(BufferedTransformation &bt) const =0
Encode this object into a BufferedTransformation.
virtual void BERDecode(BufferedTransformation &bt)=0
Decode this object from a BufferedTransformation.
Interface for buffered transformations.
Definition cryptlib.h:1652
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
Exception thrown when division by 0 is encountered.
Definition integer.h:56
Exception thrown when an error is encountered decoding an OpenPGP integer.
Definition integer.h:285
Exception thrown when a random number cannot be found that satisfies the condition.
Definition integer.h:64
Multiple precision integer with arithmetic operations.
Definition integer.h:50
Integer operator--(int)
Post-decrement.
Definition integer.h:550
Integer & operator/=(const Integer &t)
Division Assignment.
Definition integer.h:381
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random number.
Definition integer.h:508
bool IsPositive() const
Determines if the Integer is positive.
Definition integer.h:347
Integer operator++(int)
Post-increment.
Definition integer.h:548
Integer Doubled() const
Add this integer to itself.
Definition integer.h:630
bool NotZero() const
Determines if the Integer is non-0.
Definition integer.h:338
Integer operator>>(size_t n) const
Right-shift.
Definition integer.h:620
Integer & operator%=(word t)
Remainder Assignment.
Definition integer.h:395
Integer Squared() const
Multiply this integer by itself.
Definition integer.h:633
bool NotPositive() const
Determines if the Integer is non-positive.
Definition integer.h:350
Integer & operator/=(word t)
Division Assignment.
Definition integer.h:390
void SetNegative()
Sets the Integer to negative.
Definition integer.h:529
bool NotNegative() const
Determines if the Integer is non-negative.
Definition integer.h:344
void SetPositive()
Sets the Integer to positive.
Definition integer.h:526
Integer operator+() const
Addition.
Definition integer.h:540
RandomNumberType
Properties of a random integer.
Definition integer.h:91
@ ANY
a number with no special properties
Definition integer.h:93
Integer & operator*=(const Integer &t)
Multiplication Assignment.
Definition integer.h:377
Signedness
Used when importing and exporting integers.
Definition integer.h:83
@ UNSIGNED
an unsigned value
Definition integer.h:85
Integer operator<<(size_t n) const
Left-shift.
Definition integer.h:622
Integer & operator%=(const Integer &t)
Remainder Assignment.
Definition integer.h:386
bool IsZero() const
Determines if the Integer is 0.
Definition integer.h:335
bool IsNegative() const
Determines if the Integer is negative.
Definition integer.h:341
Sign
Used internally to represent the integer.
Definition integer.h:73
bool IsOdd() const
Determines if the Integer is odd parity.
Definition integer.h:356
bool IsEven() const
Determines if the Integer is even parity.
Definition integer.h:353
Ring of congruence classes modulo n.
Definition modarith.h:44
Performs modular arithmetic in Montgomery representation for increased speed.
Definition modarith.h:296
Interface for retrieving values given their names.
Definition cryptlib.h:322
Interface for random number generators.
Definition cryptlib.h:1435
Secure memory block with allocator and cleanup.
Definition secblock.h:731
word64 lword
Large word type.
Definition config_int.h:158
Abstract base classes that provide a uniform interface to this library.
ByteOrder
Provides the byte ordering.
Definition cryptlib.h:143
@ BIG_ENDIAN_ORDER
byte order is big-endian
Definition cryptlib.h:147
CryptoPP::Integer operator*(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Multiplication.
Definition integer.h:774
CryptoPP::Integer operator^(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Bitwise XOR.
Definition integer.h:826
bool operator<(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Comparison.
Definition integer.h:765
bool operator!=(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Comparison.
Definition integer.h:759
CryptoPP::Integer operator-(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Subtraction.
Definition integer.h:771
CryptoPP::Integer operator%(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Remainder.
Definition integer.h:779
bool operator<=(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Comparison.
Definition integer.h:767
CryptoPP::Integer operator|(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Bitwise OR.
Definition integer.h:812
CryptoPP::Integer operator/(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Division.
Definition integer.h:776
CryptoPP::Integer operator&(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Bitwise AND.
Definition integer.h:798
bool operator>=(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Comparison.
Definition integer.h:763
bool operator==(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Comparison.
Definition integer.h:757
CryptoPP::Integer operator+(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Addition.
Definition integer.h:769
bool operator>(const CryptoPP::Integer &a, const CryptoPP::Integer &b)
Comparison.
Definition integer.h:761
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
Definition misc.h:2010
Classes and functions for secure memory allocations.
Common C++ header files.
Performs static initialization of the Integer class.
Definition integer.h:30