randomx.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef RANDOMX_H
  26. #define RANDOMX_H
  27. #include <stddef.h>
  28. #define RANDOMX_HASH_SIZE 32
  29. #define RANDOMX_DATASET_ITEM_SIZE 64
  30. #ifndef RANDOMX_EXPORT
  31. #define RANDOMX_EXPORT
  32. #endif
  33. typedef enum {
  34. RANDOMX_FLAG_DEFAULT = 0,
  35. RANDOMX_FLAG_LARGE_PAGES = 1,
  36. RANDOMX_FLAG_HARD_AES = 2,
  37. RANDOMX_FLAG_FULL_MEM = 4,
  38. RANDOMX_FLAG_JIT = 8,
  39. RANDOMX_FLAG_SECURE = 16,
  40. RANDOMX_FLAG_ARGON2_SSSE3 = 32,
  41. RANDOMX_FLAG_ARGON2_AVX2 = 64,
  42. RANDOMX_FLAG_ARGON2 = 96
  43. } randomx_flags;
  44. typedef struct randomx_dataset randomx_dataset;
  45. typedef struct randomx_cache randomx_cache;
  46. typedef struct randomx_vm randomx_vm;
  47. #if defined(__cplusplus)
  48. #ifdef __cpp_constexpr
  49. #define CONSTEXPR constexpr
  50. #else
  51. #define CONSTEXPR
  52. #endif
  53. inline CONSTEXPR randomx_flags operator |(randomx_flags a, randomx_flags b) {
  54. return static_cast<randomx_flags>(static_cast<int>(a) | static_cast<int>(b));
  55. }
  56. inline CONSTEXPR randomx_flags operator &(randomx_flags a, randomx_flags b) {
  57. return static_cast<randomx_flags>(static_cast<int>(a) & static_cast<int>(b));
  58. }
  59. inline randomx_flags& operator |=(randomx_flags& a, randomx_flags b) {
  60. return a = a | b;
  61. }
  62. extern "C" {
  63. #endif
  64. /**
  65. * @return The recommended flags to be used on the current machine.
  66. * Does not include:
  67. * RANDOMX_FLAG_LARGE_PAGES
  68. * RANDOMX_FLAG_FULL_MEM
  69. * RANDOMX_FLAG_SECURE
  70. * These flags must be added manually if desired.
  71. */
  72. RANDOMX_EXPORT randomx_flags randomx_get_flags(void);
  73. /**
  74. * Creates a randomx_cache structure and allocates memory for RandomX Cache.
  75. *
  76. * @param flags is any combination of these 2 flags (each flag can be set or not set):
  77. * RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
  78. * RANDOMX_FLAG_JIT - create cache structure with JIT compilation support; this makes
  79. * subsequent Dataset initialization faster
  80. * Optionally, one of these two flags may be selected:
  81. * RANDOMX_FLAG_ARGON2_SSSE3 - optimized Argon2 for CPUs with the SSSE3 instruction set
  82. * makes subsequent cache initialization faster
  83. * RANDOMX_FLAG_ARGON2_AVX2 - optimized Argon2 for CPUs with the AVX2 instruction set
  84. * makes subsequent cache initialization faster
  85. *
  86. * @return Pointer to an allocated randomx_cache structure.
  87. * Returns NULL if:
  88. * (1) memory allocation fails
  89. * (2) the RANDOMX_FLAG_JIT is set and JIT compilation is not supported on the current platform
  90. * (3) an invalid or unsupported RANDOMX_FLAG_ARGON2 value is set
  91. */
  92. RANDOMX_EXPORT randomx_cache *randomx_alloc_cache(randomx_flags flags);
  93. /**
  94. * Initializes the cache memory and SuperscalarHash using the provided key value.
  95. * Does nothing if called again with the same key value.
  96. *
  97. * @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL.
  98. * @param key is a pointer to memory which contains the key value. Must not be NULL.
  99. * @param keySize is the number of bytes of the key.
  100. */
  101. RANDOMX_EXPORT void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize);
  102. /**
  103. * Releases all memory occupied by the randomx_cache structure.
  104. *
  105. * @param cache is a pointer to a previously allocated randomx_cache structure.
  106. */
  107. RANDOMX_EXPORT void randomx_release_cache(randomx_cache* cache);
  108. /**
  109. * Creates a randomx_dataset structure and allocates memory for RandomX Dataset.
  110. *
  111. * @param flags is the initialization flags. Only one flag is supported (can be set or not set):
  112. * RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
  113. *
  114. * @return Pointer to an allocated randomx_dataset structure.
  115. * NULL is returned if memory allocation fails.
  116. */
  117. RANDOMX_EXPORT randomx_dataset *randomx_alloc_dataset(randomx_flags flags);
  118. /**
  119. * Gets the number of items contained in the dataset.
  120. *
  121. * @return the number of items contained in the dataset.
  122. */
  123. RANDOMX_EXPORT unsigned long randomx_dataset_item_count(void);
  124. /**
  125. * Initializes dataset items.
  126. *
  127. * Note: In order to use the Dataset, all items from 0 to (randomx_dataset_item_count() - 1) must be initialized.
  128. * This may be done by several calls to this function using non-overlapping item sequences.
  129. *
  130. * @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
  131. * @param cache is a pointer to a previously allocated and initialized randomx_cache structure. Must not be NULL.
  132. * @param startItem is the item number where intialization should start.
  133. * @param itemCount is the number of items that should be initialized.
  134. */
  135. RANDOMX_EXPORT void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount);
  136. /**
  137. * Returns a pointer to the internal memory buffer of the dataset structure. The size
  138. * of the internal memory buffer is randomx_dataset_item_count() * RANDOMX_DATASET_ITEM_SIZE.
  139. *
  140. * @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
  141. *
  142. * @return Pointer to the internal memory buffer of the dataset structure.
  143. */
  144. RANDOMX_EXPORT void *randomx_get_dataset_memory(randomx_dataset *dataset);
  145. /**
  146. * Releases all memory occupied by the randomx_dataset structure.
  147. *
  148. * @param dataset is a pointer to a previously allocated randomx_dataset structure.
  149. */
  150. RANDOMX_EXPORT void randomx_release_dataset(randomx_dataset *dataset);
  151. /**
  152. * Creates and initializes a RandomX virtual machine.
  153. *
  154. * @param flags is any combination of these 5 flags (each flag can be set or not set):
  155. * RANDOMX_FLAG_LARGE_PAGES - allocate scratchpad memory in large pages
  156. * RANDOMX_FLAG_HARD_AES - virtual machine will use hardware accelerated AES
  157. * RANDOMX_FLAG_FULL_MEM - virtual machine will use the full dataset
  158. * RANDOMX_FLAG_JIT - virtual machine will use a JIT compiler
  159. * RANDOMX_FLAG_SECURE - when combined with RANDOMX_FLAG_JIT, the JIT pages are never
  160. * writable and executable at the same time (W^X policy)
  161. * The numeric values of the first 4 flags are ordered so that a higher value will provide
  162. * faster hash calculation and a lower numeric value will provide higher portability.
  163. * Using RANDOMX_FLAG_DEFAULT (all flags not set) works on all platforms, but is the slowest.
  164. * @param cache is a pointer to an initialized randomx_cache structure. Can be
  165. * NULL if RANDOMX_FLAG_FULL_MEM is set.
  166. * @param dataset is a pointer to a randomx_dataset structure. Can be NULL
  167. * if RANDOMX_FLAG_FULL_MEM is not set.
  168. *
  169. * @return Pointer to an initialized randomx_vm structure.
  170. * Returns NULL if:
  171. * (1) Scratchpad memory allocation fails.
  172. * (2) The requested initialization flags are not supported on the current platform.
  173. * (3) cache parameter is NULL and RANDOMX_FLAG_FULL_MEM is not set
  174. * (4) dataset parameter is NULL and RANDOMX_FLAG_FULL_MEM is set
  175. */
  176. RANDOMX_EXPORT randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset);
  177. /**
  178. * Reinitializes a virtual machine with a new Cache. This function should be called anytime
  179. * the Cache is reinitialized with a new key. Does nothing if called with a Cache containing
  180. * the same key value as already set.
  181. *
  182. * @param machine is a pointer to a randomx_vm structure that was initialized
  183. * without RANDOMX_FLAG_FULL_MEM. Must not be NULL.
  184. * @param cache is a pointer to an initialized randomx_cache structure. Must not be NULL.
  185. */
  186. RANDOMX_EXPORT void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache);
  187. /**
  188. * Reinitializes a virtual machine with a new Dataset.
  189. *
  190. * @param machine is a pointer to a randomx_vm structure that was initialized
  191. * with RANDOMX_FLAG_FULL_MEM. Must not be NULL.
  192. * @param dataset is a pointer to an initialized randomx_dataset structure. Must not be NULL.
  193. */
  194. RANDOMX_EXPORT void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset);
  195. /**
  196. * Releases all memory occupied by the randomx_vm structure.
  197. *
  198. * @param machine is a pointer to a previously created randomx_vm structure.
  199. */
  200. RANDOMX_EXPORT void randomx_destroy_vm(randomx_vm *machine);
  201. /**
  202. * Calculates a RandomX hash value.
  203. *
  204. * @param machine is a pointer to a randomx_vm structure. Must not be NULL.
  205. * @param input is a pointer to memory to be hashed. Must not be NULL.
  206. * @param inputSize is the number of bytes to be hashed.
  207. * @param output is a pointer to memory where the hash will be stored. Must not
  208. * be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing.
  209. */
  210. RANDOMX_EXPORT void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output);
  211. #if defined(__cplusplus)
  212. }
  213. #endif
  214. #endif