Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

bls_dkg.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 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 
9 extern CBLSWorker blsWorker;
10 
11 struct Member {
13 
16 };
17 
18 struct DKG
19 {
20  std::vector<Member> members;
21  std::vector<CBLSId> ids;
22 
23  std::vector<BLSVerificationVectorPtr> receivedVvecs;
25 
27 
28  DKG(int quorumSize)
29  {
30  members.resize(quorumSize);
31  ids.resize(quorumSize);
32 
33  for (int i = 0; i < quorumSize; i++) {
34  members[i].id.SetInt(i + 1);
35  ids[i] = members[i].id;
36  }
37 
38  for (int i = 0; i < quorumSize; i++) {
39  blsWorker.GenerateContributions(quorumSize / 2 + 1, ids, members[i].vvec, members[i].skShares);
40  }
41 
42  //printf("initialized quorum %d\n", quorumSize);
43  }
44 
45  void ReceiveVvecs()
46  {
47  receivedVvecs.clear();
48  for (size_t i = 0; i < members.size(); i++) {
49  receivedVvecs.emplace_back(members[i].vvec);
50  }
52  }
53 
54  void ReceiveShares(size_t whoAmI)
55  {
56  receivedSkShares.clear();
57  for (size_t i = 0; i < members.size(); i++) {
58  receivedSkShares.emplace_back(members[i].skShares[whoAmI]);
59  }
60  }
61 
62  void BuildQuorumVerificationVector(bool parallel)
63  {
65  //assert(worker.VerifyVerificationVector(*members[memberIdx].quorumVvec));
66  }
67 
69  {
70  ReceiveVvecs();
71 
72  while (state.KeepRunning()) {
74  }
75  }
76 
77  void VerifyContributionShares(size_t whoAmI, const std::set<size_t>& invalidIndexes, bool parallel, bool aggregated)
78  {
79  auto result = blsWorker.VerifyContributionShares(members[whoAmI].id, receivedVvecs, receivedSkShares, parallel, aggregated);
80  for (size_t i = 0; i < receivedVvecs.size(); i++) {
81  if (invalidIndexes.count(i)) {
82  assert(!result[i]);
83  } else {
84  assert(result[i]);
85  }
86  }
87  }
88 
89  void Bench_VerifyContributionShares(benchmark::State& state, int invalidCount, bool parallel, bool aggregated)
90  {
91  ReceiveVvecs();
92 
93  // Benchmark.
94  size_t memberIdx = 0;
95  while (state.KeepRunning()) {
96  auto& m = members[memberIdx];
97 
98  ReceiveShares(memberIdx);
99 
100  std::set<size_t> invalidIndexes;
101  for (int i = 0; i < invalidCount; i++) {
102  int shareIdx = GetRandInt(receivedSkShares.size());
103  receivedSkShares[shareIdx].MakeNewKey();
104  invalidIndexes.emplace(shareIdx);
105  }
106 
107  VerifyContributionShares(memberIdx, invalidIndexes, parallel, aggregated);
108 
109  memberIdx = (memberIdx + 1) % members.size();
110  }
111  }
112 };
113 
114 std::shared_ptr<DKG> dkg10;
115 std::shared_ptr<DKG> dkg100;
116 std::shared_ptr<DKG> dkg400;
117 
119 {
120  if (dkg10 == nullptr) {
121  dkg10 = std::make_shared<DKG>(10);
122  }
123  if (dkg100 == nullptr) {
124  dkg100 = std::make_shared<DKG>(100);
125  }
126  if (dkg400 == nullptr) {
127  dkg400 = std::make_shared<DKG>(400);
128  }
129 }
130 
132 {
133  dkg10.reset();
134  dkg100.reset();
135  dkg400.reset();
136 }
137 
138 
139 
140 #define BENCH_BuildQuorumVerificationVectors(name, quorumSize, parallel) \
141  static void BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize(benchmark::State& state) \
142  { \
143  InitIfNeeded(); \
144  dkg##quorumSize->Bench_BuildQuorumVerificationVectors(state, parallel); \
145  } \
146  BENCHMARK(BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize)
147 
148 BENCH_BuildQuorumVerificationVectors(simple, 10, false)
154 
156 
157 
158 
159 #define BENCH_VerifyContributionShares(name, quorumSize, invalidCount, parallel, aggregated) \
160  static void BLSDKG_VerifyContributionShares_##name##_##quorumSize(benchmark::State& state) \
161  { \
162  InitIfNeeded(); \
163  dkg##quorumSize->Bench_VerifyContributionShares(state, invalidCount, parallel, aggregated); \
164  } \
165  BENCHMARK(BLSDKG_VerifyContributionShares_##name##_##quorumSize)
166 
167 BENCH_VerifyContributionShares(simple, 10, 5, false, false)
170 
171 BENCH_VerifyContributionShares(aggregated, 10, 5, false, true)
172 BENCH_VerifyContributionShares(aggregated, 100, 5, false, true)
173 BENCH_VerifyContributionShares(aggregated, 400, 5, false, true)
174 
175 BENCH_VerifyContributionShares(parallel, 10, 5, true, false)
177 BENCH_VerifyContributionShares(parallel, 400, 5, true, false)
178 
179 BENCH_VerifyContributionShares(parallel_aggregated, 10, 5, true, true)
180 BENCH_VerifyContributionShares(parallel_aggregated, 100, 5, true, true)
181 BENCH_VerifyContributionShares(parallel_aggregated, 400, 5, true, true)
#define BENCH_BuildQuorumVerificationVectors(name, quorumSize, parallel)
Definition: bls_dkg.cpp:140
void VerifyContributionShares(size_t whoAmI, const std::set< size_t > &invalidIndexes, bool parallel, bool aggregated)
Definition: bls_dkg.cpp:77
int GetRandInt(int nMax)
Definition: random.cpp:379
CBLSWorker blsWorker
Definition: bls.cpp:12
void ReceiveVvecs()
Definition: bls_dkg.cpp:45
BLSSecretKeyVector skShares
Definition: bls_dkg.cpp:15
bool KeepRunning()
Definition: bench.cpp:39
void Bench_VerifyContributionShares(benchmark::State &state, int invalidCount, bool parallel, bool aggregated)
Definition: bls_dkg.cpp:89
false true true true
Definition: bls_dkg.cpp:176
BLSVerificationVectorPtr BuildQuorumVerificationVector(const std::vector< BLSVerificationVectorPtr > &vvecs, size_t start=0, size_t count=0, bool parallel=true)
Definition: bls_worker.cpp:628
std::vector< CBLSSecretKey > BLSSecretKeyVector
Definition: bls.h:467
void CleanupBLSDkgTests()
Definition: bls_dkg.cpp:131
bool GenerateContributions(int threshold, const BLSIdVector &ids, BLSVerificationVectorPtr &vvecRet, BLSSecretKeyVector &skShares)
Definition: bls_worker.cpp:75
void ReceiveShares(size_t whoAmI)
Definition: bls_dkg.cpp:54
#define BENCH_VerifyContributionShares(name, quorumSize, invalidCount, parallel, aggregated)
Definition: bls_dkg.cpp:159
Definition: bls.h:218
false
Definition: bls_dkg.cpp:168
std::shared_ptr< BLSVerificationVector > BLSVerificationVectorPtr
Definition: bls.h:471
void Bench_BuildQuorumVerificationVectors(benchmark::State &state, bool parallel)
Definition: bls_dkg.cpp:68
std::vector< Member > members
Definition: bls_dkg.cpp:20
std::shared_ptr< DKG > dkg100
Definition: bls_dkg.cpp:115
BLSSecretKeyVector receivedSkShares
Definition: bls_dkg.cpp:24
std::vector< CBLSId > ids
Definition: bls_dkg.cpp:21
std::vector< bool > VerifyContributionShares(const CBLSId &forId, const std::vector< BLSVerificationVectorPtr > &vvecs, const BLSSecretKeyVector &skShares, bool parallel=true, bool aggregated=true)
Definition: bls_worker.cpp:748
std::vector< BLSVerificationVectorPtr > receivedVvecs
Definition: bls_dkg.cpp:23
void BuildQuorumVerificationVector(bool parallel)
Definition: bls_dkg.cpp:62
BLSVerificationVectorPtr quorumVvec
Definition: bls_dkg.cpp:26
std::shared_ptr< DKG > dkg400
Definition: bls_dkg.cpp:116
BLSVerificationVectorPtr vvec
Definition: bls_dkg.cpp:14
DKG(int quorumSize)
Definition: bls_dkg.cpp:28
std::shared_ptr< DKG > dkg10
Definition: bls_dkg.cpp:114
CBLSId id
Definition: bls_dkg.cpp:12
void InitIfNeeded()
Definition: bls_dkg.cpp:118
Definition: bls_dkg.cpp:18
Released under the MIT license