MidlePayQml 0.0.4
MiddlePay QML module
arith_uint256.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Bitcoin Core developers
3 
4 
5 // modified version of https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.h
6 
7 #pragma once
8 
9 #include <cstring>
10 #include <limits>
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 
16 public:
17  explicit uint_error(const std::string& str) : std::runtime_error(str) {}
18 };
19 
21 template<unsigned int BITS>
22 class base_uint
23 {
24 protected:
25  static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
26  static constexpr int WIDTH = BITS / 32;
27  uint32_t pn[WIDTH];
28 public:
29 
31  {
32  for (int i = 0; i < WIDTH; i++)
33  pn[i] = 0;
34  }
35 
36  base_uint(const base_uint& b)
37  {
38  for (int i = 0; i < WIDTH; i++)
39  pn[i] = b.pn[i];
40  }
41 
43  {
44  for (int i = 0; i < WIDTH; i++)
45  pn[i] = b.pn[i];
46  return *this;
47  }
48 
49  base_uint(uint64_t b)
50  {
51  pn[0] = (unsigned int)b;
52  pn[1] = (unsigned int)(b >> 32);
53  for (int i = 2; i < WIDTH; i++)
54  pn[i] = 0;
55  }
56 
57 
58 
60  {
61  base_uint ret;
62  for (int i = 0; i < WIDTH; i++)
63  ret.pn[i] = ~pn[i];
64  return ret;
65  }
66 
68  {
69  base_uint ret;
70  for (int i = 0; i < WIDTH; i++)
71  ret.pn[i] = ~pn[i];
72  ++ret;
73  return ret;
74  }
75 
76  double getdouble() const;
77 
78  base_uint& operator=(uint64_t b)
79  {
80  pn[0] = (unsigned int)b;
81  pn[1] = (unsigned int)(b >> 32);
82  for (int i = 2; i < WIDTH; i++)
83  pn[i] = 0;
84  return *this;
85  }
86 
88  {
89  for (int i = 0; i < WIDTH; i++)
90  pn[i] ^= b.pn[i];
91  return *this;
92  }
93 
95  {
96  for (int i = 0; i < WIDTH; i++)
97  pn[i] &= b.pn[i];
98  return *this;
99  }
100 
102  {
103  for (int i = 0; i < WIDTH; i++)
104  pn[i] |= b.pn[i];
105  return *this;
106  }
107 
108  base_uint& operator^=(uint64_t b)
109  {
110  pn[0] ^= (unsigned int)b;
111  pn[1] ^= (unsigned int)(b >> 32);
112  return *this;
113  }
114 
115  base_uint& operator|=(uint64_t b)
116  {
117  pn[0] |= (unsigned int)b;
118  pn[1] |= (unsigned int)(b >> 32);
119  return *this;
120  }
121 
122  base_uint& operator<<=(unsigned int shift);
123  base_uint& operator>>=(unsigned int shift);
124 
126  {
127  uint64_t carry = 0;
128  for (int i = 0; i < WIDTH; i++)
129  {
130  uint64_t n = carry + pn[i] + b.pn[i];
131  pn[i] = n & 0xffffffff;
132  carry = n >> 32;
133  }
134  return *this;
135  }
136 
138  {
139  *this += -b;
140  return *this;
141  }
142 
143  base_uint& operator+=(uint64_t b64)
144  {
145  base_uint b;
146  b = b64;
147  *this += b;
148  return *this;
149  }
150 
151  base_uint& operator-=(uint64_t b64)
152  {
153  base_uint b;
154  b = b64;
155  *this += -b;
156  return *this;
157  }
158 
159  base_uint& operator*=(uint32_t b32);
162 
164  {
165  // prefix operator
166  int i = 0;
167  while (i < WIDTH && ++pn[i] == 0)
168  i++;
169  return *this;
170  }
171 
173  {
174  // postfix operator
175  const base_uint ret = *this;
176  ++(*this);
177  return ret;
178  }
179 
181  {
182  // prefix operator
183  int i = 0;
184  while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
185  i++;
186  return *this;
187  }
188 
190  {
191  // postfix operator
192  const base_uint ret = *this;
193  --(*this);
194  return ret;
195  }
196 
197  int CompareTo(const base_uint& b) const;
198  bool EqualTo(uint64_t b) const;
199 
200  friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
201  friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
202  friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
203  friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
204  friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
205  friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
206  friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
207  friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
208  friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
209  friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
210  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
211  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
212  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
213  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
214  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
215  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
216  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
217  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
218 
219 
220 
221  unsigned int size() const
222  {
223  return sizeof(pn);
224  }
225 
230  unsigned int bits() const;
231 
232  uint64_t GetLow64() const
233  {
234  static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
235  return pn[0] | (uint64_t)pn[1] << 32;
236  }
237 };
238 
Definition: arith_uint256.h:23
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:42
base_uint operator~() const
Definition: arith_uint256.h:59
uint32_t pn[WIDTH]
Definition: arith_uint256.h:27
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:78
int CompareTo(const base_uint &b) const
base_uint & operator+=(uint64_t b64)
Definition: arith_uint256.h:143
base_uint operator--(int)
Definition: arith_uint256.h:189
unsigned int size() const
Definition: arith_uint256.h:221
base_uint(uint64_t b)
Definition: arith_uint256.h:49
base_uint & operator--()
Definition: arith_uint256.h:180
base_uint & operator>>=(unsigned int shift)
friend bool operator!=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:211
base_uint & operator^=(uint64_t b)
Definition: arith_uint256.h:108
base_uint & operator++()
Definition: arith_uint256.h:163
base_uint & operator<<=(unsigned int shift)
friend base_uint operator-(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:201
base_uint(const base_uint &b)
Definition: arith_uint256.h:36
friend base_uint operator*(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:202
static constexpr int WIDTH
Definition: arith_uint256.h:26
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:94
friend base_uint operator<<(const base_uint &a, int shift)
Definition: arith_uint256.h:208
base_uint & operator/=(const base_uint &b)
friend base_uint operator&(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:205
friend bool operator<(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:213
base_uint & operator-=(const base_uint &b)
Definition: arith_uint256.h:137
base_uint & operator+=(const base_uint &b)
Definition: arith_uint256.h:125
base_uint & operator*=(const base_uint &b)
friend bool operator==(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:216
base_uint operator-() const
Definition: arith_uint256.h:67
friend base_uint operator>>(const base_uint &a, int shift)
Definition: arith_uint256.h:207
friend bool operator>=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:214
friend base_uint operator|(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:204
bool EqualTo(uint64_t b) const
friend base_uint operator*(const base_uint &a, uint32_t b)
Definition: arith_uint256.h:209
friend bool operator==(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:210
base_uint()
Definition: arith_uint256.h:30
base_uint & operator|=(const base_uint &b)
Definition: arith_uint256.h:101
base_uint & operator-=(uint64_t b64)
Definition: arith_uint256.h:151
friend bool operator!=(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:217
friend base_uint operator/(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:203
friend base_uint operator^(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:206
friend bool operator>(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:212
friend bool operator<=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:215
base_uint & operator|=(uint64_t b)
Definition: arith_uint256.h:115
double getdouble() const
base_uint operator++(int)
Definition: arith_uint256.h:172
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:87
uint64_t GetLow64() const
Definition: arith_uint256.h:232
base_uint & operator*=(uint32_t b32)
friend base_uint operator+(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:200
unsigned int bits() const
Definition: arith_uint256.h:15
uint_error(const std::string &str)
Definition: arith_uint256.h:17