Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / uint256.h
blobe090f1023105ac78056d7d0cb392150ac2f4fea1
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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>
15 #include <crypto/common.h>
17 /** Template base class for fixed-sized opaque blobs. */
18 template<unsigned int BITS>
19 class base_blob
21 protected:
22 static constexpr int WIDTH = BITS / 8;
23 uint8_t data[WIDTH];
24 public:
25 base_blob()
27 memset(data, 0, sizeof(data));
30 explicit base_blob(const std::vector<unsigned char>& vch);
32 bool IsNull() const
34 for (int i = 0; i < WIDTH; i++)
35 if (data[i] != 0)
36 return false;
37 return true;
40 void SetNull()
42 memset(data, 0, sizeof(data));
45 inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(data)); }
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; }
51 std::string GetHex() const;
52 void SetHex(const char* psz);
53 void SetHex(const std::string& str);
54 std::string ToString() const;
56 unsigned char* begin()
58 return &data[0];
61 unsigned char* end()
63 return &data[WIDTH];
66 const unsigned char* begin() const
68 return &data[0];
71 const unsigned char* end() const
73 return &data[WIDTH];
76 unsigned int size() const
78 return sizeof(data);
81 uint64_t GetUint64(int pos) const
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;
94 template<typename Stream>
95 void Serialize(Stream& s) const
97 s.write((char*)data, sizeof(data));
100 template<typename Stream>
101 void Unserialize(Stream& s)
103 s.read((char*)data, sizeof(data));
107 /** 160-bit opaque blob.
108 * @note This type is called uint160 for historical reasons only. It is an opaque
109 * blob of 160 bits and has no integer operations.
111 class uint160 : public base_blob<160> {
112 public:
113 uint160() {}
114 explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
117 /** 256-bit opaque blob.
118 * @note This type is called uint256 for historical reasons only. It is an
119 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
120 * those are required.
122 class uint256 : public base_blob<256> {
123 public:
124 uint256() {}
125 explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
127 /** A cheap hash function that just returns 64 bits from the result, it can be
128 * used when the contents are considered uniformly random. It is not appropriate
129 * when the value can easily be influenced from outside as e.g. a network adversary could
130 * provide values to trigger worst-case behavior.
132 uint64_t GetCheapHash() const
134 return ReadLE64(data);
138 /* uint256 from const char *.
139 * This is a separate function because the constructor uint256(const char*) can result
140 * in dangerously catching uint256(0).
142 inline uint256 uint256S(const char *str)
144 uint256 rv;
145 rv.SetHex(str);
146 return rv;
148 /* uint256 from std::string.
149 * This is a separate function because the constructor uint256(const std::string &str) can result
150 * in dangerously catching uint256(0) via std::string(const char*).
152 inline uint256 uint256S(const std::string& str)
154 uint256 rv;
155 rv.SetHex(str);
156 return rv;
159 #endif // BITCOIN_UINT256_H