diff --git a/CMakeLists.txt b/CMakeLists.txt index a8b6570..d2db36f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -149,6 +149,7 @@ endif() include_directories(src) include_directories(external/src) +include_directories(external/src/crypto) include_directories(external/src/cryptonote) include_directories(${UV_INCLUDE_DIR}) include_directories(external/src/cppzmq) diff --git a/external/src/crypto/sha256.h b/external/src/crypto/sha256.h index 38f7c82..00fb1e4 100644 --- a/external/src/crypto/sha256.h +++ b/external/src/crypto/sha256.h @@ -9,6 +9,10 @@ #ifndef SHA256_H #define SHA256_H +#ifdef __cplusplus +extern "C" { +#endif + /*************************** HEADER FILES ***************************/ #include @@ -30,4 +34,8 @@ void sha256_final(SHA256_CTX *ctx, uint8_t* hash); void sha256(const void* data, uint32_t len, uint8_t* hash); +#ifdef __cplusplus +} +#endif + #endif // SHA256_H diff --git a/src/merkle.cpp b/src/merkle.cpp index fec00a2..2a77966 100644 --- a/src/merkle.cpp +++ b/src/merkle.cpp @@ -19,6 +19,7 @@ #include "keccak.h" #include "merkle.h" #include "keccak.h" +#include "sha256.h" namespace p2pool { @@ -271,4 +272,24 @@ bool verify_merkle_proof(hash h, const std::vector& proof, size_t index, s return (h == root); } +uint32_t get_aux_slot(const hash &id, uint32_t nonce, uint32_t n_aux_chains) +{ + if (n_aux_chains <= 1) { + return 0; + } + + constexpr uint8_t HASH_KEY_MM_SLOT = 'm'; + + uint8_t buf[HASH_SIZE + sizeof(uint32_t) + 1]; + + memcpy(buf, &id, HASH_SIZE); + memcpy(buf + HASH_SIZE, &nonce, sizeof(uint32_t)); + buf[HASH_SIZE + sizeof(uint32_t)] = HASH_KEY_MM_SLOT; + + hash res; + sha256(buf, sizeof(buf), res.h); + + return *reinterpret_cast(res.h) % n_aux_chains; +} + } // namespace p2pool diff --git a/src/merkle.h b/src/merkle.h index a9c6cbb..8c36a65 100644 --- a/src/merkle.h +++ b/src/merkle.h @@ -21,8 +21,11 @@ namespace p2pool { void merkle_hash(const std::vector& hashes, hash& root); void merkle_hash_full_tree(const std::vector& hashes, std::vector>& tree); + bool get_merkle_proof(const std::vector>& tree, const hash& h, std::vector>& proof); bool verify_merkle_proof(hash h, const std::vector>& proof, const hash& root); bool verify_merkle_proof(hash h, const std::vector& proof, size_t index, size_t count, const hash& root); +uint32_t get_aux_slot(const hash &id, uint32_t nonce, uint32_t n_aux_chains); + } // namespace p2pool diff --git a/tests/src/check_win7.py b/tests/src/check_win7.py index 7a7371d..675c33e 100644 --- a/tests/src/check_win7.py +++ b/tests/src/check_win7.py @@ -272,7 +272,7 @@ for line in lines: count += 1 api = line[29:].strip() if api not in allowedAPIs: - print('API call "{}" is not checked for Windows 7 compatibility. Check it and then add it to the list in tests/src/check_imports.py'.format(api)) + print('API call "{}" is not checked for Windows 7 compatibility. Check it and then add it to the list in tests/src/check_win7.py'.format(api)) exit_code = 1 if exit_code == 0: diff --git a/tests/src/merkle_tests.cpp b/tests/src/merkle_tests.cpp index 3d0cf3e..f02c08d 100644 --- a/tests/src/merkle_tests.cpp +++ b/tests/src/merkle_tests.cpp @@ -224,4 +224,23 @@ TEST(merkle, tree) check_full_tree(); } +TEST(merkle, aux_slot) +{ + hash id; + + ASSERT_EQ(get_aux_slot(id, 0, 0), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 1), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 2), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 3), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 4), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 5), 1U); + ASSERT_EQ(get_aux_slot(id, 0, 6), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 7), 5U); + ASSERT_EQ(get_aux_slot(id, 0, 8), 0U); + ASSERT_EQ(get_aux_slot(id, 0, 9), 6U); + + ASSERT_EQ(get_aux_slot(id, 0, std::numeric_limits::max()), 2389612776U); + ASSERT_EQ(get_aux_slot(id, 1, std::numeric_limits::max()), 1080669337U); +} + } diff --git a/tests/src/sha256_tests.cpp b/tests/src/sha256_tests.cpp index c63ae9a..e29e805 100644 --- a/tests/src/sha256_tests.cpp +++ b/tests/src/sha256_tests.cpp @@ -16,11 +16,8 @@ */ #include "common.h" -#include "gtest/gtest.h" - -extern "C" { #include "sha256.h" -} +#include "gtest/gtest.h" namespace p2pool { @@ -28,7 +25,7 @@ TEST(sha256, hashing) { auto check = [](const char* input, const char* output) { hash h; - sha256(input, strlen(input), h.h); + sha256(input, static_cast(strlen(input)), h.h); char buf[128]; log::Stream s(buf);