diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index f6ab2935..7317f728 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -129,8 +129,21 @@ bool CryptoNight::selfTest(int algo) 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; + //TODO test multihashs + } + _mm_free(ctx->memory); _mm_free(ctx); - return resultSingle && resultDouble && resultTriple && resultQuadruple && resultQuintuple; + return resultSingle && resultDouble && resultTriple && resultQuadruple && resultQuintuple && resultV1Pow; } \ No newline at end of file diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 889bdc6a..d88f0921 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -36,6 +36,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -370,10 +371,19 @@ public: uint64_t ah[NUM_HASH_BLOCKS]; __m128i bx[NUM_HASH_BLOCKS]; uint64_t idx[NUM_HASH_BLOCKS]; + uint64_t tweak1_2[NUM_HASH_BLOCKS]; + uint64_t version[NUM_HASH_BLOCKS]; for (size_t hashBlock = 0; hashBlock < NUM_HASH_BLOCKS; ++hashBlock) { keccak(static_cast(input) + hashBlock * size, (int) size, ctx->state[hashBlock], 200); + version[hashBlock] = static_cast(input)[hashBlock * size]; + /*if (MONERO)*/ { + if (version[hashBlock] > 6) { + tweak1_2[hashBlock] = (*reinterpret_cast(reinterpret_cast(input) + 35 + hashBlock * size) ^ + *(reinterpret_cast(ctx->state[hashBlock]) + 24)); + } + } } for (size_t hashBlock = 0; hashBlock < NUM_HASH_BLOCKS; ++hashBlock) { @@ -402,6 +412,16 @@ public: _mm_store_si128((__m128i*) &l[hashBlock][idx[hashBlock] & MASK], _mm_xor_si128(bx[hashBlock], cx)); + + /*if (MONERO)*/ { + if (version[hashBlock] > 6) { + const uint8_t tmp = reinterpret_cast(&l[hashBlock][idx[hashBlock] & MASK])[11]; + static const uint32_t table = 0x75310; + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; + ((uint8_t*)(&l[hashBlock][idx[hashBlock] & MASK]))[11] = tmp ^ ((table >> index) & 0x30); + } + } + idx[hashBlock] = EXTRACT64(cx); bx[hashBlock] = cx; @@ -413,9 +433,21 @@ public: al[hashBlock] += hi; ah[hashBlock] += lo; + /*if (MONERO)*/ { + if (version[hashBlock] > 6) { + ah[hashBlock] ^= tweak1_2[hashBlock]; + } + } + ((uint64_t*) &l[hashBlock][idx[hashBlock] & MASK])[0] = al[hashBlock]; ((uint64_t*) &l[hashBlock][idx[hashBlock] & MASK])[1] = ah[hashBlock]; + /*if (MONERO)*/ { + if (version[hashBlock] > 6) { + ah[hashBlock] ^= tweak1_2[hashBlock]; + } + } + ah[hashBlock] ^= ch; al[hashBlock] ^= cl; idx[hashBlock] = al[hashBlock]; @@ -449,6 +481,8 @@ public: keccak(static_cast(input), (int) size, ctx->state[0], 200); + VARIANT1_INIT(0); + l = ctx->memory; h = reinterpret_cast(ctx->state[0]); @@ -473,6 +507,7 @@ public: } _mm_store_si128((__m128i*) &l[idx & MASK], _mm_xor_si128(bx, cx)); + VARIANT1_1(&l[idx & MASK], 0); idx = EXTRACT64(cx); bx = cx; @@ -484,8 +519,10 @@ public: al += hi; ah += lo; + VARIANT1_2(ah, 0); ((uint64_t*) &l[idx & MASK])[0] = al; ((uint64_t*) &l[idx & MASK])[1] = ah; + VARIANT1_2(ah, 0); ah ^= ch; al ^= cl; @@ -510,6 +547,9 @@ public: keccak((const uint8_t*) input, (int) size, ctx->state[0], 200); keccak((const uint8_t*) input + size, (int) size, ctx->state[1], 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state[0]); @@ -549,6 +589,9 @@ public: _mm_store_si128((__m128i*) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i*) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK], 0); + VARIANT1_1(&l1[idx1 & MASK], 1); + idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -563,8 +606,10 @@ public: al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -577,8 +622,10 @@ public: al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -609,6 +656,10 @@ public: keccak((const uint8_t*) input + size, (int) size, ctx->state[1], 200); keccak((const uint8_t*) input + 2 * size, (int) size, ctx->state[2], 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + VARIANT1_INIT(2); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; const uint8_t* l2 = ctx->memory + 2 * MEM; @@ -660,6 +711,10 @@ public: _mm_store_si128((__m128i*) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); _mm_store_si128((__m128i*) &l2[idx2 & MASK], _mm_xor_si128(bx2, cx2)); + VARIANT1_1(&l0[idx0 & MASK], 0); + VARIANT1_1(&l1[idx1 & MASK], 1); + VARIANT1_1(&l2[idx2 & MASK], 2); + idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); idx2 = EXTRACT64(cx2); @@ -677,8 +732,10 @@ public: al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -692,8 +749,10 @@ public: al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -707,8 +766,10 @@ public: al2 += hi; ah2 += lo; + VARIANT1_2(ah2, 2); ((uint64_t*) &l2[idx2 & MASK])[0] = al2; ((uint64_t*) &l2[idx2 & MASK])[1] = ah2; + VARIANT1_2(ah2, 2); ah2 ^= ch; al2 ^= cl; @@ -743,6 +804,11 @@ public: keccak((const uint8_t*) input + 2 * size, (int) size, ctx->state[2], 200); keccak((const uint8_t*) input + 3 * size, (int) size, ctx->state[3], 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + VARIANT1_INIT(2); + VARIANT1_INIT(3); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; const uint8_t* l2 = ctx->memory + 2 * MEM; @@ -806,6 +872,11 @@ public: _mm_store_si128((__m128i*) &l2[idx2 & MASK], _mm_xor_si128(bx2, cx2)); _mm_store_si128((__m128i*) &l3[idx3 & MASK], _mm_xor_si128(bx3, cx3)); + VARIANT1_1(&l0[idx0 & MASK], 0); + VARIANT1_1(&l1[idx1 & MASK], 1); + VARIANT1_1(&l2[idx2 & MASK], 2); + VARIANT1_1(&l3[idx3 & MASK], 3); + idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); idx2 = EXTRACT64(cx2); @@ -825,8 +896,10 @@ public: al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -840,8 +913,10 @@ public: al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -855,8 +930,10 @@ public: al2 += hi; ah2 += lo; + VARIANT1_2(ah2, 2); ((uint64_t*) &l2[idx2 & MASK])[0] = al2; ((uint64_t*) &l2[idx2 & MASK])[1] = ah2; + VARIANT1_2(ah2, 2); ah2 ^= ch; al2 ^= cl; @@ -870,8 +947,10 @@ public: al3 += hi; ah3 += lo; + VARIANT1_2(ah3, 3); ((uint64_t*) &l3[idx3 & MASK])[0] = al3; ((uint64_t*) &l3[idx3 & MASK])[1] = ah3; + VARIANT1_2(ah3, 3); ah3 ^= ch; al3 ^= cl; @@ -910,6 +989,12 @@ public: keccak((const uint8_t*) input + 3 * size, (int) size, ctx->state[3], 200); keccak((const uint8_t*) input + 4 * size, (int) size, ctx->state[4], 200); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + VARIANT1_INIT(2); + VARIANT1_INIT(3); + VARIANT1_INIT(4); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; const uint8_t* l2 = ctx->memory + 2 * MEM; @@ -985,6 +1070,12 @@ public: _mm_store_si128((__m128i*) &l3[idx3 & MASK], _mm_xor_si128(bx3, cx3)); _mm_store_si128((__m128i*) &l4[idx4 & MASK], _mm_xor_si128(bx4, cx4)); + VARIANT1_1(&l0[idx0 & MASK], 0); + VARIANT1_1(&l1[idx1 & MASK], 1); + VARIANT1_1(&l2[idx2 & MASK], 2); + VARIANT1_1(&l3[idx3 & MASK], 3); + VARIANT1_1(&l4[idx4 & MASK], 4); + idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); idx2 = EXTRACT64(cx2); @@ -1005,8 +1096,10 @@ public: al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -1020,8 +1113,10 @@ public: al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -1035,8 +1130,10 @@ public: al2 += hi; ah2 += lo; + VARIANT1_2(ah2, 2); ((uint64_t*) &l2[idx2 & MASK])[0] = al2; ((uint64_t*) &l2[idx2 & MASK])[1] = ah2; + VARIANT1_2(ah2, 2); ah2 ^= ch; al2 ^= cl; @@ -1050,8 +1147,10 @@ public: al3 += hi; ah3 += lo; + VARIANT1_2(ah3, 3); ((uint64_t*) &l3[idx3 & MASK])[0] = al3; ((uint64_t*) &l3[idx3 & MASK])[1] = ah3; + VARIANT1_2(ah3, 3); ah3 ^= ch; al3 ^= cl; @@ -1065,8 +1164,10 @@ public: al4 += hi; ah4 += lo; + VARIANT1_2(ah4, 4); ((uint64_t*) &l4[idx4 & MASK])[0] = al4; ((uint64_t*) &l4[idx4 & MASK])[1] = ah4; + VARIANT1_2(ah4, 4); ah4 ^= ch; al4 ^= cl; diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index dde9b247..a3d4f110 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -91,9 +91,30 @@ const static uint8_t test_output_light[] = { }; -const static uint8_t test_input_monero_slow[] = { - 0xb5, 0xa7, 0xf6, 0x3a, 0xbb, 0x94, 0xd0, 0x7d, 0x1a, 0x64, 0x45, 0xc3, 0x6c, 0x07, 0xc7, 0xe8, - 0x32, 0x7f, 0xe6, 0x1b, 0x16, 0x47, 0xe3, 0x91, 0xb4, 0xc7, 0xed, 0xae, 0x5d, 0xe5, 0x7a, 0x3d +const static uint8_t test_input_monero_v1_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[] = + {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[] = + {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] = { + {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, + 0x16, 0x56, 0x14, 0x34, 0x2d, 0xac, 0x41, 0x9a, 0xdc, 0x6a, 0x47, 0xdc, 0xe2, 0x57, 0xeb, 0x3e}, + {0xed, 0x08, 0x2e, 0x49, 0xdb, 0xd5, 0xbb, 0xe3, 0x4a, 0x37, 0x26, 0xa0, 0xd1, 0xda, 0xd9, 0x81, + 0x14, 0x60, 0x62, 0xb3, 0x9d, 0x36, 0xd6, 0x2c, 0x71, 0xeb, 0x1e, 0xd8, 0xab, 0x49, 0x45, 0x9b} }; #endif /* __CRYPTONIGHT_TEST_H__ */