From 58d3e1381bc69715d69a59d0ccd554f7ae6fdffb Mon Sep 17 00:00:00 2001 From: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com> Date: Sun, 12 Dec 2021 08:37:26 -0500 Subject: [PATCH] refactor: modifications to GenerateContributions (#4594) * modifications to GenerateContributions * Introduce some benchmarks to ensure there's no regression * minor fixups Signed-off-by: pasta --- src/bench/bls_dkg.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ src/bls/bls_worker.cpp | 21 ++++++++----------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/bench/bls_dkg.cpp b/src/bench/bls_dkg.cpp index ae1144e58b..16a5c89bf3 100644 --- a/src/bench/bls_dkg.cpp +++ b/src/bench/bls_dkg.cpp @@ -106,6 +106,47 @@ public: } }; +static void BLSDKG_GenerateContributions(benchmark::Bench& bench, uint32_t epoch_iters, int quorumSize) +{ + CBLSWorker blsWorker; + blsWorker.Start(); + std::vector ids; + std::vector 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) \ static void BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize(benchmark::Bench& bench) \ { \ @@ -124,6 +165,12 @@ public: } \ 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, 100, 10) BENCH_BuildQuorumVerificationVectors(simple, 400, 1) diff --git a/src/bls/bls_worker.cpp b/src/bls/bls_worker.cpp index 9ddaa1c7a8..0bf5a6a34d 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -75,22 +75,23 @@ void CBLSWorker::Stop() bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& ids, BLSVerificationVectorPtr& vvecRet, BLSSecretKeyVector& skSharesRet) { - auto svec = std::make_shared((size_t)quorumThreshold); + auto svec = BLSSecretKeyVector((size_t)quorumThreshold); vvecRet = std::make_shared((size_t)quorumThreshold); skSharesRet.resize(ids.size()); for (int i = 0; i < quorumThreshold; i++) { - (*svec)[i].MakeNewKey(); + svec[i].MakeNewKey(); } - std::list > futures; size_t batchSize = 8; + std::vector> futures; + futures.reserve((quorumThreshold / batchSize + ids.size() / batchSize) + 2); for (size_t i = 0; i < quorumThreshold; i += batchSize) { size_t start = i; size_t count = std::min(batchSize, quorumThreshold - start); auto f = [&, start, count](int threadId) { for (size_t j = start; j < start + count; j++) { - (*vvecRet)[j] = (*svec)[j].GetPublicKey(); + (*vvecRet)[j] = svec[j].GetPublicKey(); } return true; }; @@ -102,7 +103,7 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& i size_t count = std::min(batchSize, ids.size() - start); auto f = [&, start, count](int threadId) { 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; } } @@ -110,13 +111,9 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& i }; futures.emplace_back(workerPool.push(f)); } - bool success = true; - for (auto& f : futures) { - if (!f.get()) { - success = false; - } - } - return success; + return std::all_of(futures.begin(), futures.end(), [](auto& f){ + return f.get(); + }); } // aggregates a single vector of BLS objects in parallel