diff --git a/src/bls/bls.h b/src/bls/bls.h index b1b0eb5f75764..ead54c576250b 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -59,7 +59,7 @@ class CBLSWrapper explicit CBLSWrapper() = default; explicit CBLSWrapper(Span vecBytes) : CBLSWrapper() { - SetByteVector(vecBytes, bls::bls_legacy_scheme.load()); + SetBytes(vecBytes, bls::bls_legacy_scheme.load()); } CBLSWrapper(const CBLSWrapper& ref) = default; @@ -103,7 +103,7 @@ class CBLSWrapper *(static_cast(this)) = C(); } - void SetByteVector(Span vecBytes, const bool specificLegacyScheme) + void SetBytes(Span vecBytes, const bool specificLegacyScheme) { if (vecBytes.size() != SerSize) { Reset(); @@ -136,6 +136,15 @@ class CBLSWrapper return ToByteVector(bls::bls_legacy_scheme.load()); } + std::array ToBytes(const bool specificLegacyScheme) const + { + return impl.SerializeToArray(specificLegacyScheme); + } + std::array ToBytes() const + { + return ToBytes(bls::bls_legacy_scheme.load()); + } + const uint256& GetHash() const { if (cachedHash.IsNull()) { @@ -155,7 +164,7 @@ class CBLSWrapper Reset(); return false; } - SetByteVector(b, specificLegacyScheme); + SetBytes(b, specificLegacyScheme); return IsValid(); } @@ -167,7 +176,7 @@ class CBLSWrapper template 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 @@ -181,12 +190,12 @@ class CBLSWrapper { std::array vecBytes{}; s.read(AsWritableBytes(Span{vecBytes.data(), SerSize})); - SetByteVector(vecBytes, specificLegacyScheme); + SetBytes(vecBytes, specificLegacyScheme); if (!CheckMalleable(vecBytes, specificLegacyScheme)) { // If CheckMalleable failed with specificLegacyScheme, we need to try again with the opposite scheme. // Probably we received the BLS object sent with legacy scheme, but in the meanwhile the fork activated. - SetByteVector(vecBytes, !specificLegacyScheme); + SetBytes(vecBytes, !specificLegacyScheme); if (!CheckMalleable(vecBytes, !specificLegacyScheme)) { // Both attempts failed throw std::ios_base::failure("malleable BLS object"); @@ -206,7 +215,7 @@ class CBLSWrapper inline bool CheckMalleable(Span 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 @@ -222,7 +231,7 @@ class CBLSWrapper inline std::string ToString(const bool specificLegacyScheme) const { - std::vector buf = ToByteVector(specificLegacyScheme); + auto buf = ToBytes(specificLegacyScheme); return HexStr(buf); } @@ -249,6 +258,10 @@ struct CBLSIdImplicit : public uint256 { return {begin(), end()}; } + [[nodiscard]] std::array SerializeToArray(const bool fLegacy) const + { + return m_data; + } }; class CBLSId : public CBLSWrapper @@ -381,7 +394,7 @@ class CBLSLazyWrapper private: mutable std::mutex mutex; - mutable std::vector vecBytes; + mutable std::array vecBytes; mutable bool bufValid{false}; mutable bool bufLegacyScheme{true}; @@ -392,7 +405,7 @@ class CBLSLazyWrapper public: CBLSLazyWrapper() : - vecBytes(BLSObject::SerSize, 0), + vecBytes{0}, bufLegacyScheme(bls::bls_legacy_scheme.load()) {} @@ -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; @@ -433,10 +445,9 @@ class CBLSLazyWrapper { std::unique_lock 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(); @@ -484,7 +495,7 @@ class CBLSLazyWrapper return invalidObj; } if (!objInitialized) { - obj.SetByteVector(vecBytes, bufLegacyScheme); + obj.SetBytes(vecBytes, bufLegacyScheme); if (!obj.IsValid()) { bufValid = false; return invalidObj; @@ -518,11 +529,10 @@ class CBLSLazyWrapper { std::unique_lock 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(); } diff --git a/src/governance/object.cpp b/src/governance/object.cpp index 6ae0ed619b464..fc3a850e5d93c 100644 --- a/src/governance/object.cpp +++ b/src/governance/object.cpp @@ -265,7 +265,7 @@ bool CGovernanceObject::Sign(const CActiveMasternodeManager& mn_activeman) bool CGovernanceObject::CheckSignature(const CBLSPublicKey& pubKey) const { CBLSSignature sig; - sig.SetByteVector(m_obj.vchSig, false); + sig.SetBytes(m_obj.vchSig, false); if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), false)) { LogPrintf("CGovernanceObject::CheckSignature -- VerifyInsecure() failed\n"); return false; diff --git a/src/governance/vote.cpp b/src/governance/vote.cpp index 5cfa2b1ac1869..501078c6caec2 100644 --- a/src/governance/vote.cpp +++ b/src/governance/vote.cpp @@ -182,7 +182,7 @@ bool CGovernanceVote::Sign(const CActiveMasternodeManager& mn_activeman) bool CGovernanceVote::CheckSignature(const CBLSPublicKey& pubKey) const { CBLSSignature sig; - sig.SetByteVector(vchSig, false); + sig.SetBytes(vchSig, false); if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), false)) { LogPrintf("CGovernanceVote::CheckSignature -- VerifyInsecure() failed\n"); return false; diff --git a/src/llmq/dkgsession.cpp b/src/llmq/dkgsession.cpp index b63ebf45a4e0f..29115de07e31b 100644 --- a/src/llmq/dkgsession.cpp +++ b/src/llmq/dkgsession.cpp @@ -1016,14 +1016,14 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages, PeerManag if (lieType == 3) { const bool is_bls_legacy = bls::bls_legacy_scheme.load(); - std::vector buf = qc.sig.ToByteVector(is_bls_legacy); + auto buf = qc.sig.ToBytes(is_bls_legacy); buf[5]++; - qc.sig.SetByteVector(buf, is_bls_legacy); + qc.sig.SetBytes(buf, is_bls_legacy); } else if (lieType == 4) { const bool is_bls_legacy = bls::bls_legacy_scheme.load(); - std::vector buf = qc.quorumSig.ToByteVector(is_bls_legacy); + auto buf = qc.quorumSig.ToBytes(is_bls_legacy); buf[5]++; - qc.quorumSig.SetByteVector(buf, is_bls_legacy); + qc.quorumSig.SetBytes(buf, is_bls_legacy); } t3.stop();