Partial #19326: Simplify hash.h interface using Spans

This commit is contained in:
Kittywhiskers Van Gogh 2021-05-19 21:09:18 +05:30
parent 01272b159d
commit 56f1b2d01c
13 changed files with 78 additions and 61 deletions

View File

@ -94,7 +94,7 @@ static void HASH(benchmark::State& state, size_t buffersize)
uint8_t hash[CHash256::OUTPUT_SIZE]; uint8_t hash[CHash256::OUTPUT_SIZE];
std::vector<uint8_t> in(buffersize,0); std::vector<uint8_t> in(buffersize,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(hash); CHash256().Write(in).Finalize(hash);
} }
static void HASH_64BYTES(benchmark::State& state) static void HASH_64BYTES(benchmark::State& state)

View File

@ -58,14 +58,14 @@ static void HASH_DSHA256(benchmark::State& state)
uint8_t hash[CSHA256::OUTPUT_SIZE]; uint8_t hash[CSHA256::OUTPUT_SIZE];
std::vector<uint8_t> in(BUFFER_SIZE,0); std::vector<uint8_t> in(BUFFER_SIZE,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(hash); CHash256().Write(in).Finalize(hash);
} }
static void HASH_DSHA256_0032b(benchmark::State& state) static void HASH_DSHA256_0032b(benchmark::State& state)
{ {
std::vector<uint8_t> in(32,0); std::vector<uint8_t> in(32,0);
while (state.KeepRunning()) { while (state.KeepRunning()) {
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
} }
@ -116,42 +116,42 @@ static void HASH_DSHA256_0032b_single(benchmark::State& state)
{ {
std::vector<uint8_t> in(32,0); std::vector<uint8_t> in(32,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
static void HASH_DSHA256_0080b_single(benchmark::State& state) static void HASH_DSHA256_0080b_single(benchmark::State& state)
{ {
std::vector<uint8_t> in(80,0); std::vector<uint8_t> in(80,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
static void HASH_DSHA256_0128b_single(benchmark::State& state) static void HASH_DSHA256_0128b_single(benchmark::State& state)
{ {
std::vector<uint8_t> in(128,0); std::vector<uint8_t> in(128,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
static void HASH_DSHA256_0512b_single(benchmark::State& state) static void HASH_DSHA256_0512b_single(benchmark::State& state)
{ {
std::vector<uint8_t> in(512,0); std::vector<uint8_t> in(512,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
static void HASH_DSHA256_1024b_single(benchmark::State& state) static void HASH_DSHA256_1024b_single(benchmark::State& state)
{ {
std::vector<uint8_t> in(1024,0); std::vector<uint8_t> in(1024,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
static void HASH_DSHA256_2048b_single(benchmark::State& state) static void HASH_DSHA256_2048b_single(benchmark::State& state)
{ {
std::vector<uint8_t> in(2048,0); std::vector<uint8_t> in(2048,0);
while (state.KeepRunning()) while (state.KeepRunning())
CHash256().Write(in.data(), in.size()).Finalize(in.data()); CHash256().Write(in).Finalize(in);
} }
static void HASH_X11(benchmark::State& state) static void HASH_X11(benchmark::State& state)

View File

@ -244,7 +244,7 @@ uint256 BlockFilter::GetHash() const
const std::vector<unsigned char>& data = GetEncodedFilter(); const std::vector<unsigned char>& data = GetEncodedFilter();
uint256 result; uint256 result;
CHash256().Write(data.data(), data.size()).Finalize(result.begin()); CHash256().Write(data).Finalize(result);
return result; return result;
} }
@ -254,8 +254,8 @@ uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
uint256 result; uint256 result;
CHash256() CHash256()
.Write(filter_hash.begin(), filter_hash.size()) .Write(filter_hash)
.Write(prev_header.begin(), prev_header.size()) .Write(prev_header)
.Finalize(result.begin()); .Finalize(result);
return result; return result;
} }

View File

@ -12,7 +12,7 @@ inline uint32_t ROTL32(uint32_t x, int8_t r)
return (x << r) | (x >> (32 - 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 // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
uint32_t h1 = nHashSeed; uint32_t h1 = nHashSeed;

View File

@ -38,14 +38,15 @@ private:
public: public:
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
void Finalize(unsigned char hash[OUTPUT_SIZE]) { void Finalize(Span<unsigned char> output) {
assert(output.size() == OUTPUT_SIZE);
unsigned char buf[CSHA256::OUTPUT_SIZE]; unsigned char buf[CSHA256::OUTPUT_SIZE];
sha.Finalize(buf); sha.Finalize(buf);
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash); sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
} }
CHash256& Write(const unsigned char *data, size_t len) { CHash256& Write(Span<const unsigned char> input) {
sha.Write(data, len); sha.Write(input.data(), input.size());
return *this; return *this;
} }
@ -62,14 +63,15 @@ private:
public: public:
static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
void Finalize(unsigned char hash[OUTPUT_SIZE]) { void Finalize(Span<unsigned char> output) {
assert(output.size() == OUTPUT_SIZE);
unsigned char buf[CSHA256::OUTPUT_SIZE]; unsigned char buf[CSHA256::OUTPUT_SIZE];
sha.Finalize(buf); sha.Finalize(buf);
CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash); CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
} }
CHash160& Write(const unsigned char *data, size_t len) { CHash160& Write(Span<const unsigned char> input) {
sha.Write(data, len); sha.Write(input.data(), input.size());
return *this; return *this;
} }
@ -85,8 +87,8 @@ inline uint256 Hash(const T1 pbegin, const T1 pend)
{ {
static const unsigned char pblank[1] = {}; static const unsigned char pblank[1] = {};
uint256 result; uint256 result;
CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])) CHash256().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
.Finalize((unsigned char*)&result); .Finalize(result);
return result; return result;
} }
@ -96,9 +98,9 @@ inline uint256 Hash(const T1 p1begin, const T1 p1end,
const T2 p2begin, const T2 p2end) { const T2 p2begin, const T2 p2end) {
static const unsigned char pblank[1] = {}; static const unsigned char pblank[1] = {};
uint256 result; uint256 result;
CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])) CHash256().Write({p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])})
.Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])) .Write({p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])})
.Finalize((unsigned char*)&result); .Finalize(result);
return result; return result;
} }
@ -162,8 +164,8 @@ inline uint160 Hash160(const T1 pbegin, const T1 pend)
{ {
static unsigned char pblank[1] = {}; static unsigned char pblank[1] = {};
uint160 result; uint160 result;
CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])) CHash160().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
.Finalize((unsigned char*)&result); .Finalize(result);
return result; return result;
} }
@ -196,13 +198,13 @@ public:
int GetVersion() const { return nVersion; } int GetVersion() const { return nVersion; }
void write(const char *pch, size_t size) { void write(const char *pch, size_t size) {
ctx.Write((const unsigned char*)pch, size); ctx.Write({(const unsigned char*)pch, size});
} }
// invalidates the object // invalidates the object
uint256 GetHash() { uint256 GetHash() {
uint256 result; uint256 result;
ctx.Finalize((unsigned char*)&result); ctx.Finalize(result);
return result; return result;
} }
@ -258,7 +260,7 @@ uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL
return ss.GetHash(); 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]); void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);

