From d3b0038bda4a54d1af2ddf4f14b3a33da84b84e7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 10 May 2017 12:58:52 +0300 Subject: [PATCH] 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 02f82f90..0425d8e9 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 00000000..fe062dac --- /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 00000000..80110fb2 --- /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 00000000..055435c6 --- /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 00000000..3dec6e33 --- /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 00000000..873b8cac --- /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 00000000..51b70bc0 --- /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 4b598033..73018728 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 1770ac82..0b017073 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 49435699..2032a254 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 9be20a39..3f2eb124 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 eb466fba..2cc41915 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 bcfbe607..fad7f730 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 3211bc09..357f5483 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 4632f5a1..9ff63dad 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()) {