Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / uint256.h
blob6d016ab164867096e0e9b59ce89b6e8ec17f76aa
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
9 #include <assert.h>
10 #include <cstring>
11 #include <stdexcept>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
16 /** Template base class for fixed-sized opaque blobs. */
17 template<unsigned int BITS>
18 class base_blob
20 protected:
21 enum { WIDTH=BITS/8 };
22 uint8_t data[WIDTH];
23 public:
24 base_blob()
26 memset(data, 0, sizeof(data));
29 explicit base_blob(const std::vector<unsigned char>& vch);
31 bool IsNull() const
33 for (int i = 0; i < WIDTH; i++)
34 if (data[i] != 0)
35 return false;
36 return true;
39 void SetNull()
41 memset(data, 0, sizeof(data));
44 friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
45 friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
46 friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
48 std::string GetHex() const;
49 void SetHex(const char* psz);
50 void SetHex(const std::string& str);
51 std::string ToString() const;
53 unsigned char* begin()
55 return &data[0];
58 unsigned char* end()
60 return &data[WIDTH];
63 const unsigned char* begin() const
65 return &data[0];
68 const unsigned char* end() const
70 return &data[WIDTH];
73 unsigned int size() const
75 return sizeof(data);
78 unsigned int GetSerializeSize(int nType, int nVersion) const
80 return sizeof(data);
83 template<typename Stream>
84 void Serialize(Stream& s, int nType, int nVersion) const
86 s.write((char*)data, sizeof(data));
89 template<typename Stream>
90 void Unserialize(Stream& s, int nType, int nVersion)
92 s.read((char*)data, sizeof(data));
96 /** 160-bit opaque blob.
97 * @note This type is called uint160 for historical reasons only. It is an opaque
98 * blob of 160 bits and has no integer operations.
100 class uint160 : public base_blob<160> {
101 public:
102 uint160() {}
103 uint160(const base_blob<160>& b) : base_blob<160>(b) {}
104 explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
107 /** 256-bit opaque blob.
108 * @note This type is called uint256 for historical reasons only. It is an
109 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
110 * those are required.
112 class uint256 : public base_blob<256> {
113 public:
114 uint256() {}
115 uint256(const base_blob<256>& b) : base_blob<256>(b) {}
116 explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
118 /** A cheap hash function that just returns 64 bits from the result, it can be
119 * used when the contents are considered uniformly random. It is not appropriate
120 * when the value can easily be influenced from outside as e.g. a network adversary could
121 * provide values to trigger worst-case behavior.
122 * @note The result of this function is not stable between little and big endian.
124 uint64_t GetCheapHash() const
126 uint64_t result;
127 memcpy((void*)&result, (void*)data, 8);
128 return result;
131 /** A more secure, salted hash function.
132 * @note This hash is not stable between little and big endian.
134 uint64_t GetHash(const uint256& salt) const;
137 /* uint256 from const char *.
138 * This is a separate function because the constructor uint256(const char*) can result
139 * in dangerously catching uint256(0).
141 inline uint256 uint256S(const char *str)
143 uint256 rv;
144 rv.SetHex(str);
145 return rv;
147 /* uint256 from std::string.
148 * This is a separate function because the constructor uint256(const std::string &str) can result
149 * in dangerously catching uint256(0) via std::string(const char*).
151 inline uint256 uint256S(const std::string& str)
153 uint256 rv;
154 rv.SetHex(str);
155 return rv;
158 #endif // BITCOIN_UINT256_H