Refactored keccak interface

merge-mining
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_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<uint64_t, 25> 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<uint64_t, 25> 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

View File

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

View File

@ -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<uint64_t, 25>& 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<uint64_t, 25>& 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<uint64_t, 25>& st)
{
constexpr int rsiz = KeccakParams::HASH_DATA_AREA;
constexpr int rsizw = rsiz / 8;

View File

@ -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<uint64_t, 25> &st);
void keccak_step(const uint8_t* &in, int &inlen, std::array<uint64_t, 25>& st);
void keccak_finish(const uint8_t* in, int inlen, std::array<uint64_t, 25>& st);
template<size_t N>
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<uint64_t, 25> st = {};
keccak_step(in, inlen, st);
keccak_finish(in, inlen, st);
memcpy(md, st, N);
memcpy(md, st.data(), N);
}
template<typename T>
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 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