Skip to content

Commit

Permalink
Cherry picked commit 'Preserve floating point state when calling rand…
Browse files Browse the repository at this point in the history
…omx_calculate_hash' from tevador/RandomX@6a764e9
  • Loading branch information
codeofalltrades committed Jan 26, 2021
1 parent 6cb9858 commit c6a8be7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/crypto/randomx/randomx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <limits>
#include "utiltime.h"
#include "logging.h"

#include <cfenv>
extern "C" {

randomx_flags randomx_get_flags() {
Expand Down Expand Up @@ -493,6 +493,8 @@ extern "C" {
assert(machine != nullptr);
assert(inputSize == 0 || input != nullptr);
assert(output != nullptr);
fenv_t fpstate;
fegetenv(&fpstate);
alignas(16) uint64_t tempHash[8];
int blakeResult = blake2b(tempHash, sizeof(tempHash), input, inputSize, nullptr, 0);
assert(blakeResult == 0);
Expand All @@ -505,6 +507,7 @@ extern "C" {
}
machine->run(&tempHash);
machine->getFinalResult(output, RANDOMX_HASH_SIZE);
fesetenv(&fpstate);
}

void randomx_calculate_hash_first(randomx_vm* machine, const void* input, size_t inputSize) {
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/randomx/randomx.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ RANDOMX_EXPORT void randomx_calculate_hash(randomx_vm *machine, const void *inpu
* and begin the calculation of the next hash.
* randomx_calculate_hash_last will output the hash value of the previous input.
*
* WARNING: These functions may alter the floating point rounding mode of the calling thread.
*
* @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.
Expand Down
13 changes: 12 additions & 1 deletion src/test/randomx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "crypto/randomx/intrin_portable.h"
#include "crypto/randomx/jit_compiler.hpp"
#include "crypto/randomx/aes_hash.hpp"

#include "crypto/randomx/utility.hpp"
#include <cfenv>

#include <array>

Expand Down Expand Up @@ -1164,6 +1164,10 @@ BOOST_AUTO_TEST_CASE(randomx_run_tests)
assert(cacheMemory[33554431] == 0x1f47f056d05cd99b);
});

if (cache != nullptr)
randomx_release_cache(cache);
cache = randomx_alloc_cache(RANDOMX_FLAG_DEFAULT);

runTest("Hash batch test", RANDOMX_HAVE_COMPILER && stringsEqual(RANDOMX_ARGON_SALT, "RandomX\x03"), []() {
char hash1[RANDOMX_HASH_SIZE];
char hash2[RANDOMX_HASH_SIZE];
Expand All @@ -1183,6 +1187,13 @@ BOOST_AUTO_TEST_CASE(randomx_run_tests)
assert(equalsHex(hash3, "c36d4ed4191e617309867ed66a443be4075014e2b061bcdaf9ce7b721d2b77a8"));
});

runTest("Preserve rounding mode", RANDOMX_FREQ_CFROUND > 0, []() {
fesetround(FE_TONEAREST);
char hash[RANDOMX_HASH_SIZE];
calcStringHash("test key 000", "Lorem ipsum dolor sit amet", &hash);
assert(fegetround() == FE_TONEAREST);
});

randomx_destroy_vm(vm);
vm = nullptr;

Expand Down

0 comments on commit c6a8be7

Please sign in to comment.