Skip to content

Commit

Permalink
Partial bitcoin#19326: Simplify hash.h interface using Spans
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed May 20, 2021
1 parent 41642ff commit f7e75e3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inline uint32_t ROTL32(uint32_t x, int8_t r)
return (x << r) | (x >> (32 - r));
}

unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash)
unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
{
// The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
uint32_t h1 = nHashSeed;
Expand Down
2 changes: 1 addition & 1 deletion src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL
return ss.GetHash();
}

unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);

void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);

Expand Down
12 changes: 12 additions & 0 deletions src/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,16 @@ T& SpanPopBack(Span<T>& span)
return back;
}

// Helper functions to safely cast to unsigned char pointers.
inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; }
inline unsigned char* UCharCast(unsigned char* c) { return c; }
inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; }
inline const unsigned char* UCharCast(const unsigned char* c) { return c; }

// Helper function to safely convert a Span to a Span<[const] unsigned char>.
template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; }

/** Like MakeSpan, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(MakeSpan(std::forward<V>(v)))) { return UCharSpanCast(MakeSpan(std::forward<V>(v))); }

#endif
10 changes: 5 additions & 5 deletions src/uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
template <unsigned int BITS>
base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
{
assert(vch.size() == sizeof(data));
memcpy(data, vch.data(), sizeof(data));
assert(vch.size() == sizeof(m_data));
memcpy(m_data, vch.data(), sizeof(m_data));
}

template <unsigned int BITS>
std::string base_blob<BITS>::GetHex() const
{
uint8_t m_data_rev[WIDTH];
for (int i = 0; i < WIDTH; ++i) {
m_data_rev[i] = data[WIDTH - 1 - i];
m_data_rev[i] = m_data[WIDTH - 1 - i];
}
return HexStr(m_data_rev);
}

template <unsigned int BITS>
void base_blob<BITS>::SetHex(const char* psz)
{
memset(data, 0, sizeof(data));
memset(m_data, 0, sizeof(m_data));

// skip leading spaces
while (isspace(*psz))
Expand All @@ -45,7 +45,7 @@ void base_blob<BITS>::SetHex(const char* psz)
while (::HexDigit(*psz) != -1)
psz++;
psz--;
unsigned char* p1 = (unsigned char*)data;
unsigned char* p1 = (unsigned char*)m_data;
unsigned char* pend = p1 + WIDTH;
while (psz >= pbegin && p1 < pend) {
*p1 = ::HexDigit(*psz--);
Expand Down
33 changes: 18 additions & 15 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ class base_blob
{
protected:
static constexpr int WIDTH = BITS / 8;
uint8_t data[WIDTH];
uint8_t m_data[WIDTH];
public:
base_blob()
{
memset(data, 0, sizeof(data));
memset(m_data, 0, sizeof(m_data));
}

explicit base_blob(const std::vector<unsigned char>& vch);

bool IsNull() const
{
for (int i = 0; i < WIDTH; i++)
if (data[i] != 0)
if (m_data[i] != 0)
return false;
return true;
}

void SetNull()
{
memset(data, 0, sizeof(data));
memset(m_data, 0, sizeof(m_data));
}

inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(data)); }
inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }

friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
Expand All @@ -54,34 +54,37 @@ class base_blob
void SetHex(const std::string& str);
std::string ToString() const;

const unsigned char* data() const { return m_data; }
unsigned char* data() { return m_data; }

unsigned char* begin()
{
return &data[0];
return &m_data[0];
}

unsigned char* end()
{
return &data[WIDTH];
return &m_data[WIDTH];
}

const unsigned char* begin() const
{
return &data[0];
return &m_data[0];
}

const unsigned char* end() const
{
return &data[WIDTH];
return &m_data[WIDTH];
}

unsigned int size() const
{
return sizeof(data);
return sizeof(m_data);
}

uint64_t GetUint64(int pos) const
{
const uint8_t* ptr = data + pos * 8;
const uint8_t* ptr = m_data + pos * 8;
return ((uint64_t)ptr[0]) | \
((uint64_t)ptr[1]) << 8 | \
((uint64_t)ptr[2]) << 16 | \
Expand All @@ -95,13 +98,13 @@ class base_blob
template<typename Stream>
void Serialize(Stream& s) const
{
s.write((char*)data, sizeof(data));
s.write((char*)m_data, sizeof(m_data));
}

template<typename Stream>
void Unserialize(Stream& s)
{
s.read((char*)data, sizeof(data));
s.read((char*)m_data, sizeof(m_data));
}
};

Expand Down Expand Up @@ -132,7 +135,7 @@ class uint256 : public base_blob<256> {
*/
uint64_t GetCheapHash() const
{
return ReadLE64(data);
return ReadLE64(m_data);
}
};

Expand Down Expand Up @@ -167,7 +170,7 @@ class uint512 : public base_blob<512> {
uint256 trim256() const
{
uint256 result;
memcpy((void*)&result, (void*)data, 32);
memcpy((void*)&result, (void*)m_data, 32);
return result;
}
};
Expand Down

0 comments on commit f7e75e3

Please sign in to comment.