View File

@ -212,7 +212,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
std::string str = "Bitcoin key verification\n"; std::string str = "Bitcoin key verification\n";
GetRandBytes(rnd, sizeof(rnd)); GetRandBytes(rnd, sizeof(rnd));
uint256 hash; uint256 hash;
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin()); CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
std::vector<unsigned char> vchSig; std::vector<unsigned char> vchSig;
Sign(hash, vchSig); Sign(hash, vchSig);
return pubkey.Verify(hash, vchSig); return pubkey.Verify(hash, vchSig);

View File

@ -948,7 +948,7 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes)
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024)); vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
} }
hasher.Write((const unsigned char*)pch, nCopy); hasher.Write({(const unsigned char*)pch, nCopy});
memcpy(&vRecv[nDataPos], pch, nCopy); memcpy(&vRecv[nDataPos], pch, nCopy);
nDataPos += nCopy; nDataPos += nCopy;
@ -959,7 +959,7 @@ const uint256& CNetMessage::GetMessageHash() const
{ {
assert(complete()); assert(complete());
if (data_hash.IsNull()) if (data_hash.IsNull())
hasher.Finalize(data_hash.begin()); hasher.Finalize(data_hash);
return data_hash; return data_hash;
} }

View File

@ -934,9 +934,9 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
else if (opcode == OP_SHA256) else if (opcode == OP_SHA256)
CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data()); CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
else if (opcode == OP_HASH160) else if (opcode == OP_HASH160)
CHash160().Write(vch.data(), vch.size()).Finalize(vchHash.data()); CHash160().Write(vch).Finalize(vchHash);
else if (opcode == OP_HASH256) else if (opcode == OP_HASH256)
CHash256().Write(vch.data(), vch.size()).Finalize(vchHash.data()); CHash256().Write(vch).Finalize(vchHash);
popstack(stack); popstack(stack);
stack.push_back(vchHash); stack.push_back(vchHash);
} }

