mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
bls: Refactor CBLSWrapper::SetBuf and CBLSWrapper::GetBuf (#3867)
* bls: Add CBLSImplicit, a wrapper around uint256 This makes `CBLSImplicit` compatible (related to methods called by CBLSWrapper) with the other classes from the bls-signatures library. * bls: Use CBLSImplicit instead of uint256 as base type of CBLSId * bls: Use FromBytes directly instead of indirectly through InternalSetBuf * bls: Use Serialize directly instead of indirectly through InternalGetBuf * bls: Drop all occurrences of InternalSetBuf and InternalGetBuf * bls: Use `CBLSIdImplicit` instead of `uint256` in some more places
This commit is contained in:
parent
b95cf017c3
commit
ec77d85385
@ -14,18 +14,6 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
bool CBLSId::InternalSetBuf(const void* buf)
|
||||
{
|
||||
memcpy(impl.begin(), buf, sizeof(uint256));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CBLSId::InternalGetBuf(void* buf) const
|
||||
{
|
||||
memcpy(buf, impl.begin(), sizeof(uint256));
|
||||
return true;
|
||||
}
|
||||
|
||||
void CBLSId::SetInt(int x)
|
||||
{
|
||||
impl.SetHex(strprintf("%x", x));
|
||||
@ -54,22 +42,6 @@ CBLSId CBLSId::FromHash(const uint256& hash)
|
||||
return id;
|
||||
}
|
||||
|
||||
bool CBLSSecretKey::InternalSetBuf(const void* buf)
|
||||
{
|
||||
try {
|
||||
impl = bls::PrivateKey::FromBytes((const uint8_t*)buf);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CBLSSecretKey::InternalGetBuf(void* buf) const
|
||||
{
|
||||
impl.Serialize((uint8_t*)buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CBLSSecretKey::AggregateInsecure(const CBLSSecretKey& o)
|
||||
{
|
||||
assert(IsValid() && o.IsValid());
|
||||
@ -171,22 +143,6 @@ CBLSSignature CBLSSecretKey::Sign(const uint256& hash) const
|
||||
return sigRet;
|
||||
}
|
||||
|
||||
bool CBLSPublicKey::InternalSetBuf(const void* buf)
|
||||
{
|
||||
try {
|
||||
impl = bls::PublicKey::FromBytes((const uint8_t*)buf);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CBLSPublicKey::InternalGetBuf(void* buf) const
|
||||
{
|
||||
impl.Serialize((uint8_t*)buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CBLSPublicKey::AggregateInsecure(const CBLSPublicKey& o)
|
||||
{
|
||||
assert(IsValid() && o.IsValid());
|
||||
@ -257,22 +213,6 @@ bool CBLSPublicKey::DHKeyExchange(const CBLSSecretKey& sk, const CBLSPublicKey&
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CBLSSignature::InternalSetBuf(const void* buf)
|
||||
{
|
||||
try {
|
||||
impl = bls::InsecureSignature::FromBytes((const uint8_t*)buf);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CBLSSignature::InternalGetBuf(void* buf) const
|
||||
{
|
||||
impl.Serialize((uint8_t*)buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CBLSSignature::AggregateInsecure(const CBLSSignature& o)
|
||||
{
|
||||
assert(IsValid() && o.IsValid());
|
||||
|
@ -44,9 +44,6 @@ protected:
|
||||
|
||||
inline constexpr size_t GetSerSize() const { return SerSize; }
|
||||
|
||||
virtual bool InternalSetBuf(const void* buf) = 0;
|
||||
virtual bool InternalGetBuf(void* buf) const = 0;
|
||||
|
||||
public:
|
||||
static const size_t SerSize = _SerSize;
|
||||
|
||||
@ -106,8 +103,10 @@ public:
|
||||
if (std::all_of((const char*)buf, (const char*)buf + SerSize, [](char c) { return c == 0; })) {
|
||||
Reset();
|
||||
} else {
|
||||
fValid = InternalSetBuf(buf);
|
||||
if (!fValid) {
|
||||
try {
|
||||
impl = ImplType::FromBytes((const uint8_t*)buf);
|
||||
fValid = true;
|
||||
} catch (...) {
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
@ -126,8 +125,7 @@ public:
|
||||
if (!fValid) {
|
||||
memset(buf, 0, SerSize);
|
||||
} else {
|
||||
bool ok = InternalGetBuf(buf);
|
||||
assert(ok);
|
||||
impl.Serialize(static_cast<uint8_t*>(buf));
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,7 +213,26 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class CBLSId : public CBLSWrapper<uint256, BLS_CURVE_ID_SIZE, CBLSId>
|
||||
struct CBLSIdImplicit : public uint256
|
||||
{
|
||||
CBLSIdImplicit() {}
|
||||
CBLSIdImplicit(const uint256& id)
|
||||
{
|
||||
memcpy(begin(), id.begin(), sizeof(uint256));
|
||||
}
|
||||
static CBLSIdImplicit FromBytes(const uint8_t* buffer)
|
||||
{
|
||||
CBLSIdImplicit instance;
|
||||
memcpy(instance.begin(), buffer, sizeof(CBLSIdImplicit));
|
||||
return instance;
|
||||
}
|
||||
void Serialize(uint8_t* buffer) const
|
||||
{
|
||||
memcpy(buffer, data, sizeof(CBLSIdImplicit));
|
||||
}
|
||||
};
|
||||
|
||||
class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
|
||||
{
|
||||
public:
|
||||
using CBLSWrapper::operator=;
|
||||
@ -229,10 +246,6 @@ public:
|
||||
|
||||
static CBLSId FromInt(int64_t i);
|
||||
static CBLSId FromHash(const uint256& hash);
|
||||
|
||||
protected:
|
||||
bool InternalSetBuf(const void* buf);
|
||||
bool InternalGetBuf(void* buf) const;
|
||||
};
|
||||
|
||||
class CBLSSecretKey : public CBLSWrapper<bls::PrivateKey, BLS_CURVE_SECKEY_SIZE, CBLSSecretKey>
|
||||
@ -254,10 +267,6 @@ public:
|
||||
|
||||
CBLSPublicKey GetPublicKey() const;
|
||||
CBLSSignature Sign(const uint256& hash) const;
|
||||
|
||||
protected:
|
||||
bool InternalSetBuf(const void* buf);
|
||||
bool InternalGetBuf(void* buf) const;
|
||||
};
|
||||
|
||||
class CBLSPublicKey : public CBLSWrapper<bls::PublicKey, BLS_CURVE_PUBKEY_SIZE, CBLSPublicKey>
|
||||
@ -278,9 +287,6 @@ public:
|
||||
bool PublicKeyShare(const std::vector<CBLSPublicKey>& mpk, const CBLSId& id);
|
||||
bool DHKeyExchange(const CBLSSecretKey& sk, const CBLSPublicKey& pk);
|
||||
|
||||
protected:
|
||||
bool InternalSetBuf(const void* buf);
|
||||
bool InternalGetBuf(void* buf) const;
|
||||
};
|
||||
|
||||
class CBLSSignature : public CBLSWrapper<bls::InsecureSignature, BLS_CURVE_SIG_SIZE, CBLSSignature>
|
||||
@ -308,10 +314,6 @@ public:
|
||||
bool VerifySecureAggregated(const std::vector<CBLSPublicKey>& pks, const uint256& hash) const;
|
||||
|
||||
bool Recover(const std::vector<CBLSSignature>& sigs, const std::vector<CBLSId>& ids);
|
||||
|
||||
protected:
|
||||
bool InternalSetBuf(const void* buf);
|
||||
bool InternalGetBuf(void* buf) const;
|
||||
};
|
||||
|
||||
#ifndef BUILD_BITCOIN_INTERNAL
|
||||
|
Loading…
Reference in New Issue
Block a user