30#include <opendht/rng.h>
75 unsigned c = std::min(M, N);
76 for (
unsigned i = 0; i < c; ++i)
81 explicit FixedHash(
unsigned _u) { toBigEndian(_u, m_data); }
87 memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N));
91 auto c = std::min<unsigned>(_b.size(), N);
92 for (
unsigned i = 0; i < c; ++i)
94 = _b[_t ==
AlignRight ? _b.size() - 1 - i : i];
103 memcpy(m_data.data(), _b.
data(), std::min<unsigned>(_b.
size(), N));
107 auto c = std::min<unsigned>(_b.
size(), N);
108 for (
unsigned i = 0; i < c; ++i)
118 memcpy(m_data.data(), _bs, N);
129 explicit operator bool()
const
131 return std::any_of(m_data.begin(), m_data.end(), [](uint8_t _b) { return _b != 0; });
139 for (
unsigned i = 0; i < N; ++i)
140 if (m_data[i] < _c.m_data[i])
142 else if (m_data[i] > _c.m_data[i])
153 for (
unsigned i = 0; i < N; ++i)
154 m_data[i] ^= _c.m_data[i];
160 for (
unsigned i = 0; i < N; ++i)
161 m_data[i] |= _c.m_data[i];
167 for (
unsigned i = 0; i < N; ++i)
168 m_data[i] &= _c.m_data[i];
175 for (
unsigned i = 0; i < N; ++i)
183 for (
unsigned i =
size; i > 0 && !++m_data[--i];) {
202 return toHex(
ref().cropped(0, 4)) +
"\342\200\246" +
toHex(
ref().cropped(N - 4));
215 uint8_t*
data() {
return m_data.data(); }
218 uint8_t
const*
data()
const {
return m_data.data(); }
221 auto begin() const -> typename
std::array<uint8_t, N>::const_iterator {
return m_data.begin(); }
224 auto end() const -> typename
std::array<uint8_t, N>::const_iterator {
return m_data.end(); }
230 std::array<uint8_t, N>&
asArray() {
return m_data; }
233 std::array<uint8_t, N>
const&
asArray()
const {
return m_data; }
236 template<
class Engine>
239 for (
auto& i : m_data)
240 i = (uint8_t) std::uniform_int_distribution<uint16_t>(0, 255)(_eng);
247 std::random_device rd;
252 template<
unsigned P,
unsigned M>
255 return (*
this |= _h.template bloomPart<P, N>());
258 template<
unsigned P,
unsigned M>
261 return contains(_h.template bloomPart<P, N>());
264 template<
unsigned P,
unsigned M>
267 unsigned const c_bloomBits = M * 8;
268 unsigned const c_mask = c_bloomBits - 1;
271 static_assert((M & (M - 1)) == 0,
"M must be power-of-two");
272 static_assert(
P * c_bloomBytes <= N,
"out of range");
275 uint8_t
const* p =
data();
276 for (
unsigned i = 0; i <
P; ++i) {
278 for (
unsigned j = 0; j < c_bloomBytes; ++j, ++p)
279 index = (index << 8) | *p;
281 ret[M - 1 - index / 8] |= (1 << (index % 8));
290 for (
auto d : m_data)
292 for (;; ++ret, d <<= 1)
305 std::array<uint8_t, N> m_data;
370 return static_cast<FixedHash<T> const&
>(*this).operator==(
375 return static_cast<FixedHash<T> const&
>(*this).operator!=(
380 return static_cast<FixedHash<T> const&
>(*this).operator<(
385 return static_cast<FixedHash<T> const&
>(*this).operator>=(
390 return static_cast<FixedHash<T> const&
>(*this).operator<=(
395 return static_cast<FixedHash<T> const&
>(*this).operator>(
455 auto r = ~static_cast<FixedHash<T>
const&>(*this);
468 std::random_device rd;
482 const uint64_t* hash1 = (
const uint64_t*) data();
483 const uint64_t* hash2 = (
const uint64_t*) _other.
data();
484 return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2])
485 && (hash1[3] == hash2[3]);
510 memcpy(ret.
data(), _t.
data() + 12, 20);
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
ConstructFromHashType
Method to convert from a string.
bool operator<(FixedHash const &_c) const
void randomize(Engine &_eng)
Populate with random data.
bool contains(FixedHash const &_c) const
FixedHash(bytesConstRef _b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
bool containsBloom(FixedHash< M > const &_h)
unsigned firstBitSet() const
Returns the index of the first bit set to one, or size() * 8 if no bits are set.
FixedHash(unsigned _u)
Convert from unsigned.
bytesConstRef ref() const
FixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=AlignLeft)
Construct from another hash, filling with zeroes or cropping as necessary.
std::array< uint8_t, N > const & asArray() const
std::array< uint8_t, N > & asArray()
bool operator==(FixedHash const &_c) const
ConstructFromPointerType
A dummy flag to avoid accidental construction from pointer.
auto end() const -> typename std::array< uint8_t, N >::const_iterator
ConstructFromStringType
Method to convert from a string.
FixedHash(std::string const &_s, ConstructFromStringType _t=FromHex, ConstructFromHashType _ht=FailIfDifferent)
Explicitly construct, copying from a string.
FixedHash()
Construct an empty hash.
FixedHash(bytes const &_b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
FixedHash & shiftBloom(FixedHash< M > const &_h)
FixedHash< M > bloomPart() const
FixedHash(uint8_t const *_bs, ConstructFromPointerType)
Explicitly construct, copying from a bytes in memory with given pointer.
bool operator>=(FixedHash const &_c) const
FixedHash & operator^=(FixedHash const &_c)
FixedHash & operator&=(FixedHash const &_c)
uint8_t operator[](unsigned _i) const
uint8_t const * data() const
FixedHash operator~() const
auto begin() const -> typename std::array< uint8_t, N >::const_iterator
uint8_t & operator[](unsigned _i)
FixedHash operator^(FixedHash const &_c) const
FixedHash operator|(FixedHash const &_c) const
FixedHash & operator|=(FixedHash const &_c)
std::string abridged() const
bool operator!=(FixedHash const &_c) const
std::string abridgedMiddle() const
bool operator>(FixedHash const &_c) const
static FixedHash random()
FixedHash operator&(FixedHash const &_c) const
bool operator<=(FixedHash const &_c) const
SecureFixedHash operator&(SecureFixedHash const &_c) const
SecureFixedHash operator^(SecureFixedHash const &_c) const
SecureFixedHash & operator&=(SecureFixedHash const &_c)
FixedHash< T > & writable()
SecureFixedHash(SecureFixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)
static SecureFixedHash< T > random()
bool operator>=(SecureFixedHash const &_c) const
SecureFixedHash(SecureFixedHash< T > const &_h)
bool operator>(SecureFixedHash const &_c) const
SecureFixedHash(bytes const *_d, ConstructFromPointerType _t)
bool operator<=(SecureFixedHash const &_c) const
SecureFixedHash & operator^=(SecureFixedHash const &_c)
SecureFixedHash(bytesConstRef _b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
SecureFixedHash operator|(SecureFixedHash const &_c) const
SecureFixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)
SecureFixedHash operator~() const
SecureFixedHash operator&(FixedHash< T > const &_c) const
typename FixedHash< T >::ConstructFromStringType ConstructFromStringType
bool operator==(SecureFixedHash const &_c) const
SecureFixedHash & operator^=(FixedHash< T > const &_c)
SecureFixedHash()=default
SecureFixedHash< T > & operator=(SecureFixedHash< T > const &_c)
bool operator<(SecureFixedHash const &_c) const
SecureFixedHash & operator&=(FixedHash< T > const &_c)
SecureFixedHash & operator|=(SecureFixedHash const &_c)
SecureFixedHash(bytes const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
typename FixedHash< T >::ConstructFromHashType ConstructFromHashType
typename FixedHash< T >::ConstructFromPointerType ConstructFromPointerType
bytesConstRef ref() const
FixedHash< T > const & makeInsecure() const
SecureFixedHash(std::string const &_s, ConstructFromStringType _t=FixedHash< T >::FromHex, ConstructFromHashType _ht=FixedHash< T >::FailIfDifferent)
SecureFixedHash & operator|=(FixedHash< T > const &_c)
SecureFixedHash operator^(FixedHash< T > const &_c) const
SecureFixedHash operator|(FixedHash< T > const &_c) const
bool operator!=(SecureFixedHash const &_c) const
uint8_t const * data() const
A modifiable reference to an existing object or vector in memory.
void cleanse()
Securely overwrite the memory.
std::vector< h256 > h256s
std::vector< h160 > h160s
vector_ref< uint8_t const > bytesConstRef
std::vector< h512 > h512s
vector_ref< uint8_t > bytesRef
std::vector< uint8_t > bytes
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
std::unordered_set< h160 > h160Hash
std::unordered_set< h256 > h256Hash
std::string toHex(Iterator _it, Iterator _end, std::string _prefix)
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
Compile-time calculation of Log2 of constant values.