Refactored keccak interface

This commit is contained in:
SChernykh 2023-11-24 14:20:11 +01:00
parent 65d83aa09c
commit 1bf594d9e2
4 changed files with 22 additions and 24 deletions

View file

@ -140,10 +140,10 @@ BlockTemplate& BlockTemplate::operator=(const BlockTemplate& b)
*m_poolBlockTemplate = *b.m_poolBlockTemplate; *m_poolBlockTemplate = *b.m_poolBlockTemplate;
m_finalReward = b.m_finalReward.load(); 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; 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_sidechainHashInputLength = b.m_sidechainHashInputLength;
m_minerTx.clear(); 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()); 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; const size_t extra_nonce_offset = m_sidechainHashBlob.size() - HASH_SIZE - EXTRA_NONCE_SIZE;
if (extra_nonce_offset >= KeccakParams::HASH_DATA_AREA) { 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; const size_t extra_nonce_offset = m_extraNonceOffsetInTemplate - m_minerTxOffsetInTemplate;
if (extra_nonce_offset >= KeccakParams::HASH_DATA_AREA) { 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, m_sidechainHashBlob.data() + N, size - N);
memcpy(buf + sidechain_extra_nonce_offset - N, sidechain_extra_nonce_buf, EXTRA_NONCE_SIZE); memcpy(buf + sidechain_extra_nonce_offset - N, sidechain_extra_nonce_buf, EXTRA_NONCE_SIZE);
uint64_t st[25]; std::array<uint64_t, 25> st = m_sidechainHashKeccakState;
memcpy(st, m_sidechainHashKeccakState, sizeof(st));
keccak_finish(buf, inlen, st); 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!"); 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; 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 + extra_nonce_offset - N, extra_nonce_buf, EXTRA_NONCE_SIZE);
memcpy(tx_buf + merkle_root_offset - N, merge_mining_root.h, HASH_SIZE); memcpy(tx_buf + merkle_root_offset - N, merge_mining_root.h, HASH_SIZE);
uint64_t st[25]; std::array<uint64_t, 25> st = m_minerTxKeccakState;
memcpy(st, m_minerTxKeccakState, sizeof(st));
keccak_finish(tx_buf, inlen, st); 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!"); 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 // 2. Base RCT, single 0 byte in miner tx

View file

@ -110,11 +110,11 @@ private:
// Temp vectors, will be cleaned up after use and skipped in copy constructor/assignment operators // Temp vectors, will be cleaned up after use and skipped in copy constructor/assignment operators
std::vector<uint8_t> m_minerTx; std::vector<uint8_t> m_minerTx;
uint64_t m_minerTxKeccakState[25]; std::array<uint64_t, 25> m_minerTxKeccakState;
size_t m_minerTxKeccakStateInputLength; size_t m_minerTxKeccakStateInputLength;
std::vector<uint8_t> m_sidechainHashBlob; std::vector<uint8_t> m_sidechainHashBlob;
uint64_t m_sidechainHashKeccakState[25]; std::array<uint64_t, 25> m_sidechainHashKeccakState;
size_t m_sidechainHashInputLength; size_t m_sidechainHashInputLength;
std::vector<uint8_t> m_blockHeader; std::vector<uint8_t> m_blockHeader;

View file

@ -36,7 +36,7 @@ static const uint64_t keccakf_rndc[24] =
0x8000000000008080, 0x0000000080000001, 0x8000000080008008 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
}; };
NOINLINE void keccakf(uint64_t (&st)[25]) NOINLINE void keccakf(std::array<uint64_t, 25>& st)
{ {
for (int round = 0; round < KeccakParams::ROUNDS; ++round) { for (int round = 0; round < KeccakParams::ROUNDS; ++round) {
uint64_t bc[5]; 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<uint64_t, 25>& st)
{ {
constexpr int rsiz = KeccakParams::HASH_DATA_AREA; constexpr int rsiz = KeccakParams::HASH_DATA_AREA;
constexpr int rsizw = rsiz / 8; 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<uint64_t, 25>& st)
{ {
constexpr int rsiz = KeccakParams::HASH_DATA_AREA; constexpr int rsiz = KeccakParams::HASH_DATA_AREA;
constexpr int rsizw = rsiz / 8; constexpr int rsizw = rsiz / 8;

View file

@ -24,25 +24,25 @@ enum KeccakParams {
ROUNDS = 24, ROUNDS = 24,
}; };
void keccakf(uint64_t (&st)[25]); void keccakf(std::array<uint64_t, 25> &st);
void keccak_step(const uint8_t* &in, int &inlen, uint64_t (&st)[25]); void keccak_step(const uint8_t* &in, int &inlen, std::array<uint64_t, 25>& st);
void keccak_finish(const uint8_t* in, int inlen, uint64_t (&st)[25]); void keccak_finish(const uint8_t* in, int inlen, std::array<uint64_t, 25>& st);
template<size_t N> template<size_t N>
FORCEINLINE void keccak(const uint8_t* in, int inlen, uint8_t (&md)[N]) FORCEINLINE void keccak(const uint8_t* in, int inlen, uint8_t (&md)[N])
{ {
static_assert((N == 32) || (N == 200), "invalid size"); static_assert((N == 32) || (N == 200), "invalid size");
uint64_t st[25] = {}; std::array<uint64_t, 25> st = {};
keccak_step(in, inlen, st); keccak_step(in, inlen, st);
keccak_finish(in, inlen, st); keccak_finish(in, inlen, st);
memcpy(md, st, N); memcpy(md, st.data(), N);
} }
template<typename T> template<typename T>
FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen) FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen)
{ {
uint64_t st[25] = {}; std::array<uint64_t, 25> st = {};
const int rsiz = sizeof(st) == mdlen ? KeccakParams::HASH_DATA_AREA : 200 - 2 * mdlen; const int rsiz = sizeof(st) == mdlen ? KeccakParams::HASH_DATA_AREA : 200 - 2 * mdlen;
const int rsizw = rsiz / 8; const int rsizw = rsiz / 8;
@ -77,7 +77,7 @@ FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen)
keccakf(st); keccakf(st);
memcpy(md, st, mdlen); memcpy(md, st.data(), mdlen);
} }
} // namespace p2pool } // namespace p2pool