-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathvdf_base.hpp
328 lines (230 loc) · 6.58 KB
/
vdf_base.hpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#ifndef VDF_BASE_H
#define VDF_BASE_H
#if VDF_MODE==0
#define NDEBUG
#endif
#include "Reducer.h"
#include <gmpxx.h>
//#include <cstdint>
//#include "include.h"
#include <atomic>
#include <string>
#include <vector>
#include <cstdint>
#ifndef _WIN32
#define USED __attribute__((used))
#else
#define USED
#endif
//using namespace std;
using std::string, std::vector;
typedef int64_t int64;
typedef uint64_t uint64;
void VdfBaseInit(void);
typedef __mpz_struct mpz_struct;
struct integer {
public:
mpz_struct impl[1];
inline ~integer() {
mpz_clear(impl);
}
inline integer() {
//assert(allow_integer_constructor);
mpz_init(impl);
}
inline integer(mpz_t t) {
mpz_init_set(impl, t);
}
inline integer(const integer& t) {
mpz_init(impl);
mpz_set(impl, t.impl);
}
inline integer(integer&& t) {
mpz_init(impl);
mpz_swap(impl, t.impl);
}
explicit inline integer(int i) {
mpz_init_set_si(impl, i);
}
explicit inline integer(uint32_t i) {
mpz_init_set_ui(impl, i);
}
explicit inline integer(int64 i) {
mpz_init_set_si(impl, i);
}
explicit inline integer(uint64_t i) {
mpz_init_set_ui(impl, i);
}
explicit integer(const string& s);
explicit integer(const std::vector<uint8_t> v);
//lsb first
explicit integer(const vector<uint64>& data);
integer(const uint8_t *bytes, size_t size);
//lsb first
vector<uint64> to_vector() const;
vector<uint8_t> to_bytes() const;
inline integer& operator=(const integer& t) {
mpz_set(impl, t.impl);
return *this;
}
inline integer& operator=(integer&& t) {
mpz_swap(impl, t.impl);
return *this;
}
integer& operator=(int64 i);
integer& operator=(const string& s);
void set_bit(int index, bool value);
bool get_bit(int index);
USED string to_string() const;
string to_string_dec() const;
integer& operator+=(const integer& t);
integer operator+(const integer& t) const;
integer& operator-=(const integer& t);
integer operator-(const integer& t) const;
integer& operator*=(const integer& t);
integer operator*(const integer& t) const;
integer& operator<<=(int i);
integer operator<<(int i) const;
integer operator-() const;
integer& operator/=(const integer& t);
integer operator/(const integer& t) const;
integer& operator>>=(int i);
integer operator>>(int i) const;
//this is different from mpz_fdiv_r because it ignores the sign of t
integer& operator%=(const integer& t);
integer operator%(const integer& t) const;
integer fdiv_r(const integer& t) const;
bool prime() const;
bool operator<(const integer& t) const;
bool operator<=(const integer& t) const;
bool operator==(const integer& t) const;
bool operator>=(const integer& t) const;
bool operator>(const integer& t) const;
bool operator!=(const integer& t) const;
bool operator<(int i) const;
bool operator<=(int i) const;
bool operator==(int i) const;
bool operator>=(int i) const;
bool operator>(int i) const;
bool operator!=(int i) const;
int num_bits() const;
};
integer root(const integer& t, int n);
integer CreateDiscriminant(std::vector<uint8_t>& seed, int length = 1024);
struct form {
integer a;
integer b;
integer c;
static form from_abd(const integer& t_a, const integer& t_b, const integer& d);
static form identity(const integer& d);
static form generator(const integer& d) {
return from_abd(integer(2), integer(1), d);
}
void reduce();
bool is_reduced();
form inverse() const;
bool check_valid(const integer& d);
void assert_valid(const integer& d);
bool operator==(const form& f) const;
bool operator<(const form& f) const;
//assumes this is normalized (c has the highest magnitude)
//the inverse has the same hash
int hash() const;
};
class PulmarkReducer {
ClassGroupContext *t;
Reducer *reducer;
public:
PulmarkReducer() {
t=new ClassGroupContext(4096);
reducer=new Reducer(*t);
}
~PulmarkReducer() {
delete(reducer);
delete(t);
}
void reduce(form &f) {
mpz_set(t->a, f.a.impl);
mpz_set(t->b, f.b.impl);
mpz_set(t->c, f.c.impl);
reducer->run();
mpz_set(f.a.impl, t->a);
mpz_set(f.b.impl, t->b);
mpz_set(f.c.impl, t->c);
}
};
struct Segment {
uint64_t start;
uint64_t length;
form x;
form y;
form proof;
bool is_empty;
Segment() {
is_empty = true;
}
Segment(uint64_t start, uint64_t length, form& x, form& y) {
this->start = start;
this->length = length;
this->x = x;
this->y = y;
is_empty = false;
}
bool IsWorseThan(Segment& other);
int GetSegmentBucket();
};
class Prover {
public:
Prover(Segment segm, integer D) {
this->segm = segm;
this->D = D;
this->num_iterations = segm.length;
is_finished = false;
}
virtual form* GetForm(uint64_t iteration) = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual bool PerformExtraStep() = 0;
virtual void OnFinish() = 0;
bool IsFinished() {
return is_finished;
}
form GetProof() {
return proof;
}
uint64_t GetBlock(uint64_t i, uint64_t k, uint64_t T, integer& B);
void GenerateProof();
protected:
Segment segm;
integer D;
form proof;
uint64_t num_iterations;
uint32_t k;
uint32_t l;
std::atomic<bool> is_finished;
};
#define PROVER_MAX_SEGMENT_THREADS 8
class ParallelProver : public Prover {
public:
ParallelProver(Segment segm, integer D, size_t n_thr) : Prover(segm, D) {
this->n_threads = n_thr;
}
void GenerateProof();
protected:
integer B;
integer L;
form id;
form x_vals[PROVER_MAX_SEGMENT_THREADS];
size_t n_threads;
};
void nudupl_form(form &a, form &b, integer &D, integer &L);
integer GetB(const integer& D, form &x, form& y);
form GenerateWesolowski(form &y, form &x_init,
integer &D, PulmarkReducer& reducer,
std::vector<form>& intermediates,
uint64_t num_iterations,
uint64_t k, uint64_t l);
void VerifyWesolowskiProof(integer &D, form x, form y, form proof, uint64_t iters, bool &is_valid);
void Int64ToBytes(uint8_t *result, uint64_t input);
void Int32ToBytes(uint8_t *result, uint32_t input);
#endif // VDF_BASE_H