Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

bls.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2019 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <bench/bench.h>
6 #include <random.h>
7 #include <bls/bls_worker.h>
8 #include <utiltime.h>
9 
10 #include <iostream>
11 
13 
15 {
16  blsWorker.Start();
17 }
18 
20 {
21  blsWorker.Stop();
22 }
23 
24 static void BuildTestVectors(size_t count, size_t invalidCount,
26  std::vector<uint256>& msgHashes,
27  std::vector<bool>& invalid)
28 {
29  secKeys.resize(count);
30  pubKeys.resize(count);
31  sigs.resize(count);
32  msgHashes.resize(count);
33 
34  invalid.resize(count);
35  for (size_t i = 0; i < invalidCount; i++) {
36  invalid[i] = true;
37  }
38  std::random_shuffle(invalid.begin(), invalid.end());
39 
40  for (size_t i = 0; i < count; i++) {
41  secKeys[i].MakeNewKey();
42  pubKeys[i] = secKeys[i].GetPublicKey();
43  msgHashes[i] = GetRandHash();
44  sigs[i] = secKeys[i].Sign(msgHashes[i]);
45 
46  if (invalid[i]) {
47  CBLSSecretKey s;
48  s.MakeNewKey();
49  sigs[i] = s.Sign(msgHashes[i]);
50  }
51  }
52 }
53 
55 {
56  CBLSSecretKey secKey1, secKey2;
57  secKey1.MakeNewKey();
58  secKey2.MakeNewKey();
59  CBLSPublicKey pubKey1 = secKey1.GetPublicKey();
60  CBLSPublicKey pubKey2 = secKey2.GetPublicKey();
61 
62  // Benchmark.
63  while (state.KeepRunning()) {
64  CBLSPublicKey k(pubKey1);
65  k.AggregateInsecure(pubKey2);
66  }
67 }
68 
70 {
71  CBLSSecretKey secKey1, secKey2;
72  secKey1.MakeNewKey();
73  secKey2.MakeNewKey();
74  CBLSPublicKey pubKey1 = secKey1.GetPublicKey();
75  CBLSPublicKey pubKey2 = secKey2.GetPublicKey();
76 
77  // Benchmark.
78  while (state.KeepRunning()) {
79  CBLSSecretKey k(secKey1);
80  k.AggregateInsecure(secKey2);
81  }
82 }
83 
84 static void BLSSign_Normal(benchmark::State& state)
85 {
86  CBLSSecretKey secKey;
87  secKey.MakeNewKey();
88  CBLSPublicKey pubKey = secKey.GetPublicKey();
89 
90  // Benchmark.
91  while (state.KeepRunning()) {
92  uint256 hash = GetRandHash();
93  secKey.Sign(hash);
94  }
95 }
96 
98 {
99  BLSPublicKeyVector pubKeys;
100  BLSSecretKeyVector secKeys;
101  BLSSignatureVector sigs;
102  std::vector<uint256> msgHashes;
103  std::vector<bool> invalid;
104  BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
105 
106  // Benchmark.
107  size_t i = 0;
108  while (state.KeepRunning()) {
109  bool valid = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
110  if (valid && invalid[i]) {
111  std::cout << "expected invalid but it is valid" << std::endl;
112  assert(false);
113  } else if (!valid && !invalid[i]) {
114  std::cout << "expected valid but it is invalid" << std::endl;
115  assert(false);
116  }
117  i = (i + 1) % pubKeys.size();
118  }
119 }
120 
121 
122 static void BLSVerify_LargeBlock(size_t txCount, benchmark::State& state)
123 {
124  BLSPublicKeyVector pubKeys;
125  BLSSecretKeyVector secKeys;
126  BLSSignatureVector sigs;
127  std::vector<uint256> msgHashes;
128  std::vector<bool> invalid;
129  BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
130 
131  // Benchmark.
132  while (state.KeepRunning()) {
133  for (size_t i = 0; i < pubKeys.size(); i++) {
134  sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
135  }
136  }
137 }
138 
140 {
141  BLSVerify_LargeBlock(1000, state);
142 }
143 
145 {
146  BLSVerify_LargeBlock(10000, state);
147 }
148 
149 static void BLSVerify_LargeBlockSelfAggregated(size_t txCount, benchmark::State& state)
150 {
151  BLSPublicKeyVector pubKeys;
152  BLSSecretKeyVector secKeys;
153  BLSSignatureVector sigs;
154  std::vector<uint256> msgHashes;
155  std::vector<bool> invalid;
156  BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
157 
158  // Benchmark.
159  while (state.KeepRunning()) {
161  aggSig.VerifyInsecureAggregated(pubKeys, msgHashes);
162  }
163 }
164 
166 {
168 }
169 
171 {
173 }
174 
175 static void BLSVerify_LargeAggregatedBlock(size_t txCount, benchmark::State& state)
176 {
177  BLSPublicKeyVector pubKeys;
178  BLSSecretKeyVector secKeys;
179  BLSSignatureVector sigs;
180  std::vector<uint256> msgHashes;
181  std::vector<bool> invalid;
182  BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
183 
185 
186  // Benchmark.
187  while (state.KeepRunning()) {
188  aggSig.VerifyInsecureAggregated(pubKeys, msgHashes);
189  }
190 }
191 
193 {
194  BLSVerify_LargeAggregatedBlock(1000, state);
195 }
196 
198 {
199  BLSVerify_LargeAggregatedBlock(10000, state);
200 }
201 
203 {
204  BLSPublicKeyVector pubKeys;
205  BLSSecretKeyVector secKeys;
206  BLSSignatureVector sigs;
207  std::vector<uint256> msgHashes;
208  std::vector<bool> invalid;
209  BuildTestVectors(1000, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
210 
212 
213  std::set<size_t> prevalidated;
214 
215  while (prevalidated.size() < 900) {
216  int idx = GetRandInt((int)pubKeys.size());
217  if (prevalidated.count((size_t)idx)) {
218  continue;
219  }
220  prevalidated.emplace((size_t)idx);
221  }
222 
223  // Benchmark.
224  while (state.KeepRunning()) {
225  BLSPublicKeyVector nonvalidatedPubKeys;
226  std::vector<uint256> nonvalidatedHashes;
227  nonvalidatedPubKeys.reserve(pubKeys.size());
228  nonvalidatedHashes.reserve(msgHashes.size());
229 
230  for (size_t i = 0; i < sigs.size(); i++) {
231  if (prevalidated.count(i)) {
232  continue;
233  }
234  nonvalidatedPubKeys.emplace_back(pubKeys[i]);
235  nonvalidatedHashes.emplace_back(msgHashes[i]);
236  }
237 
238  CBLSSignature aggSigCopy = aggSig;
239  for (auto idx : prevalidated) {
240  aggSigCopy.SubInsecure(sigs[idx]);
241  }
242 
243  bool valid = aggSigCopy.VerifyInsecureAggregated(nonvalidatedPubKeys, nonvalidatedHashes);
244  assert(valid);
245  }
246 }
247 
249 {
250  BLSPublicKeyVector pubKeys;
251  BLSSecretKeyVector secKeys;
252  BLSSignatureVector sigs;
253  std::vector<uint256> msgHashes;
254  std::vector<bool> invalid;
255  BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
256 
257  // Benchmark.
258  size_t i = 0;
259  size_t j = 0;
260  size_t batchSize = 16;
261  while (state.KeepRunning()) {
262  j++;
263  if ((j % batchSize) != 0) {
264  continue;
265  }
266 
267  BLSPublicKeyVector testPubKeys;
268  BLSSignatureVector testSigs;
269  std::vector<uint256> testMsgHashes;
270  testPubKeys.reserve(batchSize);
271  testSigs.reserve(batchSize);
272  testMsgHashes.reserve(batchSize);
273  size_t startI = i;
274  for (size_t k = 0; k < batchSize; k++) {
275  testPubKeys.emplace_back(pubKeys[i]);
276  testSigs.emplace_back(sigs[i]);
277  testMsgHashes.emplace_back(msgHashes[i]);
278  i = (i + 1) % pubKeys.size();
279  }
280 
281  CBLSSignature batchSig = CBLSSignature::AggregateInsecure(testSigs);
282  bool batchValid = batchSig.VerifyInsecureAggregated(testPubKeys, testMsgHashes);
283  std::vector<bool> valid;
284  if (batchValid) {
285  valid.assign(batchSize, true);
286  } else {
287  for (size_t k = 0; k < batchSize; k++) {
288  bool valid1 = testSigs[k].VerifyInsecure(testPubKeys[k], testMsgHashes[k]);
289  valid.emplace_back(valid1);
290  }
291  }
292  for (size_t k = 0; k < batchSize; k++) {
293  if (valid[k] && invalid[(startI + k) % pubKeys.size()]) {
294  std::cout << "expected invalid but it is valid" << std::endl;
295  assert(false);
296  } else if (!valid[k] && !invalid[(startI + k) % pubKeys.size()]) {
297  std::cout << "expected valid but it is invalid" << std::endl;
298  assert(false);
299  }
300  }
301  }
302 }
303 
305 {
306  BLSPublicKeyVector pubKeys;
307  BLSSecretKeyVector secKeys;
308  BLSSignatureVector sigs;
309  std::vector<uint256> msgHashes;
310  std::vector<bool> invalid;
311  BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
312 
313  std::list<std::pair<size_t, std::future<bool>>> futures;
314 
315  volatile bool cancel = false;
316  auto cancelCond = [&]() {
317  return cancel;
318  };
319 
320  // Benchmark.
321  size_t i = 0;
322  while (state.KeepRunning()) {
323  if (futures.size() < 100) {
324  while (futures.size() < 10000) {
325  auto f = blsWorker.AsyncVerifySig(sigs[i], pubKeys[i], msgHashes[i], cancelCond);
326  futures.emplace_back(std::make_pair(i, std::move(f)));
327  i = (i + 1) % pubKeys.size();
328  }
329  }
330 
331  auto fp = std::move(futures.front());
332  futures.pop_front();
333 
334  size_t j = fp.first;
335  bool valid = fp.second.get();
336 
337  if (valid && invalid[j]) {
338  std::cout << "expected invalid but it is valid" << std::endl;
339  assert(false);
340  } else if (!valid && !invalid[j]) {
341  std::cout << "expected valid but it is invalid" << std::endl;
342  assert(false);
343  }
344  }
345  cancel = true;
347  MilliSleep(100);
348  }
349 }
350 
static void BLSVerify_LargeAggregatedBlock10000(benchmark::State &state)
Definition: bls.cpp:197
static void BLSVerify_LargeAggregatedBlock1000PreVerified(benchmark::State &state)
Definition: bls.cpp:202
#define BENCHMARK(n)
Definition: bench.h:92
std::vector< CBLSPublicKey > BLSPublicKeyVector
Definition: bls.h:466
uint256 GetRandHash()
Definition: random.cpp:384
static void BLSVerify_LargeBlockSelfAggregated(size_t txCount, benchmark::State &state)
Definition: bls.cpp:149
void MilliSleep(int64_t n)
Definition: utiltime.cpp:75
int GetRandInt(int nMax)
Definition: random.cpp:379
static void BLSVerify_LargeBlockSelfAggregated1000(benchmark::State &state)
Definition: bls.cpp:165
static void BLSVerify_Batched(benchmark::State &state)
Definition: bls.cpp:248
void SubInsecure(const CBLSSignature &o)
Definition: bls.cpp:328
static void BLSVerify_LargeBlock(size_t txCount, benchmark::State &state)
Definition: bls.cpp:122
static void BLSVerify_LargeAggregatedBlock(size_t txCount, benchmark::State &state)
Definition: bls.cpp:175
bool KeepRunning()
Definition: bench.cpp:39
static void BuildTestVectors(size_t count, size_t invalidCount, BLSPublicKeyVector &pubKeys, BLSSecretKeyVector &secKeys, BLSSignatureVector &sigs, std::vector< uint256 > &msgHashes, std::vector< bool > &invalid)
Definition: bls.cpp:24
void AggregateInsecure(const CBLSSecretKey &o)
Definition: bls.cpp:74
std::vector< CBLSSecretKey > BLSSecretKeyVector
Definition: bls.h:467
void CleanupBLSTests()
Definition: bls.cpp:19
void MakeNewKey()
Definition: bls.cpp:102
bool VerifyInsecureAggregated(const std::vector< CBLSPublicKey > &pubKeys, const std::vector< uint256 > &hashes) const
Definition: bls.cpp:348
CBLSWorker blsWorker
Definition: bls.cpp:12
std::vector< CBLSSignature > BLSSignatureVector
Definition: bls.h:468
static void BLSVerify_LargeBlockSelfAggregated10000(benchmark::State &state)
Definition: bls.cpp:170
static void BLSVerify_LargeAggregatedBlock1000(benchmark::State &state)
Definition: bls.cpp:192
static void BLSVerify_LargeBlock1000(benchmark::State &state)
Definition: bls.cpp:139
void Stop()
Definition: bls_worker.cpp:69
static void BLSPubKeyAggregate_Normal(benchmark::State &state)
Definition: bls.cpp:54
void Start()
Definition: bls_worker.cpp:61
void InitBLSTests()
Definition: bls.cpp:14
256-bit opaque blob.
Definition: uint256.h:123
bool IsAsyncVerifyInProgress()
Definition: bls_worker.cpp:884
void AsyncVerifySig(const CBLSSignature &sig, const CBLSPublicKey &pubKey, const uint256 &msgHash, SigVerifyDoneCallback doneCallback, CancelCond cancelCond=[] { return false;})
Definition: bls_worker.cpp:847
static void BLSVerify_BatchedParallel(benchmark::State &state)
Definition: bls.cpp:304
void AggregateInsecure(const CBLSPublicKey &o)
Definition: bls.cpp:191
void AggregateInsecure(const CBLSSignature &o)
Definition: bls.cpp:277
static void BLSVerify_Normal(benchmark::State &state)
Definition: bls.cpp:97
CBLSPublicKey GetPublicKey() const
Definition: bls.cpp:147
static int count
Definition: tests.c:45
static void BLSSign_Normal(benchmark::State &state)
Definition: bls.cpp:84
static void BLSVerify_LargeBlock10000(benchmark::State &state)
Definition: bls.cpp:144
static void BLSSecKeyAggregate_Normal(benchmark::State &state)
Definition: bls.cpp:69
CBLSSignature Sign(const uint256 &hash) const
Definition: bls.cpp:160
Released under the MIT license