View File

@ -179,4 +179,16 @@ T& SpanPopBack(Span<T>& span)
return back; 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 #endif

View File

@ -817,7 +817,7 @@ BOOST_AUTO_TEST_CASE(sha256d64)
in[j] = InsecureRandBits(8); in[j] = InsecureRandBits(8);
} }
for (int j = 0; j < i; ++j) { for (int j = 0; j < i; ++j) {
CHash256().Write(in + 64 * j, 64).Finalize(out1 + 32 * j); CHash256().Write({in + 64 * j, 64}).Finalize({out1 + 32 * j, 32});
} }
SHA256D64(out2, in, i); SHA256D64(out2, in, i);
BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0); BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0);

View File

@ -60,7 +60,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
} }
} }
mutated |= (inner[level] == h); mutated |= (inner[level] == h);
CHash256().Write(inner[level].begin(), 32).Write(h.begin(), 32).Finalize(h.begin()); CHash256().Write(inner[level]).Write(h).Finalize(h);
} }
// Store the resulting hash at inner position level. // Store the resulting hash at inner position level.
inner[level] = h; inner[level] = h;
@ -86,7 +86,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
if (pbranch && matchh) { if (pbranch && matchh) {
pbranch->push_back(h); pbranch->push_back(h);
} }
CHash256().Write(h.begin(), 32).Write(h.begin(), 32).Finalize(h.begin()); CHash256().Write(h).Write(h).Finalize(h);
// Increment count to the value it would have if two entries at this // Increment count to the value it would have if two entries at this
// level had existed. // level had existed.
count += (((uint32_t)1) << level); count += (((uint32_t)1) << level);
@ -101,7 +101,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
matchh = true; matchh = true;
} }
} }
CHash256().Write(inner[level].begin(), 32).Write(h.begin(), 32).Finalize(h.begin()); CHash256().Write(inner[level]).Write(h).Finalize(h);
level++; level++;
} }
} }

View File

