PRCYCoin  2.0.0.7rc1
P2P Digital Currency
arith_uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_ARITH_UINT256_H
7 #define BITCOIN_ARITH_UINT256_H
8 
9 #include "blob_uint256.h"
10 #include "uint512.h"
11 #include <assert.h>
12 #include <cstring>
13 #include <stdexcept>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 
18 class blob_uint512;
19 class blob_uint256;
20 class uint256;
21 class uint512;
22 
23 class uint_error : public std::runtime_error {
24 public:
25  explicit uint_error(const std::string& str) : std::runtime_error(str) {}
26 };
27 
29 template<unsigned int BITS>
30 class base_uint
31 {
32 public:
33  enum { WIDTH=BITS/32 };
34  uint32_t pn[WIDTH];
35 
37  {
38  for (int i = 0; i < WIDTH; i++)
39  pn[i] = 0;
40  }
41 
43  {
44  for (int i = 0; i < WIDTH; i++)
45  pn[i] = b.pn[i];
46  }
47 
49  {
50  for (int i = 0; i < WIDTH; i++)
51  pn[i] = b.pn[i];
52  return *this;
53  }
54 
55  base_uint(uint64_t b)
56  {
57  pn[0] = (unsigned int)b;
58  pn[1] = (unsigned int)(b >> 32);
59  for (int i = 2; i < WIDTH; i++)
60  pn[i] = 0;
61  }
62 
63  explicit base_uint(const std::string& str);
64  explicit base_uint(const std::vector<unsigned char>& vch);
65 
66  bool operator!() const
67  {
68  for (int i = 0; i < WIDTH; i++)
69  if (pn[i] != 0)
70  return false;
71  return true;
72  }
73 
74  const base_uint operator~() const
75  {
76  base_uint ret;
77  for (int i = 0; i < WIDTH; i++)
78  ret.pn[i] = ~pn[i];
79  return ret;
80  }
81 
82  const base_uint operator-() const
83  {
84  base_uint ret;
85  for (int i = 0; i < WIDTH; i++)
86  ret.pn[i] = ~pn[i];
87  ret++;
88  return ret;
89  }
90 
91  double getdouble() const;
92 
93  base_uint& operator=(uint64_t b)
94  {
95  pn[0] = (unsigned int)b;
96  pn[1] = (unsigned int)(b >> 32);
97  for (int i = 2; i < WIDTH; i++)
98  pn[i] = 0;
99  return *this;
100  }
101 
103  {
104  for (int i = 0; i < WIDTH; i++)
105  pn[i] ^= b.pn[i];
106  return *this;
107  }
108 
110  {
111  for (int i = 0; i < WIDTH; i++)
112  pn[i] &= b.pn[i];
113  return *this;
114  }
115 
117  {
118  for (int i = 0; i < WIDTH; i++)
119  pn[i] |= b.pn[i];
120  return *this;
121  }
122 
123  base_uint& operator^=(uint64_t b)
124  {
125  pn[0] ^= (unsigned int)b;
126  pn[1] ^= (unsigned int)(b >> 32);
127  return *this;
128  }
129 
130  base_uint& operator|=(uint64_t b)
131  {
132  pn[0] |= (unsigned int)b;
133  pn[1] |= (unsigned int)(b >> 32);
134  return *this;
135  }
136 
137  base_uint& operator<<=(unsigned int shift);
138  base_uint& operator>>=(unsigned int shift);
139 
141  {
142  uint64_t carry = 0;
143  for (int i = 0; i < WIDTH; i++)
144  {
145  uint64_t n = carry + pn[i] + b.pn[i];
146  pn[i] = n & 0xffffffff;
147  carry = n >> 32;
148  }
149  return *this;
150  }
151 
153  {
154  *this += -b;
155  return *this;
156  }
157 
158  base_uint& operator+=(uint64_t b64)
159  {
160  base_uint b;
161  b = b64;
162  *this += b;
163  return *this;
164  }
165 
166  base_uint& operator-=(uint64_t b64)
167  {
168  base_uint b;
169  b = b64;
170  *this += -b;
171  return *this;
172  }
173 
174  base_uint& operator*=(uint32_t b32);
175  base_uint& operator*=(const base_uint& b);
176  base_uint& operator/=(const base_uint& b);
177 
179  {
180  // prefix operator
181  int i = 0;
182  while (++pn[i] == 0 && i < WIDTH-1)
183  i++;
184  return *this;
185  }
186 
188  {
189  // postfix operator
190  const base_uint ret = *this;
191  ++(*this);
192  return ret;
193  }
194 
196  {
197  // prefix operator
198  int i = 0;
199  while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
200  i++;
201  return *this;
202  }
203 
205  {
206  // postfix operator
207  const base_uint ret = *this;
208  --(*this);
209  return ret;
210  }
211 
212  int CompareTo(const base_uint& b) const;
213  bool EqualTo(uint64_t b) const;
214 
215  friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
216  friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
217  friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
218  friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
219  friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
220  friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
221  friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
222  friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
223  friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
224  friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
225  friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
226  friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
227  friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
228  friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
229  friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
230  friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
231  friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
232  friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
233 
234  std::string GetHex() const;
235  void SetHex(const char* psz);
236  void SetHex(const std::string& str);
237  std::string ToString() const;
238  std::string ToStringReverseEndian() const;
239 
240  unsigned char* begin()
241  {
242  return (unsigned char*)&pn[0];
243  }
244 
245  unsigned char* end()
246  {
247  return (unsigned char*)&pn[WIDTH];
248  }
249 
250  const unsigned char* begin() const
251  {
252  return (unsigned char*)&pn[0];
253  }
254 
255  const unsigned char* end() const
256  {
257  return (unsigned char*)&pn[WIDTH];
258  }
259 
260  unsigned int size() const
261  {
262  return sizeof(pn);
263  }
264 
265  uint64_t Get64(int n = 0) const
266  {
267  return pn[2 * n] | (uint64_t)pn[2 * n + 1] << 32;
268  }
269 
270  uint32_t Get32(int n = 0) const
271  {
272  return pn[2 * n];
273  }
278  unsigned int bits() const;
279 
280  uint64_t GetLow64() const
281  {
282  assert(WIDTH >= 2);
283  return pn[0] | (uint64_t)pn[1] << 32;
284  }
285 
286  unsigned int GetSerializeSize(int nType, int nVersion) const
287  {
288  return sizeof(pn);
289  }
290 
291  template<typename Stream>
292  void Serialize(Stream& s, int nType, int nVersion) const
293  {
294  s.write((char*)pn, sizeof(pn));
295  }
296 
297  template<typename Stream>
298  void Unserialize(Stream& s, int nType, int nVersion)
299  {
300  s.read((char*)pn, sizeof(pn));
301  }
302 
303  // Temporary for migration to blob160/256
304  uint64_t GetCheapHash() const
305  {
306  return GetLow64();
307  }
308  void SetNull()
309  {
310  memset(pn, 0, sizeof(pn));
311  }
312  bool IsNull() const
313  {
314  for (int i = 0; i < WIDTH; i++)
315  if (pn[i] != 0)
316  return false;
317  return true;
318  }
319 
320  friend class uint160;
321  friend class uint256;
322  friend class uint512;
323 
324  friend class arith_uint160;
325  friend class arith_uint256;
326  friend class arith_uint512;
327 };
328 
330 class arith_uint160 : public base_uint<160> {
331 public:
334  arith_uint160(uint64_t b) : base_uint<160>(b) {}
335  explicit arith_uint160(const std::string& str) : base_uint<160>(str) {}
336  explicit arith_uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
337 };
338 
340 class arith_uint256 : public base_uint<256> {
341 public:
344  arith_uint256(uint64_t b) : base_uint<256>(b) {}
345  explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
346  explicit arith_uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
347 
368  arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
369  uint32_t GetCompact(bool fNegative = false) const;
370 
371  uint64_t GetHash(const arith_uint256& salt) const;
372 
373  uint32_t Get32(int n = 0) const { return pn[2 * n]; }
374 };
375 
377 class arith_uint512 : public base_uint<512> {
378 public:
381  arith_uint512(uint64_t b) : base_uint<512>(b) {}
382  explicit arith_uint512(const std::string& str) : base_uint<512>(str) {}
383  explicit arith_uint512(const std::vector<unsigned char>& vch) : base_uint<512>(vch) {}
384 
385  uint64_t GetHash(const arith_uint256& salt) const;
386 
387  //friend arith_uint512 UintToArith512(const blob_uint512 &a);
388  //friend blob_uint512 ArithToUint512(const arith_uint512 &a);
389 
390 };
391 
397 
398 #endif // BITCOIN_UINT256_H
base_uint::operator-
const base_uint operator-() const
Definition: arith_uint256.h:82
base_uint::operator++
const base_uint operator++(int)
Definition: arith_uint256.h:187
base_uint::operator==
friend bool operator==(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:231
base_uint::end
unsigned char * end()
Definition: arith_uint256.h:245
arith_uint256::SetCompact
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=NULL, bool *pfOverflow=NULL)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
Definition: arith_uint256.cpp:273
base_uint::operator++
base_uint & operator++()
Definition: arith_uint256.h:178
base_uint::operator|
const friend base_uint operator|(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:219
uint512.h
base_uint::begin
const unsigned char * begin() const
Definition: arith_uint256.h:250
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
base_uint::operator/
const friend base_uint operator/(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:218
arith_uint512::arith_uint512
arith_uint512(const std::string &str)
Definition: arith_uint256.h:382
base_uint::GetHex
std::string GetHex() const
Definition: arith_uint256.cpp:155
base_uint::begin
unsigned char * begin()
Definition: arith_uint256.h:240
base_uint::operator>>
const friend base_uint operator>>(const base_uint &a, int shift)
Definition: arith_uint256.h:222
base_uint::operator<<=
base_uint & operator<<=(unsigned int shift)
Definition: arith_uint256.cpp:29
arith_uint512
512-bit unsigned big integer.
Definition: arith_uint256.h:377
arith_uint256::arith_uint256
arith_uint256(const std::vector< unsigned char > &vch)
Definition: arith_uint256.h:346
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:340
blob_uint256.h
base_uint::size
unsigned int size() const
Definition: arith_uint256.h:260
base_uint::operator-=
base_uint & operator-=(const base_uint &b)
Definition: arith_uint256.h:152
base_uint::bits
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
Definition: arith_uint256.cpp:214
arith_uint256::arith_uint256
arith_uint256(const std::string &str)
Definition: arith_uint256.h:345
arith_uint512::arith_uint512
arith_uint512(uint64_t b)
Definition: arith_uint256.h:381
base_uint::operator<=
friend bool operator<=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:230
base_uint::end
const unsigned char * end() const
Definition: arith_uint256.h:255
base_uint::operator-
const friend base_uint operator-(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:216
arith_uint256::GetHash
uint64_t GetHash(const arith_uint256 &salt) const
Definition: arith_uint256.cpp:358
base_uint::operator*
const friend base_uint operator*(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:217
arith_uint160::arith_uint160
arith_uint160(const std::vector< unsigned char > &vch)
Definition: arith_uint256.h:336
base_uint::base_uint
base_uint(uint64_t b)
Definition: arith_uint256.h:55
base_uint::Get64
uint64_t Get64(int n=0) const
Definition: arith_uint256.h:265
base_uint::GetSerializeSize
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: arith_uint256.h:286
arith_uint160::arith_uint160
arith_uint160()
Definition: arith_uint256.h:332
base_uint::GetCheapHash
uint64_t GetCheapHash() const
Definition: arith_uint256.h:304
base_uint::operator!=
friend bool operator!=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:226
ARITH_UINT256_ZERO
const arith_uint256 ARITH_UINT256_ZERO
Old classes definitions.
Definition: arith_uint256.h:396
base_uint::operator+=
base_uint & operator+=(const base_uint &b)
Definition: arith_uint256.h:140
base_uint::operator^
const friend base_uint operator^(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:221
base_uint::operator>=
friend bool operator>=(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:229
base_uint::operator&=
base_uint & operator&=(const base_uint &b)
Definition: arith_uint256.h:109
base_uint::operator*=
base_uint & operator*=(uint32_t b32)
Definition: arith_uint256.cpp:63
base_uint::operator-=
base_uint & operator-=(uint64_t b64)
Definition: arith_uint256.h:166
base_uint::operator!=
friend bool operator!=(const base_uint &a, uint64_t b)
Definition: arith_uint256.h:232
base_uint::EqualTo
bool EqualTo(uint64_t b) const
Definition: arith_uint256.cpp:129
arith_uint160
160-bit unsigned big integer.
Definition: arith_uint256.h:330
base_uint::base_uint
base_uint(const base_uint &b)
Definition: arith_uint256.h:42
base_uint::Get32
uint32_t Get32(int n=0) const
Definition: arith_uint256.h:270
base_uint::GetLow64
uint64_t GetLow64() const
Definition: arith_uint256.h:280
arith_uint256::arith_uint256
arith_uint256()
Definition: arith_uint256.h:342
base_uint::operator<
friend bool operator<(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:228
arith_uint256::GetCompact
uint32_t GetCompact(bool fNegative=false) const
Definition: arith_uint256.cpp:293
base_uint::operator+
const friend base_uint operator+(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:215
base_uint::operator^=
base_uint & operator^=(uint64_t b)
Definition: arith_uint256.h:123
base_uint::operator|=
base_uint & operator|=(const base_uint &b)
Definition: arith_uint256.h:116
blob_uint512
512-bit unsigned big integer.
Definition: uint512.h:12
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
base_uint::SetHex
void SetHex(const char *psz)
Definition: arith_uint256.cpp:164
base_uint::IsNull
bool IsNull() const
Definition: arith_uint256.h:312
base_uint::operator--
const base_uint operator--(int)
Definition: arith_uint256.h:204
arith_uint256::Get32
uint32_t Get32(int n=0) const
Definition: arith_uint256.h:373
arith_uint512::arith_uint512
arith_uint512(const base_uint< 512 > &b)
Definition: arith_uint256.h:380
base_uint::operator~
const base_uint operator~() const
Definition: arith_uint256.h:74
uint_error::uint_error
uint_error(const std::string &str)
Definition: arith_uint256.h:25
base_uint::operator&
const friend base_uint operator&(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:220
base_uint::SetNull
void SetNull()
Definition: arith_uint256.h:308
base_uint::operator=
base_uint & operator=(const base_uint &b)
Definition: arith_uint256.h:48
base_uint
Template base class for unsigned big integers.
Definition: arith_uint256.h:30
base_uint::operator==
friend bool operator==(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:225
arith_uint256::arith_uint256
arith_uint256(const base_uint< 256 > &b)
Definition: arith_uint256.h:343
arith_uint512::arith_uint512
arith_uint512(const std::vector< unsigned char > &vch)
Definition: arith_uint256.h:383
uint160
160-bit unsigned big integer.
Definition: uint256.h:27
base_uint::WIDTH
@ WIDTH
Definition: arith_uint256.h:33
blob_uint256
256-bit opaque blob.
Definition: blob_uint256.h:115
base_uint::operator=
base_uint & operator=(uint64_t b)
Definition: arith_uint256.h:93
std
Definition: adjacency_graphs.hpp:25
arith_uint512::arith_uint512
arith_uint512()
Definition: arith_uint256.h:379
base_uint::operator>
friend bool operator>(const base_uint &a, const base_uint &b)
Definition: arith_uint256.h:227
base_uint::operator<<
const friend base_uint operator<<(const base_uint &a, int shift)
Definition: arith_uint256.h:223
base_uint::operator>>=
base_uint & operator>>=(unsigned int shift)
Definition: arith_uint256.cpp:46
base_uint::operator*
const friend base_uint operator*(const base_uint &a, uint32_t b)
Definition: arith_uint256.h:224
arith_uint160::arith_uint160
arith_uint160(const base_uint< 160 > &b)
Definition: arith_uint256.h:333
arith_uint256::arith_uint256
arith_uint256(uint64_t b)
Definition: arith_uint256.h:344
arith_uint160::arith_uint160
arith_uint160(const std::string &str)
Definition: arith_uint256.h:335
base_uint::operator--
base_uint & operator--()
Definition: arith_uint256.h:195
base_uint::operator!
bool operator!() const
Definition: arith_uint256.h:66
base_uint::ToStringReverseEndian
std::string ToStringReverseEndian() const
Definition: arith_uint256.cpp:205
base_uint::CompareTo
int CompareTo(const base_uint &b) const
Definition: arith_uint256.cpp:117
arith_uint160::arith_uint160
arith_uint160(uint64_t b)
Definition: arith_uint256.h:334
uint_error
Definition: arith_uint256.h:23
uint512
512-bit unsigned big integer.
Definition: uint256.h:73
base_uint::base_uint
base_uint()
Definition: arith_uint256.h:36
base_uint::operator^=
base_uint & operator^=(const base_uint &b)
Definition: arith_uint256.h:102
base_uint::getdouble
double getdouble() const
Definition: arith_uint256.cpp:143
base_uint::operator+=
base_uint & operator+=(uint64_t b64)
Definition: arith_uint256.h:158
base_uint::Serialize
void Serialize(Stream &s, int nType, int nVersion) const
Definition: arith_uint256.h:292
base_uint::operator/=
base_uint & operator/=(const base_uint &b)
Definition: arith_uint256.cpp:91
base_uint::operator|=
base_uint & operator|=(uint64_t b)
Definition: arith_uint256.h:130
base_uint::Unserialize
void Unserialize(Stream &s, int nType, int nVersion)
Definition: arith_uint256.h:298
base_uint::pn
uint32_t pn[WIDTH]
Definition: arith_uint256.h:34
arith_uint512::GetHash
uint64_t GetHash(const arith_uint256 &salt) const
base_uint::ToString
std::string ToString() const
Definition: arith_uint256.cpp:199