Skip to content

Commit 903b243

Browse files
committed
New style function selector.
1 parent 72cd6d1 commit 903b243

12 files changed

+287
-31
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(xmrig)
33

44
option(WITH_LIBCPUID "Use Libcpuid" ON)
55
option(WITH_AEON "CryptoNight-Lite support" ON)
6+
option(WITH_SUMO "CryptoNight-Heavy support" ON)
67
option(WITH_HTTPD "HTTP REST API" ON)
78
option(BUILD_STATIC "Build static binary" OFF)
89

@@ -70,6 +71,7 @@ set(HEADERS_CRYPTO
7071
src/crypto/c_keccak.h
7172
src/crypto/c_skein.h
7273
src/crypto/CryptoNight.h
74+
src/crypto/CryptoNight_constants.h
7375
src/crypto/CryptoNight_monero.h
7476
src/crypto/CryptoNight_test.h
7577
src/crypto/groestl_tables.h
@@ -203,6 +205,10 @@ if (NOT WITH_AEON)
203205
add_definitions(/DXMRIG_NO_AEON)
204206
endif()
205207

208+
if (NOT WITH_SUMO)
209+
add_definitions(/DXMRIG_NO_SUMO)
210+
endif()
211+
206212
if (WITH_HTTPD)
207213
find_package(MHD)
208214

src/crypto/CryptoNight.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
void (*cryptonight_hash_ctx)(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = nullptr;
4242

4343

44-
#define CRYPTONIGHT_HASH(NAME, ITERATIONS, MEM, MASK, SOFT_AES) \
44+
#define CRYPTONIGHT_HASH(NAME, ALGO, SOFT_AES) \
4545
switch (variant) { \
4646
case xmrig::VARIANT_V1: \
47-
return cryptonight_##NAME##_hash<ITERATIONS, MEM, MASK, SOFT_AES, xmrig::VARIANT_V1>(input, size, output, ctx); \
47+
return cryptonight_##NAME##_hash<ALGO, SOFT_AES, xmrig::VARIANT_V1>(input, size, output, ctx); \
4848
\
4949
case xmrig::VARIANT_NONE: \
50-
return cryptonight_##NAME##_hash<ITERATIONS, MEM, MASK, SOFT_AES, xmrig::VARIANT_NONE>(input, size, output, ctx); \
50+
return cryptonight_##NAME##_hash<ALGO, SOFT_AES, xmrig::VARIANT_NONE>(input, size, output, ctx); \
5151
\
5252
default: \
5353
break; \
@@ -56,50 +56,50 @@ void (*cryptonight_hash_ctx)(const uint8_t *input, size_t size, uint8_t *output,
5656

5757
static void cryptonight_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, struct cryptonight_ctx *ctx, int variant) {
5858
# if !defined(XMRIG_ARMv7)
59-
CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false)
59+
CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT, false)
6060
# endif
6161
}
6262

6363

6464
static void cryptonight_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
6565
# if !defined(XMRIG_ARMv7)
66-
CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false)
66+
CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT, false)
6767
# endif
6868
}
6969

7070

7171
static void cryptonight_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
72-
CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true)
72+
CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT, true)
7373
}
7474

7575

7676
static void cryptonight_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
77-
CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true)
77+
CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT, true)
7878
}
7979

8080

8181
#ifndef XMRIG_NO_AEON
8282
static void cryptonight_lite_av1_aesni(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
8383
# if !defined(XMRIG_ARMv7)
84-
CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, false)
84+
CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT_LITE, false)
8585
# endif
8686
}
8787

8888

8989
static void cryptonight_lite_av2_aesni_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
9090
# if !defined(XMRIG_ARMv7)
91-
CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, false)
91+
CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT_LITE, false)
9292
# endif
9393
}
9494

9595

9696
static void cryptonight_lite_av3_softaes(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
97-
CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, true)
97+
CRYPTONIGHT_HASH(single, xmrig::CRYPTONIGHT_LITE, true)
9898
}
9999

100100

101101
static void cryptonight_lite_av4_softaes_double(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) {
102-
CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, true)
102+
CRYPTONIGHT_HASH(double, xmrig::CRYPTONIGHT_LITE, true)
103103
}
104104

