From df6ca1319cbf91796edcc9fcdb3cd1c1b5f47e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ben=20Gr=C3=A4f?= Date: Wed, 21 Mar 2018 09:24:37 +0100 Subject: [PATCH] Fixed cryptonight tests when force PoW option is used --- src/Options.cpp | 1 - src/crypto/CryptoNight.cpp | 70 +++++++++++++++++++---------------- src/crypto/CryptoNight_test.h | 8 ++-- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 58beed44..2ef55f7a 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -306,7 +306,6 @@ Options::Options(int argc, char **argv) : m_ccKeyFile(nullptr), m_ccCertFile(nullptr), m_algo(ALGO_CRYPTONIGHT), - m_forcePowVersion(POW_AUTODETECT), m_algoVariant(AV0_AUTO), m_aesni(AESNI_AUTO), m_forcePowVersion(POW_AUTODETECT), diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 0a3f194a..b352b115 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -36,7 +36,8 @@ template static void cryptonight_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { # if !defined(XMRIG_ARMv7) - if (reinterpret_cast(input)[0] > 6 || Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { + if ((reinterpret_cast(input)[0] > 6 && Options::i()->forcePowVersion() == Options::PowVersion::POW_AUTODETECT) || + Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { CryptoNightMultiHash<0x80000, MEMORY, 0x1FFFF0, false, NUM_HASH_BLOCKS>::hashPowV2(input, size, output, ctx); } else { CryptoNightMultiHash<0x80000, MEMORY, 0x1FFFF0, false, NUM_HASH_BLOCKS>::hash(input, size, output, ctx); @@ -46,7 +47,8 @@ static void cryptonight_aesni(const void *input, size_t size, void *output, cryp template static void cryptonight_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - if (reinterpret_cast(input)[0] > 6 || Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { + if ((reinterpret_cast(input)[0] > 6 && Options::i()->forcePowVersion() == Options::PowVersion::POW_AUTODETECT) || + Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { CryptoNightMultiHash<0x80000, MEMORY, 0x1FFFF0, true, NUM_HASH_BLOCKS>::hashPowV2(input, size, output, ctx); } else { CryptoNightMultiHash<0x80000, MEMORY, 0x1FFFF0, true, NUM_HASH_BLOCKS>::hash(input, size, output, ctx); @@ -56,7 +58,8 @@ static void cryptonight_softaes(const void *input, size_t size, void *output, cr template static void cryptonight_lite_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { # if !defined(XMRIG_ARMv7) - if (reinterpret_cast(input)[0] > 1 || Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { + if ((reinterpret_cast(input)[0] > 1 && Options::i()->forcePowVersion() == Options::PowVersion::POW_AUTODETECT) || + Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { CryptoNightMultiHash<0x40000, MEMORY_LITE, 0xFFFF0, false, NUM_HASH_BLOCKS>::hashPowV2(input, size, output, ctx); } else { CryptoNightMultiHash<0x40000, MEMORY_LITE, 0xFFFF0, false, NUM_HASH_BLOCKS>::hash(input, size, output, ctx); @@ -66,7 +69,8 @@ static void cryptonight_lite_aesni(const void *input, size_t size, void *output, template static void cryptonight_lite_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - if (reinterpret_cast(input)[0] > 1 || Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { + if ((reinterpret_cast(input)[0] > 1 && Options::i()->forcePowVersion() == Options::PowVersion::POW_AUTODETECT) || + Options::i()->forcePowVersion() == Options::PowVersion::POW_V2) { CryptoNightMultiHash<0x40000, MEMORY_LITE, 0xFFFF0, true, NUM_HASH_BLOCKS>::hashPowV2(input, size, output, ctx); } else { CryptoNightMultiHash<0x40000, MEMORY_LITE, 0xFFFF0, true, NUM_HASH_BLOCKS>::hash(input, size, output, ctx); @@ -119,8 +123,8 @@ void CryptoNight::hash(size_t factor, const uint8_t* input, size_t size, uint8_t bool CryptoNight::selfTest(int algo) { if (cryptonight_hash_ctx[0] == nullptr || cryptonight_hash_ctx[2] == nullptr || - cryptonight_hash_ctx[2] == nullptr || cryptonight_hash_ctx[3] == nullptr || - cryptonight_hash_ctx[4] == nullptr) { + cryptonight_hash_ctx[2] == nullptr || cryptonight_hash_ctx[3] == nullptr || + cryptonight_hash_ctx[4] == nullptr) { return false; } @@ -129,36 +133,40 @@ bool CryptoNight::selfTest(int algo) auto ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 6, 16); - cryptonight_hash_ctx[0](test_input, 76, output, ctx); - bool resultSingle = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 32) == 0; - - cryptonight_hash_ctx[1](test_input, 76, output, ctx); - bool resultDouble = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 64) == 0; - - cryptonight_hash_ctx[2](test_input, 76, output, ctx); - bool resultTriple = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 96) == 0; - - cryptonight_hash_ctx[3](test_input, 76, output, ctx); - bool resultQuadruple = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 128) == 0; - - cryptonight_hash_ctx[4](test_input, 76, output, ctx); - bool resultQuintuple = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 160) == 0; - - // monero v1 pow bool resultV1Pow = true; - if (algo == Options::ALGO_CRYPTONIGHT) - { - cryptonight_hash_ctx[0](test_input_monero_v1_pow_0, sizeof(test_input_monero_v1_pow_0), output, ctx); - resultV1Pow = resultV1Pow &&memcmp(output, test_output_monero_v1_pow[0], 32) == 0; - cryptonight_hash_ctx[1](test_input_monero_v1_pow_1, sizeof(test_input_monero_v1_pow_1), output, ctx); - resultV1Pow = resultV1Pow &&memcmp(output, test_output_monero_v1_pow[1], 32) == 0; - cryptonight_hash_ctx[2](test_input_monero_v1_pow_2, sizeof(test_input_monero_v1_pow_2), output, ctx); - resultV1Pow = resultV1Pow &&memcmp(output, test_output_monero_v1_pow[2], 32) == 0; + if (Options::i()->forcePowVersion() == Options::PowVersion::POW_AUTODETECT || Options::i()->forcePowVersion() == Options::PowVersion::POW_V1) { + cryptonight_hash_ctx[0](test_input, 76, output, ctx); + resultV1Pow = resultV1Pow && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 32) == 0; + + cryptonight_hash_ctx[1](test_input, 76, output, ctx); + resultV1Pow = resultV1Pow && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 64) == 0; + + cryptonight_hash_ctx[2](test_input, 76, output, ctx); + resultV1Pow = resultV1Pow && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 96) == 0; + + cryptonight_hash_ctx[3](test_input, 76, output, ctx); + resultV1Pow = resultV1Pow && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 128) == 0; + + cryptonight_hash_ctx[4](test_input, 76, output, ctx); + resultV1Pow = resultV1Pow && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output_light : test_output, 160) == 0; + } + + // monero v2 pow + bool resultV2Pow = true; + if (algo == Options::ALGO_CRYPTONIGHT && (Options::i()->forcePowVersion() == Options::PowVersion::POW_AUTODETECT || Options::i()->forcePowVersion() == Options::PowVersion::POW_V2)) { + cryptonight_hash_ctx[0](test_input_monero_v2_pow_0, sizeof(test_input_monero_v2_pow_0), output, ctx); + resultV2Pow = resultV2Pow && memcmp(output, test_output_monero_v2_pow[0], 32) == 0; + + cryptonight_hash_ctx[1](test_input_monero_v2_pow_1, sizeof(test_input_monero_v2_pow_1), output, ctx); + resultV2Pow = resultV2Pow && memcmp(output, test_output_monero_v2_pow[1], 32) == 0; + + cryptonight_hash_ctx[2](test_input_monero_v2_pow_2, sizeof(test_input_monero_v2_pow_2), output, ctx); + resultV2Pow = resultV2Pow && memcmp(output, test_output_monero_v2_pow[2], 32) == 0; //TODO test multihashs } _mm_free(ctx->memory); _mm_free(ctx); - return resultSingle && resultDouble && resultTriple && resultQuadruple && resultQuintuple && resultV1Pow; + return resultV1Pow && resultV2Pow; } \ No newline at end of file diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index a3d4f110..1f7f1b44 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -91,24 +91,24 @@ const static uint8_t test_output_light[] = { }; -const static uint8_t test_input_monero_v1_pow_0[] = +const static uint8_t test_input_monero_v2_pow_0[] = {0x85, 0x19, 0xe0, 0x39, 0x17, 0x2b, 0x0d, 0x70, 0xe5, 0xca, 0x7b, 0x33, 0x83, 0xd6, 0xb3, 0x16, 0x73, 0x15, 0xa4, 0x22, 0x74, 0x7b, 0x73, 0xf0, 0x19, 0xcf, 0x95, 0x28, 0xf0, 0xfd, 0xe3, 0x41, 0xfd, 0x0f, 0x2a, 0x63, 0x03, 0x0b, 0xa6, 0x45, 0x05, 0x25, 0xcf, 0x6d, 0xe3, 0x18, 0x37, 0x66, 0x9a, 0xf6, 0xf1, 0xdf, 0x81, 0x31, 0xfa, 0xf5, 0x0a, 0xaa, 0xb8, 0xd3, 0xa7, 0x40, 0x55, 0x89}; -const static uint8_t test_input_monero_v1_pow_1[] = +const static uint8_t test_input_monero_v2_pow_1[] = {0x37, 0xa6, 0x36, 0xd7, 0xda, 0xfd, 0xf2, 0x59, 0xb7, 0x28, 0x7e, 0xdd, 0xca, 0x2f, 0x58, 0x09, 0x9e, 0x98, 0x61, 0x9d, 0x2f, 0x99, 0xbd, 0xb8, 0x96, 0x9d, 0x7b, 0x14, 0x49, 0x81, 0x02, 0xcc, 0x06, 0x52, 0x01, 0xc8, 0xbe, 0x90, 0xbd, 0x77, 0x73, 0x23, 0xf4, 0x49, 0x84, 0x8b, 0x21, 0x5d, 0x29, 0x77, 0xc9, 0x2c, 0x4c, 0x1c, 0x2d, 0xa3, 0x6a, 0xb4, 0x6b, 0x2e, 0x38, 0x96, 0x89, 0xed, 0x97, 0xc1, 0x8f, 0xec, 0x08, 0xcd, 0x3b, 0x03, 0x23, 0x5c, 0x5e, 0x4c, 0x62, 0xa3, 0x7a, 0xd8, 0x8c, 0x7b, 0x67, 0x93, 0x24, 0x95, 0xa7, 0x10, 0x90, 0xe8, 0x5d, 0xd4, 0x02, 0x0a, 0x93, 0x00}; -const static uint8_t test_input_monero_v1_pow_2[] = +const static uint8_t test_input_monero_v2_pow_2[] = {0x38, 0x27, 0x4c, 0x97, 0xc4, 0x5a, 0x17, 0x2c, 0xfc, 0x97, 0x67, 0x98, 0x70, 0x42, 0x2e, 0x3a, 0x1a, 0xb0, 0x78, 0x49, 0x60, 0xc6, 0x05, 0x14, 0xd8, 0x16, 0x27, 0x14, 0x15, 0xc3, 0x06, 0xee, 0x3a, 0x3e, 0xd1, 0xa7, 0x7e, 0x31, 0xf6, 0xa8, 0x85, 0xc3, 0xcb}; -const static uint8_t test_output_monero_v1_pow[3][32] = { +const static uint8_t test_output_monero_v2_pow[3][32] = { {0x5b, 0xb4, 0x0c, 0x58, 0x80, 0xce, 0xf2, 0xf7, 0x39, 0xbd, 0xb6, 0xaa, 0xaf, 0x16, 0x16, 0x1e, 0xaa, 0xe5, 0x55, 0x30, 0xe7, 0xb1, 0x0d, 0x7e, 0xa9, 0x96, 0xb7, 0x51, 0xa2, 0x99, 0xe9, 0x49}, {0x61, 0x3e, 0x63, 0x85, 0x05, 0xba, 0x1f, 0xd0, 0x5f, 0x42, 0x8d, 0x5c, 0x9f, 0x8e, 0x08, 0xf8,