Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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-2019 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  static constexpr int 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  inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(data)); }
47 
48  friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
49  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
50  friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
51 
52  std::string GetHex() const;
53  void SetHex(const char* psz);
54  void SetHex(const std::string& str);
55  std::string ToString() const;
56 
57  unsigned char* begin()
58  {
59  return &data[0];
60  }
61 
62  unsigned char* end()
63  {
64  return &data[WIDTH];
65  }
66 
67  const unsigned char* begin() const
68  {
69  return &data[0];
70  }
71 
72  const unsigned char* end() const
73  {
74  return &data[WIDTH];
75  }
76 
77  unsigned int size() const
78  {
79  return sizeof(data);
80  }
81 
82  uint64_t GetUint64(int pos) const
83  {
84  const uint8_t* ptr = data + pos * 8;
85  return ((uint64_t)ptr[0]) | \
86  ((uint64_t)ptr[1]) << 8 | \
87  ((uint64_t)ptr[2]) << 16 | \
88  ((uint64_t)ptr[3]) << 24 | \
89  ((uint64_t)ptr[4]) << 32 | \
90  ((uint64_t)ptr[5]) << 40 | \
91  ((uint64_t)ptr[6]) << 48 | \
92  ((uint64_t)ptr[7]) << 56;
93  }
94 
95  template<typename Stream>
96  void Serialize(Stream& s) const
97  {
98  s.write((char*)data, sizeof(data));
99  }
100 
101  template<typename Stream>
102  void Unserialize(Stream& s)
103  {
104  s.read((char*)data, sizeof(data));
105  }
106 };
107 
112 class uint160 : public base_blob<160> {
113 public:
114  uint160() {}
115  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
116 };
117 
123 class uint256 : public base_blob<256> {
124 public:
125  uint256() {}
126  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
127 
133  uint64_t GetCheapHash() const
134  {
135  return ReadLE64(data);
136  }
137 };
138 
139 /* uint256 from const char *.
140  * This is a separate function because the constructor uint256(const char*) can result
141  * in dangerously catching uint256(0).
142  */
143 inline uint256 uint256S(const char *str)
144 {
145  uint256 rv;
146  rv.SetHex(str);
147  return rv;
148 }
149 /* uint256 from std::string.
150  * This is a separate function because the constructor uint256(const std::string &str) can result
151  * in dangerously catching uint256(0) via std::string(const char*).
152  */
153 inline uint256 uint256S(const std::string& str)
154 {
155  uint256 rv;
156  rv.SetHex(str);
157  return rv;
158 }
159 
161 class uint512 : public base_blob<512> {
162 public:
163  uint512() {}
164  uint512(const base_blob<512>& b) : base_blob<512>(b) {}
165  explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
166 
167  uint256 trim256() const
168  {
169  uint256 result;
170  memcpy((void*)&result, (void*)data, 32);
171  return result;
172  }
173 };
174 
175 namespace std {
176  template <>
177  struct hash<uint256>
178  {
180  {
181  return (std::size_t)k.GetCheapHash();
182  }
183  };
184 }
185 
186 #endif // BITCOIN_UINT256_H
uint8_t data[WIDTH]
Definition: uint256.h:24
base_blob()
Definition: uint256.h:26
uint256 trim256() const
Definition: uint256.h:167
void SetNull()
Definition: uint256.h:41
const unsigned char * begin() const
Definition: uint256.h:67
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:48
void Serialize(Stream &s) const
Definition: uint256.h:96
uint512(const base_blob< 512 > &b)
Definition: uint256.h:164
Definition: box.hpp:161
uint160()
Definition: uint256.h:114
unsigned char * begin()
Definition: uint256.h:57
bool IsNull() const
Definition: uint256.h:33
unsigned char * end()
Definition: uint256.h:62
int Compare(const base_blob &other) const
Definition: uint256.h:46
512-bit unsigned big integer.
Definition: uint256.h:161
uint512(const std::vector< unsigned char > &vch)
Definition: uint256.h:165
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:49
const unsigned char * end() const
Definition: uint256.h:72
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:126
void Unserialize(Stream &s)
Definition: uint256.h:102
std::size_t size_t
Definition: bits.hpp:21
uint256 uint256S(const char *str)
Definition: uint256.h:143
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:50
std::string ToString() const
Definition: uint256.cpp:62
unsigned int size() const
Definition: uint256.h:77
uint256()
Definition: uint256.h:125
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:20
256-bit opaque blob.
Definition: uint256.h:123
std::size_t operator()(const uint256 &k) const
Definition: uint256.h:179
void * memcpy(void *a, const void *b, size_t c)
static constexpr int WIDTH
Definition: uint256.h:23
std::string GetHex() const
Definition: uint256.cpp:21
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
160-bit opaque blob.
Definition: uint256.h:112
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:115
uint64_t GetCheapHash() const
A cheap hash function that just returns 64 bits from the result, it can be used when the contents are...
Definition: uint256.h:133
uint64_t GetUint64(int pos) const
Definition: uint256.h:82
void SetHex(const char *psz)
Definition: uint256.cpp:27
uint512()
Definition: uint256.h:163
Released under the MIT license