Fabcoin Core  0.16.2
P2P Digital Currency
uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Fabcoin 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 FABCOIN_UINT256_H
7 #define FABCOIN_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 #include <libdevcore/Common.h>
19 #include <libdevcore/CommonData.h>
20 #include <libdevcore/FixedHash.h>
22 
24 template<unsigned int BITS>
25 class base_blob
26 {
27 protected:
28  enum { WIDTH=BITS/8 };
29  uint8_t data[WIDTH];
30 public:
32  {
33  memset(data, 0, sizeof(data));
34  }
35 
36  explicit base_blob(const std::vector<unsigned char>& vch);
37 
38  bool IsNull() const
39  {
40  for (int i = 0; i < WIDTH; i++)
41  if (data[i] != 0)
42  return false;
43  return true;
44  }
45 
46  void SetNull()
47  {
48  memset(data, 0, sizeof(data));
49  }
50 
51  inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(data)); }
52 
53  friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
54  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
55  friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
56 
57  std::string GetHex() const;
58  std::string GetReverseHex() const;
59  void SetHex(const char* psz);
60  void SetHex(const std::string& str);
61  void SetReverseHex(const char* psz);
62  void SetReverseHex(const std::string& str);
63  std::string ToString() const;
64 
65  unsigned char* begin()
66  {
67  return &data[0];
68  }
69 
70  unsigned char* end()
71  {
72  return &data[WIDTH];
73  }
74 
75  const unsigned char* begin() const
76  {
77  return &data[0];
78  }
79 
80  const unsigned char* end() const
81  {
82  return &data[WIDTH];
83  }
84 
85  unsigned int size() const
86  {
87  return sizeof(data);
88  }
89 
90  uint64_t GetUint64(int pos) const
91  {
92  const uint8_t* ptr = data + pos * 8;
93  return ((uint64_t)ptr[0]) | \
94  ((uint64_t)ptr[1]) << 8 | \
95  ((uint64_t)ptr[2]) << 16 | \
96  ((uint64_t)ptr[3]) << 24 | \
97  ((uint64_t)ptr[4]) << 32 | \
98  ((uint64_t)ptr[5]) << 40 | \
99  ((uint64_t)ptr[6]) << 48 | \
100  ((uint64_t)ptr[7]) << 56;
101  }
102 
103  template<typename Stream>
104  void Serialize(Stream& s) const
105  {
106  s.write((char*)data, sizeof(data));
107  }
108 
109  template<typename Stream>
110  void Unserialize(Stream& s)
111  {
112  s.read((char*)data, sizeof(data));
113  }
114 };
115 
120 class uint160 : public base_blob<160> {
121 public:
122  uint160() {}
123  uint160(const base_blob<160>& b) : base_blob<160>(b) {}
124  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
125 };
126 
132 class uint256 : public base_blob<256> {
133 public:
134  uint256() {}
135  uint256(const base_blob<256>& b) : base_blob<256>(b) {}
136  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
137 
143  uint64_t GetCheapHash() const
144  {
145  return ReadLE64(data);
146  }
147 };
148 
149 /* uint256 from const char *.
150  * This is a separate function because the constructor uint256(const char*) can result
151  * in dangerously catching uint256(0).
152  */
153 inline uint256 uint256S(const char *str)
154 {
155  uint256 rv;
156  rv.SetHex(str);
157  return rv;
158 }
159 /* uint256 from std::string.
160  * This is a separate function because the constructor uint256(const std::string &str) can result
161  * in dangerously catching uint256(0) via std::string(const char*).
162  */
163 inline uint256 uint256S(const std::string& str)
164 {
165  uint256 rv;
166  rv.SetHex(str);
167  return rv;
168 }
169 
171 inline dev::h256 uintToh256(const uint256& in)
172 {
173  std::vector<unsigned char> vHashBlock;
174  vHashBlock.assign(in.begin(), in.end());
175  return dev::h256(vHashBlock);
176 }
177 
178 inline uint256 h256Touint(const dev::h256& in)
179 {
180  std::vector<unsigned char> vHashBlock = in.asBytes();
181  return uint256(vHashBlock);
182 }
183 
184 inline dev::u256 uintTou256(const uint256& in)
185 {
186  std::vector<unsigned char> rawValue;
187  rawValue.assign(in.begin(), in.end());
188  return dev::fromBigEndian<dev::u256, dev::bytes>(rawValue);
189 }
190 
191 inline uint256 u256Touint(const dev::u256& in)
192 {
193  std::vector<unsigned char> rawValue(32, 0);
194  dev::toBigEndian<dev::u256, dev::bytes>(in, rawValue);
195  return uint256(rawValue);
196 }
198 
199 #endif // FABCOIN_UINT256_H
uint8_t data[WIDTH]
Definition: uint256.h:29
base_blob()
Definition: uint256.h:31
uint256(const base_blob< 256 > &b)
Definition: uint256.h:135
void SetNull()
Definition: uint256.h:46
void Serialize(Stream &s) const
Definition: uint256.h:104
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:53
std::string GetHex() const
Definition: uint256.cpp:21
const unsigned char * end() const
Definition: uint256.h:80
uint64_t GetUint64(int pos) const
Definition: uint256.h:90
const unsigned char * begin() const
Definition: uint256.h:75
uint160()
Definition: uint256.h:122
unsigned char * begin()
Definition: uint256.h:65
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:143
unsigned char * end()
Definition: uint256.h:70
std::string ToString() const
Definition: uint256.cpp:95
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:54
#define a(i)
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:136
void Unserialize(Stream &s)
Definition: uint256.h:110
dev::h256 uintToh256(const uint256 &in)
Definition: uint256.h:171
dev::u256 uintTou256(const uint256 &in)
Definition: uint256.h:184
void SetReverseHex(const char *psz)
Definition: uint256.cpp:74
bool IsNull() const
Definition: uint256.h:38
std::string GetReverseHex() const
Definition: uint256.cpp:27
uint256 uint256S(const char *str)
Definition: uint256.h:153
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:55
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
uint256()
Definition: uint256.h:134
#define b(i, j)
bytes asBytes() const
Definition: FixedHash.h:145
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:25
256-bit opaque blob.
Definition: uint256.h:132
uint256 u256Touint(const dev::u256 &in)
Definition: uint256.h:191
uint160(const base_blob< 160 > &b)
Definition: uint256.h:123
160-bit opaque blob.
Definition: uint256.h:120
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:124
int Compare(const base_blob &other) const
Definition: uint256.h:51
void SetHex(const char *psz)
Definition: uint256.cpp:39
uint256 h256Touint(const dev::h256 &in)
Definition: uint256.h:178
unsigned int size() const
Definition: uint256.h:85