Speed up FullStepRow index comparison by leveraging big-endian byte layout

This commit is contained in:
Jack Grigg 2016-06-09 18:02:29 +12:00
parent 1dd8ce9ba5
commit d07cf62991
2 changed files with 5 additions and 3 deletions

View File

@ -37,6 +37,7 @@ int Equihash<N,K>::InitialiseState(eh_HashState& base_state)
personalization);
}
// Big-endian so that array comparison is equivalent to integer comparison
void EhIndexToArray(const eh_index i, unsigned char* array)
{
assert(sizeof(eh_index) == 4);
@ -46,6 +47,7 @@ void EhIndexToArray(const eh_index i, unsigned char* array)
array[3] = i & 0xFF;
}
// Big-endian so that array comparison is equivalent to integer comparison
eh_index ArrayToEhIndex(const unsigned char* array)
{
assert(sizeof(eh_index) == 4);
@ -103,7 +105,7 @@ FullStepRow<WIDTH>::FullStepRow(const FullStepRow<W>& a, const FullStepRow<W>& b
assert(len-trim+(2*lenIndices) <= WIDTH);
for (int i = trim; i < len; i++)
hash[i-trim] = a.hash[i] ^ b.hash[i];
if (a.IndicesBefore(b, len)) {
if (a.IndicesBefore(b, len, lenIndices)) {
std::copy(a.hash+len, a.hash+len+lenIndices, hash+len-trim);
std::copy(b.hash+len, b.hash+len+lenIndices, hash+len-trim+lenIndices);
} else {
@ -532,7 +534,7 @@ bool Equihash<N,K>::IsValidSolution(const eh_HashState& base_state, std::vector<
LogPrint("pow", "X[i+1] = %s\n", X[i+1].GetHex(hashLen));
return false;
}
if (X[i+1].IndicesBefore(X[i], hashLen)) {
if (X[i+1].IndicesBefore(X[i], hashLen, lenIndices)) {
return false;
LogPrint("pow", "Invalid solution: Index tree incorrectly ordered\n");
}

View File

@ -80,7 +80,7 @@ public:
FullStepRow(const FullStepRow<W>& a, const FullStepRow<W>& b, size_t len, size_t lenIndices, int trim);
FullStepRow& operator=(const FullStepRow<WIDTH>& a);
inline bool IndicesBefore(const FullStepRow<WIDTH>& a, size_t len) const { return ArrayToEhIndex(hash+len) < ArrayToEhIndex(a.hash+len); }
inline bool IndicesBefore(const FullStepRow<WIDTH>& a, size_t len, size_t lenIndices) const { return memcmp(hash+len, a.hash+len, lenIndices) < 0; }
std::vector<eh_index> GetIndices(size_t len, size_t lenIndices) const;
template<size_t W>