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 Dec 29, 2024
1 parent bf2453b commit d350cbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
30 changes: 20 additions & 10 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ class CBLSWrapper
return ToByteVector(bls::bls_legacy_scheme.load());
}

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

const uint256& GetHash() const
{
if (cachedHash.IsNull()) {
Expand Down Expand Up @@ -167,7 +176,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 @@ -206,7 +215,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 @@ -222,7 +231,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 @@ -249,6 +258,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 @@ -381,7 +394,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 @@ -392,7 +405,7 @@ class CBLSLazyWrapper

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

Expand All @@ -410,7 +423,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 @@ -433,10 +445,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 @@ -518,11 +529,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 @@ -1016,12 +1016,12 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages, PeerManag

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

0 comments on commit d350cbc

Please sign in to comment.