refactor: modifications to GenerateContributions (#4594)

* modifications to GenerateContributions

* Introduce some benchmarks to ensure there's no regression

* minor fixups

Signed-off-by: pasta <pasta@dashboost.org>
This commit is contained in:
PastaPastaPasta 2021-12-12 08:37:26 -05:00 committed by GitHub
parent 8f36cfe236
commit 58d3e1381b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 12 deletions

View File

@ -106,6 +106,47 @@ public:
} }
}; };
static void BLSDKG_GenerateContributions(benchmark::Bench& bench, uint32_t epoch_iters, int quorumSize)
{
CBLSWorker blsWorker;
blsWorker.Start();
std::vector<CBLSId> ids;
std::vector<Member> members;
for (int i = 0; i < quorumSize; i++) {
uint256 id;
WriteLE64(id.begin(), i + 1);
members.push_back({CBLSId(id), {}, {}});
ids.emplace_back(id);
}
bench.minEpochIterations(epoch_iters).run([&blsWorker, &quorumSize, &ids, &members] {
for (int i = 0; i < quorumSize; i++) {
blsWorker.GenerateContributions(quorumSize / 2 + 1, ids, members[i].vvec, members[i].skShares);
}
});
blsWorker.Stop();
}
#define BENCH_GenerateContributions(name, quorumSize, epoch_iters) \
static void BLSDKG_GenerateContributions_##name##_##quorumSize(benchmark::Bench& bench) \
{ \
BLSDKG_InitDKG(bench, epoch_iters, quorumSize); \
} \
BENCHMARK(BLSDKG_GenerateContributions_##name##_##quorumSize)
static void BLSDKG_InitDKG(benchmark::Bench& bench, uint32_t epoch_iters, int quorumSize)
{
bench.minEpochIterations(epoch_iters).run([&] {
DKG d(quorumSize);
});
}
#define BENCH_InitDKG(name, quorumSize, epoch_iters) \
static void BLSDKG_InitDKG_##name##_##quorumSize(benchmark::Bench& bench) \
{ \
BLSDKG_InitDKG(bench, epoch_iters, quorumSize); \
} \
BENCHMARK(BLSDKG_InitDKG_##name##_##quorumSize)
#define BENCH_BuildQuorumVerificationVectors(name, quorumSize, epoch_iters) \ #define BENCH_BuildQuorumVerificationVectors(name, quorumSize, epoch_iters) \
static void BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize(benchmark::Bench& bench) \ static void BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize(benchmark::Bench& bench) \
{ \ { \
@ -124,6 +165,12 @@ public:
} \ } \
BENCHMARK(BLSDKG_VerifyContributionShares_##name##_##quorumSize) BENCHMARK(BLSDKG_VerifyContributionShares_##name##_##quorumSize)
BENCH_GenerateContributions(simple, 10, 50);
BENCH_GenerateContributions(simple, 50, 5);
BENCH_InitDKG(simple, 10, 100)
BENCH_InitDKG(simple, 50, 10)
BENCH_BuildQuorumVerificationVectors(simple, 10, 1000) BENCH_BuildQuorumVerificationVectors(simple, 10, 1000)
BENCH_BuildQuorumVerificationVectors(simple, 100, 10) BENCH_BuildQuorumVerificationVectors(simple, 100, 10)
BENCH_BuildQuorumVerificationVectors(simple, 400, 1) BENCH_BuildQuorumVerificationVectors(simple, 400, 1)

View File

@ -75,22 +75,23 @@ void CBLSWorker::Stop()
bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& ids, BLSVerificationVectorPtr& vvecRet, BLSSecretKeyVector& skSharesRet) bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& ids, BLSVerificationVectorPtr& vvecRet, BLSSecretKeyVector& skSharesRet)
{ {
auto svec = std::make_shared<BLSSecretKeyVector>((size_t)quorumThreshold); auto svec = BLSSecretKeyVector((size_t)quorumThreshold);
vvecRet = std::make_shared<BLSVerificationVector>((size_t)quorumThreshold); vvecRet = std::make_shared<BLSVerificationVector>((size_t)quorumThreshold);
skSharesRet.resize(ids.size()); skSharesRet.resize(ids.size());
for (int i = 0; i < quorumThreshold; i++) { for (int i = 0; i < quorumThreshold; i++) {
(*svec)[i].MakeNewKey(); svec[i].MakeNewKey();
} }
std::list<std::future<bool> > futures;
size_t batchSize = 8; size_t batchSize = 8;
std::vector<std::future<bool>> futures;
futures.reserve((quorumThreshold / batchSize + ids.size() / batchSize) + 2);
for (size_t i = 0; i < quorumThreshold; i += batchSize) { for (size_t i = 0; i < quorumThreshold; i += batchSize) {
size_t start = i; size_t start = i;
size_t count = std::min(batchSize, quorumThreshold - start); size_t count = std::min(batchSize, quorumThreshold - start);
auto f = [&, start, count](int threadId) { auto f = [&, start, count](int threadId) {
for (size_t j = start; j < start + count; j++) { for (size_t j = start; j < start + count; j++) {
(*vvecRet)[j] = (*svec)[j].GetPublicKey(); (*vvecRet)[j] = svec[j].GetPublicKey();
} }
return true; return true;
}; };
@ -102,7 +103,7 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& i
size_t count = std::min(batchSize, ids.size() - start); size_t count = std::min(batchSize, ids.size() - start);
auto f = [&, start, count](int threadId) { auto f = [&, start, count](int threadId) {
for (size_t j = start; j < start + count; j++) { for (size_t j = start; j < start + count; j++) {
if (!skSharesRet[j].SecretKeyShare(*svec, ids[j])) { if (!skSharesRet[j].SecretKeyShare(svec, ids[j])) {
return false; return false;
} }
} }
@ -110,13 +111,9 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& i
}; };
futures.emplace_back(workerPool.push(f)); futures.emplace_back(workerPool.push(f));
} }
bool success = true; return std::all_of(futures.begin(), futures.end(), [](auto& f){
for (auto& f : futures) { return f.get();
if (!f.get()) { });
success = false;
}
}
return success;
} }
// aggregates a single vector of BLS objects in parallel // aggregates a single vector of BLS objects in parallel