This commit is contained in:
SChernykh 2021-06-16 11:47:17 +02:00
parent 3003c067d3
commit 29f2dd4b9e
4 changed files with 22 additions and 27 deletions

View file

@ -126,13 +126,11 @@ bool CBlockTemplate::Init(const String& blockTemplate)
void CBlockTemplate::CalculateMinerTxHash(uint8_t* hash)
{
uint8_t hashes[HASH_SIZE * 3];
uint8_t md[200];
// Calculate 3 partial hashes
// 1. Prefix
keccak(raw_blob.data() + miner_tx_prefix_begin_index, miner_tx_prefix_end_index - miner_tx_prefix_begin_index, md);
memcpy(hashes, md, HASH_SIZE);
keccak(raw_blob.data() + miner_tx_prefix_begin_index, miner_tx_prefix_end_index - miner_tx_prefix_begin_index, hashes, HASH_SIZE);
// 2. Base RCT, single 0 byte in miner tx
static const uint8_t known_second_hash[HASH_SIZE] = {
@ -144,23 +142,18 @@ void CBlockTemplate::CalculateMinerTxHash(uint8_t* hash)
memset(hashes + HASH_SIZE * 2, 0, HASH_SIZE);
// Calculate miner transaction hash
keccak(hashes, sizeof(hashes), md);
memcpy(hash, md, HASH_SIZE);
keccak(hashes, sizeof(hashes), hash, HASH_SIZE);
}
void CBlockTemplate::CalculateMerkleTreeHash(const uint8_t* hashes, size_t count, uint8_t* root_hash)
{
uint8_t md[200];
if (count == 1) {
memcpy(root_hash, hashes, HASH_SIZE);
}
else if (count == 2) {
keccak(hashes, HASH_SIZE * 2, md);
memcpy(root_hash, md, HASH_SIZE);
keccak(hashes, HASH_SIZE * 2, root_hash, HASH_SIZE);
}
else {
size_t i, j;
@ -172,20 +165,17 @@ void CBlockTemplate::CalculateMerkleTreeHash(const uint8_t* hashes, size_t count
memcpy(ints.data(), hashes, (cnt * 2 - count) * HASH_SIZE);
for (i = cnt * 2 - count, j = cnt * 2 - count; j < cnt; i += 2, ++j) {
keccak(hashes + i * HASH_SIZE, HASH_SIZE * 2, md);
memcpy(ints.data() + j * HASH_SIZE, md, HASH_SIZE);
keccak(hashes + i * HASH_SIZE, HASH_SIZE * 2, ints.data() + j * HASH_SIZE, HASH_SIZE);
}
while (cnt > 2) {
cnt >>= 1;
for (i = 0, j = 0; j < cnt; i += 2, ++j) {
keccak(ints.data() + i * HASH_SIZE, HASH_SIZE * 2, md);
memcpy(ints.data() + j * HASH_SIZE, md, HASH_SIZE);
keccak(ints.data() + i * HASH_SIZE, HASH_SIZE * 2, ints.data() + j * HASH_SIZE, HASH_SIZE);
}
}
keccak(ints.data(), HASH_SIZE * 2, md);
memcpy(root_hash, md, HASH_SIZE);
keccak(ints.data(), HASH_SIZE * 2, root_hash, HASH_SIZE);
}
}

View file

@ -21,7 +21,13 @@
#include "base/crypto/keccak.h"
#include "base/tools/cryptonote/Signatures.h"
extern "C" {
#include "base/tools/cryptonote/crypto-ops.h"
}
#include "base/tools/Cvt.h"
@ -73,9 +79,7 @@ static void random_scalar(ec_scalar& res)
static void hash_to_scalar(const void* data, size_t length, ec_scalar& res)
{
uint8_t md[200];
xmrig::keccak((const char*) data, length, md);
memcpy(&res, md, sizeof(res));
xmrig::keccak((const uint8_t*) data, length, (uint8_t*) &res, sizeof(res));
sc_reduce32((uint8_t*) &res);
}
@ -193,4 +197,11 @@ void derive_secret_key(const uint8_t* derivation, size_t output_index, const uin
}
void derive_view_secret_key(const uint8_t* spend_secret_key, uint8_t* view_secret_key)
{
keccak(spend_secret_key, 32, view_secret_key, 32);
sc_reduce32(view_secret_key);
}
} /* namespace xmrig */

View file

@ -34,6 +34,8 @@ bool check_signature(const uint8_t* prefix_hash, const uint8_t* pub, const uint8
bool generate_key_derivation(const uint8_t* key1, const uint8_t* key2, uint8_t* derivation);
void derive_secret_key(const uint8_t* derivation, size_t output_index, const uint8_t* base, uint8_t* derived_key);
void derive_view_secret_key(const uint8_t* spend_secret_key, uint8_t* view_secret_key);
} /* namespace xmrig */

View file

@ -30,10 +30,6 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* From fe.h */
typedef int32_t fe[10];
@ -167,7 +163,3 @@ void fe_tobytes(unsigned char *, const fe);
void fe_invert(fe out, const fe z);
int ge_p3_is_point_at_infinity(const ge_p3 *p);
#ifdef __cplusplus
}
#endif