2019-04-20 09:08:01 +00:00
|
|
|
/*
|
2019-05-18 12:21:47 +00:00
|
|
|
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the copyright holder nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2019-04-20 09:08:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RANDOMX_H
|
|
|
|
#define RANDOMX_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#define RANDOMX_HASH_SIZE 32
|
2019-04-23 20:15:55 +00:00
|
|
|
#define RANDOMX_DATASET_ITEM_SIZE 64
|
2019-04-20 09:08:01 +00:00
|
|
|
|
2019-06-10 13:58:51 +00:00
|
|
|
#ifndef RANDOMX_EXPORT
|
|
|
|
#define RANDOMX_EXPORT
|
|
|
|
#endif
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
typedef enum {
|
|
|
|
RANDOMX_FLAG_DEFAULT = 0,
|
2019-04-21 21:23:13 +00:00
|
|
|
RANDOMX_FLAG_LARGE_PAGES = 1,
|
|
|
|
RANDOMX_FLAG_HARD_AES = 2,
|
|
|
|
RANDOMX_FLAG_FULL_MEM = 4,
|
|
|
|
RANDOMX_FLAG_JIT = 8,
|
2019-08-25 11:47:40 +00:00
|
|
|
RANDOMX_FLAG_SECURE = 16
|
2019-04-20 09:08:01 +00:00
|
|
|
} randomx_flags;
|
|
|
|
|
|
|
|
typedef struct randomx_dataset randomx_dataset;
|
|
|
|
typedef struct randomx_cache randomx_cache;
|
|
|
|
typedef struct randomx_vm randomx_vm;
|
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
/**
|
|
|
|
* Creates a randomx_cache structure and allocates memory for RandomX Cache.
|
|
|
|
*
|
|
|
|
* @param flags is 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
|
|
|
|
*
|
|
|
|
* @return Pointer to an allocated randomx_cache structure.
|
2019-04-22 16:20:46 +00:00
|
|
|
* NULL is returned if memory allocation fails or if the RANDOMX_FLAG_JIT
|
|
|
|
* is set and JIT compilation is not supported on the current platform.
|
2019-04-22 13:13:41 +00:00
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT randomx_cache *randomx_alloc_cache(randomx_flags flags);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-01 12:40:13 +00:00
|
|
|
* Initializes the cache memory and SuperscalarHash using the provided key value.
|
2019-04-22 13:13:41 +00:00
|
|
|
*
|
|
|
|
* @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL.
|
2019-05-01 12:40:13 +00:00
|
|
|
* @param key is a pointer to memory which contains the key value. Must not be NULL.
|
|
|
|
* @param keySize is the number of bytes of the key.
|
2019-04-22 13:13:41 +00:00
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases all memory occupied by the randomx_cache structure.
|
|
|
|
*
|
|
|
|
* @param cache is a pointer to a previously allocated randomx_cache structure.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_release_cache(randomx_cache* cache);
|
2019-04-20 09:08:01 +00:00
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
/**
|
|
|
|
* Creates a randomx_dataset structure and allocates memory for RandomX Dataset.
|
|
|
|
*
|
|
|
|
* @param flags is the initialization flags. Only one flag is supported (can be set or not set):
|
|
|
|
* RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
|
2019-04-22 16:20:46 +00:00
|
|
|
*
|
2019-04-22 17:08:45 +00:00
|
|
|
* @return Pointer to an allocated randomx_dataset structure.
|
2019-04-22 16:20:46 +00:00
|
|
|
* NULL is returned if memory allocation fails.
|
2019-04-22 13:13:41 +00:00
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT randomx_dataset *randomx_alloc_dataset(randomx_flags flags);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
2019-04-23 20:15:55 +00:00
|
|
|
/**
|
|
|
|
* Gets the number of items contained in the dataset.
|
|
|
|
*
|
|
|
|
* @return the number of items contained in the dataset.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT unsigned long randomx_dataset_item_count(void);
|
2019-04-23 20:15:55 +00:00
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
/**
|
|
|
|
* Initializes dataset items.
|
|
|
|
*
|
2019-04-23 20:15:55 +00:00
|
|
|
* Note: In order to use the Dataset, all items from 0 to (randomx_dataset_item_count() - 1) must be initialized.
|
2019-04-22 13:13:41 +00:00
|
|
|
* This may be done by several calls to this function using non-overlapping item sequences.
|
|
|
|
*
|
|
|
|
* @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
|
|
|
|
* @param cache is a pointer to a previously allocated and initialized randomx_cache structure. Must not be NULL.
|
|
|
|
* @param startItem is the item number where intialization should start.
|
|
|
|
* @param itemCount is the number of items that should be initialized.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
2019-04-23 20:15:55 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2019-09-22 20:18:28 +00:00
|
|
|
* @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
|
2019-04-23 20:15:55 +00:00
|
|
|
*
|
|
|
|
* @return Pointer to the internal memory buffer of the dataset structure.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void *randomx_get_dataset_memory(randomx_dataset *dataset);
|
2019-04-23 20:15:55 +00:00
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
/**
|
|
|
|
* Releases all memory occupied by the randomx_dataset structure.
|
|
|
|
*
|
|
|
|
* @param dataset is a pointer to a previously allocated randomx_dataset structure.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_release_dataset(randomx_dataset *dataset);
|
2019-04-20 09:08:01 +00:00
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
/**
|
|
|
|
* Creates and initializes a RandomX virtual machine.
|
|
|
|
*
|
2019-08-25 11:47:40 +00:00
|
|
|
* @param flags is any combination of these 5 flags (each flag can be set or not set):
|
2019-04-22 13:13:41 +00:00
|
|
|
* 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
|
2019-08-25 11:47:40 +00:00
|
|
|
* 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
|
2019-04-22 13:13:41 +00:00
|
|
|
* 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.
|
|
|
|
* @param cache is a pointer to an initialized randomx_cache structure. Can be
|
|
|
|
* NULL if RANDOMX_FLAG_FULL_MEM is set.
|
|
|
|
* @param dataset is a pointer to a randomx_dataset structure. Can be NULL
|
|
|
|
* if RANDOMX_FLAG_FULL_MEM is not set.
|
|
|
|
*
|
|
|
|
* @return 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
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reinitializes a virtual machine with a new Cache. This function should be called anytime
|
2019-05-01 12:40:13 +00:00
|
|
|
* the Cache is reinitialized with a new key.
|
2019-04-22 13:13:41 +00:00
|
|
|
*
|
|
|
|
* @param machine is a pointer to a randomx_vm structure that was initialized
|
|
|
|
* without RANDOMX_FLAG_FULL_MEM. Must not be NULL.
|
|
|
|
* @param cache is a pointer to an initialized randomx_cache structure. Must not be NULL.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reinitializes a virtual machine with a new Dataset.
|
|
|
|
*
|
|
|
|
* @param machine is a pointer to a randomx_vm structure that was initialized
|
|
|
|
* with RANDOMX_FLAG_FULL_MEM. Must not be NULL.
|
|
|
|
* @param dataset is a pointer to an initialized randomx_dataset structure. Must not be NULL.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset);
|
2019-04-22 13:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases all memory occupied by the randomx_vm structure.
|
|
|
|
*
|
|
|
|
* @param machine is a pointer to a previously created randomx_vm structure.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_destroy_vm(randomx_vm *machine);
|
2019-04-20 09:08:01 +00:00
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
/**
|
|
|
|
* Calculates a RandomX hash value.
|
|
|
|
*
|
|
|
|
* @param machine is a pointer to a randomx_vm structure. Must not be NULL.
|
|
|
|
* @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 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.
|
|
|
|
*/
|
2019-06-10 13:58:51 +00:00
|
|
|
RANDOMX_EXPORT void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output);
|
2019-04-20 09:08:01 +00:00
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|