diff --git a/src/randomx.cpp b/src/randomx.cpp index f34a5de..eb7dde9 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -431,6 +431,36 @@ extern "C" { return (const unsigned char*)machine->getScratchpad(); } + void randomx_calculate_hash_with_scratchpad_with_presets(randomx_vm *machine, unsigned char *inHash, const int randomxProgramCount) { + + assert(machine != nullptr); + +#ifdef USE_CSR_INTRINSICS + const unsigned int fpstate = _mm_getcsr(); +#else + fenv_t fpstate; + fegetenv(&fpstate); +#endif + + int blakeResult; + for (int chain = 0; chain < randomxProgramCount; ++chain) { + machine->run(inHash); + blakeResult = randomx_blake2b(inHash, sizeof(inHash), machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0); + assert(blakeResult == 0); + } + +#ifdef USE_CSR_INTRINSICS + _mm_setcsr(fpstate); +#else + fesetenv(&fpstate); +#endif + } + + unsigned char *randomx_get_scratchpad(randomx_vm *machine) { + assert(machine != nullptr); + return (unsigned char*)machine->getScratchpad(); + } + 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); diff --git a/src/randomx.h b/src/randomx.h index df2e7df..cf5e63c 100644 --- a/src/randomx.h +++ b/src/randomx.h @@ -190,6 +190,14 @@ RANDOMX_EXPORT void randomx_release_dataset(randomx_dataset *dataset); * @return the size of the scratchpad in bytes. */ RANDOMX_EXPORT unsigned long randomx_get_scratchpad_size(void); +/** + * Returns a pointer to the scratchpad memory of the virtual machine. + * + * @param machine is a pointer to a randomx_vm structure. Must not be NULL. + * + * @return Pointer to the scratchpad memory of the virtual machine. +*/ +RANDOMX_EXPORT unsigned char *randomx_get_scratchpad(randomx_vm *machine); /** * Creates and initializes a RandomX virtual machine. @@ -256,6 +264,7 @@ 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); + /** * Calculates a RandomX long hash value. * Long hash is effectively the vm scratchpad taken after finalizing the regular hash. @@ -267,6 +276,16 @@ RANDOMX_EXPORT void randomx_calculate_hash(randomx_vm *machine, const void *inpu */ RANDOMX_EXPORT const unsigned char *randomx_calculate_hash_scratchpad(randomx_vm *machine, const void *input, size_t inputSize, const int randomxProgramCount); +/** + * Calculates a RandomX long hash value. + * Long hash is effectively the vm scratchpad taken after finalizing the regular hash. + * + * @param machine is a pointer to a randomx_vm structure. Must not be NULL. + * @param inHash is a pointer to the initial hash value. Must not be NULL. + * @param randomxProgramCount is the number of RandomX iterations. +*/ +RANDOMX_EXPORT void randomx_calculate_hash_with_scratchpad_with_presets(randomx_vm *machine, unsigned char *inHash, const int randomxProgramCount); + /** * Set of functions used to calculate multiple RandomX hashes more efficiently. * randomx_calculate_hash_first will begin a hash calculation.