From 15b4244ea83679de0f3e1238ccb279ee0ec4b400 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 8 May 2017 10:29:25 +0300 Subject: [PATCH 01/17] Added --max-cpu-usage and --safe stub. --- options.c | 91 +++++++++++++++++++++++++++++++++---------------------- options.h | 2 ++ 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/options.c b/options.c index 59f8f6dac..302005ed4 100644 --- a/options.c +++ b/options.c @@ -36,21 +36,23 @@ #include "algo/cryptonight/cryptonight.h" -int64_t opt_affinity = -1L; -int opt_n_threads = 0; -int opt_algo_variant = 0; -int opt_retries = 5; -int opt_retry_pause = 5; -int opt_donate_level = DONATE_LEVEL; -bool opt_colors = true; -bool opt_keepalive = false; -bool opt_background = false; -bool opt_double_hash = false; -char *opt_url = NULL; -char *opt_backup_url = NULL; -char *opt_userpass = NULL; -char *opt_user = NULL; -char *opt_pass = NULL; +int64_t opt_affinity = -1L; +int opt_n_threads = 0; +int opt_algo_variant = 0; +int opt_retries = 5; +int opt_retry_pause = 5; +int opt_donate_level = DONATE_LEVEL; +int opt_max_cpu_usage = 75; +bool opt_colors = true; +bool opt_keepalive = false; +bool opt_background = false; +bool opt_double_hash = false; +bool opt_safe = false; +char *opt_url = NULL; +char *opt_backup_url = NULL; +char *opt_userpass = NULL; +char *opt_user = NULL; +char *opt_pass = NULL; static char const usage[] = "\ @@ -71,6 +73,8 @@ Options:\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ -B, --background run the miner in the background\n\ -c, --config=FILE load a JSON-format configuration file\n\ + --max-cpu-usage=N maximum cpu usage for automatic threads mode (default 75)\n\ + --safe safe adjust threads and av settings for current cpu\n\ -h, --help display this help and exit\n\ -V, --version output version information and exit\n\ "; @@ -80,24 +84,26 @@ static char const short_options[] = "a:c:khBp:Px:r:R:s:t:T:o:u:O:v:Vb:"; static struct option const options[] = { - { "algo", 1, NULL, 'a' }, - { "av", 1, NULL, 'v' }, - { "background", 0, NULL, 'B' }, - { "backup-url", 1, NULL, 'b' }, - { "config", 1, NULL, 'c' }, - { "cpu-affinity", 1, NULL, 1020 }, - { "donate-level", 1, NULL, 1003 }, - { "help", 0, NULL, 'h' }, - { "keepalive", 0, NULL ,'k' }, - { "no-color", 0, NULL, 1002 }, - { "pass", 1, NULL, 'p' }, - { "retries", 1, NULL, 'r' }, - { "retry-pause", 1, NULL, 'R' }, - { "threads", 1, NULL, 't' }, - { "url", 1, NULL, 'o' }, - { "user", 1, NULL, 'u' }, - { "userpass", 1, NULL, 'O' }, - { "version", 0, NULL, 'V' }, + { "algo", 1, NULL, 'a' }, + { "av", 1, NULL, 'v' }, + { "background", 0, NULL, 'B' }, + { "backup-url", 1, NULL, 'b' }, + { "config", 1, NULL, 'c' }, + { "cpu-affinity", 1, NULL, 1020 }, + { "donate-level", 1, NULL, 1003 }, + { "help", 0, NULL, 'h' }, + { "keepalive", 0, NULL ,'k' }, + { "max-cpu-usage", 1, NULL, 1004 }, + { "no-color", 0, NULL, 1002 }, + { "pass", 1, NULL, 'p' }, + { "retries", 1, NULL, 'r' }, + { "retry-pause", 1, NULL, 'R' }, + { "safe", 0, NULL, 1005 }, + { "threads", 1, NULL, 't' }, + { "url", 1, NULL, 'o' }, + { "user", 1, NULL, 'u' }, + { "userpass", 1, NULL, 'O' }, + { "version", 0, NULL, 'V' }, { 0, 0, 0, 0 } }; @@ -197,7 +203,20 @@ static void parse_arg(int key, char *arg) { opt_n_threads = v; break; - case 'k': + case 1004: /* --max-cpu-usage */ + v = atoi(arg); + if (v < 1 || v > 100) { + show_usage_and_exit(1); + } + + opt_max_cpu_usage = v; + break; + + case 1005: /* --safe */ + opt_safe = true; + break; + + case 'k': /* --keepalive */ opt_keepalive = true; break; @@ -227,7 +246,7 @@ static void parse_arg(int key, char *arg) { break; } - case 'B': + case 'B': /* --background */ opt_background = true; opt_colors = false; break; @@ -255,7 +274,7 @@ static void parse_arg(int key, char *arg) { opt_colors = false; break; - case 1003: + case 1003: /* --donate-level */ v = atoi(arg); if (v < 1 || v > 99) { show_usage_and_exit(1); diff --git a/options.h b/options.h index dfdf6fbb5..9f4a43a9d 100644 --- a/options.h +++ b/options.h @@ -47,6 +47,7 @@ extern bool opt_colors; extern bool opt_keepalive; extern bool opt_background; extern bool opt_double_hash; +extern bool opt_safe; extern char *opt_url; extern char *opt_backup_url; extern char *opt_userpass; @@ -57,6 +58,7 @@ extern int opt_algo_variant; extern int opt_retry_pause; extern int opt_retries; extern int opt_donate_level; +extern int opt_max_cpu_usage; extern int64_t opt_affinity; void parse_cmdline(int argc, char *argv[]); From a2574e1b1bd8d03aab90e93f9c657d0e726d3d55 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 8 May 2017 21:41:27 +0300 Subject: [PATCH 02/17] Added message if huge pages was enabled, but reboot required. --- utils/applog.c | 7 ++++++- utils/applog.h | 5 +++-- win/memory_win.c | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/utils/applog.c b/utils/applog.c index 60c69a0ce..e0043c865 100644 --- a/utils/applog.c +++ b/utils/applog.c @@ -75,6 +75,11 @@ void applog(int prio, const char *fmt, ...) prio = LOG_NOTICE; color = CL_CYN; break; + + case LOG_GREEN: + prio = LOG_NOTICE; + color = CL_LGR; + break; } } @@ -116,7 +121,7 @@ void applog_notime(int prio, const char *fmt, ...) if (opt_colors) { switch (prio) { case LOG_ERR: color = CL_RED; break; - case LOG_WARNING: color = CL_YLW; break; + case LOG_WARNING: color = CL_LYL; break; case LOG_NOTICE: color = CL_WHT; break; case LOG_INFO: color = ""; break; case LOG_DEBUG: color = CL_GRY; break; diff --git a/utils/applog.h b/utils/applog.h index bd808c80b..24a732265 100644 --- a/utils/applog.h +++ b/utils/applog.h @@ -30,7 +30,8 @@ enum { LOG_NOTICE, LOG_INFO, LOG_DEBUG, - LOG_BLUE = 0x10 + LOG_BLUE = 0x10, + LOG_GREEN }; #define CL_N "\x1B[0m" @@ -57,7 +58,7 @@ enum { #endif #define CL_LRD "\x1B[01;31m" /* light red */ #define CL_LGR "\x1B[01;32m" /* light green */ -#define CL_YL2 "\x1B[01;33m" /* yellow */ +#define CL_LYL "\x1B[01;33m" /* light yellow */ #define CL_LBL "\x1B[01;34m" /* light blue */ #define CL_LMA "\x1B[01;35m" /* light magenta */ #define CL_LCY "\x1B[01;36m" /* light cyan */ diff --git a/win/memory_win.c b/win/memory_win.c index e9158a663..4632f5a12 100644 --- a/win/memory_win.c +++ b/win/memory_win.c @@ -122,6 +122,7 @@ static BOOL ObtainLockPagesPrivilege() { LSA_UNICODE_STRING str = StringToLsaUnicodeString(_T(SE_LOCK_MEMORY_NAME)); if (LsaAddAccountRights(handle, user->User.Sid, &str, 1) == 0) { + applog_notime(LOG_WARNING, "Huge pages support was successfully enabled, but reboot required to use it"); result = TRUE; } From 03dbb85c823ee9e0aee80b55bf211d904226fbe7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 8 May 2017 23:06:00 +0300 Subject: [PATCH 03/17] Update test values. --- algo/cryptonight/cryptonight.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/algo/cryptonight/cryptonight.c b/algo/cryptonight/cryptonight.c index e0bd1d544..827feb7cd 100644 --- a/algo/cryptonight/cryptonight.c +++ b/algo/cryptonight/cryptonight.c @@ -39,11 +39,11 @@ const static char test_input[152] = { - 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, - 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, - 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, - 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, - 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, + 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, + 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, + 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, + 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, + 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, @@ -53,8 +53,8 @@ const static char test_input[152] = { const static char test_output[64] = { - 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, - 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00, + 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, + 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00 }; From 3b46f5eb6478e08b0c968f6ef12411f235029446 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 8 May 2017 23:28:39 +0300 Subject: [PATCH 04/17] Remove BMI2 av. --- CMakeLists.txt | 7 +- algo/cryptonight/bmi2/CMakeLists.txt | 2 - algo/cryptonight/cryptonight.c | 22 ++---- algo/cryptonight/cryptonight_av3_aesni_bmi2.c | 77 ------------------- ...v4_softaes.c => cryptonight_av3_softaes.c} | 2 +- ...ble.c => cryptonight_av4_softaes_double.c} | 2 +- options.c | 2 +- options.h | 5 +- 8 files changed, 14 insertions(+), 105 deletions(-) delete mode 100644 algo/cryptonight/bmi2/CMakeLists.txt delete mode 100644 algo/cryptonight/cryptonight_av3_aesni_bmi2.c rename algo/cryptonight/{cryptonight_av4_softaes.c => cryptonight_av3_softaes.c} (97%) rename algo/cryptonight/{cryptonight_av5_softaes_double.c => cryptonight_av4_softaes_double.c} (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 492a67faa..02f82f908 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,8 +42,8 @@ set(SOURCES algo/cryptonight/cryptonight.c algo/cryptonight/cryptonight_av1_aesni.c algo/cryptonight/cryptonight_av2_aesni_double.c - algo/cryptonight/cryptonight_av4_softaes.c - algo/cryptonight/cryptonight_av5_softaes_double.c + algo/cryptonight/cryptonight_av3_softaes.c + algo/cryptonight/cryptonight_av4_softaes_double.c util.c options.c stratum.c @@ -115,9 +115,8 @@ else() endif() if (CMAKE_SIZEOF_VOID_P EQUAL 8) - add_subdirectory(algo/cryptonight/bmi2) add_executable(xmrig ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS} ${SOURCES_CPUID}) - target_link_libraries(xmrig jansson curl cryptonight_av3_aesni_bmi2 ${CPUID_LIB} ${EXTRA_LIBS}) + target_link_libraries(xmrig jansson curl ${CPUID_LIB} ${EXTRA_LIBS}) else() add_executable(xmrig32 ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS} ${SOURCES_CPUID}) target_link_libraries(xmrig32 jansson curl ${CPUID_LIB} ${EXTRA_LIBS}) diff --git a/algo/cryptonight/bmi2/CMakeLists.txt b/algo/cryptonight/bmi2/CMakeLists.txt deleted file mode 100644 index c9a3fd856..000000000 --- a/algo/cryptonight/bmi2/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mbmi2") -add_library(cryptonight_av3_aesni_bmi2 STATIC ../cryptonight_av3_aesni_bmi2.c) diff --git a/algo/cryptonight/cryptonight.c b/algo/cryptonight/cryptonight.c index 827feb7cd..4b598033f 100644 --- a/algo/cryptonight/cryptonight.c +++ b/algo/cryptonight/cryptonight.c @@ -62,12 +62,8 @@ const static char test_output[64] = { void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av4_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av5_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); - -#if defined(__x86_64__) - void cryptonight_av3_aesni_bmi2(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -#endif +void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); void (*cryptonight_hash_ctx)(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) = NULL; @@ -99,19 +95,13 @@ bool cryptonight_init(int variant) cryptonight_hash_ctx = cryptonight_av2_aesni_double; break; -# if defined(__x86_64__) - case XMR_AV3_AESNI_BMI2: - cryptonight_hash_ctx = cryptonight_av3_aesni_bmi2; - break; -# endif - - case XMR_AV4_SOFT_AES: - cryptonight_hash_ctx = cryptonight_av4_softaes; + case XMR_AV3_SOFT_AES: + cryptonight_hash_ctx = cryptonight_av3_softaes; break; - case XMR_AV5_SOFT_AES_DOUBLE: + case XMR_AV4_SOFT_AES_DOUBLE: opt_double_hash = true; - cryptonight_hash_ctx = cryptonight_av5_softaes_double; + cryptonight_hash_ctx = cryptonight_av4_softaes_double; break; default: diff --git a/algo/cryptonight/cryptonight_av3_aesni_bmi2.c b/algo/cryptonight/cryptonight_av3_aesni_bmi2.c deleted file mode 100644 index 18582fce5..000000000 --- a/algo/cryptonight/cryptonight_av3_aesni_bmi2.c +++ /dev/null @@ -1,77 +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 fireice-uk - * Copyright 2016-2017 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 - -#include "cryptonight.h" -#include "cryptonight_aesni.h" -#include "crypto/c_keccak.h" - - -void cryptonight_av3_aesni_bmi2(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) -{ - keccak((const uint8_t *) input, size, ctx->state0, 200); - - cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); - - const uint8_t* l0 = ctx->memory; - uint64_t* h0 = (uint64_t*) ctx->state0; - - uint64_t al0 = h0[0] ^ h0[4]; - uint64_t ah0 = h0[1] ^ h0[5]; - __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); - - uint64_t idx0 = h0[0] ^ h0[4]; - - for (size_t i = 0; __builtin_expect(i < 0x80000, 1); i++) { - __m128i cx; - cx = _mm_load_si128((__m128i *)&l0[idx0 & 0x1FFFF0]); - cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); - - _mm_store_si128((__m128i *)&l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx)); - idx0 = _mm_cvtsi128_si64(cx); - bx0 = cx; - - uint64_t hi, lo, cl, ch; - cl = ((uint64_t*)&l0[idx0 & 0x1FFFF0])[0]; - ch = ((uint64_t*)&l0[idx0 & 0x1FFFF0])[1]; - lo = _mulx_u64(idx0, cl, &hi); - - al0 += hi; - ah0 += lo; - - ((uint64_t*)&l0[idx0 & 0x1FFFF0])[0] = al0; - ((uint64_t*)&l0[idx0 & 0x1FFFF0])[1] = ah0; - - ah0 ^= ch; - al0 ^= cl; - idx0 = al0; - } - - cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); - - keccakf(h0, 24); - extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); -} diff --git a/algo/cryptonight/cryptonight_av4_softaes.c b/algo/cryptonight/cryptonight_av3_softaes.c similarity index 97% rename from algo/cryptonight/cryptonight_av4_softaes.c rename to algo/cryptonight/cryptonight_av3_softaes.c index cd0fc6f23..22be894d8 100644 --- a/algo/cryptonight/cryptonight_av4_softaes.c +++ b/algo/cryptonight/cryptonight_av3_softaes.c @@ -30,7 +30,7 @@ #include "crypto/c_keccak.h" -void cryptonight_av4_softaes(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +void cryptonight_av3_softaes(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) { keccak((const uint8_t *) input, size, ctx->state0, 200); diff --git a/algo/cryptonight/cryptonight_av5_softaes_double.c b/algo/cryptonight/cryptonight_av4_softaes_double.c similarity index 98% rename from algo/cryptonight/cryptonight_av5_softaes_double.c rename to algo/cryptonight/cryptonight_av4_softaes_double.c index 113e228cb..afd4bebe1 100644 --- a/algo/cryptonight/cryptonight_av5_softaes_double.c +++ b/algo/cryptonight/cryptonight_av4_softaes_double.c @@ -30,7 +30,7 @@ #include "crypto/c_keccak.h" -void cryptonight_av5_softaes_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +void cryptonight_av4_softaes_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) { keccak((const uint8_t *) input, size, ctx->state0, 200); keccak((const uint8_t *) input + size, size, ctx->state1, 200); diff --git a/options.c b/options.c index 302005ed4..9be20a391 100644 --- a/options.c +++ b/options.c @@ -117,7 +117,7 @@ static int get_algo_variant(int variant) { return XMR_AV1_AESNI; } - return XMR_AV4_SOFT_AES; + return XMR_AV3_SOFT_AES; } diff --git a/options.h b/options.h index 9f4a43a9d..eb466fbaa 100644 --- a/options.h +++ b/options.h @@ -36,9 +36,8 @@ enum xmr_algo_variant { XMR_AV0_AUTO, XMR_AV1_AESNI, XMR_AV2_AESNI_DOUBLE, - XMR_AV3_AESNI_BMI2, - XMR_AV4_SOFT_AES, - XMR_AV5_SOFT_AES_DOUBLE, + XMR_AV3_SOFT_AES, + XMR_AV4_SOFT_AES_DOUBLE, XMR_AV_MAX }; From d3b0038bda4a54d1af2ddf4f14b3a33da84b84e7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 May 2017 12:58:52 +0300 Subject: [PATCH 05/17] Add optional CryptoNight-Lite support. --- CMakeLists.txt | 18 +- .../cryptonight-lite/cryptonight_lite_aesni.h | 256 ++++++++++++++++++ .../cryptonight_lite_av1_aesni.c | 77 ++++++ .../cryptonight_lite_av2_aesni_double.c | 111 ++++++++ .../cryptonight_lite_av3_softaes.c | 77 ++++++ .../cryptonight_lite_av4_softaes_double.c | 111 ++++++++ .../cryptonight_lite_softaes.h | 237 ++++++++++++++++ algo/cryptonight/cryptonight.c | 64 ++++- algo/cryptonight/cryptonight.h | 3 +- memory.c | 26 ++ options.c | 75 ++++- options.h | 20 ++ unix/memory_unix.c | 2 +- utils/summary.c | 4 +- win/memory_win.c | 2 +- 15 files changed, 1061 insertions(+), 22 deletions(-) create mode 100644 algo/cryptonight-lite/cryptonight_lite_aesni.h create mode 100644 algo/cryptonight-lite/cryptonight_lite_av1_aesni.c create mode 100644 algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c create mode 100644 algo/cryptonight-lite/cryptonight_lite_av3_softaes.c create mode 100644 algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c create mode 100644 algo/cryptonight-lite/cryptonight_lite_softaes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 02f82f908..0425d8e99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.0) project(xmrig C) -option(WITH_LIBCPUID "Use Libcpuid library" ON) +option(WITH_LIBCPUID "Use Libcpuid" ON) +option(WITH_AEON "CryptoNight-Lite support" ON) set(HEADERS compat.h @@ -114,8 +115,21 @@ else() set(SOURCES_CPUID cpu_stub.c) endif() +if (WITH_AEON) + set(SOURCES_AEON + algo/cryptonight-lite/cryptonight_lite_av1_aesni.c + algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c + algo/cryptonight-lite/cryptonight_lite_av3_softaes.c + algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c + algo/cryptonight-lite/cryptonight_lite_aesni.h + algo/cryptonight-lite/cryptonight_lite_softaes.h + ) +else() + add_definitions(/DXMRIG_NO_AEON) +endif() + if (CMAKE_SIZEOF_VOID_P EQUAL 8) - add_executable(xmrig ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS} ${SOURCES_CPUID}) + add_executable(xmrig ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS} ${SOURCES_CPUID} ${SOURCES_AEON}) target_link_libraries(xmrig jansson curl ${CPUID_LIB} ${EXTRA_LIBS}) else() add_executable(xmrig32 ${HEADERS} ${HEADERS_CRYPTO} ${SOURCES} ${SOURCES_CRYPTO} ${HEADERS_UTILS} ${SOURCES_UTILS} ${HEADERS_COMPAT} ${SOURCES_COMPAT} ${SOURCES_OS} ${SOURCES_CPUID}) diff --git a/algo/cryptonight-lite/cryptonight_lite_aesni.h b/algo/cryptonight-lite/cryptonight_lite_aesni.h new file mode 100644 index 000000000..fe062dac3 --- /dev/null +++ b/algo/cryptonight-lite/cryptonight_lite_aesni.h @@ -0,0 +1,256 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017 fireice-uk + * Copyright 2016-2017 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 __CRYPTONIGHT_AESNI_H__ +#define __CRYPTONIGHT_AESNI_H__ + +#include + + +#define aes_genkey_sub(imm8) \ + __m128i xout1 = _mm_aeskeygenassist_si128(*xout2, (imm8)); \ + xout1 = _mm_shuffle_epi32(xout1, 0xFF); \ + *xout0 = sl_xor(*xout0); \ + *xout0 = _mm_xor_si128(*xout0, xout1); \ + xout1 = _mm_aeskeygenassist_si128(*xout0, 0x00);\ + xout1 = _mm_shuffle_epi32(xout1, 0xAA); \ + *xout2 = sl_xor(*xout2); \ + *xout2 = _mm_xor_si128(*xout2, xout1); \ + + +// This will shift and xor tmp1 into itself as 4 32-bit vals such as +// sl_xor(a1 a2 a3 a4) = a1 (a2^a1) (a3^a2^a1) (a4^a3^a2^a1) +inline __m128i sl_xor(__m128i tmp1) +{ + __m128i tmp4; + tmp4 = _mm_slli_si128(tmp1, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + tmp4 = _mm_slli_si128(tmp4, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + tmp4 = _mm_slli_si128(tmp4, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + return tmp1; +} + + +inline void aes_genkey_sub1(__m128i* xout0, __m128i* xout2) +{ + aes_genkey_sub(0x1) +} + + +inline void aes_genkey_sub2(__m128i* xout0, __m128i* xout2) +{ + aes_genkey_sub(0x2) +} + + +inline void aes_genkey_sub4(__m128i* xout0, __m128i* xout2) +{ + aes_genkey_sub(0x4) +} + + +inline void aes_genkey_sub8(__m128i* xout0, __m128i* xout2) +{ + aes_genkey_sub(0x8) +} + + +inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) +{ + *x0 = _mm_aesenc_si128(*x0, key); + *x1 = _mm_aesenc_si128(*x1, key); + *x2 = _mm_aesenc_si128(*x2, key); + *x3 = _mm_aesenc_si128(*x3, key); + *x4 = _mm_aesenc_si128(*x4, key); + *x5 = _mm_aesenc_si128(*x5, key); + *x6 = _mm_aesenc_si128(*x6, key); + *x7 = _mm_aesenc_si128(*x7, key); +} + + +inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) +{ + __m128i xout0 = _mm_load_si128(memory); + __m128i xout2 = _mm_load_si128(memory + 1); + *k0 = xout0; + *k1 = xout2; + + aes_genkey_sub1(&xout0, &xout2); + *k2 = xout0; + *k3 = xout2; + + aes_genkey_sub2(&xout0, &xout2); + *k4 = xout0; + *k5 = xout2; + + aes_genkey_sub4(&xout0, &xout2); + *k6 = xout0; + *k7 = xout2; + + aes_genkey_sub8(&xout0, &xout2); + *k8 = xout0; + *k9 = xout2; +} + + +inline void cn_explode_scratchpad(const __m128i* input, __m128i* output) +{ + // This is more than we have registers, compiler will assign 2 keys on the stack + __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; + __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; + + aes_genkey(input, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); + + xin0 = _mm_load_si128(input + 4); + xin1 = _mm_load_si128(input + 5); + xin2 = _mm_load_si128(input + 6); + xin3 = _mm_load_si128(input + 7); + xin4 = _mm_load_si128(input + 8); + xin5 = _mm_load_si128(input + 9); + xin6 = _mm_load_si128(input + 10); + xin7 = _mm_load_si128(input + 11); + + for (size_t i = 0; __builtin_expect(i < MEMORY_LITE / sizeof(__m128i), 1); i += 8) { + aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + + _mm_store_si128(output + i + 0, xin0); + _mm_store_si128(output + i + 1, xin1); + _mm_store_si128(output + i + 2, xin2); + _mm_store_si128(output + i + 3, xin3); + _mm_store_si128(output + i + 4, xin4); + _mm_store_si128(output + i + 5, xin5); + _mm_store_si128(output + i + 6, xin6); + _mm_store_si128(output + i + 7, xin7); + } +} + + +inline void cn_implode_scratchpad(const __m128i* input, __m128i* output) +{ + // This is more than we have registers, compiler will assign 2 keys on the stack + __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; + __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; + + aes_genkey(output + 2, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); + + xout0 = _mm_load_si128(output + 4); + xout1 = _mm_load_si128(output + 5); + xout2 = _mm_load_si128(output + 6); + xout3 = _mm_load_si128(output + 7); + xout4 = _mm_load_si128(output + 8); + xout5 = _mm_load_si128(output + 9); + xout6 = _mm_load_si128(output + 10); + xout7 = _mm_load_si128(output + 11); + + for (size_t i = 0; __builtin_expect(i < MEMORY_LITE / sizeof(__m128i), 1); i += 8) + { + xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); + xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); + xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); + xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); + xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); + xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); + xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); + xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); + + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + _mm_store_si128(output + 4, xout0); + _mm_store_si128(output + 5, xout1); + _mm_store_si128(output + 6, xout2); + _mm_store_si128(output + 7, xout3); + _mm_store_si128(output + 8, xout4); + _mm_store_si128(output + 9, xout5); + _mm_store_si128(output + 10, xout6); + _mm_store_si128(output + 11, xout7); +} + + +#if defined(__x86_64__) +# define EXTRACT64(X) _mm_cvtsi128_si64(X) + +inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi) +{ + unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; + *hi = r >> 64; + return (uint64_t) r; +} +#elif defined(__i386__) +# define HI32(X) \ + _mm_srli_si128((X), 4) + + +# define EXTRACT64(X) \ + ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \ + ((uint64_t)(uint32_t)_mm_cvtsi128_si32(HI32(X)) << 32)) + +inline uint64_t _umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { + // multiplier = ab = a * 2^32 + b + // multiplicand = cd = c * 2^32 + d + // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d + uint64_t a = multiplier >> 32; + uint64_t b = multiplier & 0xFFFFFFFF; + uint64_t c = multiplicand >> 32; + uint64_t d = multiplicand & 0xFFFFFFFF; + + //uint64_t ac = a * c; + uint64_t ad = a * d; + //uint64_t bc = b * c; + uint64_t bd = b * d; + + uint64_t adbc = ad + (b * c); + uint64_t adbc_carry = adbc < ad ? 1 : 0; + + // multiplier * multiplicand = product_hi * 2^64 + product_lo + uint64_t product_lo = bd + (adbc << 32); + uint64_t product_lo_carry = product_lo < bd ? 1 : 0; + *product_hi = (a * c) + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry; + + return product_lo; +} +#endif + + +#endif /* __CRYPTONIGHT_AESNI_H__ */ diff --git a/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c b/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c new file mode 100644 index 000000000..80110fb2a --- /dev/null +++ b/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c @@ -0,0 +1,77 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017 fireice-uk + * Copyright 2016-2017 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 + +#include "algo/cryptonight/cryptonight.h" +#include "cryptonight_lite_aesni.h" +#include "crypto/c_keccak.h" + + +void cryptonight_lite_av1_aesni(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +{ + keccak((const uint8_t *) input, size, ctx->state0, 200); + + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + + const uint8_t* l0 = ctx->memory; + uint64_t* h0 = (uint64_t*) ctx->state0; + + uint64_t al0 = h0[0] ^ h0[4]; + uint64_t ah0 = h0[1] ^ h0[5]; + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + + uint64_t idx0 = h0[0] ^ h0[4]; + + for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { + __m128i cx; + cx = _mm_load_si128((__m128i *) &l0[idx0 & 0xFFFF0]); + cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); + + _mm_store_si128((__m128i *) &l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx)); + idx0 = EXTRACT64(cx); + bx0 = cx; + + uint64_t hi, lo, cl, ch; + cl = ((uint64_t*) &l0[idx0 & 0xFFFF0])[0]; + ch = ((uint64_t*) &l0[idx0 & 0xFFFF0])[1]; + lo = _umul128(idx0, cl, &hi); + + al0 += hi; + ah0 += lo; + + ((uint64_t*)&l0[idx0 & 0xFFFF0])[0] = al0; + ((uint64_t*)&l0[idx0 & 0xFFFF0])[1] = ah0; + + ah0 ^= ch; + al0 ^= cl; + idx0 = al0; + } + + cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + + keccakf(h0, 24); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); +} diff --git a/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c b/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c new file mode 100644 index 000000000..055435c6a --- /dev/null +++ b/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c @@ -0,0 +1,111 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017 fireice-uk + * Copyright 2016-2017 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 + +#include "algo/cryptonight/cryptonight.h" +#include "cryptonight_lite_aesni.h" +#include "crypto/c_keccak.h" + + +void cryptonight_lite_av2_aesni_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +{ + keccak((const uint8_t *) input, size, ctx->state0, 200); + keccak((const uint8_t *) input + size, size, ctx->state1, 200); + + const uint8_t* l0 = ctx->memory; + const uint8_t* l1 = ctx->memory + MEMORY_LITE; + uint64_t* h0 = (uint64_t*) ctx->state0; + uint64_t* h1 = (uint64_t*) ctx->state1; + + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); + cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); + + uint64_t al0 = h0[0] ^ h0[4]; + uint64_t al1 = h1[0] ^ h1[4]; + uint64_t ah0 = h0[1] ^ h0[5]; + uint64_t ah1 = h1[1] ^ h1[5]; + + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + + uint64_t idx0 = h0[0] ^ h0[4]; + uint64_t idx1 = h1[0] ^ h1[4]; + + for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { + __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & 0xFFFF0]); + __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & 0xFFFF0]); + + cx0 = _mm_aesenc_si128(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = _mm_aesenc_si128(cx1, _mm_set_epi64x(ah1, al1)); + + _mm_store_si128((__m128i *) &l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & 0xFFFF0], _mm_xor_si128(bx1, cx1)); + + idx0 = EXTRACT64(cx0); + idx1 = EXTRACT64(cx1); + + bx0 = cx0; + bx1 = cx1; + + uint64_t hi, lo, cl, ch; + cl = ((uint64_t*) &l0[idx0 & 0xFFFF0])[0]; + ch = ((uint64_t*) &l0[idx0 & 0xFFFF0])[1]; + lo = _umul128(idx0, cl, &hi); + + al0 += hi; + ah0 += lo; + + ((uint64_t*) &l0[idx0 & 0xFFFF0])[0] = al0; + ((uint64_t*) &l0[idx0 & 0xFFFF0])[1] = ah0; + + ah0 ^= ch; + al0 ^= cl; + idx0 = al0; + + cl = ((uint64_t*) &l1[idx1 & 0xFFFF0])[0]; + ch = ((uint64_t*) &l1[idx1 & 0xFFFF0])[1]; + lo = _umul128(idx1, cl, &hi); + + al1 += hi; + ah1 += lo; + + ((uint64_t*) &l1[idx1 & 0xFFFF0])[0] = al1; + ((uint64_t*) &l1[idx1 & 0xFFFF0])[1] = ah1; + + ah1 ^= ch; + al1 ^= cl; + idx1 = al1; + } + + cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); + cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); + + keccakf(h0, 24); + keccakf(h1, 24); + + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, (char*) output + 32); +} diff --git a/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c b/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c new file mode 100644 index 000000000..3dec6e335 --- /dev/null +++ b/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c @@ -0,0 +1,77 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017 fireice-uk + * Copyright 2016-2017 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 + +#include "algo/cryptonight/cryptonight.h" +#include "cryptonight_lite_softaes.h" +#include "crypto/c_keccak.h" + + +void cryptonight_lite_av3_softaes(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +{ + keccak((const uint8_t *) input, size, ctx->state0, 200); + + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); + + const uint8_t* l0 = ctx->memory; + uint64_t* h0 = (uint64_t*) ctx->state0; + + uint64_t al0 = h0[0] ^ h0[4]; + uint64_t ah0 = h0[1] ^ h0[5]; + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + + uint64_t idx0 = h0[0] ^ h0[4]; + + for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { + __m128i cx; + cx = _mm_load_si128((__m128i *)&l0[idx0 & 0xFFFF0]); + cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); + + _mm_store_si128((__m128i *)&l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx)); + idx0 = EXTRACT64(cx); + bx0 = cx; + + uint64_t hi, lo, cl, ch; + cl = ((uint64_t*)&l0[idx0 & 0xFFFF0])[0]; + ch = ((uint64_t*)&l0[idx0 & 0xFFFF0])[1]; + lo = _umul128(idx0, cl, &hi); + + al0 += hi; + ah0 += lo; + + ((uint64_t*)&l0[idx0 & 0xFFFF0])[0] = al0; + ((uint64_t*)&l0[idx0 & 0xFFFF0])[1] = ah0; + + ah0 ^= ch; + al0 ^= cl; + idx0 = al0; + } + + cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state0); + + keccakf(h0, 24); + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); +} diff --git a/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c b/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c new file mode 100644 index 000000000..873b8cac0 --- /dev/null +++ b/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c @@ -0,0 +1,111 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017 fireice-uk + * Copyright 2016-2017 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 + +#include "algo/cryptonight/cryptonight.h" +#include "cryptonight_lite_softaes.h" +#include "crypto/c_keccak.h" + + +void cryptonight_lite_av4_softaes_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +{ + keccak((const uint8_t *) input, size, ctx->state0, 200); + keccak((const uint8_t *) input + size, size, ctx->state1, 200); + + const uint8_t* l0 = ctx->memory; + const uint8_t* l1 = ctx->memory + MEMORY_LITE; + uint64_t* h0 = (uint64_t*) ctx->state0; + uint64_t* h1 = (uint64_t*) ctx->state1; + + cn_explode_scratchpad((__m128i*) h0, (__m128i*) l0); + cn_explode_scratchpad((__m128i*) h1, (__m128i*) l1); + + uint64_t al0 = h0[0] ^ h0[4]; + uint64_t al1 = h1[0] ^ h1[4]; + uint64_t ah0 = h0[1] ^ h0[5]; + uint64_t ah1 = h1[1] ^ h1[5]; + + __m128i bx0 = _mm_set_epi64x(h0[3] ^ h0[7], h0[2] ^ h0[6]); + __m128i bx1 = _mm_set_epi64x(h1[3] ^ h1[7], h1[2] ^ h1[6]); + + uint64_t idx0 = h0[0] ^ h0[4]; + uint64_t idx1 = h1[0] ^ h1[4]; + + for (size_t i = 0; __builtin_expect(i < 0x40000, 1); i++) { + __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & 0xFFFF0]); + __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & 0xFFFF0]); + + cx0 = soft_aesenc(cx0, _mm_set_epi64x(ah0, al0)); + cx1 = soft_aesenc(cx1, _mm_set_epi64x(ah1, al1)); + + _mm_store_si128((__m128i *) &l0[idx0 & 0xFFFF0], _mm_xor_si128(bx0, cx0)); + _mm_store_si128((__m128i *) &l1[idx1 & 0xFFFF0], _mm_xor_si128(bx1, cx1)); + + idx0 = EXTRACT64(cx0); + idx1 = EXTRACT64(cx1); + + bx0 = cx0; + bx1 = cx1; + + uint64_t hi, lo, cl, ch; + cl = ((uint64_t*) &l0[idx0 & 0xFFFF0])[0]; + ch = ((uint64_t*) &l0[idx0 & 0xFFFF0])[1]; + lo = _umul128(idx0, cl, &hi); + + al0 += hi; + ah0 += lo; + + ((uint64_t*) &l0[idx0 & 0xFFFF0])[0] = al0; + ((uint64_t*) &l0[idx0 & 0xFFFF0])[1] = ah0; + + ah0 ^= ch; + al0 ^= cl; + idx0 = al0; + + cl = ((uint64_t*) &l1[idx1 & 0xFFFF0])[0]; + ch = ((uint64_t*) &l1[idx1 & 0xFFFF0])[1]; + lo = _umul128(idx1, cl, &hi); + + al1 += hi; + ah1 += lo; + + ((uint64_t*) &l1[idx1 & 0xFFFF0])[0] = al1; + ((uint64_t*) &l1[idx1 & 0xFFFF0])[1] = ah1; + + ah1 ^= ch; + al1 ^= cl; + idx1 = al1; + } + + cn_implode_scratchpad((__m128i*) l0, (__m128i*) h0); + cn_implode_scratchpad((__m128i*) l1, (__m128i*) h1); + + keccakf(h0, 24); + keccakf(h1, 24); + + extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output); + extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, (char*) output + 32); +} diff --git a/algo/cryptonight-lite/cryptonight_lite_softaes.h b/algo/cryptonight-lite/cryptonight_lite_softaes.h new file mode 100644 index 000000000..51b70bc0b --- /dev/null +++ b/algo/cryptonight-lite/cryptonight_lite_softaes.h @@ -0,0 +1,237 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017 fireice-uk + * Copyright 2016-2017 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 __CRYPTONIGHT_SOFTAES_H__ +#define __CRYPTONIGHT_SOFTAES_H__ + +#include + +extern __m128i soft_aesenc(__m128i in, __m128i key); +extern __m128i soft_aeskeygenassist(__m128i key, uint8_t rcon); + + +// This will shift and xor tmp1 into itself as 4 32-bit vals such as +// sl_xor(a1 a2 a3 a4) = a1 (a2^a1) (a3^a2^a1) (a4^a3^a2^a1) +inline __m128i sl_xor(__m128i tmp1) +{ + __m128i tmp4; + tmp4 = _mm_slli_si128(tmp1, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + tmp4 = _mm_slli_si128(tmp4, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + tmp4 = _mm_slli_si128(tmp4, 0x04); + tmp1 = _mm_xor_si128(tmp1, tmp4); + return tmp1; +} + + +inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2, uint8_t rcon) +{ + __m128i xout1 = soft_aeskeygenassist(*xout2, rcon); + xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem + *xout0 = sl_xor(*xout0); + *xout0 = _mm_xor_si128(*xout0, xout1); + xout1 = soft_aeskeygenassist(*xout0, 0x00); + xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem + *xout2 = sl_xor(*xout2); + *xout2 = _mm_xor_si128(*xout2, xout1); +} + + +inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) +{ + *x0 = soft_aesenc(*x0, key); + *x1 = soft_aesenc(*x1, key); + *x2 = soft_aesenc(*x2, key); + *x3 = soft_aesenc(*x3, key); + *x4 = soft_aesenc(*x4, key); + *x5 = soft_aesenc(*x5, key); + *x6 = soft_aesenc(*x6, key); + *x7 = soft_aesenc(*x7, key); +} + + +inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) +{ + __m128i xout0 = _mm_load_si128(memory); + __m128i xout2 = _mm_load_si128(memory + 1); + *k0 = xout0; + *k1 = xout2; + + aes_genkey_sub(&xout0, &xout2, 0x1); + *k2 = xout0; + *k3 = xout2; + + aes_genkey_sub(&xout0, &xout2, 0x2); + *k4 = xout0; + *k5 = xout2; + + aes_genkey_sub(&xout0, &xout2, 0x4); + *k6 = xout0; + *k7 = xout2; + + aes_genkey_sub(&xout0, &xout2, 0x8); + *k8 = xout0; + *k9 = xout2; +} + + +inline void cn_explode_scratchpad(const __m128i* input, __m128i* output) +{ + // This is more than we have registers, compiler will assign 2 keys on the stack + __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7; + __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; + + aes_genkey(input, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); + + xin0 = _mm_load_si128(input + 4); + xin1 = _mm_load_si128(input + 5); + xin2 = _mm_load_si128(input + 6); + xin3 = _mm_load_si128(input + 7); + xin4 = _mm_load_si128(input + 8); + xin5 = _mm_load_si128(input + 9); + xin6 = _mm_load_si128(input + 10); + xin7 = _mm_load_si128(input + 11); + + for (size_t i = 0; i < MEMORY_LITE / sizeof(__m128i); i += 8) { + aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); + + _mm_store_si128(output + i + 0, xin0); + _mm_store_si128(output + i + 1, xin1); + _mm_store_si128(output + i + 2, xin2); + _mm_store_si128(output + i + 3, xin3); + _mm_store_si128(output + i + 4, xin4); + _mm_store_si128(output + i + 5, xin5); + _mm_store_si128(output + i + 6, xin6); + _mm_store_si128(output + i + 7, xin7); + } +} + + +inline void cn_implode_scratchpad(const __m128i* input, __m128i* output) +{ + // This is more than we have registers, compiler will assign 2 keys on the stack + __m128i xout0, xout1, xout2, xout3, xout4, xout5, xout6, xout7; + __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9; + + aes_genkey(output + 2, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9); + + xout0 = _mm_load_si128(output + 4); + xout1 = _mm_load_si128(output + 5); + xout2 = _mm_load_si128(output + 6); + xout3 = _mm_load_si128(output + 7); + xout4 = _mm_load_si128(output + 8); + xout5 = _mm_load_si128(output + 9); + xout6 = _mm_load_si128(output + 10); + xout7 = _mm_load_si128(output + 11); + + for (size_t i = 0; __builtin_expect(i < MEMORY_LITE / sizeof(__m128i), 1); i += 8) + { + xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); + xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); + xout2 = _mm_xor_si128(_mm_load_si128(input + i + 2), xout2); + xout3 = _mm_xor_si128(_mm_load_si128(input + i + 3), xout3); + xout4 = _mm_xor_si128(_mm_load_si128(input + i + 4), xout4); + xout5 = _mm_xor_si128(_mm_load_si128(input + i + 5), xout5); + xout6 = _mm_xor_si128(_mm_load_si128(input + i + 6), xout6); + xout7 = _mm_xor_si128(_mm_load_si128(input + i + 7), xout7); + + aes_round(k0, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k1, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k2, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k3, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k4, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k5, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k6, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k7, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k8, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + aes_round(k9, &xout0, &xout1, &xout2, &xout3, &xout4, &xout5, &xout6, &xout7); + } + + _mm_store_si128(output + 4, xout0); + _mm_store_si128(output + 5, xout1); + _mm_store_si128(output + 6, xout2); + _mm_store_si128(output + 7, xout3); + _mm_store_si128(output + 8, xout4); + _mm_store_si128(output + 9, xout5); + _mm_store_si128(output + 10, xout6); + _mm_store_si128(output + 11, xout7); +} + + +#if defined(__x86_64__) +# define EXTRACT64(X) _mm_cvtsi128_si64(X) + +inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi) +{ + unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; + *hi = r >> 64; + return (uint64_t) r; +} +#elif defined(__i386__) +# define HI32(X) \ + _mm_srli_si128((X), 4) + + +# define EXTRACT64(X) \ + ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \ + ((uint64_t)(uint32_t)_mm_cvtsi128_si32(HI32(X)) << 32)) + +inline uint64_t _umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { + // multiplier = ab = a * 2^32 + b + // multiplicand = cd = c * 2^32 + d + // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d + uint64_t a = multiplier >> 32; + uint64_t b = multiplier & 0xFFFFFFFF; + uint64_t c = multiplicand >> 32; + uint64_t d = multiplicand & 0xFFFFFFFF; + + //uint64_t ac = a * c; + uint64_t ad = a * d; + //uint64_t bc = b * c; + uint64_t bd = b * d; + + uint64_t adbc = ad + (b * c); + uint64_t adbc_carry = adbc < ad ? 1 : 0; + + // multiplier * multiplicand = product_hi * 2^64 + product_lo + uint64_t product_lo = bd + (adbc << 32); + uint64_t product_lo_carry = product_lo < bd ? 1 : 0; + *product_hi = (a * c) + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry; + + return product_lo; +} +#endif + + +#endif /* __CRYPTONIGHT_SOFTAES_H__ */ diff --git a/algo/cryptonight/cryptonight.c b/algo/cryptonight/cryptonight.c index 4b598033f..730187280 100644 --- a/algo/cryptonight/cryptonight.c +++ b/algo/cryptonight/cryptonight.c @@ -52,7 +52,7 @@ const static char test_input[152] = { }; -const static char test_output[64] = { +const static char test_output0[64] = { 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, @@ -65,10 +65,28 @@ void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +#ifndef XMRIG_NO_AEON +const static char test_output1[64] = { + 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, + 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, + 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, + 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88, +}; + +void cryptonight_lite_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_lite_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_lite_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_lite_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +#endif + void (*cryptonight_hash_ctx)(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) = NULL; static bool self_test() { + if (cryptonight_hash_ctx == NULL) { + return false; + } + char output[64]; struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); @@ -79,12 +97,54 @@ static bool self_test() { _mm_free(ctx->memory); _mm_free(ctx); - return memcmp(output, test_output, (opt_double_hash ? 64 : 32)) == 0; +# ifndef XMRIG_NO_AEON + if (opt_algo == ALGO_CRYPTONIGHT_LITE) { + return memcmp(output, test_output1, (opt_double_hash ? 64 : 32)) == 0; + } +# endif + + return memcmp(output, test_output0, (opt_double_hash ? 64 : 32)) == 0; } +#ifndef XMRIG_NO_AEON +bool cryptonight_lite_init(int variant) { + switch (variant) { + case AEON_AV1_AESNI: + cryptonight_hash_ctx = cryptonight_lite_av1_aesni; + break; + + case AEON_AV2_AESNI_DOUBLE: + opt_double_hash = true; + cryptonight_hash_ctx = cryptonight_lite_av2_aesni_double; + break; + + case AEON_AV3_SOFT_AES: + cryptonight_hash_ctx = cryptonight_lite_av3_softaes; + break; + + case AEON_AV4_SOFT_AES_DOUBLE: + opt_double_hash = true; + cryptonight_hash_ctx = cryptonight_lite_av4_softaes_double; + break; + + default: + break; + } + + return self_test(); +} +#endif + + bool cryptonight_init(int variant) { +# ifndef XMRIG_NO_AEON + if (opt_algo == ALGO_CRYPTONIGHT_LITE) { + return cryptonight_lite_init(variant); + } +# endif + switch (variant) { case XMR_AV1_AESNI: cryptonight_hash_ctx = cryptonight_av1_aesni; diff --git a/algo/cryptonight/cryptonight.h b/algo/cryptonight/cryptonight.h index 1770ac824..0b0170735 100644 --- a/algo/cryptonight/cryptonight.h +++ b/algo/cryptonight/cryptonight.h @@ -28,7 +28,8 @@ #include #include -#define MEMORY 2097152 /* 2 MiB */ +#define MEMORY 2097152 /* 2 MiB */ +#define MEMORY_LITE 1048576 /* 1 MiB */ struct cryptonight_ctx { uint8_t state0[200] __attribute__((aligned(16))); diff --git a/memory.c b/memory.c index 49435699f..2032a2548 100644 --- a/memory.c +++ b/memory.c @@ -30,6 +30,26 @@ static size_t offset = 0; +#ifndef XMRIG_NO_AEON +static void * create_persistent_ctx_lite(int thr_id) { + struct cryptonight_ctx *ctx = NULL; + + if (!opt_double_hash) { + const size_t offset = MEMORY * (thr_id + 1); + + ctx = (struct cryptonight_ctx *) &persistent_memory[offset + MEMORY_LITE]; + ctx->memory = &persistent_memory[offset]; + return ctx; + } + + ctx = (struct cryptonight_ctx *) &persistent_memory[MEMORY - sizeof(struct cryptonight_ctx) * (thr_id + 1)]; + ctx->memory = &persistent_memory[MEMORY * (thr_id + 1)]; + + return ctx; +} +#endif + + void * persistent_calloc(size_t num, size_t size) { void *mem = &persistent_memory[offset]; offset += (num * size); @@ -41,6 +61,12 @@ void * persistent_calloc(size_t num, size_t size) { void * create_persistent_ctx(int thr_id) { +# ifndef XMRIG_NO_AEON + if (opt_algo == ALGO_CRYPTONIGHT_LITE) { + return create_persistent_ctx_lite(thr_id); + } +# endif + struct cryptonight_ctx *ctx = (struct cryptonight_ctx *) &persistent_memory[MEMORY - sizeof(struct cryptonight_ctx) * (thr_id + 1)]; const int ratio = opt_double_hash ? 2 : 1; diff --git a/options.c b/options.c index 9be20a391..3f2eb1243 100644 --- a/options.c +++ b/options.c @@ -54,6 +54,8 @@ char *opt_userpass = NULL; char *opt_user = NULL; char *opt_pass = NULL; +enum mining_algo opt_algo = ALGO_CRYPTONIGHT; + static char const usage[] = "\ Usage: " APP_ID " [OPTIONS]\n\ @@ -108,16 +110,45 @@ static struct option const options[] = { }; -static int get_algo_variant(int variant) { - if (variant > XMR_AV0_AUTO && variant < XMR_AV_MAX) { - return variant; - } +static const char *algo_names[] = { + [ALGO_CRYPTONIGHT] = "cryptonight", +# ifndef XMRIG_NO_AEON + [ALGO_CRYPTONIGHT_LITE] = "cryptonight-lite" +# endif +}; - if (cpu_info.flags & CPU_FLAG_AES) { - return XMR_AV1_AESNI; - } - return XMR_AV3_SOFT_AES; +#ifndef XMRIG_NO_AEON +static int get_cryptonight_lite_variant(int variant) { + if (variant > AEON_AV0_AUTO && variant < AEON_AV_MAX) { + return variant; + } + + if (cpu_info.flags & CPU_FLAG_AES) { + return AEON_AV2_AESNI_DOUBLE; + } + + return AEON_AV4_SOFT_AES_DOUBLE; +} +#endif + + +static int get_algo_variant(int algo, int variant) { +# ifndef XMRIG_NO_AEON + if (algo == ALGO_CRYPTONIGHT_LITE) { + return get_cryptonight_lite_variant(variant); + } +# endif + + if (variant > XMR_AV0_AUTO && variant < XMR_AV_MAX) { + return variant; + } + + if (cpu_info.flags & CPU_FLAG_AES) { + return XMR_AV1_AESNI; + } + + return XMR_AV3_SOFT_AES; } @@ -133,6 +164,22 @@ static void parse_arg(int key, char *arg) { switch (key) { case 'a': + for (int i = 0; i < ARRAY_SIZE(algo_names); i++) { + if (algo_names[i] && !strcmp(arg, algo_names[i])) { + opt_algo = i; + break; + } + +# ifndef XMRIG_NO_AEON + if (i == ARRAY_SIZE(algo_names) && !strcmp(arg, "cryptonight-light")) { + opt_algo = i = ALGO_CRYPTONIGHT_LITE; + } +# endif + + if (i == ARRAY_SIZE(algo_names)) { + show_usage_and_exit(1); + } + } break; case 'O': /* --userpass */ @@ -253,7 +300,7 @@ static void parse_arg(int key, char *arg) { case 'v': /* --av */ v = atoi(arg); - if (v < 0 || v > XMR_AV_MAX) { + if (v < 0 || v > 1000) { show_usage_and_exit(1); } @@ -404,10 +451,7 @@ void parse_cmdline(int argc, char *argv[]) { opt_n_threads = get_optimal_threads_count(); } - opt_algo_variant = get_algo_variant(opt_algo_variant); - if (!opt_algo_variant) { - opt_algo_variant = get_algo_variant(0); - } + opt_algo_variant = get_algo_variant(opt_algo, opt_algo_variant); if (!cryptonight_init(opt_algo_variant)) { applog(LOG_ERR, "Cryptonight hash self-test failed. This might be caused by bad compiler optimizations."); @@ -454,3 +498,8 @@ void show_version_and_exit(void) { #endif proper_exit(0); } + + +const char* get_current_algo_name(void) { + return algo_names[opt_algo]; +} diff --git a/options.h b/options.h index eb466fbaa..2cc419158 100644 --- a/options.h +++ b/options.h @@ -32,6 +32,12 @@ #endif +enum mining_algo { + ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ + ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ +}; + + enum xmr_algo_variant { XMR_AV0_AUTO, XMR_AV1_AESNI, @@ -42,6 +48,18 @@ enum xmr_algo_variant { }; +#ifndef XMRIG_NO_AEON +enum aeon_algo_variant { + AEON_AV0_AUTO, + AEON_AV1_AESNI, + AEON_AV2_AESNI_DOUBLE, + AEON_AV3_SOFT_AES, + AEON_AV4_SOFT_AES_DOUBLE, + AEON_AV_MAX +}; +#endif + + extern bool opt_colors; extern bool opt_keepalive; extern bool opt_background; @@ -59,10 +77,12 @@ extern int opt_retries; extern int opt_donate_level; extern int opt_max_cpu_usage; extern int64_t opt_affinity; +extern enum mining_algo opt_algo; void parse_cmdline(int argc, char *argv[]); void show_usage_and_exit(int status); void show_version_and_exit(void); +const char* get_current_algo_name(void); extern void proper_exit(int reason); diff --git a/unix/memory_unix.c b/unix/memory_unix.c index bcfbe6078..fad7f730e 100644 --- a/unix/memory_unix.c +++ b/unix/memory_unix.c @@ -38,7 +38,7 @@ int persistent_memory_flags = 0; const char * persistent_memory_allocate() { - const int ratio = opt_double_hash ? 2 : 1; + const int ratio = (opt_double_hash && opt_algo != ALGO_CRYPTONIGHT_LITE) ? 2 : 1; const int size = MEMORY * (opt_n_threads * ratio + 1); persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE; diff --git a/utils/summary.c b/utils/summary.c index 3211bc099..357f5483c 100644 --- a/utils/summary.c +++ b/utils/summary.c @@ -72,10 +72,10 @@ static void print_cpu() { static void print_threads() { if (opt_colors) { - applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "THREADS: " CL_WHT "%d" CL_WHT ", av=%d, donate=%d%%", opt_n_threads, opt_algo_variant, opt_donate_level); + applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "THREADS: " CL_WHT "%d" CL_WHT ", av=%d, %s, donate=%d%%", opt_n_threads, opt_algo_variant, get_current_algo_name(), opt_donate_level); } else { - applog_notime(LOG_INFO, " * THREADS: %d, av=%d, donate=%d%%", opt_n_threads, opt_algo_variant, opt_donate_level); + applog_notime(LOG_INFO, " * THREADS: %d, av=%d, %s, donate=%d%%", opt_n_threads, opt_algo_variant, get_current_algo_name(), opt_donate_level); } } diff --git a/win/memory_win.c b/win/memory_win.c index 4632f5a12..9ff63dade 100644 --- a/win/memory_win.c +++ b/win/memory_win.c @@ -144,7 +144,7 @@ static BOOL TrySetLockPagesPrivilege() { const char * persistent_memory_allocate() { - const int ratio = opt_double_hash ? 2 : 1; + const int ratio = (opt_double_hash && opt_algo != ALGO_CRYPTONIGHT_LITE) ? 2 : 1; const int size = MEMORY * (opt_n_threads * ratio + 1); if (TrySetLockPagesPrivilege()) { From ff7be00f6f29503d6ee0d533f9538d651224cd38 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 May 2017 15:06:01 +0300 Subject: [PATCH 06/17] Fix test. --- test/cryptonight/CMakeLists.txt | 33 +++------ test/cryptonight/bmi2/CMakeLists.txt | 3 - test/cryptonight/cryptonight.c | 103 +++++++++++---------------- test/cryptonight/cryptonight32.c | 95 ------------------------ 4 files changed, 52 insertions(+), 182 deletions(-) delete mode 100644 test/cryptonight/bmi2/CMakeLists.txt delete mode 100644 test/cryptonight/cryptonight32.c diff --git a/test/cryptonight/CMakeLists.txt b/test/cryptonight/CMakeLists.txt index f15ca5fdb..4ebbbcc9b 100644 --- a/test/cryptonight/CMakeLists.txt +++ b/test/cryptonight/CMakeLists.txt @@ -1,7 +1,12 @@ set(SOURCES + cryptonight.c + ../../options.h ../../algo/cryptonight/cryptonight.h - ../../algo/cryptonight/cryptonight_common.c - ../../algo/cryptonight/cryptonight_av4_softaes.c + ../../algo/cryptonight/cryptonight.c + ../../algo/cryptonight/cryptonight_av1_aesni.c + ../../algo/cryptonight/cryptonight_av2_aesni_double.c + ../../algo/cryptonight/cryptonight_av3_softaes.c + ../../algo/cryptonight/cryptonight_av4_softaes_double.c ../../crypto/c_keccak.c ../../crypto/c_blake256.c ../../crypto/c_groestl.c @@ -10,32 +15,14 @@ set(SOURCES ../../crypto/soft_aes.c ) -if (CMAKE_SIZEOF_VOID_P EQUAL 8) - add_subdirectory(bmi2) - - add_executable(cryptonight_app ${SOURCES} - cryptonight.c - ../../algo/cryptonight/cryptonight_av1_aesni.c - ../../algo/cryptonight/cryptonight_av2_aesni_stak.c - ../../algo/cryptonight/cryptonight_av5_aesni_experimental.c - ) - - target_link_libraries(cryptonight_app unity cryptonight_av3_aesni_bmi2) -else() - add_executable(cryptonight_app ${SOURCES} - cryptonight32.c - ../../algo/cryptonight/cryptonight_av1_aesni32.c - ) - - target_link_libraries(cryptonight_app unity) -endif() - - +add_executable(cryptonight_app ${SOURCES}) +target_link_libraries(cryptonight_app unity) include_directories(../..) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -fno-strict-aliasing") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") add_definitions(-DBUILD_TEST) +add_definitions(-DXMRIG_NO_AEON) add_test(cryptonight_test cryptonight_app) diff --git a/test/cryptonight/bmi2/CMakeLists.txt b/test/cryptonight/bmi2/CMakeLists.txt deleted file mode 100644 index 785709a9f..000000000 --- a/test/cryptonight/bmi2/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -mbmi2") -include_directories(../../..) -add_library(cryptonight_av3_aesni_bmi2 STATIC ../../../algo/cryptonight/cryptonight_av3_aesni_bmi2.c) diff --git a/test/cryptonight/cryptonight.c b/test/cryptonight/cryptonight.c index 73c0c582f..bcc0db304 100644 --- a/test/cryptonight/cryptonight.c +++ b/test/cryptonight/cryptonight.c @@ -1,14 +1,25 @@ #include #include #include -#include #include +#include -const static char input1[76] = { - 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, 0x7C, 0xBF, 0x34, - 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, - 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, - 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01 +#include "options.h" +#include "algo/cryptonight/cryptonight.h" + +bool opt_double_hash = false; + +const static char input1[152] = { + 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, + 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, + 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, + 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, + 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, + 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, + 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, + 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, + 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, + 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, }; const static char input2[] = "This is a test"; @@ -16,16 +27,16 @@ const static char input3[] = "Lorem ipsum dolor sit amet, consectetur adipiscing void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av2_aesni_stak(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av3_aesni_bmi2(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av4_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -void cryptonight_av5_aesni_experimental(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); -static char hash[32]; -#define RESULT1 "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100" -#define RESULT2 "a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605" -#define RESULT3 "0bbe54bd26caa92a1d436eec71cbef02560062fa689fe14d7efcf42566b411cf" +static char hash[64]; +#define RESULT1 "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100" +#define RESULT1_DOUBLE "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd0801001b606a3f4a07d6489a1bcd07697bd16696b61c8ae982f61a90160f4e52828a7f" +#define RESULT2 "a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605" +#define RESULT3 "0bbe54bd26caa92a1d436eec71cbef02560062fa689fe14d7efcf42566b411cf" static char *bin2hex(const unsigned char *p, size_t len) @@ -43,24 +54,24 @@ static char *bin2hex(const unsigned char *p, size_t len) } -static void * create_ctx() { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx)); - ctx->memory = (uint8_t *) malloc(MEMORY); +static void * create_ctx(int ratio) { + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); + ctx->memory = (uint8_t *) _mm_malloc(MEMORY * ratio, 16); return ctx; } static void free_ctx(struct cryptonight_ctx *ctx) { - free(ctx->memory); - free(ctx); + _mm_free(ctx->memory); + _mm_free(ctx); } void test_cryptonight_av1_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - cryptonight_av1_aesni(input1, sizeof(input1), &hash, ctx); + cryptonight_av1_aesni(input1, 76, &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); cryptonight_av1_aesni(input2, strlen(input2), &hash, ctx); @@ -75,16 +86,10 @@ void test_cryptonight_av1_should_CalcHash(void) { void test_cryptonight_av2_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - cryptonight_av2_aesni_stak(input1, sizeof(input1), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av2_aesni_stak(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av2_aesni_stak(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); + cryptonight_av2_aesni_double(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); free_ctx(ctx); } @@ -92,15 +97,15 @@ void test_cryptonight_av2_should_CalcHash(void) void test_cryptonight_av3_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); - cryptonight_av3_aesni_bmi2(input1, sizeof(input1), &hash, ctx); + cryptonight_av3_softaes(input1, 76, &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - cryptonight_av3_aesni_bmi2(input2, strlen(input2), &hash, ctx); + cryptonight_av3_softaes(input2, strlen(input2), &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - cryptonight_av3_aesni_bmi2(input3, strlen(input3), &hash, ctx); + cryptonight_av3_softaes(input3, strlen(input3), &hash, ctx); TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); free_ctx(ctx); @@ -109,33 +114,10 @@ void test_cryptonight_av3_should_CalcHash(void) void test_cryptonight_av4_should_CalcHash(void) { - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); - cryptonight_av4_softaes(input1, sizeof(input1), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av4_softaes(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av4_softaes(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); - - free_ctx(ctx); -} - - -void test_cryptonight_av5_should_CalcHash(void) -{ - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(); - - cryptonight_av5_aesni_experimental(input1, sizeof(input1), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); - - cryptonight_av5_aesni_experimental(input2, strlen(input2), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT2, bin2hex(hash, 32)); - - cryptonight_av5_aesni_experimental(input3, strlen(input3), &hash, ctx); - TEST_ASSERT_EQUAL_STRING(RESULT3, bin2hex(hash, 32)); + cryptonight_av4_softaes_double(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); free_ctx(ctx); } @@ -149,7 +131,6 @@ int main(void) RUN_TEST(test_cryptonight_av2_should_CalcHash); RUN_TEST(test_cryptonight_av3_should_CalcHash); RUN_TEST(test_cryptonight_av4_should_CalcHash); - RUN_TEST(test_cryptonight_av5_should_CalcHash); return UNITY_END(); } diff --git a/test/cryptonight/cryptonight32.c b/test/cryptonight/cryptonight32.c deleted file mode 100644 index 997d8ef92..000000000 --- a/test/cryptonight/cryptonight32.c +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include - - -void cryptonight_av1_aesni32(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx); -void cryptonight_av4_legacy(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx); - - -char *bin2hex(const unsigned char *p, size_t len) -{ - int i; - char *s = malloc((len * 2) + 1); - if (!s) - return NULL; - - for (i = 0; i < len; i++) - sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); - - return s; -} - -bool hex2bin(unsigned char *p, const char *hexstr, size_t len) -{ - char hex_byte[3]; - char *ep; - - hex_byte[2] = '\0'; - - while (*hexstr && len) { - if (!hexstr[1]) { - return false; - } - hex_byte[0] = hexstr[0]; - hex_byte[1] = hexstr[1]; - *p = (unsigned char) strtol(hex_byte, &ep, 16); - if (*ep) { - return false; - } - p++; - hexstr += 2; - len--; - } - - return (len == 0 && *hexstr == 0) ? true : false; -} - - -void test_cryptonight_av1_32_should_CalcHash(void) { - char hash[32]; - char data[76]; - - hex2bin((unsigned char *) &data, "0305a0dbd6bf05cf16e503f3a66f78007cbf34144332ecbfc22ed95c8700383b309ace1923a0964b00000008ba939a62724c0d7581fce5761e9d8a0e6a1c3f924fdd8493d1115649c05eb601", 76); - - uint8_t *memory = (uint8_t *) malloc(MEMORY); - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); - - cryptonight_av1_aesni32(&hash, data, memory, ctx); - - free(memory); - free(ctx); - - TEST_ASSERT_EQUAL_STRING("1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100", bin2hex(hash, 32)); -} - - -void test_cryptonight_av4_should_CalcHash(void) -{ - char hash[32]; - char data[76]; - - hex2bin((unsigned char *) &data, "0305a0dbd6bf05cf16e503f3a66f78007cbf34144332ecbfc22ed95c8700383b309ace1923a0964b00000008ba939a62724c0d7581fce5761e9d8a0e6a1c3f924fdd8493d1115649c05eb601", 76); - - uint8_t *memory = (uint8_t *) malloc(MEMORY); - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); - - cryptonight_av4_legacy(&hash, data, memory, ctx); - - free(memory); - free(ctx); - - TEST_ASSERT_EQUAL_STRING("1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100", bin2hex(hash, 32)); -} - - -int main(void) -{ - UNITY_BEGIN(); - - RUN_TEST(test_cryptonight_av1_32_should_CalcHash); - RUN_TEST(test_cryptonight_av4_should_CalcHash); - - return UNITY_END(); -} From 719601f92b0f7e97802454a5fa719f8eb75b7920 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 May 2017 15:31:29 +0300 Subject: [PATCH 07/17] Add test for cryptonight lite. --- .../cryptonight-lite/cryptonight_lite_aesni.h | 6 +- .../cryptonight_lite_softaes.h | 6 +- test/CMakeLists.txt | 3 +- test/cryptonight_lite/CMakeLists.txt | 27 ++++ test/cryptonight_lite/cryptonight_lite.c | 124 ++++++++++++++++++ 5 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 test/cryptonight_lite/CMakeLists.txt create mode 100644 test/cryptonight_lite/cryptonight_lite.c diff --git a/algo/cryptonight-lite/cryptonight_lite_aesni.h b/algo/cryptonight-lite/cryptonight_lite_aesni.h index fe062dac3..6c9d15cbf 100644 --- a/algo/cryptonight-lite/cryptonight_lite_aesni.h +++ b/algo/cryptonight-lite/cryptonight_lite_aesni.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_AESNI_H__ -#define __CRYPTONIGHT_AESNI_H__ +#ifndef __CRYPTONIGHT_LITE_AESNI_H__ +#define __CRYPTONIGHT_LITE_AESNI_H__ #include @@ -253,4 +253,4 @@ inline uint64_t _umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *p #endif -#endif /* __CRYPTONIGHT_AESNI_H__ */ +#endif /* __CRYPTONIGHT_LITE_AESNI_H__ */ diff --git a/algo/cryptonight-lite/cryptonight_lite_softaes.h b/algo/cryptonight-lite/cryptonight_lite_softaes.h index 51b70bc0b..15a0a574e 100644 --- a/algo/cryptonight-lite/cryptonight_lite_softaes.h +++ b/algo/cryptonight-lite/cryptonight_lite_softaes.h @@ -22,8 +22,8 @@ * along with this program. If not, see . */ -#ifndef __CRYPTONIGHT_SOFTAES_H__ -#define __CRYPTONIGHT_SOFTAES_H__ +#ifndef __CRYPTONIGHT_LITE_SOFTAES_H__ +#define __CRYPTONIGHT_LITE_SOFTAES_H__ #include @@ -234,4 +234,4 @@ inline uint64_t _umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *p #endif -#endif /* __CRYPTONIGHT_SOFTAES_H__ */ +#endif /* __CRYPTONIGHT_LITE_SOFTAES_H__ */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5e907cf25..bfc8ea339 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,4 +4,5 @@ cmake_minimum_required(VERSION 3.0) include(CTest) add_subdirectory(unity) -add_subdirectory(cryptonight) \ No newline at end of file +add_subdirectory(cryptonight) +add_subdirectory(cryptonight_lite) \ No newline at end of file diff --git a/test/cryptonight_lite/CMakeLists.txt b/test/cryptonight_lite/CMakeLists.txt new file mode 100644 index 000000000..9a83ecbcc --- /dev/null +++ b/test/cryptonight_lite/CMakeLists.txt @@ -0,0 +1,27 @@ +set(SOURCES + cryptonight_lite.c + ../../options.h + ../../algo/cryptonight/cryptonight.h + ../../algo/cryptonight/cryptonight.c + ../../algo/cryptonight-lite/cryptonight_lite_av1_aesni.c + ../../algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c + ../../algo/cryptonight-lite/cryptonight_lite_av3_softaes.c + ../../algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c + ../../crypto/c_keccak.c + ../../crypto/c_blake256.c + ../../crypto/c_groestl.c + ../../crypto/c_jh.c + ../../crypto/c_skein.c + ../../crypto/soft_aes.c + ) + +add_executable(cryptonight_lite_app ${SOURCES}) +target_link_libraries(cryptonight_lite_app unity) + +include_directories(../..) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -fno-strict-aliasing") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") +add_definitions(-DBUILD_TEST) + +add_test(cryptonight_lite_test cryptonight_lite_app) diff --git a/test/cryptonight_lite/cryptonight_lite.c b/test/cryptonight_lite/cryptonight_lite.c new file mode 100644 index 000000000..a6d5b5540 --- /dev/null +++ b/test/cryptonight_lite/cryptonight_lite.c @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + +#include "options.h" +#include "algo/cryptonight/cryptonight.h" + +bool opt_double_hash = false; +enum mining_algo opt_algo = ALGO_CRYPTONIGHT_LITE; + +const static char input1[152] = { + 0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00, + 0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B, + 0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62, + 0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92, + 0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01, + 0x01, 0x00, 0xFB, 0x8E, 0x8A, 0xC8, 0x05, 0x89, 0x93, 0x23, 0x37, 0x1B, 0xB7, 0x90, 0xDB, 0x19, + 0x21, 0x8A, 0xFD, 0x8D, 0xB8, 0xE3, 0x75, 0x5D, 0x8B, 0x90, 0xF3, 0x9B, 0x3D, 0x55, 0x06, 0xA9, + 0xAB, 0xCE, 0x4F, 0xA9, 0x12, 0x24, 0x45, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x81, 0x46, 0xD4, 0x9F, + 0xA9, 0x3E, 0xE7, 0x24, 0xDE, 0xB5, 0x7D, 0x12, 0xCB, 0xC6, 0xC6, 0xF3, 0xB9, 0x24, 0xD9, 0x46, + 0x12, 0x7C, 0x7A, 0x97, 0x41, 0x8F, 0x93, 0x48, 0x82, 0x8F, 0x0F, 0x02, +}; + + +void cryptonight_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} +void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} +void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} +void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) {} + +void cryptonight_lite_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_lite_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_lite_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); +void cryptonight_lite_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx); + + +static char hash[64]; +#define RESULT1 "3695b4b53bb00358b0ad38dc160feb9e004eece09b83a72ef6ba9864d3510c88" +#define RESULT1_DOUBLE "3695b4b53bb00358b0ad38dc160feb9e004eece09b83a72ef6ba9864d3510c8828a22bad3f93d1408fca472eb5ad1cbe75f21d053c8ce5b3af105a57713e21dd" + + +static char *bin2hex(const unsigned char *p, size_t len) +{ + char *s = malloc((len * 2) + 1); + if (!s) { + return NULL; + } + + for (int i = 0; i < len; i++) { + sprintf(s + (i * 2), "%02x", (unsigned int) p[i]); + } + + return s; +} + + +static void * create_ctx(int ratio) { + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); + ctx->memory = (uint8_t *) _mm_malloc(MEMORY_LITE * ratio, 16); + + return ctx; +} + + +static void free_ctx(struct cryptonight_ctx *ctx) { + _mm_free(ctx->memory); + _mm_free(ctx); +} + + +void test_cryptonight_lite_av1_should_CalcHash(void) { + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); + + cryptonight_lite_av1_aesni(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); + + free_ctx(ctx); +} + + +void test_cryptonight_lite_av2_should_CalcHash(void) +{ + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); + + cryptonight_lite_av2_aesni_double(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); + + free_ctx(ctx); +} + + +void test_cryptonight_lite_av3_should_CalcHash(void) { + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(1); + + cryptonight_lite_av3_softaes(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1, bin2hex(hash, 32)); + + free_ctx(ctx); +} + + +void test_cryptonight_lite_av4_should_CalcHash(void) +{ + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx(2); + + cryptonight_lite_av4_softaes_double(input1, 76, &hash, ctx); + TEST_ASSERT_EQUAL_STRING(RESULT1_DOUBLE, bin2hex(hash, 64)); + + free_ctx(ctx); +} + + +int main(void) +{ + UNITY_BEGIN(); + + RUN_TEST(test_cryptonight_lite_av1_should_CalcHash); + RUN_TEST(test_cryptonight_lite_av2_should_CalcHash); + RUN_TEST(test_cryptonight_lite_av3_should_CalcHash); + RUN_TEST(test_cryptonight_lite_av4_should_CalcHash); + + return UNITY_END(); +} From c4bccf410bc51338d80fa452c5864e0801e1c961 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 May 2017 19:38:35 +0300 Subject: [PATCH 08/17] * Implement --max-cpu-usage. * Fix L2 cache size detect. * Add test for get_optimal_threads_count. --- cpu.c | 26 ++++-- cpu.h | 4 +- cpu_stub.c | 2 +- options.c | 8 +- test/CMakeLists.txt | 3 +- test/autoconf/CMakeLists.txt | 16 ++++ test/autoconf/autoconf.c | 152 +++++++++++++++++++++++++++++++++++ utils/summary.c | 4 +- 8 files changed, 201 insertions(+), 14 deletions(-) create mode 100644 test/autoconf/CMakeLists.txt create mode 100644 test/autoconf/autoconf.c diff --git a/cpu.c b/cpu.c index 655d170d1..bb8462389 100644 --- a/cpu.c +++ b/cpu.c @@ -24,11 +24,17 @@ #include #include #include -#include +#include + +#ifndef BUILD_TEST +# include +#endif #include "cpu.h" +#include "utils/applog.h" +#ifndef BUILD_TEST void cpu_init_common() { struct cpu_raw_data_t raw = { 0 }; struct cpu_id_t data = { 0 }; @@ -41,7 +47,7 @@ void cpu_init_common() { cpu_info.total_logical_cpus = data.total_logical_cpus; cpu_info.sockets = data.total_logical_cpus / data.num_logical_cpus; cpu_info.total_cores = data.num_cores * cpu_info.sockets; - cpu_info.l2_cache = data.l2_cache > 0 ? data.l2_cache * cpu_info.sockets : 0; + cpu_info.l2_cache = data.l2_cache > 0 ? data.l2_cache * cpu_info.total_cores * cpu_info.sockets : 0; cpu_info.l3_cache = data.l3_cache > 0 ? data.l3_cache * cpu_info.sockets : 0; # ifdef __x86_64__ @@ -56,21 +62,31 @@ void cpu_init_common() { cpu_info.flags |= CPU_FLAG_BMI2; } } +#endif -int get_optimal_threads_count() { +int get_optimal_threads_count(int algo, bool double_hash, int max_cpu_usage) { + if (cpu_info.total_logical_cpus == 1) { + return 1; + } + int cache = cpu_info.l3_cache ? cpu_info.l3_cache : cpu_info.l2_cache; int count = 0; + const int size = (algo ? 1024 : 2048) * (double_hash ? 2 : 1); if (cache) { - count = cache / 2048; + count = cache / size; } else { count = cpu_info.total_logical_cpus / 2; } if (count > cpu_info.total_logical_cpus) { - return cpu_info.total_logical_cpus; + count = cpu_info.total_logical_cpus; + } + + if (((float) count / cpu_info.total_logical_cpus * 100) > max_cpu_usage) { + count = ceil((float) cpu_info.total_logical_cpus * (max_cpu_usage / 100.0)); } return count < 1 ? 1 : count; diff --git a/cpu.h b/cpu.h index df0e79d91..419192bf8 100644 --- a/cpu.h +++ b/cpu.h @@ -24,6 +24,8 @@ #ifndef __CPU_H__ #define __CPU_H__ +#include + struct cpu_info { int total_cores; int total_logical_cpus; @@ -45,7 +47,7 @@ enum cpu_flags { void cpu_init(); -int get_optimal_threads_count(); +int get_optimal_threads_count(int algo, bool double_hash, int max_cpu_usage); int affine_to_cpu_mask(int id, unsigned long mask); #endif /* __CPU_H__ */ diff --git a/cpu_stub.c b/cpu_stub.c index 243316a18..83d5efc36 100644 --- a/cpu_stub.c +++ b/cpu_stub.c @@ -101,7 +101,7 @@ void cpu_init_common() { } -int get_optimal_threads_count() { +int get_optimal_threads_count(int algo, bool double_hash, int max_cpu_usage) { int count = cpu_info.total_logical_cpus / 2; return count < 1 ? 1 : count; } diff --git a/options.c b/options.c index 3f2eb1243..6163ec3bf 100644 --- a/options.c +++ b/options.c @@ -447,16 +447,16 @@ void parse_cmdline(int argc, char *argv[]) { sprintf(opt_userpass, "%s:%s", opt_user, opt_pass); } - if (!opt_n_threads) { - opt_n_threads = get_optimal_threads_count(); - } - opt_algo_variant = get_algo_variant(opt_algo, opt_algo_variant); if (!cryptonight_init(opt_algo_variant)) { applog(LOG_ERR, "Cryptonight hash self-test failed. This might be caused by bad compiler optimizations."); proper_exit(1); } + + if (!opt_n_threads) { + opt_n_threads = get_optimal_threads_count(opt_algo, opt_double_hash, opt_max_cpu_usage); + } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bfc8ea339..b30cfb0b8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,4 +5,5 @@ include(CTest) add_subdirectory(unity) add_subdirectory(cryptonight) -add_subdirectory(cryptonight_lite) \ No newline at end of file +add_subdirectory(cryptonight_lite) +add_subdirectory(autoconf) \ No newline at end of file diff --git a/test/autoconf/CMakeLists.txt b/test/autoconf/CMakeLists.txt new file mode 100644 index 000000000..b956a9dee --- /dev/null +++ b/test/autoconf/CMakeLists.txt @@ -0,0 +1,16 @@ +set(SOURCES + autoconf.c + ../../cpu.h + ../../cpu.c + ) + +add_executable(autoconf_app ${SOURCES}) +target_link_libraries(autoconf_app unity) + +include_directories(../..) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") +add_definitions(-DBUILD_TEST) + +add_test(autoconf_test autoconf_app) diff --git a/test/autoconf/autoconf.c b/test/autoconf/autoconf.c new file mode 100644 index 000000000..86ced5f0a --- /dev/null +++ b/test/autoconf/autoconf.c @@ -0,0 +1,152 @@ +#include + +#include "cpu.h" +#include "options.h" + +struct cpu_info cpu_info = { 0 }; + + +static void set_cpu_info(int total_logical_cpus, int l2_cache, int l3_cache) { + cpu_info.total_cores = total_logical_cpus; + cpu_info.total_logical_cpus = total_logical_cpus; + cpu_info.l2_cache = l2_cache; + cpu_info.l3_cache = l3_cache; +} + + +void test_autoconf_should_GetOptimalThreadsCounti7(void) { + set_cpu_info(8, 1024, 8192); // 4C/8T 8 MB (Generic i7 CPU) + + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(5, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 60)); + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 50)); + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 35)); + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 20)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 5)); +} + + +void test_autoconf_should_GetOptimalThreadsCounti5(void) { + set_cpu_info(4, 1024, 6144); // 2C/4T 6 MB (Generic i5 CPU) + + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); + + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); +} + + +void test_autoconf_should_GetOptimalThreadsCounti3(void) { + set_cpu_info(4, 512, 3072); // 2C/4T 3 MB (Generic i3 CPU) + + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); + + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); +} + + +void test_autoconf_should_GetOptimalThreadsCountR7(void) { + set_cpu_info(16, 4096, 16384); // 8C/16T 16 MB (AMD Ryzen 7) + + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); + + TEST_ASSERT_EQUAL_INT(16, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); +} + + +void test_autoconf_should_GetOptimalThreadsCountTwoE5620(void) { + set_cpu_info(16, 2048, 24576); // 8C/16T 24 MB (Two E5620) + + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); + + TEST_ASSERT_EQUAL_INT(16, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); +} + + +void test_autoconf_should_GetOptimalThreadsCountVCPU(void) { + set_cpu_info(1, 1024, 15360); // 1C/1T 15 MB (Single core Virtual CPU) + + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); + + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); +} + + +void test_autoconf_should_GetOptimalThreadsCountNoL3(void) { + set_cpu_info(8, 8192, 0); // 4C/8T (Multi core Virtual CPU without L3 cache) + + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); + + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); + + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); + TEST_ASSERT_EQUAL_INT(5, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 60)); + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 50)); + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 35)); + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 20)); + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 5)); +} + + +int main(void) +{ + UNITY_BEGIN(); + + RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti7); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti5); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti3); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountR7); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountR7); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountTwoE5620); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountVCPU); + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountNoL3); + + return UNITY_END(); +} diff --git a/utils/summary.c b/utils/summary.c index 357f5483c..e5490a912 100644 --- a/utils/summary.c +++ b/utils/summary.c @@ -54,10 +54,10 @@ static void print_cpu() { # ifndef XMRIG_NO_LIBCPUID if (opt_colors) { - applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "CPU L2/L3: %dK/%dK", cpu_info.l2_cache, cpu_info.l3_cache); + applog_notime(LOG_INFO, CL_LGR " * " CL_WHT "CPU L2/L3: %.1f MB/%.1f MB", cpu_info.l2_cache / 1024.0, cpu_info.l3_cache / 1024.0); } else { - applog_notime(LOG_INFO, " * CPU L2/L3: %dK/%dK", cpu_info.l2_cache, cpu_info.l3_cache); + applog_notime(LOG_INFO, " * CPU L2/L3: %.1f MB/%.1f MB", cpu_info.l2_cache / 1024.0, cpu_info.l3_cache / 1024.0); } # endif From d71a15e8da40524ed16f675550ee664ac6ece9f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 12 May 2017 15:04:04 +0300 Subject: [PATCH 09/17] Use --safe options to disable AES algo variations if CPU not support it. --- options.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/options.c b/options.c index 6163ec3bf..a97c71f90 100644 --- a/options.c +++ b/options.c @@ -120,15 +120,15 @@ static const char *algo_names[] = { #ifndef XMRIG_NO_AEON static int get_cryptonight_lite_variant(int variant) { - if (variant > AEON_AV0_AUTO && variant < AEON_AV_MAX) { - return variant; + if (variant <= AEON_AV0_AUTO || variant >= AEON_AV_MAX) { + return (cpu_info.flags & CPU_FLAG_AES) ? AEON_AV2_AESNI_DOUBLE : AEON_AV4_SOFT_AES_DOUBLE; } - if (cpu_info.flags & CPU_FLAG_AES) { - return AEON_AV2_AESNI_DOUBLE; + if (opt_safe && !(cpu_info.flags & CPU_FLAG_AES) && variant <= AEON_AV2_AESNI_DOUBLE) { + return variant + 2; } - return AEON_AV4_SOFT_AES_DOUBLE; + return variant; } #endif @@ -140,15 +140,15 @@ static int get_algo_variant(int algo, int variant) { } # endif - if (variant > XMR_AV0_AUTO && variant < XMR_AV_MAX) { - return variant; + if (variant <= XMR_AV0_AUTO || variant >= XMR_AV_MAX) { + return (cpu_info.flags & CPU_FLAG_AES) ? XMR_AV1_AESNI : XMR_AV3_SOFT_AES; } - if (cpu_info.flags & CPU_FLAG_AES) { - return XMR_AV1_AESNI; + if (opt_safe && !(cpu_info.flags & CPU_FLAG_AES) && variant <= XMR_AV2_AESNI_DOUBLE) { + return variant + 2; } - return XMR_AV3_SOFT_AES; + return variant; } @@ -457,6 +457,13 @@ void parse_cmdline(int argc, char *argv[]) { if (!opt_n_threads) { opt_n_threads = get_optimal_threads_count(opt_algo, opt_double_hash, opt_max_cpu_usage); } + + if (opt_safe) { + const int count = get_optimal_threads_count(opt_algo, opt_double_hash, opt_max_cpu_usage); + if (opt_n_threads > count) { + opt_n_threads = count; + } + } } From 0c2bda9aa5f0b3b1f93611e0169c5e77b7b7ca49 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 13 May 2017 19:47:12 +0300 Subject: [PATCH 10/17] Remove default url. --- options.c | 8 ++------ xmrig.c | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/options.c b/options.c index a97c71f90..5445cd1cb 100644 --- a/options.c +++ b/options.c @@ -430,12 +430,8 @@ void parse_cmdline(int argc, char *argv[]) { } if (!opt_url) { - opt_url = strdup("stratum+tcp://proxy.xmrig.com:443"); - opt_keepalive = true; - - if (!opt_backup_url) { - opt_backup_url = strdup("stratum+tcp://failover.xmrig.com:80"); - } + applog_notime(LOG_ERR, "No pool URL supplied. Exiting.\n", argv[0]); + proper_exit(1); } if (!opt_userpass) { diff --git a/xmrig.c b/xmrig.c index 6d938c77c..5aeb04c5c 100644 --- a/xmrig.c +++ b/xmrig.c @@ -446,7 +446,7 @@ static void switch_stratum() { static bool want_donate = false; if (g_want_donate && !want_donate) { - stratum_ctx->url = "stratum+tcp://donate.xmrig.com:443"; + stratum_ctx->url = opt_algo == ALGO_CRYPTONIGHT ? "stratum+tcp://donate.xmrig.com:443" : "stratum+tcp://donate.xmrig.com:3333"; applog(LOG_NOTICE, "Switching to dev pool"); want_donate = true; } From bf25b4e5d47764638b46816a9c9438b8fa47ae33 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 13 May 2017 20:26:35 +0300 Subject: [PATCH 11/17] Update README.md --- README.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ee7e096ff..8e2095c02 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,12 @@ Based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of ## Features * High performance (290+ H/s on i7 6700). * Official Windows support. -* Small Windows executable, only 430 KB without dependencies. +* Small Windows executable, only 535 KB without dependencies. * Support for backup (failover) mining server. * keepalived support. * Command line options compatible with cpuminer. +* CryptoNight-Lite support for AEON. +* Smart automatic CPU configuration. * It's open source software. ## Download @@ -51,18 +53,18 @@ xmrig.exe -o xmr-eu.dwarfpool.com:8005 -u YOUR_WALLET -p x -k --donate-level=N donate level, default 5% (5 minutes in 100 minutes) -B, --background run the miner in the background -c, --config=FILE load a JSON-format configuration file + --max-cpu-usage=N maximum cpu usage for automatic threads mode (default 75) + --safe safe adjust threads and av settings for current cpu -h, --help display this help and exit -V, --version output version information and exit ``` ## Algorithm variations -Since version 0.6.0. -* `--av=1` Default for CPUs with hardware AES. -* `--av=2` [XMR-Stak-CPU](https://github.com/fireice-uk/xmr-stak-cpu) algorithm. -* `--av=3` Same as `1` but uses BMI2 instruction [mulx](https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mulx_u64). -* `--av=4` Software AES implementation. - -For 32 bit platform only available `1` and `4`. +Since version 0.8.0. +* `--av=1` For CPUs with hardware AES. +* `--av=2` Lower power mode (double hash) of `1`. +* `--av=3` Software AES implementation. +* `--av=4` Lower power mode (double hash) of `3`. ## Build ### Ubuntu (Debian-based distros) @@ -96,13 +98,16 @@ CMake options: cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCURL_INCLUDE_DIR="c:\\curl-7.53.1\include" -DCURL_LIBRARY="c:\\curl-7.53.1\lib\.libs" ``` +### Optional features +`-DWITH_LIBCPUID=OFF` Disable libcpuid. Auto configuration of CPU after this will be very limited. +`-DWITH_AEON=OFF` Disable CryptoNight-Lite support. + ## Common Issues ### HUGE PAGES unavailable * Run XMRig as Administrator. -* Enable SeLockMemoryPrivilege. For Windows 7 pro, or Windows 8 and above see [this article](https://msdn.microsoft.com/en-gb/library/ms190730.aspx). +* Since version 0.8.0 XMRig automatically enable SeLockMemoryPrivilege for current user, but reboot or sign out still required. [Manual instruction](https://msdn.microsoft.com/en-gb/library/ms190730.aspx). ## Other information -* Now only support 64 bit operating systems (Windows/Linux). * No HTTP support, only stratum protocol support. * No TLS support. * Default donation 5% (5 minutes in 100 minutes) can be reduced to 1% via command line option `--donate-level`. From aab48fde963bf312407e2748418c5864eeb9cae6 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 13 May 2017 20:31:27 +0300 Subject: [PATCH 12/17] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e2095c02..bedfab691 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ Based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of * [Build](#build) * [Common Issues](#common-issues) * [Other information](#other-information) -* [Donations](#Donations) +* [Donations](#donations) +* [Contacts](#contacts) ## Features * High performance (290+ H/s on i7 6700). From cf8f81f5fa7f2580bb2e3f664e283e2f7e90287b Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 13 May 2017 22:32:29 +0300 Subject: [PATCH 13/17] Update CHANGELOG.md --- CHANGELOG.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 792329208..f2f5605ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,16 @@ -# v0.7.0 -- Added double hash mode, also known as lower power mode. `--av=2` and `--av=5`. -- Default threads count now depends on size of the L3 cache of CPU. +# v0.8.0 +- Added double hash mode, also known as lower power mode. `--av=2` and `--av=4`. +- Added smart automatic CPU configuration. Default threads count now depends on size of the L3 cache of CPU. +- Added CryptoNight-Lite support for AEON. +- Added `--max-cpu-usage` option for auto CPU configuration mode. +- Added `--safe` option for adjust threads and algorithm variations to current CPU. - No more manual steps to enable huge pages on Windows. XMRig will do it automatically. +- Removed BMI2 algorithm variation. +- Removed default pool URL. # v0.6.0 - Added automatic cryptonight self test. -- New software AES algorithm variation `--av=4`. Will be automatically selected if cpu not support AES-NI. +- New software AES algorithm variation. Will be automatically selected if cpu not support AES-NI. - Added 32 bit builds. - Documented [algorithm variations](https://github.com/xmrig/xmrig#algorithm-variations). From 6080f292e71b1ff55f803044a72a1bd3cd569438 Mon Sep 17 00:00:00 2001 From: xmrig Date: Sat, 13 May 2017 22:39:40 +0300 Subject: [PATCH 14/17] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f5605ff..aeadf9f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # v0.8.0 - Added double hash mode, also known as lower power mode. `--av=2` and `--av=4`. - Added smart automatic CPU configuration. Default threads count now depends on size of the L3 cache of CPU. -- Added CryptoNight-Lite support for AEON. +- Added CryptoNight-Lite support for AEON `-a cryptonight-lite`. - Added `--max-cpu-usage` option for auto CPU configuration mode. - Added `--safe` option for adjust threads and algorithm variations to current CPU. - No more manual steps to enable huge pages on Windows. XMRig will do it automatically. From ee9ba778f81af6c6927ef46b797a2667a387c719 Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 15 May 2017 18:32:27 +0300 Subject: [PATCH 15/17] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bedfab691..e20011c95 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ xmrig.exe -o xmr-eu.dwarfpool.com:8005 -u YOUR_WALLET -p x -k ### Options ``` + -a, --algo=ALGO cryptonight (default) or cryptonight-lite -o, --url=URL URL of mining server -b, --backup-url=URL URL of backup mining server -O, --userpass=U:P username:password pair for mining server From 88dd218ad8eb5e3f1913743f2a813cbf8feda77b Mon Sep 17 00:00:00 2001 From: xmrig Date: Mon, 15 May 2017 19:57:20 +0300 Subject: [PATCH 16/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e20011c95..6137945cb 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Based on cpuminer-multi with heavy optimizations/rewrites and removing a lot of * keepalived support. * Command line options compatible with cpuminer. * CryptoNight-Lite support for AEON. -* Smart automatic CPU configuration. +* Smart automatic [CPU configuration](https://github.com/xmrig/xmrig/wiki/Threads). * It's open source software. ## Download From e67a95bd8bc063026767b1f6ea396848cbbdebb6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 15 May 2017 22:06:54 +0300 Subject: [PATCH 17/17] Version increment and update help. --- options.c | 7 ++++--- version.h | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/options.c b/options.c index 5445cd1cb..b13a579c9 100644 --- a/options.c +++ b/options.c @@ -60,6 +60,7 @@ enum mining_algo opt_algo = ALGO_CRYPTONIGHT; static char const usage[] = "\ Usage: " APP_ID " [OPTIONS]\n\ Options:\n\ + -a, --algo=ALGO cryptonight (default) or cryptonight-lite\n\ -o, --url=URL URL of mining server\n\ -b, --backup-url=URL URL of backup mining server\n\ -O, --userpass=U:P username:password pair for mining server\n\ @@ -70,13 +71,13 @@ Options:\n\ -k, --keepalive send keepalived for prevent timeout (need pool support)\n\ -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ -R, --retry-pause=N time to pause between retries (default: 5)\n\ - --cpu-affinity set process affinity to cpu core(s), mask 0x3 for cores 0 and 1\n\ + --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ --no-color disable colored output\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ -B, --background run the miner in the background\n\ -c, --config=FILE load a JSON-format configuration file\n\ - --max-cpu-usage=N maximum cpu usage for automatic threads mode (default 75)\n\ - --safe safe adjust threads and av settings for current cpu\n\ + --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ + --safe safe adjust threads and av settings for current CPU\n\ -h, --help display this help and exit\n\ -V, --version output version information and exit\n\ "; diff --git a/version.h b/version.h index c55044705..301e3f453 100644 --- a/version.h +++ b/version.h @@ -27,13 +27,13 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "Monero (XMR) CPU miner" -#define APP_VERSION "0.7.0" +#define APP_VERSION "0.8.0" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" #define APP_VER_MAJOR 0 -#define APP_VER_MINOR 7 +#define APP_VER_MINOR 8 #define APP_VER_BUILD 0 #define APP_VER_REV 0