Dash Core  0.12.2.1
P2P Digital Currency
uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2017 The Dash Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_UINT256_H
8 #define BITCOIN_UINT256_H
9 
10 #include <assert.h>
11 #include <cstring>
12 #include <stdexcept>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 #include "crypto/common.h"
17 
19 template<unsigned int BITS>
20 class base_blob
21 {
22 protected:
23  enum { WIDTH=BITS/8 };
24  uint8_t data[WIDTH];
25 public:
27  {
28  memset(data, 0, sizeof(data));
29  }
30 
31  explicit base_blob(const std::vector<unsigned char>& vch);
32 
33  bool IsNull() const
34  {
35  for (int i = 0; i < WIDTH; i++)
36  if (data[i] != 0)
37  return false;
38  return true;
39  }
40 
41  void SetNull()
42  {
43  memset(data, 0, sizeof(data));
44  }
45 
46  friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
47  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
48  friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
49 
50  std::string GetHex() const;
51  void SetHex(const char* psz);
52  void SetHex(const std::string& str);
53  std::string ToString() const;
54 
55  unsigned char* begin()
56  {
57  return &data[0];
58  }
59 
60  unsigned char* end()
61  {
62  return &data[WIDTH];
63  }
64 
65  const unsigned char* begin() const
66  {
67  return &data[0];
68  }
69 
70  const unsigned char* end() const
71  {
72  return &data[WIDTH];
73  }
74 
75  unsigned int size() const
76  {
77  return sizeof(data);
78  }
79 
80  unsigned int GetSerializeSize(int nType, int nVersion) const
81  {
82  return sizeof(data);
83  }
84 
85  template<typename Stream>
86  void Serialize(Stream& s, int nType, int nVersion) const
87  {
88  s.write((char*)data, sizeof(data));
89  }
90 
91  template<typename Stream>
92  void Unserialize(Stream& s, int nType, int nVersion)
93  {
94  s.read((char*)data, sizeof(data));
95  }
96 };
97 
102 class uint160 : public base_blob<160> {
103 public:
104  uint160() {}
105  uint160(const base_blob<160>& b) : base_blob<160>(b) {}
106  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
107 };
108 
114 class uint256 : public base_blob<256> {
115 public:
116  uint256() {}
117  uint256(const base_blob<256>& b) : base_blob<256>(b) {}
118  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
119 
125  uint64_t GetCheapHash() const
126  {
127  return ReadLE64(data);
128  }
129 
133  uint64_t GetHash(const uint256& salt) const;
134 };
135 
136 /* uint256 from const char *.
137  * This is a separate function because the constructor uint256(const char*) can result
138  * in dangerously catching uint256(0).
139  */
140 inline uint256 uint256S(const char *str)
141 {
142  uint256 rv;
143  rv.SetHex(str);
144  return rv;
145 }
146 /* uint256 from std::string.
147  * This is a separate function because the constructor uint256(const std::string &str) can result
148  * in dangerously catching uint256(0) via std::string(const char*).
149  */
150 inline uint256 uint256S(const std::string& str)
151 {
152  uint256 rv;
153  rv.SetHex(str);
154  return rv;
155 }
156 
158 class uint512 : public base_blob<512> {
159 public:
160  uint512() {}
161  uint512(const base_blob<512>& b) : base_blob<512>(b) {}
162  explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
163 
164  uint256 trim256() const
165  {
166  uint256 result;
167  memcpy((void*)&result, (void*)data, 32);
168  return result;
169  }
170 };
171 
172 
173 #endif // BITCOIN_UINT256_H
string salt
Definition: rpcuser.py:25
uint8_t data[WIDTH]
Definition: uint256.h:24
base_blob()
Definition: uint256.h:26
uint256 trim256() const
Definition: uint256.h:164
uint256(const base_blob< 256 > &b)
Definition: uint256.h:117
void SetNull()
Definition: uint256.h:41
void Serialize(Stream &s, int nType, int nVersion) const
Definition: uint256.h:86
const unsigned char * begin() const
Definition: uint256.h:65
uint64_t GetHash(const uint256 &salt) const
Definition: uint256.cpp:126
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:46
uint512(const base_blob< 512 > &b)
Definition: uint256.h:161
uint160()
Definition: uint256.h:104
unsigned char * begin()
Definition: uint256.h:55
bool IsNull() const
Definition: uint256.h:33
unsigned char * end()
Definition: uint256.h:60
uint512(const std::vector< unsigned char > &vch)
Definition: uint256.h:162
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:47
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: uint256.h:80
const unsigned char * end() const
Definition: uint256.h:70
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:118
uint256 uint256S(const char *str)
Definition: uint256.h:140
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:48
std::string ToString() const
Definition: uint256.cpp:65
unsigned int size() const
Definition: uint256.h:75
uint256()
Definition: uint256.h:116
uint160(const base_blob< 160 > &b)
Definition: uint256.h:105
void * memcpy(void *a, const void *b, size_t c)
std::string GetHex() const
Definition: uint256.cpp:21
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:26
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:106
void Unserialize(Stream &s, int nType, int nVersion)
Definition: uint256.h:92
uint64_t GetCheapHash() const
Definition: uint256.h:125
void SetHex(const char *psz)
Definition: uint256.cpp:30
uint512()
Definition: uint256.h:160
result
Definition: rpcuser.py:37