FixedHash.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file FixedHash.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * The FixedHash fixed-size "hash" container type.
  19. */
  20. #pragma once
  21. #include <array>
  22. #include <cstdint>
  23. #include <algorithm>
  24. #include <boost/random/random_device.hpp>
  25. #include <boost/random/uniform_int_distribution.hpp>
  26. #include <boost/functional/hash.hpp>
  27. #include "CommonData.h"
  28. namespace dev
  29. {
  30. /// Compile-time calculation of Log2 of constant values.
  31. template <unsigned N> struct StaticLog2 { enum { result = 1 + StaticLog2<N/2>::result }; };
  32. template <> struct StaticLog2<1> { enum { result = 0 }; };
  33. extern boost::random_device s_fixedHashEngine;
  34. /// Fixed-size raw-byte array container type, with an API optimised for storing hashes.
  35. /// Transparently converts to/from the corresponding arithmetic type; this will
  36. /// assume the data contained in the hash is big-endian.
  37. template <unsigned N>
  38. class FixedHash
  39. {
  40. public:
  41. /// The corresponding arithmetic type.
  42. using Arith = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
  43. /// The size of the container.
  44. enum { size = N };
  45. /// A dummy flag to avoid accidental construction from pointer.
  46. enum ConstructFromPointerType { ConstructFromPointer };
  47. /// Method to convert from a string.
  48. enum ConstructFromStringType { FromHex, FromBinary };
  49. /// Method to convert from a string.
  50. enum ConstructFromHashType { AlignLeft, AlignRight, FailIfDifferent };
  51. /// Construct an empty hash.
  52. FixedHash() { m_data.fill(0); }
  53. /// Construct from another hash, filling with zeroes or cropping as necessary.
  54. template <unsigned M> explicit FixedHash(FixedHash<M> const& _h, ConstructFromHashType _t = AlignLeft) { m_data.fill(0); unsigned c = std::min(M, N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _h[_t == AlignRight ? M - 1 - i : i]; }
  55. /// Convert from the corresponding arithmetic type.
  56. FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); }
  57. /// Convert from unsigned
  58. explicit FixedHash(unsigned _u) { toBigEndian(_u, m_data); }
  59. /// Explicitly construct, copying from a byte array.
  60. explicit FixedHash(bytes const& _b, ConstructFromHashType _t = FailIfDifferent) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N)); else { m_data.fill(0); if (_t != FailIfDifferent) { auto c = std::min<unsigned>(_b.size(), N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } }
  61. /// Explicitly construct, copying from a byte array.
  62. explicit FixedHash(bytesConstRef _b, ConstructFromHashType _t = FailIfDifferent) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N)); else { m_data.fill(0); if (_t != FailIfDifferent) { auto c = std::min<unsigned>(_b.size(), N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } }
  63. /// Explicitly construct, copying from a bytes in memory with given pointer.
  64. explicit FixedHash(byte const* _bs, ConstructFromPointerType) { memcpy(m_data.data(), _bs, N); }
  65. /// Explicitly construct, copying from a string.
  66. explicit FixedHash(std::string const& _s, ConstructFromStringType _t = FromHex, ConstructFromHashType _ht = FailIfDifferent): FixedHash(_t == FromHex ? fromHex(_s, WhenError::Throw) : dev::asBytes(_s), _ht) {}
  67. /// Convert to arithmetic type.
  68. operator Arith() const { return fromBigEndian<Arith>(m_data); }
  69. /// @returns true iff this is the empty hash.
  70. explicit operator bool() const { return std::any_of(m_data.begin(), m_data.end(), [](byte _b) { return _b != 0; }); }
  71. // The obvious comparison operators.
  72. bool operator==(FixedHash const& _c) const { return m_data == _c.m_data; }
  73. bool operator!=(FixedHash const& _c) const { return m_data != _c.m_data; }
  74. bool operator<(FixedHash const& _c) const { for (unsigned i = 0; i < N; ++i) if (m_data[i] < _c.m_data[i]) return true; else if (m_data[i] > _c.m_data[i]) return false; return false; }
  75. bool operator>=(FixedHash const& _c) const { return !operator<(_c); }
  76. bool operator<=(FixedHash const& _c) const { return operator==(_c) || operator<(_c); }
  77. bool operator>(FixedHash const& _c) const { return !operator<=(_c); }
  78. // The obvious binary operators.
  79. FixedHash& operator^=(FixedHash const& _c) { for (unsigned i = 0; i < N; ++i) m_data[i] ^= _c.m_data[i]; return *this; }
  80. FixedHash operator^(FixedHash const& _c) const { return FixedHash(*this) ^= _c; }
  81. FixedHash& operator|=(FixedHash const& _c) { for (unsigned i = 0; i < N; ++i) m_data[i] |= _c.m_data[i]; return *this; }
  82. FixedHash operator|(FixedHash const& _c) const { return FixedHash(*this) |= _c; }
  83. FixedHash& operator&=(FixedHash const& _c) { for (unsigned i = 0; i < N; ++i) m_data[i] &= _c.m_data[i]; return *this; }
  84. FixedHash operator&(FixedHash const& _c) const { return FixedHash(*this) &= _c; }
  85. FixedHash operator~() const { FixedHash ret; for (unsigned i = 0; i < N; ++i) ret[i] = ~m_data[i]; return ret; }
  86. // Big-endian increment.
  87. FixedHash& operator++() { for (unsigned i = size; i > 0 && !++m_data[--i]; ) {} return *this; }
  88. /// @returns true if all one-bits in @a _c are set in this object.
  89. bool contains(FixedHash const& _c) const { return (*this & _c) == _c; }
  90. /// @returns a particular byte from the hash.
  91. byte& operator[](unsigned _i) { return m_data[_i]; }
  92. /// @returns a particular byte from the hash.
  93. byte operator[](unsigned _i) const { return m_data[_i]; }
  94. /// @returns an abridged version of the hash as a user-readable hex string.
  95. std::string abridged() const { return toHex(ref().cropped(0, 4)) + "\342\200\246"; }
  96. /// @returns a version of the hash as a user-readable hex string that leaves out the middle part.
  97. std::string abridgedMiddle() const { return toHex(ref().cropped(0, 4)) + "\342\200\246" + toHex(ref().cropped(N - 4)); }
  98. /// @returns the hash as a user-readable hex string.
  99. std::string hex() const { return toHex(ref()); }
  100. /// @returns a mutable byte vector_ref to the object's data.
  101. bytesRef ref() { return bytesRef(m_data.data(), N); }
  102. /// @returns a constant byte vector_ref to the object's data.
  103. bytesConstRef ref() const { return bytesConstRef(m_data.data(), N); }
  104. /// @returns a mutable byte pointer to the object's data.
  105. byte* data() { return m_data.data(); }
  106. /// @returns a constant byte pointer to the object's data.
  107. byte const* data() const { return m_data.data(); }
  108. /// @returns a copy of the object's data as a byte vector.
  109. bytes asBytes() const { return bytes(data(), data() + N); }
  110. /// @returns a mutable reference to the object's data as an STL array.
  111. std::array<byte, N>& asArray() { return m_data; }
  112. /// @returns a constant reference to the object's data as an STL array.
  113. std::array<byte, N> const& asArray() const { return m_data; }
  114. /// Populate with random data.
  115. template <class Engine>
  116. void randomize(Engine& _eng)
  117. {
  118. for (auto& i: m_data)
  119. i = (uint8_t)boost::random::uniform_int_distribution<uint16_t>(0, 255)(_eng);
  120. }
  121. /// @returns a random valued object.
  122. static FixedHash random() { FixedHash ret; ret.randomize(s_fixedHashEngine); return ret; }
  123. struct hash
  124. {
  125. /// Make a hash of the object's data.
  126. size_t operator()(FixedHash const& _value) const { return boost::hash_range(_value.m_data.cbegin(), _value.m_data.cend()); }
  127. };
  128. template <unsigned P, unsigned M> inline FixedHash& shiftBloom(FixedHash<M> const& _h)
  129. {
  130. return (*this |= _h.template bloomPart<P, N>());
  131. }
  132. template <unsigned P, unsigned M> inline bool containsBloom(FixedHash<M> const& _h)
  133. {
  134. return contains(_h.template bloomPart<P, N>());
  135. }
  136. template <unsigned P, unsigned M> inline FixedHash<M> bloomPart() const
  137. {
  138. unsigned const c_bloomBits = M * 8;
  139. unsigned const c_mask = c_bloomBits - 1;
  140. unsigned const c_bloomBytes = (StaticLog2<c_bloomBits>::result + 7) / 8;
  141. static_assert((M & (M - 1)) == 0, "M must be power-of-two");
  142. static_assert(P * c_bloomBytes <= N, "out of range");
  143. FixedHash<M> ret;
  144. byte const* p = data();
  145. for (unsigned i = 0; i < P; ++i)
  146. {
  147. unsigned index = 0;
  148. for (unsigned j = 0; j < c_bloomBytes; ++j, ++p)
  149. index = (index << 8) | *p;
  150. index &= c_mask;
  151. ret[M - 1 - index / 8] |= (1 << (index % 8));
  152. }
  153. return ret;
  154. }
  155. /// Returns the index of the first bit set to one, or size() * 8 if no bits are set.
  156. inline unsigned firstBitSet() const
  157. {
  158. unsigned ret = 0;
  159. for (auto d: m_data)
  160. if (d)
  161. for (;; ++ret, d <<= 1)
  162. if (d & 0x80)
  163. return ret;
  164. else {}
  165. else
  166. ret += 8;
  167. return ret;
  168. }
  169. void clear() { m_data.fill(0); }
  170. private:
  171. std::array<byte, N> m_data; ///< The binary data.
  172. };
  173. template <unsigned T>
  174. class SecureFixedHash: private FixedHash<T>
  175. {
  176. public:
  177. using ConstructFromHashType = typename FixedHash<T>::ConstructFromHashType;
  178. using ConstructFromStringType = typename FixedHash<T>::ConstructFromStringType;
  179. using ConstructFromPointerType = typename FixedHash<T>::ConstructFromPointerType;
  180. SecureFixedHash() = default;
  181. explicit SecureFixedHash(bytes const& _b, ConstructFromHashType _t = FixedHash<T>::FailIfDifferent): FixedHash<T>(_b, _t) {}
  182. explicit SecureFixedHash(bytesConstRef _b, ConstructFromHashType _t = FixedHash<T>::FailIfDifferent): FixedHash<T>(_b, _t) {}
  183. explicit SecureFixedHash(bytesSec const& _b, ConstructFromHashType _t = FixedHash<T>::FailIfDifferent): FixedHash<T>(_b.ref(), _t) {}
  184. template <unsigned M> explicit SecureFixedHash(FixedHash<M> const& _h, ConstructFromHashType _t = FixedHash<T>::AlignLeft): FixedHash<T>(_h, _t) {}
  185. template <unsigned M> explicit SecureFixedHash(SecureFixedHash<M> const& _h, ConstructFromHashType _t = FixedHash<T>::AlignLeft): FixedHash<T>(_h.makeInsecure(), _t) {}
  186. explicit SecureFixedHash(std::string const& _s, ConstructFromStringType _t = FixedHash<T>::FromHex, ConstructFromHashType _ht = FixedHash<T>::FailIfDifferent): FixedHash<T>(_s, _t, _ht) {}
  187. explicit SecureFixedHash(bytes const* _d, ConstructFromPointerType _t): FixedHash<T>(_d, _t) {}
  188. ~SecureFixedHash() { ref().cleanse(); }
  189. SecureFixedHash<T>& operator=(SecureFixedHash<T> const& _c)
  190. {
  191. if (&_c == this)
  192. return *this;
  193. ref().cleanse();
  194. FixedHash<T>::operator=(static_cast<FixedHash<T> const&>(_c));
  195. return *this;
  196. }
  197. using FixedHash<T>::size;
  198. bytesSec asBytesSec() const { return bytesSec(ref()); }
  199. FixedHash<T> const& makeInsecure() const { return static_cast<FixedHash<T> const&>(*this); }
  200. FixedHash<T>& writable() { clear(); return static_cast<FixedHash<T>&>(*this); }
  201. using FixedHash<T>::operator bool;
  202. // The obvious comparison operators.
  203. bool operator==(SecureFixedHash const& _c) const { return static_cast<FixedHash<T> const&>(*this).operator==(static_cast<FixedHash<T> const&>(_c)); }
  204. bool operator!=(SecureFixedHash const& _c) const { return static_cast<FixedHash<T> const&>(*this).operator!=(static_cast<FixedHash<T> const&>(_c)); }
  205. bool operator<(SecureFixedHash const& _c) const { return static_cast<FixedHash<T> const&>(*this).operator<(static_cast<FixedHash<T> const&>(_c)); }
  206. bool operator>=(SecureFixedHash const& _c) const { return static_cast<FixedHash<T> const&>(*this).operator>=(static_cast<FixedHash<T> const&>(_c)); }
  207. bool operator<=(SecureFixedHash const& _c) const { return static_cast<FixedHash<T> const&>(*this).operator<=(static_cast<FixedHash<T> const&>(_c)); }
  208. bool operator>(SecureFixedHash const& _c) const { return static_cast<FixedHash<T> const&>(*this).operator>(static_cast<FixedHash<T> const&>(_c)); }
  209. using FixedHash<T>::operator==;
  210. using FixedHash<T>::operator!=;
  211. using FixedHash<T>::operator<;
  212. using FixedHash<T>::operator>=;
  213. using FixedHash<T>::operator<=;
  214. using FixedHash<T>::operator>;
  215. // The obvious binary operators.
  216. SecureFixedHash& operator^=(FixedHash<T> const& _c) { static_cast<FixedHash<T>&>(*this).operator^=(_c); return *this; }
  217. SecureFixedHash operator^(FixedHash<T> const& _c) const { return SecureFixedHash(*this) ^= _c; }
  218. SecureFixedHash& operator|=(FixedHash<T> const& _c) { static_cast<FixedHash<T>&>(*this).operator^=(_c); return *this; }
  219. SecureFixedHash operator|(FixedHash<T> const& _c) const { return SecureFixedHash(*this) |= _c; }
  220. SecureFixedHash& operator&=(FixedHash<T> const& _c) { static_cast<FixedHash<T>&>(*this).operator^=(_c); return *this; }
  221. SecureFixedHash operator&(FixedHash<T> const& _c) const { return SecureFixedHash(*this) &= _c; }
  222. SecureFixedHash& operator^=(SecureFixedHash const& _c) { static_cast<FixedHash<T>&>(*this).operator^=(static_cast<FixedHash<T> const&>(_c)); return *this; }
  223. SecureFixedHash operator^(SecureFixedHash const& _c) const { return SecureFixedHash(*this) ^= _c; }
  224. SecureFixedHash& operator|=(SecureFixedHash const& _c) { static_cast<FixedHash<T>&>(*this).operator^=(static_cast<FixedHash<T> const&>(_c)); return *this; }
  225. SecureFixedHash operator|(SecureFixedHash const& _c) const { return SecureFixedHash(*this) |= _c; }
  226. SecureFixedHash& operator&=(SecureFixedHash const& _c) { static_cast<FixedHash<T>&>(*this).operator^=(static_cast<FixedHash<T> const&>(_c)); return *this; }
  227. SecureFixedHash operator&(SecureFixedHash const& _c) const { return SecureFixedHash(*this) &= _c; }
  228. SecureFixedHash operator~() const { auto r = ~static_cast<FixedHash<T> const&>(*this); return static_cast<SecureFixedHash const&>(r); }
  229. using FixedHash<T>::abridged;
  230. using FixedHash<T>::abridgedMiddle;
  231. bytesConstRef ref() const { return FixedHash<T>::ref(); }
  232. byte const* data() const { return FixedHash<T>::data(); }
  233. static SecureFixedHash<T> random() { SecureFixedHash<T> ret; ret.randomize(s_fixedHashEngine); return ret; }
  234. using FixedHash<T>::firstBitSet;
  235. void clear() { ref().cleanse(); }
  236. };
  237. /// Fast equality operator for h256.
  238. template<> inline bool FixedHash<32>::operator==(FixedHash<32> const& _other) const
  239. {
  240. const uint64_t* hash1 = (const uint64_t*)data();
  241. const uint64_t* hash2 = (const uint64_t*)_other.data();
  242. return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2]) && (hash1[3] == hash2[3]);
  243. }
  244. /// Fast std::hash compatible hash function object for h256.
  245. template<> inline size_t FixedHash<32>::hash::operator()(FixedHash<32> const& value) const
  246. {
  247. uint64_t const* data = reinterpret_cast<uint64_t const*>(value.data());
  248. return boost::hash_range(data, data + 4);
  249. }
  250. /// Stream I/O for the FixedHash class.
  251. template <unsigned N>
  252. inline std::ostream& operator<<(std::ostream& _out, FixedHash<N> const& _h)
  253. {
  254. _out << std::noshowbase << std::hex << std::setfill('0');
  255. for (unsigned i = 0; i < N; ++i)
  256. _out << std::setw(2) << (int)_h[i];
  257. _out << std::dec;
  258. return _out;
  259. }
  260. /// Stream I/O for the SecureFixedHash class.
  261. template <unsigned N>
  262. inline std::ostream& operator<<(std::ostream& _out, SecureFixedHash<N> const& _h)
  263. {
  264. _out << "SecureFixedHash#" << std::hex << typename FixedHash<N>::hash()(_h.makeInsecure()) << std::dec;
  265. return _out;
  266. }
  267. // Common types of FixedHash.
  268. using h2048 = FixedHash<256>;
  269. using h1024 = FixedHash<128>;
  270. using h520 = FixedHash<65>;
  271. using h512 = FixedHash<64>;
  272. using h256 = FixedHash<32>;
  273. using h160 = FixedHash<20>;
  274. using h128 = FixedHash<16>;
  275. using h64 = FixedHash<8>;
  276. using h512s = std::vector<h512>;
  277. using h256s = std::vector<h256>;
  278. using h160s = std::vector<h160>;
  279. using h256Set = std::set<h256>;
  280. using h160Set = std::set<h160>;
  281. using h256Hash = std::unordered_set<h256>;
  282. using h160Hash = std::unordered_set<h160>;
  283. /// Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
  284. inline h160 right160(h256 const& _t)
  285. {
  286. h160 ret;
  287. memcpy(ret.data(), _t.data() + 12, 20);
  288. return ret;
  289. }
  290. /// Convert the given value into h160 (160-bit unsigned integer) using the left 20 bytes.
  291. inline h160 left160(h256 const& _t)
  292. {
  293. h160 ret;
  294. memcpy(&ret[0], _t.data(), 20);
  295. return ret;
  296. }
  297. h128 fromUUID(std::string const& _uuid);
  298. std::string toUUID(h128 const& _uuid);
  299. inline std::string toString(h256s const& _bs)
  300. {
  301. std::ostringstream out;
  302. out << "[ ";
  303. for (auto i: _bs)
  304. out << i.abridged() << ", ";
  305. out << "]";
  306. return out.str();
  307. }
  308. }
  309. namespace std
  310. {
  311. /// Forward std::hash<dev::FixedHash> to dev::FixedHash::hash.
  312. template<> struct hash<dev::h64>: dev::h64::hash {};
  313. template<> struct hash<dev::h128>: dev::h128::hash {};
  314. template<> struct hash<dev::h160>: dev::h160::hash {};
  315. template<> struct hash<dev::h256>: dev::h256::hash {};
  316. template<> struct hash<dev::h512>: dev::h512::hash {};
  317. }