diff --git a/src/bls/bls.cpp b/src/bls/bls.cpp index df5fe5caf0ee..62262e37dd46 100644 --- a/src/bls/bls.cpp +++ b/src/bls/bls.cpp @@ -5,14 +5,13 @@ #include #include -#include #ifndef BUILD_BITCOIN_INTERNAL #include #endif -#include -#include +#include +#include static std::unique_ptr pSchemeLegacy(new bls::LegacySchemeMPL); static std::unique_ptr pScheme(new bls::BasicSchemeMPL); diff --git a/src/bls/bls_worker.cpp b/src/bls/bls_worker.cpp index b292dc2adacc..29440e832f1d 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -404,13 +404,13 @@ struct ContributionVerifier { BLSVerificationVectorPtr vvec; CBLSSecretKey skShare; - // starts with 0 and is incremented if either vvec or skShare aggregation finishs. If it reaches 2, we know + // starts with 0 and is incremented if either vvec or skShare aggregation finishes. If it reaches 2, we know // that aggregation for this batch is fully done. We can then start verification. std::unique_ptr > aggDone; // we can't directly update a vector in parallel // as vector is not thread safe (uses bitsets internally) - // so we must use vector temporarely and concatenate/convert + // so we must use vector temporarily and concatenate/convert // each batch result into a final vector std::vector verifyResults; }; @@ -575,7 +575,7 @@ struct ContributionVerifier { } } - bool Verify(const BLSVerificationVectorPtr& vvec, const CBLSSecretKey& skShare) + bool Verify(const BLSVerificationVectorPtr& vvec, const CBLSSecretKey& skShare) const { CBLSPublicKey pk1; if (!pk1.PublicKeyShare(*vvec, forId)) { @@ -590,7 +590,7 @@ struct ContributionVerifier { void PushOrDoWork(Callable&& f) { if (parallel) { - workerPool.push(std::move(f)); + workerPool.push(std::forward(f)); } else { f(0); } @@ -656,7 +656,7 @@ void CBLSWorker::AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, size_t start, size_t count, bool parallel, std::function doneCallback) { - AsyncAggregateHelper(workerPool, secKeys, start, count, parallel, doneCallback); + AsyncAggregateHelper(workerPool, secKeys, start, count, parallel, std::move(doneCallback)); } std::future CBLSWorker::AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, @@ -677,7 +677,7 @@ void CBLSWorker::AsyncAggregatePublicKeys(const BLSPublicKeyVector& pubKeys, size_t start, size_t count, bool parallel, std::function doneCallback) { - AsyncAggregateHelper(workerPool, pubKeys, start, count, parallel, doneCallback); + AsyncAggregateHelper(workerPool, pubKeys, start, count, parallel, std::move(doneCallback)); } std::future CBLSWorker::AsyncAggregatePublicKeys(const BLSPublicKeyVector& pubKeys, @@ -698,7 +698,7 @@ void CBLSWorker::AsyncAggregateSigs(const BLSSignatureVector& sigs, size_t start, size_t count, bool parallel, std::function doneCallback) { - AsyncAggregateHelper(workerPool, sigs, start, count, parallel, doneCallback); + AsyncAggregateHelper(workerPool, sigs, start, count, parallel, std::move(doneCallback)); } std::future CBLSWorker::AsyncAggregateSigs(const BLSSignatureVector& sigs, @@ -830,7 +830,7 @@ bool CBLSWorker::VerifySignatureVector(const BLSSignatureVector& sigs, size_t st return VerifyVectorHelper(sigs, start, count); } -void CBLSWorker::AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, CBLSWorker::SignDoneCallback doneCallback) +void CBLSWorker::AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, const CBLSWorker::SignDoneCallback& doneCallback) { workerPool.push([secKey, msgHash, doneCallback](int threadId) { doneCallback(secKey.Sign(msgHash)); @@ -840,7 +840,7 @@ void CBLSWorker::AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, std::future CBLSWorker::AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash) { auto p = BuildFutureDoneCallback(); - AsyncSign(secKey, msgHash, std::move(p.first)); + AsyncSign(secKey, msgHash, p.first); return std::move(p.second); } @@ -877,7 +877,7 @@ void CBLSWorker::AsyncVerifySig(const CBLSSignature& sig, const CBLSPublicKey& p std::future CBLSWorker::AsyncVerifySig(const CBLSSignature& sig, const CBLSPublicKey& pubKey, const uint256& msgHash, CancelCond cancelCond) { auto p = BuildFutureDoneCallback2(); - AsyncVerifySig(sig, pubKey, msgHash, std::move(p.first), cancelCond); + AsyncVerifySig(sig, pubKey, msgHash, std::move(p.first), std::move(cancelCond)); return std::move(p.second); } @@ -890,7 +890,7 @@ bool CBLSWorker::IsAsyncVerifyInProgress() // sigVerifyMutex must be held while calling void CBLSWorker::PushSigVerifyBatch() { - auto f = [this](int threadId, std::shared_ptr > _jobs) { + auto f = [this](int threadId, const std::shared_ptr >& _jobs) { auto& jobs = *_jobs; if (jobs.size() == 1) { auto& job = jobs[0]; diff --git a/src/bls/bls_worker.h b/src/bls/bls_worker.h index b8cc8c677b45..6b46ae93ceeb 100644 --- a/src/bls/bls_worker.h +++ b/src/bls/bls_worker.h @@ -12,8 +12,6 @@ #include #include -#include - // Low level BLS/DKG stuff. All very compute intensive and optimized for parallelization // The worker tries to parallelize as much as possible and utilizes a few properties of BLS aggregation to speed up things // For example, public key vectors can be aggregated in parallel if they are split into batches and the batched aggregations are @@ -69,7 +67,7 @@ class CBLSWorker // The result is in the following form: // [ a1+a2+a3+a4, b1+b2+b3+b4, c1+c2+c3+c4, d1+d2+d3+d4] // Multiple things can be parallelized here. For example, all 4 entries in the result vector can be calculated in parallel - // Also, each individual vector can be split into multiple batches and aggregating the batches can also be paralellized. + // Also, each individual vector can be split into multiple batches and aggregating the batches can also be parallelized. void AsyncBuildQuorumVerificationVector(const std::vector& vvecs, size_t start, size_t count, bool parallel, std::function doneCallback); @@ -82,7 +80,7 @@ class CBLSWorker // Inputs are in the following form: // [a, b, c, d], // The result is simply a+b+c+d - // Aggregation is paralellized by splitting up the input vector into multiple batches and then aggregating the individual batch results + // Aggregation is parallelized by splitting up the input vector into multiple batches and then aggregating the individual batch results void AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, size_t start, size_t count, bool parallel, std::function doneCallback); @@ -106,7 +104,7 @@ class CBLSWorker // Calculate public key share from public key vector and id. Not parallelized - CBLSPublicKey BuildPubKeyShare(const BLSVerificationVectorPtr& vvec, const CBLSId& id); + static CBLSPublicKey BuildPubKeyShare(const BLSVerificationVectorPtr& vvec, const CBLSId& id); // The following functions verify multiple verification vectors and contributions for the same id // This is parallelized by performing batched verification. The verification vectors and the contributions of @@ -123,17 +121,17 @@ class CBLSWorker std::future AsyncVerifyContributionShare(const CBLSId& forId, const BLSVerificationVectorPtr& vvec, const CBLSSecretKey& skContribution); - // Non paralellized verification of a single contribution - bool VerifyContributionShare(const CBLSId& forId, const BLSVerificationVectorPtr& vvec, const CBLSSecretKey& skContribution); + // Non parallelized verification of a single contribution + static bool VerifyContributionShare(const CBLSId& forId, const BLSVerificationVectorPtr& vvec, const CBLSSecretKey& skContribution); // Simple verification of vectors. Checks x.IsValid() for every entry and checks for duplicate entries - bool VerifyVerificationVector(const BLSVerificationVector& vvec, size_t start = 0, size_t count = 0); - bool VerifyVerificationVectors(const std::vector& vvecs, size_t start = 0, size_t count = 0); - bool VerifySecretKeyVector(const BLSSecretKeyVector& secKeys, size_t start = 0, size_t count = 0); - bool VerifySignatureVector(const BLSSignatureVector& sigs, size_t start = 0, size_t count = 0); + static bool VerifyVerificationVector(const BLSVerificationVector& vvec, size_t start = 0, size_t count = 0); + static bool VerifyVerificationVectors(const std::vector& vvecs, size_t start = 0, size_t count = 0); + static bool VerifySecretKeyVector(const BLSSecretKeyVector& secKeys, size_t start = 0, size_t count = 0); + static bool VerifySignatureVector(const BLSSignatureVector& sigs, size_t start = 0, size_t count = 0); // Internally batched signature signing and verification - void AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, SignDoneCallback doneCallback); + void AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, const SignDoneCallback& doneCallback); std::future AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash); void AsyncVerifySig(const CBLSSignature& sig, const CBLSPublicKey& pubKey, const uint256& msgHash, SigVerifyDoneCallback doneCallback, CancelCond cancelCond = [] { return false; }); std::future AsyncVerifySig(const CBLSSignature& sig, const CBLSPublicKey& pubKey, const uint256& msgHash, CancelCond cancelCond = [] { return false; }); @@ -176,7 +174,7 @@ class CBLSWorkerCache CBLSPublicKey BuildPubKeyShare(const uint256& cacheKey, const BLSVerificationVectorPtr& vvec, const CBLSId& id) { return GetOrBuild(cacheKey, publicKeyShareCache, [&]() { - return worker.BuildPubKeyShare(vvec, id); + return CBLSWorker::BuildPubKeyShare(vvec, id); }); }