105105
void (*cryptonight_variations[8])(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) = {

src/crypto/CryptoNight_constants.h

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* XMRig
2+
* Copyright 2010 Jeff Garzik <[email protected]>
3+
* Copyright 2012-2014 pooler <[email protected]>
4+
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
5+
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
6+
* Copyright 2016 Jay D Dee <[email protected]>
7+
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
8+
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
9+
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <[email protected]>
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
#ifndef __CRYPTONIGHT_CONSTANTS_H__
26+
#define __CRYPTONIGHT_CONSTANTS_H__
27+
28+
29+
#include <stdint.h>
30+
31+
32+
#include "xmrig.h"
33+
34+
35+
namespace xmrig
36+
{
37+
38+
constexpr const size_t CRYPTONIGHT_MEMORY = 2 * 1024 * 1024;
39+
constexpr const uint32_t CRYPTONIGHT_MASK = 0x1FFFF0;
40+
constexpr const uint32_t CRYPTONIGHT_ITER = 0x80000;
41+
42+
constexpr const size_t CRYPTONIGHT_LITE_MEMORY = 1 * 1024 * 1024;
43+
constexpr const uint32_t CRYPTONIGHT_LITE_MASK = 0xFFFF0;
44+
constexpr const uint32_t CRYPTONIGHT_LITE_ITER = 0x40000;
45+
46+
constexpr const size_t CRYPTONIGHT_HEAVY_MEMORY = 4 * 1024 * 1024;
47+
constexpr const uint32_t CRYPTONIGHT_HEAVY_MASK = 0x3FFFF0;
48+
constexpr const uint32_t CRYPTONIGHT_HEAVY_ITER = 0x40000;
49+
50+
51+
template<Algo ALGO> inline constexpr size_t cn_select_memory() { return 0; }
52+
template<> inline constexpr size_t cn_select_memory<CRYPTONIGHT>() { return CRYPTONIGHT_MEMORY; }
53+
template<> inline constexpr size_t cn_select_memory<CRYPTONIGHT_LITE>() { return CRYPTONIGHT_LITE_MEMORY; }
54+
template<> inline constexpr size_t cn_select_memory<CRYPTONIGHT_HEAVY>() { return CRYPTONIGHT_HEAVY_MEMORY; }
55+
56+
inline size_t cn_select_memory(Algo algorithm)
57+
{
58+
switch(algorithm)
59+
{
60+
case CRYPTONIGHT:
61+
return CRYPTONIGHT_MEMORY;
62+
63+
case CRYPTONIGHT_LITE:
64+
return CRYPTONIGHT_LITE_MEMORY;
65+
66+
case CRYPTONIGHT_HEAVY:
67+
return CRYPTONIGHT_HEAVY_MEMORY;
68+
}
69+
}
70+
71+
72+
template<Algo ALGO> inline constexpr uint32_t cn_select_mask() { return 0; }
73+
template<> inline constexpr uint32_t cn_select_mask<CRYPTONIGHT>() { return CRYPTONIGHT_MASK; }
74+
template<> inline constexpr uint32_t cn_select_mask<CRYPTONIGHT_LITE>() { return CRYPTONIGHT_LITE_MASK; }
75+
template<> inline constexpr uint32_t cn_select_mask<CRYPTONIGHT_HEAVY>() { return CRYPTONIGHT_HEAVY_MASK; }
76+
77+
inline uint32_t cn_select_mask(Algo algorithm)
78+
{
79+
switch(algorithm)
80+
{
81+
case CRYPTONIGHT:
82+
return CRYPTONIGHT_MASK;
83+
84+
case CRYPTONIGHT_LITE:
85+
return CRYPTONIGHT_LITE_MASK;
86+
87+
case CRYPTONIGHT_HEAVY:
88+
return CRYPTONIGHT_HEAVY_MASK;
89+
}
90+
}
91+
92+
93+
template<Algo ALGO> inline constexpr uint32_t cn_select_iter() { return 0; }
94+
template<> inline constexpr uint32_t cn_select_iter<CRYPTONIGHT>() { return CRYPTONIGHT_ITER; }
95+
template<> inline constexpr uint32_t cn_select_iter<CRYPTONIGHT_LITE>() { return CRYPTONIGHT_LITE_ITER; }
96+
template<> inline constexpr uint32_t cn_select_iter<CRYPTONIGHT_HEAVY>() { return CRYPTONIGHT_HEAVY_ITER; }
97+
98+
inline uint32_t cn_select_iter(Algo algorithm)
99+
{
100+
switch(algorithm)
101+
{
102+
case CRYPTONIGHT:
103+
return CRYPTONIGHT_ITER;
104+
105+
case CRYPTONIGHT_LITE:
106+
return CRYPTONIGHT_LITE_ITER;
107+
108+
case CRYPTONIGHT_HEAVY:
109+
return CRYPTONIGHT_HEAVY_ITER;
110+
}
111+
}
112+
113+
114+
} /* namespace xmrig */
115+
116+
117+
#endif /* __CRYPTONIGHT_CONSTANTS_H__ */

src/crypto/CryptoNight_x86.h

+29-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636

3737
#include "crypto/CryptoNight.h"
38+
#include "crypto/CryptoNight_constants.h"
3839
#include "crypto/CryptoNight_monero.h"
3940
#include "crypto/soft_aes.h"
4041

@@ -309,9 +310,13 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output)
309310
}
310311

311312

