Skip to content

Instantly share code, notes, and snippets.

@monkins1010
Last active July 21, 2018 08:32
Show Gist options
  • Save monkins1010/42eae4db87667b16a2aeee3677ee20cd to your computer and use it in GitHub Desktop.
Save monkins1010/42eae4db87667b16a2aeee3677ee20cd to your computer and use it in GitHub Desktop.
C++ code source miner
// VARIANT ALTERATIONS
#define VARIANT1_INIT(part) {}
#define VARIANT1_1(p) \
if (VARIANT > 0) { \
const uint8_t tmp1 = ((const uint8_t*)(p))[11]; \
static const uint32_t table1 = 0x86420; \
const uint8_t index1 = (((tmp1 >> 3) & 6) | (tmp1 & 1)) << 1; \
((uint8_t*)(p))[11] = tmp1 ^ ((table1 >> index1) & 0x30); \
}
#define VARIANT1_2(p, part) \
if (VARIANT > 0) { \
const uint8_t tmp2 = ((uint8_t*)(&p))[1]; \
static const uint32_t table2 = 0x75310; \
const uint8_t index2 = (((tmp2 >> 3) & 6) | (tmp2 & 1)) << 1; \
((uint8_t*)(&p))[1] = tmp2 ^ ((table2 >> index2) & 0x33); \
}
#endif
template<size_t ITERATIONS, size_t MEM, size_t MASK, bool SOFT_AES, int VARIANT>
inline void cryptonight_single_hash(const uint8_t *__restrict__ input, size_t size, uint8_t *__restrict__ output, cryptonight_ctx *__restrict__ ctx)
{
keccak(input, (int) size, ctx->state0, 200);
VARIANT1_INIT(0);
cn_explode_scratchpad<MEM, SOFT_AES>((__m128i*) ctx->state0, (__m128i*) ctx->memory);
const uint8_t* l0 = ctx->memory;
uint64_t* h0 = reinterpret_cast<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; i < ITERATIONS; i++) {
__m128i cx;
if (SOFT_AES) {
cx = soft_aesenc((uint32_t*)&l0[idx0 & MASK], _mm_set_epi64x(ah0, al0));
}
else {
cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]);
cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0));
}
_mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx));
VARIANT1_1(&l0[idx0 & MASK]);
idx0 = EXTRACT64(cx);
bx0 = cx;
uint64_t hi, lo, cl, ch;
cl = ((uint64_t*) &l0[idx0 & MASK])[0];
ch = ((uint64_t*) &l0[idx0 & MASK])[1];
lo = __umul128(idx0, cl, &hi);
al0 += hi;
ah0 += lo;
uint64_t tmp_al0 = al0;
VARIANT1_2(al0, 0);
((uint64_t*)&l0[idx0 & MASK])[0] = al0;
((uint64_t*)&l0[idx0 & MASK])[1] = ah0;
al0 = tmp_al0;
ah0 ^= ch;
al0 ^= cl;
idx0 = al0;
}
cn_implode_scratchpad<MEM, SOFT_AES>((__m128i*) ctx->memory, (__m128i*) ctx->state0);
keccakf(h0, 24);
extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment