-
Couldn't load subscription status.
- Fork 1.2k
refactor: optimize BLS serialization with stack-allocated arrays #6917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
6b41290
ee42c15
990e239
bbca2d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,8 +44,7 @@ bool CBLSIESEncryptedBlob::Decrypt(size_t idx, const CBLSSecretKey& secretKey, C | |
| return false; | ||
| } | ||
|
|
||
| std::vector<unsigned char> symKey = pk.ToByteVector(false); | ||
| symKey.resize(32); | ||
| auto symKey = pk.ToBytes(false); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think better to write directly type of also consider {} initialization instead = There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it being std::array<uint8_t, BLS_PUBLIC_KEY_SIZE> really more readable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because it's easy to see, that there's no allocation, and it's raw bytes, no any wrappers or overhead. I guess it's still subjective and anyway it's an opinion |
||
|
|
||
| uint256 iv = GetIV(idx); | ||
| return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv.begin()); | ||
|
|
@@ -80,8 +79,7 @@ bool CBLSIESMultiRecipientBlobs::Encrypt(size_t idx, const CBLSPublicKey& recipi | |
| return false; | ||
| } | ||
|
|
||
| std::vector<uint8_t> symKey = pk.ToByteVector(false); | ||
| symKey.resize(32); | ||
| auto symKey = pk.ToBytes(false); | ||
|
|
||
| return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin()); | ||
| } | ||
|
|
@@ -97,13 +95,12 @@ bool CBLSIESMultiRecipientBlobs::Decrypt(size_t idx, const CBLSSecretKey& sk, Bl | |
| return false; | ||
| } | ||
|
|
||
| std::vector<uint8_t> symKey = pk.ToByteVector(false); | ||
| symKey.resize(32); | ||
|
|
||
| uint256 iv = ivSeed; | ||
| for (size_t i = 0; i < idx; i++) { | ||
| iv = ::SerializeHash(iv); | ||
| } | ||
|
|
||
| auto symKey = pk.ToBytes(false); | ||
|
|
||
| return DecryptBlob(blobs[idx].data(), blobs[idx].size(), blobRet, symKey.data(), iv.begin()); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| #include <coinjoin/common.h> | ||
|
|
||
| #include <bls/bls.h> | ||
| #include <core_io.h> | ||
| #include <netaddress.h> | ||
| #include <primitives/block.h> | ||
|
|
@@ -182,7 +183,7 @@ class CCoinJoinQueue | |
| uint256 m_protxHash; | ||
| int64_t nTime{0}; | ||
| bool fReady{false}; //ready for submit | ||
| std::vector<unsigned char> vchSig; | ||
| std::array<uint8_t, BLS_CURVE_SIG_SIZE> vchSig{}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mixing fails with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does it fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
| // memory only | ||
| bool fTried{false}; | ||
|
|
||
|
|
@@ -235,7 +236,7 @@ class CCoinJoinBroadcastTx | |
| CTransactionRef tx; | ||
| COutPoint masternodeOutpoint; | ||
| uint256 m_protxHash; | ||
| std::vector<unsigned char> vchSig; | ||
| std::array<uint8_t, BLS_CURVE_SIG_SIZE> vchSig{}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
| int64_t sigTime{0}; | ||
| CCoinJoinBroadcastTx() : | ||
| tx(MakeTransactionRef(CMutableTransaction{})) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,7 @@ class CGovernanceVote | |
| UpdateHash(); | ||
| } | ||
|
|
||
PastaPastaPasta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void SetSignature(const std::vector<unsigned char>& vchSigIn) { vchSig = vchSigIn; } | ||
| void SetSignature(Span<const uint8_t> vchSigIn) { vchSig.assign(vchSigIn.begin(), vchSigIn.end()); } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so basically back to |
||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bool CheckSignature(const CKeyID& keyID) const; | ||
| bool CheckSignature(const CBLSPublicKey& pubKey) const; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -838,6 +838,12 @@ template<typename Stream, unsigned int N, typename T> inline void Unserialize(St | |
| template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v); | ||
| template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v); | ||
|
|
||
| /** | ||
| * array | ||
| */ | ||
| template<typename Stream, typename T, size_t N> void Serialize(Stream& os, const std::array<T, N>& a); | ||
| template<typename Stream, typename T, size_t N> void Unserialize(Stream& is, std::array<T, N>& a); | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * pair | ||
| */ | ||
|
|
@@ -1069,6 +1075,51 @@ void Unserialize(Stream& is, std::vector<T, A>& v) | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * array | ||
| */ | ||
| template<typename Stream, typename T, size_t N> | ||
| void Serialize(Stream& os, const std::array<T, N>& a) | ||
| { | ||
| if constexpr (std::is_same_v<T, unsigned char>) { | ||
| // Directly write the byte data without writing the size | ||
| if constexpr (N > 0) { | ||
| os.write(MakeByteSpan(a)); | ||
| } | ||
| } else if constexpr (std::is_same_v<T, bool>) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need a case of std::array<bool, N> ? I don't see any usage in code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think to just keep compatibility with the vector api. |
||
| // Serialize each bool individually | ||
| for (const bool& elem : a) { | ||
| ::Serialize(os, elem); | ||
| } | ||
| } else { | ||
| // Serialize each element using the default Serialize function | ||
| for (const T& elem : a) { | ||
| ::Serialize(os, elem); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template<typename Stream, typename T, size_t N> | ||
| void Unserialize(Stream& is, std::array<T, N>& a) | ||
| { | ||
| if constexpr (std::is_same_v<T, unsigned char>) { | ||
| // Directly read the byte data without reading the size | ||
| if constexpr (N > 0) { | ||
| is.read(AsWritableBytes(Span{a})); | ||
| } | ||
| } else if constexpr (std::is_same_v<T, bool>) { | ||
| // Unserialize each bool individually | ||
| for (bool& elem : a) { | ||
| ::Unserialize(is, elem); | ||
| } | ||
| } else { | ||
| // Unserialize each element using the default Unserialize function | ||
| for (T& elem : a) { | ||
| ::Unserialize(is, elem); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * pair | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing include for PROTOCOL_VERSION (bench build may fail).
CDataStream uses PROTOCOL_VERSION here, but protocol.h isn’t included in this TU.
Apply near the other headers:
#include <streams.h> +#include <protocol.h>Also applies to: 430-431
🤖 Prompt for AI Agents