-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.h
294 lines (265 loc) · 7.06 KB
/
post.h
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
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define CPU_PROVIDER_ID UINT32_MAX
typedef enum DeviceClass {
CPU = 1,
GPU = 2,
} DeviceClass;
typedef enum InitializeResult {
InitializeOk = 0,
InitializeOkNonceNotFound = 1,
InitializeInvalidLabelsRange = 2,
InitializeError = 3,
InitializeInvalidArgument = 4,
InitializeFailedToGetProviders = 5,
} InitializeResult;
/**
* An enum representing the available verbosity levels of the logger.
*
* Typical usage includes: checking if a certain `Level` is enabled with
* [`log_enabled!`](macro.log_enabled.html), specifying the `Level` of
* [`log!`](macro.log.html), and comparing a `Level` directly to a
* [`LevelFilter`](enum.LevelFilter.html).
*/
enum Level {
/**
* The "error" level.
*
* Designates very serious errors.
*/
Error = 1,
/**
* The "warn" level.
*
* Designates hazardous situations.
*/
Warn,
/**
* The "info" level.
*
* Designates useful information.
*/
Info,
/**
* The "debug" level.
*
* Designates lower priority information.
*/
Debug,
/**
* The "trace" level.
*
* Designates very low priority, often extremely verbose, information.
*/
Trace,
};
typedef uintptr_t Level;
typedef enum VerifyResult {
Ok,
Invalid,
InvalidArgument,
FailedToCreateVerifier,
Failed,
} VerifyResult;
typedef struct Initializer Initializer;
typedef struct Verifier Verifier;
typedef struct Provider {
char name[64];
uint32_t id;
enum DeviceClass class_;
} Provider;
/**
* Scrypt algorithm parameters.
*
* Refer to <https://www.rfc-editor.org/rfc/rfc7914#section-2>
*/
typedef struct ScryptParams {
/**
* N = 1 << (nfactor + 1)
*/
uint8_t nfactor;
/**
* r = 1 << rfactor
*/
uint8_t rfactor;
/**
* p = 1 << pfactor
*/
uint8_t pfactor;
} ScryptParams;
/**
* FFI-safe borrowed Rust &str
*/
typedef struct StringView {
const char *ptr;
uintptr_t len;
} StringView;
typedef struct ExternCRecord {
Level level;
struct StringView message;
struct StringView module_path;
struct StringView file;
int64_t line;
} ExternCRecord;
typedef struct ArrayU8 {
uint8_t *ptr;
uintptr_t len;
uintptr_t cap;
} ArrayU8;
typedef struct Proof {
uint32_t nonce;
struct ArrayU8 indices;
uint64_t pow;
struct ArrayU8 pow_creator;
} Proof;
typedef struct Config {
/**
* K1 specifies the difficulty for a label to be a candidate for a proof.
*/
uint32_t k1;
/**
* K2 is the number of labels below the required difficulty required for a proof.
*/
uint32_t k2;
/**
* K3 is the size of the subset of proof indices that is validated.
*/
uint32_t k3;
/**
* Difficulty for the nonce proof of work. Lower values increase difficulty of finding
* `pow` for [Proof][crate::prove::Proof].
*/
uint8_t pow_difficulty[32];
/**
* Scrypt paramters for initilizing labels
*/
struct ScryptParams scrypt;
} Config;
/**
* RandomX Flags are used to configure the library.
*/
typedef uint32_t RandomXFlag;
/**
* No flags set. Works on all platforms, but is the slowest.
*/
#define RandomXFlag_FLAG_DEFAULT (uint32_t)0
/**
* Allocate memory in large pages.
*/
#define RandomXFlag_FLAG_LARGE_PAGES (uint32_t)1
/**
* Use hardware accelerated AES.
*/
#define RandomXFlag_FLAG_HARD_AES (uint32_t)2
/**
* Use the full dataset.
*/
#define RandomXFlag_FLAG_FULL_MEM (uint32_t)4
/**
* Use JIT compilation support.
*/
#define RandomXFlag_FLAG_JIT (uint32_t)8
/**
* When combined with FLAG_JIT, the JIT pages are never writable and executable at the
* same time.
*/
#define RandomXFlag_FLAG_SECURE (uint32_t)16
/**
* Optimize Argon2 for CPUs with the SSSE3 instruction set.
*/
#define RandomXFlag_FLAG_ARGON2_SSSE3 (uint32_t)32
/**
* Optimize Argon2 for CPUs with the AVX2 instruction set.
*/
#define RandomXFlag_FLAG_ARGON2_AVX2 (uint32_t)64
/**
* Optimize Argon2 for CPUs without the AVX2 or SSSE3 instruction sets.
*/
#define RandomXFlag_FLAG_ARGON2 (uint32_t)96
typedef struct ProofMetadata {
uint8_t node_id[32];
uint8_t commitment_atx_id[32];
uint8_t challenge[32];
uint32_t num_units;
uint64_t labels_per_unit;
} ProofMetadata;
/**
* Returns the number of providers available.
*/
uintptr_t get_providers_count(void);
/**
* Returns all available providers.
*/
enum InitializeResult get_providers(struct Provider *out, uintptr_t out_len);
/**
* Initializes labels for the given range.
*
* start and end are inclusive.
*/
enum InitializeResult initialize(struct Initializer *initializer,
uint64_t start,
uint64_t end,
uint8_t *out_buffer,
uint64_t *out_nonce);
struct Initializer *new_initializer(uint32_t provider_id,
uintptr_t n,
const uint8_t *commitment,
const uint8_t *vrf_difficulty);
void free_initializer(struct Initializer *initializer);
enum VerifyResult verify_pos(const char *datadir,
const uint32_t *from_file,
const uint32_t *to_file,
double fraction,
struct ScryptParams scrypt);
/**
* Set a logging callback function
* The function is idempotent, calling it more then once will have no effect.
* Returns 0 if the callback was set successfully, 1 otherwise.
*/
int32_t set_logging_callback(Level level, void (*callback)(const struct ExternCRecord*));
/**
* Deallocate a proof obtained with generate_proof().
* # Safety
* `proof` must be a pointer to a Proof struct obtained with generate_proof().
*/
void free_proof(struct Proof *proof);
/**
* Generates a proof of space for the given challenge using the provided parameters.
* Returns a pointer to a Proof struct which should be freed with free_proof() after use.
* If an error occurs, prints it on stderr and returns null.
* # Safety
* `challenge` must be a 32-byte array.
* `miner_id` must be null or point to a 32-byte array.
*/
struct Proof *generate_proof(const char *datadir,
const unsigned char *challenge,
struct Config cfg,
uintptr_t nonces,
uintptr_t threads,
RandomXFlag pow_flags,
const unsigned char *miner_id);
/**
* Get the recommended RandomX flags
*
* Does not include:
* * FLAG_LARGE_PAGES
* * FLAG_FULL_MEM
* * FLAG_SECURE
*
* The above flags need to be set manually, if required.
*/
RandomXFlag recommended_pow_flags(void);
enum VerifyResult new_verifier(RandomXFlag flags, struct Verifier **out);
void free_verifier(struct Verifier *verifier);
/**
* Verify a proof
*
* # Safety
* `metadata` must be initialized and properly aligned.
*/
enum VerifyResult verify_proof(const struct Verifier *verifier,
struct Proof proof,
const struct ProofMetadata *metadata,
struct Config cfg);