Skip to content

Commit

Permalink
Fix block verification, move iteration halving to format_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
who-biz committed Jul 10, 2019
1 parent 6571eac commit 92748d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
15 changes: 5 additions & 10 deletions src/crypto/slow-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
// the useAes test is only performed once, not every iteration.
if(useAes)
{
uint32_t iters_var = variant > 1 ? (iters >> 1) : iters;
for(i = 0; i < iters_var; i++)
for(i = 0; i < iters; i++)
{
pre_aes();
_c = _mm_aesenc_si128(_c, _a);
Expand All @@ -782,8 +781,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
}
else
{
uint32_t iters_var = variant > 1 ? (iters >> 1) : iters;
for(i = 0; i < iters_var; i++)
for(i = 0; i < iters; i++)
{
pre_aes();
aesb_single_round((uint8_t *) &_c, (uint8_t *) &_c, (uint8_t *) &_a);
Expand Down Expand Up @@ -1127,8 +1125,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
_b1 = vld1q_u8(((const uint8_t *)b) + AES_BLOCK_SIZE);


uint32_t iters_var = variant > 1 ? (iters >> 1) : iters;
for(i = 0; i < iters_var; i++)
for(i = 0; i < iters; i++)
{
pre_aes();
_c = vaeseq_u8(_c, zero);
Expand Down Expand Up @@ -1335,8 +1332,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
U64(b)[0] = U64(&state.k[16])[0] ^ U64(&state.k[48])[0];
U64(b)[1] = U64(&state.k[16])[1] ^ U64(&state.k[48])[1];

uint32_t iters_var = variant > 1 ? (iters >> 1) : iters;
for(i = 0; i < iters_var; i++)
for(i = 0; i < iters; i++)
{
#define MASK ((uint32_t)(((MEMORY / AES_BLOCK_SIZE) - 1) << 4))
#define state_index(x) ((*(uint32_t *) x) & MASK)
Expand Down Expand Up @@ -1523,8 +1519,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int
b[i] = state.k[AES_BLOCK_SIZE + i] ^ state.k[AES_BLOCK_SIZE * 3 + i];
}

uint32_t iters_var = variant > 1 ? (iters >> 1) : iters;
for(i = 0; i < iters_var; i++) {
for(i = 0; i < iters; i++) {
/* Dependency chain: address -> read value ------+
* written value <-+ hard function (AES or MUL) <+
* next address <-+
Expand Down
22 changes: 11 additions & 11 deletions src/cryptonote_basic/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace cryptonote
//---------------------------------------------------------------
bool expand_transaction_1(transaction &tx, bool base_only)
{
if (tx.version >= 2 && !is_coinbase(tx))
if (tx.version >= 1 && !is_coinbase(tx))
{
rct::rctSig &rv = tx.rct_signatures;
if (rv.outPk.size() != tx.vout.size())
Expand Down Expand Up @@ -369,13 +369,11 @@ namespace cryptonote
return r;
}
//---------------------------------------------------------------
bool parse_tx_extra(const std::vector<uint8_t>& tx_extra, std::vector<tx_extra_field>& tx_extra_fields)
bool parse_tx_extra(const std::vector<uint8_t>& tx_extra, std::vector<tx_extra_field>& tx_extra_fields)
{
tx_extra_fields.clear();

if(tx_extra.empty())
return true;

std::string extra_str(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size());
std::istringstream iss(extra_str);
binary_archive<false> ar(iss);
Expand Down Expand Up @@ -640,8 +638,6 @@ namespace cryptonote
{
std::string res = string_tools::pod_to_hex(h);
CHECK_AND_ASSERT_MES(res.size() == 64, res, "wrong hash256 with string_tools::pod_to_hex conversion");
auto erased_pos = res.erase(8, 48);
res.insert(8, "....");
return res;
}
//---------------------------------------------------------------
Expand Down Expand Up @@ -847,9 +843,7 @@ namespace cryptonote
{
if (t.is_hash_valid())
{
#ifdef ENABLE_HASH_CASH_INTEGRITY_CHECK
CHECK_AND_ASSERT_THROW_MES(!calculate_transaction_hash(t, res, blob_size) || t.hash == res, "tx hash cash integrity failure");
#endif
res = t.hash;
if (blob_size)
{
Expand Down Expand Up @@ -901,9 +895,7 @@ namespace cryptonote
{
if (b.is_hash_valid())
{
#ifdef ENABLE_HASH_CASH_INTEGRITY_CHECK
CHECK_AND_ASSERT_THROW_MES(!calculate_block_hash(b, res) || b.hash == res, "block hash cash integrity failure");
#endif
res = b.hash;
++block_hashes_cached_count;
return true;
Expand All @@ -930,14 +922,20 @@ namespace cryptonote
const int cn_variant = b.major_version >= 5 ? ( b.major_version >= 8 ? 2 : 1 ) : 0;
int cn_iters = b.major_version >= 6 ? ( b.major_version >= 7 ? 0x40000 : 0x20000 ) : 0x80000;

if (b.major_version <= 8)
if (b.major_version <= 7)
{
cn_iters += ((height + 1) & 0x3FF);
}
else if (b.major_version == 8)
{
cn_iters += ((height + 1) & 0x3FF);
cn_iters >>= 1;
}
else if (b.major_version == 9)
{
const uint64_t stamp = b.timestamp;
cn_iters += (((stamp % height) + (height + 1)) & 0xFFF);
cn_iters >>= 1;
}
else if (b.major_version >= 10)
{
Expand All @@ -960,6 +958,8 @@ namespace cryptonote
else if (!two) {
cn_iters += (((stamp % id_num) + height) & 0x7FFF); }

cn_iters >>= 1;

LOG_PRINT_L2("\nIterations : "<< cn_iters);

}
Expand Down

0 comments on commit 92748d5

Please sign in to comment.