1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Generated with help of rust-bindgen 0.69.2 */

#![allow(dead_code)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]

pub mod flags {
    pub const randomx_flags_RANDOMX_FLAG_DEFAULT: randomx_flags = 0;
    pub const randomx_flags_RANDOMX_FLAG_LARGE_PAGES: randomx_flags = 1;
    pub const randomx_flags_RANDOMX_FLAG_HARD_AES: randomx_flags = 2;
    pub const randomx_flags_RANDOMX_FLAG_FULL_MEM: randomx_flags = 4;
    pub const randomx_flags_RANDOMX_FLAG_JIT: randomx_flags = 8;
    pub const randomx_flags_RANDOMX_FLAG_SECURE: randomx_flags = 16;
    pub const randomx_flags_RANDOMX_FLAG_ARGON2_SSSE3: randomx_flags = 32;
    pub const randomx_flags_RANDOMX_FLAG_ARGON2_AVX2: randomx_flags = 64;
    pub const randomx_flags_RANDOMX_FLAG_ARGON2: randomx_flags = 96;

    pub type randomx_flags = ::std::os::raw::c_uint;

    extern "C" {
        #[doc = " @return 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.
        "]
        pub fn randomx_get_flags() -> randomx_flags;
    }
}

pub mod cache {
    use super::flags::randomx_flags;

    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct randomx_cache {
        _unused: [u8; 0],
    }

    extern "C" {
        #[doc = " 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
         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

         @return 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
        "]
        pub fn randomx_alloc_cache(flags: randomx_flags) -> *mut randomx_cache;
    }

    extern "C" {
        #[doc = " Initializes the cache memory and SuperscalarHash using the provided key value.
          Does nothing if called again with the same key value.

          @param cache is a pointer to a previously allocated randomx_cache structure. Must not be NULL.
          @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.
        "]
        pub fn randomx_init_cache(
            cache: *mut randomx_cache,
            key: *const ::std::os::raw::c_void,
            keySize: usize,
        );
    }

    extern "C" {
        #[doc = " Releases all memory occupied by the randomx_cache structure.

          @param cache is a pointer to a previously allocated randomx_cache structure.
        "]
        pub fn randomx_release_cache(cache: *mut randomx_cache);
    }
}

pub mod dataset {
    use super::cache::randomx_cache;
    use super::flags::randomx_flags;

    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct randomx_dataset {
        _unused: [u8; 0],
    }

    extern "C" {
        #[doc = " 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

           @return Pointer to an allocated randomx_dataset structure.
                    NULL is returned if memory allocation fails.
        "]
        pub fn randomx_alloc_dataset(flags: randomx_flags) -> *mut randomx_dataset;
    }

    extern "C" {
        #[doc = " Gets the number of items contained in the dataset.

          @return the number of items contained in the dataset.
        "]
        pub fn randomx_dataset_item_count() -> ::std::os::raw::c_ulong;
    }

    extern "C" {
        #[doc = " 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.

          @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.
        "]
        pub fn randomx_init_dataset(
            dataset: *mut randomx_dataset,
            cache: *mut randomx_cache,
            startItem: ::std::os::raw::c_ulong,
            itemCount: ::std::os::raw::c_ulong,
        );
    }

    extern "C" {
        #[doc = " 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.

           @param dataset is a pointer to a previously allocated randomx_dataset structure. Must not be NULL.
           @return Pointer to the internal memory buffer of the dataset structure.
        "]
        pub fn randomx_get_dataset_memory(
            dataset: *mut randomx_dataset,
        ) -> *mut ::std::os::raw::c_void;
    }

    extern "C" {
        #[doc = " Releases all memory occupied by the randomx_dataset structure.\
          @param dataset is a pointer to a previously allocated randomx_dataset structure.\
        "]
        pub fn randomx_release_dataset(dataset: *mut randomx_dataset);
    }
}

pub mod vm {
    use super::cache::randomx_cache;
    use super::dataset::randomx_dataset;
    use super::flags::randomx_flags;

    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct randomx_vm {
        _unused: [u8; 0],
    }

    extern "C" {
        #[doc = " Creates and initializes a RandomX virtual machine.

          @param flags is 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.
          @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
         "]
        pub fn randomx_create_vm(
            flags: randomx_flags,
            cache: *mut randomx_cache,
            dataset: *mut randomx_dataset,
        ) -> *mut randomx_vm;
    }

    extern "C" {
        #[doc = " 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.

           @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.
        "]
        pub fn randomx_vm_set_cache(machine: *mut randomx_vm, cache: *mut randomx_cache);
    }

    extern "C" {
        #[doc = " 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.
        "]
        pub fn randomx_vm_set_dataset(machine: *mut randomx_vm, dataset: *mut randomx_dataset);
    }

    extern "C" {
        #[doc = " Releases all memory occupied by the randomx_vm structure.
          @param machine is a pointer to a previously created randomx_vm structure.\
        "]
        pub fn randomx_destroy_vm(machine: *mut randomx_vm);
    }

    extern "C" {
        #[doc = " 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.
        "]
        pub fn randomx_calculate_hash(
            machine: *mut randomx_vm,
            input: *const ::std::os::raw::c_void,
            inputSize: usize,
            output: *mut ::std::os::raw::c_void,
        );
    }

    extern "C" {
        #[doc = " 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.

           @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 nextInput is a pointer to memory to be hashed for the next hash. Must not be NULL.
           @param nextInputSize is the number of bytes to be hashed for the next hash.
           @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.
        "]
        pub fn randomx_calculate_hash_first(
            machine: *mut randomx_vm,
            input: *const ::std::os::raw::c_void,
            inputSize: usize,
        );
    }

    extern "C" {
        pub fn randomx_calculate_hash_next(
            machine: *mut randomx_vm,
            nextInput: *const ::std::os::raw::c_void,
            nextInputSize: usize,
            output: *mut ::std::os::raw::c_void,
        );
    }

    extern "C" {
        pub fn randomx_calculate_hash_last(
            machine: *mut randomx_vm,
            output: *mut ::std::os::raw::c_void,
        );
    }
}