File indexing completed on 2025-03-02 04:26:33

0001 /*
0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
0003 
0004 Redistribution and use in source and binary forms, for any use, with or without
0005 modification, is permitted provided that the following conditions are met:
0006 
0007 1. Redistributions of source code must retain the above copyright notice, this
0008 list of conditions, and the following disclaimer.
0009 
0010 2. Redistributions in binary form must reproduce the above copyright notice,
0011 this list of conditions, and the following disclaimer in the documentation
0012 and/or other materials provided with the distribution.
0013 
0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
0017 
0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 // LICENSEHEADER_END
0027 namespace QCA { // WRAPNS_LINE
0028 /*************************************************
0029  * BigInt Header File                             *
0030  * (C) 1999-2007 The Botan Project                *
0031  *************************************************/
0032 
0033 #ifndef BOTAN_BIGINT_H__
0034 #define BOTAN_BIGINT_H__
0035 
0036 #ifdef BOTAN_MINIMAL_BIGINT
0037 } // WRAPNS_LINE
0038 #include <botan/secmem.h>
0039 namespace QCA { // WRAPNS_LINE
0040 } // WRAPNS_LINE
0041 #include <botan/exceptn.h>
0042 namespace QCA { // WRAPNS_LINE
0043 #else
0044 } // WRAPNS_LINE
0045 #include <botan/base.h>
0046 namespace QCA { // WRAPNS_LINE
0047 #endif
0048 
0049 } // WRAPNS_LINE
0050 #include <botan/mp_types.h>
0051 namespace QCA { // WRAPNS_LINE
0052 } // WRAPNS_LINE
0053 #include <iosfwd>
0054 namespace QCA { // WRAPNS_LINE
0055 
0056 namespace Botan {
0057 
0058 /*************************************************
0059  * BigInt                                         *
0060  *************************************************/
0061 class BigInt // clazy:exclude=rule-of-three TODO Needs checking if a real bug or not
0062 {
0063 public:
0064     enum Base
0065     {
0066         Octal       = 8,
0067         Decimal     = 10,
0068         Hexadecimal = 16,
0069         Binary      = 256
0070     };
0071     enum Sign
0072     {
0073         Negative = 0,
0074         Positive = 1
0075     };
0076     enum NumberType
0077     {
0078         Random,
0079         Power2
0080     };
0081 
0082     struct DivideByZero : public Exception
0083     {
0084         DivideByZero()
0085             : Exception("BigInt divide by zero")
0086         {
0087         }
0088     };
0089 
0090     BigInt &operator+=(const BigInt &);
0091     BigInt &operator-=(const BigInt &);
0092 
0093     BigInt &operator*=(const BigInt &);
0094     BigInt &operator/=(const BigInt &);
0095     BigInt &operator%=(const BigInt &);
0096     word operator%=(word);
0097     BigInt &operator<<=(u32bit);
0098     BigInt &operator>>=(u32bit);
0099 
0100     BigInt &operator++()
0101     {
0102         return (*this += 1);
0103     }
0104     BigInt &operator--()
0105     {
0106         return (*this -= 1);
0107     }
0108     BigInt operator++(int)
0109     {
0110         BigInt x = (*this);
0111         ++(*this);
0112         return x;
0113     }
0114     BigInt operator--(int)
0115     {
0116         BigInt x = (*this);
0117         --(*this);
0118         return x;
0119     }
0120 
0121     BigInt operator-() const;
0122     bool   operator!() const
0123     {
0124         return (!is_nonzero());
0125     }
0126 
0127     s32bit cmp(const BigInt &, bool = true) const;
0128     bool   is_even() const
0129     {
0130         return (get_bit(0) == 0);
0131     }
0132     bool is_odd() const
0133     {
0134         return (get_bit(0) == 1);
0135     }
0136     bool is_nonzero() const
0137     {
0138         return (!is_zero());
0139     }
0140     bool is_zero() const;
0141 
0142     void set_bit(u32bit);
0143     void clear_bit(u32bit);
0144     void mask_bits(u32bit);
0145 
0146     bool   get_bit(u32bit) const;
0147     u32bit get_substring(u32bit, u32bit) const;
0148     byte   byte_at(u32bit) const;
0149     word   word_at(u32bit n) const
0150     {
0151         return ((n < size()) ? reg[n] : 0);
0152     }
0153 
0154     u32bit to_u32bit() const;
0155 
0156     bool is_negative() const
0157     {
0158         return (sign() == Negative);
0159     }
0160     bool is_positive() const
0161     {
0162         return (sign() == Positive);
0163     }
0164     Sign sign() const
0165     {
0166         return (signedness);
0167     }
0168     Sign   reverse_sign() const;
0169     void   flip_sign();
0170     void   set_sign(Sign);
0171     BigInt abs() const;
0172 
0173     u32bit size() const
0174     {
0175         return reg.size();
0176     }
0177     u32bit sig_words() const;
0178     u32bit bytes() const;
0179     u32bit bits() const;
0180 
0181     const word *data() const
0182     {
0183         return reg.begin();
0184     }
0185     SecureVector<word> &get_reg()
0186     {
0187         return reg;
0188     }
0189     void grow_reg(u32bit) const;
0190 
0191     word &operator[](u32bit index)
0192     {
0193         return reg[index];
0194     }
0195     word operator[](u32bit index) const
0196     {
0197         return reg[index];
0198     }
0199     void clear()
0200     {
0201         reg.clear();
0202     }
0203 
0204 #ifndef BOTAN_MINIMAL_BIGINT
0205     void randomize(u32bit = 0);
0206 #endif
0207 
0208     void   binary_encode(byte[]) const;
0209     void   binary_decode(const byte[], u32bit);
0210     u32bit encoded_size(Base = Binary) const;
0211 
0212     static SecureVector<byte> encode(const BigInt &, Base = Binary);
0213     static void               encode(byte[], const BigInt &, Base = Binary);
0214     static BigInt             decode(const byte[], u32bit, Base = Binary);
0215     static BigInt             decode(const MemoryRegion<byte> &, Base = Binary);
0216     static SecureVector<byte> encode_1363(const BigInt &, u32bit);
0217 
0218     void swap(BigInt &);
0219 
0220     BigInt()
0221     {
0222         signedness = Positive;
0223     }
0224     BigInt(u64bit);
0225     BigInt(const BigInt &);
0226     BigInt(const std::string &);
0227     BigInt(const byte[], u32bit, Base = Binary);
0228     BigInt(Sign, u32bit);
0229 #ifndef BOTAN_MINIMAL_BIGINT
0230     BigInt(NumberType, u32bit);
0231 #endif
0232     BigInt &operator=(const BigInt &);
0233 
0234 private:
0235     void               grow_to(u32bit) const;
0236     SecureVector<word> reg;
0237     Sign               signedness;
0238 };
0239 
0240 /*************************************************
0241  * Arithmetic Operators                           *
0242  *************************************************/
0243 BigInt operator+(const BigInt &, const BigInt &);
0244 BigInt operator-(const BigInt &, const BigInt &);
0245 BigInt operator*(const BigInt &, const BigInt &);
0246 BigInt operator/(const BigInt &, const BigInt &);
0247 BigInt operator%(const BigInt &, const BigInt &);
0248 word   operator%(const BigInt &, word);
0249 BigInt operator<<(const BigInt &, u32bit);
0250 BigInt operator>>(const BigInt &, u32bit);
0251 
0252 /*************************************************
0253  * Comparison Operators                           *
0254  *************************************************/
0255 inline bool operator==(const BigInt &a, const BigInt &b)
0256 {
0257     return (a.cmp(b) == 0);
0258 }
0259 inline bool operator!=(const BigInt &a, const BigInt &b)
0260 {
0261     return (a.cmp(b) != 0);
0262 }
0263 inline bool operator<=(const BigInt &a, const BigInt &b)
0264 {
0265     return (a.cmp(b) <= 0);
0266 }
0267 inline bool operator>=(const BigInt &a, const BigInt &b)
0268 {
0269     return (a.cmp(b) >= 0);
0270 }
0271 inline bool operator<(const BigInt &a, const BigInt &b)
0272 {
0273     return (a.cmp(b) < 0);
0274 }
0275 inline bool operator>(const BigInt &a, const BigInt &b)
0276 {
0277     return (a.cmp(b) > 0);
0278 }
0279 
0280 /*************************************************
0281  * I/O Operators                                  *
0282  *************************************************/
0283 #ifndef BOTAN_MINIMAL_BIGINT
0284 std::ostream &operator<<(std::ostream &, const BigInt &);
0285 std::istream &operator>>(std::istream &, BigInt &);
0286 #endif
0287 
0288 }
0289 
0290 #ifndef BOTAN_MINIMAL_BIGINT
0291 } // WRAPNS_LINE
0292 namespace std {
0293 
0294 inline void swap(Botan::BigInt &a, Botan::BigInt &b)
0295 {
0296     a.swap(b);
0297 }
0298 
0299 }
0300 namespace QCA { // WRAPNS_LINE
0301 #endif
0302 
0303 #endif
0304 } // WRAPNS_LINE