Monero
Loading...
Searching...
No Matches
randomx.cpp File Reference
#include "randomx.h"
#include "dataset.hpp"
#include "vm_interpreted.hpp"
#include "vm_interpreted_light.hpp"
#include "vm_compiled.hpp"
#include "vm_compiled_light.hpp"
#include "blake2/blake2.h"
#include "cpu.hpp"
#include <cassert>
#include <limits>
#include <cfenv>
Include dependency graph for randomx.cpp:

Functions

randomx_flags randomx_get_flags ()
randomx_cacherandomx_alloc_cache (randomx_flags flags)
void randomx_init_cache (randomx_cache *cache, const void *key, size_t keySize)
void randomx_release_cache (randomx_cache *cache)
randomx_datasetrandomx_alloc_dataset (randomx_flags flags)
unsigned long randomx_dataset_item_count ()
void randomx_init_dataset (randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount)
void * randomx_get_dataset_memory (randomx_dataset *dataset)
void randomx_release_dataset (randomx_dataset *dataset)
randomx_vmrandomx_create_vm (randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset)
void randomx_vm_set_cache (randomx_vm *machine, randomx_cache *cache)
void randomx_vm_set_dataset (randomx_vm *machine, randomx_dataset *dataset)
void randomx_destroy_vm (randomx_vm *machine)
void randomx_calculate_hash (randomx_vm *machine, const void *input, size_t inputSize, void *output)
void randomx_calculate_hash_first (randomx_vm *machine, const void *input, size_t inputSize)
void randomx_calculate_hash_next (randomx_vm *machine, const void *nextInput, size_t nextInputSize, void *output)
void randomx_calculate_hash_last (randomx_vm *machine, void *output)
void randomx_calculate_commitment (const void *input, size_t inputSize, const void *hash_in, void *com_out)

Variables

constexpr unsigned long DatasetItemCount = randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE

Function Documentation

◆ randomx_alloc_cache()

randomx_cache * randomx_alloc_cache ( randomx_flags flags)

Creates a randomx_cache structure and allocates memory for RandomX Cache.

Parameters
flagsis any combination of these 2 flags (each flag can be set or not set): RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages RANDOMX_FLAG_JIT - create cache structure with JIT compilation support; this makes subsequent Dataset initialization faster Optionally, one of these two flags may be selected: RANDOMX_FLAG_ARGON2_SSSE3 - optimized Argon2 for CPUs with the SSSE3 instruction set makes subsequent cache initialization faster RANDOMX_FLAG_ARGON2_AVX2 - optimized Argon2 for CPUs with the AVX2 instruction set makes subsequent cache initialization faster
Returns
Pointer to an allocated randomx_cache structure. Returns NULL if: (1) memory allocation fails (2) the RANDOMX_FLAG_JIT is set and JIT compilation is not supported on the current platform (3) an invalid or unsupported RANDOMX_FLAG_ARGON2 value is set

◆ randomx_alloc_dataset()

randomx_dataset * randomx_alloc_dataset ( randomx_flags flags)

Creates a randomx_dataset structure and allocates memory for RandomX Dataset.

Parameters
flagsis the initialization flags. Only one flag is supported (can be set or not set): RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
Returns
Pointer to an allocated randomx_dataset structure. NULL is returned if memory allocation fails.

◆ randomx_calculate_commitment()

void randomx_calculate_commitment ( const void * input,
size_t inputSize,
const void * hash_in,
void * com_out )

Calculate a RandomX commitment from a RandomX hash and its input.

Parameters
inputis a pointer to memory that was hashed. Must not be NULL.
inputSizeis the number of bytes in the input.
hash_inis the output from randomx_calculate_hash* (RANDOMX_HASH_SIZE bytes).
com_outis a pointer to memory where the commitment will be stored. Must not be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing.

◆ randomx_calculate_hash()

void randomx_calculate_hash ( randomx_vm * machine,
const void * input,
size_t inputSize,
void * output )

Calculates a RandomX hash value.

Parameters
machineis a pointer to a randomx_vm structure. Must not be NULL.
inputis a pointer to memory to be hashed. Must not be NULL.
inputSizeis the number of bytes to be hashed.
outputis 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_calculate_hash_first()

void randomx_calculate_hash_first ( randomx_vm * machine,
const void * input,
size_t inputSize )

Set of functions used to calculate multiple RandomX hashes more efficiently. randomx_calculate_hash_first will begin a hash calculation. randomx_calculate_hash_next will output the hash value of the previous input 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.

Parameters
machineis a pointer to a randomx_vm structure. Must not be NULL.
inputis a pointer to memory to be hashed. Must not be NULL.
inputSizeis the number of bytes to be hashed.
nextInputis a pointer to memory to be hashed for the next hash. Must not be NULL.
nextInputSizeis the number of bytes to be hashed for the next hash.
outputis 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_calculate_hash_last()

void randomx_calculate_hash_last ( randomx_vm * machine,
void * output )

◆ randomx_calculate_hash_next()

void randomx_calculate_hash_next ( randomx_vm * machine,
const void * nextInput,
size_t nextInputSize,
void * output )

◆ randomx_create_vm()

