From ec77d85385d0b56541e42abc96cb9dc071fc14d3 Mon Sep 17 00:00:00 2001 From: dustinface <35775977+xdustinface@users.noreply.github.com> Date: Sat, 12 Dec 2020 10:45:43 +0100 Subject: [PATCH] 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 --- src/bls/bls.cpp | 60 ------------------------------------------------- src/bls/bls.h | 48 ++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 83 deletions(-) diff --git a/src/bls/bls.cpp b/src/bls/bls.cpp index d8927cede6..6fe738656c 100644 --- a/src/bls/bls.cpp +++ b/src/bls/bls.cpp @@ -14,18 +14,6 @@ #include #include -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()); diff --git a/src/bls/bls.h b/src/bls/bls.h index e7bdd8ab9f..17b610ab09 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -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(buf)); } } @@ -215,7 +213,26 @@ public: } }; -class CBLSId : public CBLSWrapper +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 { 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 @@ -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 @@ -278,9 +287,6 @@ public: bool PublicKeyShare(const std::vector& 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 @@ -308,10 +314,6 @@ public: bool VerifySecureAggregated(const std::vector& pks, const uint256& hash) const; bool Recover(const std::vector& sigs, const std::vector& ids); - -protected: - bool InternalSetBuf(const void* buf); - bool InternalGetBuf(void* buf) const; }; #ifndef BUILD_BITCOIN_INTERNAL