From 90a826cbe4f07dfccdb415445a73e2301f33e15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Rosick=C3=BD?= Date: Tue, 26 Jul 2022 23:54:26 +0200 Subject: [PATCH] sha256csm init --- CMakeLists.txt | 4 +- cmake/ghostrider.cmake | 2 +- cmake/sha256.cmake | 8 +++ src/backend/cpu/CpuConfig.cpp | 1 + src/backend/cpu/CpuConfig_gen.h | 7 +++ src/backend/cpu/CpuWorker.cpp | 49 +++++++++++++++++++ src/base/crypto/Algorithm.cpp | 14 +++++- src/base/crypto/Algorithm.h | 8 ++- src/base/net/stratum/EthStratumClient.cpp | 22 +++++---- src/base/net/stratum/EthStratumClient.h | 4 +- src/base/net/stratum/Pool.cpp | 6 +-- .../net/stratum/benchmark/BenchConfig.cpp | 3 ++ src/crypto/cn/CryptoNight_test.h | 6 +++ src/crypto/ghostrider/sph_sha2.c | 16 ++++++ src/crypto/ghostrider/sph_sha2.h | 1 + src/crypto/rx/Rx.cpp | 9 ++++ src/crypto/sha256/CMakeLists.txt | 24 +++++++++ 17 files changed, 166 insertions(+), 18 deletions(-) create mode 100644 cmake/sha256.cmake create mode 100644 src/crypto/sha256/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 02aaccd6..9a6f4644 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ option(WITH_RANDOMX "Enable RandomX algorithms family" ON) option(WITH_ARGON2 "Enable Argon2 algorithms family" ON) option(WITH_KAWPOW "Enable KawPow algorithms family" ON) option(WITH_GHOSTRIDER "Enable GhostRider algorithm" ON) +option(WITH_SHA256CSM "Enable Sha256CSM algorithm" ON) option(WITH_HTTP "Enable HTTP protocol support (client/server)" ON) option(WITH_DEBUG_LOG "Enable debug log output" OFF) option(WITH_TLS "Enable OpenSSL support" ON) @@ -200,6 +201,7 @@ include(cmake/randomx.cmake) include(cmake/argon2.cmake) include(cmake/kawpow.cmake) include(cmake/ghostrider.cmake) +include(cmake/sha256.cmake) include(cmake/OpenSSL.cmake) include(cmake/asm.cmake) @@ -235,7 +237,7 @@ if (WITH_DEBUG_LOG) endif() add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${TLS_SOURCES} ${XMRIG_ASM_SOURCES}) -target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB} ${ARGON2_LIBRARY} ${ETHASH_LIBRARY} ${GHOSTRIDER_LIBRARY}) +target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB} ${ARGON2_LIBRARY} ${ETHASH_LIBRARY} ${GHOSTRIDER_LIBRARY} ${SHA256_LIBRARY}) if (WIN32) add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/bin/WinRing0/WinRing0x64.sys" $) diff --git a/cmake/ghostrider.cmake b/cmake/ghostrider.cmake index 3bc935b1..688d7359 100644 --- a/cmake/ghostrider.cmake +++ b/cmake/ghostrider.cmake @@ -1,4 +1,4 @@ -if (WITH_GHOSTRIDER) +if (WITH_GHOSTRIDER OR WITH_SHA256CSM) # WITH_SHA256CSM for sph_sha2 add_definitions(/DXMRIG_ALGO_GHOSTRIDER) add_subdirectory(src/crypto/ghostrider) set(GHOSTRIDER_LIBRARY ghostrider) diff --git a/cmake/sha256.cmake b/cmake/sha256.cmake new file mode 100644 index 00000000..4a5f7df2 --- /dev/null +++ b/cmake/sha256.cmake @@ -0,0 +1,8 @@ +if (WITH_SHA256CSM) + add_definitions(/DXMRIG_ALGO_SHA256CSM) +# add_subdirectory(src/crypto/sha256) +# set(SHA256_LIBRARY sha256) +else() + remove_definitions(/DXMRIG_ALGO_SHA256CSM) +# set(SHA256_LIBRARY "") +endif() \ No newline at end of file diff --git a/src/backend/cpu/CpuConfig.cpp b/src/backend/cpu/CpuConfig.cpp index a7d5db55..d8f3e2cd 100644 --- a/src/backend/cpu/CpuConfig.cpp +++ b/src/backend/cpu/CpuConfig.cpp @@ -179,6 +179,7 @@ void xmrig::CpuConfig::generate() count += xmrig::generate(m_threads, m_limit); count += xmrig::generate(m_threads, m_limit); count += xmrig::generate(m_threads, m_limit); + count += xmrig::generate(m_threads, m_limit); m_shouldSave |= count > 0; } diff --git a/src/backend/cpu/CpuConfig_gen.h b/src/backend/cpu/CpuConfig_gen.h index c1bdb908..e71f0ca8 100644 --- a/src/backend/cpu/CpuConfig_gen.h +++ b/src/backend/cpu/CpuConfig_gen.h @@ -161,6 +161,13 @@ size_t inline generate(Threads& threads, uint } #endif +#ifdef XMRIG_ALGO_SHA256CSM +template<> +size_t inline generate(Threads& threads, uint32_t limit) +{ + return generate(Algorithm::kSHA256CSM, threads, Algorithm::SHA256CSM, limit); +} +#endif } /* namespace xmrig */ diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index 39e46f6d..bc584dc1 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -20,6 +20,16 @@ #include #include +#include +#include +#include +#include +#include + +extern "C" { +#include "crypto/ghostrider/sph_sha2.h" +} + #include "backend/cpu/Cpu.h" #include "backend/cpu/CpuWorker.h" @@ -216,6 +226,10 @@ bool xmrig::CpuWorker::selfTest() } # endif +# ifdef XMRIG_ALGO_SHA256CSM + if (m_algorithm.id() == Algorithm::SHA256CSM) return verify(Algorithm::SHA256CSM, test_output_sha256csm); +# endif + return false; } @@ -316,6 +330,12 @@ void xmrig::CpuWorker::start() break; # endif +# ifdef XMRIG_ALGO_SHA256CSM + case Algorithm::SHA256: + sha256csm(m_hash, m_job.blob(), 0); + break; +# endif + default: fn(job.algorithm())(m_job.blob(), job.size(), m_hash, m_ctx, job.height()); break; @@ -408,6 +428,35 @@ bool xmrig::CpuWorker::verify(const Algorithm &algorithm, const uint8_t *refe } # endif +# ifdef XMRIG_ALGO_SHA256CSM + if (algorithm == Algorithm::SHA256CSM) { + uint8_t blob[80] = { + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 + }; + + // 62ddda4d -> e7412af4 + + uint8_t hash1[32] = {}; + sha256csm(hash1, blob, 0); + + // for(int i = 0; i < 32; i++) { + // printf("%02x", hash1[i]); + // } + + std::stringstream ss; + ss << std::hex << std::setfill('0'); + for (int i = 0; i < 32; i++) { + ss << std::hex << std::setw(2) << static_cast(hash1[i]); + } + + return ss.str() == "e537f42caaeadfc2f022eff26f6e4b16c78ce86f5eda63b347d4466806e07821"; + } +# endif + cn_hash_fun func = fn(algorithm); if (!func) { return false; diff --git a/src/base/crypto/Algorithm.cpp b/src/base/crypto/Algorithm.cpp index 6e76fd26..823c779a 100644 --- a/src/base/crypto/Algorithm.cpp +++ b/src/base/crypto/Algorithm.cpp @@ -101,6 +101,9 @@ const char* Algorithm::kGHOSTRIDER = "ghostrider"; const char* Algorithm::kGHOSTRIDER_RTM = "ghostrider"; #endif +#ifdef XMRIG_ALGO_SHA256CSM +const char* Algorithm::kSHA256CSM = "sha256csm"; +#endif #define ALGO_NAME(ALGO) { Algorithm::ALGO, Algorithm::k##ALGO } #define ALGO_ALIAS(ALGO, NAME) { NAME, Algorithm::ALGO } @@ -163,6 +166,10 @@ static const std::map kAlgorithmNames = { # ifdef XMRIG_ALGO_GHOSTRIDER ALGO_NAME(GHOSTRIDER_RTM), # endif + +# ifdef XMRIG_ALGO_SHA256CSM + ALGO_NAME(SHA256CSM), +# endif }; @@ -275,6 +282,10 @@ static const std::map kAlgorithmAlias ALGO_ALIAS_AUTO(KAWPOW_RVN), ALGO_ALIAS(KAWPOW_RVN, "kawpow/rvn"), # endif +# ifdef XMRIG_ALGO_SHA256CSM + ALGO_ALIAS_AUTO(SHA256CSM), ALGO_ALIAS(SHA256CSM, "sha256csm/gale"), +# endif + # ifdef XMRIG_ALGO_GHOSTRIDER ALGO_ALIAS_AUTO(GHOSTRIDER_RTM), ALGO_ALIAS(GHOSTRIDER_RTM, "ghostrider/rtm"), ALGO_ALIAS(GHOSTRIDER_RTM, "gr"), @@ -353,7 +364,8 @@ std::vector xmrig::Algorithm::all(const std::function(m_extraNonce2Size * 2)), allocator); + } else if (m_pool.algorithm().id() == Algorithm::SHA256CSM) { + params.PushBack(Value("00000000", static_cast(m_extraNonce2Size * 2)), allocator); + } + if ((m_pool.algorithm().id() == Algorithm::GHOSTRIDER_RTM || m_pool.algorithm().id() == Algorithm::SHA256CSM)) { params.PushBack(Value(m_ntime.data(), allocator), allocator); std::stringstream s; @@ -113,8 +117,8 @@ int64_t xmrig::EthStratumClient::submit(const JobResult& result) uint64_t actual_diff; -# ifdef XMRIG_ALGO_GHOSTRIDER - if (result.algorithm == Algorithm::GHOSTRIDER_RTM) { +# if defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM + if ((result.algorithm == Algorithm::GHOSTRIDER_RTM) || (result.algorithm == Algorithm::SHA256CSM)) { actual_diff = reinterpret_cast(result.result())[3]; } else @@ -195,14 +199,14 @@ void xmrig::EthStratumClient::parseNotification(const char *method, const rapidj setExtraNonce(arr[0]); } -# ifdef XMRIG_ALGO_GHOSTRIDER +# if defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM if (strcmp(method, "mining.set_difficulty") == 0) { if (!params.IsArray()) { LOG_ERR("%s " RED("invalid mining.set_difficulty notification: params is not an array"), tag()); return; } - if (m_pool.algorithm().id() != Algorithm::GHOSTRIDER_RTM) { + if ((m_pool.algorithm().id() != Algorithm::GHOSTRIDER_RTM) && (m_pool.algorithm().id() != Algorithm::SHA256CSM)) { return; } @@ -236,7 +240,7 @@ void xmrig::EthStratumClient::parseNotification(const char *method, const rapidj algo = m_pool.coin().algorithm(); } - const size_t min_arr_size = (algo.id() == Algorithm::GHOSTRIDER_RTM) ? 8 : 6; + const size_t min_arr_size = ((algo.id() == Algorithm::GHOSTRIDER_RTM) || (algo.id() == Algorithm::SHA256CSM)) ? 8 : 6; if (arr.Size() < min_arr_size) { LOG_ERR("%s " RED("invalid mining.notify notification: params array has wrong size"), tag()); @@ -256,8 +260,8 @@ void xmrig::EthStratumClient::parseNotification(const char *method, const rapidj std::stringstream s; -# ifdef XMRIG_ALGO_GHOSTRIDER - if (algo.id() == Algorithm::GHOSTRIDER_RTM) { + # if defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM + if ((algo.id() == Algorithm::GHOSTRIDER_RTM) || (algo.id() == Algorithm::SHA256CSM)) { // Raptoreum uses Bitcoin's Stratum protocol // https://en.bitcoinwiki.org/wiki/Stratum_mining_protocol#mining.notify @@ -534,7 +538,7 @@ void xmrig::EthStratumClient::onSubscribeResponse(const rapidjson::Value &result setExtraNonce(arr[1]); -# ifdef XMRIG_ALGO_GHOSTRIDER +# if defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM if ((arr.Size() > 2) && (arr[2].IsUint())) { m_extraNonce2Size = arr[2].GetUint(); } diff --git a/src/base/net/stratum/EthStratumClient.h b/src/base/net/stratum/EthStratumClient.h index ebfda6ac..49bd9a80 100644 --- a/src/base/net/stratum/EthStratumClient.h +++ b/src/base/net/stratum/EthStratumClient.h @@ -47,7 +47,7 @@ protected: void setExtraNonce(const rapidjson::Value &nonce); -# ifdef XMRIG_ALGO_GHOSTRIDER +# if defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM inline void setExtraNonce2Size(uint64_t size) { m_extraNonce2Size = size; } # endif @@ -62,7 +62,7 @@ private: bool m_authorized = false; std::pair m_extraNonce{}; -# ifdef XMRIG_ALGO_GHOSTRIDER +# if defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM uint64_t m_extraNonce2Size = 0; uint64_t m_nextDifficulty = 0; String m_ntime; diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index 7a58f4cb..f1e8f5d1 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -218,9 +218,9 @@ xmrig::IClient *xmrig::Pool::createClient(int id, IClientListener *listener) con IClient *client = nullptr; if (m_mode == MODE_POOL) { -# if defined XMRIG_ALGO_KAWPOW || defined XMRIG_ALGO_GHOSTRIDER +# if defined XMRIG_ALGO_KAWPOW || defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM const uint32_t f = m_algorithm.family(); - if ((f == Algorithm::KAWPOW) || (f == Algorithm::GHOSTRIDER) || (m_coin == Coin::RAVEN)) { + if ((f == Algorithm::KAWPOW) || (f == Algorithm::GHOSTRIDER) || (m_coin == Coin::RAVEN) || (f == Algorithm::SHA256)) { client = new EthStratumClient(id, Platform::userAgent(), listener); } else @@ -237,7 +237,7 @@ xmrig::IClient *xmrig::Pool::createClient(int id, IClientListener *listener) con client = new SelfSelectClient(id, Platform::userAgent(), listener, m_submitToOrigin); } # endif -# if defined XMRIG_ALGO_KAWPOW || defined XMRIG_ALGO_GHOSTRIDER +# if defined XMRIG_ALGO_KAWPOW || defined XMRIG_ALGO_GHOSTRIDER || defined XMRIG_ALGO_SHA256CSM else if (m_mode == MODE_AUTO_ETH) { client = new AutoClient(id, Platform::userAgent(), listener); } diff --git a/src/base/net/stratum/benchmark/BenchConfig.cpp b/src/base/net/stratum/benchmark/BenchConfig.cpp index b7acc07d..1c2f5c6e 100644 --- a/src/base/net/stratum/benchmark/BenchConfig.cpp +++ b/src/base/net/stratum/benchmark/BenchConfig.cpp @@ -69,6 +69,9 @@ xmrig::BenchConfig::BenchConfig(uint32_t size, const String &id, const rapidjson if (!m_algorithm.isValid() || (f != Algorithm::RANDOM_X # ifdef XMRIG_ALGO_GHOSTRIDER && f != Algorithm::GHOSTRIDER +# endif +# ifdef XMRIG_ALGO_SHA256CSM + && f != Algorithm::SHA256 # endif )) { m_algorithm = Algorithm::RX_0; diff --git a/src/crypto/cn/CryptoNight_test.h b/src/crypto/cn/CryptoNight_test.h index 0ce7ca46..5f9415ba 100644 --- a/src/crypto/cn/CryptoNight_test.h +++ b/src/crypto/cn/CryptoNight_test.h @@ -454,6 +454,12 @@ const static uint8_t test_output_gr[256] = { }; #endif +#ifdef XMRIG_ALGO_SHA256CSM +// "SHA256CSM" +const static uint8_t test_output_sha256csm[32] = { +}; +#endif + } // namespace xmrig diff --git a/src/crypto/ghostrider/sph_sha2.c b/src/crypto/ghostrider/sph_sha2.c index fd089684..44f272eb 100644 --- a/src/crypto/ghostrider/sph_sha2.c +++ b/src/crypto/ghostrider/sph_sha2.c @@ -783,6 +783,22 @@ void sha256d(void* hash, const void* data, int len) sph_sha256_full(hash, hash, 32); } +void sha256csm(void* hash, const void* data, int len) +{ + char emptybuffer[112]; + memset(emptybuffer, 0, sizeof(emptybuffer)); + memcpy(emptybuffer, data, 80); + + sph_sha256_context cc; + sph_sha256_init( &cc ); + sph_sha256( &cc, emptybuffer, 112 ); + sph_sha256_close( &cc, hash ); + + sph_sha256_init( &cc ); + sph_sha256( &cc, hash, 32 ); + sph_sha256_close( &cc, hash ); +} + /* see sph_sha2.h */ //void //sph_sha224_comp(const sph_u32 msg[16], sph_u32 val[8]) diff --git a/src/crypto/ghostrider/sph_sha2.h b/src/crypto/ghostrider/sph_sha2.h index ae1e3fd9..91e16b9a 100644 --- a/src/crypto/ghostrider/sph_sha2.h +++ b/src/crypto/ghostrider/sph_sha2.h @@ -208,6 +208,7 @@ void sph_sha256_comp(const sph_u32 msg[16], sph_u32 val[8]); void sph_sha256_full( void *dst, const void *data, size_t len ); void sha256d(void* hash, const void* data, int len); +void sha256csm(void* hash, const void* data, int len); // These shouldn't be called directly, use sha256-hash.h generic functions // sha256_transform_le & sha256_transform_be instead. diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp index dae49655..2e005333 100644 --- a/src/crypto/rx/Rx.cpp +++ b/src/crypto/rx/Rx.cpp @@ -94,6 +94,9 @@ bool xmrig::Rx::init(const T &seed, const RxConfig &config, const CpuConfig &cpu # endif # ifdef XMRIG_ALGO_GHOSTRIDER && (f != Algorithm::GHOSTRIDER) +# endif +# ifdef XMRIG_ALGO_SHA256CSM + && (f != Algorithm::SHA256CSM) # endif ) { # ifdef XMRIG_FEATURE_MSR @@ -121,6 +124,12 @@ bool xmrig::Rx::init(const T &seed, const RxConfig &config, const CpuConfig &cpu } # endif +# ifdef XMRIG_ALGO_SHA256CSM + if (f == Algorithm::SHA256CSM) { + return true; + } +# endif + randomx_set_scratchpad_prefetch_mode(config.scratchpadPrefetchMode()); randomx_set_huge_pages_jit(cpu.isHugePagesJit()); randomx_set_optimized_dataset_init(config.initDatasetAVX2()); diff --git a/src/crypto/sha256/CMakeLists.txt b/src/crypto/sha256/CMakeLists.txt new file mode 100644 index 00000000..981b99d7 --- /dev/null +++ b/src/crypto/sha256/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.9) +cmake_policy(SET CMP0069 NEW) + +project(sha256) + +#set(HEADERS +# sha2.h +#) + +#set(SOURCES +# sha2.c +#) + +if (CMAKE_C_COMPILER_ID MATCHES MSVC) +# set_source_files_properties(sha2.c PROPERTIES COMPILE_FLAGS "/O1 /Oi /Os") +elseif (CMAKE_C_COMPILER_ID MATCHES GNU OR CMAKE_C_COMPILER_ID MATCHES Clang) +# set_source_files_properties(sha2.c PROPERTIES COMPILE_FLAGS "-Os -s") +endif() + +include_directories(.) +include_directories(../..) +include_directories(${UV_INCLUDE_DIR}) + +add_library(sha256 STATIC ${HEADERS} ${SOURCES})