312-
template<size_t ITERATIONS, size_t MEM, size_t MASK, bool SOFT_AES, int VARIANT>
313+
template<xmrig::Algo ALGO, bool SOFT_AES, int VARIANT>
313314
inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx)
314315
{
316+
constexpr size_t MASK = xmrig::cn_select_mask<ALGO>();
317+
constexpr size_t ITERATIONS = xmrig::cn_select_iter<ALGO>();
318+
constexpr size_t MEM = xmrig::cn_select_memory<ALGO>();
319+
315320
keccak(input, (int) size, ctx->state0, 200);
316321

317322
VARIANT1_INIT(0);
@@ -367,9 +372,13 @@ inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t si
367372
}
368373

369374

370-
template<size_t ITERATIONS, size_t MEM, size_t MASK, bool SOFT_AES, int VARIANT>
375+
template<xmrig::Algo ALGO, bool SOFT_AES, int VARIANT>
371376
inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx)
372377
{
378+
constexpr size_t MASK = xmrig::cn_select_mask<ALGO>();
379+
constexpr size_t ITERATIONS = xmrig::cn_select_iter<ALGO>();
380+
constexpr size_t MEM = xmrig::cn_select_memory<ALGO>();
381+
373382
keccak(input, (int) size, ctx->state0, 200);
374383
keccak(input + size, (int) size, ctx->state1, 200);
375384

@@ -464,4 +473,22 @@ inline void cryptonight_double_hash(const uint8_t *__restrict__ input, size_t si
464473
extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, output + 32);
465474
}
466475

476+
477+
template<xmrig::Algo ALGO, bool SOFT_AES, int VARIANT>
478+
inline void cryptonight_triple_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx)
479+
{
480+
}
481+
482+
483+
template<xmrig::Algo ALGO, bool SOFT_AES, int VARIANT>
484+
inline void cryptonight_quad_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx)
485+
{
486+
}
487+
488+
489+
template<xmrig::Algo ALGO, bool SOFT_AES, int VARIANT>
490+
inline void cryptonight_penta_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx)
491+
{
492+
}
493+
467494
#endif /* __CRYPTONIGHT_X86_H__ */

src/net/Job.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525

26+
#include <assert.h>
2627
#include <string.h>
2728

2829

@@ -62,11 +63,11 @@ Job::Job() :
6263
m_algo(xmrig::CRYPTONIGHT),
6364
m_poolId(-2),
6465
m_threadId(-1),
65-
m_variant(xmrig::VARIANT_AUTO),
6666
m_size(0),
6767
m_diff(0),
6868
m_target(0),
69-
m_blob()
69+
m_blob(),
70+
m_variant(xmrig::VARIANT_AUTO)
7071
{
7172
}
7273

@@ -77,12 +78,12 @@ Job::Job(int poolId, bool nicehash, int algo, int variant) :
7778
m_algo(algo),
7879
m_poolId(poolId),
7980
m_threadId(-1),
80-
m_variant(variant),
8181
m_size(0),
8282
m_diff(0),
8383
m_target(0),
8484
m_blob()
8585
{
86+
setVariant(variant);
8687
}
8788

8889

@@ -174,10 +175,12 @@ void Job::setVariant(int variant)
174175
case xmrig::VARIANT_AUTO:
175176
case xmrig::VARIANT_NONE:
176177
case xmrig::VARIANT_V1:
177-
m_variant = variant;
178+
m_variant = static_cast<xmrig::Variant>(variant);
178179
break;
179180

180181
default:
182+
assert(false);
183+
m_variant = xmrig::VARIANT_AUTO;
181184
break;
182185
}
183186
}

src/net/Job.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ class Job
5555
inline const xmrig::Id &id() const { return m_id; }
5656
inline int poolId() const { return m_poolId; }
5757
inline int threadId() const { return m_threadId; }
58-
inline int variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? 1 : 0) : m_variant); }
5958
inline size_t size() const { return m_size; }
6059
inline uint32_t *nonce() { return reinterpret_cast<uint32_t*>(m_blob + 39); }
6160
inline uint32_t diff() const { return (uint32_t) m_diff; }
6261
inline uint64_t target() const { return m_target; }
6362
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
6463
inline void setPoolId(int poolId) { m_poolId = poolId; }
6564
inline void setThreadId(int threadId) { m_threadId = threadId; }
65+
inline xmrig::Variant variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? xmrig::VARIANT_V1 : xmrig::VARIANT_NONE) : m_variant); }
6666

6767
static bool fromHex(const char* in, unsigned int len, unsigned char* out);
6868
static inline uint32_t *nonce(uint8_t *blob) { return reinterpret_cast<uint32_t*>(blob + 39); }
@@ -78,12 +78,12 @@ class Job
7878
int m_algo;
7979
int m_poolId;
8080
int m_threadId;
81-
int m_variant;
8281
size_t m_size;
8382
uint64_t m_diff;
8483
uint64_t m_target;
8584
uint8_t m_blob[96]; // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk.
8685
xmrig::Id m_id;
86+
xmrig::Variant m_variant;
8787
};
8888

8989
#endif /* __JOB_H__ */

0 commit comments

Comments
 (0)