Skip to content

Commit

Permalink
refactor: further spanification of Dash code (dashpay#5586)
Browse files Browse the repository at this point in the history
## Issue being fixed or feature implemented
Use Spans instead of const std::vector<T>&

## What was done?
Replaced with Span

## How Has This Been Tested?
Building, ran a few tests

## Breaking Changes
Should be none, please review potential lifetime issues in bls_worker;
it scares me a bit and I don't understand how we know these won't
dangle.

## Checklist:
_Go over all the following points, and put an `x` in all the boxes that
apply._
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
- [x] I have assigned this pull request to a milestone _(for repository
code-owners and collaborators only)_
  • Loading branch information
PastaPastaPasta authored Oct 3, 2023
1 parent e72eb40 commit b27765f
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 123 deletions.
50 changes: 25 additions & 25 deletions src/bench/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <iostream>

static void BuildTestVectors(size_t count, size_t invalidCount,
BLSPublicKeyVector& pubKeys, BLSSecretKeyVector& secKeys, BLSSignatureVector& sigs,
std::vector<CBLSPublicKey>& pubKeys, std::vector<CBLSSecretKey>& secKeys, std::vector<CBLSSignature>& sigs,
std::vector<uint256>& msgHashes,
std::vector<bool>& invalid)
{
Expand Down Expand Up @@ -95,9 +95,9 @@ static void BLS_Sign_Normal(benchmark::Bench& bench)

static void BLS_Verify_Normal(benchmark::Bench& bench)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand All @@ -120,9 +120,9 @@ static void BLS_Verify_Normal(benchmark::Bench& bench)

static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand All @@ -148,9 +148,9 @@ static void BLS_Verify_LargeBlock1000(benchmark::Bench& bench)

static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand All @@ -175,9 +175,9 @@ static void BLS_Verify_LargeBlockSelfAggregated1000(benchmark::Bench& bench)

static void BLS_Verify_LargeAggregatedBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand All @@ -203,9 +203,9 @@ static void BLS_Verify_LargeAggregatedBlock1000(benchmark::Bench& bench)

static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& bench)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(1000, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand All @@ -224,7 +224,7 @@ static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& ben

// Benchmark.
bench.minEpochIterations(10).run([&] {
BLSPublicKeyVector nonvalidatedPubKeys;
std::vector<CBLSPublicKey> nonvalidatedPubKeys;
std::vector<uint256> nonvalidatedHashes;
nonvalidatedPubKeys.reserve(pubKeys.size());
nonvalidatedHashes.reserve(msgHashes.size());
Expand All @@ -249,9 +249,9 @@ static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& ben

static void BLS_Verify_Batched(benchmark::Bench& bench)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand All @@ -266,8 +266,8 @@ static void BLS_Verify_Batched(benchmark::Bench& bench)
return;
}

BLSPublicKeyVector testPubKeys;
BLSSignatureVector testSigs;
std::vector<CBLSPublicKey> testPubKeys;
std::vector<CBLSSignature> testSigs;
std::vector<uint256> testMsgHashes;
testPubKeys.reserve(batchSize);
testSigs.reserve(batchSize);
Expand Down Expand Up @@ -305,9 +305,9 @@ static void BLS_Verify_Batched(benchmark::Bench& bench)

static void BLS_Verify_BatchedParallel(benchmark::Bench& bench)
{
BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys;
BLSSignatureVector sigs;
std::vector<CBLSPublicKey> pubKeys;
std::vector<CBLSSecretKey> secKeys;
std::vector<CBLSSignature> sigs;
std::vector<uint256> msgHashes;
std::vector<bool> invalid;
BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid);
Expand Down
4 changes: 2 additions & 2 deletions src/bench/bls_dkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Member {
CBLSId id;

BLSVerificationVectorPtr vvec;
BLSSecretKeyVector skShares;
std::vector<CBLSSecretKey> skShares;
};

class DKG
Expand All @@ -21,7 +21,7 @@ class DKG
std::vector<Member> members;
std::vector<BLSVerificationVectorPtr> receivedVvecs;

BLSSecretKeyVector receivedSkShares;
std::vector<CBLSSecretKey> receivedSkShares;
BLSVerificationVectorPtr quorumVvec;
CBLSWorker blsWorker;

Expand Down
16 changes: 5 additions & 11 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CBLSWrapper
static constexpr size_t SerSize = _SerSize;

explicit CBLSWrapper() = default;
explicit CBLSWrapper(const std::vector<unsigned char>& vecBytes) : CBLSWrapper<ImplType, _SerSize, C>()
explicit CBLSWrapper(Span<const unsigned char> vecBytes) : CBLSWrapper<ImplType, _SerSize, C>()
{
SetByteVector(vecBytes, bls::bls_legacy_scheme.load());
}
Expand Down Expand Up @@ -103,7 +103,7 @@ class CBLSWrapper
*(static_cast<C*>(this)) = C();
}

void SetByteVector(const std::vector<uint8_t>& vecBytes, const bool specificLegacyScheme)
void SetByteVector(Span<const uint8_t> vecBytes, const bool specificLegacyScheme)
{
if (vecBytes.size() != SerSize) {
Reset();
Expand All @@ -114,7 +114,7 @@ class CBLSWrapper
Reset();
} else {
try {
impl = ImplType::FromBytes(bls::Bytes(vecBytes), specificLegacyScheme);
impl = ImplType::FromBytes(bls::Bytes(vecBytes.data(), vecBytes.size()), specificLegacyScheme);
fValid = true;
} catch (...) {
Reset();
Expand Down Expand Up @@ -179,7 +179,7 @@ class CBLSWrapper
template <typename Stream>
inline void Unserialize(Stream& s, const bool specificLegacyScheme)
{
std::vector<uint8_t> vecBytes(SerSize, 0);
std::array<uint8_t, SerSize> vecBytes{};
s.read(reinterpret_cast<char*>(vecBytes.data()), SerSize);
SetByteVector(vecBytes, specificLegacyScheme);

Expand Down Expand Up @@ -586,13 +586,7 @@ class CBLSLazyPublicKeyVersionWrapper {
};
#endif

using BLSIdVector = std::vector<CBLSId>;
using BLSVerificationVector = std::vector<CBLSPublicKey>;
using BLSPublicKeyVector = std::vector<CBLSPublicKey>;
using BLSSecretKeyVector = std::vector<CBLSSecretKey>;
using BLSSignatureVector = std::vector<CBLSSignature>;

using BLSVerificationVectorPtr = std::shared_ptr<BLSVerificationVector>;
using BLSVerificationVectorPtr = std::shared_ptr<std::vector<CBLSPublicKey>>;

bool BLSInit();

Expand Down
Loading

0 comments on commit b27765f

Please sign in to comment.