Skip to content

Commit

Permalink
Implement randomx with preset blake hash
Browse files Browse the repository at this point in the history
  • Loading branch information
shizzard committed Dec 12, 2024
1 parent 81961b0 commit 2cc6291
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/randomx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/randomx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 2cc6291

Please sign in to comment.