Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven 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 RAVEN_UINT256_H
8 #define RAVEN_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  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 
128  int GetNibble(int index) const
129  {
130  index = 63 - index;
131  if (index % 2 == 1)
132  return(data[index / 2] >> 4);
133  return(data[index / 2] & 0x0F);
134  }
140  uint64_t GetCheapHash() const
141  {
142  return ReadLE64(data);
143  }
144 };
145 
146 /* uint256 from const char *.
147  * This is a separate function because the constructor uint256(const char*) can result
148  * in dangerously catching uint256(0).
149  */
150 inline uint256 uint256S(const char *str)
151 {
152  uint256 rv;
153  rv.SetHex(str);
154  return rv;
155 }
156 /* uint256 from std::string.
157  * This is a separate function because the constructor uint256(const std::string &str) can result
158  * in dangerously catching uint256(0) via std::string(const char*).
159  */
160 inline uint256 uint256S(const std::string& str)
161 {
162  uint256 rv;
163  rv.SetHex(str);
164  return rv;
165 }
166 
167 class uint512 : public base_blob<512> {
168 public:
169  uint512() {}
170  uint512(const base_blob<512>& b) : base_blob<512>(b) {}
171  explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
172  uint256 trim256() const
173  {
174  uint256 result;
175  memcpy((void*)&result, (void*)data, 32);
176  return result;
177  }
178 };
179 #endif // RAVEN_UINT256_H
uint8_t data[WIDTH]
Definition: uint256.h:24
base_blob()
Definition: uint256.h:26
uint256 trim256() const
Definition: uint256.h:172
void SetNull()
Definition: uint256.h:41
int GetNibble(int index) const
Definition: uint256.h:128
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:170
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
uint512(const std::vector< unsigned char > &vch)
Definition: uint256.h:171
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
uint256 uint256S(const char *str)
Definition: uint256.h:150
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:50
std::string ToString() const
Definition: uint256.cpp:63
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
void * memcpy(void *a, const void *b, size_t c)
std::string GetHex() const
Definition: uint256.cpp:22
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:140
uint64_t GetUint64(int pos) const
Definition: uint256.h:82
void SetHex(const char *psz)
Definition: uint256.cpp:28
uint512()
Definition: uint256.h:169