-
Notifications
You must be signed in to change notification settings - Fork 63
/
callback.h
233 lines (201 loc) · 6.71 KB
/
callback.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
#ifndef CALLBACK_H
#define CALLBACK_H
#include "util.h"
// Applies to n-weso.
const int kWindowSize = 20;
// Applies only to 2-weso.
const int kMaxItersAllowed = 8e8;
const int kSwitchIters = 91000000;
class WesolowskiCallback :public INUDUPLListener {
public:
WesolowskiCallback(integer& D) {
vdfo = new vdf_original();
reducer = new PulmarkReducer();
this->D = D;
this->L = root(-D, 4);
}
virtual ~WesolowskiCallback() {
delete(vdfo);
delete(reducer);
}
void reduce(form& inf) {
reducer->reduce(inf);
}
void SetForm(int type, void *data, form* mulf, bool reduced = true) {
switch(type) {
case NL_SQUARESTATE:
{
//cout << "NL_SQUARESTATE" << endl;
uint64 res;
square_state_type *square_state=(square_state_type *)data;
if(!square_state->assign(mulf->a, mulf->b, mulf->c, res))
cout << "square_state->assign failed" << endl;
break;
}
case NL_FORM:
{
//cout << "NL_FORM" << endl;
vdf_original::form *f=(vdf_original::form *)data;
mpz_set(mulf->a.impl, f->a);
mpz_set(mulf->b.impl, f->b);
mpz_set(mulf->c.impl, f->c);
break;
}
default:
cout << "Unknown case" << endl;
}
if (reduced) {
reduce(*mulf);
}
}
virtual void OnIteration(int type, void *data, uint64_t iteration) = 0;
std::unique_ptr<form[]> forms;
std::atomic<int64_t> iterations{0};
integer D;
integer L;
PulmarkReducer* reducer;
vdf_original* vdfo;
};
class OneWesolowskiCallback: public WesolowskiCallback {
public:
OneWesolowskiCallback(integer& D, form& f, uint64_t wanted_iter) : WesolowskiCallback(D) {
uint32_t k, l;
this->wanted_iter = wanted_iter;
if (wanted_iter >= (1 << 16)) {
ApproximateParameters(wanted_iter, k, l);
} else {
k = 10;
l = 1;
}
kl = k * l;
uint64_t space_needed = wanted_iter / (k * l) + 100;
forms.reset(new form[space_needed]);
forms[0] = f;
}
void OnIteration(int type, void *data, uint64_t iteration) {
iteration++;
if (iteration > wanted_iter)
return ;
if (iteration % kl == 0) {
uint64_t pos = iteration / kl;
form* mulf = &forms[pos];
SetForm(type, data, mulf);
}
if (iteration == wanted_iter) {
SetForm(type, data, &result);
}
}
uint64_t wanted_iter;
uint32_t kl;
form result;
};
class TwoWesolowskiCallback: public WesolowskiCallback {
public:
TwoWesolowskiCallback(integer& D, form f) : WesolowskiCallback(D) {
int space_needed = kSwitchIters / 10 + (kMaxItersAllowed - kSwitchIters) / 100;
forms.reset(new form[space_needed]);
forms[0] = f;
kl = 10;
switch_iters = -1;
}
void IncreaseConstants(uint64_t num_iters) {
kl = 100;
switch_iters = num_iters;
switch_index = num_iters / 10;
}
int GetPosition(uint64_t power) {
if (switch_iters == -1 || power < switch_iters) {
return power / 10;
} else {
return (switch_index + (power - switch_iters) / 100);
}
}
form *GetForm(uint64_t power) {
return &(forms[GetPosition(power)]);
}
bool LargeConstants() {
return kl == 100;
}
void OnIteration(int type, void *data, uint64_t iteration) {
iteration++;
if (iteration % kl == 0) {
uint64_t pos = GetPosition(iteration);
form* mulf = &forms[pos];
SetForm(type, data, mulf);
}
}
private:
uint64_t switch_index;
int64_t switch_iters;
uint32_t kl;
};
class FastAlgorithmCallback : public WesolowskiCallback {
public:
FastAlgorithmCallback(int segments, integer& D, form f, bool multi_proc_machine) : WesolowskiCallback(D) {
buckets_begin.push_back(0);
buckets_begin.push_back(bucket_size1 * window_size);
this->segments = segments;
this->multi_proc_machine = multi_proc_machine;
for (int i = 0; i < segments - 2; i++) {
buckets_begin.push_back(buckets_begin[buckets_begin.size() - 1] + bucket_size2 * window_size);
}
int space_needed = window_size * (bucket_size1 + bucket_size2 * (segments - 1));
forms.reset(new form[space_needed]);
checkpoints.reset(new form[1 << 18]);
y_ret = f;
for (int i = 0; i < segments; i++)
forms[buckets_begin[i]] = f;
checkpoints[0] = f;
}
int GetPosition(uint64_t exponent, int bucket) {
uint64_t power_2 = 1LL << (16 + 2 * bucket);
int position = buckets_begin[bucket];
int size = (bucket == 0) ? bucket_size1 : bucket_size2;
int kl = (bucket == 0) ? 10 : (12 * (power_2 >> 18));
position += ((exponent / power_2) % window_size) * size;
position += (exponent % power_2) / kl;
return position;
}
form *GetForm(uint64_t exponent, int bucket) {
uint64_t pos = GetPosition(exponent, bucket);
return &(forms[pos]);
}
// We need to store:
// 2^16 * k + 10 * l
// 2^(18 + 2*m) * k + 12 * 2^(2*m) * l
void OnIteration(int type, void *data, uint64_t iteration) {
iteration++;
if (multi_proc_machine) {
if (iteration % (1 << 15) == 0) {
SetForm(type, data, &y_ret);
}
} else {
// If 'multi_proc_machine' is 0, we store the intermediates
// right away.
for (int i = 0; i < segments; i++) {
uint64_t power_2 = 1LL << (16 + 2LL * i);
int kl = (i == 0) ? 10 : (12 * (power_2 >> 18));
if ((iteration % power_2) % kl == 0) {
form* mulf = GetForm(iteration, i);
SetForm(type, data, mulf);
}
}
}
if (iteration % (1 << 16) == 0) {
form* mulf = (&checkpoints[(iteration / (1 << 16))]);
SetForm(type, data, mulf);
}
}
std::vector<int> buckets_begin;
std::unique_ptr<form[]> checkpoints;
form y_ret;
int segments;
// The intermediate values size of a 2^16 segment.
const int bucket_size1 = 6554;
// The intermediate values size of a >= 2^18 segment.
const int bucket_size2 = 21846;
// Assume provers won't be left behind by more than this # of segments.
const int window_size = kWindowSize;
bool multi_proc_machine;
};
#endif // CALLBACK_H