From 3910d49b498fe974c971771ec0a48c0ef44e9196 Mon Sep 17 00:00:00 2001 From: tevador Date: Sun, 1 Dec 2019 18:19:09 +0100 Subject: [PATCH] Hide tempHash from the public API --- src/randomx.cpp | 18 +++++++++--------- src/randomx.h | 9 +++++---- src/tests/benchmark.cpp | 6 ++---- src/virtual_machine.hpp | 1 + 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/randomx.cpp b/src/randomx.cpp index c4281b3..898425c 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -363,21 +363,21 @@ extern "C" { machine->getFinalResult(output, RANDOMX_HASH_SIZE); } - void randomx_calculate_hash_first(randomx_vm* machine, uint64_t *tempHash, const void* input, size_t inputSize) { - blake2b(tempHash, sizeof(uint64_t) * 8, input, inputSize, nullptr, 0); - machine->initScratchpad(tempHash); + void randomx_calculate_hash_first(randomx_vm* machine, const void* input, size_t inputSize) { + blake2b(machine->tempHash, sizeof(machine->tempHash), input, inputSize, nullptr, 0); + machine->initScratchpad(machine->tempHash); } - void randomx_calculate_hash_next(randomx_vm* machine, uint64_t *tempHash, const void* nextInput, size_t nextInputSize, void* output) { + void randomx_calculate_hash_next(randomx_vm* machine, const void* nextInput, size_t nextInputSize, void* output) { machine->resetRoundingMode(); for (uint32_t chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) { - machine->run(tempHash); - blake2b(tempHash, sizeof(uint64_t) * 8, machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0); + machine->run(machine->tempHash); + blake2b(machine->tempHash, sizeof(machine->tempHash), machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0); } - machine->run(tempHash); + machine->run(machine->tempHash); // Finish current hash and fill the scratchpad for the next hash at the same time - blake2b(tempHash, sizeof(uint64_t) * 8, nextInput, nextInputSize, nullptr, 0); - machine->hashAndFill(output, RANDOMX_HASH_SIZE, tempHash); + blake2b(machine->tempHash, sizeof(machine->tempHash), nextInput, nextInputSize, nullptr, 0); + machine->hashAndFill(output, RANDOMX_HASH_SIZE, machine->tempHash); } } diff --git a/src/randomx.h b/src/randomx.h index 4a7fcbf..d582cf0 100644 --- a/src/randomx.h +++ b/src/randomx.h @@ -240,10 +240,11 @@ RANDOMX_EXPORT void randomx_destroy_vm(randomx_vm *machine); RANDOMX_EXPORT void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output); /** - * Paired functions used to calculate multiple RandomX hashes during mining for example. + * Paired functions used to calculate multiple RandomX hashes more efficiently. + * randomx_calculate_hash_first is called for the first input value. + * randomx_calculate_hash_next will output the hash value of the previous input. * * @param machine is a pointer to a randomx_vm structure. Must not be NULL. - * @param tempHash an array of 8 64-bit values used to store intermediate data between calls to randomx_calculate_hash_first and randomx_calculate_hash_next. * @param input is a pointer to memory to be hashed. Must not be NULL. * @param inputSize is the number of bytes to be hashed. * @param nextInput is a pointer to memory to be hashed for the next hash. Must not be NULL. @@ -251,8 +252,8 @@ RANDOMX_EXPORT void randomx_calculate_hash(randomx_vm *machine, const void *inpu * @param output is a pointer to memory where the hash will be stored. Must not * be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing. */ -RANDOMX_EXPORT void randomx_calculate_hash_first(randomx_vm* machine, uint64_t *tempHash, const void* input, size_t inputSize); -RANDOMX_EXPORT void randomx_calculate_hash_next(randomx_vm* machine, uint64_t *tempHash, const void* nextInput, size_t nextInputSize, void* output); +RANDOMX_EXPORT void randomx_calculate_hash_first(randomx_vm* machine, const void* input, size_t inputSize); +RANDOMX_EXPORT void randomx_calculate_hash_next(randomx_vm* machine, const void* nextInput, size_t nextInputSize, void* output); #if defined(__cplusplus) } diff --git a/src/tests/benchmark.cpp b/src/tests/benchmark.cpp index 868dd28..ed450e1 100644 --- a/src/tests/benchmark.cpp +++ b/src/tests/benchmark.cpp @@ -122,15 +122,13 @@ void mine(randomx_vm* vm, std::atomic& atomicNonce, AtomicHash& result void* noncePtr = blockTemplate + 39; auto nonce = atomicNonce.fetch_add(1); - uint64_t tempHash[8]; - store32(noncePtr, nonce); - randomx_calculate_hash_first(vm, tempHash, blockTemplate, sizeof(blockTemplate)); + randomx_calculate_hash_first(vm, blockTemplate, sizeof(blockTemplate)); while (nonce < noncesCount) { nonce = atomicNonce.fetch_add(1); store32(noncePtr, nonce); - randomx_calculate_hash_next(vm, tempHash, blockTemplate, sizeof(blockTemplate), &hash); + randomx_calculate_hash_next(vm, blockTemplate, sizeof(blockTemplate), &hash); result.xorWith(hash); } } diff --git a/src/virtual_machine.hpp b/src/virtual_machine.hpp index 4e89366..d72a918 100644 --- a/src/virtual_machine.hpp +++ b/src/virtual_machine.hpp @@ -68,6 +68,7 @@ protected: uint64_t datasetOffset; public: std::string cacheKey; + alignas(16) uint64_t tempHash[8]; //8 64-bit values used to store intermediate data }; namespace randomx {