From 079e5405e90c03133dcaf8be3792bcbded30f2db Mon Sep 17 00:00:00 2001 From: "crypro.zoidberg" Date: Wed, 9 Jan 2019 22:29:33 +0300 Subject: [PATCH] condidate to wild keccak2 --- src/crypto/wild_keccak.h | 98 +++++++++++++++------------ tests/performance_tests/keccak_test.h | 50 +++++++------- 2 files changed, 81 insertions(+), 67 deletions(-) diff --git a/src/crypto/wild_keccak.h b/src/crypto/wild_keccak.h index ed105799..284849e4 100644 --- a/src/crypto/wild_keccak.h +++ b/src/crypto/wild_keccak.h @@ -293,30 +293,30 @@ namespace crypto return h; } //------------------------------------------------------------------ - template - bool get_wild_keccak2_over_accessor(const std::string& bd, crypto::hash& res, uint64_t height, callback_t accessor) - { - crypto::wild_keccak2_dbl(reinterpret_cast(bd.data()), bd.size(), reinterpret_cast(&res), sizeof(res), [&](crypto::state_t_m& st, crypto::mixin_t& mix) - { - if (!height) - { - memset(&mix, 0, sizeof(mix)); - return; - } - -#define GET_M(index) accessor(mix[index]) - - for (size_t i = 0; i != 6; i++) - { - *(crypto::hash*)&mix[i * 4] = XOR_4(GET_H(i * 4), GET_H(i * 4 + 1), GET_H(i * 4 + 2), GET_H(i * 4 + 3)); - } - for (size_t i = 0; i != 6; i++) - { - *(crypto::hash*)&mix[(5-i) * 4] = XOR_4(GET_M(i * 4), GET_M(i * 4 + 1), GET_M(i * 4 + 2), GET_M(i * 4 + 3)); - } - }); - return true; - } +// template +// bool get_wild_keccak2_over_accessor(const std::string& bd, crypto::hash& res, uint64_t height, callback_t accessor) +// { +// crypto::wild_keccak2_dbl(reinterpret_cast(bd.data()), bd.size(), reinterpret_cast(&res), sizeof(res), [&](crypto::state_t_m& st, crypto::mixin_t& mix) +// { +// if (!height) +// { +// memset(&mix, 0, sizeof(mix)); +// return; +// } +// +// #define GET_M(index) accessor(mix[index]) +// +// for (size_t i = 0; i != 6; i++) +// { +// *(crypto::hash*)&mix[i * 4] = XOR_4(GET_H(i * 4), GET_H(i * 4 + 1), GET_H(i * 4 + 2), GET_H(i * 4 + 3)); +// } +// for (size_t i = 0; i != 6; i++) +// { +// //*(crypto::hash*)&mix[(5-i) * 4] = XOR_4(GET_M(i * 4), GET_M(i * 4 + 1), GET_M(i * 4 + 2), GET_M(i * 4 + 3)); +// } +// }); +// return true; +// } //------------------------------------------------------------------ inline @@ -333,14 +333,28 @@ namespace crypto #define OPT_GET_H(index) scratchpad[st[index]%sz] #define OPT_GET_M(index) scratchpad[mix[index]%sz] - for (size_t i = 0; i != 6; i++) - { - OPT_XOR_4_RES(OPT_GET_H(i * 4), OPT_GET_H(i * 4 + 1), OPT_GET_H(i * 4 + 2), OPT_GET_H(i * 4 + 3), (*(crypto::hash*)&mix[i * 4])); - } - for (size_t i = 0; i != 6; i++) - { - OPT_XOR_4_RES(OPT_GET_M(i * 4), OPT_GET_M(i * 4 + 1), OPT_GET_M(i * 4 + 2), OPT_GET_M(i * 4 + 3), (*(crypto::hash*)&mix[ (5-i) * 4])); - } + const uint64_t* int_array_ptr = (const uint64_t*)&scratchpad[0]; + size_t int64_sz = sz * 4; + + // for (size_t i = 0; i != 6; i++) + // { + // OPT_XOR_4_RES(OPT_GET_H(i * 4), OPT_GET_H(i * 4 + 1), OPT_GET_H(i * 4 + 2), OPT_GET_H(i * 4 + 3), (*(crypto::hash*)&mix[i * 4])); + // } +// for (size_t count = 0; count != 4; count++) +// { + for (size_t i = 0; i != sizeof(st) / sizeof(st[0]); i++) + { + if (i == 0) + { + st[i] ^= int_array_ptr[ int_array_ptr[ int_array_ptr[st[sizeof(mix) / sizeof(st[0]) -1] % int64_sz] % int64_sz] % int64_sz]; + } + else + { + st[i] ^= int_array_ptr[int_array_ptr[int_array_ptr[st[i - 1] % int64_sz] % int64_sz] % int64_sz]; + } + //OPT_XOR_4_RES(OPT_GET_M(i * 4), OPT_GET_M(i * 4 + 1), OPT_GET_M(i * 4 + 2), OPT_GET_M(i * 4 + 3), (*(crypto::hash*)&mix[ (5-i) * 4])); + } +// } }); return true; } @@ -367,16 +381,16 @@ namespace crypto return true; } //------------------------------------------------------------------ - inline - crypto::hash get_wild_keccak2_over_scratchpad(const std::string& bd, uint64_t height, const std::vector& scratchpad, uint64_t sz) - { - crypto::hash h = { 0 }; - get_wild_keccak2_over_accessor(bd, h, height, [&](uint64_t index) -> const crypto::hash& - { - return scratchpad[index%sz]; - }); - return h; - } +// inline +// crypto::hash get_wild_keccak2_over_scratchpad(const std::string& bd, uint64_t height, const std::vector& scratchpad, uint64_t sz) +// { +// crypto::hash h = { 0 }; +// get_wild_keccak2_over_accessor(bd, h, height, [&](uint64_t index) -> const crypto::hash& +// { +// return scratchpad[index%sz]; +// }); +// return h; +// } } diff --git a/tests/performance_tests/keccak_test.h b/tests/performance_tests/keccak_test.h index 7ee1af11..668a9873 100644 --- a/tests/performance_tests/keccak_test.h +++ b/tests/performance_tests/keccak_test.h @@ -157,9 +157,9 @@ protected: void measure_keccak_over_scratchpad() { std::cout << std::setw(20) << std::left << "sz\t" << - std::setw(10) << "original\t" << + //std::setw(10) << "original\t" << std::setw(10) << "original opt\t" << - std::setw(10) << "w2\t" << +// std::setw(10) << "w2\t" << std::setw(10) << "w2_opt" << ENDL; std::vector scratchpad_vec; @@ -180,29 +180,29 @@ void measure_keccak_over_scratchpad() res_etalon = XOR_4(scratchpad_vec[0], scratchpad_vec[1], scratchpad_vec[2], scratchpad_vec[3]); +// +// crypto::hash res_h1 = currency::null_hash; +// res_h1 = crypto::get_wild_keccak2_over_scratchpad(has_str, 1, scratchpad_vec, 1000); +// +// crypto::hash res_h2 = currency::null_hash; +// crypto::get_wild_keccak2(has_str, res_h2, 1, scratchpad_vec, 1000); +// if (res_h2 != res_h1) +// { +// return; +// } - crypto::hash res_h1 = currency::null_hash; - res_h1 = crypto::get_wild_keccak2_over_scratchpad(has_str, 1, scratchpad_vec, 1000); - - crypto::hash res_h2 = currency::null_hash; - crypto::get_wild_keccak2(has_str, res_h2, 1, scratchpad_vec, 1000); - if (res_h2 != res_h1) - { - return; - } - - for (uint64_t i = 1000; i < max_measere_scratchpad; i += 50000) + for (uint64_t i = 1000; i < max_measere_scratchpad; i += 100000) { crypto::hash res_h = currency::null_hash; uint64_t ticks_a = epee::misc_utils::get_tick_count(); *(uint64_t*)(&has_str[8]) = i; //original keccak - for (size_t r = 0; r != measere_rounds; r++) - { - *(size_t*)(&has_str[0]) = r; - res_h = crypto::get_blob_longhash(has_str, 1, scratchpad_vec, i); - } +// for (size_t r = 0; r != measere_rounds; r++) +// { +// *(size_t*)(&has_str[0]) = r; +// res_h = crypto::get_blob_longhash(has_str, 1, scratchpad_vec, i); +// } //original keccak opt uint64_t ticks_b = epee::misc_utils::get_tick_count(); for (size_t r = 0; r != measere_rounds; r++) @@ -213,11 +213,11 @@ void measure_keccak_over_scratchpad() //wild keccak 2 uint64_t ticks_c = epee::misc_utils::get_tick_count(); - for (size_t r = 0; r != measere_rounds; r++) - { - *(size_t*)(&has_str[1]) = r; - res_h = crypto::get_wild_keccak2_over_scratchpad(has_str, 1, scratchpad_vec, i); - } +// for (size_t r = 0; r != measere_rounds; r++) +// { +// *(size_t*)(&has_str[1]) = r; +// res_h = crypto::get_wild_keccak2_over_scratchpad(has_str, 1, scratchpad_vec, i); +// } //wild keccak 2 opt uint64_t ticks_d = epee::misc_utils::get_tick_count(); for (size_t r = 0; r != measere_rounds; r++) @@ -227,9 +227,9 @@ void measure_keccak_over_scratchpad() uint64_t ticks_e = epee::misc_utils::get_tick_count(); std::cout << std::setw(20) << std::left << i * sizeof(crypto::hash) << "\t" << - std::setw(10) << ticks_b - ticks_a << "\t" << + //std::setw(10) << ticks_b - ticks_a << "\t" << std::setw(10) << ticks_c - ticks_b << "\t" << - std::setw(10) << ticks_d - ticks_c << "\t" << + //std::setw(10) << ticks_d - ticks_c << "\t" << std::setw(10) << ticks_e - ticks_d << ENDL; } }