randomx.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Copyright (c) 2019 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef RANDOMX_H
  16. #define RANDOMX_H
  17. #include <stddef.h>
  18. #define RANDOMX_HASH_SIZE 32
  19. #define RANDOMX_DATASET_ITEM_SIZE 64
  20. typedef enum {
  21. RANDOMX_FLAG_DEFAULT = 0,
  22. RANDOMX_FLAG_LARGE_PAGES = 1,
  23. RANDOMX_FLAG_HARD_AES = 2,
  24. RANDOMX_FLAG_FULL_MEM = 4,
  25. RANDOMX_FLAG_JIT = 8,
  26. } randomx_flags;
  27. typedef struct randomx_dataset randomx_dataset;
  28. typedef struct randomx_cache randomx_cache;
  29. typedef struct randomx_vm randomx_vm;
  30. #if defined(__cplusplus)
  31. extern "C" {
  32. #endif
  33. /**
  34. * Creates a randomx_cache structure and allocates memory for RandomX Cache.
  35. *
  36. * @param flags is any combination of these 2 flags (each flag can be set or not set):
  37. * RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
  38. * RANDOMX_FLAG_JIT - create cache structure with JIT compilation support; this makes
  39. * subsequent Dataset initialization faster
  40. *
  41. * @return Pointer to an allocated randomx_cache structure.
  42. * NULL is returned if memory allocation fails or if the RANDOMX_FLAG_JIT
  43. * is set and JIT compilation is not supported on the current platform.
  44. */
  45. randomx_cache *randomx_alloc_cache(randomx_flags flags);
  46. /**
  47. * Initializes the cache memory and SuperscalarHash using the provided key value.
  48. *
  49. * @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL.
  50. * @param key is a pointer to memory which contains the key value. Must not be NULL.
  51. * @param keySize is the number of bytes of the key.
  52. */
  53. void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize);
  54. /**
  55. * Releases all memory occupied by the randomx_cache structure.
  56. *
  57. * @param cache is a pointer to a previously allocated randomx_cache structure.
  58. */
  59. void randomx_release_cache(randomx_cache* cache);
  60. /**
  61. * Creates a randomx_dataset structure and allocates memory for RandomX Dataset.
  62. *
  63. * @param flags is the initialization flags. Only one flag is supported (can be set or not set):
  64. * RANDOMX_FLAG_LARGE_PAGES - allocate memory in large pages
  65. *
  66. * @return Pointer to an allocated randomx_dataset structure.
  67. * NULL is returned if memory allocation fails.
  68. */
  69. randomx_dataset *randomx_alloc_dataset(randomx_flags flags);
  70. /**
  71. * Gets the number of items contained in the dataset.
  72. *
  73. * @return the number of items contained in the dataset.
  74. */
  75. unsigned long randomx_dataset_item_count();
  76. /**
  77. * Initializes dataset items.
  78. *
  79. * Note: In order to use the Dataset, all items from 0 to (randomx_dataset_item_count() - 1) must be initialized.
  80. * This may be done by several calls to this function using non-overlapping item sequences.
  81. *
  82. * @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
  83. * @param cache is a pointer to a previously allocated and initialized randomx_cache structure. Must not be NULL.
  84. * @param startItem is the item number where intialization should start.
  85. * @param itemCount is the number of items that should be initialized.
  86. */
  87. void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount);
  88. /**
  89. * Returns a pointer to the internal memory buffer of the dataset structure. The size
  90. * of the internal memory buffer is randomx_dataset_item_count() * RANDOMX_DATASET_ITEM_SIZE.
  91. *
  92. * @param dataset is dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
  93. *
  94. * @return Pointer to the internal memory buffer of the dataset structure.
  95. */
  96. void *randomx_get_dataset_memory(randomx_dataset *dataset);
  97. /**
  98. * Releases all memory occupied by the randomx_dataset structure.
  99. *
  100. * @param dataset is a pointer to a previously allocated randomx_dataset structure.
  101. */
  102. void randomx_release_dataset(randomx_dataset *dataset);
  103. /**
  104. * Creates and initializes a RandomX virtual machine.
  105. *
  106. * @param flags is any combination of these 4 flags (each flag can be set or not set):
  107. * RANDOMX_FLAG_LARGE_PAGES - allocate scratchpad memory in large pages
  108. * RANDOMX_FLAG_HARD_AES - virtual machine will use hardware accelerated AES
  109. * RANDOMX_FLAG_FULL_MEM - virtual machine will use the full dataset
  110. * RANDOMX_FLAG_JIT - virtual machine will use a JIT compiler
  111. * The numeric values of the flags are ordered so that a higher value will provide
  112. * faster hash calculation and a lower numeric value will provide higher portability.
  113. * Using RANDOMX_FLAG_DEFAULT (all flags not set) works on all platforms, but is the slowest.
  114. * @param cache is a pointer to an initialized randomx_cache structure. Can be
  115. * NULL if RANDOMX_FLAG_FULL_MEM is set.
  116. * @param dataset is a pointer to a randomx_dataset structure. Can be NULL
  117. * if RANDOMX_FLAG_FULL_MEM is not set.
  118. *
  119. * @return Pointer to an initialized randomx_vm structure.
  120. * Returns NULL if:
  121. * (1) Scratchpad memory allocation fails.
  122. * (2) The requested initialization flags are not supported on the current platform.
  123. * (3) cache parameter is NULL and RANDOMX_FLAG_FULL_MEM is not set
  124. * (4) dataset parameter is NULL and RANDOMX_FLAG_FULL_MEM is set
  125. */
  126. randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset);
  127. /**
  128. * Reinitializes a virtual machine with a new Cache. This function should be called anytime
  129. * the Cache is reinitialized with a new key.
  130. *
  131. * @param machine is a pointer to a randomx_vm structure that was initialized
  132. * without RANDOMX_FLAG_FULL_MEM. Must not be NULL.
  133. * @param cache is a pointer to an initialized randomx_cache structure. Must not be NULL.
  134. */
  135. void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache);
  136. /**
  137. * Reinitializes a virtual machine with a new Dataset.
  138. *
  139. * @param machine is a pointer to a randomx_vm structure that was initialized
  140. * with RANDOMX_FLAG_FULL_MEM. Must not be NULL.
  141. * @param dataset is a pointer to an initialized randomx_dataset structure. Must not be NULL.
  142. */
  143. void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset);
  144. /**
  145. * Releases all memory occupied by the randomx_vm structure.
  146. *
  147. * @param machine is a pointer to a previously created randomx_vm structure.
  148. */
  149. void randomx_destroy_vm(randomx_vm *machine);
  150. /**
  151. * Calculates a RandomX hash value.
  152. *
  153. * @param machine is a pointer to a randomx_vm structure. Must not be NULL.
  154. * @param input is a pointer to memory to be hashed. Must not be NULL.
  155. * @param inputSize is the number of bytes to be hashed.
  156. * @param output is a pointer to memory where the hash will be stored. Must not
  157. * be NULL and at least RANDOMX_HASH_SIZE bytes must be available for writing.
  158. */
  159. void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output);
  160. #if defined(__cplusplus)
  161. }
  162. #endif
  163. #endif