Skip to content

Commit

Permalink
Merge pull request #11 from uPlexa/dev
Browse files Browse the repository at this point in the history
Remember the past to fix the future
  • Loading branch information
uPlexa authored Apr 20, 2019
2 parents eddac44 + 8157961 commit 2b635ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/crypto/hash-ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ enum {
};

void cn_fast_hash(const void *data, size_t length, char *hash);
void cn_slow_hash(const void *data, size_t length, char *hash, int light, int variant, int prehashed);
void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int variant, int prehashed);

void hash_extra_blake(const void *data, size_t length, char *hash);
void hash_extra_groestl(const void *data, size_t length, char *hash);
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ namespace crypto {
return h;
}

inline void cn_slow_hash(const void *data, std::size_t length, hash &hash, int light = 0, int variant = 0) {
cn_slow_hash(data, length, reinterpret_cast<char *>(&hash), light, variant, 0/*prehashed*/);
inline void cn_slow_hash(const void *data, std::size_t length, hash &hash, int upxtwo = 0, int variant = 0) {
cn_slow_hash(data, length, reinterpret_cast<char *>(&hash), upxtwo, variant, 0/*prehashed*/);
}

inline void cn_slow_hash_prehashed(const void *data, std::size_t length, hash &hash, int light = 0, int variant = 0) {
cn_slow_hash(data, length, reinterpret_cast<char *>(&hash), light, variant, 1/*prehashed*/);
inline void cn_slow_hash_prehashed(const void *data, std::size_t length, hash &hash, int upxtwo = 0, int variant = 0) {
cn_slow_hash(data, length, reinterpret_cast<char *>(&hash), upxtwo, variant, 1/*prehashed*/);
}

inline void tree_hash(const hash *hashes, std::size_t count, hash &root_hash) {
Expand Down
45 changes: 23 additions & 22 deletions src/crypto/slow-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
#include "variant2_int_sqrt.h"

#define MEMORY (1 << 21) // 2MB scratchpad
#define ITER() (variant >= 2 ? (1 << 15) : (1 << 18)) // 2^18 = 262,144, 2^15 = 32,768
//#define ITER() (variant >= 2 ? (1 << 15) : (1 << 18)) // 2^18 = 262,144, 2^15 = 32,768
#define ITER() (variant >= 2 ? (1 << 15) : (variant == 1 ? (1 << 18) : (1 << 20))) // 2^18 = 262,144, 2^15 = 32,768
#define AES_BLOCK_SIZE 16
#define AES_KEY_SIZE 32
#define INIT_SIZE_BLK 8
Expand Down Expand Up @@ -275,7 +276,7 @@ extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *exp
#endif

#define pre_aes() \
j = state_index(a,(upxtwo?16:2)); \
j = state_index(a,(upxtwo>=2?16:(upxtwo==1?2:1))); \
_c = _mm_load_si128(R128(&hp_state[j])); \
_a = _mm_load_si128(R128(a)); \

Expand All @@ -293,7 +294,7 @@ extern int aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *exp
_mm_store_si128(R128(c), _c); \
_mm_store_si128(R128(&hp_state[j]), _mm_xor_si128(_b, _c)); \
VARIANT1_1(&hp_state[j]); \
j = state_index(c,(upxtwo?16:2)); \
j = state_index(c,(upxtwo>=2?16:(upxtwo==1?2:1))); \
p = U64(&hp_state[j]); \
b[0] = p[0]; b[1] = p[1]; \
VARIANT2_INTEGER_MATH_SSE2(b, c); \
Expand Down Expand Up @@ -737,7 +738,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
if(useAes)
{
aes_expand_key(state.hs.b, expandedKey);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
aes_pseudo_round(text, text, expandedKey, INIT_SIZE_BLK);
memcpy(&hp_state[i * INIT_SIZE_BYTE], text, INIT_SIZE_BYTE);
Expand All @@ -747,7 +748,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
{
aes_ctx = (oaes_ctx *) oaes_alloc();
oaes_key_import_data(aes_ctx, state.hs.b, AES_KEY_SIZE);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
for(j = 0; j < INIT_SIZE_BLK; j++)
aesb_pseudo_round(&text[AES_BLOCK_SIZE * j], &text[AES_BLOCK_SIZE * j], aes_ctx->key->exp_data);
Expand Down Expand Up @@ -797,7 +798,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
if(useAes)
{
aes_expand_key(&state.hs.b[32], expandedKey);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
// add the xor to the pseudo round
aes_pseudo_round_xor(text, text, expandedKey, &hp_state[i * INIT_SIZE_BYTE], INIT_SIZE_BLK);
Expand All @@ -806,7 +807,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
else
{
oaes_key_import_data(aes_ctx, &state.hs.b[32], AES_KEY_SIZE);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
for(j = 0; j < INIT_SIZE_BLK; j++)
{
Expand Down Expand Up @@ -887,7 +888,7 @@ union cn_slow_hash_state
__asm__("umulh %0, %1, %2\n\t" : "=r"(hi) : "r"(c[0]), "r"(b[0]) );

#define pre_aes() \
j = state_index(a,(upxtwo?16:2)); \
j = state_index(a,(upxtwo>=2?16:(upxtwo==1?2:1))); \
_c = vld1q_u8(&hp_state[j]); \
_a = vld1q_u8((const uint8_t *)a); \

Expand All @@ -896,7 +897,7 @@ union cn_slow_hash_state
vst1q_u8((uint8_t *)c, _c); \
vst1q_u8(&hp_state[j], veorq_u8(_b, _c)); \
VARIANT1_1(&hp_state[j]); \
j = state_index(c,(upxtwo?16:2)); \
j = state_index(c,(upxtwo>=2?16:(upxtwo==1?2:1))); \
p = U64(&hp_state[j]); \
b[0] = p[0]; b[1] = p[1]; \
VARIANT2_PORTABLE_INTEGER_MATH(b, c); \
Expand Down Expand Up @@ -1069,7 +1070,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
#ifndef FORCE_USE_HEAP
RDATA_ALIGN16 uint8_t hp_state[MEMORY];
#else
uint8_t *hp_state = (uint8_t *)aligned_malloc(MEMORY,16); // (upxtwo?16:2) ??? changeThis
uint8_t *hp_state = (uint8_t *)aligned_malloc(MEMORY,16); // (upxtwo>=2?16:(upxtwo==1?2:1)) ??? changeThis
#endif

uint8_t text[INIT_SIZE_BYTE];
Expand Down Expand Up @@ -1105,7 +1106,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
*/

aes_expand_key(state.hs.b, expandedKey);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
aes_pseudo_round(text, text, expandedKey, INIT_SIZE_BLK);
memcpy(&hp_state[i * INIT_SIZE_BYTE], text, INIT_SIZE_BYTE);
Expand Down Expand Up @@ -1140,7 +1141,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
memcpy(text, state.init, INIT_SIZE_BYTE);

aes_expand_key(&state.hs.b[32], expandedKey);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
// add the xor to the pseudo round
aes_pseudo_round_xor(text, text, expandedKey, &hp_state[i * INIT_SIZE_BYTE], INIT_SIZE_BLK);
Expand Down Expand Up @@ -1319,7 +1320,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v

// use aligned data
memcpy(expandedKey, aes_ctx->key->exp_data, aes_ctx->key->exp_data_len);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
for(j = 0; j < INIT_SIZE_BLK; j++)
aesb_pseudo_round(&text[AES_BLOCK_SIZE * j], &text[AES_BLOCK_SIZE * j], expandedKey);
Expand All @@ -1338,7 +1339,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v


// Iteration 1
j = state_index(a, (upxtwo?16:2));
j = state_index(a, (upxtwo>=2?16:(upxtwo==1?2:1)));
p = &long_state[j];
aesb_single_round(p, p, a);
copy_block(c1, p);
Expand All @@ -1348,7 +1349,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
VARIANT1_1(p);

// Iteration 2
j = state_index(c1, (upxtwo?16:2));
j = state_index(c1, (upxtwo>=2?16:(upxtwo==1?2:1)));
p = &long_state[j];
copy_block(c, p);

Expand All @@ -1371,7 +1372,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
memcpy(text, state.init, INIT_SIZE_BYTE);
oaes_key_import_data(aes_ctx, &state.hs.b[32], AES_KEY_SIZE);
memcpy(expandedKey, aes_ctx->key->exp_data, aes_ctx->key->exp_data_len);
for(i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++)
for(i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++)
{
for(j = 0; j < INIT_SIZE_BLK; j++)
{
Expand Down Expand Up @@ -1510,7 +1511,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
VARIANT2_PORTABLE_INIT();

oaes_key_import_data(aes_ctx, aes_key, AES_KEY_SIZE);
for (i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++) {
for (i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++) {
for (j = 0; j < INIT_SIZE_BLK; j++) {
aesb_pseudo_round(&text[AES_BLOCK_SIZE * j], &text[AES_BLOCK_SIZE * j], aes_ctx->key->exp_data);
}
Expand All @@ -1528,16 +1529,16 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
* next address <-+
*/
/* Iteration 1 */
j = e2i(a, MEMORY / (upxtwo?16:2) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
j = e2i(a, MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
copy_block(c1, &long_state[j]);
aesb_single_round(c1, c1, a);
VARIANT2_PORTABLE_SHUFFLE_ADD(long_state, j);
copy_block(&long_state[j], c1);
xor_blocks(&long_state[j], b);
assert(j == e2i(a, MEMORY / (upxtwo?16:2) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
assert(j == e2i(a, MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
VARIANT1_1(&long_state[j]);
/* Iteration 2 */
j = e2i(c1, MEMORY / (upxtwo?16:2) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
j = e2i(c1, MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
copy_block(c2, &long_state[j]);
VARIANT2_PORTABLE_INTEGER_MATH(c2, c1);
mul(c1, c2, d);
Expand All @@ -1549,7 +1550,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v
xor_blocks(c1, c2);
VARIANT1_2(c2 + 8);
copy_block(&long_state[j], c2);
assert(j == e2i(a, MEMORY / (upxtwo?16:2) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
assert(j == e2i(a, MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
if (variant >= 2) {
copy_block(b + AES_BLOCK_SIZE, b);
}
Expand All @@ -1559,7 +1560,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int upxtwo, int v

memcpy(text, state.init, INIT_SIZE_BYTE);
oaes_key_import_data(aes_ctx, &state.hs.b[32], AES_KEY_SIZE);
for (i = 0; i < MEMORY / (upxtwo?16:2) / INIT_SIZE_BYTE; i++) {
for (i = 0; i < MEMORY / (upxtwo>=2?16:(upxtwo==1?2:1)) / INIT_SIZE_BYTE; i++) {
for (j = 0; j < INIT_SIZE_BLK; j++) {
xor_blocks(&text[j * AES_BLOCK_SIZE], &long_state[i * INIT_SIZE_BYTE + j * AES_BLOCK_SIZE]);
aesb_pseudo_round(&text[AES_BLOCK_SIZE * j], &text[AES_BLOCK_SIZE * j], aes_ctx->key->exp_data);
Expand Down
3 changes: 2 additions & 1 deletion src/cryptonote_basic/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,8 @@ namespace cryptonote
// Opting out of CNv2 PoW Change due to decreased efficiency on lower-end CPU devices.
//const int cn_miner_variant = 1;
const int cn_miner_variant = b.major_version >= 11 ? 2 : 1;
crypto::cn_slow_hash(bd.data(), bd.size(), res, height >= HARDFORK_1_HEIGHT || b.major_version >= 2, cn_miner_variant);
const int upxtwo = b.major_version >= 11 ? 2 : b.major_version >= 2;
crypto::cn_slow_hash(bd.data(), bd.size(), res, upxtwo, cn_miner_variant);
return true;
}
//---------------------------------------------------------------
Expand Down

0 comments on commit 2b635ae

Please sign in to comment.