Dogecoin Core  1.14.2
P2P Digital Currency
uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core 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_UINT256_H
7 #define BITCOIN_UINT256_H
8 
9 #include <assert.h>
10 #include <cstring>
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
15 #include "crypto/common.h"
16 
18 template<unsigned int BITS>
19 class base_blob
20 {
21 protected:
22  enum { WIDTH=BITS/8 };
23  uint8_t data[WIDTH];
24 public:
26  {
27  memset(data, 0, sizeof(data));
28  }
29 
30  explicit base_blob(const std::vector<unsigned char>& vch);
31 
32  bool IsNull() const
33  {
34  for (int i = 0; i < WIDTH; i++)
35  if (data[i] != 0)
36  return false;
37  return true;
38  }
39 
40  void SetNull()
41  {
42  memset(data, 0, sizeof(data));
43  }
44 
45  inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(data)); }
46 
47  friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
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 
51  std::string GetHex() const;
52  void SetHex(const char* psz);
53  void SetHex(const std::string& str);
54  std::string ToString() const;
55 
56  unsigned char* begin()
57  {
58  return &data[0];
59  }
60 
61  unsigned char* end()
62  {
63  return &data[WIDTH];
64  }
65 
66  const unsigned char* begin() const
67  {
68  return &data[0];
69  }
70 
71  const unsigned char* end() const
72  {
73  return &data[WIDTH];
74  }
75 
76  unsigned int size() const
77  {
78  return sizeof(data);
79  }
80 
81  uint64_t GetUint64(int pos) const
82  {
83  const uint8_t* ptr = data + pos * 8;
84  return ((uint64_t)ptr[0]) | \
85  ((uint64_t)ptr[1]) << 8 | \
86  ((uint64_t)ptr[2]) << 16 | \
87  ((uint64_t)ptr[3]) << 24 | \
88  ((uint64_t)ptr[4]) << 32 | \
89  ((uint64_t)ptr[5]) << 40 | \
90  ((uint64_t)ptr[6]) << 48 | \
91  ((uint64_t)ptr[7]) << 56;
92  }
93 
94  template<typename Stream>
95  void Serialize(Stream& s) const
96  {
97  s.write((char*)data, sizeof(data));
98  }
99 
100  template<typename Stream>
101  void Unserialize(Stream& s)
102  {
103  s.read((char*)data, sizeof(data));
104  }
105 };
106 
111 class uint160 : public base_blob<160> {
112 public:
113  uint160() {}
114  uint160(const base_blob<160>& b) : base_blob<160>(b) {}
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  uint256(const base_blob<256>& b) : base_blob<256>(b) {}
127  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
128 
134  uint64_t GetCheapHash() const
135  {
136  return ReadLE64(data);
137  }
138 };
139 
140 /* uint256 from const char *.
141  * This is a separate function because the constructor uint256(const char*) can result
142  * in dangerously catching uint256(0).
143  */
144 inline uint256 uint256S(const char *str)
145 {
146  uint256 rv;
147  rv.SetHex(str);
148  return rv;
149 }
150 /* uint256 from std::string.
151  * This is a separate function because the constructor uint256(const std::string &str) can result
152  * in dangerously catching uint256(0) via std::string(const char*).
153  */
154 inline uint256 uint256S(const std::string& str)
155 {
156  uint256 rv;
157  rv.SetHex(str);
158  return rv;
159 }
160 
161 #endif // BITCOIN_UINT256_H
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:20
unsigned int size() const
Definition: uint256.h:76
@ WIDTH
Definition: uint256.h:22
void SetHex(const char *psz)
Definition: uint256.cpp:30
void Unserialize(Stream &s)
Definition: uint256.h:101
int Compare(const base_blob &other) const
Definition: uint256.h:45
uint8_t data[WIDTH]
Definition: uint256.h:23
const unsigned char * begin() const
Definition: uint256.h:66
std::string ToString() const
Definition: uint256.cpp:65
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:48
void SetNull()
Definition: uint256.h:40
unsigned char * end()
Definition: uint256.h:61
bool IsNull() const
Definition: uint256.h:32
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:47
void Serialize(Stream &s) const
Definition: uint256.h:95
std::string GetHex() const
Definition: uint256.cpp:21
base_blob()
Definition: uint256.h:25
const unsigned char * end() const
Definition: uint256.h:71
unsigned char * begin()
Definition: uint256.h:56
uint64_t GetUint64(int pos) const
Definition: uint256.h:81
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:49
160-bit opaque blob.
Definition: uint256.h:111
uint160()
Definition: uint256.h:113
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:115
uint160(const base_blob< 160 > &b)
Definition: uint256.h:114
256-bit opaque blob.
Definition: uint256.h:123
uint256(const base_blob< 256 > &b)
Definition: uint256.h:126
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:127
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:134
uint256()
Definition: uint256.h:125
uint256 uint256S(const char *str)
Definition: uint256.h:144