ledger-core
BigInt.h
1 /*
2  *
3  * BigInt
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 15/09/2016.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_BIGINT_H
32 #define LEDGER_CORE_BIGINT_H
33 
34 #ifndef LIBCORE_EXPORT
35  #if defined(_MSC_VER)
36  #include <libcore_export.h>
37  #else
38  #define LIBCORE_EXPORT
39  #endif
40 #endif
41 
42 #include <string>
43 #include <vector>
44 #include <bigd.h>
45 #include <memory>
46 #include "../utils/endian.h"
47 
48 namespace ledger {
49 
50  namespace core {
51 
56  class BigInt {
57 
58  public:
59  static LIBCORE_EXPORT const BigInt ZERO;
60  static LIBCORE_EXPORT const BigInt ONE;
61  static LIBCORE_EXPORT const BigInt TEN;
62  typedef unsigned int SimpleInt;
63  typedef unsigned long DoubleInt;
67  static LIBCORE_EXPORT const std::string DIGITS;
71  static LIBCORE_EXPORT const int MIN_RADIX;
75  static LIBCORE_EXPORT const int MAX_RADIX;
76 
82  static LIBCORE_EXPORT BigInt* from_hex(const std::string& str);
87  static LIBCORE_EXPORT BigInt fromHex(const std::string& str);
93  static LIBCORE_EXPORT BigInt* from_dec(const std::string& str);
99  static LIBCORE_EXPORT BigInt fromDecimal(const std::string& str);
100 
101  static LIBCORE_EXPORT BigInt fromString(const std::string& str);
102 
103  template <typename T>
104  static BigInt fromScalar(T value) {
105  BigInt result;
106  result.assignScalar<T>(value);
107  return result;
108  }
109 
110  private:
111  BigInt(const std::string& str, int radix);
112 
113 
115  static bool all_digits(std::string const& s);
116 
117  public:
118  BigInt();
119  BigInt(const BigInt& cpy);
120  BigInt(BigInt&& mov);
121 
128  BigInt(const void *data, size_t length, bool negative);
129  BigInt(const std::vector<uint8_t>& data, bool negative);
130 
137  explicit BigInt(int value);
138  explicit BigInt(unsigned int value);
139  explicit BigInt(unsigned long long value);
140  explicit BigInt(int64_t value);
146  BigInt(const std::string& str);
147 
152  int toInt() const;
153 
158  unsigned toUnsignedInt() const;
159 
160  uint64_t toUint64() const;
161  int64_t toInt64() const;
162 
169  std::string toString() const;
170  std::string to_string() const {return toString();};
171 
176  std::string toHexString() const;
177 
182  std::vector<uint8_t> toByteArray() const;
183 
184  BigInt operator+(const BigInt& rhs) const;
185  BigInt operator-(const BigInt& rhs) const;
186  BigInt operator*(const BigInt& rhs) const;
187  BigInt operator/(const BigInt& rhs) const;
188  BigInt operator%(const BigInt& rhs) const;
189 
190  BigInt& operator++();
191  BigInt operator++(int);
192  BigInt& operator--();
193  BigInt operator--(int);
194 
195  BigInt& operator=(const BigInt&);
196  BigInt& operator=(BigInt&& a);
197 
198  bool operator<(const BigInt&) const;
199  bool operator<=(const BigInt&) const;
200  bool operator==(const BigInt&) const;
201  bool operator>(const BigInt&) const;
202  bool operator>=(const BigInt&) const;
203 
204  int compare(const BigInt&) const;
205 
206  BigInt pow(unsigned short p) const;
207 
208  unsigned long getBitSize() const;
209  bool isNegative() const;
210  bool isPositive() const;
211  bool isZero() const;
212  BigInt negative() const;
213  BigInt positive() const;
214 
215  BigInt& assignI64(int64_t value);
216 
217  template <typename T>
218  BigInt& assignScalar(T value) {
219  auto bytes = endianness::scalar_type_to_array<T>(std::abs(value), endianness::Endianness::BIG);
220  bdConvFromOctets(_bigd, reinterpret_cast<const unsigned char *>(bytes), sizeof(value));
221  std::free(bytes);
222  _negative = value < 0LL;
223  return *this;
224  }
225 
226  virtual ~BigInt();
227 
228  private:
229  BIGD _bigd;
230  bool _negative;
231  };
232  }
233 
234 }
235 #endif //LEDGER_CORE_BIGINT_H
std::string toHexString() const
Definition: BigInt.cpp:151
unsigned toUnsignedInt() const
Definition: BigInt.cpp:136
static LIBCORE_EXPORT const std::string DIGITS
Definition: BigInt.h:67
std::string toString() const
Definition: BigInt.cpp:140
int toInt() const
Definition: BigInt.cpp:132
static LIBCORE_EXPORT const int MIN_RADIX
Definition: BigInt.h:71
std::vector< uint8_t > toByteArray() const
Definition: BigInt.cpp:342
static LIBCORE_EXPORT BigInt fromDecimal(const std::string &str)
Definition: BigInt.cpp:382
Definition: Account.cpp:8
static LIBCORE_EXPORT BigInt * from_hex(const std::string &str)
Definition: BigInt.cpp:166
static LIBCORE_EXPORT const int MAX_RADIX
Definition: BigInt.h:75
Definition: BigInt.h:56
static LIBCORE_EXPORT BigInt fromHex(const std::string &str)
Definition: BigInt.cpp:378
static LIBCORE_EXPORT BigInt * from_dec(const std::string &str)
Definition: BigInt.cpp:170