From 33944595a2049427a5f19d18e1a475485727d927 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 11 Mar 2018 21:23:14 +0700 Subject: [PATCH] Add Monero v7 support. --- CMakeLists.txt | 22 ++--- .../cryptonight_lite_av1_aesni.c | 7 +- .../cryptonight_lite_av2_aesni_double.c | 7 +- .../cryptonight_lite_av3_softaes.c | 7 +- .../cryptonight_lite_av4_softaes_double.c | 7 +- algo/cryptonight/cryptonight.c | 81 +++++++------------ algo/cryptonight/cryptonight.h | 5 +- algo/cryptonight/cryptonight_av1_aesni.c | 15 +++- .../cryptonight_av2_aesni_double.c | 20 ++++- algo/cryptonight/cryptonight_av3_softaes.c | 17 ++-- .../cryptonight_av4_softaes_double.c | 22 +++-- algo/cryptonight/cryptonight_monero.h | 51 ++++++++++++ algo/cryptonight/cryptonight_test.h | 70 ++++++++++++++++ 13 files changed, 238 insertions(+), 93 deletions(-) create mode 100644 algo/cryptonight/cryptonight_monero.h create mode 100644 algo/cryptonight/cryptonight_test.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d30acd3..9bc81bff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,20 +5,22 @@ option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) set(HEADERS - compat.h algo/cryptonight/cryptonight.h algo/cryptonight/cryptonight_aesni.h + algo/cryptonight/cryptonight_monero.h algo/cryptonight/cryptonight_softaes.h - elist.h - xmrig.h - version.h - options.h + algo/cryptonight/cryptonight_test.h + compat.h cpu.h - persistent_memory.h - stratum.h - stats.h - util.h donate.h + elist.h + options.h + persistent_memory.h + stats.h + stratum.h + util.h + version.h + xmrig.h ) set(HEADERS_CRYPTO @@ -68,7 +70,7 @@ set(SOURCES_UTILS if (WIN32) set(SOURCES_OS win/cpu_win.c win/memory_win.c win/xmrig_win.c win/app.rc compat/winansi.c) - set(EXTRA_LIBS ws2_32) + set(EXTRA_LIBS ws2_32 crypt32) add_definitions(/D_WIN32_WINNT=0x600) elseif (APPLE) set(SOURCES_OS mac/cpu_mac.c mac/memory_mac.c mac/xmrig_mac.c) diff --git a/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c b/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c index 80110fb2..fb678746 100644 --- a/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c +++ b/algo/cryptonight-lite/cryptonight_lite_av1_aesni.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -30,7 +31,7 @@ #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) +void cryptonight_lite_av1_aesni(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); diff --git a/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c b/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c index 055435c6..727e804b 100644 --- a/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c +++ b/algo/cryptonight-lite/cryptonight_lite_av2_aesni_double.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -30,7 +31,7 @@ #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) +void cryptonight_lite_av2_aesni_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); keccak((const uint8_t *) input + size, size, ctx->state1, 200); diff --git a/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c b/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c index 3dec6e33..a5a36fbb 100644 --- a/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c +++ b/algo/cryptonight-lite/cryptonight_lite_av3_softaes.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -30,7 +31,7 @@ #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) +void cryptonight_lite_av3_softaes(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); diff --git a/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c b/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c index 873b8cac..cdf8ff5d 100644 --- a/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c +++ b/algo/cryptonight-lite/cryptonight_lite_av4_softaes_double.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -30,7 +31,7 @@ #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) +void cryptonight_lite_av4_softaes_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); keccak((const uint8_t *) input + size, size, ctx->state1, 200); diff --git a/algo/cryptonight/cryptonight.c b/algo/cryptonight/cryptonight.c index 73018728..fb981df2 100644 --- a/algo/cryptonight/cryptonight.c +++ b/algo/cryptonight/cryptonight.c @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -30,56 +31,28 @@ # include "xmrig.h" #endif -#include "crypto/c_groestl.h" #include "crypto/c_blake256.h" +#include "crypto/c_groestl.h" #include "crypto/c_jh.h" #include "crypto/c_skein.h" #include "cryptonight.h" +#include "cryptonight_test.h" #include "options.h" -const static char test_input[152] = { - 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, - 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 -}; - - -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, - 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00 -}; - - -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_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t version); +void cryptonight_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t version); +void cryptonight_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t version); +void cryptonight_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t version); #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); +void cryptonight_lite_av1_aesni(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t); +void cryptonight_lite_av2_aesni_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t); +void cryptonight_lite_av3_softaes(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t); +void cryptonight_lite_av4_softaes_double(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t); #endif -void (*cryptonight_hash_ctx)(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx) = NULL; +void (*cryptonight_hash_ctx)(const void* input, size_t size, void* output, struct cryptonight_ctx* ctx, uint8_t version) = NULL; static bool self_test() { @@ -92,18 +65,24 @@ static bool self_test() { struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 2, 16); - cryptonight_hash_ctx(test_input, 76, output, ctx); + cryptonight_hash_ctx(test_input, 76, output, ctx, 0); + +# ifndef XMRIG_NO_AEON + bool rc = memcmp(output, opt_algo == ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (opt_double_hash ? 64 : 32)) == 0; +# else + bool rc = memcmp(output, test_output0, opt_double_hash ? 64 : 32)) == 0; +# endif + + if (rc && opt_algo == ALGO_CRYPTONIGHT) { + cryptonight_hash_ctx(test_input, 76, output, ctx, 7); + + rc = memcmp(output, test_output2, (opt_double_hash ? 64 : 32)) == 0; + } _mm_free(ctx->memory); _mm_free(ctx); -# 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; + return rc; } @@ -200,7 +179,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict blob, si uint32_t *nonceptr = (uint32_t*) (((char*) blob) + 39); do { - cryptonight_hash_ctx(blob, blob_size, hash, ctx); + cryptonight_hash_ctx(blob, blob_size, hash, ctx, ((uint8_t*) blob)[0]); (*hashes_done)++; if (unlikely(hash[7] < target)) { @@ -220,7 +199,7 @@ int scanhash_cryptonight_double(int thr_id, uint32_t *hash, uint8_t *restrict bl uint32_t *nonceptr1 = (uint32_t*) (((char*) blob) + 39 + blob_size); do { - cryptonight_hash_ctx(blob, blob_size, hash, ctx); + cryptonight_hash_ctx(blob, blob_size, hash, ctx, ((uint8_t*) blob)[0]); (*hashes_done) += 2; if (unlikely(hash[7] < target)) { diff --git a/algo/cryptonight/cryptonight.h b/algo/cryptonight/cryptonight.h index 0b017073..f8002afe 100644 --- a/algo/cryptonight/cryptonight.h +++ b/algo/cryptonight/cryptonight.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/algo/cryptonight/cryptonight_av1_aesni.c b/algo/cryptonight/cryptonight_av1_aesni.c index 3f30544e..b2c45c70 100644 --- a/algo/cryptonight/cryptonight_av1_aesni.c +++ b/algo/cryptonight/cryptonight_av1_aesni.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -25,15 +26,18 @@ #include #include +#include "crypto/c_keccak.h" #include "cryptonight.h" #include "cryptonight_aesni.h" -#include "crypto/c_keccak.h" +#include "cryptonight_monero.h" -void cryptonight_av1_aesni(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +void cryptonight_av1_aesni(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -51,6 +55,7 @@ void cryptonight_av1_aesni(const void *restrict input, size_t size, void *restri cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); _mm_store_si128((__m128i *) &l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & 0x1FFFF0]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -62,8 +67,10 @@ void cryptonight_av1_aesni(const void *restrict input, size_t size, void *restri al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & 0x1FFFF0])[0] = al0; ((uint64_t*)&l0[idx0 & 0x1FFFF0])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; diff --git a/algo/cryptonight/cryptonight_av2_aesni_double.c b/algo/cryptonight/cryptonight_av2_aesni_double.c index 779b9bc3..345207f7 100644 --- a/algo/cryptonight/cryptonight_av2_aesni_double.c +++ b/algo/cryptonight/cryptonight_av2_aesni_double.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -25,16 +26,20 @@ #include #include +#include "crypto/c_keccak.h" #include "cryptonight.h" #include "cryptonight_aesni.h" -#include "crypto/c_keccak.h" +#include "cryptonight_monero.h" -void cryptonight_av2_aesni_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx) +void cryptonight_av2_aesni_double(const void *restrict input, size_t size, void *restrict output, struct cryptonight_ctx *restrict ctx, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); keccak((const uint8_t *) input + size, size, ctx->state1, 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEMORY; uint64_t* h0 = (uint64_t*) ctx->state0; @@ -64,6 +69,9 @@ void cryptonight_av2_aesni_double(const void *restrict input, size_t size, void _mm_store_si128((__m128i *) &l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & 0x1FFFF0], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & 0x1FFFF0]); + VARIANT1_1(&l1[idx1 & 0x1FFFF0]); + idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -78,8 +86,10 @@ void cryptonight_av2_aesni_double(const void *restrict input, size_t size, void al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & 0x1FFFF0])[0] = al0; ((uint64_t*) &l0[idx0 & 0x1FFFF0])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -92,8 +102,10 @@ void cryptonight_av2_aesni_double(const void *restrict input, size_t size, void al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & 0x1FFFF0])[0] = al1; ((uint64_t*) &l1[idx1 & 0x1FFFF0])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; diff --git a/algo/cryptonight/cryptonight_av3_softaes.c b/algo/cryptonight/cryptonight_av3_softaes.c index 22be894d..1d9f654a 100644 --- a/algo/cryptonight/cryptonight_av3_softaes.c +++ b/algo/cryptonight/cryptonight_av3_softaes.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -25,15 +26,18 @@ #include #include -#include "cryptonight.h" -#include "cryptonight_softaes.h" #include "crypto/c_keccak.h" +#include "cryptonight.h" +#include "cryptonight_monero.h" +#include "cryptonight_softaes.h" -void cryptonight_av3_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, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -51,6 +55,7 @@ void cryptonight_av3_softaes(const void *restrict input, size_t size, void *rest cx = soft_aesenc(cx, _mm_set_epi64x(ah0, al0)); _mm_store_si128((__m128i *)&l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & 0x1FFFF0]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -62,8 +67,10 @@ void cryptonight_av3_softaes(const void *restrict input, size_t size, void *rest al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & 0x1FFFF0])[0] = al0; ((uint64_t*)&l0[idx0 & 0x1FFFF0])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; diff --git a/algo/cryptonight/cryptonight_av4_softaes_double.c b/algo/cryptonight/cryptonight_av4_softaes_double.c index afd4bebe..4085429d 100644 --- a/algo/cryptonight/cryptonight_av4_softaes_double.c +++ b/algo/cryptonight/cryptonight_av4_softaes_double.c @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017 fireice-uk - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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 @@ -25,16 +26,20 @@ #include #include -#include "cryptonight.h" -#include "cryptonight_softaes.h" #include "crypto/c_keccak.h" +#include "cryptonight.h" +#include "cryptonight_monero.h" +#include "cryptonight_softaes.h" -void cryptonight_av4_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, uint8_t version) { keccak((const uint8_t *) input, size, ctx->state0, 200); keccak((const uint8_t *) input + size, size, ctx->state1, 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEMORY; uint64_t* h0 = (uint64_t*) ctx->state0; @@ -64,6 +69,9 @@ void cryptonight_av4_softaes_double(const void *restrict input, size_t size, voi _mm_store_si128((__m128i *) &l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & 0x1FFFF0], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & 0x1FFFF0]); + VARIANT1_1(&l1[idx1 & 0x1FFFF0]); + idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -78,8 +86,10 @@ void cryptonight_av4_softaes_double(const void *restrict input, size_t size, voi al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & 0x1FFFF0])[0] = al0; ((uint64_t*) &l0[idx0 & 0x1FFFF0])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -92,8 +102,10 @@ void cryptonight_av4_softaes_double(const void *restrict input, size_t size, voi al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & 0x1FFFF0])[0] = al1; ((uint64_t*) &l1[idx1 & 0x1FFFF0])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; diff --git a/algo/cryptonight/cryptonight_monero.h b/algo/cryptonight/cryptonight_monero.h new file mode 100644 index 00000000..2a4e7ee1 --- /dev/null +++ b/algo/cryptonight/cryptonight_monero.h @@ -0,0 +1,51 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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_MONERO_H__ +#define __CRYPTONIGHT_MONERO_H__ + + +// VARIANT ALTERATIONS +#define VARIANT1_INIT(part) \ + uint64_t tweak1_2_##part = 0; \ + if (version > 6) { \ + tweak1_2_##part = (*(const uint64_t*)(((const uint8_t*) input) + 35 + part * size) ^ \ + *((const uint64_t*)(ctx->state##part) + 24)); \ + } + +#define VARIANT1_1(p) \ + if (version > 6) { \ + const uint8_t tmp = ((const uint8_t*)(p))[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ + } + +#define VARIANT1_2(p, part) \ + if (version > 6) { \ + (p) ^= tweak1_2_##part; \ + } + + +#endif /* __CRYPTONIGHT_MONERO_H__ */ diff --git a/algo/cryptonight/cryptonight_test.h b/algo/cryptonight/cryptonight_test.h new file mode 100644 index 00000000..65200f75 --- /dev/null +++ b/algo/cryptonight/cryptonight_test.h @@ -0,0 +1,70 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 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_TEST_H__ +#define __CRYPTONIGHT_TEST_H__ + + +const static uint8_t test_input[152] = { + 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, + 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 +}; + + +const static uint8_t 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, + 0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00 +}; + + +#ifndef XMRIG_NO_AEON +const static uint8_t 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, +}; +#endif + + +// Monero v7 +const static uint8_t test_output2[64] = { + 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, + 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, + 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, + 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9 +}; + + +#endif /* __CRYPTONIGHT_TEST_H__ */