1
0
Fork 0
forked from lthn/blockchain

condidate to wild keccak2

This commit is contained in:
crypro.zoidberg 2019-01-09 22:29:33 +03:00
parent c6f1fb3b67
commit 079e5405e9
2 changed files with 81 additions and 67 deletions

View file

@ -293,30 +293,30 @@ namespace crypto
return h;
}
//------------------------------------------------------------------
template<typename callback_t>
bool get_wild_keccak2_over_accessor(const std::string& bd, crypto::hash& res, uint64_t height, callback_t accessor)
{
crypto::wild_keccak2_dbl<crypto::regular_f>(reinterpret_cast<const uint8_t*>(bd.data()), bd.size(), reinterpret_cast<uint8_t*>(&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<typename callback_t>
// bool get_wild_keccak2_over_accessor(const std::string& bd, crypto::hash& res, uint64_t height, callback_t accessor)
// {
// crypto::wild_keccak2_dbl<crypto::regular_f>(reinterpret_cast<const uint8_t*>(bd.data()), bd.size(), reinterpret_cast<uint8_t*>(&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<crypto::hash>& 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<crypto::hash>& 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;
// }
}

View file

@ -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<crypto::hash> 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;
}
}