1 #ifndef CRYPTOPP_INTEGER_H
2 #define CRYPTOPP_INTEGER_H
12 NAMESPACE_BEGIN(CryptoPP)
46 enum Sign {POSITIVE=0, NEGATIVE=1};
56 enum RandomNumberType {
78 Integer(Sign s, word highWord, word lowWord);
84 explicit Integer(
const char *str);
85 explicit Integer(
const wchar_t *str);
88 Integer(
const byte *encodedInteger,
size_t byteCount, Signedness s=UNSIGNED);
101 static const Integer & CRYPTOPP_API Zero();
103 static const Integer & CRYPTOPP_API One();
105 static const Integer & CRYPTOPP_API Two();
121 static Integer CRYPTOPP_API Power2(
size_t e);
128 size_t MinEncodedSize(Signedness=UNSIGNED)
const;
134 void Encode(byte *output,
size_t outputLen, Signedness=UNSIGNED)
const;
145 size_t OpenPGPEncode(byte *output,
size_t bufferSize)
const;
150 void Decode(
const byte *input,
size_t inputLen, Signedness=UNSIGNED);
156 void BERDecode(
const byte *input,
size_t inputLen);
170 void OpenPGPDecode(
const byte *input,
size_t inputLen);
178 bool IsConvertableToLong()
const;
180 signed long ConvertToLong()
const;
183 unsigned int BitCount()
const;
185 unsigned int ByteCount()
const;
187 unsigned int WordCount()
const;
190 bool GetBit(
size_t i)
const;
192 byte GetByte(
size_t i)
const;
194 lword GetBits(
size_t i,
size_t n)
const;
197 bool IsZero()
const {
return !*
this;}
199 bool NotZero()
const {
return !IsZero();}
201 bool IsNegative()
const {
return sign == NEGATIVE;}
203 bool NotNegative()
const {
return !IsNegative();}
205 bool IsPositive()
const {
return NotNegative() && NotZero();}
207 bool NotPositive()
const {
return !IsPositive();}
209 bool IsEven()
const {
return GetBit(0) == 0;}
211 bool IsOdd()
const {
return GetBit(0) == 1;}
226 Integer& operator/=(
const Integer& t) {
return *
this = DividedBy(t);}
230 Integer& operator/=(word t) {
return *
this = DividedBy(t);}
232 Integer& operator%=(word t) {
return *
this =
Integer(POSITIVE, 0, Modulo(t));}
250 if (!GenerateRandomNoThrow(rng, params))
251 throw RandomNumberNotFound();
255 void SetBit(
size_t n,
bool value=1);
257 void SetByte(
size_t n, byte value);
262 void SetPositive() {sign = POSITIVE;}
264 void SetNegative() {
if (!!(*
this)) sign = NEGATIVE;}
273 bool operator!()
const;
275 Integer operator+()
const {
return *
this;}
283 Integer operator++(
int) {
Integer temp = *
this; ++*
this;
return temp;}
285 Integer operator--(
int) {
Integer temp = *
this; --*
this;
return temp;}
295 int Compare(
const Integer& a)
const;
308 Integer DividedBy(word b)
const;
310 word Modulo(word b)
const;
323 Integer Doubled()
const {
return Plus(*
this);}
325 Integer Squared()
const {
return Times(*
this);}
329 bool IsSquare()
const;
334 Integer MultiplicativeInverse()
const;
344 static void CRYPTOPP_API Divide(word &r,
Integer &q,
const Integer &a, word d);
354 word InverseMod(word n)
const;
360 friend CRYPTOPP_DLL std::istream& CRYPTOPP_API operator>>(std::istream& in,
Integer &a);
362 friend CRYPTOPP_DLL std::ostream& CRYPTOPP_API operator<<(std::ostream& out,
const Integer &a);
368 friend class HalfMontgomeryRepresentation;
370 Integer(word value,
size_t length);
372 int PositiveCompare(
const Integer &t)
const;
383 inline bool operator==(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)==0;}
385 inline bool operator!=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)!=0;}
387 inline bool operator> (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)> 0;}
389 inline bool operator>=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)>=0;}
391 inline bool operator< (
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)< 0;}
393 inline bool operator<=(
const CryptoPP::Integer& a,
const CryptoPP::Integer& b) {
return a.Compare(b)<=0;}
395 inline CryptoPP::Integer operator+(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Plus(b);}
397 inline CryptoPP::Integer operator-(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Minus(b);}
399 inline CryptoPP::Integer operator*(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Times(b);}
401 inline CryptoPP::Integer operator/(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.DividedBy(b);}
403 inline CryptoPP::Integer operator%(
const CryptoPP::Integer &a,
const CryptoPP::Integer &b) {
return a.Modulo(b);}
405 inline CryptoPP::Integer operator/(
const CryptoPP::Integer &a, CryptoPP::word b) {
return a.DividedBy(b);}
407 inline CryptoPP::word operator%(
const CryptoPP::Integer &a, CryptoPP::word b) {
return a.Modulo(b);}
base class for all exceptions thrown by Crypto++
bool operator>=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
bool operator>(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
virtual void DEREncode(BufferedTransformation &bt) const =0
encode this object into a BufferedTransformation, using DER (Distinguished Encoding Rules) ...
a block of memory allocated using A
ring of congruence classes modulo n
interface for random number generators
virtual void BERDecode(BufferedTransformation &bt)=0
decode this object from a BufferedTransformation, using BER (Basic Encoding Rules) ...
interface for encoding and decoding ASN1 objects
multiple precision integer and basic arithmetics
const NameValuePairs & g_nullNameValuePairs
empty set of name-value pairs
bool operator<(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
division by zero exception
do modular arithmetics in Montgomery representation for increased speed
bool operator<=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
interface for retrieving values given their names