mirror of
https://github.com/dashpay/dash.git
synced 2024-12-24 03:22:47 +01:00
feat: serialize on the stack
This commit is contained in:
parent
dd96032e12
commit
47972aab8c
@ -123,6 +123,14 @@ public:
|
|||||||
cachedHash.SetNull();
|
cachedHash.SetNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::array<uint8_t, SerSize> ToBytes(const bool specificLegacyScheme) const
|
||||||
|
{
|
||||||
|
if (!fValid) {
|
||||||
|
return std::array<uint8_t, SerSize>{};
|
||||||
|
}
|
||||||
|
return impl.SerializeToArray(specificLegacyScheme);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> ToByteVector(const bool specificLegacyScheme) const
|
std::vector<uint8_t> ToByteVector(const bool specificLegacyScheme) const
|
||||||
{
|
{
|
||||||
if (!fValid) {
|
if (!fValid) {
|
||||||
@ -131,9 +139,9 @@ public:
|
|||||||
return impl.Serialize(specificLegacyScheme);
|
return impl.Serialize(specificLegacyScheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> ToByteVector() const
|
std::array<uint8_t, SerSize> ToBytes() const
|
||||||
{
|
{
|
||||||
return ToByteVector(bls::bls_legacy_scheme.load());
|
return ToBytes(bls::bls_legacy_scheme.load());
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint256& GetHash() const
|
const uint256& GetHash() const
|
||||||
@ -167,7 +175,7 @@ public:
|
|||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
inline void Serialize(Stream& s, const bool specificLegacyScheme) const
|
inline void Serialize(Stream& s, const bool specificLegacyScheme) const
|
||||||
{
|
{
|
||||||
s.write(AsBytes(Span{ToByteVector(specificLegacyScheme).data(), SerSize}));
|
s.write(AsBytes(Span{ToBytes(specificLegacyScheme)}));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
@ -206,7 +214,7 @@ public:
|
|||||||
|
|
||||||
inline bool CheckMalleable(Span<uint8_t> vecBytes, const bool specificLegacyScheme) const
|
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
|
// 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
|
// these libs masking might happen, so that 2 different binary representations could result in the same object
|
||||||
// representation
|
// representation
|
||||||
@ -222,7 +230,7 @@ public:
|
|||||||
|
|
||||||
inline std::string ToString(const bool specificLegacyScheme) const
|
inline std::string ToString(const bool specificLegacyScheme) const
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> buf = ToByteVector(specificLegacyScheme);
|
auto buf = ToBytes(specificLegacyScheme);
|
||||||
return HexStr(buf);
|
return HexStr(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,10 +253,12 @@ struct CBLSIdImplicit : public uint256
|
|||||||
memcpy(instance.begin(), buffer, sizeof(CBLSIdImplicit));
|
memcpy(instance.begin(), buffer, sizeof(CBLSIdImplicit));
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
[[nodiscard]] std::vector<uint8_t> Serialize(const bool fLegacy) const
|
[[nodiscard]] std::vector<uint8_t> SerializeToVec(const bool fLegacy) const
|
||||||
{
|
{
|
||||||
return {begin(), end()};
|
return {begin(), end()};
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] std::array<uint8_t, WIDTH> Serialize(const bool fLegacy) const { return m_data; }
|
||||||
|
[[nodiscard]] std::array<uint8_t, WIDTH> SerializeToArray(const bool fLegacy) const { return Serialize(fLegacy); }
|
||||||
};
|
};
|
||||||
|
|
||||||
class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
|
class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
|
||||||
@ -381,7 +391,7 @@ class CBLSLazyWrapper
|
|||||||
private:
|
private:
|
||||||
mutable std::mutex mutex;
|
mutable std::mutex mutex;
|
||||||
|
|
||||||
mutable std::vector<uint8_t> vecBytes;
|
mutable std::array<uint8_t, BLSObject::SerSize> vecBytes{};
|
||||||
mutable bool bufValid{false};
|
mutable bool bufValid{false};
|
||||||
mutable bool bufLegacyScheme{true};
|
mutable bool bufLegacyScheme{true};
|
||||||
|
|
||||||
@ -392,7 +402,6 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
CBLSLazyWrapper() :
|
CBLSLazyWrapper() :
|
||||||
vecBytes(BLSObject::SerSize, 0),
|
|
||||||
bufLegacyScheme(bls::bls_legacy_scheme.load())
|
bufLegacyScheme(bls::bls_legacy_scheme.load())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -410,7 +419,6 @@ public:
|
|||||||
if (r.bufValid) {
|
if (r.bufValid) {
|
||||||
vecBytes = r.vecBytes;
|
vecBytes = r.vecBytes;
|
||||||
} else {
|
} else {
|
||||||
vecBytes.resize(BLSObject::SerSize);
|
|
||||||
std::fill(vecBytes.begin(), vecBytes.end(), 0);
|
std::fill(vecBytes.begin(), vecBytes.end(), 0);
|
||||||
}
|
}
|
||||||
objInitialized = r.objInitialized;
|
objInitialized = r.objInitialized;
|
||||||
@ -433,10 +441,9 @@ public:
|
|||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(mutex);
|
std::unique_lock<std::mutex> l(mutex);
|
||||||
if (!objInitialized && !bufValid) {
|
if (!objInitialized && !bufValid) {
|
||||||
vecBytes.resize(BLSObject::SerSize);
|
|
||||||
std::fill(vecBytes.begin(), vecBytes.end(), 0);
|
std::fill(vecBytes.begin(), vecBytes.end(), 0);
|
||||||
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
|
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
|
||||||
vecBytes = obj.ToByteVector(specificLegacyScheme);
|
vecBytes = obj.ToBytes(specificLegacyScheme);
|
||||||
bufValid = true;
|
bufValid = true;
|
||||||
bufLegacyScheme = specificLegacyScheme;
|
bufLegacyScheme = specificLegacyScheme;
|
||||||
hash.SetNull();
|
hash.SetNull();
|
||||||
@ -518,11 +525,10 @@ public:
|
|||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(mutex);
|
std::unique_lock<std::mutex> l(mutex);
|
||||||
if (!objInitialized && !bufValid) {
|
if (!objInitialized && !bufValid) {
|
||||||
vecBytes.resize(BLSObject::SerSize);
|
|
||||||
std::fill(vecBytes.begin(), vecBytes.end(), 0);
|
std::fill(vecBytes.begin(), vecBytes.end(), 0);
|
||||||
hash.SetNull();
|
hash.SetNull();
|
||||||
} else if (!bufValid) {
|
} else if (!bufValid) {
|
||||||
vecBytes = obj.ToByteVector(bufLegacyScheme);
|
vecBytes = obj.ToBytes(bufLegacyScheme);
|
||||||
bufValid = true;
|
bufValid = true;
|
||||||
hash.SetNull();
|
hash.SetNull();
|
||||||
}
|
}
|
||||||
|
@ -49,8 +49,7 @@ bool CBLSIESEncryptedBlob::Encrypt(size_t idx, const CBLSPublicKey& peerPubKey,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> symKey = pk.ToByteVector();
|
auto symKey = pk.ToBytes();
|
||||||
symKey.resize(32);
|
|
||||||
|
|
||||||
uint256 iv = GetIV(idx);
|
uint256 iv = GetIV(idx);
|
||||||
return EncryptBlob(plainTextData, dataSize, data, symKey.data(), iv.begin());
|
return EncryptBlob(plainTextData, dataSize, data, symKey.data(), iv.begin());
|
||||||
@ -63,10 +62,9 @@ bool CBLSIESEncryptedBlob::Decrypt(size_t idx, const CBLSSecretKey& secretKey, C
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> symKey = pk.ToByteVector();
|
|
||||||
symKey.resize(32);
|
|
||||||
|
|
||||||
uint256 iv = GetIV(idx);
|
uint256 iv = GetIV(idx);
|
||||||
|
auto symKey = pk.ToBytes();
|
||||||
|
|
||||||
return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv.begin());
|
return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,8 +115,7 @@ bool CBLSIESMultiRecipientBlobs::Encrypt(size_t idx, const CBLSPublicKey& recipi
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> symKey = pk.ToByteVector();
|
auto symKey = pk.ToBytes();
|
||||||
symKey.resize(32);
|
|
||||||
|
|
||||||
return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin());
|
return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin());
|
||||||
}
|
}
|
||||||
@ -134,13 +131,11 @@ bool CBLSIESMultiRecipientBlobs::Decrypt(size_t idx, const CBLSSecretKey& sk, Bl
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> symKey = pk.ToByteVector();
|
|
||||||
symKey.resize(32);
|
|
||||||
|
|
||||||
uint256 iv = ivSeed;
|
uint256 iv = ivSeed;
|
||||||
for (size_t i = 0; i < idx; i++) {
|
for (size_t i = 0; i < idx; i++) {
|
||||||
iv = ::SerializeHash(iv);
|
iv = ::SerializeHash(iv);
|
||||||
}
|
}
|
||||||
|
auto symKey = pk.ToBytes();
|
||||||
|
|
||||||
return DecryptBlob(blobs[idx].data(), blobs[idx].size(), blobRet, symKey.data(), iv.begin());
|
return DecryptBlob(blobs[idx].data(), blobs[idx].size(), blobRet, symKey.data(), iv.begin());
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ bool CCoinJoinQueue::Sign(const CActiveMasternodeManager& mn_activeman)
|
|||||||
if (!sig.IsValid()) {
|
if (!sig.IsValid()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
vchSig = sig.ToByteVector(false);
|
vchSig = sig.ToBytes(false);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ bool CCoinJoinBroadcastTx::Sign(const CActiveMasternodeManager& mn_activeman)
|
|||||||
if (!sig.IsValid()) {
|
if (!sig.IsValid()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
vchSig = sig.ToByteVector(false);
|
vchSig = sig.ToBytes(false);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <coinjoin/common.h>
|
#include <coinjoin/common.h>
|
||||||
|
|
||||||
|
#include <bls/bls.h>
|
||||||
#include <core_io.h>
|
#include <core_io.h>
|
||||||
#include <netaddress.h>
|
#include <netaddress.h>
|
||||||
#include <primitives/block.h>
|
#include <primitives/block.h>
|
||||||
@ -183,7 +184,7 @@ public:
|
|||||||
uint256 m_protxHash;
|
uint256 m_protxHash;
|
||||||
int64_t nTime{0};
|
int64_t nTime{0};
|
||||||
bool fReady{false}; //ready for submit
|
bool fReady{false}; //ready for submit
|
||||||
std::vector<unsigned char> vchSig;
|
std::array<unsigned char, BLS_CURVE_SIG_SIZE> vchSig;
|
||||||
// memory only
|
// memory only
|
||||||
bool fTried{false};
|
bool fTried{false};
|
||||||
|
|
||||||
@ -243,7 +244,7 @@ public:
|
|||||||
CTransactionRef tx;
|
CTransactionRef tx;
|
||||||
COutPoint masternodeOutpoint;
|
COutPoint masternodeOutpoint;
|
||||||
uint256 m_protxHash;
|
uint256 m_protxHash;
|
||||||
std::vector<unsigned char> vchSig;
|
std::array<unsigned char, BLS_CURVE_SIG_SIZE> vchSig;
|
||||||
int64_t sigTime{0};
|
int64_t sigTime{0};
|
||||||
CCoinJoinBroadcastTx() :
|
CCoinJoinBroadcastTx() :
|
||||||
tx(MakeTransactionRef(CMutableTransaction{}))
|
tx(MakeTransactionRef(CMutableTransaction{}))
|
||||||
|
@ -1016,12 +1016,12 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages, PeerManag
|
|||||||
|
|
||||||
if (lieType == 3) {
|
if (lieType == 3) {
|
||||||
const bool is_bls_legacy = bls::bls_legacy_scheme.load();
|
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]++;
|
buf[5]++;
|
||||||
qc.sig.SetByteVector(buf, is_bls_legacy);
|
qc.sig.SetByteVector(buf, is_bls_legacy);
|
||||||
} else if (lieType == 4) {
|
} else if (lieType == 4) {
|
||||||
const bool is_bls_legacy = bls::bls_legacy_scheme.load();
|
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]++;
|
buf[5]++;
|
||||||
qc.quorumSig.SetByteVector(buf, is_bls_legacy);
|
qc.quorumSig.SetByteVector(buf, is_bls_legacy);
|
||||||
}
|
}
|
||||||
|
@ -820,6 +820,9 @@ 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 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);
|
template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
|
||||||
|
|
||||||
|
template <typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const std::array<T, N>& a);
|
||||||
|
template <typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, std::array<T, N>& a);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pair
|
* pair
|
||||||
*/
|
*/
|
||||||
@ -1051,6 +1054,54 @@ void Unserialize(Stream& is, std::vector<T, A>& v)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* array
|
||||||
|
*/
|
||||||
|
template <typename Stream, typename T, std::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 (!a.empty()) {
|
||||||
|
os.write(MakeByteSpan(a));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, bool>) {
|
||||||
|
// 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, std::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 (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
|
* pair
|
||||||
|
Loading…
Reference in New Issue
Block a user