| 1 | /* |
| 2 | * Class for simple, arbitrary size unsigned integer math. |
| 3 | * Note that some method implementations might lack features that weren't required so far! |
| 4 | * |
| 5 | * Copyright (C) 2008 - Olaf Freyer |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Affero General Public License |
| 9 | * as published by the Free Software Foundation; either version 3 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #ifndef INCLUDED_BIGINT_H |
| 19 | #define INCLUDED_BIGINT_H |
| 20 | |
| 21 | #include <cstdint> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace pvpgn |
| 25 | { |
| 26 | |
| 27 | class BigInt |
| 28 | { |
| 29 | public: |
| 30 | BigInt(); |
| 31 | explicit BigInt(std::uint8_t input); |
| 32 | explicit BigInt(std::uint16_t input); |
| 33 | explicit BigInt(std::uint32_t input); |
| 34 | BigInt(const BigInt& input); |
| 35 | BigInt& operator=(const BigInt& input); |
| 36 | BigInt(unsigned char const* input, int input_size, int blockSize = 1, bool bigEndian = true); |
| 37 | ~BigInt() throw (); |
| 38 | bool operator== (const BigInt& right) const; |
| 39 | bool operator< (const BigInt& right) const; |
| 40 | bool operator> (const BigInt& right) const; |
| 41 | BigInt operator+ (const BigInt& right) const; |
| 42 | BigInt operator- (const BigInt& right) const; |
| 43 | BigInt operator* (const BigInt& right) const; |
| 44 | BigInt operator/ (const BigInt& right) const; |
| 45 | BigInt operator% (const BigInt& right) const; |
| 46 | BigInt operator<< (int bytesToShift) const; |
| 47 | static BigInt random(int size); |
| 48 | static void status(); |
| 49 | BigInt powm(const BigInt& exp, const BigInt& mod) const; |
| 50 | unsigned char* getData(int byteCount, int blockSize = 1, bool bigEndian = true) const; |
| 51 | void getData(unsigned char* out, int byteCount, int blockSize = 1, bool bigEndian = true) const; |
| 52 | std::string toHexString() const; |
| 53 | |
| 54 | private: |
| 55 | |
| 56 | explicit BigInt(std::uint64_t input); |
| 57 | using bigint_base = std::uint32_t; |
| 58 | using bigint_extended = std::uint64_t; |
| 59 | #define bigint_base_mask 0xffffffff |
| 60 | #define bigint_extended_carry bigint_base_mask+0x01 |
| 61 | #define bigint_base_bitcount (sizeof(bigint_base)*8) |
| 62 | |
| 63 | bigint_base *segment; |
| 64 | int segment_count; |
| 65 | }; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | #endif /* __BIGINT_INCLUDED__ */ |