Auto merge of #1173 - str4d:1165-equihash-fix-large-k-ballooning, r=bitcartel

Eliminate some of the duplicates caused by truncating indices

Closes #1165
This commit is contained in:
zkbot 2016-08-05 17:35:35 +00:00
commit e5b57a1432
1 changed files with 38 additions and 4 deletions

View File

@ -317,6 +317,28 @@ bool Equihash<N,K>::BasicSolve(const eh_HashState& base_state,
return false;
}
template<size_t MAX_INDICES>
bool IsProbablyDuplicate(std::shared_ptr<eh_trunc> indices, size_t lenIndices)
{
assert(lenIndices <= MAX_INDICES);
bool checked_index[MAX_INDICES] = {false};
bool count_checked = 0;
for (int z = 0; z < lenIndices; z++) {
// Skip over indices we have already paired
if (!checked_index[z]) {
for (int y = z+1; y < lenIndices; y++) {
if (!checked_index[y] && indices.get()[z] == indices.get()[y]) {
// Pair found
checked_index[y] = true;
count_checked += 2;
break;
}
}
}
}
return count_checked == lenIndices;
}
template<size_t WIDTH>
void CollideBranches(std::vector<FullStepRow<WIDTH>>& X, const size_t hlen, const size_t lenIndices, const unsigned int clen, const unsigned int ilen, const eh_trunc lt, const eh_trunc rt)
{
@ -379,7 +401,7 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
// First run the algorithm with truncated indices
eh_index soln_size { 1 << K };
const eh_index soln_size { 1 << K };
std::vector<std::shared_ptr<eh_trunc>> partialSolns;
int invalidCount = 0;
{
@ -417,10 +439,18 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
}
// 2c) Calculate tuples (X_i ^ X_j, (i, j))
bool checking_for_zero = (i == 0 && Xt[0].IsZero(hashLen));
for (int l = 0; l < j - 1; l++) {
for (int m = l + 1; m < j; m++) {
// We truncated, so don't check for distinct indices here
Xc.emplace_back(Xt[i+l], Xt[i+m], hashLen, lenIndices, CollisionByteLength);
TruncatedStepRow<TruncatedWidth> Xi {Xt[i+l], Xt[i+m],
hashLen, lenIndices,
CollisionByteLength};
if (!(Xi.IsZero(hashLen-CollisionByteLength) &&
IsProbablyDuplicate<soln_size>(Xi.GetTruncatedIndices(hashLen-CollisionByteLength, 2*lenIndices),
2*lenIndices))) {
Xc.emplace_back(Xi);
}
}
}
@ -471,8 +501,12 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
for (int l = 0; l < j - 1; l++) {
for (int m = l + 1; m < j; m++) {
TruncatedStepRow<FinalTruncatedWidth> res(Xt[i+l], Xt[i+m], hashLen, lenIndices, 0);
partialSolns.push_back(res.GetTruncatedIndices(hashLen, 2*lenIndices));
TruncatedStepRow<FinalTruncatedWidth> res(Xt[i+l], Xt[i+m],
hashLen, lenIndices, 0);
auto soln = res.GetTruncatedIndices(hashLen, 2*lenIndices);
if (!IsProbablyDuplicate<soln_size>(soln, 2*lenIndices)) {
partialSolns.push_back(soln);
}
}
}