forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbls.cpp
More file actions
377 lines (322 loc) · 12.1 KB
/
bls.cpp
File metadata and controls
377 lines (322 loc) · 12.1 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) 2018-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bench/bench.h>
#include <bls/bls_worker.h>
#include <llmq/options.h>
#include <random.h>
#include <util/time.h>
#include <atomic>
#include <iostream>
static void BuildTestVectors(size_t count, size_t invalidCount,
std::vector<CBLSPublicKey>& pubKeys, std::vector<CBLSSecretKey>& secKeys, std::vector<CBLSSignature>& sigs,
std::vector<uint256>& msgHashes,
std::vector<bool>& invalid)
{
secKeys.resize(count);
pubKeys.resize(count);
sigs.resize(count);
msgHashes.resize(count);
invalid.resize(count);
for (size_t i = 0; i < invalidCount; i++) {
invalid[i] = true;
}
Shuffle(invalid.begin(), invalid.end(), FastRandomContext());
for (size_t i = 0; i < count; i++) {
secKeys[i].MakeNewKey();
pubKeys[i] = secKeys[i].GetPublicKey();
msgHashes[i] = GetRandHash();
sigs[i] = secKeys[i].Sign(msgHashes[i], false);
if (invalid[i]) {
CBLSSecretKey s;
s.MakeNewKey();
sigs[i] = s.Sign(msgHashes[i], false);
}
}
}
static void BLS_PubKeyAggregate_Normal(benchmark::Bench& bench)
{
CBLSSecretKey secKey1, secKey2;
secKey1.MakeNewKey();
secKey2.MakeNewKey();
CBLSPublicKey pubKey1 = secKey1.GetPublicKey();
CBLSPublicKey pubKey2 = secKey2.GetPublicKey();
// Benchmark.
bench.minEpochIterations(bench.output() ? 100 : 1).run([&] {
pubKey1.AggregateInsecure(pubKey2);
});
}
static void BLS_SecKeyAggregate_Normal(benchmark::Bench& bench)
{
CBLSSecretKey secKey1, secKey2;
secKey1.MakeNewKey();
secKey2.MakeNewKey();
// Benchmark.
bench.run([&] {
secKey1.AggregateInsecure(secKey2);
});
}
static void BLS_SignatureAggregate_Normal(benchmark::Bench& bench)
{
uint256 hash = GetRandHash();
CBLSSecretKey secKey1, secKey2;
secKey1.MakeNewKey();
secKey2.MakeNewKey();
CBLSSignature sig1 = secKey1.Sign(hash, false);
CBLSSignature sig2 = secKey2.Sign(hash, false);
// Benchmark.
bench.run([&] {
sig1.AggregateInsecure(sig2);
});
}
static void BLS_Sign_Normal(benchmark::Bench& bench)
{
CBLSSecretKey secKey;
secKey.MakeNewKey();
CBLSSignature sig;
// Benchmark.
bench.minEpochIterations(100).run([&] {
uint256 hash = GetRandHash();
sig = secKey.Sign(hash, false);
});
}
static void BLS_Verify_Normal(benchmark::Bench& bench)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
// Benchmark.
size_t i = 0;
bench.minEpochIterations(20).run([&] {
bool valid = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
if (valid && invalid[i]) {
std::cout << "expected invalid but it is valid" << std::endl;
assert(false);
} else if (!valid && !invalid[i]) {
std::cout << "expected valid but it is invalid" << std::endl;
assert(false);
}
i = (i + 1) % pubKeys.size();
});
}
static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(bench.output() ? txCount : 1, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
// Benchmark.
bench.minEpochIterations(bench.output() ? epoch_iters : 1).run([&] {
for (size_t i = 0; i < pubKeys.size(); i++) {
bool ok = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
assert(ok);
}
});
}
static void BLS_Verify_LargeBlock100(benchmark::Bench& bench)
{
BLS_Verify_LargeBlock(100, bench, 10);
}
static void BLS_Verify_LargeBlock1000(benchmark::Bench& bench)
{
BLS_Verify_LargeBlock(1000, bench, 1);
}
static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(bench.output() ? txCount : 1, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
// Benchmark.
bench.minEpochIterations(bench.output() ? epoch_iters : 1).run([&] {
CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs);
bool ok = aggSig.VerifyInsecureAggregated(pubKeys, msgHashes);
assert(ok);
});
}
static void BLS_Verify_LargeBlockSelfAggregated100(benchmark::Bench& bench)
{
BLS_Verify_LargeBlockSelfAggregated(100, bench, 10);
}
static void BLS_Verify_LargeBlockSelfAggregated1000(benchmark::Bench& bench)
{
BLS_Verify_LargeBlockSelfAggregated(1000, bench, 2);
}
static void BLS_Verify_LargeAggregatedBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(bench.output() ? txCount : 1, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs);
// Benchmark.
bench.minEpochIterations(bench.output() ? epoch_iters : 1).run([&] {
bool ok = aggSig.VerifyInsecureAggregated(pubKeys, msgHashes);
assert(ok);
});
}
static void BLS_Verify_LargeAggregatedBlock100(benchmark::Bench& bench)
{
BLS_Verify_LargeAggregatedBlock(100, bench, 10);
}
static void BLS_Verify_LargeAggregatedBlock1000(benchmark::Bench& bench)
{
BLS_Verify_LargeAggregatedBlock(1000, bench, 1);
}
static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& bench)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(1000, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs);
std::set<size_t> prevalidated;
while (prevalidated.size() < 900) {
size_t idx = GetRand<size_t>(pubKeys.size());
if (prevalidated.count(idx)) {
continue;
}
prevalidated.emplace(idx);
}
// Benchmark.
bench.minEpochIterations(bench.output() ? 10 : 1).run([&] {
std::vector<CBLSPublicKey> nonvalidatedPubKeys;
std::vector<uint256> nonvalidatedHashes;
nonvalidatedPubKeys.reserve(pubKeys.size());
nonvalidatedHashes.reserve(msgHashes.size());
for (size_t i = 0; i < sigs.size(); i++) {
if (prevalidated.count(i)) {
continue;
}
nonvalidatedPubKeys.emplace_back(pubKeys[i]);
nonvalidatedHashes.emplace_back(msgHashes[i]);
}
CBLSSignature aggSigCopy = aggSig;
for (auto idx : prevalidated) {
aggSigCopy.SubInsecure(sigs[idx]);
}
bool valid = aggSigCopy.VerifyInsecureAggregated(nonvalidatedPubKeys, nonvalidatedHashes);
assert(valid);
});
}
static void BLS_Verify_Batched(benchmark::Bench& bench)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(bench.output() ? 1000 : 1, bench.output() ? 10 : 1, pubKeys, secKeys, sigs, msgHashes, invalid);
// Benchmark.
size_t i = 0;
size_t j = 0;
size_t batchSize = 16;
bench.minEpochIterations(bench.output() ? 1000 : 1).run([&] {
j++;
if ((j % batchSize) != 0) {
return;
}
std::vector<CBLSPublicKey> testPubKeys;
std::vector<CBLSSignature> testSigs;
std::vector<uint256> testMsgHashes;
testPubKeys.reserve(batchSize);
testSigs.reserve(batchSize);
testMsgHashes.reserve(batchSize);
size_t startI = i;
for (size_t k = 0; k < batchSize; k++) {
testPubKeys.emplace_back(pubKeys[i]);
testSigs.emplace_back(sigs[i]);
testMsgHashes.emplace_back(msgHashes[i]);
i = (i + 1) % pubKeys.size();
}
CBLSSignature batchSig = CBLSSignature::AggregateInsecure(testSigs);
bool batchValid = batchSig.VerifyInsecureAggregated(testPubKeys, testMsgHashes);
std::vector<bool> valid;
if (batchValid) {
valid.assign(batchSize, true);
} else {
for (size_t k = 0; k < batchSize; k++) {
bool valid1 = testSigs[k].VerifyInsecure(testPubKeys[k], testMsgHashes[k]);
valid.emplace_back(valid1);
}
}
for (size_t k = 0; k < batchSize; k++) {
if (valid[k] && invalid[(startI + k) % pubKeys.size()]) {
std::cout << "expected invalid but it is valid" << std::endl;
assert(false);
} else if (!valid[k] && !invalid[(startI + k) % pubKeys.size()]) {
std::cout << "expected valid but it is invalid" << std::endl;
assert(false);
}
}
});
}
static void BLS_Verify_BatchedParallel(benchmark::Bench& bench)
{
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(bench.output() ? 1000 : 1, bench.output() ? 10 : 1, pubKeys, secKeys, sigs, msgHashes, invalid);
std::list<std::pair<size_t, std::future<bool>>> futures;
std::atomic<bool> cancel{false};
auto cancelCond = [&]() -> bool {
return cancel.load();
};
CBLSWorker blsWorker;
blsWorker.Start(llmq::DEFAULT_WORKER_COUNT);
// Benchmark.
bench.minEpochIterations(bench.output() ? 1000 : 1).run([&] {
if (futures.size() < 100) {
while (futures.size() < 10000) {
size_t i = 0;
auto f = blsWorker.AsyncVerifySig(sigs[i], pubKeys[i], msgHashes[i], cancelCond);
futures.emplace_back(std::make_pair(i, std::move(f)));
i = (i + 1) % pubKeys.size();
}
}
auto fp = std::move(futures.front());
futures.pop_front();
size_t j = fp.first;
bool valid = fp.second.get();
if (valid && invalid[j]) {
std::cout << "expected invalid but it is valid" << std::endl;
assert(false);
} else if (!valid && !invalid[j]) {
std::cout << "expected valid but it is invalid" << std::endl;
assert(false);
}
});
cancel.store(true);
while (blsWorker.IsAsyncVerifyInProgress())
{
UninterruptibleSleep(std::chrono::milliseconds{100});
}
blsWorker.Stop();
}
BENCHMARK(BLS_PubKeyAggregate_Normal, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_SecKeyAggregate_Normal, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_SignatureAggregate_Normal, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Sign_Normal, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_Normal, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeBlock100, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeBlock1000, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeBlockSelfAggregated100, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeBlockSelfAggregated1000, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeAggregatedBlock100, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeAggregatedBlock1000, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_LargeAggregatedBlock1000PreVerified, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_Batched, benchmark::PriorityLevel::HIGH)
BENCHMARK(BLS_Verify_BatchedParallel, benchmark::PriorityLevel::HIGH)