From 7feaf9e52e6d5e6287dd8bafdbd444007b827e4f Mon Sep 17 00:00:00 2001 From: Farhan Kathawala Date: Tue, 4 Jul 2017 16:16:06 -0500 Subject: [PATCH 1/7] Slight optimization of keccak --- crypto/c_keccak.c | 65 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/crypto/c_keccak.c b/crypto/c_keccak.c index e11ca47..db82810 100644 --- a/crypto/c_keccak.c +++ b/crypto/c_keccak.c @@ -70,19 +70,60 @@ void keccakf(uint64_t st[25], int rounds) } // Chi - for (j = 0; j < 25; j += 5) { - bc[0] = st[j ]; - bc[1] = st[j + 1]; - bc[2] = st[j + 2]; - bc[3] = st[j + 3]; - bc[4] = st[j + 4]; - st[j ] ^= (~bc[1]) & bc[2]; - st[j + 1] ^= (~bc[2]) & bc[3]; - st[j + 2] ^= (~bc[3]) & bc[4]; - st[j + 3] ^= (~bc[4]) & bc[0]; - st[j + 4] ^= (~bc[0]) & bc[1]; - } + // unrolled loop, where only last iteration is different + j = 0; + bc[0] = st[j ]; + bc[1] = st[j + 1]; + st[j ] ^= (~st[j + 1]) & st[j + 2]; + st[j + 1] ^= (~st[j + 2]) & st[j + 3]; + st[j + 2] ^= (~st[j + 3]) & st[j + 4]; + st[j + 3] ^= (~st[j + 4]) & bc[0]; + st[j + 4] ^= (~bc[0]) & bc[1]; + + j = 5; + bc[0] = st[j ]; + bc[1] = st[j + 1]; + + st[j ] ^= (~st[j + 1]) & st[j + 2]; + st[j + 1] ^= (~st[j + 2]) & st[j + 3]; + st[j + 2] ^= (~st[j + 3]) & st[j + 4]; + st[j + 3] ^= (~st[j + 4]) & bc[0]; + st[j + 4] ^= (~bc[0]) & bc[1]; + + j = 10; + bc[0] = st[j ]; + bc[1] = st[j + 1]; + + st[j ] ^= (~st[j + 1]) & st[j + 2]; + st[j + 1] ^= (~st[j + 2]) & st[j + 3]; + st[j + 2] ^= (~st[j + 3]) & st[j + 4]; + st[j + 3] ^= (~st[j + 4]) & bc[0]; + st[j + 4] ^= (~bc[0]) & bc[1]; + + j = 15; + bc[0] = st[j ]; + bc[1] = st[j + 1]; + + st[j ] ^= (~st[j + 1]) & st[j + 2]; + st[j + 1] ^= (~st[j + 2]) & st[j + 3]; + st[j + 2] ^= (~st[j + 3]) & st[j + 4]; + st[j + 3] ^= (~st[j + 4]) & bc[0]; + st[j + 4] ^= (~bc[0]) & bc[1]; + + j = 20; + bc[0] = st[j ]; + bc[1] = st[j + 1]; + bc[2] = st[j + 2]; + bc[3] = st[j + 3]; + bc[4] = st[j + 4]; + + st[j ] ^= (~bc[1]) & bc[2]; + st[j + 1] ^= (~bc[2]) & bc[3]; + st[j + 2] ^= (~bc[3]) & bc[4]; + st[j + 3] ^= (~bc[4]) & bc[0]; + st[j + 4] ^= (~bc[0]) & bc[1]; + // Iota st[0] ^= keccakf_rndc[round]; } From d9646022a40537545386e0e6f1e194c24a10bff9 Mon Sep 17 00:00:00 2001 From: Farhan Kathawala Date: Wed, 5 Jul 2017 20:01:05 -0500 Subject: [PATCH 2/7] optimization to Rho Pi loop in keccak --- crypto/c_keccak.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/crypto/c_keccak.c b/crypto/c_keccak.c index db82810..eadb85b 100644 --- a/crypto/c_keccak.c +++ b/crypto/c_keccak.c @@ -24,18 +24,6 @@ const uint64_t keccakf_rndc[24] = 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 }; -const int keccakf_rotc[24] = -{ - 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, - 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 -}; - -const int keccakf_piln[24] = -{ - 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, - 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 -}; - // update the state with given number of rounds void keccakf(uint64_t st[25], int rounds) @@ -63,11 +51,30 @@ void keccakf(uint64_t st[25], int rounds) // Rho Pi t = st[1]; - for (i = 0; i < 24; ++i) { - bc[0] = st[keccakf_piln[i]]; - st[keccakf_piln[i]] = ROTL64(t, keccakf_rotc[i]); - t = bc[0]; - } + st[ 1] = ROTL64(st[ 6], 44); + st[ 6] = ROTL64(st[ 9], 20); + st[ 9] = ROTL64(st[22], 61); + st[22] = ROTL64(st[14], 39); + st[14] = ROTL64(st[20], 18); + st[20] = ROTL64(st[ 2], 62); + st[ 2] = ROTL64(st[12], 43); + st[12] = ROTL64(st[13], 25); + st[13] = ROTL64(st[19], 8); + st[19] = ROTL64(st[23], 56); + st[23] = ROTL64(st[15], 41); + st[15] = ROTL64(st[ 4], 27); + st[ 4] = ROTL64(st[24], 14); + st[24] = ROTL64(st[21], 2); + st[21] = ROTL64(st[ 8], 55); + st[ 8] = ROTL64(st[16], 45); + st[16] = ROTL64(st[ 5], 36); + st[ 5] = ROTL64(st[ 3], 28); + st[ 3] = ROTL64(st[18], 21); + st[18] = ROTL64(st[17], 15); + st[17] = ROTL64(st[11], 10); + st[11] = ROTL64(st[ 7], 6); + st[ 7] = ROTL64(st[10], 3); + st[10] = ROTL64(t, 1); // Chi // unrolled loop, where only last iteration is different From 2eeeac6b46e002ed62044c37ef7788354c3002c3 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Wed, 12 Jul 2017 19:52:32 +0100 Subject: [PATCH 3/7] AES override feature --- config.txt | 10 ++++++++++ jconf.cpp | 16 ++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config.txt b/config.txt index 98675a2..cb15fb9 100644 --- a/config.txt +++ b/config.txt @@ -76,6 +76,16 @@ null, */ "nicehash_nonce" : false, +/* + * Manual hardware AES override + * + * Some VMs don't report AES capability correctly. You can set this value to true to make the miner use hardware + * AES or to false to make it use software AES. + * + * WARNING: setting this to true on a CPU that doesn't support hardware AES will crash the miner. + */ +"aes_override" : null, + /* * TLS Settings * If you need real security, make sure tls_secure_algo is enabled (otherwise MITM attack can downgrade encryption diff --git a/jconf.cpp b/jconf.cpp index 3457ed9..4ac4c13 100644 --- a/jconf.cpp +++ b/jconf.cpp @@ -45,7 +45,7 @@ using namespace rapidjson; /* * This enum needs to match index in oConfigValues, otherwise we will get a runtime error */ -enum configEnum { aCpuThreadsConf, sUseSlowMem, bNiceHashMode, +enum configEnum { aCpuThreadsConf, sUseSlowMem, bNiceHashMode, bAesOverride, bTlsMode, bTlsSecureAlgo, sTlsFingerprint, sPoolAddr, sWalletAddr, sPoolPwd, iCallTimeout, iNetRetry, iGiveUpLimit, iVerboseLevel, iAutohashTime, bDaemonMode, sOutputFile, iHttpdPort, bPreferIpv4 }; @@ -62,6 +62,7 @@ configVal oConfigValues[] = { { aCpuThreadsConf, "cpu_threads_conf", kNullType }, { sUseSlowMem, "use_slow_memory", kStringType }, { bNiceHashMode, "nicehash_nonce", kTrueType }, + { bAesOverride, "aes_override", kNullType }, { bTlsMode, "use_tls", kTrueType }, { bTlsSecureAlgo, "tls_secure_algo", kTrueType }, { sTlsFingerprint, "tls_fingerprint", kStringType }, @@ -453,11 +454,14 @@ bool jconf::parse_config(const char* sFilename) printer::inst()->set_verbose_level(prv->configValues[iVerboseLevel]->GetUint64()); - if(!NeedsAutoconf()) - { - if(!bHaveAes) - printer::inst()->print_msg(L0, "Your CPU doesn't support hardware AES. Don't expect high hashrates."); - } + if(NeedsAutoconf()) + return true; + + if(prv->configValues[bAesOverride]->IsBool()) + bHaveAes = prv->configValues[bAesOverride]->GetBool(); + + if(!bHaveAes) + printer::inst()->print_msg(L0, "Your CPU doesn't support hardware AES. Don't expect high hashrates."); return true; } From d04f9d3803b1d37b93505aad406bedfcd39be6ba Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Wed, 12 Jul 2017 23:17:57 +0100 Subject: [PATCH 4/7] Make the config comments clearer --- config.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.txt b/config.txt index cb15fb9..78df0c0 100644 --- a/config.txt +++ b/config.txt @@ -79,8 +79,8 @@ null, /* * Manual hardware AES override * - * Some VMs don't report AES capability correctly. You can set this value to true to make the miner use hardware - * AES or to false to make it use software AES. + * Some VMs don't report AES capability correctly. You can set this value to true to enforce hardware AES or + * to false to force disable AES or null to let the miner decide if AES is used. * * WARNING: setting this to true on a CPU that doesn't support hardware AES will crash the miner. */ From 7ea46b04d811c8ca3fc7090f38357fc51b1d3b1b Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 17 Jul 2017 12:19:54 +0200 Subject: [PATCH 5/7] Randomly choose from all available IP addresses This is to avoid getting stuck on the same IP which doesn't work for whatever reason. pool.supportxmr.com uses DNS for load balancing, and it had issues with xmr-stak miners when one of the servers went down and all xmr-stak miners that were on that server didn't switch to the other working server. This change fixes the bug. --- socket.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/socket.cpp b/socket.cpp index 257da22..52f46b5 100644 --- a/socket.cpp +++ b/socket.cpp @@ -78,33 +78,34 @@ bool plain_socket::set_hostname(const char* sAddr) return pCallback->set_socket_error_strerr("CONNECT error: GetAddrInfo: ", err); addrinfo *ptr = pAddrRoot; - addrinfo *ipv4 = nullptr, *ipv6 = nullptr; + std::vector ipv4; + std::vector ipv6; while (ptr != nullptr) { if (ptr->ai_family == AF_INET) - ipv4 = ptr; + ipv4.push_back(ptr); if (ptr->ai_family == AF_INET6) - ipv6 = ptr; + ipv6.push_back(ptr); ptr = ptr->ai_next; } - if (ipv4 == nullptr && ipv6 == nullptr) + if (ipv4.empty() && ipv6.empty()) { freeaddrinfo(pAddrRoot); pAddrRoot = nullptr; return pCallback->set_socket_error("CONNECT error: I found some DNS records but no IPv4 or IPv6 addresses."); } - else if (ipv4 != nullptr && ipv6 == nullptr) - pSockAddr = ipv4; - else if (ipv4 == nullptr && ipv6 != nullptr) - pSockAddr = ipv6; - else if (ipv4 != nullptr && ipv6 != nullptr) + else if (!ipv4.empty() && ipv6.empty()) + pSockAddr = ipv4[rand() % ipv4.size()]; + else if (ipv4.empty() && !ipv6.empty()) + pSockAddr = ipv6[rand() % ipv6.size()]; + else if (!ipv4.empty() && !ipv6.empty()) { if(jconf::inst()->PreferIpv4()) - pSockAddr = ipv4; + pSockAddr = ipv4[rand() % ipv4.size()]; else - pSockAddr = ipv6; + pSockAddr = ipv6[rand() % ipv6.size()]; } hSocket = socket(pSockAddr->ai_family, pSockAddr->ai_socktype, pSockAddr->ai_protocol); From f5c2757b04c54636e442b62d9fd8b02999da03a8 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 17 Jul 2017 21:10:37 +0200 Subject: [PATCH 6/7] Initialize random number generator --- cli-miner.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli-miner.cpp b/cli-miner.cpp index 43329e2..2dc5173 100644 --- a/cli-miner.cpp +++ b/cli-miner.cpp @@ -41,6 +41,8 @@ #include #include +#include + #ifndef CONF_NO_TLS #include #include @@ -74,6 +76,8 @@ int main(int argc, char *argv[]) OpenSSL_add_all_digests(); #endif + std::srand(std::time(0)); + const char* sFilename = "config.txt"; bool benchmark_mode = false; From 752d1b8a1cf595d6e00c12b421ad8d6fd5d18fbe Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 18 Jul 2017 08:19:31 +0200 Subject: [PATCH 7/7] std::srand -> srand --- cli-miner.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli-miner.cpp b/cli-miner.cpp index 2dc5173..45d2c16 100644 --- a/cli-miner.cpp +++ b/cli-miner.cpp @@ -41,7 +41,7 @@ #include #include -#include +#include #ifndef CONF_NO_TLS #include @@ -76,7 +76,7 @@ int main(int argc, char *argv[]) OpenSSL_add_all_digests(); #endif - std::srand(std::time(0)); + srand(time(0)); const char* sFilename = "config.txt"; bool benchmark_mode = false;