randomx_vm * randomx_create_vm ( randomx_flags flags,
randomx_cache * cache,
randomx_dataset * dataset )

Creates and initializes a RandomX virtual machine.

Parameters
flagsis any combination of these 5 flags (each flag can be set or not set): RANDOMX_FLAG_LARGE_PAGES - allocate scratchpad memory in large pages RANDOMX_FLAG_HARD_AES - virtual machine will use hardware accelerated AES RANDOMX_FLAG_FULL_MEM - virtual machine will use the full dataset RANDOMX_FLAG_JIT - virtual machine will use a JIT compiler RANDOMX_FLAG_SECURE - when combined with RANDOMX_FLAG_JIT, the JIT pages are never writable and executable at the same time (W^X policy) The numeric values of the first 4 flags are ordered so that a higher value will provide faster hash calculation and a lower numeric value will provide higher portability. Using RANDOMX_FLAG_DEFAULT (all flags not set) works on all platforms, but is the slowest.
cacheis a pointer to an initialized randomx_cache structure. Can be NULL if RANDOMX_FLAG_FULL_MEM is set.
datasetis a pointer to a randomx_dataset structure. Can be NULL if RANDOMX_FLAG_FULL_MEM is not set.
Returns
Pointer to an initialized randomx_vm structure. Returns NULL if: (1) Scratchpad memory allocation fails. (2) The requested initialization flags are not supported on the current platform. (3) cache parameter is NULL and RANDOMX_FLAG_FULL_MEM is not set (4) dataset parameter is NULL and RANDOMX_FLAG_FULL_MEM is set

◆ randomx_dataset_item_count()

unsigned long randomx_dataset_item_count ( void )

Gets the number of items contained in the dataset.

Returns
the number of items contained in the dataset.

◆ randomx_destroy_vm()

void randomx_destroy_vm ( randomx_vm * machine)

Releases all memory occupied by the randomx_vm structure.

Parameters
machineis a pointer to a previously created randomx_vm structure.

◆ randomx_get_dataset_memory()

void * randomx_get_dataset_memory ( randomx_dataset * dataset)

Returns a pointer to the internal memory buffer of the dataset structure. The size of the internal memory buffer is randomx_dataset_item_count() * RANDOMX_DATASET_ITEM_SIZE.

Parameters
datasetis a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
Returns
Pointer to the internal memory buffer of the dataset structure.

◆ randomx_get_flags()

randomx_flags randomx_get_flags ( void )
Returns
The recommended flags to be used on the current machine. Does not include: RANDOMX_FLAG_LARGE_PAGES RANDOMX_FLAG_FULL_MEM RANDOMX_FLAG_SECURE These flags must be added manually if desired. On OpenBSD RANDOMX_FLAG_SECURE is enabled by default in JIT mode as W^X is enforced by the OS.

◆ randomx_init_cache()

void randomx_init_cache ( randomx_cache * cache,
const void * key,
size_t keySize )

Initializes the cache memory and SuperscalarHash using the provided key value. Does nothing if called again with the same key value.

Parameters
cacheis a pointer to a previously allocated randomx_cache structure. Must not be NULL.
keyis a pointer to memory which contains the key value. Must not be NULL.
keySizeis the number of bytes of the key.

◆ randomx_init_dataset()

void randomx_init_dataset ( randomx_dataset * dataset,
randomx_cache * cache,
unsigned long startItem,
unsigned long itemCount )

Initializes dataset items.

Note: In order to use the Dataset, all items from 0 to (randomx_dataset_item_count() - 1) must be initialized. This may be done by several calls to this function using non-overlapping item sequences.

Parameters
datasetis a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
cacheis a pointer to a previously allocated and initialized randomx_cache structure. Must not be NULL.
startItemis the item number where intialization should start.
itemCountis the number of items that should be initialized.

◆ randomx_release_cache()

void randomx_release_cache ( randomx_cache * cache)

Releases all memory occupied by the randomx_cache structure.

Parameters
cacheis a pointer to a previously allocated randomx_cache structure.

◆ randomx_release_dataset()

void randomx_release_dataset ( randomx_dataset * dataset)

Releases all memory occupied by the randomx_dataset structure.

Parameters
datasetis a pointer to a previously allocated randomx_dataset structure.

◆ randomx_vm_set_cache()

void randomx_vm_set_cache ( randomx_vm * machine,
randomx_cache * cache )

Reinitializes a virtual machine with a new Cache. This function should be called anytime the Cache is reinitialized with a new key. Does nothing if called with a Cache containing the same key value as already set.

Parameters
machineis a pointer to a randomx_vm structure that was initialized without RANDOMX_FLAG_FULL_MEM. Must not be NULL.
cacheis a pointer to an initialized randomx_cache structure. Must not be NULL.

◆ randomx_vm_set_dataset()

void randomx_vm_set_dataset ( randomx_vm * machine,
randomx_dataset * dataset )

Reinitializes a virtual machine with a new Dataset.

Parameters
machineis a pointer to a randomx_vm structure that was initialized with RANDOMX_FLAG_FULL_MEM. Must not be NULL.
datasetis a pointer to an initialized randomx_dataset structure. Must not be NULL.

Variable Documentation

◆ DatasetItemCount

unsigned long DatasetItemCount = randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE
constexpr