diff --git a/CHANGELOG.md b/CHANGELOG.md index 949e60c1..3cf6268a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# v5.10.0 +- [#1602](https://github.com/xmrig/xmrig/pull/1602) Added AMD GPUs support for AstroBWT algorithm. +- [#1590](https://github.com/xmrig/xmrig/pull/1590) MSR mod automatically deactivated after switching from RandomX algorithms. +- [#1592](https://github.com/xmrig/xmrig/pull/1592) Added AVX2 optimized code for AstroBWT algorithm. + - Added new config option `astrobwt-avx2` in `cpu` object and command line option `--astrobwt-avx2`. +- [#1596](https://github.com/xmrig/xmrig/issues/1596) Major TLS (Transport Layer Security) subsystem update. + - Added new TLS options, please check [xmrig-proxy documentation](https://xmrig.com/docs/proxy/tls) for details. +- `cn/gpu` algorithm now disabled by default and will be removed in next major (v6.x.x) release, no ETA for it right now. +- Added command line option `--data-dir`. + # v5.9.0 - [#1578](https://github.com/xmrig/xmrig/pull/1578) Added new RandomKEVA algorithm for upcoming Kevacoin fork, as `"algo": "rx/keva"` or `"coin": "keva"`. - [#1584](https://github.com/xmrig/xmrig/pull/1584) Fixed invalid AstroBWT hashes after algorithm switching. diff --git a/README.md b/README.md index a628ca8a..798dadcd 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ CPU backend: --randomx-wrmsr=N write custom value (0-15) to Intel MSR register 0x1a4 or disable MSR mod (-1) --randomx-no-rdmsr disable reverting initial MSR values on exit --astrobwt-max-size=N skip hashes with large stage 2 size, default: 550, min: 400, max: 1200 + --astrobwt-avx2 enable AVX2 optimizations for AstroBWT algorithm API: --api-worker-id=ID custom worker-id for API @@ -93,6 +94,15 @@ CUDA backend: --cuda-bsleep-hint=N bsleep hint for autoconfig --no-nvml disable NVML (NVIDIA Management Library) support +TLS: + --tls-gen=HOSTNAME generate TLS certificate for specific hostname + --tls-cert=FILE load TLS certificate chain from a file in the PEM format + --tls-cert-key=FILE load TLS certificate private key from a file in the PEM format + --tls-dhparam=FILE load DH parameters for DHE ciphers from a file in the PEM format + --tls-protocols=N enable specified TLS protocols, example: "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3" + --tls-ciphers=S set list of available ciphers (TLSv1.2 and below) + --tls-ciphersuites=S set list of available TLSv1.3 ciphersuites + Logging: -S, --syslog use system log for output messages -l, --log-file=FILE log all output to a file diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index 7ff83995..53e970df 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -13,11 +13,30 @@ if (WITH_TLS) find_package(OpenSSL) if (OPENSSL_FOUND) - set(TLS_SOURCES src/base/net/stratum/Tls.h src/base/net/stratum/Tls.cpp) + set(TLS_SOURCES + src/base/net/stratum/Tls.cpp + src/base/net/stratum/Tls.h + src/base/net/tls/ServerTls.cpp + src/base/net/tls/ServerTls.h + src/base/net/tls/TlsConfig.cpp + src/base/net/tls/TlsConfig.h + src/base/net/tls/TlsContext.cpp + src/base/net/tls/TlsContext.h + src/base/net/tls/TlsGen.cpp + src/base/net/tls/TlsGen.h + ) + include_directories(${OPENSSL_INCLUDE_DIR}) if (WITH_HTTP) - set(TLS_SOURCES ${TLS_SOURCES} src/base/net/http/HttpsClient.h src/base/net/http/HttpsClient.cpp) + set(TLS_SOURCES ${TLS_SOURCES} + src/base/net/https/HttpsClient.cpp + src/base/net/https/HttpsClient.h + src/base/net/https/HttpsContext.cpp + src/base/net/https/HttpsContext.h + src/base/net/https/HttpsServer.cpp + src/base/net/https/HttpsServer.h + ) endif() else() message(FATAL_ERROR "OpenSSL NOT found: use `-DWITH_TLS=OFF` to build without TLS support") @@ -29,5 +48,12 @@ else() set(OPENSSL_LIBRARIES "") remove_definitions(/DXMRIG_FEATURE_TLS) + if (WITH_HTTP) + set(TLS_SOURCES ${TLS_SOURCES} + src/base/net/http/HttpServer.cpp + src/base/net/http/HttpServer.h + ) + endif() + set(CMAKE_PROJECT_NAME "${CMAKE_PROJECT_NAME}-notls") endif() diff --git a/cmake/astrobwt.cmake b/cmake/astrobwt.cmake index 8c89da00..9ba06e0c 100644 --- a/cmake/astrobwt.cmake +++ b/cmake/astrobwt.cmake @@ -23,6 +23,16 @@ if (WITH_ASTROBWT) src/crypto/astrobwt/salsa20_ref/salsa20.c ) else() + if (CMAKE_SIZEOF_VOID_P EQUAL 8) + enable_language(ASM_MASM) + add_definitions(/DASTROBWT_AVX2) + if (CMAKE_C_COMPILER_ID MATCHES MSVC) + list(APPEND SOURCES_CRYPTO src/crypto/astrobwt/sha3_256_avx2.asm) + else() + list(APPEND SOURCES_CRYPTO src/crypto/astrobwt/sha3_256_avx2.S) + endif() + endif() + list(APPEND HEADERS_CRYPTO src/crypto/astrobwt/Salsa20.hpp ) diff --git a/scripts/generate_cl.js b/scripts/generate_cl.js index 453a50fd..fb2a387a 100644 --- a/scripts/generate_cl.js +++ b/scripts/generate_cl.js @@ -76,6 +76,15 @@ function rx() } +function astrobwt() +{ + const astrobwt = opencl_minify(addIncludes('astrobwt.cl', [ 'BWT.cl', 'salsa20.cl', 'sha3.cl' ])); + + // fs.writeFileSync('astrobwt_gen.cl', astrobwt); + fs.writeFileSync('astrobwt_cl.h', text2h(astrobwt, 'xmrig', 'astrobwt_cl')); +} + + process.chdir(path.resolve('src/backend/opencl/cl/cn')); cn(); @@ -85,4 +94,9 @@ cn_gpu(); process.chdir(cwd); process.chdir(path.resolve('src/backend/opencl/cl/rx')); -rx(); \ No newline at end of file +rx(); + +process.chdir(cwd); +process.chdir(path.resolve('src/backend/opencl/cl/astrobwt')); + +astrobwt(); diff --git a/src/backend/cpu/CpuConfig.cpp b/src/backend/cpu/CpuConfig.cpp index 0f156e92..4c7a630f 100644 --- a/src/backend/cpu/CpuConfig.cpp +++ b/src/backend/cpu/CpuConfig.cpp @@ -52,6 +52,7 @@ static const char *kArgon2Impl = "argon2-impl"; #ifdef XMRIG_ALGO_ASTROBWT static const char* kAstroBWTMaxSize = "astrobwt-max-size"; +static const char* kAstroBWTAVX2 = "astrobwt-avx2"; #endif @@ -93,7 +94,8 @@ rapidjson::Value xmrig::CpuConfig::toJSON(rapidjson::Document &doc) const # endif # ifdef XMRIG_ALGO_ASTROBWT - obj.AddMember(StringRef(kAstroBWTMaxSize), m_astrobwtMaxSize, allocator); + obj.AddMember(StringRef(kAstroBWTMaxSize), m_astrobwtMaxSize, allocator); + obj.AddMember(StringRef(kAstroBWTAVX2), m_astrobwtAVX2, allocator); # endif m_threads.toJSON(obj, doc); @@ -148,12 +150,20 @@ void xmrig::CpuConfig::read(const rapidjson::Value &value) # endif # ifdef XMRIG_ALGO_ASTROBWT - const auto& obj = Json::getValue(value, kAstroBWTMaxSize); - if (obj.IsNull() || !obj.IsInt()) { + const auto& astroBWTMaxSize = Json::getValue(value, kAstroBWTMaxSize); + if (astroBWTMaxSize.IsNull() || !astroBWTMaxSize.IsInt()) { m_shouldSave = true; } else { - m_astrobwtMaxSize = std::min(std::max(obj.GetInt(), 400), 1200); + m_astrobwtMaxSize = std::min(std::max(astroBWTMaxSize.GetInt(), 400), 1200); + } + + const auto& astroBWTAVX2 = Json::getValue(value, kAstroBWTAVX2); + if (astroBWTAVX2.IsNull() || !astroBWTAVX2.IsBool()) { + m_shouldSave = true; + } + else { + m_astrobwtAVX2 = astroBWTAVX2.GetBool(); } # endif diff --git a/src/backend/cpu/CpuConfig.h b/src/backend/cpu/CpuConfig.h index c294f069..30166215 100644 --- a/src/backend/cpu/CpuConfig.h +++ b/src/backend/cpu/CpuConfig.h @@ -60,6 +60,7 @@ public: inline const String &argon2Impl() const { return m_argon2Impl; } inline const Threads &threads() const { return m_threads; } inline int astrobwtMaxSize() const { return m_astrobwtMaxSize; } + inline bool astrobwtAVX2() const { return m_astrobwtAVX2; } inline int priority() const { return m_priority; } inline uint32_t limit() const { return m_limit; } @@ -72,6 +73,7 @@ private: AesMode m_aes = AES_AUTO; Assembly m_assembly; + bool m_astrobwtAVX2 = false; bool m_enabled = true; bool m_hugePages = true; bool m_shouldSave = false; diff --git a/src/backend/cpu/CpuLaunchData.cpp b/src/backend/cpu/CpuLaunchData.cpp index dbc7e5c5..b0090a21 100644 --- a/src/backend/cpu/CpuLaunchData.cpp +++ b/src/backend/cpu/CpuLaunchData.cpp @@ -35,10 +35,11 @@ xmrig::CpuLaunchData::CpuLaunchData(const Miner *miner, const Algorithm &algorithm, const CpuConfig &config, const CpuThread &thread) : algorithm(algorithm), assembly(config.assembly()), + astrobwtAVX2(config.astrobwtAVX2()), hugePages(config.isHugePages()), hwAES(config.isHwAES()), yield(config.isYield()), - astrobwtMaxSize(config.astrobwtMaxSize()), + astrobwtMaxSize(config.astrobwtMaxSize()), priority(config.priority()), affinity(thread.affinity()), miner(miner), diff --git a/src/backend/cpu/CpuLaunchData.h b/src/backend/cpu/CpuLaunchData.h index 1cadc6de..7728d1d3 100644 --- a/src/backend/cpu/CpuLaunchData.h +++ b/src/backend/cpu/CpuLaunchData.h @@ -58,6 +58,7 @@ public: const Algorithm algorithm; const Assembly assembly; + const bool astrobwtAVX2; const bool hugePages; const bool hwAES; const bool yield; diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index 002d438f..dfde706e 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -78,6 +78,7 @@ xmrig::CpuWorker::CpuWorker(size_t id, const CpuLaunchData &data) : Worker(id, data.affinity, data.priority), m_algorithm(data.algorithm), m_assembly(data.assembly), + m_astrobwtAVX2(data.astrobwtAVX2), m_hwAES(data.hwAES), m_yield(data.yield), m_av(data.av()), @@ -273,7 +274,7 @@ void xmrig::CpuWorker::start() { # ifdef XMRIG_ALGO_ASTROBWT if (job.algorithm().family() == Algorithm::ASTROBWT) { - if (!astrobwt::astrobwt_dero(m_job.blob(), job.size(), m_ctx[0]->memory, m_hash, m_astrobwtMaxSize)) + if (!astrobwt::astrobwt_dero(m_job.blob(), job.size(), m_ctx[0]->memory, m_hash, m_astrobwtMaxSize, m_astrobwtAVX2)) valid = false; } else diff --git a/src/backend/cpu/CpuWorker.h b/src/backend/cpu/CpuWorker.h index 8d2c583f..8d539126 100644 --- a/src/backend/cpu/CpuWorker.h +++ b/src/backend/cpu/CpuWorker.h @@ -70,6 +70,7 @@ private: const Algorithm m_algorithm; const Assembly m_assembly; + const bool m_astrobwtAVX2; const bool m_hwAES; const bool m_yield; const CnHash::AlgoVariant m_av; diff --git a/src/backend/opencl/OclBackend.cpp b/src/backend/opencl/OclBackend.cpp index 5cbf57b7..4b1a14bc 100644 --- a/src/backend/opencl/OclBackend.cpp +++ b/src/backend/opencl/OclBackend.cpp @@ -36,6 +36,7 @@ #include "backend/opencl/OclLaunchData.h" #include "backend/opencl/OclWorker.h" #include "backend/opencl/runners/tools/OclSharedState.h" +#include "backend/opencl/runners/OclAstroBWTRunner.h" #include "backend/opencl/wrappers/OclContext.h" #include "backend/opencl/wrappers/OclLib.h" #include "base/io/log/Log.h" @@ -202,6 +203,14 @@ public: Log::print(WHITE_BOLD("| # | GPU | BUS ID | I | W | SI | MC | U | MEM | NAME")); + size_t algo_l3 = algo.l3(); + +# ifdef XMRIG_ALGO_ASTROBWT + if (algo.family() == Algorithm::ASTROBWT) { + algo_l3 = OclAstroBWTRunner::BWT_DATA_STRIDE * 17 + 324; + } +# endif + size_t i = 0; for (const auto &data : threads) { Log::print("|" CYAN_BOLD("%3zu") " |" CYAN_BOLD("%4u") " |" YELLOW(" %7s") " |" CYAN_BOLD("%5u") " |" CYAN_BOLD("%3u") " |" @@ -214,7 +223,7 @@ public: data.thread.stridedIndex(), data.thread.stridedIndex() == 2 ? std::to_string(data.thread.memChunk()).c_str() : "-", data.thread.unrollFactor(), - data.thread.intensity() * algo.l3() / oneMiB, + data.thread.intensity() * algo_l3 / oneMiB, data.device.printableName().data() ); diff --git a/src/backend/opencl/OclConfig.cpp b/src/backend/opencl/OclConfig.cpp index a90bcc90..b06746f3 100644 --- a/src/backend/opencl/OclConfig.cpp +++ b/src/backend/opencl/OclConfig.cpp @@ -220,6 +220,7 @@ void xmrig::OclConfig::generate() count += xmrig::generate(m_threads, devices); count += xmrig::generate(m_threads, devices); count += xmrig::generate(m_threads, devices); + count += xmrig::generate(m_threads, devices); m_shouldSave = count > 0; } diff --git a/src/backend/opencl/OclConfig_gen.h b/src/backend/opencl/OclConfig_gen.h index 1c8a6a43..5feacc6f 100644 --- a/src/backend/opencl/OclConfig_gen.h +++ b/src/backend/opencl/OclConfig_gen.h @@ -130,6 +130,15 @@ size_t inline generate(Threads &threads, const #endif +#ifdef XMRIG_ALGO_ASTROBWT +template<> +size_t inline generate(Threads& threads, const std::vector& devices) +{ + return generate("astrobwt", threads, Algorithm::ASTROBWT_DERO, devices); +} +#endif + + static inline std::vector filterDevices(const std::vector &devices, const std::vector &hints) { std::vector out; diff --git a/src/backend/opencl/OclThread.cpp b/src/backend/opencl/OclThread.cpp index e94eb876..b542b443 100644 --- a/src/backend/opencl/OclThread.cpp +++ b/src/backend/opencl/OclThread.cpp @@ -123,7 +123,9 @@ rapidjson::Value xmrig::OclThread::toJSON(rapidjson::Document &doc) const out.AddMember(StringRef(kIndex), index(), allocator); out.AddMember(StringRef(kIntensity), intensity(), allocator); - out.AddMember(StringRef(kWorksize), worksize(), allocator); + if (!m_fields.test(ASTROBWT_FIELDS)) { + out.AddMember(StringRef(kWorksize), worksize(), allocator); + } if (m_fields.test(STRIDED_INDEX_FIELD)) { Value si(kArrayType); @@ -149,7 +151,7 @@ rapidjson::Value xmrig::OclThread::toJSON(rapidjson::Document &doc) const out.AddMember(StringRef(kDatasetHost), isDatasetHost(), allocator); # endif } - else { + else if (!m_fields.test(ASTROBWT_FIELDS)) { out.AddMember(StringRef(kUnroll), unrollFactor(), allocator); } diff --git a/src/backend/opencl/OclThread.h b/src/backend/opencl/OclThread.h index 4febb7a0..dac57a4f 100644 --- a/src/backend/opencl/OclThread.h +++ b/src/backend/opencl/OclThread.h @@ -81,6 +81,20 @@ public: } # endif +# ifdef XMRIG_ALGO_ASTROBWT + OclThread(uint32_t index, uint32_t intensity, uint32_t threads) : + m_fields(4), + m_threads(threads, -1), + m_index(index), + m_memChunk(0), + m_stridedIndex(0), + m_unrollFactor(1), + m_worksize(1) + { + setIntensity(intensity); + } +# endif + OclThread(const rapidjson::Value &value); inline bool isAsm() const { return m_gcnAsm; } @@ -105,6 +119,7 @@ private: enum Fields { STRIDED_INDEX_FIELD, RANDOMX_FIELDS, + ASTROBWT_FIELDS, FIELD_MAX }; diff --git a/src/backend/opencl/OclWorker.cpp b/src/backend/opencl/OclWorker.cpp index c8f69b27..10e0ab33 100644 --- a/src/backend/opencl/OclWorker.cpp +++ b/src/backend/opencl/OclWorker.cpp @@ -41,6 +41,10 @@ # include "backend/opencl/runners/OclRxVmRunner.h" #endif +#ifdef XMRIG_ALGO_ASTROBWT +# include "backend/opencl/runners/OclAstroBWTRunner.h" +#endif + #ifdef XMRIG_ALGO_CN_GPU # include "backend/opencl/runners/OclRyoRunner.h" #endif @@ -96,6 +100,12 @@ xmrig::OclWorker::OclWorker(size_t id, const OclLaunchData &data) : # endif break; + case Algorithm::ASTROBWT: +# ifdef XMRIG_ALGO_ASTROBWT + m_runner = new OclAstroBWTRunner(id, data); +# endif + break; + default: # ifdef XMRIG_ALGO_CN_GPU if (m_algorithm == Algorithm::CN_GPU) { @@ -148,6 +158,8 @@ void xmrig::OclWorker::start() { cl_uint results[0x100]; + const uint32_t runnerRoundSize = m_runner->roundSize(); + while (Nonce::sequence(Nonce::OPENCL) > 0) { if (!isReady()) { m_sharedData.setResumeCounter(0); @@ -186,7 +198,7 @@ void xmrig::OclWorker::start() JobResults::submit(m_job.currentJob(), results, results[0xFF]); } - if (!m_job.nextRound(roundSize(m_intensity), m_intensity)) { + if (!m_job.nextRound(roundSize(runnerRoundSize), runnerRoundSize)) { JobResults::done(m_job.currentJob()); } @@ -228,7 +240,7 @@ void xmrig::OclWorker::storeStats(uint64_t t) return; } - m_count += m_intensity; + m_count += m_runner->processedHashes(); m_sharedData.setRunTime(Chrono::steadyMSecs() - t); diff --git a/src/backend/opencl/cl/OclSource.cpp b/src/backend/opencl/cl/OclSource.cpp index b30e8a8f..85ed4612 100644 --- a/src/backend/opencl/cl/OclSource.cpp +++ b/src/backend/opencl/cl/OclSource.cpp @@ -36,6 +36,10 @@ # include "backend/opencl/cl/rx/randomx_cl.h" #endif +#ifdef XMRIG_ALGO_ASTROBWT +# include "backend/opencl/cl/astrobwt/astrobwt_cl.h" +#endif + const char *xmrig::OclSource::get(const Algorithm &algorithm) { @@ -45,6 +49,12 @@ const char *xmrig::OclSource::get(const Algorithm &algorithm) } # endif +# ifdef XMRIG_ALGO_ASTROBWT + if (algorithm.family() == Algorithm::ASTROBWT) { + return astrobwt_cl; + } +# endif + # ifdef XMRIG_ALGO_CN_GPU if (algorithm == Algorithm::CN_GPU) { return cryptonight_gpu_cl; diff --git a/src/backend/opencl/cl/astrobwt/BWT.cl b/src/backend/opencl/cl/astrobwt/BWT.cl new file mode 100644 index 00000000..94551d95 --- /dev/null +++ b/src/backend/opencl/cl/astrobwt/BWT.cl @@ -0,0 +1,199 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define STAGE1_SIZE 147253 + +#define COUNTING_SORT_BITS 11 +#define COUNTING_SORT_SIZE (1 << COUNTING_SORT_BITS) +#define FINAL_SORT_BATCH_SIZE COUNTING_SORT_SIZE +#define FINAL_SORT_OVERLAP_SIZE 32 + +__attribute__((reqd_work_group_size(BWT_GROUP_SIZE, 1, 1))) +__kernel void BWT(__global uint8_t* datas, __global uint32_t* data_sizes, uint32_t data_stride, __global uint64_t* indices, __global uint64_t* tmp_indices) +{ + const uint32_t tid = get_local_id(0); + const uint32_t gid = get_group_id(0); + + __local int counters[COUNTING_SORT_SIZE][2]; + + for (uint32_t i = tid; i < COUNTING_SORT_SIZE * 2; i += BWT_GROUP_SIZE) + ((__local int*)counters)[i] = 0; + + const uint64_t data_offset = (uint64_t)(gid) * data_stride; + + __global uint8_t* input = datas + data_offset + 128; + const uint32_t N = data_sizes[gid] + 1; + + __global uint64_t* p = (__global uint64_t*)(input); + volatile __local uint8_t* counters_atomic = (volatile __local uint8_t*)(counters); + + indices += data_offset; + tmp_indices += data_offset; + + for (uint32_t i = tid; i < N; i += BWT_GROUP_SIZE) + { + const uint32_t index = i >> 3; + const uint32_t bit_offset = (i & 7) << 3; + + const uint64_t a = p[index]; + uint64_t b = p[index + 1]; + if (bit_offset == 0) + b = 0; + + uint64_t value = (a >> bit_offset) | (b << (64 - bit_offset)); + + uint2 tmp; + const uchar4 mask = (uchar4)(3, 2, 1, 0); + + tmp.x = as_uint(shuffle(as_uchar4(as_uint2(value).y), mask)); + tmp.y = as_uint(shuffle(as_uchar4(as_uint2(value).x), mask)); + + value = as_ulong(tmp); + + indices[i] = (value & ((uint64_t)(-1) << 21)) | i; + atomic_add((volatile __local int*)(counters_atomic + (((value >> (64 - COUNTING_SORT_BITS * 2)) & (COUNTING_SORT_SIZE - 1)) << 3)), 1); + atomic_add((volatile __local int*)(counters_atomic + ((value >> (64 - COUNTING_SORT_BITS)) << 3) + 4), 1); + } + + if (tid == 0) + { + int t0 = counters[0][0]; + int t1 = counters[0][1]; + counters[0][0] = t0 - 1; + counters[0][1] = t1 - 1; + for (uint32_t i = 1; i < COUNTING_SORT_SIZE; ++i) + { + t0 += counters[i][0]; + t1 += counters[i][1]; + counters[i][0] = t0 - 1; + counters[i][1] = t1 - 1; + } + } + + for (int i = tid; i < N; i += BWT_GROUP_SIZE) + { + const uint64_t data = indices[i]; + const int k = atomic_sub((volatile __local int*)(counters_atomic + (((data >> (64 - COUNTING_SORT_BITS * 2)) & (COUNTING_SORT_SIZE - 1)) << 3)), 1); + tmp_indices[k] = data; + } + + for (int i = N - 1 - tid; i >= 0; i -= BWT_GROUP_SIZE) + { + const uint64_t data = tmp_indices[i]; + const int k = atomic_sub((volatile __local int*)(counters_atomic + ((data >> (64 - COUNTING_SORT_BITS)) << 3) + 4), 1); + indices[k] = data; + } + + __local uint64_t* buf = (__local uint64_t*)(counters); + for (uint32_t i = 0; i < N; i += FINAL_SORT_BATCH_SIZE - FINAL_SORT_OVERLAP_SIZE) + { + const uint32_t len = (N - i < FINAL_SORT_BATCH_SIZE) ? (N - i) : FINAL_SORT_BATCH_SIZE; + + for (uint32_t j = tid; j < len; j += BWT_GROUP_SIZE) + buf[j] = indices[i + j]; + + if (tid == 0) + { + uint64_t prev_t = buf[0]; + for (int i = 1; i < len; ++i) + { + uint64_t t = buf[i]; + if (t < prev_t) + { + const uint64_t t2 = prev_t; + int j = i - 1; + do + { + buf[j + 1] = prev_t; + --j; + if (j < 0) + break; + prev_t = buf[j]; + } while (t < prev_t); + buf[j + 1] = t; + t = t2; + } + prev_t = t; + } + } + + for (uint32_t j = tid; j < len; j += BWT_GROUP_SIZE) + indices[i + j] = buf[j]; + } + + --input; + __global uint8_t* output = (__global uint8_t*)(tmp_indices); + for (int i = tid; i <= N; i += BWT_GROUP_SIZE) + output[i] = input[indices[i] & ((1 << 21) - 1)]; +} + +__kernel void filter(uint32_t nonce, uint32_t bwt_max_size, __global const uint32_t* hashes, __global uint32_t* filtered_hashes) +{ + const uint32_t global_id = get_global_id(0); + + __global const uint32_t* hash = hashes + global_id * (32 / sizeof(uint32_t)); + const uint32_t stage2_size = STAGE1_SIZE + (*hash & 0xfffff); + + if (stage2_size < bwt_max_size) + { + const int index = atomic_add((volatile __global int*)(filtered_hashes), 1) * (36 / sizeof(uint32_t)) + 1; + + filtered_hashes[index] = nonce + global_id; + + #pragma unroll(8) + for (uint32_t i = 0; i < 8; ++i) + filtered_hashes[index + i + 1] = hash[i]; + } +} + +__kernel void prepare_batch2(__global uint32_t* hashes, __global uint32_t* filtered_hashes, __global uint32_t* data_sizes) +{ + const uint32_t global_id = get_global_id(0); + const uint32_t N = filtered_hashes[0] - get_global_size(0); + + if (global_id == 0) + filtered_hashes[0] = N; + + __global uint32_t* hash = hashes + global_id * 8; + __global uint32_t* filtered_hash = filtered_hashes + (global_id + N) * 9 + 1; + + const uint32_t stage2_size = STAGE1_SIZE + (filtered_hash[1] & 0xfffff); + data_sizes[global_id] = stage2_size; + + #pragma unroll(8) + for (uint32_t i = 0; i < 8; ++i) + hash[i] = filtered_hash[i + 1]; +} + +__kernel void find_shares(__global const uint64_t* hashes, __global const uint32_t* filtered_hashes, uint64_t target, __global uint32_t* shares) +{ + const uint32_t global_index = get_global_id(0); + + if (hashes[global_index * 4 + 3] < target) + { + const uint32_t idx = atomic_inc(shares + 0xFF); + if (idx < 0xFF) + shares[idx] = filtered_hashes[(filtered_hashes[0] + global_index) * 9 + 1]; + } +} diff --git a/src/backend/opencl/cl/astrobwt/astrobwt.cl b/src/backend/opencl/cl/astrobwt/astrobwt.cl new file mode 100644 index 00000000..476366c8 --- /dev/null +++ b/src/backend/opencl/cl/astrobwt/astrobwt.cl @@ -0,0 +1,35 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +typedef uchar uint8_t; +typedef ushort uint16_t; +typedef uint uint32_t; +typedef ulong uint64_t; + +typedef int int32_t; +typedef long int64_t; + +#include "BWT.cl" +#include "salsa20.cl" +#include "sha3.cl" diff --git a/src/backend/opencl/cl/astrobwt/astrobwt_cl.h b/src/backend/opencl/cl/astrobwt/astrobwt_cl.h new file mode 100644 index 00000000..200ac244 --- /dev/null +++ b/src/backend/opencl/cl/astrobwt/astrobwt_cl.h @@ -0,0 +1,395 @@ +#pragma once + +namespace xmrig { + +static char astrobwt_cl[12378] = { + 0x74,0x79,0x70,0x65,0x64,0x65,0x66,0x20,0x75,0x63,0x68,0x61,0x72,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x3b,0x0a,0x74,0x79,0x70,0x65,0x64,0x65,0x66,0x20,0x75, + 0x73,0x68,0x6f,0x72,0x74,0x20,0x75,0x69,0x6e,0x74,0x31,0x36,0x5f,0x74,0x3b,0x0a,0x74,0x79,0x70,0x65,0x64,0x65,0x66,0x20,0x75,0x69,0x6e,0x74,0x20,0x75,0x69,0x6e, + 0x74,0x33,0x32,0x5f,0x74,0x3b,0x0a,0x74,0x79,0x70,0x65,0x64,0x65,0x66,0x20,0x75,0x6c,0x6f,0x6e,0x67,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x3b,0x0a,0x74, + 0x79,0x70,0x65,0x64,0x65,0x66,0x20,0x69,0x6e,0x74,0x20,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x3b,0x0a,0x74,0x79,0x70,0x65,0x64,0x65,0x66,0x20,0x6c,0x6f,0x6e,0x67, + 0x20,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x3b,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x53,0x54,0x41,0x47,0x45,0x31,0x5f,0x53,0x49,0x5a,0x45,0x20,0x31,0x34, + 0x37,0x32,0x35,0x33,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x49,0x54,0x53,0x20, + 0x31,0x31,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x20,0x28,0x31, + 0x20,0x3c,0x3c,0x20,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x49,0x54,0x53,0x29,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20, + 0x46,0x49,0x4e,0x41,0x4c,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x41,0x54,0x43,0x48,0x5f,0x53,0x49,0x5a,0x45,0x20,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53, + 0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x46,0x49,0x4e,0x41,0x4c,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x4f,0x56,0x45,0x52, + 0x4c,0x41,0x50,0x5f,0x53,0x49,0x5a,0x45,0x20,0x33,0x32,0x0a,0x5f,0x5f,0x61,0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x5f,0x5f,0x28,0x28,0x72,0x65,0x71,0x64,0x5f, + 0x77,0x6f,0x72,0x6b,0x5f,0x67,0x72,0x6f,0x75,0x70,0x5f,0x73,0x69,0x7a,0x65,0x28,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x2c,0x31, + 0x2c,0x31,0x29,0x29,0x29,0x0a,0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65,0x6c,0x20,0x76,0x6f,0x69,0x64,0x20,0x42,0x57,0x54,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c, + 0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x64,0x61,0x74,0x61,0x73,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f, + 0x74,0x2a,0x20,0x64,0x61,0x74,0x61,0x5f,0x73,0x69,0x7a,0x65,0x73,0x2c,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x64,0x61,0x74,0x61,0x5f,0x73,0x74,0x72,0x69, + 0x64,0x65,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x2c,0x5f,0x5f, + 0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x74,0x6d,0x70,0x5f,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x29,0x0a,0x7b,0x0a, + 0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x74,0x69,0x64,0x3d,0x67,0x65,0x74,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x5f,0x69,0x64,0x28, + 0x30,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x69,0x64,0x3d,0x67,0x65,0x74,0x5f,0x67,0x72,0x6f,0x75,0x70, + 0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x69,0x6e,0x74,0x20,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5b,0x43,0x4f,0x55, + 0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x5d,0x5b,0x32,0x5d,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32, + 0x5f,0x74,0x20,0x69,0x3d,0x74,0x69,0x64,0x3b,0x20,0x69,0x3c,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x2a,0x32, + 0x3b,0x20,0x69,0x2b,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x28,0x28,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x69, + 0x6e,0x74,0x2a,0x29,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x29,0x5b,0x69,0x5d,0x3d,0x30,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34, + 0x5f,0x74,0x20,0x64,0x61,0x74,0x61,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x3d,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x67,0x69,0x64,0x29,0x2a,0x64, + 0x61,0x74,0x61,0x5f,0x73,0x74,0x72,0x69,0x64,0x65,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x69,0x6e, + 0x70,0x75,0x74,0x3d,0x64,0x61,0x74,0x61,0x73,0x2b,0x64,0x61,0x74,0x61,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x2b,0x31,0x32,0x38,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74, + 0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x4e,0x3d,0x64,0x61,0x74,0x61,0x5f,0x73,0x69,0x7a,0x65,0x73,0x5b,0x67,0x69,0x64,0x5d,0x2b,0x31,0x3b,0x0a,0x5f, + 0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x70,0x3d,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69, + 0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x29,0x28,0x69,0x6e,0x70,0x75,0x74,0x29,0x3b,0x0a,0x76,0x6f,0x6c,0x61,0x74,0x69,0x6c,0x65,0x20,0x5f,0x5f,0x6c,0x6f,0x63,0x61, + 0x6c,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5f,0x61,0x74,0x6f,0x6d,0x69,0x63,0x3d,0x28,0x76,0x6f,0x6c,0x61, + 0x74,0x69,0x6c,0x65,0x20,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x29,0x28,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x29, + 0x3b,0x0a,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x2b,0x3d,0x64,0x61,0x74,0x61,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x3b,0x0a,0x74,0x6d,0x70,0x5f,0x69,0x6e,0x64,0x69, + 0x63,0x65,0x73,0x2b,0x3d,0x64,0x61,0x74,0x61,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20, + 0x69,0x3d,0x74,0x69,0x64,0x3b,0x20,0x69,0x3c,0x4e,0x3b,0x20,0x69,0x2b,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x7b, + 0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x6e,0x64,0x65,0x78,0x3d,0x69,0x3e,0x3e,0x33,0x3b,0x0a,0x63,0x6f,0x6e,0x73, + 0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x62,0x69,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x3d,0x28,0x69,0x26,0x37,0x29,0x3c,0x3c,0x33,0x3b,0x0a, + 0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x61,0x3d,0x70,0x5b,0x69,0x6e,0x64,0x65,0x78,0x5d,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x36, + 0x34,0x5f,0x74,0x20,0x62,0x3d,0x70,0x5b,0x69,0x6e,0x64,0x65,0x78,0x2b,0x31,0x5d,0x3b,0x0a,0x69,0x66,0x28,0x62,0x69,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x3d, + 0x3d,0x30,0x29,0x0a,0x62,0x3d,0x30,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x76,0x61,0x6c,0x75,0x65,0x3d,0x28,0x61,0x3e,0x3e,0x62,0x69,0x74,0x5f, + 0x6f,0x66,0x66,0x73,0x65,0x74,0x29,0x7c,0x28,0x62,0x3c,0x3c,0x28,0x36,0x34,0x2d,0x62,0x69,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x29,0x29,0x3b,0x0a,0x75,0x69, + 0x6e,0x74,0x32,0x20,0x74,0x6d,0x70,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x63,0x68,0x61,0x72,0x34,0x20,0x6d,0x61,0x73,0x6b,0x3d,0x28,0x75,0x63,0x68,0x61, + 0x72,0x34,0x29,0x28,0x33,0x2c,0x32,0x2c,0x31,0x2c,0x30,0x29,0x3b,0x0a,0x74,0x6d,0x70,0x2e,0x78,0x3d,0x61,0x73,0x5f,0x75,0x69,0x6e,0x74,0x28,0x73,0x68,0x75,0x66, + 0x66,0x6c,0x65,0x28,0x61,0x73,0x5f,0x75,0x63,0x68,0x61,0x72,0x34,0x28,0x61,0x73,0x5f,0x75,0x69,0x6e,0x74,0x32,0x28,0x76,0x61,0x6c,0x75,0x65,0x29,0x2e,0x79,0x29, + 0x2c,0x6d,0x61,0x73,0x6b,0x29,0x29,0x3b,0x0a,0x74,0x6d,0x70,0x2e,0x79,0x3d,0x61,0x73,0x5f,0x75,0x69,0x6e,0x74,0x28,0x73,0x68,0x75,0x66,0x66,0x6c,0x65,0x28,0x61, + 0x73,0x5f,0x75,0x63,0x68,0x61,0x72,0x34,0x28,0x61,0x73,0x5f,0x75,0x69,0x6e,0x74,0x32,0x28,0x76,0x61,0x6c,0x75,0x65,0x29,0x2e,0x78,0x29,0x2c,0x6d,0x61,0x73,0x6b, + 0x29,0x29,0x3b,0x0a,0x76,0x61,0x6c,0x75,0x65,0x3d,0x61,0x73,0x5f,0x75,0x6c,0x6f,0x6e,0x67,0x28,0x74,0x6d,0x70,0x29,0x3b,0x0a,0x69,0x6e,0x64,0x69,0x63,0x65,0x73, + 0x5b,0x69,0x5d,0x3d,0x28,0x76,0x61,0x6c,0x75,0x65,0x26,0x28,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x2d,0x31,0x29,0x3c,0x3c,0x32,0x31,0x29,0x29, + 0x7c,0x69,0x3b,0x0a,0x61,0x74,0x6f,0x6d,0x69,0x63,0x5f,0x61,0x64,0x64,0x28,0x28,0x76,0x6f,0x6c,0x61,0x74,0x69,0x6c,0x65,0x20,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c, + 0x20,0x69,0x6e,0x74,0x2a,0x29,0x28,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5f,0x61,0x74,0x6f,0x6d,0x69,0x63,0x2b,0x28,0x28,0x28,0x76,0x61,0x6c,0x75,0x65,0x3e, + 0x3e,0x28,0x36,0x34,0x2d,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x49,0x54,0x53,0x2a,0x32,0x29,0x29,0x26,0x28,0x43,0x4f,0x55, + 0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x2d,0x31,0x29,0x29,0x3c,0x3c,0x33,0x29,0x29,0x2c,0x31,0x29,0x3b,0x0a,0x61,0x74,0x6f, + 0x6d,0x69,0x63,0x5f,0x61,0x64,0x64,0x28,0x28,0x76,0x6f,0x6c,0x61,0x74,0x69,0x6c,0x65,0x20,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x69,0x6e,0x74,0x2a,0x29,0x28, + 0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5f,0x61,0x74,0x6f,0x6d,0x69,0x63,0x2b,0x28,0x28,0x76,0x61,0x6c,0x75,0x65,0x3e,0x3e,0x28,0x36,0x34,0x2d,0x43,0x4f,0x55, + 0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x49,0x54,0x53,0x29,0x29,0x3c,0x3c,0x33,0x29,0x2b,0x34,0x29,0x2c,0x31,0x29,0x3b,0x0a,0x7d,0x0a,0x69, + 0x66,0x28,0x74,0x69,0x64,0x3d,0x3d,0x30,0x29,0x0a,0x7b,0x0a,0x69,0x6e,0x74,0x20,0x74,0x30,0x3d,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5b,0x30,0x5d,0x5b,0x30, + 0x5d,0x3b,0x0a,0x69,0x6e,0x74,0x20,0x74,0x31,0x3d,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5b,0x30,0x5d,0x5b,0x31,0x5d,0x3b,0x0a,0x63,0x6f,0x75,0x6e,0x74,0x65, + 0x72,0x73,0x5b,0x30,0x5d,0x5b,0x30,0x5d,0x3d,0x74,0x30,0x2d,0x31,0x3b,0x0a,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5b,0x30,0x5d,0x5b,0x31,0x5d,0x3d,0x74,0x31, + 0x2d,0x31,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x31,0x3b,0x20,0x69,0x3c,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e, + 0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x3b,0x20,0x2b,0x2b,0x69,0x29,0x0a,0x7b,0x0a,0x74,0x30,0x2b,0x3d,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73, + 0x5b,0x69,0x5d,0x5b,0x30,0x5d,0x3b,0x0a,0x74,0x31,0x2b,0x3d,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5b,0x69,0x5d,0x5b,0x31,0x5d,0x3b,0x0a,0x63,0x6f,0x75,0x6e, + 0x74,0x65,0x72,0x73,0x5b,0x69,0x5d,0x5b,0x30,0x5d,0x3d,0x74,0x30,0x2d,0x31,0x3b,0x0a,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5b,0x69,0x5d,0x5b,0x31,0x5d,0x3d, + 0x74,0x31,0x2d,0x31,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69,0x6e,0x74,0x20,0x69,0x3d,0x74,0x69,0x64,0x3b,0x20,0x69,0x3c,0x4e,0x3b,0x20,0x69, + 0x2b,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34, + 0x5f,0x74,0x20,0x64,0x61,0x74,0x61,0x3d,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x5b,0x69,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x6b,0x3d, + 0x61,0x74,0x6f,0x6d,0x69,0x63,0x5f,0x73,0x75,0x62,0x28,0x28,0x76,0x6f,0x6c,0x61,0x74,0x69,0x6c,0x65,0x20,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x69,0x6e,0x74, + 0x2a,0x29,0x28,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5f,0x61,0x74,0x6f,0x6d,0x69,0x63,0x2b,0x28,0x28,0x28,0x64,0x61,0x74,0x61,0x3e,0x3e,0x28,0x36,0x34,0x2d, + 0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x49,0x54,0x53,0x2a,0x32,0x29,0x29,0x26,0x28,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47, + 0x5f,0x53,0x4f,0x52,0x54,0x5f,0x53,0x49,0x5a,0x45,0x2d,0x31,0x29,0x29,0x3c,0x3c,0x33,0x29,0x29,0x2c,0x31,0x29,0x3b,0x0a,0x74,0x6d,0x70,0x5f,0x69,0x6e,0x64,0x69, + 0x63,0x65,0x73,0x5b,0x6b,0x5d,0x3d,0x64,0x61,0x74,0x61,0x3b,0x0a,0x7d,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69,0x6e,0x74,0x20,0x69,0x3d,0x4e,0x2d,0x31,0x2d,0x74,0x69, + 0x64,0x3b,0x20,0x69,0x3e,0x3d,0x30,0x3b,0x20,0x69,0x2d,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x7b,0x0a,0x63,0x6f, + 0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x64,0x61,0x74,0x61,0x3d,0x74,0x6d,0x70,0x5f,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x5b,0x69,0x5d, + 0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x6b,0x3d,0x61,0x74,0x6f,0x6d,0x69,0x63,0x5f,0x73,0x75,0x62,0x28,0x28,0x76,0x6f,0x6c,0x61,0x74,0x69, + 0x6c,0x65,0x20,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x69,0x6e,0x74,0x2a,0x29,0x28,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x5f,0x61,0x74,0x6f,0x6d,0x69,0x63, + 0x2b,0x28,0x28,0x64,0x61,0x74,0x61,0x3e,0x3e,0x28,0x36,0x34,0x2d,0x43,0x4f,0x55,0x4e,0x54,0x49,0x4e,0x47,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x49,0x54,0x53,0x29, + 0x29,0x3c,0x3c,0x33,0x29,0x2b,0x34,0x29,0x2c,0x31,0x29,0x3b,0x0a,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x5b,0x6b,0x5d,0x3d,0x64,0x61,0x74,0x61,0x3b,0x0a,0x7d,0x0a, + 0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x62,0x75,0x66,0x3d,0x28,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75, + 0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x29,0x28,0x63,0x6f,0x75,0x6e,0x74,0x65,0x72,0x73,0x29,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32, + 0x5f,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x4e,0x3b,0x20,0x69,0x2b,0x3d,0x46,0x49,0x4e,0x41,0x4c,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x42,0x41,0x54,0x43,0x48, + 0x5f,0x53,0x49,0x5a,0x45,0x2d,0x46,0x49,0x4e,0x41,0x4c,0x5f,0x53,0x4f,0x52,0x54,0x5f,0x4f,0x56,0x45,0x52,0x4c,0x41,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x7b, + 0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6c,0x65,0x6e,0x3d,0x28,0x4e,0x2d,0x69,0x3c,0x46,0x49,0x4e,0x41,0x4c,0x5f,0x53, + 0x4f,0x52,0x54,0x5f,0x42,0x41,0x54,0x43,0x48,0x5f,0x53,0x49,0x5a,0x45,0x29,0x3f,0x28,0x4e,0x2d,0x69,0x29,0x3a,0x46,0x49,0x4e,0x41,0x4c,0x5f,0x53,0x4f,0x52,0x54, + 0x5f,0x42,0x41,0x54,0x43,0x48,0x5f,0x53,0x49,0x5a,0x45,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x3d,0x74,0x69,0x64, + 0x3b,0x20,0x6a,0x3c,0x6c,0x65,0x6e,0x3b,0x20,0x6a,0x2b,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x62,0x75,0x66,0x5b, + 0x6a,0x5d,0x3d,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x5b,0x69,0x2b,0x6a,0x5d,0x3b,0x0a,0x69,0x66,0x28,0x74,0x69,0x64,0x3d,0x3d,0x30,0x29,0x0a,0x7b,0x0a,0x75,0x69, + 0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x70,0x72,0x65,0x76,0x5f,0x74,0x3d,0x62,0x75,0x66,0x5b,0x30,0x5d,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69,0x6e,0x74,0x20,0x69, + 0x3d,0x31,0x3b,0x20,0x69,0x3c,0x6c,0x65,0x6e,0x3b,0x20,0x2b,0x2b,0x69,0x29,0x0a,0x7b,0x0a,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x74,0x3d,0x62,0x75,0x66, + 0x5b,0x69,0x5d,0x3b,0x0a,0x69,0x66,0x28,0x74,0x3c,0x70,0x72,0x65,0x76,0x5f,0x74,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34, + 0x5f,0x74,0x20,0x74,0x32,0x3d,0x70,0x72,0x65,0x76,0x5f,0x74,0x3b,0x0a,0x69,0x6e,0x74,0x20,0x6a,0x3d,0x69,0x2d,0x31,0x3b,0x0a,0x64,0x6f,0x0a,0x7b,0x0a,0x62,0x75, + 0x66,0x5b,0x6a,0x2b,0x31,0x5d,0x3d,0x70,0x72,0x65,0x76,0x5f,0x74,0x3b,0x0a,0x2d,0x2d,0x6a,0x3b,0x0a,0x69,0x66,0x28,0x6a,0x3c,0x30,0x29,0x0a,0x62,0x72,0x65,0x61, + 0x6b,0x3b,0x0a,0x70,0x72,0x65,0x76,0x5f,0x74,0x3d,0x62,0x75,0x66,0x5b,0x6a,0x5d,0x3b,0x0a,0x7d,0x20,0x77,0x68,0x69,0x6c,0x65,0x20,0x28,0x74,0x3c,0x70,0x72,0x65, + 0x76,0x5f,0x74,0x29,0x3b,0x0a,0x62,0x75,0x66,0x5b,0x6a,0x2b,0x31,0x5d,0x3d,0x74,0x3b,0x0a,0x74,0x3d,0x74,0x32,0x3b,0x0a,0x7d,0x0a,0x70,0x72,0x65,0x76,0x5f,0x74, + 0x3d,0x74,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x3d,0x74,0x69,0x64,0x3b,0x20,0x6a,0x3c,0x6c, + 0x65,0x6e,0x3b,0x20,0x6a,0x2b,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x5b,0x69, + 0x2b,0x6a,0x5d,0x3d,0x62,0x75,0x66,0x5b,0x6a,0x5d,0x3b,0x0a,0x7d,0x0a,0x2d,0x2d,0x69,0x6e,0x70,0x75,0x74,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20, + 0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x3d,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f, + 0x74,0x2a,0x29,0x28,0x74,0x6d,0x70,0x5f,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x29,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69,0x6e,0x74,0x20,0x69,0x3d,0x74,0x69,0x64, + 0x3b,0x20,0x69,0x3c,0x3d,0x4e,0x3b,0x20,0x69,0x2b,0x3d,0x42,0x57,0x54,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x6f,0x75,0x74,0x70,0x75, + 0x74,0x5b,0x69,0x5d,0x3d,0x69,0x6e,0x70,0x75,0x74,0x5b,0x69,0x6e,0x64,0x69,0x63,0x65,0x73,0x5b,0x69,0x5d,0x26,0x28,0x28,0x31,0x3c,0x3c,0x32,0x31,0x29,0x2d,0x31, + 0x29,0x5d,0x3b,0x0a,0x7d,0x0a,0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65,0x6c,0x20,0x76,0x6f,0x69,0x64,0x20,0x66,0x69,0x6c,0x74,0x65,0x72,0x28,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x6e,0x6f,0x6e,0x63,0x65,0x2c,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x62,0x77,0x74,0x5f,0x6d,0x61,0x78,0x5f,0x73,0x69,0x7a,0x65,0x2c, + 0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x68,0x61,0x73,0x68,0x65,0x73,0x2c, + 0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68, + 0x65,0x73,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x3d,0x67, + 0x65,0x74,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20, + 0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x68,0x61,0x73,0x68,0x3d,0x68,0x61,0x73,0x68,0x65,0x73,0x2b,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x2a, + 0x28,0x33,0x32,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e, + 0x74,0x33,0x32,0x5f,0x74,0x20,0x73,0x74,0x61,0x67,0x65,0x32,0x5f,0x73,0x69,0x7a,0x65,0x3d,0x53,0x54,0x41,0x47,0x45,0x31,0x5f,0x53,0x49,0x5a,0x45,0x2b,0x28,0x2a, + 0x68,0x61,0x73,0x68,0x26,0x30,0x78,0x66,0x66,0x66,0x66,0x66,0x29,0x3b,0x0a,0x69,0x66,0x28,0x73,0x74,0x61,0x67,0x65,0x32,0x5f,0x73,0x69,0x7a,0x65,0x3c,0x62,0x77, + 0x74,0x5f,0x6d,0x61,0x78,0x5f,0x73,0x69,0x7a,0x65,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x69,0x6e,0x64,0x65,0x78,0x3d,0x61,0x74, + 0x6f,0x6d,0x69,0x63,0x5f,0x61,0x64,0x64,0x28,0x28,0x76,0x6f,0x6c,0x61,0x74,0x69,0x6c,0x65,0x20,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x69,0x6e,0x74,0x2a, + 0x29,0x28,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x29,0x2c,0x31,0x29,0x2a,0x28,0x33,0x36,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66, + 0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x29,0x2b,0x31,0x3b,0x0a,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x69, + 0x6e,0x64,0x65,0x78,0x5d,0x3d,0x6e,0x6f,0x6e,0x63,0x65,0x2b,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x3b,0x0a,0x23,0x70,0x72,0x61,0x67,0x6d,0x61,0x20,0x75, + 0x6e,0x72,0x6f,0x6c,0x6c,0x28,0x38,0x29,0x0a,0x66,0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x38,0x3b, + 0x20,0x2b,0x2b,0x69,0x29,0x0a,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x69,0x6e,0x64,0x65,0x78,0x2b,0x69,0x2b,0x31,0x5d, + 0x3d,0x68,0x61,0x73,0x68,0x5b,0x69,0x5d,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65,0x6c,0x20,0x76,0x6f,0x69,0x64,0x20,0x70,0x72,0x65,0x70, + 0x61,0x72,0x65,0x5f,0x62,0x61,0x74,0x63,0x68,0x32,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x68,0x61, + 0x73,0x68,0x65,0x73,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64, + 0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x64,0x61,0x74,0x61,0x5f, + 0x73,0x69,0x7a,0x65,0x73,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69, + 0x64,0x3d,0x67,0x65,0x74,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32, + 0x5f,0x74,0x20,0x4e,0x3d,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x30,0x5d,0x2d,0x67,0x65,0x74,0x5f,0x67,0x6c,0x6f,0x62, + 0x61,0x6c,0x5f,0x73,0x69,0x7a,0x65,0x28,0x30,0x29,0x3b,0x0a,0x69,0x66,0x28,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x3d,0x3d,0x30,0x29,0x0a,0x66,0x69,0x6c, + 0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x30,0x5d,0x3d,0x4e,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74, + 0x33,0x32,0x5f,0x74,0x2a,0x20,0x68,0x61,0x73,0x68,0x3d,0x68,0x61,0x73,0x68,0x65,0x73,0x2b,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x2a,0x38,0x3b,0x0a,0x5f, + 0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x3d, + 0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x2b,0x28,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x2b,0x4e,0x29,0x2a,0x39,0x2b, + 0x31,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x73,0x74,0x61,0x67,0x65,0x32,0x5f,0x73,0x69,0x7a,0x65,0x3d,0x53,0x54, + 0x41,0x47,0x45,0x31,0x5f,0x53,0x49,0x5a,0x45,0x2b,0x28,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x5b,0x31,0x5d,0x26,0x30,0x78,0x66,0x66, + 0x66,0x66,0x66,0x29,0x3b,0x0a,0x64,0x61,0x74,0x61,0x5f,0x73,0x69,0x7a,0x65,0x73,0x5b,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x5d,0x3d,0x73,0x74,0x61,0x67, + 0x65,0x32,0x5f,0x73,0x69,0x7a,0x65,0x3b,0x0a,0x23,0x70,0x72,0x61,0x67,0x6d,0x61,0x20,0x75,0x6e,0x72,0x6f,0x6c,0x6c,0x28,0x38,0x29,0x0a,0x66,0x6f,0x72,0x20,0x28, + 0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x38,0x3b,0x20,0x2b,0x2b,0x69,0x29,0x0a,0x68,0x61,0x73,0x68,0x5b,0x69,0x5d,0x3d, + 0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x5b,0x69,0x2b,0x31,0x5d,0x3b,0x0a,0x7d,0x0a,0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65,0x6c,0x20,0x76, + 0x6f,0x69,0x64,0x20,0x66,0x69,0x6e,0x64,0x5f,0x73,0x68,0x61,0x72,0x65,0x73,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75, + 0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x68,0x61,0x73,0x68,0x65,0x73,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75, + 0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x2c,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f, + 0x74,0x20,0x74,0x61,0x72,0x67,0x65,0x74,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x73,0x68,0x61,0x72, + 0x65,0x73,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x6e,0x64,0x65, + 0x78,0x3d,0x67,0x65,0x74,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x69,0x66,0x28,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x67,0x6c, + 0x6f,0x62,0x61,0x6c,0x5f,0x69,0x6e,0x64,0x65,0x78,0x2a,0x34,0x2b,0x33,0x5d,0x3c,0x74,0x61,0x72,0x67,0x65,0x74,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20, + 0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x64,0x78,0x3d,0x61,0x74,0x6f,0x6d,0x69,0x63,0x5f,0x69,0x6e,0x63,0x28,0x73,0x68,0x61,0x72,0x65,0x73,0x2b,0x30, + 0x78,0x46,0x46,0x29,0x3b,0x0a,0x69,0x66,0x28,0x69,0x64,0x78,0x3c,0x30,0x78,0x46,0x46,0x29,0x0a,0x73,0x68,0x61,0x72,0x65,0x73,0x5b,0x69,0x64,0x78,0x5d,0x3d,0x66, + 0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x28,0x66,0x69,0x6c,0x74,0x65,0x72,0x65,0x64,0x5f,0x68,0x61,0x73,0x68,0x65,0x73,0x5b, + 0x30,0x5d,0x2b,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x5f,0x69,0x6e,0x64,0x65,0x78,0x29,0x2a,0x39,0x2b,0x31,0x5d,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x23,0x64,0x65,0x66,0x69, + 0x6e,0x65,0x20,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x76,0x2c,0x63,0x29,0x20,0x28,0x72,0x6f,0x74,0x61,0x74,0x65,0x28,0x76,0x2c,0x28,0x75,0x69,0x6e,0x74,0x33,0x32, + 0x5f,0x74,0x29,0x63,0x29,0x29,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x58,0x4f,0x52,0x28,0x76,0x2c,0x77,0x29,0x20,0x28,0x28,0x76,0x29,0x20,0x5e,0x20,0x28, + 0x77,0x29,0x29,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x50,0x4c,0x55,0x53,0x28,0x76,0x2c,0x77,0x29,0x20,0x28,0x28,0x76,0x29,0x20,0x2b,0x20,0x28,0x77,0x29, + 0x29,0x0a,0x5f,0x5f,0x61,0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x5f,0x5f,0x28,0x28,0x72,0x65,0x71,0x64,0x5f,0x77,0x6f,0x72,0x6b,0x5f,0x67,0x72,0x6f,0x75,0x70, + 0x5f,0x73,0x69,0x7a,0x65,0x28,0x53,0x41,0x4c,0x53,0x41,0x32,0x30,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x2c,0x31,0x2c,0x31,0x29,0x29,0x29,0x0a, + 0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65,0x6c,0x20,0x76,0x6f,0x69,0x64,0x20,0x53,0x61,0x6c,0x73,0x61,0x32,0x30,0x5f,0x58,0x4f,0x52,0x4b,0x65,0x79,0x53,0x74,0x72,0x65, + 0x61,0x6d,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x6b,0x65,0x79,0x73, + 0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x73,0x2c,0x5f,0x5f,0x67,0x6c, + 0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x73,0x2c,0x75,0x69,0x6e,0x74, + 0x33,0x32,0x5f,0x74,0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x74,0x72,0x69,0x64,0x65,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74, + 0x33,0x32,0x5f,0x74,0x20,0x74,0x3d,0x67,0x65,0x74,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69, + 0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x3d,0x67,0x65,0x74,0x5f,0x67,0x72,0x6f,0x75,0x70,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20, + 0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x3d,0x67,0x2a,0x28,0x28,0x75,0x69,0x6e,0x74,0x36, + 0x34,0x5f,0x74,0x29,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x74,0x72,0x69,0x64,0x65,0x29,0x2b,0x31,0x32,0x38,0x3b,0x0a,0x7b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62, + 0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x70,0x3d,0x6f,0x75,0x74,0x70,0x75,0x74,0x73,0x2b,0x28,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x6f, + 0x66,0x66,0x73,0x65,0x74,0x2d,0x31,0x32,0x38,0x29,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x3b,0x0a,0x66,0x6f,0x72, + 0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x74,0x3b,0x20,0x69,0x3c,0x31,0x32,0x38,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e, + 0x74,0x33,0x32,0x5f,0x74,0x29,0x3b,0x20,0x69,0x2b,0x3d,0x53,0x41,0x4c,0x53,0x41,0x32,0x30,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x29,0x0a,0x70, + 0x5b,0x69,0x5d,0x3d,0x30,0x3b,0x0a,0x7d,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74, + 0x2a,0x20,0x6b,0x3d,0x6b,0x65,0x79,0x73,0x2b,0x67,0x2a,0x38,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a, + 0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x3d,0x6f,0x75,0x74,0x70,0x75,0x74,0x73,0x2b,0x28,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x2b,0x28, + 0x74,0x2a,0x36,0x34,0x29,0x29,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75, + 0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x3d,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x73, + 0x5b,0x67,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x31,0x3d,0x6b,0x5b,0x30,0x5d,0x3b,0x0a,0x63,0x6f,0x6e, + 0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x32,0x3d,0x6b,0x5b,0x31,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x6a,0x33,0x3d,0x6b,0x5b,0x32,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x34,0x3d,0x6b, + 0x5b,0x33,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x31,0x31,0x3d,0x6b,0x5b,0x34,0x5d,0x3b,0x0a,0x63,0x6f, + 0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x31,0x32,0x3d,0x6b,0x5b,0x35,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e, + 0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x31,0x33,0x3d,0x6b,0x5b,0x36,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a, + 0x31,0x34,0x3d,0x6b,0x5b,0x37,0x5d,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x30,0x3d,0x30,0x78,0x36,0x31,0x37, + 0x30,0x37,0x38,0x36,0x35,0x55,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x35,0x3d,0x30,0x78,0x33,0x33,0x32,0x30, + 0x36,0x34,0x36,0x45,0x55,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x31,0x30,0x3d,0x30,0x78,0x37,0x39,0x36,0x32, + 0x32,0x44,0x33,0x32,0x55,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x31,0x35,0x3d,0x30,0x78,0x36,0x42,0x32,0x30, + 0x36,0x35,0x37,0x34,0x55,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x36,0x3d,0x30,0x3b,0x0a,0x63,0x6f,0x6e,0x73, + 0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x37,0x3d,0x30,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20, + 0x6a,0x38,0x3d,0x30,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x39,0x3d,0x30,0x3b,0x0a,0x66,0x6f,0x72,0x20,0x28, + 0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x74,0x2a,0x36,0x34,0x3b,0x20,0x69,0x3c,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x3b,0x20, + 0x69,0x2b,0x3d,0x53,0x41,0x4c,0x53,0x41,0x32,0x30,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f,0x53,0x49,0x5a,0x45,0x2a,0x36,0x34,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73, + 0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x38,0x5f,0x31,0x3d,0x6a,0x38,0x2b,0x28,0x69,0x2f,0x36,0x34,0x29,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x78,0x30,0x3d,0x6a,0x30,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x31,0x3d,0x6a,0x31,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x78,0x32,0x3d,0x6a,0x32,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x33,0x3d,0x6a,0x33,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x78,0x34,0x3d,0x6a,0x34,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x35,0x3d,0x6a,0x35,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x78,0x36,0x3d,0x6a,0x36,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x37,0x3d,0x6a,0x37,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33, + 0x32,0x5f,0x74,0x20,0x78,0x38,0x3d,0x6a,0x38,0x5f,0x31,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x39,0x3d,0x6a,0x39,0x3b,0x0a,0x75,0x69,0x6e, + 0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x31,0x30,0x3d,0x6a,0x31,0x30,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x31,0x31,0x3d,0x6a,0x31,0x31,0x3b, + 0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x31,0x32,0x3d,0x6a,0x31,0x32,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x31,0x33,0x3d, + 0x6a,0x31,0x33,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x78,0x31,0x34,0x3d,0x6a,0x31,0x34,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20, + 0x78,0x31,0x35,0x3d,0x6a,0x31,0x35,0x3b,0x0a,0x23,0x70,0x72,0x61,0x67,0x6d,0x61,0x20,0x75,0x6e,0x72,0x6f,0x6c,0x6c,0x28,0x35,0x29,0x0a,0x66,0x6f,0x72,0x20,0x28, + 0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6a,0x3d,0x30,0x3b,0x20,0x6a,0x3c,0x31,0x30,0x3b,0x20,0x2b,0x2b,0x6a,0x29,0x0a,0x7b,0x0a,0x78,0x34,0x3d,0x58,0x4f, + 0x52,0x28,0x20,0x78,0x34,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x30,0x2c,0x78,0x31,0x32,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a, + 0x78,0x38,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x38,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x34,0x2c,0x78,0x30,0x29,0x2c,0x39, + 0x29,0x29,0x3b,0x0a,0x78,0x31,0x32,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x32,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x38,0x2c, + 0x78,0x34,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x30,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x30,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53, + 0x28,0x78,0x31,0x32,0x2c,0x78,0x38,0x29,0x2c,0x31,0x38,0x29,0x29,0x3b,0x0a,0x78,0x39,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x39,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45, + 0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x35,0x2c,0x78,0x31,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a,0x78,0x31,0x33,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x33,0x2c,0x52, + 0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x39,0x2c,0x78,0x35,0x29,0x2c,0x39,0x29,0x29,0x3b,0x0a,0x78,0x31,0x3d,0x58,0x4f,0x52,0x28,0x20, + 0x78,0x31,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x33,0x2c,0x78,0x39,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x35,0x3d, + 0x58,0x4f,0x52,0x28,0x20,0x78,0x35,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x31,0x2c,0x78,0x31,0x33,0x29,0x2c,0x31,0x38,0x29, + 0x29,0x3b,0x0a,0x78,0x31,0x34,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x34,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x30,0x2c,0x78, + 0x36,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a,0x78,0x32,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x32,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78, + 0x31,0x34,0x2c,0x78,0x31,0x30,0x29,0x2c,0x39,0x29,0x29,0x3b,0x0a,0x78,0x36,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x36,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50, + 0x4c,0x55,0x53,0x28,0x20,0x78,0x32,0x2c,0x78,0x31,0x34,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x31,0x30,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x30,0x2c,0x52, + 0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x36,0x2c,0x78,0x32,0x29,0x2c,0x31,0x38,0x29,0x29,0x3b,0x0a,0x78,0x33,0x3d,0x58,0x4f,0x52,0x28, + 0x20,0x78,0x33,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x35,0x2c,0x78,0x31,0x31,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a,0x78,0x37, + 0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x37,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x33,0x2c,0x78,0x31,0x35,0x29,0x2c,0x39,0x29, + 0x29,0x3b,0x0a,0x78,0x31,0x31,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x31,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x37,0x2c,0x78, + 0x33,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x31,0x35,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x35,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53, + 0x28,0x78,0x31,0x31,0x2c,0x78,0x37,0x29,0x2c,0x31,0x38,0x29,0x29,0x3b,0x0a,0x78,0x31,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x31,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45, + 0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x30,0x2c,0x78,0x33,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a,0x78,0x32,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x32,0x2c,0x52,0x4f, + 0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x31,0x2c,0x78,0x30,0x29,0x2c,0x39,0x29,0x29,0x3b,0x0a,0x78,0x33,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78, + 0x33,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x32,0x2c,0x78,0x31,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x30,0x3d,0x58, + 0x4f,0x52,0x28,0x20,0x78,0x30,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x33,0x2c,0x78,0x32,0x29,0x2c,0x31,0x38,0x29,0x29,0x3b, + 0x0a,0x78,0x36,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x36,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x35,0x2c,0x78,0x34,0x29,0x2c, + 0x37,0x29,0x29,0x3b,0x0a,0x78,0x37,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x37,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x36,0x2c, + 0x78,0x35,0x29,0x2c,0x39,0x29,0x29,0x3b,0x0a,0x78,0x34,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x34,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28, + 0x20,0x78,0x37,0x2c,0x78,0x36,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x35,0x3d,0x58,0x4f,0x52,0x28,0x20,0x78,0x35,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28, + 0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x34,0x2c,0x78,0x37,0x29,0x2c,0x31,0x38,0x29,0x29,0x3b,0x0a,0x78,0x31,0x31,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x31,0x2c,0x52, + 0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x30,0x2c,0x78,0x39,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a,0x78,0x38,0x3d,0x58,0x4f,0x52,0x28,0x20, + 0x78,0x38,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x31,0x2c,0x78,0x31,0x30,0x29,0x2c,0x39,0x29,0x29,0x3b,0x0a,0x78,0x39,0x3d, + 0x58,0x4f,0x52,0x28,0x20,0x78,0x39,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x38,0x2c,0x78,0x31,0x31,0x29,0x2c,0x31,0x33,0x29, + 0x29,0x3b,0x0a,0x78,0x31,0x30,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x30,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x20,0x78,0x39,0x2c,0x78, + 0x38,0x29,0x2c,0x31,0x38,0x29,0x29,0x3b,0x0a,0x78,0x31,0x32,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x32,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53, + 0x28,0x78,0x31,0x35,0x2c,0x78,0x31,0x34,0x29,0x2c,0x37,0x29,0x29,0x3b,0x0a,0x78,0x31,0x33,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x33,0x2c,0x52,0x4f,0x54,0x41,0x54, + 0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x32,0x2c,0x78,0x31,0x35,0x29,0x2c,0x39,0x29,0x29,0x3b,0x0a,0x78,0x31,0x34,0x3d,0x58,0x4f,0x52,0x28,0x78,0x31,0x34, + 0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x33,0x2c,0x78,0x31,0x32,0x29,0x2c,0x31,0x33,0x29,0x29,0x3b,0x0a,0x78,0x31,0x35,0x3d, + 0x58,0x4f,0x52,0x28,0x78,0x31,0x35,0x2c,0x52,0x4f,0x54,0x41,0x54,0x45,0x28,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x34,0x2c,0x78,0x31,0x33,0x29,0x2c,0x31,0x38,0x29, + 0x29,0x3b,0x0a,0x7d,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x30,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x30,0x2c,0x6a,0x30,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70, + 0x75,0x74,0x5b,0x31,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x2c,0x6a,0x31,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x32,0x5d,0x3d,0x50,0x4c,0x55, + 0x53,0x28,0x78,0x32,0x2c,0x6a,0x32,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x33,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x33,0x2c,0x6a,0x33,0x29,0x3b, + 0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x34,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x34,0x2c,0x6a,0x34,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x35, + 0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x35,0x2c,0x6a,0x35,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x36,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x36, + 0x2c,0x6a,0x36,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x37,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x37,0x2c,0x6a,0x37,0x29,0x3b,0x0a,0x6f,0x75,0x74, + 0x70,0x75,0x74,0x5b,0x38,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x38,0x2c,0x6a,0x38,0x5f,0x31,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x39,0x5d,0x3d, + 0x50,0x4c,0x55,0x53,0x28,0x78,0x39,0x2c,0x6a,0x39,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x31,0x30,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x30, + 0x2c,0x6a,0x31,0x30,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x31,0x31,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x31,0x2c,0x6a,0x31,0x31,0x29,0x3b, + 0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x31,0x32,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x32,0x2c,0x6a,0x31,0x32,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75, + 0x74,0x5b,0x31,0x33,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x33,0x2c,0x6a,0x31,0x33,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x31,0x34,0x5d,0x3d, + 0x50,0x4c,0x55,0x53,0x28,0x78,0x31,0x34,0x2c,0x6a,0x31,0x34,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x5b,0x31,0x35,0x5d,0x3d,0x50,0x4c,0x55,0x53,0x28,0x78, + 0x31,0x35,0x2c,0x6a,0x31,0x35,0x29,0x3b,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74,0x2b,0x3d,0x28,0x53,0x41,0x4c,0x53,0x41,0x32,0x30,0x5f,0x47,0x52,0x4f,0x55,0x50,0x5f, + 0x53,0x49,0x5a,0x45,0x2a,0x36,0x34,0x29,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x3b,0x0a,0x7d,0x0a,0x62,0x61,0x72, + 0x72,0x69,0x65,0x72,0x28,0x43,0x4c,0x4b,0x5f,0x47,0x4c,0x4f,0x42,0x41,0x4c,0x5f,0x4d,0x45,0x4d,0x5f,0x46,0x45,0x4e,0x43,0x45,0x29,0x3b,0x0a,0x69,0x66,0x28,0x74, + 0x3c,0x31,0x36,0x29,0x0a,0x7b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x70,0x3d,0x6f,0x75,0x74,0x70, + 0x75,0x74,0x73,0x2b,0x28,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x2b,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x2b,0x33, + 0x29,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x3b,0x0a,0x70,0x5b,0x74,0x5d,0x3d,0x30,0x3b,0x0a,0x7d,0x0a,0x69,0x66, + 0x28,0x28,0x74,0x3d,0x3d,0x30,0x29,0x26,0x26,0x28,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x26,0x33,0x29,0x29,0x0a,0x6f,0x75,0x74,0x70,0x75,0x74, + 0x73,0x5b,0x28,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74,0x2b,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x29,0x2f,0x73,0x69, + 0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x29,0x5d,0x20,0x26,0x3d,0x20,0x30,0x78,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x55,0x3e,0x3e, + 0x28,0x28,0x34,0x2d,0x28,0x6f,0x75,0x74,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x26,0x33,0x29,0x29,0x3c,0x3c,0x33,0x29,0x3b,0x0a,0x7d,0x0a,0x23,0x64,0x65,0x66, + 0x69,0x6e,0x65,0x20,0x52,0x4f,0x55,0x4e,0x44,0x53,0x20,0x32,0x34,0x20,0x0a,0x23,0x64,0x65,0x66,0x69,0x6e,0x65,0x20,0x52,0x36,0x34,0x28,0x61,0x2c,0x62,0x2c,0x63, + 0x29,0x20,0x28,0x28,0x28,0x61,0x29,0x20,0x3c,0x3c,0x20,0x62,0x29,0x20,0x7c,0x20,0x28,0x28,0x61,0x29,0x20,0x3e,0x3e,0x20,0x63,0x29,0x29,0x0a,0x5f,0x5f,0x63,0x6f, + 0x6e,0x73,0x74,0x61,0x6e,0x74,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x72,0x63,0x5b,0x32,0x5d,0x5b,0x52,0x4f,0x55,0x4e, + 0x44,0x53,0x5d,0x3d,0x7b,0x0a,0x7b,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x38,0x32,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38, + 0x30,0x38,0x41,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x38,0x42,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x30, + 0x30,0x30,0x31,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x38,0x30,0x38,0x31,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x39,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30, + 0x30,0x38,0x41,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x38,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x38,0x30,0x30,0x39,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x30, + 0x30,0x30,0x41,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x38,0x30,0x38,0x42,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x42,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38, + 0x30,0x38,0x39,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x33,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x32,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30, + 0x30,0x38,0x30,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x41,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x41,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x38, + 0x30,0x38,0x31,0x55,0x4c,0x2c,0x0a,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x38,0x30,0x55,0x4c,0x2c,0x30,0x78,0x30,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x55,0x4c,0x2c,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x38,0x30,0x30,0x30,0x38, + 0x30,0x30,0x38,0x55,0x4c,0x7d,0x2c,0x0a,0x7b,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c, + 0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x0a,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55, + 0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x0a,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30, + 0x55,0x4c,0x2c,0x30,0x55,0x4c,0x2c,0x30,0x55,0x4c,0x7d,0x0a,0x7d,0x3b,0x0a,0x5f,0x5f,0x63,0x6f,0x6e,0x73,0x74,0x61,0x6e,0x74,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20, + 0x69,0x6e,0x74,0x20,0x72,0x6f,0x5b,0x32,0x35,0x5d,0x5b,0x32,0x5d,0x3d,0x7b,0x0a,0x7b,0x20,0x30,0x2c,0x36,0x34,0x7d,0x2c,0x7b,0x34,0x34,0x2c,0x32,0x30,0x7d,0x2c, + 0x7b,0x34,0x33,0x2c,0x32,0x31,0x7d,0x2c,0x7b,0x32,0x31,0x2c,0x34,0x33,0x7d,0x2c,0x7b,0x31,0x34,0x2c,0x35,0x30,0x7d,0x2c,0x0a,0x7b,0x20,0x31,0x2c,0x36,0x33,0x7d, + 0x2c,0x7b,0x20,0x36,0x2c,0x35,0x38,0x7d,0x2c,0x7b,0x32,0x35,0x2c,0x33,0x39,0x7d,0x2c,0x7b,0x20,0x38,0x2c,0x35,0x36,0x7d,0x2c,0x7b,0x31,0x38,0x2c,0x34,0x36,0x7d, + 0x2c,0x0a,0x7b,0x36,0x32,0x2c,0x32,0x7d,0x2c,0x7b,0x35,0x35,0x2c,0x39,0x7d,0x2c,0x7b,0x33,0x39,0x2c,0x32,0x35,0x7d,0x2c,0x7b,0x34,0x31,0x2c,0x32,0x33,0x7d,0x2c, + 0x7b,0x20,0x32,0x2c,0x36,0x32,0x7d,0x2c,0x0a,0x7b,0x32,0x38,0x2c,0x33,0x36,0x7d,0x2c,0x7b,0x32,0x30,0x2c,0x34,0x34,0x7d,0x2c,0x7b,0x20,0x33,0x2c,0x36,0x31,0x7d, + 0x2c,0x7b,0x34,0x35,0x2c,0x31,0x39,0x7d,0x2c,0x7b,0x36,0x31,0x2c,0x33,0x7d,0x2c,0x0a,0x7b,0x32,0x37,0x2c,0x33,0x37,0x7d,0x2c,0x7b,0x33,0x36,0x2c,0x32,0x38,0x7d, + 0x2c,0x7b,0x31,0x30,0x2c,0x35,0x34,0x7d,0x2c,0x7b,0x31,0x35,0x2c,0x34,0x39,0x7d,0x2c,0x7b,0x35,0x36,0x2c,0x38,0x7d,0x0a,0x7d,0x3b,0x0a,0x5f,0x5f,0x63,0x6f,0x6e, + 0x73,0x74,0x61,0x6e,0x74,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x61,0x5b,0x32,0x35,0x5d,0x3d,0x7b,0x0a,0x30,0x2c,0x36,0x2c,0x31,0x32,0x2c,0x31, + 0x38,0x2c,0x32,0x34,0x2c,0x0a,0x31,0x2c,0x37,0x2c,0x31,0x33,0x2c,0x31,0x39,0x2c,0x32,0x30,0x2c,0x0a,0x32,0x2c,0x38,0x2c,0x31,0x34,0x2c,0x31,0x35,0x2c,0x32,0x31, + 0x2c,0x0a,0x33,0x2c,0x39,0x2c,0x31,0x30,0x2c,0x31,0x36,0x2c,0x32,0x32,0x2c,0x0a,0x34,0x2c,0x35,0x2c,0x31,0x31,0x2c,0x31,0x37,0x2c,0x32,0x33,0x0a,0x7d,0x3b,0x0a, + 0x5f,0x5f,0x63,0x6f,0x6e,0x73,0x74,0x61,0x6e,0x74,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x62,0x5b,0x32,0x35,0x5d,0x3d,0x7b,0x0a,0x30,0x2c,0x31, + 0x2c,0x32,0x2c,0x33,0x2c,0x34,0x2c,0x0a,0x31,0x2c,0x32,0x2c,0x33,0x2c,0x34,0x2c,0x30,0x2c,0x0a,0x32,0x2c,0x33,0x2c,0x34,0x2c,0x30,0x2c,0x31,0x2c,0x0a,0x33,0x2c, + 0x34,0x2c,0x30,0x2c,0x31,0x2c,0x32,0x2c,0x0a,0x34,0x2c,0x30,0x2c,0x31,0x2c,0x32,0x2c,0x33,0x0a,0x7d,0x3b,0x0a,0x5f,0x5f,0x63,0x6f,0x6e,0x73,0x74,0x61,0x6e,0x74, + 0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x63,0x5b,0x32,0x35,0x5d,0x5b,0x33,0x5d,0x3d,0x7b,0x0a,0x7b,0x20,0x30,0x2c,0x31,0x2c,0x32,0x7d,0x2c,0x7b, + 0x20,0x31,0x2c,0x32,0x2c,0x33,0x7d,0x2c,0x7b,0x20,0x32,0x2c,0x33,0x2c,0x34,0x7d,0x2c,0x7b,0x20,0x33,0x2c,0x34,0x2c,0x30,0x7d,0x2c,0x7b,0x20,0x34,0x2c,0x30,0x2c, + 0x31,0x7d,0x2c,0x0a,0x7b,0x20,0x35,0x2c,0x36,0x2c,0x37,0x7d,0x2c,0x7b,0x20,0x36,0x2c,0x37,0x2c,0x38,0x7d,0x2c,0x7b,0x20,0x37,0x2c,0x38,0x2c,0x39,0x7d,0x2c,0x7b, + 0x20,0x38,0x2c,0x39,0x2c,0x35,0x7d,0x2c,0x7b,0x20,0x39,0x2c,0x35,0x2c,0x36,0x7d,0x2c,0x0a,0x7b,0x31,0x30,0x2c,0x31,0x31,0x2c,0x31,0x32,0x7d,0x2c,0x7b,0x31,0x31, + 0x2c,0x31,0x32,0x2c,0x31,0x33,0x7d,0x2c,0x7b,0x31,0x32,0x2c,0x31,0x33,0x2c,0x31,0x34,0x7d,0x2c,0x7b,0x31,0x33,0x2c,0x31,0x34,0x2c,0x31,0x30,0x7d,0x2c,0x7b,0x31, + 0x34,0x2c,0x31,0x30,0x2c,0x31,0x31,0x7d,0x2c,0x0a,0x7b,0x31,0x35,0x2c,0x31,0x36,0x2c,0x31,0x37,0x7d,0x2c,0x7b,0x31,0x36,0x2c,0x31,0x37,0x2c,0x31,0x38,0x7d,0x2c, + 0x7b,0x31,0x37,0x2c,0x31,0x38,0x2c,0x31,0x39,0x7d,0x2c,0x7b,0x31,0x38,0x2c,0x31,0x39,0x2c,0x31,0x35,0x7d,0x2c,0x7b,0x31,0x39,0x2c,0x31,0x35,0x2c,0x31,0x36,0x7d, + 0x2c,0x0a,0x7b,0x32,0x30,0x2c,0x32,0x31,0x2c,0x32,0x32,0x7d,0x2c,0x7b,0x32,0x31,0x2c,0x32,0x32,0x2c,0x32,0x33,0x7d,0x2c,0x7b,0x32,0x32,0x2c,0x32,0x33,0x2c,0x32, + 0x34,0x7d,0x2c,0x7b,0x32,0x33,0x2c,0x32,0x34,0x2c,0x32,0x30,0x7d,0x2c,0x7b,0x32,0x34,0x2c,0x32,0x30,0x2c,0x32,0x31,0x7d,0x0a,0x7d,0x3b,0x0a,0x5f,0x5f,0x63,0x6f, + 0x6e,0x73,0x74,0x61,0x6e,0x74,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x69,0x6e,0x74,0x20,0x64,0x5b,0x32,0x35,0x5d,0x3d,0x7b,0x0a,0x30,0x2c,0x31,0x2c,0x32,0x2c,0x33, + 0x2c,0x34,0x2c,0x0a,0x31,0x30,0x2c,0x31,0x31,0x2c,0x31,0x32,0x2c,0x31,0x33,0x2c,0x31,0x34,0x2c,0x0a,0x32,0x30,0x2c,0x32,0x31,0x2c,0x32,0x32,0x2c,0x32,0x33,0x2c, + 0x32,0x34,0x2c,0x0a,0x35,0x2c,0x36,0x2c,0x37,0x2c,0x38,0x2c,0x39,0x2c,0x0a,0x31,0x35,0x2c,0x31,0x36,0x2c,0x31,0x37,0x2c,0x31,0x38,0x2c,0x31,0x39,0x0a,0x7d,0x3b, + 0x0a,0x5f,0x5f,0x61,0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x5f,0x5f,0x28,0x28,0x72,0x65,0x71,0x64,0x5f,0x77,0x6f,0x72,0x6b,0x5f,0x67,0x72,0x6f,0x75,0x70,0x5f, + 0x73,0x69,0x7a,0x65,0x28,0x33,0x32,0x2c,0x31,0x2c,0x31,0x29,0x29,0x29,0x0a,0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65,0x6c,0x20,0x76,0x6f,0x69,0x64,0x20,0x73,0x68,0x61, + 0x33,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x69,0x6e,0x70,0x75,0x74,0x73, + 0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x69,0x6e,0x70,0x75,0x74,0x5f, + 0x73,0x69,0x7a,0x65,0x73,0x2c,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x74,0x72,0x69,0x64,0x65,0x2c,0x5f,0x5f,0x67,0x6c, + 0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x68,0x61,0x73,0x68,0x65,0x73,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75, + 0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x74,0x3d,0x67,0x65,0x74,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74, + 0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x3d,0x67,0x65,0x74,0x5f,0x67,0x72,0x6f,0x75,0x70,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x69,0x66,0x28, + 0x74,0x3e,0x3d,0x32,0x35,0x29,0x0a,0x72,0x65,0x74,0x75,0x72,0x6e,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x73,0x3d, + 0x74,0x20,0x25,0x20,0x35,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x69,0x6e,0x70,0x75,0x74,0x5f,0x6f,0x66,0x66,0x73, + 0x65,0x74,0x3d,0x28,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x74,0x72,0x69,0x64,0x65,0x29,0x2a,0x67,0x3b,0x0a,0x5f, + 0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x69,0x6e,0x70,0x75,0x74,0x3d,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61, + 0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x29,0x28,0x69,0x6e,0x70,0x75,0x74,0x73,0x2b,0x69,0x6e,0x70,0x75,0x74,0x5f,0x6f,0x66,0x66,0x73,0x65,0x74, + 0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x3d,0x69,0x6e,0x70, + 0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x73,0x5b,0x67,0x5d,0x2b,0x31,0x3b,0x0a,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20, + 0x41,0x5b,0x32,0x35,0x5d,0x3b,0x0a,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x43,0x5b,0x32,0x35,0x5d,0x3b,0x0a,0x5f, + 0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x44,0x5b,0x32,0x35,0x5d,0x3b,0x0a,0x41,0x5b,0x74,0x5d,0x3d,0x30,0x3b,0x0a,0x63, + 0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x77,0x6f,0x72,0x64,0x73,0x3d,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x2f,0x73, + 0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20, + 0x74,0x61,0x69,0x6c,0x5f,0x73,0x69,0x7a,0x65,0x3d,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x20,0x25,0x20,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69, + 0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78,0x3d,0x30,0x3b,0x0a,0x66, + 0x6f,0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x77,0x6f,0x72,0x64,0x73,0x3b,0x20,0x2b,0x2b,0x69,0x2c,0x2b, + 0x2b,0x69,0x6e,0x70,0x75,0x74,0x29,0x0a,0x7b,0x0a,0x41,0x5b,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78,0x5d,0x20,0x5e,0x3d,0x20,0x2a,0x69,0x6e,0x70,0x75,0x74, + 0x3b,0x0a,0x2b,0x2b,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78,0x3b,0x0a,0x69,0x66,0x28,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78,0x3d,0x3d,0x31,0x37,0x29, + 0x0a,0x7b,0x0a,0x23,0x70,0x72,0x61,0x67,0x6d,0x61,0x20,0x75,0x6e,0x72,0x6f,0x6c,0x6c,0x28,0x52,0x4f,0x55,0x4e,0x44,0x53,0x29,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69, + 0x6e,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x52,0x4f,0x55,0x4e,0x44,0x53,0x3b,0x20,0x2b,0x2b,0x69,0x29,0x0a,0x7b,0x0a,0x43,0x5b,0x74,0x5d,0x3d,0x41,0x5b, + 0x73,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x35,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x31,0x30,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x31,0x35,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x32,0x30, + 0x5d,0x3b,0x0a,0x44,0x5b,0x74,0x5d,0x3d,0x43,0x5b,0x62,0x5b,0x32,0x30,0x2b,0x73,0x5d,0x5d,0x5e,0x52,0x36,0x34,0x28,0x43,0x5b,0x62,0x5b,0x35,0x2b,0x73,0x5d,0x5d, + 0x2c,0x31,0x2c,0x36,0x33,0x29,0x3b,0x0a,0x43,0x5b,0x74,0x5d,0x3d,0x52,0x36,0x34,0x28,0x41,0x5b,0x61,0x5b,0x74,0x5d,0x5d,0x5e,0x44,0x5b,0x62,0x5b,0x74,0x5d,0x5d, + 0x2c,0x72,0x6f,0x5b,0x74,0x5d,0x5b,0x30,0x5d,0x2c,0x72,0x6f,0x5b,0x74,0x5d,0x5b,0x31,0x5d,0x29,0x3b,0x0a,0x41,0x5b,0x64,0x5b,0x74,0x5d,0x5d,0x3d,0x43,0x5b,0x63, + 0x5b,0x74,0x5d,0x5b,0x30,0x5d,0x5d,0x5e,0x28,0x28,0x7e,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x31,0x5d,0x5d,0x29,0x26,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x32,0x5d, + 0x5d,0x29,0x3b,0x0a,0x41,0x5b,0x74,0x5d,0x20,0x5e,0x3d,0x20,0x72,0x63,0x5b,0x28,0x74,0x3d,0x3d,0x30,0x29,0x3f,0x30,0x3a,0x31,0x5d,0x5b,0x69,0x5d,0x3b,0x20,0x0a, + 0x7d,0x0a,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78,0x3d,0x30,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x74,0x61,0x69,0x6c, + 0x3d,0x30,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x70,0x3d,0x28,0x5f, + 0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x29,0x69,0x6e,0x70,0x75,0x74,0x3b,0x0a,0x66,0x6f, + 0x72,0x20,0x28,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x74,0x61,0x69,0x6c,0x5f,0x73,0x69,0x7a,0x65,0x3b,0x20,0x2b,0x2b, + 0x69,0x29,0x0a,0x7b,0x0a,0x74,0x61,0x69,0x6c,0x7c,0x3d,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x70,0x5b,0x69,0x5d,0x29,0x3c,0x3c,0x28,0x69,0x2a, + 0x38,0x29,0x3b,0x0a,0x7d,0x0a,0x41,0x5b,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78,0x5d,0x20,0x5e,0x3d,0x20,0x74,0x61,0x69,0x6c,0x5e,0x28,0x28,0x75,0x69,0x6e, + 0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x28,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x30,0x78,0x30,0x32,0x7c,0x28,0x31,0x3c,0x3c,0x32,0x29,0x29,0x29, + 0x3c,0x3c,0x28,0x74,0x61,0x69,0x6c,0x5f,0x73,0x69,0x7a,0x65,0x2a,0x38,0x29,0x29,0x29,0x3b,0x0a,0x41,0x5b,0x31,0x36,0x5d,0x20,0x5e,0x3d,0x20,0x30,0x78,0x38,0x30, + 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x55,0x4c,0x3b,0x0a,0x23,0x70,0x72,0x61,0x67,0x6d,0x61,0x20,0x75,0x6e,0x72,0x6f,0x6c,0x6c, + 0x28,0x31,0x29,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69,0x6e,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20,0x69,0x3c,0x52,0x4f,0x55,0x4e,0x44,0x53,0x3b,0x20,0x2b,0x2b,0x69,0x29, + 0x0a,0x7b,0x0a,0x43,0x5b,0x74,0x5d,0x3d,0x41,0x5b,0x73,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x35,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x31,0x30,0x5d,0x5e,0x41,0x5b,0x73,0x2b, + 0x31,0x35,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x32,0x30,0x5d,0x3b,0x0a,0x44,0x5b,0x74,0x5d,0x3d,0x43,0x5b,0x62,0x5b,0x32,0x30,0x2b,0x73,0x5d,0x5d,0x5e,0x52,0x36,0x34, + 0x28,0x43,0x5b,0x62,0x5b,0x35,0x2b,0x73,0x5d,0x5d,0x2c,0x31,0x2c,0x36,0x33,0x29,0x3b,0x0a,0x43,0x5b,0x74,0x5d,0x3d,0x52,0x36,0x34,0x28,0x41,0x5b,0x61,0x5b,0x74, + 0x5d,0x5d,0x5e,0x44,0x5b,0x62,0x5b,0x74,0x5d,0x5d,0x2c,0x72,0x6f,0x5b,0x74,0x5d,0x5b,0x30,0x5d,0x2c,0x72,0x6f,0x5b,0x74,0x5d,0x5b,0x31,0x5d,0x29,0x3b,0x0a,0x41, + 0x5b,0x64,0x5b,0x74,0x5d,0x5d,0x3d,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x30,0x5d,0x5d,0x5e,0x28,0x28,0x7e,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x31,0x5d,0x5d,0x29, + 0x26,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x32,0x5d,0x5d,0x29,0x3b,0x0a,0x41,0x5b,0x74,0x5d,0x20,0x5e,0x3d,0x20,0x72,0x63,0x5b,0x28,0x74,0x3d,0x3d,0x30,0x29,0x3f, + 0x30,0x3a,0x31,0x5d,0x5b,0x69,0x5d,0x3b,0x20,0x0a,0x7d,0x0a,0x69,0x66,0x28,0x74,0x3c,0x34,0x29,0x0a,0x7b,0x0a,0x68,0x61,0x73,0x68,0x65,0x73,0x2b,0x3d,0x67,0x2a, + 0x28,0x33,0x32,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x29,0x3b,0x0a,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x74,0x5d, + 0x3d,0x41,0x5b,0x74,0x5d,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x5f,0x5f,0x61,0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x5f,0x5f,0x28,0x28,0x72,0x65,0x71,0x64,0x5f,0x77, + 0x6f,0x72,0x6b,0x5f,0x67,0x72,0x6f,0x75,0x70,0x5f,0x73,0x69,0x7a,0x65,0x28,0x33,0x32,0x2c,0x31,0x2c,0x31,0x29,0x29,0x29,0x0a,0x5f,0x5f,0x6b,0x65,0x72,0x6e,0x65, + 0x6c,0x20,0x76,0x6f,0x69,0x64,0x20,0x73,0x68,0x61,0x33,0x5f,0x69,0x6e,0x69,0x74,0x69,0x61,0x6c,0x28,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x63,0x6f,0x6e, + 0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x38,0x5f,0x74,0x2a,0x20,0x69,0x6e,0x70,0x75,0x74,0x5f,0x64,0x61,0x74,0x61,0x2c,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20, + 0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x2c,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x6e,0x6f,0x6e,0x63,0x65,0x2c,0x5f,0x5f,0x67,0x6c,0x6f,0x62, + 0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x68,0x61,0x73,0x68,0x65,0x73,0x29,0x0a,0x7b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e, + 0x74,0x33,0x32,0x5f,0x74,0x20,0x74,0x3d,0x67,0x65,0x74,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75, + 0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x67,0x3d,0x67,0x65,0x74,0x5f,0x67,0x72,0x6f,0x75,0x70,0x5f,0x69,0x64,0x28,0x30,0x29,0x3b,0x0a,0x69,0x66,0x28,0x74,0x3e, + 0x3d,0x32,0x35,0x29,0x0a,0x72,0x65,0x74,0x75,0x72,0x6e,0x3b,0x0a,0x63,0x6f,0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x73,0x3d,0x74,0x20, + 0x25,0x20,0x35,0x3b,0x0a,0x5f,0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x20,0x69,0x6e,0x70,0x75,0x74,0x3d,0x28,0x5f, + 0x5f,0x67,0x6c,0x6f,0x62,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x2a,0x29,0x28,0x69,0x6e,0x70,0x75,0x74,0x5f,0x64,0x61,0x74,0x61,0x29,0x3b,0x0a, + 0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x41,0x5b,0x32,0x35,0x5d,0x3b,0x0a,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20, + 0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x20,0x43,0x5b,0x32,0x35,0x5d,0x3b,0x0a,0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74, + 0x20,0x44,0x5b,0x32,0x35,0x5d,0x3b,0x0a,0x41,0x5b,0x74,0x5d,0x3d,0x28,0x74,0x3c,0x31,0x36,0x29,0x3f,0x69,0x6e,0x70,0x75,0x74,0x5b,0x74,0x5d,0x3a,0x30,0x3b,0x0a, + 0x5f,0x5f,0x6c,0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x20,0x6e,0x6f,0x6e,0x63,0x65,0x5f,0x70,0x6f,0x73,0x3d,0x28,0x5f,0x5f,0x6c, + 0x6f,0x63,0x61,0x6c,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x2a,0x29,0x28,0x41,0x29,0x2b,0x39,0x3b,0x0a,0x6e,0x6f,0x6e,0x63,0x65,0x2b,0x3d,0x67,0x3b,0x0a, + 0x6e,0x6f,0x6e,0x63,0x65,0x5f,0x70,0x6f,0x73,0x5b,0x30,0x5d,0x3d,0x28,0x6e,0x6f,0x6e,0x63,0x65,0x5f,0x70,0x6f,0x73,0x5b,0x30,0x5d,0x26,0x30,0x78,0x46,0x46,0x46, + 0x46,0x46,0x46,0x55,0x29,0x7c,0x28,0x28,0x6e,0x6f,0x6e,0x63,0x65,0x26,0x30,0x78,0x46,0x46,0x29,0x3c,0x3c,0x32,0x34,0x29,0x3b,0x0a,0x6e,0x6f,0x6e,0x63,0x65,0x5f, + 0x70,0x6f,0x73,0x5b,0x31,0x5d,0x3d,0x28,0x6e,0x6f,0x6e,0x63,0x65,0x5f,0x70,0x6f,0x73,0x5b,0x31,0x5d,0x26,0x30,0x78,0x46,0x46,0x30,0x30,0x30,0x30,0x30,0x30,0x55, + 0x29,0x7c,0x28,0x6e,0x6f,0x6e,0x63,0x65,0x3e,0x3e,0x38,0x29,0x3b,0x0a,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65,0x78, + 0x3d,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a,0x65,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x3b,0x0a,0x63,0x6f, + 0x6e,0x73,0x74,0x20,0x75,0x69,0x6e,0x74,0x33,0x32,0x5f,0x74,0x20,0x74,0x61,0x69,0x6c,0x5f,0x73,0x69,0x7a,0x65,0x3d,0x69,0x6e,0x70,0x75,0x74,0x5f,0x73,0x69,0x7a, + 0x65,0x20,0x25,0x20,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x3b,0x0a,0x41,0x5b,0x77,0x6f,0x72,0x64,0x49,0x6e,0x64,0x65, + 0x78,0x5d,0x20,0x5e,0x3d,0x20,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x28,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f,0x74,0x29,0x28,0x30,0x78,0x30, + 0x32,0x7c,0x28,0x31,0x3c,0x3c,0x32,0x29,0x29,0x29,0x3c,0x3c,0x28,0x74,0x61,0x69,0x6c,0x5f,0x73,0x69,0x7a,0x65,0x2a,0x38,0x29,0x29,0x3b,0x0a,0x41,0x5b,0x31,0x36, + 0x5d,0x20,0x5e,0x3d,0x20,0x30,0x78,0x38,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x55,0x4c,0x3b,0x0a,0x23,0x70,0x72,0x61,0x67, + 0x6d,0x61,0x20,0x75,0x6e,0x72,0x6f,0x6c,0x6c,0x28,0x52,0x4f,0x55,0x4e,0x44,0x53,0x29,0x0a,0x66,0x6f,0x72,0x20,0x28,0x69,0x6e,0x74,0x20,0x69,0x3d,0x30,0x3b,0x20, + 0x69,0x3c,0x52,0x4f,0x55,0x4e,0x44,0x53,0x3b,0x20,0x2b,0x2b,0x69,0x29,0x0a,0x7b,0x0a,0x43,0x5b,0x74,0x5d,0x3d,0x41,0x5b,0x73,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x35, + 0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x31,0x30,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x31,0x35,0x5d,0x5e,0x41,0x5b,0x73,0x2b,0x32,0x30,0x5d,0x3b,0x0a,0x44,0x5b,0x74,0x5d,0x3d, + 0x43,0x5b,0x62,0x5b,0x32,0x30,0x2b,0x73,0x5d,0x5d,0x5e,0x52,0x36,0x34,0x28,0x43,0x5b,0x62,0x5b,0x35,0x2b,0x73,0x5d,0x5d,0x2c,0x31,0x2c,0x36,0x33,0x29,0x3b,0x0a, + 0x43,0x5b,0x74,0x5d,0x3d,0x52,0x36,0x34,0x28,0x41,0x5b,0x61,0x5b,0x74,0x5d,0x5d,0x5e,0x44,0x5b,0x62,0x5b,0x74,0x5d,0x5d,0x2c,0x72,0x6f,0x5b,0x74,0x5d,0x5b,0x30, + 0x5d,0x2c,0x72,0x6f,0x5b,0x74,0x5d,0x5b,0x31,0x5d,0x29,0x3b,0x0a,0x41,0x5b,0x64,0x5b,0x74,0x5d,0x5d,0x3d,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x30,0x5d,0x5d,0x5e, + 0x28,0x28,0x7e,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x31,0x5d,0x5d,0x29,0x26,0x43,0x5b,0x63,0x5b,0x74,0x5d,0x5b,0x32,0x5d,0x5d,0x29,0x3b,0x0a,0x41,0x5b,0x74,0x5d, + 0x20,0x5e,0x3d,0x20,0x72,0x63,0x5b,0x28,0x74,0x3d,0x3d,0x30,0x29,0x3f,0x30,0x3a,0x31,0x5d,0x5b,0x69,0x5d,0x3b,0x20,0x0a,0x7d,0x0a,0x69,0x66,0x28,0x74,0x3c,0x34, + 0x29,0x0a,0x7b,0x0a,0x68,0x61,0x73,0x68,0x65,0x73,0x2b,0x3d,0x67,0x2a,0x28,0x33,0x32,0x2f,0x73,0x69,0x7a,0x65,0x6f,0x66,0x28,0x75,0x69,0x6e,0x74,0x36,0x34,0x5f, + 0x74,0x29,0x29,0x3b,0x0a,0x68,0x61,0x73,0x68,0x65,0x73,0x5b,0x74,0x5d,0x3d,0x41,0x5b,0x74,0x5d,0x3b,0x0a,0x7d,0x0a,0x7d,0x0a,0x00 +}; + +} // namespace xmrig diff --git a/src/backend/opencl/cl/astrobwt/salsa20.cl b/src/backend/opencl/cl/astrobwt/salsa20.cl new file mode 100644 index 00000000..3c8431c7 --- /dev/null +++ b/src/backend/opencl/cl/astrobwt/salsa20.cl @@ -0,0 +1,153 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define ROTATE(v,c) (rotate(v,(uint32_t)c)) +#define XOR(v,w) ((v) ^ (w)) +#define PLUS(v,w) ((v) + (w)) + +__attribute__((reqd_work_group_size(SALSA20_GROUP_SIZE, 1, 1))) +__kernel void Salsa20_XORKeyStream(__global const uint32_t* keys, __global uint32_t* outputs, __global uint32_t* output_sizes, uint32_t output_stride) +{ + const uint32_t t = get_local_id(0); + const uint32_t g = get_group_id(0); + + // Put zeroes in the beginning + const uint64_t output_offset = g * ((uint64_t)output_stride) + 128; + { + __global uint32_t* p = outputs + (output_offset - 128) / sizeof(uint32_t); + for (uint32_t i = t; i < 128 / sizeof(uint32_t); i += SALSA20_GROUP_SIZE) + p[i] = 0; + } + + __global const uint32_t* k = keys + g * 8; + __global uint32_t* output = outputs + (output_offset + (t * 64)) / sizeof(uint32_t); + const uint32_t output_size = output_sizes[g]; + + const uint32_t j1 = k[0]; + const uint32_t j2 = k[1]; + const uint32_t j3 = k[2]; + const uint32_t j4 = k[3]; + const uint32_t j11 = k[4]; + const uint32_t j12 = k[5]; + const uint32_t j13 = k[6]; + const uint32_t j14 = k[7]; + const uint32_t j0 = 0x61707865U; + const uint32_t j5 = 0x3320646EU; + const uint32_t j10 = 0x79622D32U; + const uint32_t j15 = 0x6B206574U; + const uint32_t j6 = 0; + const uint32_t j7 = 0; + const uint32_t j8 = 0; + const uint32_t j9 = 0; + + for (uint32_t i = t * 64; i < output_size; i += SALSA20_GROUP_SIZE * 64) + { + const uint32_t j8_1 = j8 + (i / 64); + + uint32_t x0 = j0; + uint32_t x1 = j1; + uint32_t x2 = j2; + uint32_t x3 = j3; + uint32_t x4 = j4; + uint32_t x5 = j5; + uint32_t x6 = j6; + uint32_t x7 = j7; + uint32_t x8 = j8_1; + uint32_t x9 = j9; + uint32_t x10 = j10; + uint32_t x11 = j11; + uint32_t x12 = j12; + uint32_t x13 = j13; + uint32_t x14 = j14; + uint32_t x15 = j15; + + #pragma unroll(5) + for (uint32_t j = 0; j < 10; ++j) + { + x4 = XOR( x4,ROTATE(PLUS( x0,x12), 7)); + x8 = XOR( x8,ROTATE(PLUS( x4, x0), 9)); + x12 = XOR(x12,ROTATE(PLUS( x8, x4),13)); + x0 = XOR( x0,ROTATE(PLUS(x12, x8),18)); + x9 = XOR( x9,ROTATE(PLUS( x5, x1), 7)); + x13 = XOR(x13,ROTATE(PLUS( x9, x5), 9)); + x1 = XOR( x1,ROTATE(PLUS(x13, x9),13)); + x5 = XOR( x5,ROTATE(PLUS( x1,x13),18)); + x14 = XOR(x14,ROTATE(PLUS(x10, x6), 7)); + x2 = XOR( x2,ROTATE(PLUS(x14,x10), 9)); + x6 = XOR( x6,ROTATE(PLUS( x2,x14),13)); + x10 = XOR(x10,ROTATE(PLUS( x6, x2),18)); + x3 = XOR( x3,ROTATE(PLUS(x15,x11), 7)); + x7 = XOR( x7,ROTATE(PLUS( x3,x15), 9)); + x11 = XOR(x11,ROTATE(PLUS( x7, x3),13)); + x15 = XOR(x15,ROTATE(PLUS(x11, x7),18)); + x1 = XOR( x1,ROTATE(PLUS( x0, x3), 7)); + x2 = XOR( x2,ROTATE(PLUS( x1, x0), 9)); + x3 = XOR( x3,ROTATE(PLUS( x2, x1),13)); + x0 = XOR( x0,ROTATE(PLUS( x3, x2),18)); + x6 = XOR( x6,ROTATE(PLUS( x5, x4), 7)); + x7 = XOR( x7,ROTATE(PLUS( x6, x5), 9)); + x4 = XOR( x4,ROTATE(PLUS( x7, x6),13)); + x5 = XOR( x5,ROTATE(PLUS( x4, x7),18)); + x11 = XOR(x11,ROTATE(PLUS(x10, x9), 7)); + x8 = XOR( x8,ROTATE(PLUS(x11,x10), 9)); + x9 = XOR( x9,ROTATE(PLUS( x8,x11),13)); + x10 = XOR(x10,ROTATE(PLUS( x9, x8),18)); + x12 = XOR(x12,ROTATE(PLUS(x15,x14), 7)); + x13 = XOR(x13,ROTATE(PLUS(x12,x15), 9)); + x14 = XOR(x14,ROTATE(PLUS(x13,x12),13)); + x15 = XOR(x15,ROTATE(PLUS(x14,x13),18)); + } + + output[0] = PLUS(x0, j0); + output[1] = PLUS(x1, j1); + output[2] = PLUS(x2, j2); + output[3] = PLUS(x3, j3); + output[4] = PLUS(x4, j4); + output[5] = PLUS(x5, j5); + output[6] = PLUS(x6, j6); + output[7] = PLUS(x7, j7); + output[8] = PLUS(x8, j8_1); + output[9] = PLUS(x9, j9); + output[10] = PLUS(x10,j10); + output[11] = PLUS(x11,j11); + output[12] = PLUS(x12,j12); + output[13] = PLUS(x13,j13); + output[14] = PLUS(x14,j14); + output[15] = PLUS(x15,j15); + + output += (SALSA20_GROUP_SIZE * 64) / sizeof(uint32_t); + } + + barrier(CLK_GLOBAL_MEM_FENCE); + + // Put zeroes after output's end + if (t < 16) + { + __global uint32_t* p = outputs + (output_offset + output_size + 3) / sizeof(uint32_t); + p[t] = 0; + } + + if ((t == 0) && (output_size & 3)) + outputs[(output_offset + output_size) / sizeof(uint32_t)] &= 0xFFFFFFFFU >> ((4 - (output_size & 3)) << 3); +} diff --git a/src/backend/opencl/cl/astrobwt/sha3.cl b/src/backend/opencl/cl/astrobwt/sha3.cl new file mode 100644 index 00000000..8c2ee24d --- /dev/null +++ b/src/backend/opencl/cl/astrobwt/sha3.cl @@ -0,0 +1,198 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define ROUNDS 24 +#define R64(a,b,c) (((a) << b) | ((a) >> c)) + +__constant const uint64_t rc[2][ROUNDS] = { + {0x0000000000000001UL, 0x0000000000008082UL, 0x800000000000808AUL, + 0x8000000080008000UL, 0x000000000000808BUL, 0x0000000080000001UL, + 0x8000000080008081UL, 0x8000000000008009UL, 0x000000000000008AUL, + 0x0000000000000088UL, 0x0000000080008009UL, 0x000000008000000AUL, + 0x000000008000808BUL, 0x800000000000008BUL, 0x8000000000008089UL, + 0x8000000000008003UL, 0x8000000000008002UL, 0x8000000000000080UL, + 0x000000000000800AUL, 0x800000008000000AUL, 0x8000000080008081UL, + 0x8000000000008080UL, 0x0000000080000001UL, 0x8000000080008008UL}, + {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, + 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, + 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL} +}; + + +__constant const int ro[25][2] = { + { 0,64}, {44,20}, {43,21}, {21,43}, {14,50}, + { 1,63}, { 6,58}, {25,39}, { 8,56}, {18,46}, + {62, 2}, {55, 9}, {39,25}, {41,23}, { 2,62}, + {28,36}, {20,44}, { 3,61}, {45,19}, {61, 3}, + {27,37}, {36,28}, {10,54}, {15,49}, {56, 8} +}; + +__constant const int a[25] = { + 0, 6, 12, 18, 24, + 1, 7, 13, 19, 20, + 2, 8, 14, 15, 21, + 3, 9, 10, 16, 22, + 4, 5, 11, 17, 23 +}; + +__constant const int b[25] = { + 0, 1, 2, 3, 4, + 1, 2, 3, 4, 0, + 2, 3, 4, 0, 1, + 3, 4, 0, 1, 2, + 4, 0, 1, 2, 3 +}; + +__constant const int c[25][3] = { + { 0, 1, 2}, { 1, 2, 3}, { 2, 3, 4}, { 3, 4, 0}, { 4, 0, 1}, + { 5, 6, 7}, { 6, 7, 8}, { 7, 8, 9}, { 8, 9, 5}, { 9, 5, 6}, + {10,11,12}, {11,12,13}, {12,13,14}, {13,14,10}, {14,10,11}, + {15,16,17}, {16,17,18}, {17,18,19}, {18,19,15}, {19,15,16}, + {20,21,22}, {21,22,23}, {22,23,24}, {23,24,20}, {24,20,21} +}; + +__constant const int d[25] = { + 0, 1, 2, 3, 4, + 10, 11, 12, 13, 14, + 20, 21, 22, 23, 24, + 5, 6, 7, 8, 9, + 15, 16, 17, 18, 19 +}; + +__attribute__((reqd_work_group_size(32, 1, 1))) +__kernel void sha3(__global const uint8_t* inputs, __global const uint32_t* input_sizes, uint32_t input_stride, __global uint64_t* hashes) +{ + const uint32_t t = get_local_id(0); + const uint32_t g = get_group_id(0); + + if (t >= 25) + return; + + const uint32_t s = t % 5; + + const uint64_t input_offset = ((uint64_t)input_stride) * g; + __global uint64_t* input = (__global uint64_t*)(inputs + input_offset); + const uint32_t input_size = input_sizes[g] + 1; + + __local uint64_t A[25]; + __local uint64_t C[25]; + __local uint64_t D[25]; + + A[t] = 0; + + const uint32_t words = input_size / sizeof(uint64_t); + const uint32_t tail_size = input_size % sizeof(uint64_t); + + uint32_t wordIndex = 0; + for (uint32_t i = 0; i < words; ++i, ++input) + { + A[wordIndex] ^= *input; + ++wordIndex; + if (wordIndex == 17) + { + #pragma unroll(ROUNDS) + for (int i = 0; i < ROUNDS; ++i) + { + C[t] = A[s] ^ A[s+5] ^ A[s+10] ^ A[s+15] ^ A[s+20]; + D[t] = C[b[20+s]] ^ R64(C[b[5+s]], 1, 63); + C[t] = R64(A[a[t]] ^ D[b[t]], ro[t][0], ro[t][1]); + A[d[t]] = C[c[t][0]] ^ ((~C[c[t][1]]) & C[c[t][2]]); + A[t] ^= rc[(t == 0) ? 0 : 1][i]; + } + wordIndex = 0; + } + } + + uint64_t tail = 0; + __global const uint8_t* p = (__global const uint8_t*)input; + for (uint32_t i = 0; i < tail_size; ++i) + { + tail |= (uint64_t)(p[i]) << (i * 8); + } + A[wordIndex] ^= tail ^ ((uint64_t)(((uint64_t)(0x02 | (1 << 2))) << (tail_size * 8))); + A[16] ^= 0x8000000000000000UL; + + #pragma unroll(1) + for (int i = 0; i < ROUNDS; ++i) + { + C[t] = A[s] ^ A[s+5] ^ A[s+10] ^ A[s+15] ^ A[s+20]; + D[t] = C[b[20+s]] ^ R64(C[b[5+s]], 1, 63); + C[t] = R64(A[a[t]] ^ D[b[t]], ro[t][0], ro[t][1]); + A[d[t]] = C[c[t][0]] ^ ((~C[c[t][1]]) & C[c[t][2]]); + A[t] ^= rc[(t == 0) ? 0 : 1][i]; + } + + if (t < 4) + { + hashes += g * (32 / sizeof(uint64_t)); + hashes[t] = A[t]; + } +} + +__attribute__((reqd_work_group_size(32, 1, 1))) +__kernel void sha3_initial(__global const uint8_t* input_data, uint32_t input_size, uint32_t nonce, __global uint64_t* hashes) +{ + const uint32_t t = get_local_id(0); + const uint32_t g = get_group_id(0); + + if (t >= 25) + return; + + const uint32_t s = t % 5; + + __global uint64_t* input = (__global uint64_t*)(input_data); + + __local uint64_t A[25]; + __local uint64_t C[25]; + __local uint64_t D[25]; + + A[t] = (t < 16) ? input[t] : 0; + + __local uint32_t* nonce_pos = (__local uint32_t*)(A) + 9; + nonce += g; + nonce_pos[0] = (nonce_pos[0] & 0xFFFFFFU) | ((nonce & 0xFF) << 24); + nonce_pos[1] = (nonce_pos[1] & 0xFF000000U) | (nonce >> 8); + + uint32_t wordIndex = input_size / sizeof(uint64_t); + const uint32_t tail_size = input_size % sizeof(uint64_t); + + A[wordIndex] ^= (uint64_t)(((uint64_t)(0x02 | (1 << 2))) << (tail_size * 8)); + A[16] ^= 0x8000000000000000UL; + + #pragma unroll(ROUNDS) + for (int i = 0; i < ROUNDS; ++i) + { + C[t] = A[s] ^ A[s+5] ^ A[s+10] ^ A[s+15] ^ A[s+20]; + D[t] = C[b[20+s]] ^ R64(C[b[5+s]], 1, 63); + C[t] = R64(A[a[t]] ^ D[b[t]], ro[t][0], ro[t][1]); + A[d[t]] = C[c[t][0]] ^ ((~C[c[t][1]]) & C[c[t][2]]); + A[t] ^= rc[(t == 0) ? 0 : 1][i]; + } + + if (t < 4) + { + hashes += g * (32 / sizeof(uint64_t)); + hashes[t] = A[t]; + } +} diff --git a/src/backend/opencl/generators/ocl_generic_astrobwt_generator.cpp b/src/backend/opencl/generators/ocl_generic_astrobwt_generator.cpp new file mode 100644 index 00000000..95dffd5b --- /dev/null +++ b/src/backend/opencl/generators/ocl_generic_astrobwt_generator.cpp @@ -0,0 +1,64 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/OclThreads.h" +#include "backend/opencl/wrappers/OclDevice.h" +#include "base/crypto/Algorithm.h" +#include "crypto/randomx/randomx.h" +#include "crypto/rx/RxAlgo.h" + + +namespace xmrig { + + +bool ocl_generic_astrobwt_generator(const OclDevice &device, const Algorithm &algorithm, OclThreads &threads) +{ + if (algorithm.family() != Algorithm::ASTROBWT) { + return false; + } + + const size_t mem = device.globalMemSize(); + + uint32_t per_thread_mem = 10 << 20; + uint32_t intensity = static_cast((mem - (128 << 20)) / per_thread_mem / 2); + + intensity &= ~63U; + + if (!intensity) { + return false; + } + + if (intensity > 256) { + intensity = 256; + } + + threads.add(OclThread(device.index(), intensity, 2)); + + return true; +} + + +} // namespace xmrig diff --git a/src/backend/opencl/interfaces/IOclRunner.h b/src/backend/opencl/interfaces/IOclRunner.h index 0b47bcd2..74f02c6c 100644 --- a/src/backend/opencl/interfaces/IOclRunner.h +++ b/src/backend/opencl/interfaces/IOclRunner.h @@ -59,6 +59,8 @@ public: virtual const OclLaunchData &data() const = 0; virtual size_t intensity() const = 0; virtual size_t threadId() const = 0; + virtual uint32_t roundSize() const = 0; + virtual uint32_t processedHashes() const = 0; virtual uint32_t deviceIndex() const = 0; virtual void build() = 0; virtual void init() = 0; diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.cpp new file mode 100644 index 00000000..95f65d32 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.cpp @@ -0,0 +1,42 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_FilterKernel::enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size) +{ + enqueueNDRange(queue, 1, nullptr, &threads, &workgroup_size); +} + + +void xmrig::AstroBWT_FilterKernel::setArgs(uint32_t nonce, uint32_t bwt_max_size, cl_mem hashes, cl_mem filtered_hashes) +{ + setArg(0, sizeof(uint32_t), &nonce); + setArg(1, sizeof(uint32_t), &bwt_max_size); + setArg(2, sizeof(cl_mem), &hashes); + setArg(3, sizeof(cl_mem), &filtered_hashes); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.h new file mode 100644 index 00000000..60a748de --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWT_FILTERKERNEL_H +#define XMRIG_ASTROBWT_FILTERKERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_FilterKernel : public OclKernel +{ +public: + inline AstroBWT_FilterKernel(cl_program program) : OclKernel(program, "filter") {} + + void enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size); + void setArgs(uint32_t nonce, uint32_t bwt_max_size, cl_mem hashes, cl_mem filtered_hashes); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWT_FILTERKERNEL_H */ diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.cpp new file mode 100644 index 00000000..4f3d8aee --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.cpp @@ -0,0 +1,47 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_FindSharesKernel::enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size) +{ + enqueueNDRange(queue, 1, nullptr, &threads, &workgroup_size); +} + + +void xmrig::AstroBWT_FindSharesKernel::setArgs(cl_mem hashes, cl_mem filtered_hashes, cl_mem shares) +{ + setArg(0, sizeof(cl_mem), &hashes); + setArg(1, sizeof(cl_mem), &filtered_hashes); + setArg(3, sizeof(cl_mem), &shares); +} + + +void xmrig::AstroBWT_FindSharesKernel::setTarget(uint64_t target) +{ + setArg(2, sizeof(uint64_t), &target); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.h new file mode 100644 index 00000000..6645cc56 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.h @@ -0,0 +1,49 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWTFINDSHARESKERNEL_H +#define XMRIG_ASTROBWTFINDSHARESKERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_FindSharesKernel : public OclKernel +{ +public: + inline AstroBWT_FindSharesKernel(cl_program program) : OclKernel(program, "find_shares") {} + + void enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size); + void setArgs(cl_mem hashes, cl_mem filtered_hashes, cl_mem shares); + void setTarget(uint64_t target); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWTFINDSHARESKERNEL_H */ diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.cpp new file mode 100644 index 00000000..c08562a6 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.cpp @@ -0,0 +1,44 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_MainKernel::enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size) +{ + const size_t gthreads = threads * workgroup_size; + enqueueNDRange(queue, 1, nullptr, >hreads, &workgroup_size); +} + + +void xmrig::AstroBWT_MainKernel::setArgs(cl_mem inputs, cl_mem input_sizes, uint32_t input_stride, cl_mem indices, cl_mem tmp_indices) +{ + setArg(0, sizeof(cl_mem), &inputs); + setArg(1, sizeof(cl_mem), &input_sizes); + setArg(2, sizeof(uint32_t), &input_stride); + setArg(3, sizeof(cl_mem), &indices); + setArg(4, sizeof(cl_mem), &tmp_indices); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.h new file mode 100644 index 00000000..812256eb --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWT_MAINKERNEL_H +#define XMRIG_ASTROBWT_MAINKERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_MainKernel : public OclKernel +{ +public: + inline AstroBWT_MainKernel(cl_program program) : OclKernel(program, "BWT") {} + + void enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size); + void setArgs(cl_mem inputs, cl_mem input_sizes, uint32_t input_stride, cl_mem indices, cl_mem tmp_indices); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWT_MAINKERNEL_H */ diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.cpp new file mode 100644 index 00000000..962e4821 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.cpp @@ -0,0 +1,41 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_PrepareBatch2Kernel::enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size) +{ + enqueueNDRange(queue, 1, nullptr, &threads, &workgroup_size); +} + + +void xmrig::AstroBWT_PrepareBatch2Kernel::setArgs(cl_mem hashes, cl_mem filtered_hashes, cl_mem bwt_data_sizes) +{ + setArg(0, sizeof(cl_mem), &hashes); + setArg(1, sizeof(cl_mem), &filtered_hashes); + setArg(2, sizeof(cl_mem), &bwt_data_sizes); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.h new file mode 100644 index 00000000..8dd1f983 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWT_PREPAREBATCH2KERNEL_H +#define XMRIG_ASTROBWT_PREPAREBATCH2KERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_PrepareBatch2Kernel : public OclKernel +{ +public: + inline AstroBWT_PrepareBatch2Kernel(cl_program program) : OclKernel(program, "prepare_batch2") {} + + void enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size); + void setArgs(cl_mem hashes, cl_mem filtered_hashes, cl_mem bwt_data_sizes); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWT_PREPAREBATCH2KERNEL_H */ diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.cpp new file mode 100644 index 00000000..ac80ae10 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.cpp @@ -0,0 +1,44 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_SHA3InitialKernel::enqueue(cl_command_queue queue, size_t threads) +{ + const size_t workgroup_size = 32; + const size_t gthreads = threads * workgroup_size; + enqueueNDRange(queue, 1, nullptr, >hreads, &workgroup_size); +} + + +void xmrig::AstroBWT_SHA3InitialKernel::setArgs(cl_mem input, uint32_t input_size, uint32_t nonce, cl_mem output_salsa20_keys) +{ + setArg(0, sizeof(cl_mem), &input); + setArg(1, sizeof(uint32_t), &input_size); + setArg(2, sizeof(uint32_t), &nonce); + setArg(3, sizeof(cl_mem), &output_salsa20_keys); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.h new file mode 100644 index 00000000..ad6486c5 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWT_SHA3INITIALKERNEL_H +#define XMRIG_ASTROBWT_SHA3INITIALKERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_SHA3InitialKernel : public OclKernel +{ +public: + inline AstroBWT_SHA3InitialKernel(cl_program program) : OclKernel(program, "sha3_initial") {} + + void enqueue(cl_command_queue queue, size_t threads); + void setArgs(cl_mem input, uint32_t input_size, uint32_t nonce, cl_mem output_salsa20_keys); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWT_SHA3INITIALKERNEL_H */ diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.cpp new file mode 100644 index 00000000..323505cb --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.cpp @@ -0,0 +1,44 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_SHA3Kernel::enqueue(cl_command_queue queue, size_t threads) +{ + const size_t workgroup_size = 32; + const size_t gthreads = threads * workgroup_size; + enqueueNDRange(queue, 1, nullptr, >hreads, &workgroup_size); +} + + +void xmrig::AstroBWT_SHA3Kernel::setArgs(cl_mem inputs, cl_mem input_sizes, uint32_t input_stride, cl_mem output_salsa20_keys) +{ + setArg(0, sizeof(cl_mem), &inputs); + setArg(1, sizeof(cl_mem), &input_sizes); + setArg(2, sizeof(uint32_t), &input_stride); + setArg(3, sizeof(cl_mem), &output_salsa20_keys); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.h new file mode 100644 index 00000000..62dc1a1a --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWT_SHA3KERNEL_H +#define XMRIG_ASTROBWT_SHA3KERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_SHA3Kernel : public OclKernel +{ +public: + inline AstroBWT_SHA3Kernel(cl_program program) : OclKernel(program, "sha3") {} + + void enqueue(cl_command_queue queue, size_t threads); + void setArgs(cl_mem inputs, cl_mem input_sizes, uint32_t input_stride, cl_mem output_salsa20_keys); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWT_SHA3KERNEL_H */ diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.cpp b/src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.cpp new file mode 100644 index 00000000..8a0ae9ae --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.cpp @@ -0,0 +1,43 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.h" +#include "backend/opencl/wrappers/OclLib.h" + + +void xmrig::AstroBWT_Salsa20Kernel::enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size) +{ + const size_t gthreads = threads * workgroup_size; + enqueueNDRange(queue, 1, nullptr, >hreads, &workgroup_size); +} + + +void xmrig::AstroBWT_Salsa20Kernel::setArgs(cl_mem salsa20_keys, cl_mem outputs, cl_mem output_sizes, uint32_t output_stride) +{ + setArg(0, sizeof(cl_mem), &salsa20_keys); + setArg(1, sizeof(cl_mem), &outputs); + setArg(2, sizeof(cl_mem), &output_sizes); + setArg(3, sizeof(uint32_t), &output_stride); +} diff --git a/src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.h b/src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.h new file mode 100644 index 00000000..945b1fd6 --- /dev/null +++ b/src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ASTROBWT_SALSA20KERNEL_H +#define XMRIG_ASTROBWT_SALSA20KERNEL_H + + +#include "backend/opencl/wrappers/OclKernel.h" + + +namespace xmrig { + + +class AstroBWT_Salsa20Kernel : public OclKernel +{ +public: + inline AstroBWT_Salsa20Kernel(cl_program program) : OclKernel(program, "Salsa20_XORKeyStream") {} + + void enqueue(cl_command_queue queue, size_t threads, size_t workgroup_size); + void setArgs(cl_mem salsa20_keys, cl_mem outputs, cl_mem output_sizes, uint32_t output_stride); +}; + + +} // namespace xmrig + + +#endif /* XMRIG_ASTROBWT_SALSA20KERNEL_H */ diff --git a/src/backend/opencl/opencl.cmake b/src/backend/opencl/opencl.cmake index 535e1a73..ebee4673 100644 --- a/src/backend/opencl/opencl.cmake +++ b/src/backend/opencl/opencl.cmake @@ -100,6 +100,31 @@ if (WITH_OPENCL) ) endif() + if (WITH_ASTROBWT) + list(APPEND HEADERS_BACKEND_OPENCL + src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.h + src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.h + src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.h + src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.h + src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.h + src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.h + src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.h + src/backend/opencl/runners/OclAstroBWTRunner.h + ) + + list(APPEND SOURCES_BACKEND_OPENCL + src/backend/opencl/generators/ocl_generic_astrobwt_generator.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.cpp + src/backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.cpp + src/backend/opencl/runners/OclAstroBWTRunner.cpp + ) + endif() + if (WITH_CN_GPU AND CMAKE_SIZEOF_VOID_P EQUAL 8) list(APPEND HEADERS_BACKEND_OPENCL src/backend/opencl/kernels/Cn00RyoKernel.h diff --git a/src/backend/opencl/runners/OclAstroBWTRunner.cpp b/src/backend/opencl/runners/OclAstroBWTRunner.cpp new file mode 100644 index 00000000..ed28387d --- /dev/null +++ b/src/backend/opencl/runners/OclAstroBWTRunner.cpp @@ -0,0 +1,218 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "backend/opencl/runners/OclAstroBWTRunner.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_FilterKernel.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_FindSharesKernel.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_MainKernel.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_PrepareBatch2Kernel.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_Salsa20Kernel.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_SHA3InitialKernel.h" +#include "backend/opencl/kernels/astrobwt/AstroBWT_SHA3Kernel.h" +#include "backend/opencl/OclLaunchData.h" +#include "backend/opencl/wrappers/OclLib.h" +#include "base/io/log/Log.h" +#include "base/net/stratum/Job.h" + + +namespace xmrig { + + +constexpr int STAGE1_SIZE = 147253; +constexpr uint32_t STAGE1_DATA_STRIDE = (STAGE1_SIZE + 256 + 255) & ~255U; +constexpr uint32_t OclAstroBWTRunner::BWT_DATA_STRIDE; + + +} // namespace xmrig + + +xmrig::OclAstroBWTRunner::OclAstroBWTRunner(size_t index, const OclLaunchData &data) : OclBaseRunner(index, data) +{ + switch (data.device.type()) + { + case OclDevice::Baffin: + case OclDevice::Polaris: + case OclDevice::Lexa: + case OclDevice::Vega_10: + case OclDevice::Vega_20: + case OclDevice::Raven: + m_workgroup_size = 64; + break; + + default: + m_workgroup_size = 32; + break; + } + + m_options += " -DSALSA20_GROUP_SIZE=" + std::to_string(m_workgroup_size); + m_options += " -DBWT_GROUP_SIZE=" + std::to_string(m_workgroup_size); + + m_bwt_allocation_size = static_cast(m_intensity) * BWT_DATA_STRIDE; + m_batch_size1 = static_cast(m_bwt_allocation_size / STAGE1_DATA_STRIDE) & ~255U; + + m_bwt_data_sizes_host = new uint32_t[m_batch_size1]; +} + + +xmrig::OclAstroBWTRunner::~OclAstroBWTRunner() +{ + delete m_sha3_initial_kernel; + delete m_sha3_kernel; + delete m_salsa20_kernel; + delete m_bwt_kernel; + delete m_filter_kernel; + delete m_prepare_batch2_kernel; + delete m_find_shares_kernel; + + OclLib::release(m_salsa20_keys); + OclLib::release(m_bwt_data); + OclLib::release(m_bwt_data_sizes); + OclLib::release(m_indices); + OclLib::release(m_tmp_indices); + OclLib::release(m_filtered_hashes); + + delete m_bwt_data_sizes_host; +} + + +size_t xmrig::OclAstroBWTRunner::bufferSize() const +{ + return OclBaseRunner::bufferSize() + + align(m_batch_size1 * 32) + // m_salsa20_keys + align(m_bwt_allocation_size) + // m_bwt_data + align(m_batch_size1 * 4) + // m_bwt_data_sizes + align(m_bwt_allocation_size * 8) + // m_indices + align(m_bwt_allocation_size * 8) + // m_tmp_indices + align((m_batch_size1 + m_intensity) * 36 + 4); // m_filtered_hashes +} + + +void xmrig::OclAstroBWTRunner::run(uint32_t nonce, uint32_t *hashOutput) +{ + m_sha3_initial_kernel->setArg(2, sizeof(nonce), &nonce); + m_salsa20_kernel->setArg(3, sizeof(STAGE1_DATA_STRIDE), &STAGE1_DATA_STRIDE); + m_bwt_kernel->setArg(2, sizeof(STAGE1_DATA_STRIDE), &STAGE1_DATA_STRIDE); + + const uint32_t t = STAGE1_DATA_STRIDE * 8; + m_sha3_kernel->setArg(2, sizeof(t), &t); + m_filter_kernel->setArg(0, sizeof(nonce), &nonce); + + const uint32_t zero = 0; + enqueueWriteBuffer(m_output, CL_FALSE, sizeof(cl_uint) * 0xFF, sizeof(uint32_t), &zero); + + m_sha3_initial_kernel->enqueue(m_queue, m_batch_size1); + + for (uint32_t i = 0; i < m_batch_size1; ++i) + m_bwt_data_sizes_host[i] = STAGE1_SIZE; + + enqueueWriteBuffer(m_bwt_data_sizes, CL_FALSE, 0, m_batch_size1 * sizeof(uint32_t), m_bwt_data_sizes_host); + + m_salsa20_kernel->enqueue(m_queue, m_batch_size1, m_workgroup_size); + m_bwt_kernel->enqueue(m_queue, m_batch_size1, m_workgroup_size); + m_sha3_kernel->enqueue(m_queue, m_batch_size1); + m_filter_kernel->enqueue(m_queue, m_batch_size1, m_workgroup_size); + + uint32_t num_filtered_hashes = 0; + enqueueReadBuffer(m_filtered_hashes, CL_TRUE, 0, sizeof(num_filtered_hashes), &num_filtered_hashes); + + m_processedHashes = 0; + while (num_filtered_hashes >= m_intensity) + { + num_filtered_hashes -= m_intensity; + m_processedHashes += m_intensity; + + m_salsa20_kernel->setArg(3, sizeof(BWT_DATA_STRIDE), &BWT_DATA_STRIDE); + m_bwt_kernel->setArg(2, sizeof(BWT_DATA_STRIDE), &BWT_DATA_STRIDE); + + const uint32_t t = BWT_DATA_STRIDE * 8; + m_sha3_kernel->setArg(2, sizeof(t), &t); + + m_prepare_batch2_kernel->enqueue(m_queue, m_intensity, m_workgroup_size); + m_salsa20_kernel->enqueue(m_queue, m_intensity, m_workgroup_size); + m_bwt_kernel->enqueue(m_queue, m_intensity, m_workgroup_size); + m_sha3_kernel->enqueue(m_queue, m_intensity); + + m_find_shares_kernel->enqueue(m_queue, m_intensity, m_workgroup_size); + + finalize(hashOutput); + + OclLib::finish(m_queue); + } +} + + +void xmrig::OclAstroBWTRunner::set(const Job &job, uint8_t *blob) +{ + if (job.size() > (Job::kMaxBlobSize - 4)) { + throw std::length_error("job size too big"); + } + + if (job.size() < Job::kMaxBlobSize) { + memset(blob + job.size(), 0, Job::kMaxBlobSize - job.size()); + } + + enqueueWriteBuffer(m_input, CL_TRUE, 0, Job::kMaxBlobSize, blob); + + m_sha3_initial_kernel->setArgs(m_input, static_cast(job.size()), *job.nonce(), m_salsa20_keys); + m_salsa20_kernel->setArgs(m_salsa20_keys, m_bwt_data, m_bwt_data_sizes, STAGE1_DATA_STRIDE); + m_bwt_kernel->setArgs(m_bwt_data, m_bwt_data_sizes, STAGE1_DATA_STRIDE, m_indices, m_tmp_indices); + m_sha3_kernel->setArgs(m_tmp_indices, m_bwt_data_sizes, STAGE1_DATA_STRIDE * 8, m_salsa20_keys); + m_filter_kernel->setArgs(*job.nonce(), BWT_DATA_MAX_SIZE, m_salsa20_keys, m_filtered_hashes); + m_prepare_batch2_kernel->setArgs(m_salsa20_keys, m_filtered_hashes, m_bwt_data_sizes); + m_find_shares_kernel->setArgs(m_salsa20_keys, m_filtered_hashes, m_output); + m_find_shares_kernel->setTarget(job.target()); + + const uint32_t zero = 0; + enqueueWriteBuffer(m_filtered_hashes, CL_TRUE, 0, sizeof(uint32_t), &zero); +} + + +void xmrig::OclAstroBWTRunner::build() +{ + OclBaseRunner::build(); + + m_sha3_initial_kernel = new AstroBWT_SHA3InitialKernel(m_program); + m_sha3_kernel = new AstroBWT_SHA3Kernel(m_program); + m_salsa20_kernel = new AstroBWT_Salsa20Kernel(m_program); + m_bwt_kernel = new AstroBWT_MainKernel(m_program); + m_filter_kernel = new AstroBWT_FilterKernel(m_program); + m_prepare_batch2_kernel = new AstroBWT_PrepareBatch2Kernel(m_program); + m_find_shares_kernel = new AstroBWT_FindSharesKernel(m_program); +} + + +void xmrig::OclAstroBWTRunner::init() +{ + OclBaseRunner::init(); + + const cl_mem_flags f = CL_MEM_READ_WRITE | CL_MEM_HOST_NO_ACCESS; + + m_salsa20_keys = createSubBuffer(f, m_batch_size1 * 32); + m_bwt_data = createSubBuffer(f, m_bwt_allocation_size); + m_bwt_data_sizes = createSubBuffer(CL_MEM_READ_WRITE | CL_MEM_HOST_WRITE_ONLY, m_batch_size1 * 4); + m_indices = createSubBuffer(f, m_bwt_allocation_size * 8); + m_tmp_indices = createSubBuffer(f, m_bwt_allocation_size * 8); + m_filtered_hashes = createSubBuffer(CL_MEM_READ_WRITE, (m_batch_size1 + m_intensity) * 36 + 4); +} diff --git a/src/backend/opencl/runners/OclAstroBWTRunner.h b/src/backend/opencl/runners/OclAstroBWTRunner.h new file mode 100644 index 00000000..f16e0001 --- /dev/null +++ b/src/backend/opencl/runners/OclAstroBWTRunner.h @@ -0,0 +1,95 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_OCLASTROBWTRUNNER_H +#define XMRIG_OCLASTROBWTRUNNER_H + + +#include "backend/opencl/runners/OclBaseRunner.h" + +namespace xmrig { + + +class AstroBWT_FilterKernel; +class AstroBWT_MainKernel; +class AstroBWT_PrepareBatch2Kernel; +class AstroBWT_Salsa20Kernel; +class AstroBWT_SHA3InitialKernel; +class AstroBWT_SHA3Kernel; +class AstroBWT_FindSharesKernel; + + +class OclAstroBWTRunner : public OclBaseRunner +{ +public: + static constexpr uint32_t BWT_DATA_MAX_SIZE = 600 * 1024 - 256; + static constexpr uint32_t BWT_DATA_STRIDE = (BWT_DATA_MAX_SIZE + 256 + 255) & ~255U; + + XMRIG_DISABLE_COPY_MOVE_DEFAULT(OclAstroBWTRunner) + + OclAstroBWTRunner(size_t index, const OclLaunchData &data); + ~OclAstroBWTRunner() override; + + inline uint32_t roundSize() const override { return m_batch_size1; } + + // ~0.5% of all hashes are invalid + inline uint32_t processedHashes() const override { return static_cast(m_processedHashes * 0.995); } + +protected: + size_t bufferSize() const override; + void run(uint32_t nonce, uint32_t *hashOutput) override; + void set(const Job &job, uint8_t *blob) override; + void build() override; + void init() override; + +private: + AstroBWT_SHA3InitialKernel* m_sha3_initial_kernel = nullptr; + AstroBWT_SHA3Kernel* m_sha3_kernel = nullptr; + AstroBWT_Salsa20Kernel* m_salsa20_kernel = nullptr; + AstroBWT_MainKernel* m_bwt_kernel = nullptr; + AstroBWT_FilterKernel* m_filter_kernel = nullptr; + AstroBWT_PrepareBatch2Kernel* m_prepare_batch2_kernel = nullptr; + AstroBWT_FindSharesKernel* m_find_shares_kernel = nullptr; + + + cl_mem m_salsa20_keys = nullptr; + cl_mem m_bwt_data = nullptr; + cl_mem m_bwt_data_sizes = nullptr; + cl_mem m_indices = nullptr; + cl_mem m_tmp_indices = nullptr; + cl_mem m_filtered_hashes = nullptr; + + uint32_t m_workgroup_size = 0; + uint64_t m_bwt_allocation_size = 0; + uint64_t m_batch_size1 = 0; + uint32_t m_processedHashes = 0; + + uint32_t* m_bwt_data_sizes_host = nullptr; +}; + + +} /* namespace xmrig */ + + +#endif // XMRIG_OCLASTROBWTRUNNER_H diff --git a/src/backend/opencl/runners/OclBaseRunner.h b/src/backend/opencl/runners/OclBaseRunner.h index 68a20f25..db9f69fd 100644 --- a/src/backend/opencl/runners/OclBaseRunner.h +++ b/src/backend/opencl/runners/OclBaseRunner.h @@ -57,6 +57,8 @@ protected: inline const OclLaunchData &data() const override { return m_data; } inline size_t intensity() const override { return m_intensity; } inline size_t threadId() const override { return m_threadId; } + inline uint32_t roundSize() const override { return m_intensity; } + inline uint32_t processedHashes() const override { return m_intensity; } size_t bufferSize() const override; uint32_t deviceIndex() const override; diff --git a/src/backend/opencl/wrappers/OclDevice.cpp b/src/backend/opencl/wrappers/OclDevice.cpp index bd51d0f3..cd90e95b 100644 --- a/src/backend/opencl/wrappers/OclDevice.cpp +++ b/src/backend/opencl/wrappers/OclDevice.cpp @@ -52,6 +52,10 @@ namespace xmrig { extern bool ocl_generic_rx_generator(const OclDevice &device, const Algorithm &algorithm, OclThreads &threads); #endif +#ifdef XMRIG_ALGO_ASTROBWT +extern bool ocl_generic_astrobwt_generator(const OclDevice& device, const Algorithm& algorithm, OclThreads& threads); +#endif + #ifdef XMRIG_ALGO_CN_GPU extern bool ocl_generic_cn_gpu_generator(const OclDevice &device, const Algorithm &algorithm, OclThreads &threads); #endif @@ -64,6 +68,9 @@ static ocl_gen_config_fun generators[] = { # ifdef XMRIG_ALGO_RANDOMX ocl_generic_rx_generator, # endif +# ifdef XMRIG_ALGO_ASTROBWT + ocl_generic_astrobwt_generator, +# endif # ifdef XMRIG_ALGO_CN_GPU ocl_generic_cn_gpu_generator, # endif diff --git a/src/base/api/Httpd.cpp b/src/base/api/Httpd.cpp index 37db1ad6..1aba8796 100644 --- a/src/base/api/Httpd.cpp +++ b/src/base/api/Httpd.cpp @@ -29,12 +29,18 @@ #include "base/io/log/Log.h" #include "base/net/http/HttpApiResponse.h" #include "base/net/http/HttpData.h" -#include "base/net/http/HttpServer.h" #include "base/net/tools/TcpServer.h" #include "core/config/Config.h" #include "core/Controller.h" +#ifdef XMRIG_FEATURE_TLS +# include "base/net/https/HttpsServer.h" +#else +# include "base/net/http/HttpServer.h" +#endif + + namespace xmrig { static const char *kAuthorization = "authorization"; @@ -48,10 +54,7 @@ static size_t faviconSize = 0; xmrig::Httpd::Httpd(Base *base) : - m_base(base), - m_http(nullptr), - m_server(nullptr), - m_port(0) + m_base(base) { m_httpListener = std::make_shared(this); @@ -64,18 +67,27 @@ xmrig::Httpd::~Httpd() = default; bool xmrig::Httpd::start() { - const Http &config = m_base->config()->http(); + const auto &config = m_base->config()->http(); if (!config.isEnabled()) { return true; } - m_http = new HttpServer(m_httpListener); + bool tls = false; + +# ifdef XMRIG_FEATURE_TLS + m_http = new HttpsServer(m_httpListener); + tls = m_http->setTls(m_base->config()->tls()); +# else + m_http = new HttpServer(m_httpListener); +# endif + m_server = new TcpServer(config.host(), config.port(), m_http); const int rc = m_server->bind(); - Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") BLUE_BOLD("%s:%d") " " RED_BOLD("%s"), + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CSI "1;%dm%s:%d" " " RED_BOLD("%s"), "HTTP API", + tls ? 32 : 36, config.host().data(), rc < 0 ? config.port() : rc, rc < 0 ? uv_strerror(rc) : "" @@ -143,7 +155,7 @@ void xmrig::Httpd::onHttpData(const HttpData &data) } # endif - return HttpResponse(data.id(), 404).end(); + return HttpResponse(data.id(), HTTP_STATUS_NOT_FOUND).end(); } if (data.method > 4) { diff --git a/src/base/api/Httpd.h b/src/base/api/Httpd.h index 25d88577..462a0671 100644 --- a/src/base/api/Httpd.h +++ b/src/base/api/Httpd.h @@ -40,6 +40,7 @@ namespace xmrig { class Base; class HttpServer; +class HttpsServer; class TcpServer; @@ -61,11 +62,16 @@ protected: private: int auth(const HttpData &req) const; - Base *m_base; - HttpServer *m_http; + const Base *m_base; std::shared_ptr m_httpListener; - TcpServer *m_server; - uint16_t m_port; + TcpServer *m_server = nullptr; + uint16_t m_port = 0; + +# ifdef XMRIG_FEATURE_TLS + HttpsServer *m_http = nullptr; +# else + HttpServer *m_http = nullptr; +# endif }; diff --git a/src/base/base.cmake b/src/base/base.cmake index f2a14381..5116da06 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -53,7 +53,9 @@ set(HEADERS_BASE src/base/net/stratum/strategies/StrategyProxy.h src/base/net/stratum/SubmitResult.h src/base/net/stratum/Url.h - src/base/net/tools/RecvBuf.h + src/base/net/tools/LineReader.h + src/base/net/tools/MemPool.h + src/base/net/tools/NetBuffer.h src/base/net/tools/Storage.h src/base/tools/Arguments.h src/base/tools/Baton.h @@ -99,6 +101,8 @@ set(SOURCES_BASE src/base/net/stratum/strategies/FailoverStrategy.cpp src/base/net/stratum/strategies/SinglePoolStrategy.cpp src/base/net/stratum/Url.cpp + src/base/net/tools/LineReader.cpp + src/base/net/tools/NetBuffer.cpp src/base/tools/Arguments.cpp src/base/tools/Buffer.cpp src/base/tools/String.cpp @@ -157,7 +161,6 @@ if (WITH_HTTP) src/base/net/http/HttpContext.h src/base/net/http/HttpData.h src/base/net/http/HttpResponse.h - src/base/net/http/HttpServer.h src/base/net/stratum/DaemonClient.h src/base/net/stratum/SelfSelectClient.h src/base/net/tools/TcpServer.h @@ -176,7 +179,6 @@ if (WITH_HTTP) src/base/net/http/HttpData.cpp src/base/net/http/HttpListener.cpp src/base/net/http/HttpResponse.cpp - src/base/net/http/HttpServer.cpp src/base/net/stratum/DaemonClient.cpp src/base/net/stratum/SelfSelectClient.cpp src/base/net/tools/TcpServer.cpp diff --git a/src/base/kernel/Base.cpp b/src/base/kernel/Base.cpp index 42d27467..34f6dbfd 100644 --- a/src/base/kernel/Base.cpp +++ b/src/base/kernel/Base.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,16 +27,17 @@ #include +#include "base/kernel/Base.h" #include "base/io/json/Json.h" #include "base/io/json/JsonChain.h" #include "base/io/log/backends/ConsoleLog.h" #include "base/io/log/backends/FileLog.h" #include "base/io/log/Log.h" #include "base/io/Watcher.h" -#include "base/kernel/Base.h" #include "base/kernel/interfaces/IBaseListener.h" #include "base/kernel/Platform.h" #include "base/kernel/Process.h" +#include "base/net/tools/NetBuffer.h" #include "core/config/Config.h" #include "core/config/ConfigTransform.h" @@ -84,6 +85,8 @@ public: delete config; delete watcher; + + NetBuffer::destroy(); } @@ -127,7 +130,7 @@ private: return config.release(); } - chain.addFile(Process::location(Process::ExeLocation, "config.json")); + chain.addFile(Process::location(Process::DataLocation, "config.json")); if (read(chain, config)) { return config.release(); diff --git a/src/base/kernel/Base.h b/src/base/kernel/Base.h index ef850edd..756b6caf 100644 --- a/src/base/kernel/Base.h +++ b/src/base/kernel/Base.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/kernel/Env.cpp b/src/base/kernel/Env.cpp index 3498f2ec..9b018e6e 100644 --- a/src/base/kernel/Env.cpp +++ b/src/base/kernel/Env.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -56,9 +56,13 @@ static std::map variables; static void createVariables() { - variables.insert({ "XMRIG_VERSION", APP_VERSION }); - variables.insert({ "XMRIG_EXE_DIR", Process::location(Process::ExeLocation, "") }); - variables.insert({ "XMRIG_CWD", Process::location(Process::CwdLocation, "") }); + variables.insert({ "XMRIG_VERSION", APP_VERSION }); + variables.insert({ "XMRIG_EXE", Process::exepath() }); + variables.insert({ "XMRIG_EXE_DIR", Process::location(Process::ExeLocation) }); + variables.insert({ "XMRIG_CWD", Process::location(Process::CwdLocation) }); + variables.insert({ "XMRIG_HOME_DIR", Process::location(Process::HomeLocation) }); + variables.insert({ "XMRIG_TEMP_DIR", Process::location(Process::TempLocation) }); + variables.insert({ "XMRIG_DATA_DIR", Process::location(Process::DataLocation) }); } #endif diff --git a/src/base/kernel/Env.h b/src/base/kernel/Env.h index 2f33abf9..dd06297d 100644 --- a/src/base/kernel/Env.h +++ b/src/base/kernel/Env.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/kernel/Process.cpp b/src/base/kernel/Process.cpp index 4fc3ea3a..34f5d02b 100644 --- a/src/base/kernel/Process.cpp +++ b/src/base/kernel/Process.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,7 @@ #include +#include #include @@ -34,20 +35,75 @@ namespace xmrig { -static size_t getLocation(Process::Location location, char *buf, size_t max) -{ - using namespace xmrig; +static char pathBuf[520]; +static std::string dataDir; + + +static std::string getPath(Process::Location location) +{ + size_t size = sizeof(pathBuf); + + if (location == Process::DataLocation) { + if (!dataDir.empty()) { + return dataDir; + } + + location = Process::ExeLocation; + } + + if (location == Process::HomeLocation) { +# if UV_VERSION_HEX >= 0x010600 + return uv_os_homedir(pathBuf, &size) < 0 ? "" : std::string(pathBuf, size); +# else + location = Process::ExeLocation; +# endif + } + + if (location == Process::TempLocation) { +# if UV_VERSION_HEX >= 0x010900 + return uv_os_tmpdir(pathBuf, &size) < 0 ? "" : std::string(pathBuf, size); +# else + location = Process::ExeLocation; +# endif + } - size_t size = max; if (location == Process::ExeLocation) { - return uv_exepath(buf, &size) < 0 ? 0 : size; + if (uv_exepath(pathBuf, &size) < 0) { + return {}; + } + + const auto path = std::string(pathBuf, size); + const auto pos = path.rfind(Process::kDirSeparator); + + if (pos != std::string::npos) { + return path.substr(0, pos); + } + + return path; } if (location == Process::CwdLocation) { - return uv_cwd(buf, &size) < 0 ? 0 : size; + return uv_cwd(pathBuf, &size) < 0 ? "" : std::string(pathBuf, size); } - return 0; + return {}; +} + + +static void setDataDir(const char *path) +{ + if (path == nullptr) { + return; + } + + std::string dir = path; + if (!dir.empty() && (dir.back() == '/' || dir.back() == '\\')) { + dir.pop_back(); + } + + if (!dir.empty() && uv_chdir(dir.c_str()) == 0) { + dataDir = dir; + } } @@ -58,46 +114,25 @@ xmrig::Process::Process(int argc, char **argv) : m_arguments(argc, argv) { srand(static_cast(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast(this))); + + setDataDir(m_arguments.value("--data-dir")); +} + + +xmrig::String xmrig::Process::exepath() +{ + size_t size = sizeof(pathBuf); + + return uv_exepath(pathBuf, &size) < 0 ? "" : String(pathBuf, size); } xmrig::String xmrig::Process::location(Location location, const char *fileName) { - constexpr const size_t max = 520; - - char *buf = new char[max](); - size_t size = getLocation(location, buf, max); - - if (size == 0) { - delete [] buf; - - return String(); + auto path = getPath(location); + if (path.empty() || fileName == nullptr) { + return path.c_str(); } - if (fileName == nullptr) { - return buf; - } - - if (location == ExeLocation) { - char *p = strrchr(buf, kDirSeparator); - - if (p == nullptr) { - delete [] buf; - - return String(); - } - - size = static_cast(p - buf); - } - - if ((size + strlen(fileName) + 2) >= max) { - delete [] buf; - - return String(); - } - - buf[size] = kDirSeparator; - strcpy(buf + size + 1, fileName); - - return buf; + return (path + kDirSeparator + fileName).c_str(); } diff --git a/src/base/kernel/Process.h b/src/base/kernel/Process.h index 12cfc904..29f247f2 100644 --- a/src/base/kernel/Process.h +++ b/src/base/kernel/Process.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,7 +37,10 @@ class Process public: enum Location { ExeLocation, - CwdLocation + CwdLocation, + DataLocation, + HomeLocation, + TempLocation }; # ifdef WIN32 @@ -48,6 +51,7 @@ public: Process(int argc, char **argv); + static String exepath(); static String location(Location location, const char *fileName = nullptr); inline const Arguments &arguments() const { return m_arguments; } diff --git a/src/base/kernel/config/BaseConfig.cpp b/src/base/kernel/config/BaseConfig.cpp index ae8cb4b0..c61b687a 100644 --- a/src/base/kernel/config/BaseConfig.cpp +++ b/src/base/kernel/config/BaseConfig.cpp @@ -66,6 +66,11 @@ const char *BaseConfig::kVerbose = "verbose"; const char *BaseConfig::kWatch = "watch"; +#ifdef XMRIG_FEATURE_TLS +const char *BaseConfig::kTls = "tls"; +#endif + + } // namespace xmrig @@ -87,11 +92,16 @@ bool xmrig::BaseConfig::read(const IJsonReader &reader, const char *fileName) m_rebenchAlgo = reader.getBool("rebench-algo", m_rebenchAlgo); m_benchAlgoTime = reader.getInt("bench-algo-time", m_benchAlgoTime); + +# ifdef XMRIG_FEATURE_TLS + m_tls = reader.getValue(kTls); +# endif + Log::setColors(reader.getBool(kColors, Log::isColors())); setPrintTime(reader.getUint(kPrintTime, 60)); setVerbose(reader.getValue(kVerbose)); - const rapidjson::Value &api = reader.getObject(kApi); + const auto &api = reader.getObject(kApi); if (api.IsObject()) { m_apiId = Json::getString(api, kApiId); m_apiWorkerId = Json::getString(api, kApiWorkerId); diff --git a/src/base/kernel/config/BaseConfig.h b/src/base/kernel/config/BaseConfig.h index f46bbd83..18b1d3ca 100644 --- a/src/base/kernel/config/BaseConfig.h +++ b/src/base/kernel/config/BaseConfig.h @@ -31,6 +31,11 @@ #include "base/net/stratum/Pools.h" +#ifdef XMRIG_FEATURE_TLS +# include "base/net/tls/TlsConfig.h" +#endif + + namespace xmrig { @@ -55,26 +60,34 @@ public: static const char *kVerbose; static const char *kWatch; +# ifdef XMRIG_FEATURE_TLS + static const char *kTls; +# endif + BaseConfig() = default; - inline bool isAutoSave() const { return m_autoSave; } - inline bool isBackground() const { return m_background; } - inline bool isDryRun() const { return m_dryRun; } - inline bool isSyslog() const { return m_syslog; } - inline const char *logFile() const { return m_logFile.data(); } - inline const char *userAgent() const { return m_userAgent.data(); } - inline const Http &http() const { return m_http; } - inline const Pools &pools() const { return m_pools; } - inline const String &apiId() const { return m_apiId; } - inline const String &apiWorkerId() const { return m_apiWorkerId; } - inline uint32_t printTime() const { return m_printTime; } + inline bool isAutoSave() const { return m_autoSave; } + inline bool isBackground() const { return m_background; } + inline bool isDryRun() const { return m_dryRun; } + inline bool isSyslog() const { return m_syslog; } + inline const char *logFile() const { return m_logFile.data(); } + inline const char *userAgent() const { return m_userAgent.data(); } + inline const Http &http() const { return m_http; } + inline const Pools &pools() const { return m_pools; } + inline const String &apiId() const { return m_apiId; } + inline const String &apiWorkerId() const { return m_apiWorkerId; } + inline uint32_t printTime() const { return m_printTime; } - inline bool isRebenchAlgo() const { return m_rebenchAlgo; } - inline int benchAlgoTime() const { return m_benchAlgoTime; } + inline bool isRebenchAlgo() const { return m_rebenchAlgo; } + inline int benchAlgoTime() const { return m_benchAlgoTime; } - inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } - inline const String &fileName() const override { return m_fileName; } - inline void setFileName(const char *fileName) override { m_fileName = fileName; } +# ifdef XMRIG_FEATURE_TLS + inline const TlsConfig &tls() const { return m_tls; } +# endif + + inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } + inline const String &fileName() const override { return m_fileName; } + inline void setFileName(const char *fileName) override { m_fileName = fileName; } bool read(const IJsonReader &reader, const char *fileName) override; bool save() override; @@ -100,6 +113,10 @@ protected: bool m_rebenchAlgo = false; int m_benchAlgoTime = 10; +# ifdef XMRIG_FEATURE_TLS + TlsConfig m_tls; +# endif + private: inline void setPrintTime(uint32_t printTime) { if (printTime <= 3600) { m_printTime = printTime; } } diff --git a/src/base/kernel/config/BaseTransform.cpp b/src/base/kernel/config/BaseTransform.cpp index 499bdaa6..bf269b7c 100644 --- a/src/base/kernel/config/BaseTransform.cpp +++ b/src/base/kernel/config/BaseTransform.cpp @@ -44,6 +44,11 @@ #include "core/config/Config_platform.h" +#ifdef XMRIG_FEATURE_TLS +# include "base/net/tls/TlsConfig.h" +#endif + + void xmrig::BaseTransform::load(JsonChain &chain, Process *process, IConfigTransform &transform) { using namespace rapidjson; @@ -199,6 +204,29 @@ void xmrig::BaseTransform::transform(rapidjson::Document &doc, int key, const ch case IConfig::UserAgentKey: /* --user-agent */ return set(doc, BaseConfig::kUserAgent, arg); +# ifdef XMRIG_FEATURE_TLS + case IConfig::TlsCertKey: /* --tls-cert */ + return set(doc, BaseConfig::kTls, TlsConfig::kCert, arg); + + case IConfig::TlsCertKeyKey: /* --tls-cert-key */ + return set(doc, BaseConfig::kTls, TlsConfig::kCertKey, arg); + + case IConfig::TlsDHparamKey: /* --tls-dhparam */ + return set(doc, BaseConfig::kTls, TlsConfig::kDhparam, arg); + + case IConfig::TlsCiphersKey: /* --tls-ciphers */ + return set(doc, BaseConfig::kTls, TlsConfig::kCiphers, arg); + + case IConfig::TlsCipherSuitesKey: /* --tls-ciphersuites */ + return set(doc, BaseConfig::kTls, TlsConfig::kCipherSuites, arg); + + case IConfig::TlsProtocolsKey: /* --tls-protocols */ + return set(doc, BaseConfig::kTls, TlsConfig::kProtocols, arg); + + case IConfig::TlsGenKey: /* --tls-gen */ + return set(doc, BaseConfig::kTls, TlsConfig::kGen, arg); +# endif + case IConfig::RetriesKey: /* --retries */ case IConfig::RetryPauseKey: /* --retry-pause */ case IConfig::PrintTimeKey: /* --print-time */ diff --git a/src/base/kernel/interfaces/IConfig.h b/src/base/kernel/interfaces/IConfig.h index 21a1d1af..bf4083b6 100644 --- a/src/base/kernel/interfaces/IConfig.h +++ b/src/base/kernel/interfaces/IConfig.h @@ -73,6 +73,7 @@ public: DaemonKey = 1018, DaemonPollKey = 1019, SelfSelectKey = 1028, + DataDirKey = 1035, // xmrig common CPUPriorityKey = 1021, @@ -100,6 +101,7 @@ public: MemoryPoolKey = 1027, YieldKey = 1030, AstroBWTMaxSizeKey = 1034, + AstroBWTAVX2Key = 1036, // xmrig amd OclPlatformKey = 1400, @@ -133,6 +135,7 @@ public: TlsCiphersKey = 1112, TlsCipherSuitesKey = 1113, TlsProtocolsKey = 1114, + TlsGenKey = 1117, AlgoExtKey = 1115, ProxyPasswordKey = 1116, LoginFileKey = 'L', diff --git a/src/base/kernel/interfaces/ILineListener.h b/src/base/kernel/interfaces/ILineListener.h index 1a6d4914..2759dcec 100644 --- a/src/base/kernel/interfaces/ILineListener.h +++ b/src/base/kernel/interfaces/ILineListener.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,15 +26,12 @@ #define XMRIG_ILINELISTENER_H -#include +#include namespace xmrig { -class String; - - class ILineListener { public: diff --git a/src/base/net/http/Fetch.cpp b/src/base/net/http/Fetch.cpp index 361769e9..a0eb2b55 100644 --- a/src/base/net/http/Fetch.cpp +++ b/src/base/net/http/Fetch.cpp @@ -27,7 +27,7 @@ #ifdef XMRIG_FEATURE_TLS -# include "base/net/http/HttpsClient.h" +# include "base/net/https/HttpsClient.h" #endif diff --git a/src/base/net/http/HttpClient.cpp b/src/base/net/http/HttpClient.cpp index b684ddae..82f62c84 100644 --- a/src/base/net/http/HttpClient.cpp +++ b/src/base/net/http/HttpClient.cpp @@ -29,7 +29,7 @@ #include "base/io/log/Log.h" #include "base/kernel/Platform.h" #include "base/net/dns/Dns.h" -#include "base/tools/Baton.h" +#include "base/net/tools/NetBuffer.h" #include @@ -41,31 +41,6 @@ namespace xmrig { static const char *kCRLF = "\r\n"; -class HttpClientWriteBaton : public Baton -{ -public: - inline HttpClientWriteBaton(const std::string &header, std::string &&body) : - m_body(std::move(body)), - m_header(header) - { - m_bufs[0] = uv_buf_init(const_cast(m_header.c_str()), m_header.size()); - m_bufs[1] = m_body.empty() ? uv_buf_init(nullptr, 0) : uv_buf_init(const_cast(m_body.c_str()), m_body.size()); - } - - void write(uv_stream_t *stream) - { - uv_write(&req, stream, m_bufs, nbufs(), [](uv_write_t *req, int) { delete reinterpret_cast(req->data); }); - } - -private: - inline size_t nbufs() const { return m_bufs[1].len > 0 ? 2 : 1; } - - std::string m_body; - std::string m_header; - uv_buf_t m_bufs[2]{}; -}; - - } // namespace xmrig @@ -137,7 +112,8 @@ void xmrig::HttpClient::handshake() headers.clear(); - write(ss.str()); + body.insert(0, ss.str()); + write(std::move(body), false); } @@ -149,13 +125,6 @@ void xmrig::HttpClient::read(const char *data, size_t size) } -void xmrig::HttpClient::write(const std::string &header) -{ - auto baton = new HttpClientWriteBaton(header, std::move(body)); - baton->write(stream()); -} - - void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) { auto client = static_cast(req->data); @@ -174,12 +143,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) return; } - uv_read_start(client->stream(), - [](uv_handle_t *, size_t suggested_size, uv_buf_t *buf) - { - buf->base = new char[suggested_size]; - buf->len = suggested_size; - }, + uv_read_start(client->stream(), NetBuffer::onAlloc, [](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) { auto client = static_cast(tcp->data); @@ -194,7 +158,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) client->close(static_cast(nread)); } - delete [] buf->base; + NetBuffer::release(buf); }); client->handshake(); diff --git a/src/base/net/http/HttpClient.h b/src/base/net/http/HttpClient.h index b54882a0..acfe15e2 100644 --- a/src/base/net/http/HttpClient.h +++ b/src/base/net/http/HttpClient.h @@ -59,7 +59,6 @@ protected: virtual void handshake(); virtual void read(const char *data, size_t size); - virtual void write(const std::string &header); protected: inline const FetchRequest &req() const { return m_req; } diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp index 66a25d6a..441f3b82 100644 --- a/src/base/net/http/HttpContext.cpp +++ b/src/base/net/http/HttpContext.cpp @@ -27,6 +27,7 @@ #include "base/net/http/HttpContext.h" #include "3rdparty/http-parser/http_parser.h" #include "base/kernel/interfaces/IHttpListener.h" +#include "base/tools/Baton.h" #include "base/tools/Chrono.h" @@ -42,6 +43,37 @@ static std::map storage; static uint64_t SEQUENCE = 0; +class HttpWriteBaton : public Baton +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpWriteBaton) + + inline HttpWriteBaton(std::string &&body, HttpContext *ctx) : + m_ctx(ctx), + m_body(std::move(body)) + { + m_buf = uv_buf_init(const_cast(m_body.c_str()), m_body.size()); + } + + inline ~HttpWriteBaton() + { + if (m_ctx) { + m_ctx->close(); + } + } + + void write(uv_stream_t *stream) + { + uv_write(&req, stream, &m_buf, 1, [](uv_write_t *req, int) { delete reinterpret_cast(req->data); }); + } + +private: + HttpContext *m_ctx; + std::string m_body; + uv_buf_t m_buf{}; +}; + + } // namespace xmrig @@ -75,6 +107,17 @@ xmrig::HttpContext::~HttpContext() } +void xmrig::HttpContext::write(std::string &&data, bool close) +{ + if (uv_is_writable(stream()) != 1) { + return; + } + + auto baton = new HttpWriteBaton(std::move(data), close ? this : nullptr); + baton->write(stream()); +} + + bool xmrig::HttpContext::isRequest() const { return m_parser->type == HTTP_REQUEST; diff --git a/src/base/net/http/HttpContext.h b/src/base/net/http/HttpContext.h index 784bb7ed..a7554679 100644 --- a/src/base/net/http/HttpContext.h +++ b/src/base/net/http/HttpContext.h @@ -65,6 +65,8 @@ public: inline const char *tlsVersion() const override { return nullptr; } inline uint16_t port() const override { return 0; } + void write(std::string &&data, bool close) override; + bool isRequest() const override; size_t parse(const char *data, size_t size); std::string ip() const override; diff --git a/src/base/net/http/HttpData.h b/src/base/net/http/HttpData.h index cd852639..82a8ee8f 100644 --- a/src/base/net/http/HttpData.h +++ b/src/base/net/http/HttpData.h @@ -53,12 +53,13 @@ public: inline uint64_t id() const { return m_id; } - virtual bool isRequest() const = 0; - virtual const char *host() const = 0; - virtual const char *tlsFingerprint() const = 0; - virtual const char *tlsVersion() const = 0; - virtual std::string ip() const = 0; - virtual uint16_t port() const = 0; + virtual bool isRequest() const = 0; + virtual const char *host() const = 0; + virtual const char *tlsFingerprint() const = 0; + virtual const char *tlsVersion() const = 0; + virtual std::string ip() const = 0; + virtual uint16_t port() const = 0; + virtual void write(std::string &&data, bool close) = 0; int method = 0; int status = 0; diff --git a/src/base/net/http/HttpResponse.cpp b/src/base/net/http/HttpResponse.cpp index ebd0bcf9..89e22431 100644 --- a/src/base/net/http/HttpResponse.cpp +++ b/src/base/net/http/HttpResponse.cpp @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2014-2019 heapwolf - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,8 +28,6 @@ #include "3rdparty/http-parser/http_parser.h" #include "base/io/log/Log.h" #include "base/net/http/HttpContext.h" -#include "base/tools/Baton.h" -#include "base/tools/Object.h" #include @@ -45,52 +43,6 @@ static const char *kCRLF = "\r\n"; static const char *kUserAgent = "user-agent"; -class WriteBaton : public Baton -{ -public: - XMRIG_DISABLE_COPY_MOVE_DEFAULT(WriteBaton) - - inline WriteBaton(const std::stringstream &ss, const char *data, size_t size, HttpContext *ctx) : - m_ctx(ctx), - m_header(ss.str()) - { - bufs[0].len = m_header.size(); - bufs[0].base = const_cast(m_header.c_str()); - - if (data) { - bufs[1].len = size; - bufs[1].base = new char[size]; - memcpy(bufs[1].base, data, size); - } - else { - bufs[1].base = nullptr; - bufs[1].len = 0; - } - } - - - inline ~WriteBaton() - { - if (count() == 2) { - delete [] bufs[1].base; - } - - m_ctx->close(); - } - - - inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; } - inline size_t size() const { return bufs[0].len + bufs[1].len; } - inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast(req->data); } - - - uv_buf_t bufs[2]{}; - -private: - HttpContext *m_ctx; - std::string m_header; -}; - } // namespace xmrig @@ -134,8 +86,8 @@ void xmrig::HttpResponse::end(const char *data, size_t size) ss << kCRLF; - auto ctx = HttpContext::get(m_id); - auto baton = new WriteBaton(ss, data, size, ctx); + auto ctx = HttpContext::get(m_id); + std::string body = data ? (ss.str() + std::string(data, size)) : ss.str(); # ifndef APP_DEBUG if (statusCode() >= 400) @@ -149,11 +101,11 @@ void xmrig::HttpResponse::end(const char *data, size_t size) ctx->url.c_str(), err ? 31 : 32, statusCode(), - baton->size(), + body.size(), ctx->elapsed(), ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr ); } - uv_write(&baton->req, ctx->stream(), baton->bufs, baton->count(), WriteBaton::onWrite); + ctx->write(std::move(body), true); } diff --git a/src/base/net/http/HttpResponse.h b/src/base/net/http/HttpResponse.h index c2bc9001..1891ad7e 100644 --- a/src/base/net/http/HttpResponse.h +++ b/src/base/net/http/HttpResponse.h @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2014-2019 heapwolf - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/net/http/HttpServer.cpp b/src/base/net/http/HttpServer.cpp index 4af39c2e..80377f77 100644 --- a/src/base/net/http/HttpServer.cpp +++ b/src/base/net/http/HttpServer.cpp @@ -28,11 +28,10 @@ #include -#include "3rdparty/http-parser/http_parser.h" -#include "base/kernel/interfaces/IHttpListener.h" -#include "base/net/http/HttpContext.h" -#include "base/net/http/HttpResponse.h" #include "base/net/http/HttpServer.h" +#include "3rdparty/http-parser/http_parser.h" +#include "base/net/http/HttpContext.h" +#include "base/net/tools/NetBuffer.h" xmrig::HttpServer::HttpServer(const std::shared_ptr &listener) : @@ -52,17 +51,7 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) auto ctx = new HttpContext(HTTP_REQUEST, m_listener); uv_accept(stream, ctx->stream()); - uv_read_start(ctx->stream(), - [](uv_handle_t *, size_t suggested_size, uv_buf_t *buf) - { - buf->base = new char[suggested_size]; - -# ifdef _WIN32 - buf->len = static_cast(suggested_size); -# else - buf->len = suggested_size; -# endif - }, + uv_read_start(ctx->stream(), NetBuffer::onAlloc, [](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) { auto ctx = static_cast(tcp->data); @@ -78,6 +67,6 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) ctx->close(); } - delete [] buf->base; + NetBuffer::release(buf); }); } diff --git a/src/base/net/http/HttpsClient.cpp b/src/base/net/https/HttpsClient.cpp similarity index 81% rename from src/base/net/http/HttpsClient.cpp rename to src/base/net/https/HttpsClient.cpp index 96162e9c..28b5a5ef 100644 --- a/src/base/net/http/HttpsClient.cpp +++ b/src/base/net/https/HttpsClient.cpp @@ -29,7 +29,7 @@ #include -#include "base/net/http/HttpsClient.h" +#include "base/net/https/HttpsClient.h" #include "base/io/log/Log.h" #include "base/tools/Buffer.h" @@ -49,8 +49,8 @@ xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr 0) { - HttpClient::read(m_buf, static_cast(bytes_read)); + static char buf[16384]{}; + + int rc = 0; + while ((rc = SSL_read(m_ssl, buf, sizeof(buf))) > 0) { + HttpClient::read(buf, static_cast(rc)); + } + + if (rc == 0) { + close(UV_EOF); } } -void xmrig::HttpsClient::write(const std::string &header) +void xmrig::HttpsClient::write(std::string &&data, bool close) { - SSL_write(m_ssl, (header + body).c_str(), header.size() + body.size()); - body.clear(); + const std::string body = std::move(data); + SSL_write(m_ssl, body.data(), body.size()); - flush(); + flush(close); } @@ -182,23 +188,17 @@ bool xmrig::HttpsClient::verifyFingerprint(X509 *cert) } -void xmrig::HttpsClient::flush() +void xmrig::HttpsClient::flush(bool close) { - uv_buf_t buf; - buf.len = BIO_get_mem_data(m_writeBio, &buf.base); - - if (buf.len == 0) { + if (uv_is_writable(stream()) != 1) { return; } - bool result = false; - if (uv_is_writable(stream())) { - result = uv_try_write(stream(), &buf, 1) == static_cast(buf.len); + char *data = nullptr; + const size_t size = BIO_get_mem_data(m_write, &data); + std::string body(data, size); - if (!result) { - close(UV_EIO); - } - } + (void) BIO_reset(m_write); - (void) BIO_reset(m_writeBio); + HttpContext::write(std::move(body), close); } diff --git a/src/base/net/http/HttpsClient.h b/src/base/net/https/HttpsClient.h similarity index 92% rename from src/base/net/http/HttpsClient.h rename to src/base/net/https/HttpsClient.h index afac6e01..eeeec747 100644 --- a/src/base/net/http/HttpsClient.h +++ b/src/base/net/https/HttpsClient.h @@ -55,17 +55,17 @@ public: protected: void handshake() override; void read(const char *data, size_t size) override; - void write(const std::string &header) override; private: + void write(std::string &&data, bool close) override; + bool verify(X509 *cert); bool verifyFingerprint(X509 *cert); - void flush(); + void flush(bool close); - BIO *m_readBio = nullptr; - BIO *m_writeBio = nullptr; + BIO *m_read = nullptr; + BIO *m_write = nullptr; bool m_ready = false; - char m_buf[1024 * 2]{}; char m_fingerprint[32 * 2 + 8]{}; SSL *m_ssl = nullptr; SSL_CTX *m_ctx = nullptr; diff --git a/src/base/net/https/HttpsContext.cpp b/src/base/net/https/HttpsContext.cpp new file mode 100644 index 00000000..8db31812 --- /dev/null +++ b/src/base/net/https/HttpsContext.cpp @@ -0,0 +1,99 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/https/HttpsContext.h" +#include "3rdparty/http-parser/http_parser.h" +#include "base/net/tls/TlsContext.h" + + +#include +#include + + +xmrig::HttpsContext::HttpsContext(TlsContext *tls, const std::weak_ptr &listener) : + HttpContext(HTTP_REQUEST, listener), + ServerTls(tls ? tls->ctx() : nullptr) +{ + if (!tls) { + m_mode = TLS_OFF; + } +} + + +xmrig::HttpsContext::~HttpsContext() = default; + + +void xmrig::HttpsContext::append(char *data, size_t size) +{ + if (m_mode == TLS_AUTO) { + m_mode = isTLS(data, size) ? TLS_ON : TLS_OFF; + } + + if (m_mode == TLS_ON) { + read(data, size); + } + else { + parse(data, size); + } +} + + +bool xmrig::HttpsContext::write(BIO *bio) +{ + if (uv_is_writable(stream()) != 1) { + return false; + } + + char *data = nullptr; + const size_t size = BIO_get_mem_data(bio, &data); + std::string body(data, size); + + (void) BIO_reset(bio); + + HttpContext::write(std::move(body), m_close); + + return true; +} + + +void xmrig::HttpsContext::parse(char *data, size_t size) +{ + if (HttpContext::parse(data, size) < size) { + close(); + } +} + + +void xmrig::HttpsContext::shutdown() +{ + close(); +} + + +void xmrig::HttpsContext::write(std::string &&data, bool close) +{ + m_close = close; + + if (m_mode == TLS_ON) { + send(data.data(), data.size()); + } + else { + HttpContext::write(std::move(data), close); + } +} diff --git a/src/base/net/https/HttpsContext.h b/src/base/net/https/HttpsContext.h new file mode 100644 index 00000000..af57b6b4 --- /dev/null +++ b/src/base/net/https/HttpsContext.h @@ -0,0 +1,73 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef XMRIG_HTTPSCONTEXT_H +#define XMRIG_HTTPSCONTEXT_H + + +using BIO = struct bio_st; +using SSL = struct ssl_st; + + +#include "base/net/http/HttpContext.h" +#include "base/net/tls/ServerTls.h" + + +namespace xmrig { + + +class TlsContext; + + +class HttpsContext : public HttpContext, public ServerTls +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsContext) + + HttpsContext(TlsContext *tls, const std::weak_ptr &listener); + ~HttpsContext() override; + + void append(char *data, size_t size); + +protected: + // ServerTls + bool write(BIO *bio) override; + void parse(char *data, size_t size) override; + void shutdown() override; + + // HttpContext + void write(std::string &&data, bool close) override; + +private: + enum TlsMode : uint32_t { + TLS_AUTO, + TLS_OFF, + TLS_ON + }; + + bool m_close = false; + TlsMode m_mode = TLS_AUTO; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPSCONTEXT_H + diff --git a/src/base/net/https/HttpsServer.cpp b/src/base/net/https/HttpsServer.cpp new file mode 100644 index 00000000..3a6fb080 --- /dev/null +++ b/src/base/net/https/HttpsServer.cpp @@ -0,0 +1,71 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "base/net/https/HttpsServer.h" +#include "base/net/https/HttpsContext.h" +#include "base/net/tls/TlsContext.h" +#include "base/net/tools/NetBuffer.h" + + +xmrig::HttpsServer::HttpsServer(const std::shared_ptr &listener) : + m_listener(listener) +{ +} + + +xmrig::HttpsServer::~HttpsServer() +{ + HttpContext::closeAll(); + + delete m_tls; +} + + +bool xmrig::HttpsServer::setTls(const TlsConfig &config) +{ + m_tls = TlsContext::create(config); + + return m_tls != nullptr; +} + + +void xmrig::HttpsServer::onConnection(uv_stream_t *stream, uint16_t) +{ + auto ctx = new HttpsContext(m_tls, m_listener); + uv_accept(stream, ctx->stream()); + + uv_read_start(ctx->stream(), NetBuffer::onAlloc, onRead); +} + + +void xmrig::HttpsServer::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) +{ + auto ctx = static_cast(stream->data); + if (nread >= 0) { + ctx->append(buf->base, static_cast(nread)); + } + else { + ctx->close(); + } + + NetBuffer::release(buf); +} diff --git a/src/base/net/https/HttpsServer.h b/src/base/net/https/HttpsServer.h new file mode 100644 index 00000000..7f567625 --- /dev/null +++ b/src/base/net/https/HttpsServer.h @@ -0,0 +1,72 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef XMRIG_HTTPSSERVER_H +#define XMRIG_HTTPSSERVER_H + + +using uv_tcp_t = struct uv_tcp_s; + +struct http_parser; +struct http_parser_settings; +struct uv_buf_t; + + +#include "base/kernel/interfaces/ITcpServerListener.h" +#include "base/tools/Object.h" + + +#include + + +namespace xmrig { + + +class IHttpListener; +class TlsContext; +class TlsConfig; + + +class HttpsServer : public ITcpServerListener +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsServer) + + HttpsServer(const std::shared_ptr &listener); + ~HttpsServer() override; + + bool setTls(const TlsConfig &config); + +protected: + void onConnection(uv_stream_t *stream, uint16_t port) override; + +private: + static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); + + std::weak_ptr m_listener; + TlsContext *m_tls = nullptr; + uv_tcp_t *m_tcp = nullptr; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPSSERVER_H + diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index bd7b2219..16a5f79c 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -38,13 +38,14 @@ #endif +#include "base/net/stratum/Client.h" #include "base/io/json/Json.h" #include "base/io/json/JsonRequest.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IClientListener.h" #include "base/net/dns/Dns.h" -#include "base/net/stratum/Client.h" #include "base/net/stratum/Socks5.h" +#include "base/net/tools/NetBuffer.h" #include "base/tools/Buffer.h" #include "base/tools/Chrono.h" #include "net/JobResult.h" @@ -83,6 +84,7 @@ xmrig::Client::Client(int id, const char *agent, IClientListener *listener) : m_agent(agent), m_sendBuf(1024) { + m_reader.setListener(this); m_key = m_storage.add(this); m_dns = new Dns(this); } @@ -542,7 +544,7 @@ int xmrig::Client::resolve(const String &host) { setState(HostLookupState); - m_recvBuf.reset(); + m_reader.reset(); if (m_failures == -1) { m_failures = 0; @@ -837,14 +839,10 @@ void xmrig::Client::ping() } -void xmrig::Client::read(ssize_t nread) +void xmrig::Client::read(ssize_t nread, const uv_buf_t *buf) { const auto size = static_cast(nread); - if (nread > 0 && size > m_recvBuf.available()) { - nread = UV_ENOBUFS; - } - if (nread < 0) { if (!isQuiet()) { LOG_ERR("[%s] read error: \"%s\"", url(), uv_strerror(static_cast(nread))); @@ -859,12 +857,8 @@ void xmrig::Client::read(ssize_t nread) return reconnect(); } - m_recvBuf.nread(size); - if (m_socks5) { - if (m_socks5->read(m_recvBuf.base(), m_recvBuf.pos())) { - m_recvBuf.reset(); - } + m_socks5->read(buf->base, size); if (m_socks5->isReady()) { delete m_socks5; @@ -886,13 +880,12 @@ void xmrig::Client::read(ssize_t nread) if (isTLS()) { LOG_DEBUG("[%s] TLS received (%d bytes)", url(), static_cast(nread)); - m_tls->read(m_recvBuf.base(), m_recvBuf.pos()); - m_recvBuf.reset(); + m_tls->read(buf->base, size); } else # endif { - m_recvBuf.getline(this); + m_reader.parse(buf->base, size); } } @@ -959,23 +952,6 @@ void xmrig::Client::startTimeout() } -void xmrig::Client::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf) -{ - auto client = getClient(handle->data); - if (!client) { - return; - } - - buf->base = client->m_recvBuf.current(); - -# ifdef _WIN32 - buf->len = static_cast(client->m_recvBuf.available()); -# else - buf->len = client->m_recvBuf.available(); -# endif -} - - void xmrig::Client::onClose(uv_handle_t *handle) { auto client = getClient(handle->data); @@ -1027,17 +1003,19 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status) client->m_stream->data = req->data; client->setState(ConnectedState); - uv_read_start(client->m_stream, onAllocBuffer, onRead); + uv_read_start(client->m_stream, NetBuffer::onAlloc, onRead); delete req; client->handshake(); } -void xmrig::Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *) +void xmrig::Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { auto client = getClient(stream->data); if (client) { - client->read(nread); + client->read(nread, buf); } + + NetBuffer::release(buf); } diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index ead8039c..d6ebf396 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -39,7 +39,7 @@ #include "base/net/stratum/Job.h" #include "base/net/stratum/Pool.h" #include "base/net/stratum/SubmitResult.h" -#include "base/net/tools/RecvBuf.h" +#include "base/net/tools/LineReader.h" #include "base/net/tools/Storage.h" #include "base/tools/Object.h" @@ -61,7 +61,6 @@ public: constexpr static uint64_t kConnectTimeout = 20 * 1000; constexpr static uint64_t kResponseTimeout = 20 * 1000; - constexpr static size_t kInputBufferSize = 1024 * 16; constexpr static size_t kMaxSendBufferSize = 1024 * 16; Client(int id, const char *agent, IClientListener *listener); @@ -108,7 +107,7 @@ private: void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error); void ping(); - void read(ssize_t nread); + void read(ssize_t nread, const uv_buf_t *buf); void reconnect(); void setState(SocketState state); void startTimeout(); @@ -118,7 +117,6 @@ private: inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); } template inline bool has() const noexcept { return m_extensions.test(ext); } - static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); static void onClose(uv_handle_t *handle); static void onConnect(uv_connect_t *req, int status); static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); @@ -127,7 +125,7 @@ private: const char *m_agent; Dns *m_dns; - RecvBuf m_recvBuf; + LineReader m_reader; Socks5 *m_socks5 = nullptr; std::bitset m_extensions; std::vector m_sendBuf; diff --git a/src/base/net/stratum/Socks5.cpp b/src/base/net/stratum/Socks5.cpp index f27ee0d0..9577906f 100644 --- a/src/base/net/stratum/Socks5.cpp +++ b/src/base/net/stratum/Socks5.cpp @@ -78,7 +78,7 @@ void xmrig::Client::Socks5::connect() const auto &host = m_client->pool().host(); std::vector buf; - sockaddr_storage addr; + sockaddr_storage addr{}; if (isIPv4(host, &addr)) { buf.resize(10); diff --git a/src/base/net/stratum/Tls.cpp b/src/base/net/stratum/Tls.cpp index 8916579b..5915fad9 100644 --- a/src/base/net/stratum/Tls.cpp +++ b/src/base/net/stratum/Tls.cpp @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,12 +24,9 @@ */ -#include - - +#include "base/net/stratum/Tls.h" #include "base/io/log/Log.h" #include "base/net/stratum/Client.h" -#include "base/net/stratum/Tls.h" #include "base/tools/Buffer.h" @@ -38,12 +35,12 @@ #endif +#include +#include + + xmrig::Client::Tls::Tls(Client *client) : - m_ready(false), - m_buf(), - m_fingerprint(), - m_client(client), - m_ssl(nullptr) + m_client(client) { m_ctx = SSL_CTX_new(SSLv23_method()); assert(m_ctx != nullptr); @@ -52,8 +49,8 @@ xmrig::Client::Tls::Tls(Client *client) : return; } - m_writeBio = BIO_new(BIO_s_mem()); - m_readBio = BIO_new(BIO_s_mem()); + m_write = BIO_new(BIO_s_mem()); + m_read = BIO_new(BIO_s_mem()); SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); } @@ -80,7 +77,7 @@ bool xmrig::Client::Tls::handshake() } SSL_set_connect_state(m_ssl); - SSL_set_bio(m_ssl, m_readBio, m_writeBio); + SSL_set_bio(m_ssl, m_read, m_write); SSL_do_handshake(m_ssl); return send(); @@ -109,7 +106,7 @@ const char *xmrig::Client::Tls::version() const void xmrig::Client::Tls::read(const char *data, size_t size) { - BIO_write(m_readBio, data, size); + BIO_write(m_read, data, size); if (!SSL_is_init_finished(m_ssl)) { const int rc = SSL_connect(m_ssl); @@ -133,17 +130,18 @@ void xmrig::Client::Tls::read(const char *data, size_t size) return; } + static char buf[16384]{}; int bytes_read = 0; - while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) { - m_buf[bytes_read - 1] = '\0'; - m_client->parse(m_buf, static_cast(bytes_read)); + + while ((bytes_read = SSL_read(m_ssl, buf, sizeof(buf))) > 0) { + m_client->m_reader.parse(buf, static_cast(bytes_read)); } } bool xmrig::Client::Tls::send() { - return m_client->send(m_writeBio); + return m_client->send(m_write); } diff --git a/src/base/net/stratum/Tls.h b/src/base/net/stratum/Tls.h index e070be52..38cf2f9e 100644 --- a/src/base/net/stratum/Tls.h +++ b/src/base/net/stratum/Tls.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,10 +26,14 @@ #define XMRIG_CLIENT_TLS_H -#include +using BIO = struct bio_st; +using SSL = struct ssl_st; +using SSL_CTX = struct ssl_ctx_st; +using X509 = struct x509_st; #include "base/net/stratum/Client.h" +#include "base/tools/Object.h" namespace xmrig { @@ -38,6 +42,8 @@ namespace xmrig { class Client::Tls { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Tls) + Tls(Client *client); ~Tls(); @@ -52,13 +58,12 @@ private: bool verify(X509 *cert); bool verifyFingerprint(X509 *cert); - BIO *m_readBio; - BIO *m_writeBio; - bool m_ready; - char m_buf[1024 * 2]; - char m_fingerprint[32 * 2 + 8]; + BIO *m_read = nullptr; + BIO *m_write = nullptr; + bool m_ready = false; + char m_fingerprint[32 * 2 + 8]{}; Client *m_client; - SSL *m_ssl; + SSL *m_ssl = nullptr; SSL_CTX *m_ctx; }; diff --git a/src/base/net/tls/ServerTls.cpp b/src/base/net/tls/ServerTls.cpp new file mode 100644 index 00000000..e76d5862 --- /dev/null +++ b/src/base/net/tls/ServerTls.cpp @@ -0,0 +1,103 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/tls/ServerTls.h" + + +#include +#include +#include + + +xmrig::ServerTls::ServerTls(SSL_CTX *ctx) : + m_ctx(ctx) +{ +} + + +xmrig::ServerTls::~ServerTls() +{ + if (m_ssl) { + SSL_free(m_ssl); + } +} + + +bool xmrig::ServerTls::isTLS(const char *data, size_t size) +{ + static const uint8_t test[3] = { 0x16, 0x03, 0x01 }; + + return size >= sizeof(test) && memcmp(data, test, sizeof(test)) == 0; +} + + +bool xmrig::ServerTls::send(const char *data, size_t size) +{ + SSL_write(m_ssl, data, size); + + return write(m_write); +} + + +void xmrig::ServerTls::read(const char *data, size_t size) +{ + if (!m_ssl) { + m_ssl = SSL_new(m_ctx); + + m_write = BIO_new(BIO_s_mem()); + m_read = BIO_new(BIO_s_mem()); + + SSL_set_accept_state(m_ssl); + SSL_set_bio(m_ssl, m_read, m_write); + } + + + BIO_write(m_read, data, size); + + if (!SSL_is_init_finished(m_ssl)) { + const int rc = SSL_do_handshake(m_ssl); + + if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { + write(m_write); + } else if (rc == 1) { + write(m_write); + + m_ready = true; + read(); + } + else { + shutdown(); + } + + return; + } + + read(); +} + + +void xmrig::ServerTls::read() +{ + static char buf[16384]{}; + + int bytes_read = 0; + while ((bytes_read = SSL_read(m_ssl, buf, sizeof(buf))) > 0) { + parse(buf, bytes_read); + } +} diff --git a/src/base/net/tls/ServerTls.h b/src/base/net/tls/ServerTls.h new file mode 100644 index 00000000..892bed2b --- /dev/null +++ b/src/base/net/tls/ServerTls.h @@ -0,0 +1,68 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef XMRIG_SERVERTLS_H +#define XMRIG_SERVERTLS_H + + +using BIO = struct bio_st; +using SSL = struct ssl_st; +using SSL_CTX = struct ssl_ctx_st; + + + +#include "base/tools/Object.h" + + +namespace xmrig { + + +class ServerTls +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(ServerTls) + + ServerTls(SSL_CTX *ctx); + virtual ~ServerTls(); + + static bool isTLS(const char *data, size_t size); + + bool send(const char *data, size_t size); + void read(const char *data, size_t size); + +protected: + virtual bool write(BIO *bio) = 0; + virtual void parse(char *data, size_t size) = 0; + virtual void shutdown() = 0; + +private: + void read(); + + BIO *m_read = nullptr; + BIO *m_write = nullptr; + bool m_ready = false; + SSL *m_ssl = nullptr; + SSL_CTX *m_ctx; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_SERVERTLS_H */ diff --git a/src/base/net/tls/TlsConfig.cpp b/src/base/net/tls/TlsConfig.cpp new file mode 100644 index 00000000..49b1970d --- /dev/null +++ b/src/base/net/tls/TlsConfig.cpp @@ -0,0 +1,200 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/tls/TlsConfig.h" +#include "base/io/json/Json.h" +#include "base/io/log/Log.h" +#include "base/net/tls/TlsGen.h" +#include "rapidjson/document.h" + + +namespace xmrig { + + +const char *TlsConfig::kCert = "cert"; +const char *TlsConfig::kEnabled = "enabled"; +const char *TlsConfig::kCertKey = "cert_key"; +const char *TlsConfig::kCiphers = "ciphers"; +const char *TlsConfig::kCipherSuites = "ciphersuites"; +const char *TlsConfig::kDhparam = "dhparam"; +const char *TlsConfig::kGen = "gen"; +const char *TlsConfig::kProtocols = "protocols"; + +static const char *kTLSv1 = "TLSv1"; +static const char *kTLSv1_1 = "TLSv1.1"; +static const char *kTLSv1_2 = "TLSv1.2"; +static const char *kTLSv1_3 = "TLSv1.3"; + + +} // namespace xmrig + + +/** + * "cert" load TLS certificate chain from file. + * "cert_key" load TLS private key from file. + * "ciphers" set list of available ciphers (TLSv1.2 and below). + * "ciphersuites" set list of available TLSv1.3 ciphersuites. + * "dhparam" load DH parameters for DHE ciphers from file. + */ +xmrig::TlsConfig::TlsConfig(const rapidjson::Value &value) +{ + if (value.IsObject()) { + m_enabled = Json::getBool(value, kEnabled, m_enabled); + + setProtocols(Json::getString(value, kProtocols)); + setCert(Json::getString(value, kCert)); + setKey(Json::getString(value, kCertKey)); + setCiphers(Json::getString(value, kCiphers)); + setCipherSuites(Json::getString(value, kCipherSuites)); + setDH(Json::getString(value, kDhparam)); + + if (m_key.isNull()) { + setKey(Json::getString(value, "cert-key")); + } + + if (m_enabled && !isValid()) { + generate(Json::getString(value, kGen)); + } + } + else if (value.IsBool()) { + m_enabled = value.GetBool(); + + if (m_enabled) { + generate(); + } + } +# ifdef XMRIG_PROXY_PROJECT + else if (value.IsNull()) { + generate(); + } +# endif + else if (value.IsString()) { + generate(value.GetString()); + } + else { + m_enabled = false; + } +} + + +bool xmrig::TlsConfig::generate(const char *commonName) +{ + TlsGen gen; + + try { + gen.generate(commonName); + } + catch (std::exception &ex) { + LOG_ERR("%s", ex.what()); + + return false; + } + + setCert(gen.cert()); + setKey(gen.certKey()); + + m_enabled = true; + + return true; +} + + +rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + Value obj(kObjectType); + obj.AddMember(StringRef(kEnabled), m_enabled, allocator); + + if (m_protocols > 0) { + std::vector protocols; + + if (m_protocols & TLSv1) { + protocols.emplace_back(kTLSv1); + } + + if (m_protocols & TLSv1_1) { + protocols.emplace_back(kTLSv1_1); + } + + if (m_protocols & TLSv1_2) { + protocols.emplace_back(kTLSv1_2); + } + + if (m_protocols & TLSv1_3) { + protocols.emplace_back(kTLSv1_3); + } + + obj.AddMember(StringRef(kProtocols), String::join(protocols, ' ').toJSON(doc), allocator); + } + else { + obj.AddMember(StringRef(kProtocols), kNullType, allocator); + } + + obj.AddMember(StringRef(kCert), m_cert.toJSON(), allocator); + obj.AddMember(StringRef(kCertKey), m_key.toJSON(), allocator); + obj.AddMember(StringRef(kCiphers), m_ciphers.toJSON(), allocator); + obj.AddMember(StringRef(kCipherSuites), m_cipherSuites.toJSON(), allocator); + obj.AddMember(StringRef(kDhparam), m_dhparam.toJSON(), allocator); + + return obj; +} + + +void xmrig::TlsConfig::setProtocols(const char *protocols) +{ + const std::vector vec = String(protocols).split(' '); + + for (const String &value : vec) { + if (value == kTLSv1) { + m_protocols |= TLSv1; + } + else if (value == kTLSv1_1) { + m_protocols |= TLSv1_1; + } + else if (value == kTLSv1_2) { + m_protocols |= TLSv1_2; + } + else if (value == kTLSv1_3) { + m_protocols |= TLSv1_3; + } + } +} + + +void xmrig::TlsConfig::setProtocols(const rapidjson::Value &protocols) +{ + m_protocols = 0; + + if (protocols.IsUint()) { + return setProtocols(protocols.GetUint()); + } + + if (protocols.IsString()) { + return setProtocols(protocols.GetString()); + } +} diff --git a/src/base/net/tls/TlsConfig.h b/src/base/net/tls/TlsConfig.h new file mode 100644 index 00000000..4e0fe787 --- /dev/null +++ b/src/base/net/tls/TlsConfig.h @@ -0,0 +1,92 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_TLSCONFIG_H +#define XMRIG_TLSCONFIG_H + + +#include "base/tools/String.h" +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class TlsConfig +{ +public: + static const char *kCert; + static const char *kCertKey; + static const char *kCiphers; + static const char *kCipherSuites; + static const char *kDhparam; + static const char *kEnabled; + static const char *kGen; + static const char *kProtocols; + + enum Versions { + TLSv1 = 1, + TLSv1_1 = 2, + TLSv1_2 = 4, + TLSv1_3 = 8 + }; + + TlsConfig() = default; + TlsConfig(const rapidjson::Value &object); + + inline bool isEnabled() const { return m_enabled && isValid(); } + inline bool isValid() const { return !m_cert.isEmpty() && !m_key.isEmpty(); } + inline const char *cert() const { return m_cert.data(); } + inline const char *ciphers() const { return m_ciphers.isEmpty() ? nullptr : m_ciphers.data(); } + inline const char *cipherSuites() const { return m_cipherSuites.isEmpty() ? nullptr : m_cipherSuites.data(); } + inline const char *dhparam() const { return m_dhparam.isEmpty() ? nullptr : m_dhparam.data(); } + inline const char *key() const { return m_key.data(); } + inline uint32_t protocols() const { return m_protocols; } + inline void setCert(const char *cert) { m_cert = cert; } + inline void setCiphers(const char *ciphers) { m_ciphers = ciphers; } + inline void setCipherSuites(const char *ciphers) { m_cipherSuites = ciphers; } + inline void setDH(const char *dhparam) { m_dhparam = dhparam; } + inline void setKey(const char *key) { m_key = key; } + inline void setProtocols(uint32_t protocols) { m_protocols = protocols; } + + bool generate(const char *commonName = nullptr); + rapidjson::Value toJSON(rapidjson::Document &doc) const; + void setProtocols(const char *protocols); + void setProtocols(const rapidjson::Value &protocols); + +private: + bool m_enabled = true; + uint32_t m_protocols = 0; + String m_cert; + String m_ciphers; + String m_cipherSuites; + String m_dhparam; + String m_key; +}; + + +} /* namespace xmrig */ + +#endif /* XMRIG_TLSCONFIG_H */ diff --git a/src/base/net/tls/TlsContext.cpp b/src/base/net/tls/TlsContext.cpp new file mode 100644 index 00000000..d29c744b --- /dev/null +++ b/src/base/net/tls/TlsContext.cpp @@ -0,0 +1,270 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/tls/TlsContext.h" +#include "base/io/log/Log.h" +#include "base/kernel/Env.h" +#include "base/net/tls/TlsConfig.h" + + +#include +#include + + +// https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes#Compatibility_Layer +#if OPENSSL_VERSION_NUMBER < 0x10100000L +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) +{ + assert(q == nullptr); + + dh->p = p; + dh->g = g; + + return 1; + } +#endif + + +namespace xmrig { + + +// https://wiki.openssl.org/index.php/Diffie-Hellman_parameters +static DH *get_dh2048() +{ + static unsigned char dhp_2048[] = { + 0xB2, 0x91, 0xA7, 0x05, 0x31, 0xCE, 0x12, 0x9D, 0x03, 0x43, + 0xAF, 0x13, 0xAF, 0x4B, 0x8E, 0x4C, 0x04, 0x13, 0x4F, 0x72, + 0x00, 0x73, 0x2C, 0x67, 0xC3, 0xE0, 0x50, 0xBF, 0x72, 0x5E, + 0xBE, 0x45, 0x89, 0x4C, 0x01, 0x45, 0xA6, 0x5E, 0xA7, 0xA8, + 0xDC, 0x2F, 0x1D, 0x91, 0x2D, 0x58, 0x0D, 0x71, 0x97, 0x3D, + 0xAE, 0xFE, 0x86, 0x29, 0x37, 0x5F, 0x5E, 0x6D, 0x81, 0x56, + 0x07, 0x83, 0xF2, 0xF8, 0xEC, 0x4E, 0xF8, 0x7A, 0xEC, 0xEA, + 0xD9, 0xEA, 0x61, 0x3C, 0xAF, 0x51, 0x30, 0xB7, 0xA7, 0x67, + 0x3F, 0x59, 0xAD, 0x2E, 0x23, 0x57, 0x64, 0xA2, 0x99, 0x15, + 0xBD, 0xD9, 0x8D, 0xBA, 0xE6, 0x8F, 0xFB, 0xB3, 0x77, 0x3B, + 0xE6, 0x5C, 0xC1, 0x03, 0xCF, 0x38, 0xD4, 0xF6, 0x2E, 0x0B, + 0xF3, 0x20, 0xBE, 0xF0, 0xFC, 0x85, 0xEF, 0x5F, 0xCE, 0x0E, + 0x42, 0x17, 0x3B, 0x72, 0x43, 0x4C, 0x3A, 0xF5, 0xC8, 0xB4, + 0x40, 0x52, 0x03, 0x72, 0x9A, 0x2C, 0xA4, 0x23, 0x2A, 0xA2, + 0x52, 0xA3, 0xC2, 0x76, 0x08, 0x1C, 0x2E, 0x60, 0x44, 0xE4, + 0x12, 0x5D, 0x80, 0x47, 0x6C, 0x7A, 0x5A, 0x8E, 0x18, 0xC9, + 0x8C, 0x22, 0xC8, 0x07, 0x75, 0xE2, 0x77, 0x3A, 0x90, 0x2E, + 0x79, 0xC3, 0xF5, 0x4E, 0x4E, 0xDE, 0x14, 0x29, 0xA4, 0x5B, + 0x32, 0xCC, 0xE5, 0x05, 0x09, 0x2A, 0xC9, 0x1C, 0xB4, 0x8E, + 0x99, 0xCF, 0x57, 0xF2, 0x1B, 0x5F, 0x18, 0x89, 0x29, 0xF2, + 0xB0, 0xF3, 0xAC, 0x67, 0x16, 0x90, 0x4A, 0x1D, 0xD6, 0xF5, + 0x84, 0x71, 0x1D, 0x0E, 0x61, 0x5F, 0xE2, 0x2D, 0x52, 0x87, + 0x0D, 0x8F, 0x84, 0xCB, 0xFC, 0xF0, 0x5D, 0x4C, 0x9F, 0x59, + 0xA9, 0xD6, 0x83, 0x70, 0x4B, 0x98, 0x6A, 0xCA, 0x78, 0x53, + 0x27, 0x32, 0x59, 0x35, 0x0A, 0xB8, 0x29, 0x18, 0xAF, 0x58, + 0x45, 0x63, 0xEB, 0x43, 0x28, 0x7B + }; + + static unsigned char dhg_2048[] = { 0x02 }; + + auto dh = DH_new(); + if (dh == nullptr) { + return nullptr; + } + + auto p = BN_bin2bn(dhp_2048, sizeof(dhp_2048), nullptr); + auto g = BN_bin2bn(dhg_2048, sizeof(dhg_2048), nullptr); + + if (p == nullptr || g == nullptr || !DH_set0_pqg(dh, p, nullptr, g)) { + DH_free(dh); + BN_free(p); + BN_free(g); + + return nullptr; + } + + return dh; +} + +} // namespace xmrig + + +xmrig::TlsContext::~TlsContext() +{ + SSL_CTX_free(m_ctx); +} + + +xmrig::TlsContext *xmrig::TlsContext::create(const TlsConfig &config) +{ + if (!config.isEnabled()) { + return nullptr; + } + + auto tls = new TlsContext(); + if (!tls->load(config)) { + delete tls; + + return nullptr; + } + + return tls; +} + + +bool xmrig::TlsContext::load(const TlsConfig &config) +{ + m_ctx = SSL_CTX_new(SSLv23_server_method()); + if (m_ctx == nullptr) { + LOG_ERR("Unable to create SSL context"); + + return false; + } + + const auto cert = Env::expand(config.cert()); + if (SSL_CTX_use_certificate_chain_file(m_ctx, cert) <= 0) { + LOG_ERR("SSL_CTX_use_certificate_chain_file(\"%s\") failed.", config.cert()); + + return false; + } + + const auto key = Env::expand(config.key()); + if (SSL_CTX_use_PrivateKey_file(m_ctx, key, SSL_FILETYPE_PEM) <= 0) { + LOG_ERR("SSL_CTX_use_PrivateKey_file(\"%s\") failed.", config.key()); + + return false; + } + + SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); + SSL_CTX_set_options(m_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); + +# if OPENSSL_VERSION_NUMBER >= 0x1010100fL + SSL_CTX_set_max_early_data(m_ctx, 0); +# endif + + setProtocols(config.protocols()); + + return setCiphers(config.ciphers()) && setCipherSuites(config.cipherSuites()) && setDH(config.dhparam()); +} + + +bool xmrig::TlsContext::setCiphers(const char *ciphers) +{ + if (ciphers == nullptr || SSL_CTX_set_cipher_list(m_ctx, ciphers) == 1) { + return true; + } + + LOG_ERR("SSL_CTX_set_cipher_list(\"%s\") failed.", ciphers); + + return true; +} + + +bool xmrig::TlsContext::setCipherSuites(const char *ciphersuites) +{ + if (ciphersuites == nullptr) { + return true; + } + +# if OPENSSL_VERSION_NUMBER >= 0x1010100fL + if (SSL_CTX_set_ciphersuites(m_ctx, ciphersuites) == 1) { + return true; + } +# endif + + LOG_ERR("SSL_CTX_set_ciphersuites(\"%s\") failed.", ciphersuites); + + return false; +} + + +bool xmrig::TlsContext::setDH(const char *dhparam) +{ + DH *dh = nullptr; + + if (dhparam != nullptr) { + BIO *bio = BIO_new_file(Env::expand(dhparam), "r"); + if (bio == nullptr) { + LOG_ERR("BIO_new_file(\"%s\") failed.", dhparam); + + return false; + } + + dh = PEM_read_bio_DHparams(bio, nullptr, nullptr, nullptr); + if (dh == nullptr) { + LOG_ERR("PEM_read_bio_DHparams(\"%s\") failed.", dhparam); + + BIO_free(bio); + + return false; + } + + BIO_free(bio); + } + else { + dh = get_dh2048(); + } + + const int rc = SSL_CTX_set_tmp_dh(m_ctx, dh); + + DH_free(dh); + + if (rc == 0) { + LOG_ERR("SSL_CTX_set_tmp_dh(\"%s\") failed.", dhparam); + + return false; + } + + return true; +} + + +void xmrig::TlsContext::setProtocols(uint32_t protocols) +{ + if (protocols == 0) { + return; + } + + if (!(protocols & TlsConfig::TLSv1)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1); + } + +# ifdef SSL_OP_NO_TLSv1_1 + SSL_CTX_clear_options(m_ctx, SSL_OP_NO_TLSv1_1); + if (!(protocols & TlsConfig::TLSv1_1)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1_1); + } +# endif + +# ifdef SSL_OP_NO_TLSv1_2 + SSL_CTX_clear_options(m_ctx, SSL_OP_NO_TLSv1_2); + if (!(protocols & TlsConfig::TLSv1_2)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1_2); + } +# endif + +# ifdef SSL_OP_NO_TLSv1_3 + SSL_CTX_clear_options(m_ctx, SSL_OP_NO_TLSv1_3); + if (!(protocols & TlsConfig::TLSv1_3)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1_3); + } +# endif +} diff --git a/src/base/net/tls/TlsContext.h b/src/base/net/tls/TlsContext.h new file mode 100644 index 00000000..b87798c5 --- /dev/null +++ b/src/base/net/tls/TlsContext.h @@ -0,0 +1,71 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_TLSCONTEXT_H +#define XMRIG_TLSCONTEXT_H + + +#include "base/tools/Object.h" + + +#include + + +using SSL_CTX = struct ssl_ctx_st; + + +namespace xmrig { + + +class TlsConfig; + + +class TlsContext +{ +public: + XMRIG_DISABLE_COPY_MOVE(TlsContext) + + ~TlsContext(); + + static TlsContext *create(const TlsConfig &config); + + inline SSL_CTX *ctx() const { return m_ctx; } + +private: + TlsContext() = default; + + bool load(const TlsConfig &config); + bool setCiphers(const char *ciphers); + bool setCipherSuites(const char *ciphersuites); + bool setDH(const char *dhparam); + void setProtocols(uint32_t protocols); + + SSL_CTX *m_ctx = nullptr; +}; + + +} /* namespace xmrig */ + +#endif /* XMRIG_TLSCONTEXT_H */ diff --git a/src/base/net/tls/TlsGen.cpp b/src/base/net/tls/TlsGen.cpp new file mode 100644 index 00000000..c56ef2a2 --- /dev/null +++ b/src/base/net/tls/TlsGen.cpp @@ -0,0 +1,144 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/tls/TlsGen.h" + + +#include +#include +#include +#include + + +namespace xmrig { + + +static const char *kLocalhost = "localhost"; + + +static EVP_PKEY *generate_pkey() +{ + auto pkey = EVP_PKEY_new(); + if (!pkey) { + return nullptr; + } + + auto exponent = BN_new(); + auto rsa = RSA_new(); + if (!exponent || !rsa || !BN_set_word(exponent, RSA_F4) || !RSA_generate_key_ex(rsa, 2048, exponent, nullptr) || !EVP_PKEY_assign_RSA(pkey, rsa)) { + EVP_PKEY_free(pkey); + BN_free(exponent); + RSA_free(rsa); + + return nullptr; + } + + BN_free(exponent); + + return pkey; +} + + +bool isFileExist(const char *fileName) +{ + std::ifstream in(fileName); + + return in.good(); +} + + +} // namespace xmrig + + +xmrig::TlsGen::~TlsGen() +{ + EVP_PKEY_free(m_pkey); + X509_free(m_x509); +} + + +void xmrig::TlsGen::generate(const char *commonName) +{ + if (isFileExist(m_cert) && isFileExist(m_certKey)) { + return; + } + + m_pkey = generate_pkey(); + if (!m_pkey) { + throw std::runtime_error("RSA key generation failed."); + } + + if (!generate_x509(commonName == nullptr || strlen(commonName) == 0 ? kLocalhost : commonName)) { + throw std::runtime_error("x509 certificate generation failed."); + } + + if (!write()) { + throw std::runtime_error("unable to write certificate to disk."); + } +} + + +bool xmrig::TlsGen::generate_x509(const char *commonName) +{ + m_x509 = X509_new(); + if (!m_x509) { + return false; + } + + if (!X509_set_pubkey(m_x509, m_pkey)) { + return false; + } + + ASN1_INTEGER_set(X509_get_serialNumber(m_x509), 1); + X509_gmtime_adj(X509_get_notBefore(m_x509), 0); + X509_gmtime_adj(X509_get_notAfter(m_x509), 315360000L); + + auto name = X509_get_subject_name(m_x509); + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast(commonName), -1, -1, 0); + + X509_set_issuer_name(m_x509, name); + + return X509_sign(m_x509, m_pkey, EVP_sha256()); +} + + +bool xmrig::TlsGen::write() +{ + auto pkey_file = fopen(m_certKey, "wb"); + if (!pkey_file) { + return false; + } + + bool ret = PEM_write_PrivateKey(pkey_file, m_pkey, nullptr, nullptr, 0, nullptr, nullptr); + fclose(pkey_file); + + if (!ret) { + return false; + } + + auto x509_file = fopen(m_cert, "wb"); + if (!x509_file) { + return false; + } + + ret = PEM_write_X509(x509_file, m_x509); + fclose(x509_file); + + return ret; +} diff --git a/src/base/net/tls/TlsGen.h b/src/base/net/tls/TlsGen.h new file mode 100644 index 00000000..9386e88c --- /dev/null +++ b/src/base/net/tls/TlsGen.h @@ -0,0 +1,61 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_TLSGEN_H +#define XMRIG_TLSGEN_H + + +#include "base/tools/Object.h" +#include "base/tools/String.h" + + +using EVP_PKEY = struct evp_pkey_st; +using X509 = struct x509_st; + + +namespace xmrig { + + +class TlsGen +{ +public: + XMRIG_DISABLE_COPY_MOVE(TlsGen) + + TlsGen() : m_cert("cert.pem"), m_certKey("cert_key.pem") {} + ~TlsGen(); + + inline const String &cert() const { return m_cert; } + inline const String &certKey() const { return m_certKey; } + + void generate(const char *commonName = nullptr); + +private: + bool generate_x509(const char *commonName); + bool write(); + + const String m_cert; + const String m_certKey; + EVP_PKEY *m_pkey = nullptr; + X509 *m_x509 = nullptr; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_TLSGEN_H */ diff --git a/src/base/net/tools/LineReader.cpp b/src/base/net/tools/LineReader.cpp new file mode 100644 index 00000000..646852bf --- /dev/null +++ b/src/base/net/tools/LineReader.cpp @@ -0,0 +1,103 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/tools/LineReader.h" +#include "base/net/tools/NetBuffer.h" +#include "base/kernel/interfaces/ILineListener.h" + +#include +#include + + +xmrig::LineReader::~LineReader() +{ + NetBuffer::release(m_buf); +} + + +void xmrig::LineReader::parse(char *data, size_t size) +{ + assert(m_listener != nullptr && size > 0); + if (!m_listener || size == 0) { + return; + } + + if (!m_buf) { + return getline(data, size); + } + + add(data, size); + getline(m_buf, m_pos); +} + + +void xmrig::LineReader::reset() +{ + if (m_buf) { + NetBuffer::release(m_buf); + m_buf = nullptr; + m_pos = 0; + } +} + + +void xmrig::LineReader::add(const char *data, size_t size) +{ + if (size > NetBuffer::kChunkSize - m_pos) { + return; + } + + if (!m_buf) { + m_buf = NetBuffer::allocate(); + m_pos = 0; + } + + memcpy(m_buf + m_pos, data, size); + m_pos += size; +} + + +void xmrig::LineReader::getline(char *data, size_t size) +{ + char *end = nullptr; + char *start = data; + size_t remaining = size; + + while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { + *end = '\0'; + + end++; + + const auto len = static_cast(end - start); + if (len > 1) { + m_listener->onLine(start, len - 1); + } + + remaining -= len; + start = end; + } + + if (remaining == 0) { + return reset(); + } + + if (!m_buf || m_buf != data) { + add(start, remaining); + } +} diff --git a/src/base/net/tools/LineReader.h b/src/base/net/tools/LineReader.h new file mode 100644 index 00000000..601f5cdb --- /dev/null +++ b/src/base/net/tools/LineReader.h @@ -0,0 +1,62 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_LINEREADER_H +#define XMRIG_LINEREADER_H + + +#include "base/tools/Object.h" + + +#include + + +namespace xmrig { + + +class ILineListener; + + +class LineReader +{ +public: + XMRIG_DISABLE_COPY_MOVE(LineReader) + + LineReader() = default; + LineReader(ILineListener *listener) : m_listener(listener) {} + ~LineReader(); + + inline void setListener(ILineListener *listener) { m_listener = listener; } + + void parse(char *data, size_t size); + void reset(); + +private: + void add(const char *data, size_t size); + void getline(char *data, size_t size); + + char *m_buf = nullptr; + ILineListener *m_listener = nullptr; + size_t m_pos = 0; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_NETBUFFER_H */ diff --git a/src/base/net/tools/MemPool.h b/src/base/net/tools/MemPool.h new file mode 100644 index 00000000..3b8a8ef4 --- /dev/null +++ b/src/base/net/tools/MemPool.h @@ -0,0 +1,96 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_MEMPOOL_H +#define XMRIG_MEMPOOL_H + + +#include +#include +#include +#include + + +namespace xmrig { + + +template +class MemPool +{ +public: + MemPool() = default; + + + constexpr size_t chunkSize() const { return CHUNK_SIZE; } + inline size_t freeSize() const { return m_free.size() * CHUNK_SIZE; } + inline size_t size() const { return m_data.size() * CHUNK_SIZE * INIT_SIZE; } + + + inline char *allocate() + { + if (m_free.empty()) { + resize(); + } + + const size_t i = *m_free.begin(); + const size_t r = i / INIT_SIZE; + + char *ptr = m_data[r].data() + (i - r * INIT_SIZE) * CHUNK_SIZE; + + m_used.insert({ ptr, i }); + m_free.erase(i); + + return ptr; + } + + + inline void deallocate(const char *ptr) + { + if (ptr == nullptr) { + return; + } + + assert(m_used.count(ptr)); + + m_free.emplace(m_used[ptr]); + m_used.erase(ptr); + } + + +private: + inline void resize() + { + const size_t index = m_data.size(); + m_data[index]; + + for (size_t i = 0; i < INIT_SIZE; ++i) { + m_free.emplace((index * INIT_SIZE) + i); + } + } + + + std::map m_used; + std::map > m_data; + std::set m_free; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_MEMPOOL_H */ diff --git a/src/base/net/tools/NetBuffer.cpp b/src/base/net/tools/NetBuffer.cpp new file mode 100644 index 00000000..3303d971 --- /dev/null +++ b/src/base/net/tools/NetBuffer.cpp @@ -0,0 +1,83 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/net/tools/MemPool.h" +#include "base/net/tools/NetBuffer.h" + + +#include +#include + + +namespace xmrig { + + +static constexpr size_t kInitSize = 4; +static MemPool *pool = nullptr; + + +inline MemPool *getPool() +{ + if (!pool) { + pool = new MemPool(); + } + + return pool; +} + + +} // namespace xmrig + + +char *xmrig::NetBuffer::allocate() +{ + return getPool()->allocate(); +} + + +void xmrig::NetBuffer::destroy() +{ + if (!pool) { + return; + } + + assert(pool->freeSize() == pool->size()); + + delete pool; + pool = nullptr; +} + + +void xmrig::NetBuffer::onAlloc(uv_handle_t *, size_t, uv_buf_t *buf) +{ + buf->base = getPool()->allocate(); + buf->len = kChunkSize; +} + + +void xmrig::NetBuffer::release(const char *buf) +{ + getPool()->deallocate(buf); +} + + +void xmrig::NetBuffer::release(const uv_buf_t *buf) +{ + getPool()->deallocate(buf->base); +} diff --git a/src/base/net/tools/NetBuffer.h b/src/base/net/tools/NetBuffer.h new file mode 100644 index 00000000..14255bb0 --- /dev/null +++ b/src/base/net/tools/NetBuffer.h @@ -0,0 +1,49 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_NETBUFFER_H +#define XMRIG_NETBUFFER_H + + +struct uv_buf_t; +using uv_handle_t = struct uv_handle_s; + + +#include + + +namespace xmrig { + + +class NetBuffer +{ +public: + static constexpr size_t kChunkSize = 16 * 1024; + + static char *allocate(); + static void destroy(); + static void onAlloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); + static void release(const char *buf); + static void release(const uv_buf_t *buf); +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_NETBUFFER_H */ diff --git a/src/base/net/tools/RecvBuf.h b/src/base/net/tools/RecvBuf.h deleted file mode 100644 index 5122c980..00000000 --- a/src/base/net/tools/RecvBuf.h +++ /dev/null @@ -1,99 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef XMRIG_RECVBUF_H -#define XMRIG_RECVBUF_H - - -#include - - -#include "base/kernel/interfaces/ILineListener.h" - - -namespace xmrig { - - -template -class RecvBuf -{ -public: - inline RecvBuf() : - m_buf(), - m_pos(0) - { - } - - inline char *base() { return m_buf; } - inline char *current() { return m_buf + m_pos; } - inline const char *base() const { return m_buf; } - inline const char *current() const { return m_buf + m_pos; } - inline size_t available() const { return N - m_pos; } - inline size_t pos() const { return m_pos; } - inline void nread(size_t size) { m_pos += size; } - inline void reset() { m_pos = 0; } - - constexpr inline size_t size() const { return N; } - - inline void getline(ILineListener *listener) - { - char *end; - char *start = m_buf; - size_t remaining = m_pos; - - while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { - *end = '\0'; - - end++; - const size_t len = static_cast(end - start); - - listener->onLine(start, len - 1); - - remaining -= len; - start = end; - } - - if (remaining == 0) { - m_pos = 0; - return; - } - - if (start == m_buf) { - return; - } - - memcpy(m_buf, start, remaining); - m_pos = remaining; - } - -private: - char m_buf[N]; - size_t m_pos; -}; - - -} /* namespace xmrig */ - - -#endif /* XMRIG_RECVBUF_H */ diff --git a/src/base/net/tools/TcpServer.cpp b/src/base/net/tools/TcpServer.cpp index e5e6e4e1..06d4033a 100644 --- a/src/base/net/tools/TcpServer.cpp +++ b/src/base/net/tools/TcpServer.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,11 +34,11 @@ static const xmrig::String kLocalHost("127.0.0.1"); xmrig::TcpServer::TcpServer(const String &host, uint16_t port, ITcpServerListener *listener) : m_host(host.isNull() ? kLocalHost : host), - m_version(0), m_listener(listener), - m_addr(), m_port(port) { + assert(m_listener != nullptr); + m_tcp = new uv_tcp_t; uv_tcp_init(uv_default_loop(), m_tcp); m_tcp->data = this; diff --git a/src/base/net/tools/TcpServer.h b/src/base/net/tools/TcpServer.h index fc29fa2d..de464d37 100644 --- a/src/base/net/tools/TcpServer.h +++ b/src/base/net/tools/TcpServer.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,6 +29,9 @@ #include +#include "base/tools/Object.h" + + namespace xmrig { @@ -39,6 +42,8 @@ class String; class TcpServer { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(TcpServer) + TcpServer(const String &host, uint16_t port, ITcpServerListener *listener); ~TcpServer(); @@ -50,9 +55,9 @@ private: static void onConnection(uv_stream_t *stream, int status); const String &m_host; - int m_version; + int m_version = 0; ITcpServerListener *m_listener; - sockaddr_storage m_addr; + sockaddr_storage m_addr{}; uint16_t m_port; uv_tcp_t *m_tcp; }; diff --git a/src/base/tools/Arguments.cpp b/src/base/tools/Arguments.cpp index 933173aa..d0352939 100644 --- a/src/base/tools/Arguments.cpp +++ b/src/base/tools/Arguments.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,6 +52,23 @@ bool xmrig::Arguments::hasArg(const char *name) const } +const char *xmrig::Arguments::value(const char *key) const +{ + const size_t size = m_data.size(); + if (size < 3) { + return nullptr; + } + + for (size_t i = 1; i < size - 1; ++i) { + if (m_data[i] == key) { + return m_data[i + 1]; + } + } + + return nullptr; +} + + void xmrig::Arguments::add(const char *arg) { if (arg == nullptr) { diff --git a/src/base/tools/Arguments.h b/src/base/tools/Arguments.h index e47e39e3..0016c519 100644 --- a/src/base/tools/Arguments.h +++ b/src/base/tools/Arguments.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,6 +41,7 @@ public: Arguments(int argc, char **argv); bool hasArg(const char *name) const; + const char *value(const char *key) const; inline char **argv() const { return m_argv; } inline const std::vector &data() const { return m_data; } diff --git a/src/config.json b/src/config.json index ddbc7c72..26471e12 100644 --- a/src/config.json +++ b/src/config.json @@ -79,6 +79,15 @@ "retries": 5, "retry-pause": 5, "syslog": false, + "tls": { + "enabled": false, + "protocols": null, + "cert": null, + "cert_key": null, + "ciphers": null, + "ciphersuites": null, + "dhparam": null + }, "user-agent": null, "verbose": 0, "watch": true diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 55a448e2..254428fb 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -41,6 +41,7 @@ #include "core/Miner.h" #include "crypto/common/Nonce.h" #include "crypto/rx/Rx.h" +#include "crypto/astrobwt/AstroBWT.h" #include "rapidjson/document.h" #include "version.h" @@ -242,6 +243,10 @@ public: # endif +# ifdef XMRIG_ALGO_ASTROBWT + inline bool initAstroBWT() { return astrobwt::init(job); } +# endif + Algorithm algorithm; Algorithms algorithms; bool active = false; @@ -454,10 +459,14 @@ void xmrig::Miner::setJob(const Job &job, bool donate) d_ptr->userJobId = job.id(); } + bool ready = true; + # ifdef XMRIG_ALGO_RANDOMX - const bool ready = d_ptr->initRX(); -# else - constexpr const bool ready = true; + ready &= d_ptr->initRX(); +# endif + +# ifdef XMRIG_ALGO_ASTROBWT + ready &= d_ptr->initAstroBWT(); # endif mutex.unlock(); diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index acda3413..27312477 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -212,7 +212,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); Value api(kObjectType); - api.AddMember(StringRef(kApiId), m_apiId.toJSON(), allocator); + api.AddMember(StringRef(kApiId), m_apiId.toJSON(), allocator); api.AddMember(StringRef(kApiWorkerId), m_apiWorkerId.toJSON(), allocator); doc.AddMember(StringRef(kApi), api, allocator); @@ -246,6 +246,11 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember(StringRef(Pools::kRetries), m_pools.retries(), allocator); doc.AddMember(StringRef(Pools::kRetryPause), m_pools.retryPause(), allocator); doc.AddMember(StringRef(kSyslog), isSyslog(), allocator); + +# ifdef XMRIG_FEATURE_TLS + doc.AddMember(StringRef(kTls), m_tls.toJSON(doc), allocator); +# endif + doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator); doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator); doc.AddMember(StringRef(kWatch), m_watch, allocator); diff --git a/src/core/config/ConfigTransform.cpp b/src/core/config/ConfigTransform.cpp index ebbd7dbd..eccbd235 100644 --- a/src/core/config/ConfigTransform.cpp +++ b/src/core/config/ConfigTransform.cpp @@ -159,6 +159,9 @@ void xmrig::ConfigTransform::transform(rapidjson::Document &doc, int key, const # ifdef XMRIG_ALGO_ASTROBWT case IConfig::AstroBWTMaxSizeKey: /* --astrobwt-max-size */ return set(doc, kCpu, "astrobwt-max-size", static_cast(strtol(arg, nullptr, 10))); + + case IConfig::AstroBWTAVX2Key: /* --astrobwt-avx2 */ + return set(doc, kCpu, "astrobwt-avx2", true); # endif # ifdef XMRIG_ALGO_RANDOMX diff --git a/src/core/config/Config_platform.h b/src/core/config/Config_platform.h index 72b6ab31..7078dc5c 100644 --- a/src/core/config/Config_platform.h +++ b/src/core/config/Config_platform.h @@ -91,9 +91,17 @@ static const option options[] = { { "cpu-no-yield", 0, nullptr, IConfig::YieldKey }, { "verbose", 0, nullptr, IConfig::VerboseKey }, { "proxy", 1, nullptr, IConfig::ProxyKey }, + { "data-dir", 1, nullptr, IConfig::DataDirKey }, # ifdef XMRIG_FEATURE_TLS { "tls", 0, nullptr, IConfig::TlsKey }, { "tls-fingerprint", 1, nullptr, IConfig::FingerprintKey }, + { "tls-cert", 1, nullptr, IConfig::TlsCertKey }, + { "tls-cert-key", 1, nullptr, IConfig::TlsCertKeyKey }, + { "tls-dhparam", 1, nullptr, IConfig::TlsDHparamKey }, + { "tls-protocols", 1, nullptr, IConfig::TlsProtocolsKey }, + { "tls-ciphers", 1, nullptr, IConfig::TlsCiphersKey }, + { "tls-ciphersuites", 1, nullptr, IConfig::TlsCipherSuitesKey }, + { "tls-gen", 1, nullptr, IConfig::TlsGenKey }, # endif # ifdef XMRIG_FEATURE_ASM { "asm", 1, nullptr, IConfig::AssemblyKey }, @@ -111,6 +119,7 @@ static const option options[] = { # endif #ifdef XMRIG_ALGO_ASTROBWT { "astrobwt-max-size", 1, nullptr, IConfig::AstroBWTMaxSizeKey }, + { "astrobwt-avx2", 0, nullptr, IConfig::AstroBWTAVX2Key }, #endif # ifdef XMRIG_FEATURE_OPENCL { "opencl", 0, nullptr, IConfig::OclKey }, diff --git a/src/core/config/usage.h b/src/core/config/usage.h index 0bc9db8c..542ee4d3 100644 --- a/src/core/config/usage.h +++ b/src/core/config/usage.h @@ -96,6 +96,7 @@ static inline const std::string &usage() # ifdef XMRIG_ALGO_ASTROBWT u += " --astrobwt-max-size=N skip hashes with large stage 2 size, default: 550, min: 400, max: 1200\n"; + u += " --astrobwt-avx2 enable AVX2 optimizations for AstroBWT algorithm"; # endif # ifdef XMRIG_FEATURE_HTTP @@ -130,6 +131,17 @@ static inline const std::string &usage() u += " --no-nvml disable NVML (NVIDIA Management Library) support\n"; # endif +# ifdef XMRIG_FEATURE_TLS + u += "\nTLS:\n"; + u += " --tls-gen=HOSTNAME generate TLS certificate for specific hostname\n"; + u += " --tls-cert=FILE load TLS certificate chain from a file in the PEM format\n"; + u += " --tls-cert-key=FILE load TLS certificate private key from a file in the PEM format\n"; + u += " --tls-dhparam=FILE load DH parameters for DHE ciphers from a file in the PEM format\n"; + u += " --tls-protocols=N enable specified TLS protocols, example: \"TLSv1 TLSv1.1 TLSv1.2 TLSv1.3\"\n"; + u += " --tls-ciphers=S set list of available ciphers (TLSv1.2 and below)\n"; + u += " --tls-ciphersuites=S set list of available TLSv1.3 ciphersuites\n"; +# endif + u += "\nLogging:\n"; # ifdef HAVE_SYSLOG_H diff --git a/src/crypto/astrobwt/AstroBWT.cpp b/src/crypto/astrobwt/AstroBWT.cpp index ca325bff..47949e4c 100644 --- a/src/crypto/astrobwt/AstroBWT.cpp +++ b/src/crypto/astrobwt/AstroBWT.cpp @@ -30,6 +30,10 @@ #include "AstroBWT.h" #include "sha3.h" #include "crypto/cn/CryptoNight.h" +#include "base/net/stratum/Job.h" +#include "base/crypto/Algorithm.h" +#include "base/io/log/Log.h" +#include "backend/cpu/Cpu.h" #include constexpr int STAGE1_SIZE = 147253; @@ -38,6 +42,18 @@ constexpr int ALLOCATION_SIZE = (STAGE1_SIZE + 1048576) + (128 - (STAGE1_SIZE & constexpr int COUNTING_SORT_BITS = 10; constexpr int COUNTING_SORT_SIZE = 1 << COUNTING_SORT_BITS; +static bool astrobwtInitialized = false; + +#ifdef ASTROBWT_AVX2 +static bool hasAVX2 = false; + +extern "C" +#ifndef _MSC_VER +__attribute__((ms_abi)) +#endif +void SHA3_256_AVX2_ASM(const void* in, size_t inBytes, void* out); +#endif + #ifdef _MSC_VER #include @@ -155,7 +171,25 @@ void sort_indices(int N, const uint8_t* v, uint64_t* indices, uint64_t* tmp_indi } } -bool xmrig::astrobwt::astrobwt_dero(const void* input_data, uint32_t input_size, void* scratchpad, uint8_t* output_hash, int stage2_max_size) +bool xmrig::astrobwt::init(const xmrig::Job& job) +{ + if (job.algorithm().family() != xmrig::Algorithm::ASTROBWT) + return true; + + if (astrobwtInitialized) + return true; + +#ifdef ASTROBWT_AVX2 + if (xmrig::Cpu::info()->hasAVX2()) { + hasAVX2 = true; + } +#endif + + astrobwtInitialized = true; + return true; +} + +bool xmrig::astrobwt::astrobwt_dero(const void* input_data, uint32_t input_size, void* scratchpad, uint8_t* output_hash, int stage2_max_size, bool avx2) { uint8_t key[32]; uint8_t* scratchpad_ptr = (uint8_t*)(scratchpad) + 64; @@ -166,7 +200,12 @@ bool xmrig::astrobwt::astrobwt_dero(const void* input_data, uint32_t input_size, uint8_t* stage1_result = (uint8_t*)(tmp_indices); uint8_t* stage2_result = (uint8_t*)(tmp_indices); - sha3_HashBuffer(256, SHA3_FLAGS_NONE, input_data, input_size, key, sizeof(key)); +#ifdef ASTROBWT_AVX2 + if (hasAVX2 && avx2) + SHA3_256_AVX2_ASM(input_data, input_size, key); + else +#endif + sha3_HashBuffer(256, SHA3_FLAGS_NONE, input_data, input_size, key, sizeof(key)); Salsa20_XORKeyStream(key, stage1_output, STAGE1_SIZE); @@ -178,7 +217,12 @@ bool xmrig::astrobwt::astrobwt_dero(const void* input_data, uint32_t input_size, stage1_result[i] = tmp[indices[i] & ((1 << 21) - 1)]; } - sha3_HashBuffer(256, SHA3_FLAGS_NONE, stage1_result, STAGE1_SIZE + 1, key, sizeof(key)); +#ifdef ASTROBWT_AVX2 + if (hasAVX2 && avx2) + SHA3_256_AVX2_ASM(stage1_result, STAGE1_SIZE + 1, key); + else +#endif + sha3_HashBuffer(256, SHA3_FLAGS_NONE, stage1_result, STAGE1_SIZE + 1, key, sizeof(key)); const int stage2_size = STAGE1_SIZE + (*(uint32_t*)(key) & 0xfffff); if (stage2_size > stage2_max_size) @@ -203,7 +247,12 @@ bool xmrig::astrobwt::astrobwt_dero(const void* input_data, uint32_t input_size, stage2_result[i] = tmp[indices[i] & ((1 << 21) - 1)]; } - sha3_HashBuffer(256, SHA3_FLAGS_NONE, stage2_result, stage2_size + 1, output_hash, 32); +#ifdef ASTROBWT_AVX2 + if (hasAVX2 && avx2) + SHA3_256_AVX2_ASM(stage2_result, stage2_size + 1, output_hash); + else +#endif + sha3_HashBuffer(256, SHA3_FLAGS_NONE, stage2_result, stage2_size + 1, output_hash, 32); return true; } @@ -211,5 +260,5 @@ bool xmrig::astrobwt::astrobwt_dero(const void* input_data, uint32_t input_size, template<> void xmrig::astrobwt::single_hash(const uint8_t* input, size_t size, uint8_t* output, cryptonight_ctx** ctx, uint64_t) { - astrobwt_dero(input, static_cast(size), ctx[0]->memory, output, std::numeric_limits::max()); + astrobwt_dero(input, static_cast(size), ctx[0]->memory, output, std::numeric_limits::max(), true); } diff --git a/src/crypto/astrobwt/AstroBWT.h b/src/crypto/astrobwt/AstroBWT.h index 1419641e..e10dc8a5 100644 --- a/src/crypto/astrobwt/AstroBWT.h +++ b/src/crypto/astrobwt/AstroBWT.h @@ -33,9 +33,14 @@ struct cryptonight_ctx; -namespace xmrig { namespace astrobwt { +namespace xmrig { -bool astrobwt_dero(const void* input_data, uint32_t input_size, void* scratchpad, uint8_t* output_hash, int stage2_max_size); +class Job; + +namespace astrobwt { + +bool init(const Job&); +bool astrobwt_dero(const void* input_data, uint32_t input_size, void* scratchpad, uint8_t* output_hash, int stage2_max_size, bool avx2); template void single_hash(const uint8_t* input, size_t size, uint8_t* output, cryptonight_ctx** ctx, uint64_t); @@ -44,4 +49,4 @@ template<> void single_hash(const uint8_t* input, size_t size, uint8_t* output, cryptonight_ctx** ctx, uint64_t); -}} // namespace xmrig::argon2 +}} // namespace xmrig::astrobwt diff --git a/src/crypto/astrobwt/sha3_256_avx2.S b/src/crypto/astrobwt/sha3_256_avx2.S new file mode 100644 index 00000000..16dba72f --- /dev/null +++ b/src/crypto/astrobwt/sha3_256_avx2.S @@ -0,0 +1,53 @@ +;# XMRig +;# Copyright 2010 Jeff Garzik +;# Copyright 2012-2014 pooler +;# Copyright 2014 Lucas Jones +;# Copyright 2014-2016 Wolf9466 +;# Copyright 2016 Jay D Dee +;# Copyright 2017-2019 XMR-Stak , +;# Copyright 2018 Lee Clagett +;# Copyright 2018-2019 tevador +;# Copyright 2000 Transmeta Corporation +;# Copyright 2004-2008 H. Peter Anvin +;# Copyright 2018-2020 SChernykh +;# Copyright 2016-2020 XMRig , +;# +;# This program is free software: you can redistribute it and/or modify +;# it under the terms of the GNU General Public License as published by +;# the Free Software Foundation, either version 3 of the License, or +;# (at your option) any later version. +;# +;# This program is distributed in the hope that it will be useful, +;# but WITHOUT ANY WARRANTY; without even the implied warranty of +;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;# GNU General Public License for more details. +;# +;# You should have received a copy of the GNU General Public License +;# along with this program. If not, see . +;# + +.intel_syntax noprefix +#if defined(__APPLE__) +.text +#define DECL(x) _##x +#else +.section .text +#define DECL(x) x +#endif + +#define ALIGN .balign +#define dq .quad + +.global DECL(SHA3_256_AVX2_ASM) + +ALIGN 64 +DECL(SHA3_256_AVX2_ASM): + +#include "sha3_256_avx2.inc" + +KeccakF1600_AVX2_ASM: + lea r8,[rip+rot_left+96] + lea r9,[rip+rot_right+96] + lea r10,[rip+rndc] + +#include "sha3_256_keccakf1600_avx2.inc" diff --git a/src/crypto/astrobwt/sha3_256_avx2.asm b/src/crypto/astrobwt/sha3_256_avx2.asm new file mode 100644 index 00000000..f38e09f1 --- /dev/null +++ b/src/crypto/astrobwt/sha3_256_avx2.asm @@ -0,0 +1,45 @@ +;# XMRig +;# Copyright 2010 Jeff Garzik +;# Copyright 2012-2014 pooler +;# Copyright 2014 Lucas Jones +;# Copyright 2014-2016 Wolf9466 +;# Copyright 2016 Jay D Dee +;# Copyright 2017-2019 XMR-Stak , +;# Copyright 2018 Lee Clagett +;# Copyright 2018-2019 tevador +;# Copyright 2000 Transmeta Corporation +;# Copyright 2004-2008 H. Peter Anvin +;# Copyright 2018-2020 SChernykh +;# Copyright 2016-2020 XMRig , +;# +;# This program is free software: you can redistribute it and/or modify +;# it under the terms of the GNU General Public License as published by +;# the Free Software Foundation, either version 3 of the License, or +;# (at your option) any later version. +;# +;# This program is distributed in the hope that it will be useful, +;# but WITHOUT ANY WARRANTY; without even the implied warranty of +;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;# GNU General Public License for more details. +;# +;# You should have received a copy of the GNU General Public License +;# along with this program. If not, see . +;# + +_SHA3_256_AVX2_ASM SEGMENT PAGE READ EXECUTE +PUBLIC SHA3_256_AVX2_ASM + +ALIGN 64 +SHA3_256_AVX2_ASM: + +include sha3_256_avx2.inc + +KeccakF1600_AVX2_ASM: + lea r8,[rot_left+96] + lea r9,[rot_right+96] + lea r10,[rndc] + +include sha3_256_keccakf1600_avx2.inc + +_SHA3_256_AVX2_ASM ENDS +END diff --git a/src/crypto/astrobwt/sha3_256_avx2.inc b/src/crypto/astrobwt/sha3_256_avx2.inc new file mode 100644 index 00000000..81753dba --- /dev/null +++ b/src/crypto/astrobwt/sha3_256_avx2.inc @@ -0,0 +1,162 @@ +;# XMRig +;# Copyright 2010 Jeff Garzik +;# Copyright 2012-2014 pooler +;# Copyright 2014 Lucas Jones +;# Copyright 2014-2016 Wolf9466 +;# Copyright 2016 Jay D Dee +;# Copyright 2017-2019 XMR-Stak , +;# Copyright 2018 Lee Clagett +;# Copyright 2018-2019 tevador +;# Copyright 2000 Transmeta Corporation +;# Copyright 2004-2008 H. Peter Anvin +;# Copyright 2018-2020 SChernykh +;# Copyright 2016-2020 XMRig , +;# +;# This program is free software: you can redistribute it and/or modify +;# it under the terms of the GNU General Public License as published by +;# the Free Software Foundation, either version 3 of the License, or +;# (at your option) any later version. +;# +;# This program is distributed in the hope that it will be useful, +;# but WITHOUT ANY WARRANTY; without even the implied warranty of +;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;# GNU General Public License for more details. +;# +;# You should have received a copy of the GNU General Public License +;# along with this program. If not, see . +;# + + vzeroupper + + mov qword ptr [rsp+8],rbx + mov qword ptr [rsp+16],rsi + mov qword ptr [rsp+24],rdi + push rbp + push r12 + push r13 + push r14 + push r15 + + sub rsp, 80 + movdqu xmmword ptr [rsp+64], xmm6 + movdqu xmmword ptr [rsp+48], xmm7 + movdqu xmmword ptr [rsp+32], xmm8 + movdqu xmmword ptr [rsp+16], xmm9 + movdqu xmmword ptr [rsp+0], xmm10 + sub rsp, 80 + movdqu xmmword ptr [rsp+64], xmm11 + movdqu xmmword ptr [rsp+48], xmm12 + movdqu xmmword ptr [rsp+32], xmm13 + movdqu xmmword ptr [rsp+16], xmm14 + movdqu xmmword ptr [rsp+0], xmm15 + + sub rsp,320 + lea rbp,[rsp+64] + and rbp,-32 + vpxor xmm0,xmm0,xmm0 + xor edi,edi + mov dword ptr [rbp],50462976 + mov r12,rdx + mov dword ptr [rbp+4],169150212 + mov r14,rdx + mov dword ptr [rbp+8],218436623 + shr r14,3 + and r12d,7 + mov dword ptr [rbp+12],135009046 + mov r13,r8 + mov byte ptr [rbp+16],9 + mov rsi,rcx + mov ebx,edi + vmovdqa ymmword ptr [rbp+32],ymm0 + vmovdqa ymmword ptr [rbp+64],ymm0 + vmovdqa ymmword ptr [rbp+96],ymm0 + vmovdqa ymmword ptr [rbp+128],ymm0 + vmovdqa ymmword ptr [rbp+160],ymm0 + vmovdqa ymmword ptr [rbp+192],ymm0 + vmovdqa ymmword ptr [rbp+224],ymm0 + test r14,r14 + je sha3_main_loop_end + +sha3_main_loop: + movzx eax,byte ptr [rbp+rbx] + lea rcx,[rbp+32] + lea rcx,[rcx+rax*8] + mov rax,qword ptr [rsi] + xor qword ptr [rcx],rax + lea r15,[rbx+1] + cmp rbx,16 + jne skip_keccak + + lea rcx,[rbp+32] + call KeccakF1600_AVX2_ASM + +skip_keccak: + cmp rbx,16 + mov rax,rdi + cmovne rax,r15 + add rsi,8 + mov rbx,rax + sub r14,1 + jne sha3_main_loop + +sha3_main_loop_end: + mov rdx,rdi + test r12,r12 + je sha3_tail_loop_end + mov r8,rdi + +sha3_tail_loop: + movzx eax,byte ptr [rdx+rsi] + inc rdx + shlx rcx,rax,r8 + or rdi,rcx + add r8,8 + cmp rdx,r12 + jb sha3_tail_loop + +sha3_tail_loop_end: + movzx eax,byte ptr [rbp+rbx] + lea rdx,[rbp+32] + lea rdx,[rdx+rax*8] + mov ecx,6 + lea rax,[r12*8] + shlx rcx,rcx,rax + xor rcx,qword ptr [rdx] + mov eax,1 + shl rax,63 + xor rcx,rdi + mov qword ptr [rdx],rcx + xor qword ptr [rbp+104],rax + + lea rcx,[rbp+32] + call KeccakF1600_AVX2_ASM + + vmovups ymm0,ymmword ptr [rbp+32] + vmovups ymmword ptr [r13],ymm0 + vzeroupper + + add rsp,320 + + movdqu xmm15, xmmword ptr [rsp] + movdqu xmm14, xmmword ptr [rsp+16] + movdqu xmm13, xmmword ptr [rsp+32] + movdqu xmm12, xmmword ptr [rsp+48] + movdqu xmm11, xmmword ptr [rsp+64] + add rsp, 80 + movdqu xmm10, xmmword ptr [rsp] + movdqu xmm9, xmmword ptr [rsp+16] + movdqu xmm8, xmmword ptr [rsp+32] + movdqu xmm7, xmmword ptr [rsp+48] + movdqu xmm6, xmmword ptr [rsp+64] + add rsp, 80 + + pop r15 + pop r14 + pop r13 + pop r12 + pop rbp + mov rbx,qword ptr [rsp+8] + mov rsi,qword ptr [rsp+16] + mov rdi,qword ptr [rsp+24] + + ret diff --git a/src/crypto/astrobwt/sha3_256_keccakf1600_avx2.inc b/src/crypto/astrobwt/sha3_256_keccakf1600_avx2.inc new file mode 100644 index 00000000..a9bae47a --- /dev/null +++ b/src/crypto/astrobwt/sha3_256_keccakf1600_avx2.inc @@ -0,0 +1,203 @@ +;# XMRig +;# Copyright 2010 Jeff Garzik +;# Copyright 2012-2014 pooler +;# Copyright 2014 Lucas Jones +;# Copyright 2014-2016 Wolf9466 +;# Copyright 2016 Jay D Dee +;# Copyright 2017-2019 XMR-Stak , +;# Copyright 2018 Lee Clagett +;# Copyright 2018-2019 tevador +;# Copyright 2000 Transmeta Corporation +;# Copyright 2004-2008 H. Peter Anvin +;# Copyright 2018-2020 SChernykh +;# Copyright 2016-2020 XMRig , +;# +;# This program is free software: you can redistribute it and/or modify +;# it under the terms of the GNU General Public License as published by +;# the Free Software Foundation, either version 3 of the License, or +;# (at your option) any later version. +;# +;# This program is distributed in the hope that it will be useful, +;# but WITHOUT ANY WARRANTY; without even the implied warranty of +;# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;# GNU General Public License for more details. +;# +;# You should have received a copy of the GNU General Public License +;# along with this program. If not, see . +;# + + mov eax,24 + lea rcx,[rcx+96] + vpbroadcastq ymm0,QWORD PTR [rcx-96] + vmovdqu ymm1,YMMWORD PTR [rcx-88] + vmovdqu ymm2,YMMWORD PTR [rcx-56] + vmovdqu ymm3,YMMWORD PTR [rcx-24] + vmovdqu ymm4,YMMWORD PTR [rcx+8] + vmovdqu ymm5,YMMWORD PTR [rcx+40] + vmovdqu ymm6,YMMWORD PTR [rcx+72] + +ALIGN 64 +Loop_avx2: + vpshufd ymm13,ymm2,78 + vpxor ymm12,ymm5,ymm3 + vpxor ymm9,ymm4,ymm6 + vpxor ymm12,ymm12,ymm1 + vpxor ymm12,ymm12,ymm9 + vpermq ymm11,ymm12,147 + vpxor ymm13,ymm13,ymm2 + vpermq ymm7,ymm13,78 + vpsrlq ymm8,ymm12,63 + vpaddq ymm9,ymm12,ymm12 + vpor ymm8,ymm8,ymm9 + vpermq ymm15,ymm8,57 + vpxor ymm14,ymm8,ymm11 + vpermq ymm14,ymm14,0 + vpxor ymm13,ymm13,ymm0 + vpxor ymm13,ymm13,ymm7 + vpsrlq ymm7,ymm13,63 + vpaddq ymm8,ymm13,ymm13 + vpor ymm8,ymm8,ymm7 + vpxor ymm2,ymm2,ymm14 + vpxor ymm0,ymm0,ymm14 + vpblendd ymm15,ymm15,ymm8,192 + vpblendd ymm11,ymm11,ymm13,3 + vpxor ymm15,ymm15,ymm11 + vpsllvq ymm10,ymm2,YMMWORD PTR [r8-96] + vpsrlvq ymm2,ymm2,YMMWORD PTR [r9-96] + vpor ymm2,ymm2,ymm10 + vpxor ymm3,ymm3,ymm15 + vpsllvq ymm11,ymm3,YMMWORD PTR [r8-32] + vpsrlvq ymm3,ymm3,YMMWORD PTR [r9-32] + vpor ymm3,ymm3,ymm11 + vpxor ymm4,ymm4,ymm15 + vpsllvq ymm12,ymm4,YMMWORD PTR [r8] + vpsrlvq ymm4,ymm4,YMMWORD PTR [r9] + vpor ymm4,ymm4,ymm12 + vpxor ymm5,ymm5,ymm15 + vpsllvq ymm13,ymm5,YMMWORD PTR [r8+32] + vpsrlvq ymm5,ymm5,YMMWORD PTR [r9+32] + vpor ymm5,ymm5,ymm13 + vpxor ymm6,ymm6,ymm15 + vpermq ymm10,ymm2,141 + vpermq ymm11,ymm3,141 + vpsllvq ymm14,ymm6,YMMWORD PTR [r8+64] + vpsrlvq ymm8,ymm6,YMMWORD PTR [r9+64] + vpor ymm8,ymm8,ymm14 + vpxor ymm1,ymm1,ymm15 + vpermq ymm12,ymm4,27 + vpermq ymm13,ymm5,114 + vpsllvq ymm15,ymm1,YMMWORD PTR [r8-64] + vpsrlvq ymm9,ymm1,YMMWORD PTR [r9-64] + vpor ymm9,ymm9,ymm15 + vpsrldq ymm14,ymm8,8 + vpandn ymm7,ymm8,ymm14 + vpblendd ymm3,ymm9,ymm13,12 + vpblendd ymm15,ymm11,ymm9,12 + vpblendd ymm5,ymm10,ymm11,12 + vpblendd ymm14,ymm9,ymm10,12 + vpblendd ymm3,ymm3,ymm11,48 + vpblendd ymm15,ymm15,ymm12,48 + vpblendd ymm5,ymm5,ymm9,48 + vpblendd ymm14,ymm14,ymm13,48 + vpblendd ymm3,ymm3,ymm12,192 + vpblendd ymm15,ymm15,ymm13,192 + vpblendd ymm5,ymm5,ymm13,192 + vpblendd ymm14,ymm14,ymm11,192 + vpandn ymm3,ymm3,ymm15 + vpandn ymm5,ymm5,ymm14 + vpblendd ymm6,ymm12,ymm9,12 + vpblendd ymm15,ymm10,ymm12,12 + vpxor ymm3,ymm3,ymm10 + vpblendd ymm6,ymm6,ymm10,48 + vpblendd ymm15,ymm15,ymm11,48 + vpxor ymm5,ymm5,ymm12 + vpblendd ymm6,ymm6,ymm11,192 + vpblendd ymm15,ymm15,ymm9,192 + vpandn ymm6,ymm6,ymm15 + vpxor ymm6,ymm6,ymm13 + vpermq ymm4,ymm8,30 + vpblendd ymm15,ymm4,ymm0,48 + vpermq ymm1,ymm8,57 + vpblendd ymm1,ymm1,ymm0,192 + vpandn ymm1,ymm1,ymm15 + vpblendd ymm2,ymm11,ymm12,12 + vpblendd ymm14,ymm13,ymm11,12 + vpblendd ymm2,ymm2,ymm13,48 + vpblendd ymm14,ymm14,ymm10,48 + vpblendd ymm2,ymm2,ymm10,192 + vpblendd ymm14,ymm14,ymm12,192 + vpandn ymm2,ymm2,ymm14 + vpxor ymm2,ymm2,ymm9 + vpermq ymm7,ymm7,0 + vpermq ymm3,ymm3,27 + vpermq ymm5,ymm5,141 + vpermq ymm6,ymm6,114 + vpblendd ymm4,ymm13,ymm10,12 + vpblendd ymm14,ymm12,ymm13,12 + vpblendd ymm4,ymm4,ymm12,48 + vpblendd ymm14,ymm14,ymm9,48 + vpblendd ymm4,ymm4,ymm9,192 + vpblendd ymm14,ymm14,ymm10,192 + vpandn ymm4,ymm4,ymm14 + vpxor ymm0,ymm0,ymm7 + vpxor ymm1,ymm1,ymm8 + vpxor ymm4,ymm4,ymm11 + vpxor ymm0,ymm0,YMMWORD PTR [r10] + lea r10,[r10+32] + dec eax + jnz Loop_avx2 + + vmovq QWORD PTR [rcx-96],xmm0 + vmovdqu YMMWORD PTR [rcx-88],ymm1 + vmovdqu YMMWORD PTR [rcx-56],ymm2 + vmovdqu YMMWORD PTR [rcx-24],ymm3 + vmovdqu YMMWORD PTR [rcx+8],ymm4 + vmovdqu YMMWORD PTR [rcx+40],ymm5 + vmovdqu YMMWORD PTR [rcx+72],ymm6 + + ret + +ALIGN 32 +rot_left: + dq 3, 18, 36, 41 + dq 1, 62, 28, 27 + dq 45, 6, 56, 39 + dq 10, 61, 55, 8 + dq 2, 15, 25, 20 + dq 44, 43, 21, 14 + +ALIGN 32 +rot_right: + dq 64-3, 64-18, 64-36, 64-41 + dq 64-1, 64-62, 64-28, 64-27 + dq 64-45, 64-6, 64-56, 64-39 + dq 64-10, 64-61, 64-55, 64-8 + dq 64-2, 64-15, 64-25, 64-20 + dq 64-44, 64-43, 64-21, 64-14 + +ALIGN 32 +rndc: + dq 1, 1, 1, 1 + dq 32898, 32898, 32898, 32898 + dq 9223372036854808714, 9223372036854808714, 9223372036854808714, 9223372036854808714 + dq 9223372039002292224, 9223372039002292224, 9223372039002292224, 9223372039002292224 + dq 32907, 32907, 32907, 32907 + dq 2147483649, 2147483649, 2147483649, 2147483649 + dq 9223372039002292353, 9223372039002292353, 9223372039002292353, 9223372039002292353 + dq 9223372036854808585, 9223372036854808585, 9223372036854808585, 9223372036854808585 + dq 138, 138, 138, 138 + dq 136, 136, 136, 136 + dq 2147516425, 2147516425, 2147516425, 2147516425 + dq 2147483658, 2147483658, 2147483658, 2147483658 + dq 2147516555, 2147516555, 2147516555, 2147516555 + dq 9223372036854775947, 9223372036854775947, 9223372036854775947, 9223372036854775947 + dq 9223372036854808713, 9223372036854808713, 9223372036854808713, 9223372036854808713 + dq 9223372036854808579, 9223372036854808579, 9223372036854808579, 9223372036854808579 + dq 9223372036854808578, 9223372036854808578, 9223372036854808578, 9223372036854808578 + dq 9223372036854775936, 9223372036854775936, 9223372036854775936, 9223372036854775936 + dq 32778, 32778, 32778, 32778 + dq 9223372039002259466, 9223372039002259466, 9223372039002259466, 9223372039002259466 + dq 9223372039002292353, 9223372039002292353, 9223372039002292353, 9223372039002292353 + dq 9223372036854808704, 9223372036854808704, 9223372036854808704, 9223372036854808704 + dq 2147483649, 2147483649, 2147483649, 2147483649 + dq 9223372039002292232, 9223372039002292232, 9223372039002292232, 9223372039002292232 diff --git a/src/version.h b/src/version.h index 1a9c6442..c343527a 100644 --- a/src/version.h +++ b/src/version.h @@ -28,15 +28,15 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig miner" -#define APP_VERSION "5.9.0-mo3" +#define APP_VERSION "5.10.0-mo1" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2020 xmrig.com" #define APP_KIND "miner" #define APP_VER_MAJOR 5 -#define APP_VER_MINOR 9 -#define APP_VER_PATCH 1 +#define APP_VER_MINOR 10 +#define APP_VER_PATCH 0 #ifdef _MSC_VER # if (_MSC_VER >= 1920)