@ -13,8 +13,8 @@
template <unsigned int BITS> template <unsigned int BITS>
base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch) base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
{ {
assert(vch.size() == sizeof(data)); assert(vch.size() == sizeof(m_data));
memcpy(data, vch.data(), sizeof(data)); memcpy(m_data, vch.data(), sizeof(m_data));
} }
template <unsigned int BITS> template <unsigned int BITS>
@ -22,7 +22,7 @@ std::string base_blob<BITS>::GetHex() const
{ {
uint8_t m_data_rev[WIDTH]; uint8_t m_data_rev[WIDTH];
for (int i = 0; i < WIDTH; ++i) { 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); return HexStr(m_data_rev);
} }
@ -30,7 +30,7 @@ std::string base_blob<BITS>::GetHex() const
template <unsigned int BITS> template <unsigned int BITS>
void base_blob<BITS>::SetHex(const char* psz) void base_blob<BITS>::SetHex(const char* psz)
{ {
memset(data, 0, sizeof(data)); memset(m_data, 0, sizeof(m_data));
// skip leading spaces // skip leading spaces
while (isspace(*psz)) while (isspace(*psz))
@ -45,7 +45,7 @@ void base_blob<BITS>::SetHex(const char* psz)
while (::HexDigit(*psz) != -1) while (::HexDigit(*psz) != -1)
psz++; psz++;
psz--; psz--;
unsigned char* p1 = (unsigned char*)data; unsigned char* p1 = (unsigned char*)m_data;
unsigned char* pend = p1 + WIDTH; unsigned char* pend = p1 + WIDTH;
while (psz >= pbegin && p1 < pend) { while (psz >= pbegin && p1 < pend) {
*p1 = ::HexDigit(*psz--); *p1 = ::HexDigit(*psz--);

View File

@ -21,11 +21,11 @@ class base_blob
{ {
protected: protected:
static constexpr int WIDTH = BITS / 8; static constexpr int WIDTH = BITS / 8;
uint8_t data[WIDTH]; uint8_t m_data[WIDTH];
public: public:
base_blob() base_blob()
{ {
memset(data, 0, sizeof(data)); memset(m_data, 0, sizeof(m_data));
} }
explicit base_blob(const std::vector<unsigned char>& vch); explicit base_blob(const std::vector<unsigned char>& vch);
@ -33,17 +33,17 @@ public:
bool IsNull() const bool IsNull() const
{ {
for (int i = 0; i < WIDTH; i++) for (int i = 0; i < WIDTH; i++)
if (data[i] != 0) if (m_data[i] != 0)
return false; return false;
return true; return true;
} }
void SetNull() 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; }
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; }
@ -54,34 +54,37 @@ public:
void SetHex(const std::string& str); void SetHex(const std::string& str);
std::string ToString() const; std::string ToString() const;
const unsigned char* data() const { return m_data; }
unsigned char* data() { return m_data; }
unsigned char* begin() unsigned char* begin()
{ {
return &data[0]; return &m_data[0];
} }
unsigned char* end() unsigned char* end()
{ {
return &data[WIDTH]; return &m_data[WIDTH];
} }
const unsigned char* begin() const const unsigned char* begin() const
{ {
return &data[0]; return &m_data[0];
} }
const unsigned char* end() const const unsigned char* end() const
{ {
return &data[WIDTH]; return &m_data[WIDTH];
} }
unsigned int size() const unsigned int size() const
{ {
return sizeof(data); return sizeof(m_data);
} }
uint64_t GetUint64(int pos) const 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]) | \ return ((uint64_t)ptr[0]) | \
((uint64_t)ptr[1]) << 8 | \ ((uint64_t)ptr[1]) << 8 | \
((uint64_t)ptr[2]) << 16 | \ ((uint64_t)ptr[2]) << 16 | \
@ -95,13 +98,13 @@ public:
template<typename Stream> template<typename Stream>
void Serialize(Stream& s) const void Serialize(Stream& s) const
{ {
s.write((char*)data, sizeof(data)); s.write((char*)m_data, sizeof(m_data));
} }
template<typename Stream> template<typename Stream>
void Unserialize(Stream& s) void Unserialize(Stream& s)
{ {
s.read((char*)data, sizeof(data)); s.read((char*)m_data, sizeof(m_data));
} }
}; };
@ -132,7 +135,7 @@ public:
*/ */
uint64_t GetCheapHash() const uint64_t GetCheapHash() const
{ {
return ReadLE64(data); return ReadLE64(m_data);
} }
}; };
@ -167,7 +170,7 @@ public:
uint256 trim256() const uint256 trim256() const
{ {
uint256 result; uint256 result;
memcpy((void*)&result, (void*)data, 32); memcpy((void*)&result, (void*)m_data, 32);
return result; return result;
} }
}; };