From a2fac71ab8e2b97de2112180d888c8c28e7f0fa4 Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 12 Jan 2021 23:56:53 +0300 Subject: [PATCH] experimental crypto: L2S wip and hash helper + test --- tests/functional_tests/L2S.h | 6 +- tests/functional_tests/crypto_tests.cpp | 76 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/tests/functional_tests/L2S.h b/tests/functional_tests/L2S.h index 164c1d38..e60e139f 100644 --- a/tests/functional_tests/L2S.h +++ b/tests/functional_tests/L2S.h @@ -52,12 +52,12 @@ struct ml2s_signature std::vector elements; }; -/* WIP // reference: mL2SLnkSig_Verif() bool ml2s_lnk_sig_verif(const scalar_t& m, const std::vector& B_array, const ml2s_signature& signature, uint8_t* p_err = nullptr) { #define CHECK_AND_FAIL_WITH_ERROR_IF_FALSE(cond, err_code) \ - if (!(cond)) { LOG_PRINT_RED("ml2s_lnk_sig_verif: \"" << #cond << "\" is false at " << LOCATION_SS, LOG_LEVEL_3); if (p_err) *p_err = err_code; return false; } + if (!(cond)) { LOG_PRINT_RED("ml2s_lnk_sig_verif: \"" << #cond << "\" is false at " << LOCATION_SS << ENDL << "error code = " << err_code, LOG_LEVEL_3); \ + if (p_err) *p_err = err_code; return false; } auto hash_point_lambda = [&signature](const point_t& point) { return point + signature.z * hash_helper_t::hp(point); }; @@ -108,5 +108,3 @@ bool ml2s_lnk_sig_verif(const scalar_t& m, const std::vector& B_array, return false; #undef CHECK_AND_FAIL_WITH_ERROR_IF_FALSE } - -*/ diff --git a/tests/functional_tests/crypto_tests.cpp b/tests/functional_tests/crypto_tests.cpp index d23fc623..16ddcfe3 100644 --- a/tests/functional_tests/crypto_tests.cpp +++ b/tests/functional_tests/crypto_tests.cpp @@ -538,6 +538,48 @@ static const scalar_t c_scalar_Pm1 = { 0xffffffffffffffec, 0xffffffffffffffff static const scalar_t c_scalar_256m1 = { 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff }; +// H_s hash function +struct hash_helper_t +{ + static scalar_t hs(const scalar_t& s) + { + scalar_t result = 0; + + crypto::cn_fast_hash(s.data(), sizeof s, (char*)result.data()); + + return result; + } + + static scalar_t hs(const scalar_t& s, const std::vector& ss, const std::vector& ps) + { + scalar_t result = 0; + return result; + } + + static scalar_t hs(const scalar_t& s, const std::vector& ps0, const std::vector& ps1) + { + scalar_t result = 0; + return result; + } + + static scalar_t hs(const std::vector& ps0, const std::vector& ps1) + { + scalar_t result = 0; + return result; + } + + static point_t hp(const point_t& p) + { + point_t result; + crypto::public_key pk = p; + + ge_bytes_hash_to_ec(&result.m_p3, (const unsigned char*)&pk); + + //result = 2 * result; + + return result; + } +}; //////////////////////////////////////////////////////////////////////////////// #include "L2S.h" @@ -926,6 +968,40 @@ TEST(ml2s, rsum) return true; } +TEST(ml2s, hs) +{ + scalar_t x = 2, p = 250; + //sc_exp(r.data(), x.data(), p.data()); + + x = 0; + + crypto::hash h; + scalar_t r; + + sha3(0, 0, &h, sizeof h); + LOG_PRINT("SHA3 0 -> " << h, LOG_LEVEL_0); + LOG_PRINT("SHA3 0 -> " << (scalar_t&)h, LOG_LEVEL_0); + + h = crypto::cn_fast_hash(0, 0); + LOG_PRINT("CN 0 -> " << h, LOG_LEVEL_0); + LOG_PRINT("CN 0 -> " << (scalar_t&)h, LOG_LEVEL_0); + + std::string abc("abc"); + sha3(abc.c_str(), abc.size(), &h, sizeof h); + LOG_PRINT(abc << " -> " << h, LOG_LEVEL_0); + LOG_PRINT(abc << " -> " << (scalar_t&)h, LOG_LEVEL_0); + + h = crypto::cn_fast_hash(abc.c_str(), abc.size()); + LOG_PRINT(abc << " -> " << h, LOG_LEVEL_0); + LOG_PRINT(abc << " -> " << (scalar_t&)h, LOG_LEVEL_0); + + + return true; +} + +// +// test's runner +// int crypto_tests() {