diff --git a/src/block_template.cpp b/src/block_template.cpp index 736d416..70a89d3 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -140,10 +140,10 @@ BlockTemplate& BlockTemplate::operator=(const BlockTemplate& b) *m_poolBlockTemplate = *b.m_poolBlockTemplate; m_finalReward = b.m_finalReward.load(); - memcpy(m_minerTxKeccakState, b.m_minerTxKeccakState, sizeof(m_minerTxKeccakState)); + m_minerTxKeccakState = b.m_minerTxKeccakState; m_minerTxKeccakStateInputLength = b.m_minerTxKeccakStateInputLength; - memcpy(m_sidechainHashKeccakState, b.m_sidechainHashKeccakState, sizeof(m_sidechainHashKeccakState)); + m_sidechainHashKeccakState = b.m_sidechainHashKeccakState; m_sidechainHashInputLength = b.m_sidechainHashInputLength; m_minerTx.clear(); @@ -680,7 +680,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const m_sidechainHashBlob.insert(m_sidechainHashBlob.end(), consensus_id.begin(), consensus_id.end()); { - memset(m_sidechainHashKeccakState, 0, sizeof(m_sidechainHashKeccakState)); + m_sidechainHashKeccakState = {}; const size_t extra_nonce_offset = m_sidechainHashBlob.size() - HASH_SIZE - EXTRA_NONCE_SIZE; if (extra_nonce_offset >= KeccakParams::HASH_DATA_AREA) { @@ -734,7 +734,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const } } - memset(m_minerTxKeccakState, 0, sizeof(m_minerTxKeccakState)); + m_minerTxKeccakState = {}; const size_t extra_nonce_offset = m_extraNonceOffsetInTemplate - m_minerTxOffsetInTemplate; if (extra_nonce_offset >= KeccakParams::HASH_DATA_AREA) { @@ -1005,15 +1005,14 @@ hash BlockTemplate::calc_sidechain_hash(uint32_t sidechain_extra_nonce) const memcpy(buf, m_sidechainHashBlob.data() + N, size - N); memcpy(buf + sidechain_extra_nonce_offset - N, sidechain_extra_nonce_buf, EXTRA_NONCE_SIZE); - uint64_t st[25]; - memcpy(st, m_sidechainHashKeccakState, sizeof(st)); + std::array st = m_sidechainHashKeccakState; keccak_finish(buf, inlen, st); - if (pool_block_debug() && (memcmp(st, result.h, HASH_SIZE) != 0)) { + if (pool_block_debug() && (memcmp(st.data(), result.h, HASH_SIZE) != 0)) { LOGERR(1, "calc_sidechain_hash fast path is broken. Fix the code!"); } - memcpy(result.h, st, HASH_SIZE); + memcpy(result.h, st.data(), HASH_SIZE); } return result; @@ -1081,15 +1080,14 @@ hash BlockTemplate::calc_miner_tx_hash(uint32_t extra_nonce) const memcpy(tx_buf + extra_nonce_offset - N, extra_nonce_buf, EXTRA_NONCE_SIZE); memcpy(tx_buf + merkle_root_offset - N, merge_mining_root.h, HASH_SIZE); - uint64_t st[25]; - memcpy(st, m_minerTxKeccakState, sizeof(st)); + std::array st = m_minerTxKeccakState; keccak_finish(tx_buf, inlen, st); - if (pool_block_debug() && (memcmp(st, full_hash.h, HASH_SIZE) != 0)) { + if (pool_block_debug() && (memcmp(st.data(), full_hash.h, HASH_SIZE) != 0)) { LOGERR(1, "calc_miner_tx_hash fast path is broken. Fix the code!"); } - memcpy(hashes, st, HASH_SIZE); + memcpy(hashes, st.data(), HASH_SIZE); } // 2. Base RCT, single 0 byte in miner tx diff --git a/src/block_template.h b/src/block_template.h index dfacf59..98eff14 100644 --- a/src/block_template.h +++ b/src/block_template.h @@ -110,11 +110,11 @@ private: // Temp vectors, will be cleaned up after use and skipped in copy constructor/assignment operators std::vector m_minerTx; - uint64_t m_minerTxKeccakState[25]; + std::array m_minerTxKeccakState; size_t m_minerTxKeccakStateInputLength; std::vector m_sidechainHashBlob; - uint64_t m_sidechainHashKeccakState[25]; + std::array m_sidechainHashKeccakState; size_t m_sidechainHashInputLength; std::vector m_blockHeader; diff --git a/src/keccak.cpp b/src/keccak.cpp index 082ecd0..c1d0789 100644 --- a/src/keccak.cpp +++ b/src/keccak.cpp @@ -36,7 +36,7 @@ static const uint64_t keccakf_rndc[24] = 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 }; -NOINLINE void keccakf(uint64_t (&st)[25]) +NOINLINE void keccakf(std::array& st) { for (int round = 0; round < KeccakParams::ROUNDS; ++round) { uint64_t bc[5]; @@ -115,7 +115,7 @@ NOINLINE void keccakf(uint64_t (&st)[25]) } } -NOINLINE void keccak_step(const uint8_t* &in, int &inlen, uint64_t (&st)[25]) +NOINLINE void keccak_step(const uint8_t* &in, int &inlen, std::array& st) { constexpr int rsiz = KeccakParams::HASH_DATA_AREA; constexpr int rsizw = rsiz / 8; @@ -128,7 +128,7 @@ NOINLINE void keccak_step(const uint8_t* &in, int &inlen, uint64_t (&st)[25]) } } -NOINLINE void keccak_finish(const uint8_t* in, int inlen, uint64_t (&st)[25]) +NOINLINE void keccak_finish(const uint8_t* in, int inlen, std::array& st) { constexpr int rsiz = KeccakParams::HASH_DATA_AREA; constexpr int rsizw = rsiz / 8; diff --git a/src/keccak.h b/src/keccak.h index 7274d61..9d7b846 100644 --- a/src/keccak.h +++ b/src/keccak.h @@ -24,25 +24,25 @@ enum KeccakParams { ROUNDS = 24, }; -void keccakf(uint64_t (&st)[25]); -void keccak_step(const uint8_t* &in, int &inlen, uint64_t (&st)[25]); -void keccak_finish(const uint8_t* in, int inlen, uint64_t (&st)[25]); +void keccakf(std::array &st); +void keccak_step(const uint8_t* &in, int &inlen, std::array& st); +void keccak_finish(const uint8_t* in, int inlen, std::array& st); template FORCEINLINE void keccak(const uint8_t* in, int inlen, uint8_t (&md)[N]) { static_assert((N == 32) || (N == 200), "invalid size"); - uint64_t st[25] = {}; + std::array st = {}; keccak_step(in, inlen, st); keccak_finish(in, inlen, st); - memcpy(md, st, N); + memcpy(md, st.data(), N); } template FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen) { - uint64_t st[25] = {}; + std::array st = {}; const int rsiz = sizeof(st) == mdlen ? KeccakParams::HASH_DATA_AREA : 200 - 2 * mdlen; const int rsizw = rsiz / 8; @@ -77,7 +77,7 @@ FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen) keccakf(st); - memcpy(md, st, mdlen); + memcpy(md, st.data(), mdlen); } } // namespace p2pool