30 #include <boost/functional/hash.hpp> 50 using Arith = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
89 operator Arith()
const {
return fromBigEndian<Arith>(m_data); }
92 explicit operator bool()
const {
return std::any_of(m_data.begin(), m_data.end(), [](
byte _b) {
return _b != 0; }); }
97 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; }
144 auto begin() const -> typename
std::array<
byte, N>::const_iterator {
return m_data.begin(); }
147 auto end() const -> typename
std::array<
byte, N>::const_iterator {
return m_data.end(); }
153 std::array<byte, N>&
asArray() {
return m_data; }
156 std::array<byte, N>
const&
asArray()
const {
return m_data; }
159 template <
class Engine>
162 for (
auto& i: m_data)
163 i = (uint8_t)std::uniform_int_distribution<uint16_t>(0, 255)(_eng);
172 size_t operator()(
FixedHash const& _value)
const {
return boost::hash_range(_value.m_data.cbegin(), _value.m_data.cend()); }
177 return (*
this |= _h.template bloomPart<P, N>());
182 return contains(_h.template bloomPart<P, N>());
187 unsigned const c_bloomBits = M * 8;
188 unsigned const c_mask = c_bloomBits - 1;
191 static_assert((M & (M - 1)) == 0,
"M must be power-of-two");
192 static_assert(P * c_bloomBytes <= N,
"out of range");
196 for (
unsigned i = 0; i < P; ++i)
199 for (
unsigned j = 0; j < c_bloomBytes; ++j, ++p)
200 index = (index << 8) | *p;
202 ret[M - 1 - index / 8] |= (1 << (index % 8));
214 for (;; ++ret, d <<= 1)
228 std::array<byte, N> m_data;
231 template <
unsigned T>
312 const uint64_t* hash1 = (
const uint64_t*)data();
313 const uint64_t* hash2 = (
const uint64_t*)_other.
data();
314 return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2]) && (hash1[3] == hash2[3]);
320 uint64_t
const* data =
reinterpret_cast<uint64_t const*
>(value.
data());
321 return boost::hash_range(data, data + 4);
325 template <
unsigned N>
326 inline std::ostream& operator<<(std::ostream& _out, FixedHash<N>
const& _h)
332 template <
unsigned N>
342 template <
unsigned N>
343 inline std::ostream& operator<<(std::ostream& _out, SecureFixedHash<N>
const& _h)
345 _out <<
"SecureFixedHash#" << std::hex << typename FixedHash<N>::hash()(_h.makeInsecure()) << std::dec;
370 memcpy(ret.
data(), _t.
data() + 12, 20);
380 std::ostringstream out;
382 for (
h256 const& i: _bs)
383 out << i.abridged() <<
", ";
396 template<>
struct hash<
dev::
h256>: dev::h256::hash {};
bool operator!=(SecureFixedHash const &_c) const
bytesConstRef ref() const
h128 fromUUID(std::string const &_uuid)
FixedHash operator &(FixedHash const &_c) const
byte operator[](unsigned _i) const
auto end() const -> typename std::array< byte, N >::const_iterator
FixedHash & operator|=(FixedHash const &_c)
bool operator<=(SecureFixedHash const &_c) const
void toBigEndian(T _val, Out &o_out)
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
std::istream & operator>>(std::istream &_in, FixedHash< N > &o_h)
unsigned firstBitSet() const
Returns the index of the first bit set to one, or size() * 8 if no bits are set.
FixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=AlignLeft)
Construct from another hash, filling with zeroes or cropping as necessary.
secure_vector< byte > bytesSec
FixedHash(unsigned _u)
Convert from unsigned.
SecureFixedHash & operator|=(FixedHash< T > const &_c)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > Arith
The corresponding arithmetic type.
bool operator>(FixedHash const &_c) const
byte & operator[](unsigned _i)
bool operator<(SecureFixedHash const &_c) const
Compile-time calculation of Log2 of constant values.
std::string abridged() const
FixedHash & shiftBloom(FixedHash< M > const &_h)
byte const * data() const
FixedHash()
Construct an empty hash.
std::vector< h512 > h512s
bool operator>=(SecureFixedHash const &_c) const
SecureFixedHash(bytesSec const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
SecureFixedHash(std::string const &_s, ConstructFromStringType _t=FixedHash< T >::FromHex, ConstructFromHashType _ht=FixedHash< T >::FailIfDifferent)
std::random_device s_fixedHashEngine
FixedHash operator^(FixedHash const &_c) const
void randomize(Engine &_eng)
Populate with random data.
FixedHash operator~() const
bool contains(FixedHash const &_c) const
SecureFixedHash(bytes const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
SecureFixedHash & operator &=(FixedHash< T > const &_c)
bool containsBloom(FixedHash< M > const &_h)
FixedHash(bytes const &_b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
FixedHash operator|(FixedHash const &_c) const
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
FixedHash(std::string const &_s, ConstructFromStringType _t=FromHex, ConstructFromHashType _ht=FailIfDifferent)
Explicitly construct, copying from a string.
FixedHash(byte const *_bs, ConstructFromPointerType)
Explicitly construct, copying from a bytes in memory with given pointer.
byte const * data() const
SecureFixedHash()=default
FixedHash(Arith const &_arith)
Convert from the corresponding arithmetic type.
static SecureFixedHash< T > random()
vector_ref< byte > bytesRef
std::string abridgedMiddle() const
SecureFixedHash & operator|=(SecureFixedHash const &_c)
SecureFixedHash operator^(SecureFixedHash const &_c) const
FixedHash(bytesConstRef _b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
SecureFixedHash(bytesConstRef _b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
auto begin() const -> typename std::array< byte, N >::const_iterator
std::unordered_set< h160 > h160Hash
bytesConstRef ref() const
SecureFixedHash operator &(FixedHash< T > const &_c) const
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
FixedHash< T > & writable()
SecureFixedHash operator^(FixedHash< T > const &_c) const
bool operator==(FixedHash const &_c) const
SecureFixedHash & operator &=(SecureFixedHash const &_c)
FixedHash & operator &=(FixedHash const &_c)
bool operator<=(FixedHash const &_c) const
bool operator!=(FixedHash const &_c) const
std::array< byte, N > const & asArray() const
SecureFixedHash & operator^=(FixedHash< T > const &_c)
static FixedHash random()
std::vector< h160 > h160s
bool operator<(FixedHash const &_c) const
SecureFixedHash(bytes const *_d, ConstructFromPointerType _t)
SecureFixedHash & operator^=(SecureFixedHash const &_c)
size_t operator()(FixedHash const &_value) const
Make a hash of the object's data.
SecureFixedHash operator|(SecureFixedHash const &_c) const
SecureFixedHash operator|(FixedHash< T > const &_c) const
bool operator==(SecureFixedHash const &_c) const
std::unordered_set< h256 > h256Hash
bool operator>=(FixedHash const &_c) const
FixedHash< M > bloomPart() const
SecureFixedHash operator &(SecureFixedHash const &_c) const
ConstructFromHashType
Method to convert from a string.
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
std::array< byte, N > & asArray()
FixedHash< T > const & makeInsecure() const
bool operator>(SecureFixedHash const &_c) const
std::string toString(h256s const &_bs)
std::vector< h256 > h256s
bytesSec asBytesSec() const
ConstructFromPointerType
A dummy flag to avoid accidental construction from pointer.
ConstructFromStringType
Method to convert from a string.
FixedHash & operator^=(FixedHash const &_c)
SecureFixedHash(SecureFixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)
SecureFixedHash< T > & operator=(SecureFixedHash< T > const &_c)
std::string toUUID(h128 const &_uuid)
SecureFixedHash operator~() const
SecureFixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)