Skip to content

Commit

Permalink
perf: introduce ToBytes in bls.h and use it in all spots trivially po…
Browse files Browse the repository at this point in the history
…ssible, convert vecBytes to std::array

this results in an improved reindex speed as previously roughly 2% of total cycles were just in allocating the vector.
  • Loading branch information
PastaPastaPasta committed Jan 27, 2025
1 parent a348d9f commit bbf97d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class CBLSWrapper
return impl.Serialize(specificLegacyScheme);
}

std::array<uint8_t, SerSize> ToBytes(const bool specificLegacyScheme) const
{
return impl.SerializeToArray(specificLegacyScheme);
}

const uint256& GetHash() const
{
if (cachedHash.IsNull()) {
Expand Down Expand Up @@ -158,7 +163,7 @@ class CBLSWrapper
template <typename Stream>
inline void Serialize(Stream& s, const bool specificLegacyScheme) const
{
s.write(AsBytes(Span{ToByteVector(specificLegacyScheme).data(), SerSize}));
s.write(AsBytes(Span{ToBytes(specificLegacyScheme).data(), SerSize}));
}

template <typename Stream>
Expand Down Expand Up @@ -197,7 +202,7 @@ class CBLSWrapper

inline bool CheckMalleable(Span<uint8_t> vecBytes, const bool specificLegacyScheme) const
{
if (memcmp(vecBytes.data(), ToByteVector(specificLegacyScheme).data(), SerSize)) {
if (memcmp(vecBytes.data(), ToBytes(specificLegacyScheme).data(), SerSize)) {
// TODO not sure if this is actually possible with the BLS libs. I'm assuming here that somewhere deep inside
// these libs masking might happen, so that 2 different binary representations could result in the same object
// representation
Expand All @@ -208,7 +213,7 @@ class CBLSWrapper

inline std::string ToString(const bool specificLegacyScheme) const
{
std::vector<uint8_t> buf = ToByteVector(specificLegacyScheme);
auto buf = ToBytes(specificLegacyScheme);
return HexStr(buf);
}

Expand All @@ -235,6 +240,10 @@ struct CBLSIdImplicit : public uint256
{
return {begin(), end()};
}
[[nodiscard]] std::array<uint8_t, 32> SerializeToArray(const bool fLegacy) const
{
return m_data;
}
};

class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
Expand Down Expand Up @@ -379,7 +388,7 @@ class CBLSLazyWrapper
private:
mutable std::mutex mutex;

mutable std::vector<uint8_t> vecBytes;
mutable std::array<uint8_t, BLSObject::SerSize> vecBytes;
mutable bool bufValid{false};
mutable bool bufLegacyScheme{true};

Expand All @@ -390,7 +399,7 @@ class CBLSLazyWrapper

public:
CBLSLazyWrapper() :
vecBytes(BLSObject::SerSize, 0),
vecBytes{0},
bufLegacyScheme(bls::bls_legacy_scheme.load())
{}

Expand All @@ -408,7 +417,6 @@ class CBLSLazyWrapper
if (r.bufValid) {
vecBytes = r.vecBytes;
} else {
vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
}
objInitialized = r.objInitialized;
Expand All @@ -431,10 +439,9 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
if (!objInitialized && !bufValid) {
vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
vecBytes = obj.ToByteVector(specificLegacyScheme);
vecBytes = obj.ToBytes(specificLegacyScheme);
bufValid = true;
bufLegacyScheme = specificLegacyScheme;
hash.SetNull();
Expand Down Expand Up @@ -516,11 +523,10 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
if (!objInitialized && !bufValid) {
vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
hash.SetNull();
} else if (!bufValid) {
vecBytes = obj.ToByteVector(bufLegacyScheme);
vecBytes = obj.ToBytes(bufLegacyScheme);
bufValid = true;
hash.SetNull();
}
Expand Down
4 changes: 2 additions & 2 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,11 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages, PeerManag
qc.quorumSig = skShare.Sign(commitmentHash, m_use_legacy_bls);

if (lieType == 3) {
std::vector<uint8_t> buf = qc.sig.ToByteVector(m_use_legacy_bls);
auto buf = qc.sig.ToBytes(m_use_legacy_bls);
buf[5]++;
qc.sig.SetBytes(buf, m_use_legacy_bls);
} else if (lieType == 4) {
std::vector<uint8_t> buf = qc.quorumSig.ToByteVector(m_use_legacy_bls);
auto buf = qc.quorumSig.ToBytes(m_use_legacy_bls);
buf[5]++;
qc.quorumSig.SetBytes(buf, m_use_legacy_bls);
}
Expand Down

0 comments on commit bbf97d7

Please sign in to comment.