randomx.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. typedef enum {
  31. RANDOMX_FLAG_DEFAULT = 0,
  32. RANDOMX_FLAG_LARGE_PAGES = 1,
  33. RANDOMX_FLAG_HARD_AES = 2,
  34. RANDOMX_FLAG_FULL_MEM = 4,
  35. RANDOMX_FLAG_JIT = 8,
  36. } randomx_flags;
  37. typedef struct randomx_dataset randomx_dataset;
  38. typedef struct randomx_cache randomx_cache;
  39. typedef struct randomx_vm randomx_vm;
  40. #if defined(__cplusplus)
  41. extern "C" {
  42. #endif
  43. /**
  44. * Creates a randomx_cache structure and allocates memory for RandomX Cache.
  45. *
  46. * @param flags is any combination of these 2 flags (each flag can be set or not set):
  47. * RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
  48. * RANDOMX_FLAG_JIT - create cache structure with JIT compilation support; this makes
  49. * subsequent Dataset initialization faster
  50. *
  51. * @return Pointer to an allocated randomx_cache structure.
  52. * NULL is returned if memory allocation fails or if the RANDOMX_FLAG_JIT
  53. * is set and JIT compilation is not supported on the current platform.
  54. */
  55. randomx_cache *randomx_alloc_cache(randomx_flags flags);
  56. /**
  57. * Initializes the cache memory and SuperscalarHash using the provided key value.
  58. *
  59. * @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL.
  60. * @param key is a pointer to memory which contains the key value. Must not be NULL.
  61. * @param keySize is the number of bytes of the key.
  62. */
  63. void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize);
  64. /**
  65. * Releases all memory occupied by the randomx_cache structure.
  66. *
  67. * @param cache is a pointer to a previously allocated randomx_cache structure.
  68. */
  69. void randomx_release_cache(randomx_cache* cache);
  70. /**
  71. * Creates a randomx_dataset structure and allocates memory for RandomX Dataset.
  72. *
  73. * @param flags is the initialization flags. Only one flag is supported (can be set or not set):
  74. * RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
  75. *
  76. * @return Pointer to an allocated randomx_dataset structure.
  77. * NULL is returned if memory allocation fails.
  78. */
  79. randomx_dataset *randomx_alloc_dataset(randomx_flags flags);
  80. /**
  81. * Gets the number of items contained in the dataset.
  82. *
  83. * @return the number of items contained in the dataset.
  84. */
  85. unsigned long randomx_dataset_item_count(void);
  86. /**
  87. * Initializes dataset items.
  88. *
  89. * Note: In order to use the Dataset, all items from 0 to (randomx_dataset_item_count() - 1) must be initialized.
  90. * This may be done by several calls to this function using non-overlapping item sequences.
  91. *
  92. * @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
  93. * @param cache is a pointer to a previously allocated and initialized randomx_cache structure. Must not be NULL.
  94. * @param startItem is the item number where intialization should start.
  95. * @param itemCount is the number of items that should be initialized.
  96. */
  97. void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount);
  98. /**
  99. * Returns a pointer to the internal memory buffer of the dataset structure. The size
  100. * of the internal memory buffer is randomx_dataset_item_count() * RANDOMX_DATASET_ITEM_SIZE.
  101. *
  102. * @param dataset is dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
  103. *
  104. * @return Pointer to the internal memory buffer of the dataset structure.
  105. */
  106. void *randomx_get_dataset_memory(randomx_dataset *dataset);
  107. /**
  108. * Releases all memory occupied by the randomx_dataset structure.
  109. *
  110. * @param dataset is a pointer to a previously allocated randomx_dataset structure.
  111. */
  112. void randomx_release_dataset(randomx_dataset *dataset);
  113. /**
  114. * Creates and initializes a RandomX virtual machine.
  115. *
  116. * @param flags is any combination of these 4 flags (each flag can be set or not set):
  117. * RANDOMX_FLAG_LARGE_PAGES - allocate scratchpad memory in large pages
  118. * RANDOMX_FLAG_HARD_AES - virtual machine will use hardware accelerated AES
  119. * RANDOMX_FLAG_FULL_MEM - virtual machine will use the full dataset
  120. * RANDOMX_FLAG_JIT - virtual machine will use a JIT compiler
  121. * The numeric values of the flags are ordered so that a higher value will provide
  122. * faster hash calculation and a lower numeric value will provide higher portability.
  123. * Using RANDOMX_FLAG_DEFAULT (all flags not set) works on all platforms, but is the slowest.
  124. * @param cache is a pointer to an initialized randomx_cache structure. Can be
  125. * NULL if RANDOMX_FLAG_FULL_MEM is set.
  126. * @param dataset is a pointer to a randomx_dataset structure. Can be NULL
  127. * if RANDOMX_FLAG_FULL_MEM is not set.
  128. *
  129. * @return Pointer to an initialized randomx_vm structure.
  130. * Returns NULL if:
  131. * (1) Scratchpad memory allocation fails.
  132. * (2) The requested initialization flags are not supported on the current platform.
  133. * (3) cache parameter is NULL and RANDOMX_FLAG_FULL_MEM is not set
  134. * (4) dataset parameter is NULL and RANDOMX_FLAG_FULL_MEM is set
  135. */
  136. randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset);
  137. /**
  138. * Reinitializes a virtual machine with a new Cache. This function should be called anytime
  139. * the Cache is reinitialized with a new key.
  140. *
  141. * @param machine is a pointer to a randomx_vm structure that was initialized
  142. * without RANDOMX_FLAG_FULL_MEM. Must not be NULL.
  143. * @param cache is a pointer to an initialized randomx_cache structure. Must not be NULL.
  144. */
  145. void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache);
  146. /**
  147. * Reinitializes a virtual machine with a new Dataset.
  148. *
  149. * @param machine is a pointer to a randomx_vm structure that was initialized
  150. * with RANDOMX_FLAG_FULL_MEM. Must not be NULL.
  151. * @param dataset is a pointer to an initialized randomx_dataset structure. Must not be NULL.
  152. */
  153. void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset);
  154. /**
  155. * Releases all memory occupied by the randomx_vm structure.
  156. *
  157. * @param machine is a pointer to a previously created randomx_vm structure.
  158. */
  159. void randomx_destroy_vm(randomx_vm *machine);
  160. /**
  161. * Calculates a RandomX hash value.
  162. *
  163. * @param machine is a pointer to a randomx_vm structure. Must not be NULL.
  164. * @param input is a pointer to memory to be hashed. Must not be NULL.
  165. * @param inputSize is the number of bytes to be hashed.
  166. * @param output is a pointer to memory where the hash will be stored. Must not
  167. * be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing.
  168. */
  169. void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output);
  170. #if defined(__cplusplus)
  171. }
  172. #endif
  173. #endif