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 };
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 unsigned int GetSerializeSize(int nType
, int nVersion
) const
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> {
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> {
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
127 memcpy((void*)&result
, (void*)data
, 8);
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
)
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
)
158 #endif // BITCOIN_UINT256_H