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
16 /** Template base class for fixed-sized opaque blobs. */
17 template<unsigned int BITS>
21 enum { WIDTH=BITS/8 };
22 alignas(uint32_t) uint8_t data[WIDTH];
26 memset(data, 0, sizeof(data));
29 explicit base_blob(const std::vector<unsigned char>& vch);
33 for (int i = 0; i < WIDTH; i++)
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()
63 const unsigned char* begin() const
68 const unsigned char* end() const
73 unsigned int size() const
78 template<typename Stream>
79 void Serialize(Stream& s, int nType, int nVersion) const
81 s.write((char*)data, sizeof(data));
84 template<typename Stream>
85 void Unserialize(Stream& s, int nType, int nVersion)
87 s.read((char*)data, sizeof(data));
91 /** 160-bit opaque blob.
92 * @note This type is called uint160 for historical reasons only. It is an opaque
93 * blob of 160 bits and has no integer operations.
95 class uint160 : public base_blob<160> {
98 uint160(const base_blob<160>& b) : base_blob<160>(b) {}
99 explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
102 /** 256-bit opaque blob.
103 * @note This type is called uint256 for historical reasons only. It is an
104 * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
105 * those are required.
107 class uint256 : public base_blob<256> {
110 uint256(const base_blob<256>& b) : base_blob<256>(b) {}
111 explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
113 /** A cheap hash function that just returns 64 bits from the result, it can be
114 * used when the contents are considered uniformly random. It is not appropriate
115 * when the value can easily be influenced from outside as e.g. a network adversary could
116 * provide values to trigger worst-case behavior.
117 * @note The result of this function is not stable between little and big endian.
119 uint64_t GetCheapHash() const
122 memcpy((void*)&result, (void*)data, 8);
126 /** A more secure, salted hash function.
127 * @note This hash is not stable between little and big endian.
129 uint64_t GetHash(const uint256& salt) const;
132 /* uint256 from const char *.
133 * This is a separate function because the constructor uint256(const char*) can result
134 * in dangerously catching uint256(0).
136 inline uint256 uint256S(const char *str)
142 /* uint256 from std::string.
143 * This is a separate function because the constructor uint256(const std::string &str) can result
144 * in dangerously catching uint256(0) via std::string(const char*).
146 inline uint256 uint256S(const std::string& str)
153 #endif // BITCOIN_UINT256_H