forked from Chia-Network/chiapos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.cpp
299 lines (272 loc) · 11 KB
/
cli.cpp
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
// Copyright 2018 Chia Network Inc
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <ctime>
#include <set>
#include "cxxopts.hpp"
#include "../lib/include/picosha2.hpp"
#include "plotter_disk.hpp"
#include "prover_disk.hpp"
#include "verifier.hpp"
using std::string;
using std::vector;
using std::endl;
using std::cout;
void HexToBytes(const string &hex, uint8_t *result)
{
for (uint32_t i = 0; i < hex.length(); i += 2) {
string byteString = hex.substr(i, 2);
uint8_t byte = (uint8_t)strtol(byteString.c_str(), NULL, 16);
result[i / 2] = byte;
}
}
vector<unsigned char> intToBytes(uint32_t paramInt, uint32_t numBytes)
{
vector<unsigned char> arrayOfByte(numBytes, 0);
for (uint32_t i = 0; paramInt > 0; i++) {
arrayOfByte[numBytes - i - 1] = paramInt & 0xff;
paramInt >>= 8;
}
return arrayOfByte;
}
string Strip0x(const string &hex)
{
if (hex.size() > 1 && (hex.substr(0, 2) == "0x" || hex.substr(0, 2) == "0X")) {
return hex.substr(2);
}
return hex;
}
void HelpAndQuit(cxxopts::Options options)
{
cout << options.help({""}) << endl;
cout << "./ProofOfSpace create" << endl;
cout << "./ProofOfSpace prove <challenge>" << endl;
cout << "./ProofOfSpace verify <proof> <challenge>" << endl;
cout << "./ProofOfSpace check" << endl;
exit(0);
}
int main(int argc, char *argv[]) try {
cxxopts::Options options(
"ProofOfSpace", "Utility for plotting, generating and verifying proofs of space.");
options.positional_help("(create/prove/verify/check) param1 param2 ")
.show_positional_help();
// Default values
uint8_t k = 20;
uint32_t num_buckets = 0;
uint32_t num_stripes = 0;
uint8_t num_threads = 0;
string filename = "plot.dat";
string tempdir = ".";
string tempdir2 = ".";
string finaldir = ".";
string operation = "help";
string memo = "0102030405";
string id = "022fb42c08c12de3a6af053880199806532e79515f94e83461612101f9412f9e";
bool nobitfield = false;
bool show_progress = false;
bool parallel_read = true;
uint32_t buffmegabytes = 0;
options.allow_unrecognised_options().add_options()(
"k, size", "Plot size", cxxopts::value<uint8_t>(k))(
"r, threads", "Number of threads", cxxopts::value<uint8_t>(num_threads))(
"u, buckets", "Number of buckets", cxxopts::value<uint32_t>(num_buckets))(
"s, stripes", "Size of stripes", cxxopts::value<uint32_t>(num_stripes))(
"t, tempdir", "Temporary directory", cxxopts::value<string>(tempdir))(
"2, tempdir2", "Second Temporary directory", cxxopts::value<string>(tempdir2))(
"d, finaldir", "Final directory", cxxopts::value<string>(finaldir))(
"f, file", "Filename", cxxopts::value<string>(filename))(
"m, memo", "Memo to insert into the plot", cxxopts::value<string>(memo))(
"i, id", "Unique 32-byte seed for the plot", cxxopts::value<string>(id))(
"e, nobitfield", "Disable bitfield", cxxopts::value<bool>(nobitfield))(
"b, buffer",
"Megabytes to be used as buffer for sorting and plotting",
cxxopts::value<uint32_t>(buffmegabytes))(
"p, progress", "Display progress percentage during plotting",
cxxopts::value<bool>(show_progress))(
"parallel_read", "Set to false to use sequential reads",
cxxopts::value<bool>(parallel_read)->default_value("true"))(
"help", "Print help");
auto result = options.parse(argc, argv);
if (result.count("help") || argc < 2) {
HelpAndQuit(options);
}
operation = argv[1];
std::cout << "operation: " << operation << std::endl;
if (operation == "help") {
HelpAndQuit(options);
} else if (operation == "create") {
cout << "Generating plot for k=" << static_cast<int>(k) << " filename=" << filename
<< " id=" << id << endl
<< endl;
id = Strip0x(id);
if (id.size() != 64) {
cout << "Invalid ID, should be 32 bytes (hex)" << endl;
exit(1);
}
memo = Strip0x(memo);
if (memo.size() % 2 != 0) {
cout << "Invalid memo, should be only whole bytes (hex)" << endl;
exit(1);
}
std::vector<uint8_t> memo_bytes(memo.size() / 2);
std::array<uint8_t, 32> id_bytes;
HexToBytes(memo, memo_bytes.data());
HexToBytes(id, id_bytes.data());
DiskPlotter plotter = DiskPlotter();
uint8_t phases_flags = 0;
if (!nobitfield) {
phases_flags = ENABLE_BITFIELD;
}
if (show_progress) {
phases_flags = phases_flags | SHOW_PROGRESS;
}
plotter.CreatePlotDisk(
tempdir,
tempdir2,
finaldir,
filename,
k,
memo_bytes.data(),
memo_bytes.size(),
id_bytes.data(),
id_bytes.size(),
buffmegabytes,
num_buckets,
num_stripes,
num_threads,
phases_flags);
} else if (operation == "prove") {
if (argc < 3) {
HelpAndQuit(options);
}
cout << "Proving using filename=" << filename << " challenge=" << argv[2] << endl
<< endl;
string challenge = Strip0x(argv[2]);
if (challenge.size() != 64) {
cout << "Invalid challenge, should be 32 bytes" << endl;
exit(1);
}
uint8_t challenge_bytes[32];
HexToBytes(challenge, challenge_bytes);
DiskProver prover(filename);
try {
vector<LargeBits> qualities = prover.GetQualitiesForChallenge(challenge_bytes);
for (uint32_t i = 0; i < qualities.size(); i++) {
k = prover.GetSize();
uint8_t *proof_data = new uint8_t[8 * k];
LargeBits proof = prover.GetFullProof(challenge_bytes, i, parallel_read);
proof.ToBytes(proof_data);
cout << "Proof: 0x" << Util::HexStr(proof_data, k * 8) << endl;
delete[] proof_data;
}
if (qualities.empty()) {
cout << "No proofs found." << endl;
exit(1);
}
} catch (const std::exception& ex) {
std::cout << "Error proving. " << ex.what() << std::endl;
exit(1);
} catch (...) {
std::cout << "Error proving. " << std::endl;
exit(1);
}
} else if (operation == "verify") {
if (argc < 4) {
HelpAndQuit(options);
}
Verifier verifier = Verifier();
id = Strip0x(id);
string proof = Strip0x(argv[2]);
string challenge = Strip0x(argv[3]);
if (id.size() != 64) {
cout << "Invalid ID, should be 32 bytes" << endl;
exit(1);
}
if (challenge.size() != 64) {
cout << "Invalid challenge, should be 32 bytes" << endl;
exit(1);
}
if (proof.size() % 16) {
cout << "Invalid proof, should be a multiple of 8 bytes" << endl;
exit(1);
}
k = proof.size() / 16;
cout << "Verifying proof=" << argv[2] << " for challenge=" << argv[3]
<< " and k=" << static_cast<int>(k) << endl
<< endl;
uint8_t id_bytes[32];
uint8_t challenge_bytes[32];
uint8_t *proof_bytes = new uint8_t[proof.size() / 2];
HexToBytes(id, id_bytes);
HexToBytes(challenge, challenge_bytes);
HexToBytes(proof, proof_bytes);
LargeBits quality =
verifier.ValidateProof(id_bytes, k, challenge_bytes, proof_bytes, k * 8);
if (quality.GetSize() == 256) {
cout << "Proof verification suceeded. Quality: " << quality << endl;
} else {
cout << "Proof verification failed." << endl;
exit(1);
}
delete[] proof_bytes;
} else if (operation == "check") {
uint32_t iterations = 1000;
if (argc == 3) {
iterations = std::stoi(argv[2]);
}
DiskProver prover(filename);
Verifier verifier = Verifier();
uint32_t success = 0;
std::vector<uint8_t> id_bytes = prover.GetId();
k = prover.GetSize();
for (uint32_t num = 0; num < iterations; num++) {
vector<unsigned char> hash_input = intToBytes(num, 4);
hash_input.insert(hash_input.end(), id_bytes.begin(), id_bytes.end());
vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(hash_input.begin(), hash_input.end(), hash.begin(), hash.end());
try {
vector<LargeBits> qualities = prover.GetQualitiesForChallenge(hash.data());
for (uint32_t i = 0; i < qualities.size(); i++) {
LargeBits proof = prover.GetFullProof(hash.data(), i, parallel_read);
uint8_t *proof_data = new uint8_t[proof.GetSize() / 8];
proof.ToBytes(proof_data);
cout << "i: " << num << std::endl;
cout << "challenge: 0x" << Util::HexStr(hash.data(), 256 / 8) << endl;
cout << "proof: 0x" << Util::HexStr(proof_data, k * 8) << endl;
LargeBits quality =
verifier.ValidateProof(id_bytes.data(), k, hash.data(), proof_data, k * 8);
if (quality.GetSize() == 256 && quality == qualities[i]) {
cout << "quality: " << quality << endl;
cout << "Proof verification succeeded. k = " << static_cast<int>(k) << endl;
success++;
} else {
cout << "Proof verification failed." << endl;
}
delete[] proof_data;
}
} catch (const std::exception& error) {
cout << "Threw: " << error.what() << endl;
continue;
}
}
std::cout << "Total success: " << success << "/" << iterations << ", "
<< (success * 100 / static_cast<double>(iterations)) << "%." << std::endl;
if (show_progress) { progress(4, 1, 1); }
} else {
cout << "Invalid operation. Use create/prove/verify/check" << endl;
}
return 0;
} catch (const cxxopts::OptionException &e) {
cout << "error parsing options: " << e.what() << endl;
return 1;
} catch (const std::exception &e) {
std::cerr << "Caught exception: " << e.what() << endl;
throw e;
}