From 1bb40a42c216628de4698b49fb7b3de2e350f27f Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 21 Jun 2016 15:12:06 +1200 Subject: [PATCH 1/2] Collect all permutations of final solutions --- src/crypto/equihash.cpp | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/crypto/equihash.cpp b/src/crypto/equihash.cpp index 4165693d3..e8a8fcb22 100644 --- a/src/crypto/equihash.cpp +++ b/src/crypto/equihash.cpp @@ -268,11 +268,24 @@ std::set> Equihash::BasicSolve(const eh_HashState& ba LogPrint("pow", "- Sorting list\n"); std::sort(X.begin(), X.end(), CompareSR(hashLen)); LogPrint("pow", "- Finding collisions\n"); - for (int i = 0; i < X.size() - 1; i++) { - FullStepRow res(X[i], X[i+1], hashLen, lenIndices, 0); - if (res.IsZero(hashLen) && DistinctIndices(X[i], X[i+1], hashLen, lenIndices)) { - solns.insert(res.GetIndices(hashLen, 2*lenIndices)); + int i = 0; + while (i < X.size() - 1) { + int j = 1; + while (i+j < X.size() && + HasCollision(X[i], X[i+j], hashLen)) { + j++; } + + for (int l = 0; l < j - 1; l++) { + for (int m = l + 1; m < j; m++) { + FullStepRow res(X[i+l], X[i+m], hashLen, lenIndices, 0); + if (DistinctIndices(X[i+l], X[i+m], hashLen, lenIndices)) { + solns.insert(res.GetIndices(hashLen, 2*lenIndices)); + } + } + } + + i += j; } } else LogPrint("pow", "- List is empty\n"); @@ -416,11 +429,22 @@ std::set> Equihash::OptimisedSolve(const eh_HashState LogPrint("pow", "- Sorting list\n"); std::sort(Xt.begin(), Xt.end(), CompareSR(hashLen)); LogPrint("pow", "- Finding collisions\n"); - for (int i = 0; i < Xt.size() - 1; i++) { - TruncatedStepRow res(Xt[i], Xt[i+1], hashLen, lenIndices, 0); - if (res.IsZero(hashLen)) { - partialSolns.push_back(res.GetTruncatedIndices(hashLen, 2*lenIndices)); + int i = 0; + while (i < Xt.size() - 1) { + int j = 1; + while (i+j < Xt.size() && + HasCollision(Xt[i], Xt[i+j], hashLen)) { + j++; } + + for (int l = 0; l < j - 1; l++) { + for (int m = l + 1; m < j; m++) { + TruncatedStepRow res(Xt[i+l], Xt[i+m], hashLen, lenIndices, 0); + partialSolns.push_back(res.GetTruncatedIndices(hashLen, 2*lenIndices)); + } + } + + i += j; } } else LogPrint("pow", "- List is empty\n"); From c6365f4d80f72b3be985e3ebf747757541af9ba0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 19 Jul 2016 23:33:28 +1200 Subject: [PATCH 2/2] Add test case that requires the fix --- src/test/equihash_tests.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/equihash_tests.cpp b/src/test/equihash_tests.cpp index f6f5112ab..dbe757a7a 100644 --- a/src/test/equihash_tests.cpp +++ b/src/test/equihash_tests.cpp @@ -119,6 +119,14 @@ BOOST_AUTO_TEST_CASE(solver_testvectors) { {637, 78032, 97478, 118268, 16058, 44395, 19029, 39150, 1566, 66582, 4084, 107252, 59619, 116281, 67957, 128728, 30916, 69051, 90422, 102716, 51905, 66753, 60509, 78066, 38568, 119630, 75839, 113134, 54356, 70996, 63085, 83048}, {4130, 71826, 46248, 50447, 4281, 129092, 23122, 103196, 9305, 34797, 111094, 127775, 82662, 120386, 109738, 124765, 24770, 125174, 83477, 102473, 45209, 79062, 84764, 125929, 31689, 95554, 66614, 127658, 31756, 55684, 53670, 53776} }); + + TestEquihashSolvers(96, 5, "Test case with 3+-way collision in the final round.", 0x2af3, { + {1093, 45982, 37399, 89200, 29813, 113936, 40951, 64027, 4370, 122807, 82736, 119086, 21530, 60168, 65163, 68120, 2310, 94346, 22675, 61162, 19193, 54507, 38352, 94322, 5341, 65047, 48071, 120787, 32561, 129305, 62593, 93497}, + {6411, 91796, 58827, 93129, 6705, 74430, 91891, 124391, 32765, 128544, 51395, 74269, 55980, 89012, 58573, 75543, 7902, 22572, 27944, 66370, 61755, 76417, 68687, 90417, 31576, 105479, 117582, 130960, 52751, 53260, 71636, 118134}, + {6411, 91796, 58827, 93129, 6705, 74430, 91891, 124391, 32765, 128544, 51395, 74269, 55980, 89012, 58573, 75543, 7902, 22572, 61755, 76417, 27944, 66370, 68687, 90417, 31576, 105479, 117582, 130960, 52751, 53260, 71636, 118134}, + {6411, 91796, 58827, 93129, 6705, 74430, 91891, 124391, 32765, 128544, 51395, 74269, 55980, 89012, 58573, 75543, 7902, 22572, 68687, 90417, 27944, 66370, 61755, 76417, 31576, 105479, 117582, 130960, 52751, 53260, 71636, 118134}, + {11541, 106147, 29075, 118360, 53060, 62499, 109856, 118745, 14581, 56663, 114595, 121986, 35542, 72577, 88428, 116033, 11996, 55170, 16966, 94150, 16116, 81559, 34889, 112430, 37304, 104803, 92442, 119354, 46613, 72741, 50259, 89047} + }); } BOOST_AUTO_TEST_CASE(validator_testvectors) {