 |
Ethereum
PoC-8
The C++ Implementation of Ethereum
|
Go to the documentation of this file.
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; }
138 byte*
data() {
return m_data.data(); }
141 byte const*
data()
const {
return m_data.data(); }
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");
195 byte const* p =
data();
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>
314 const uint64_t* hash1 = (
const uint64_t*)data();
315 const uint64_t* hash2 = (
const uint64_t*)_other.
data();
316 return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2]) && (hash1[3] == hash2[3]);
322 uint64_t
const* data =
reinterpret_cast<uint64_t const*
>(value.
data());
323 return boost::hash_range(data, data + 4);
327 template <
unsigned N>
334 template <
unsigned N>
344 template <
unsigned N>
347 _out <<
"SecureFixedHash#" << std::hex << typename FixedHash<N>::hash()(_h.
makeInsecure()) << std::dec;
372 memcpy(ret.
data(), _t.
data() + 12, 20);
382 std::ostringstream out;
384 for (
h256 const& i: _bs)
385 out << i.abridged() <<
", ";
397 template<>
struct hash<
dev::
h160>: dev::h160::hash {};
398 template<>
struct hash<
dev::
h256>: dev::h256::hash {};
399 template<>
struct hash<
dev::
h512>: dev::h512::hash {};
SecureFixedHash(bytes const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
FixedHash & shiftBloom(FixedHash< M > const &_h)
SecureFixedHash(std::string const &_s, ConstructFromStringType _t=FixedHash< T >::FromHex, ConstructFromHashType _ht=FixedHash< T >::FailIfDifferent)
bool operator>=(SecureFixedHash const &_c) const
SecureFixedHash(bytesSec const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
bool operator==(SecureFixedHash const &_c) const
void toBigEndian(T _val, Out &o_out)
FixedHash operator~() const
size_t operator()(FixedHash const &_value) const
Make a hash of the object's data.
byte const * data() const
SecureFixedHash & operator^=(FixedHash< T > const &_c)
FixedHash(bytes const &_b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
FixedHash operator|(FixedHash const &_c) const
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.
SecureFixedHash & operator|=(FixedHash< T > const &_c)
bool operator>(SecureFixedHash const &_c) const
std::string toString(h256s const &_bs)
FixedHash()
Construct an empty hash.
bool operator<(SecureFixedHash const &_c) const
ConstructFromPointerType
A dummy flag to avoid accidental construction from pointer.
std::unordered_set< h160 > h160Hash
bytesConstRef ref() const
void randomize(Engine &_eng)
Populate with random data.
FixedHash operator&(FixedHash const &_c) const
h128 fromUUID(std::string const &_uuid)
SecureFixedHash & operator^=(SecureFixedHash const &_c)
bool operator>(FixedHash const &_c) const
bool contains(FixedHash const &_c) const
std::istream & operator>>(std::istream &_in, FixedHash< N > &o_h)
FixedHash(unsigned _u)
Convert from unsigned.
auto end() const -> typename std::array< byte, N >::const_iterator
bool operator!=(FixedHash const &_c) const
byte operator[](unsigned _i) const
bool operator<=(SecureFixedHash const &_c) const
FixedHash(std::string const &_s, ConstructFromStringType _t=FromHex, ConstructFromHashType _ht=FailIfDifferent)
Explicitly construct, copying from a string.
std::array< byte, N > const & asArray() const
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
bytesSec asBytesSec() const
SecureFixedHash(SecureFixedHash< T > const &_c)
SecureFixedHash & operator|=(SecureFixedHash const &_c)
std::string abridgedMiddle() const
byte const * data() const
FixedHash< T > const & makeInsecure() const
bool containsBloom(FixedHash< M > const &_h)
auto begin() const -> typename std::array< byte, N >::const_iterator
std::unordered_set< h256 > h256Hash
std::vector< h256 > h256s
SecureFixedHash operator|(SecureFixedHash const &_c) const
FixedHash(Arith const &_arith)
Convert from the corresponding arithmetic type.
FixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=AlignLeft)
Construct from another hash, filling with zeroes or cropping as necessary.
SecureFixedHash operator&(FixedHash< T > const &_c) const
FixedHash & operator&=(FixedHash const &_c)
std::vector< byte > bytes
bool operator!=(SecureFixedHash const &_c) const
std::string abridged() const
vector_ref< byte const > bytesConstRef
std::array< byte, N > & asArray()
SecureFixedHash operator|(FixedHash< T > const &_c) const
static FixedHash random()
bool operator==(FixedHash const &_c) const
SecureFixedHash & operator&=(SecureFixedHash const &_c)
std::string toUUID(h128 const &_uuid)
secure_vector< byte > bytesSec
SecureFixedHash & operator&=(FixedHash< T > const &_c)
vector_ref< byte > bytesRef
FixedHash operator^(FixedHash const &_c) const
bool operator<(FixedHash const &_c) const
SecureFixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)
SecureFixedHash< T > & operator=(SecureFixedHash< T > const &_c)
FixedHash(byte const *_bs, ConstructFromPointerType)
Explicitly construct, copying from a bytes in memory with given pointer.
static SecureFixedHash< T > random()
SecureFixedHash operator~() const
SecureFixedHash()=default
std::random_device s_fixedHashEngine
std::ostream & operator<<(std::ostream &_out, SecureFixedHash< N > const &_h)
Stream I/O for the SecureFixedHash class.
bool operator>=(FixedHash const &_c) const
FixedHash(bytesConstRef _b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
ConstructFromStringType
Method to convert from a string.
ConstructFromHashType
Method to convert from a string.
SecureFixedHash(bytesConstRef _b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
FixedHash & operator|=(FixedHash const &_c)
FixedHash & operator^=(FixedHash const &_c)
Compile-time calculation of Log2 of constant values.
unsigned firstBitSet() const
Returns the index of the first bit set to one, or size() * 8 if no bits are set.
SecureFixedHash(SecureFixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)
SecureFixedHash(byte const *_d, ConstructFromPointerType _t)
SecureFixedHash operator^(FixedHash< T > const &_c) const
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
FixedHash< M > bloomPart() const
bool operator<=(FixedHash const &_c) const
std::vector< h512 > h512s
SecureFixedHash operator&(SecureFixedHash const &_c) const
SecureFixedHash operator^(SecureFixedHash const &_c) const
bytesConstRef ref() const
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
byte & operator[](unsigned _i)
std::vector< h160 > h160s
FixedHash< T > & writable()