From 73d32711dfcac246254878c378c4df6bec11c1ee Mon Sep 17 00:00:00 2001 From: xdustinface Date: Thu, 10 Dec 2020 22:33:17 +0100 Subject: [PATCH 1/6] 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. --- src/bls/bls.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/bls/bls.h b/src/bls/bls.h index e7bdd8ab9f82..50752b438641 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -215,6 +215,25 @@ class CBLSWrapper } }; +struct CBLSIdImplicit : public uint256 +{ + CBLSIdImplicit() {} + CBLSIdImplicit(const uint256& id) + { + memcpy(begin(), id.begin(), sizeof(uint256)); + } + static uint256 FromBytes(const uint8_t* buffer) + { + uint256 instance; + memcpy(instance.begin(), buffer, sizeof(uint256)); + return instance; + } + void Serialize(uint8_t* buffer) const + { + memcpy(buffer, data, sizeof(uint256)); + } +}; + class CBLSId : public CBLSWrapper { public: From 2de99db6cc6439ff98ed69a6e833bfb3162431b4 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Thu, 10 Dec 2020 22:37:15 +0100 Subject: [PATCH 2/6] bls: Use CBLSImplicit instead of uint256 as base type of CBLSId --- src/bls/bls.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bls/bls.h b/src/bls/bls.h index 50752b438641..e0f94ab0a959 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -234,7 +234,7 @@ struct CBLSIdImplicit : public uint256 } }; -class CBLSId : public CBLSWrapper +class CBLSId : public CBLSWrapper { public: using CBLSWrapper::operator=; From 1df1d8bb248943a992437ee2b397831dda865f9c Mon Sep 17 00:00:00 2001 From: xdustinface Date: Thu, 10 Dec 2020 22:37:32 +0100 Subject: [PATCH 3/6] bls: Use FromBytes directly instead of indirectly through InternalSetBuf --- src/bls/bls.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bls/bls.h b/src/bls/bls.h index e0f94ab0a959..7f690eeef58a 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -106,8 +106,10 @@ class CBLSWrapper 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(); } } From 2c2e29de7ba6366f8a342d3f8daa91be03830ad9 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Thu, 10 Dec 2020 22:38:10 +0100 Subject: [PATCH 4/6] bls: Use Serialize directly instead of indirectly through InternalGetBuf --- src/bls/bls.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bls/bls.h b/src/bls/bls.h index 7f690eeef58a..291fde3e0553 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -128,8 +128,7 @@ class CBLSWrapper if (!fValid) { memset(buf, 0, SerSize); } else { - bool ok = InternalGetBuf(buf); - assert(ok); + impl.Serialize(static_cast(buf)); } } From b936f17ddc7e968b35ed7e00b57a94d7c9462e91 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Thu, 10 Dec 2020 22:39:35 +0100 Subject: [PATCH 5/6] bls: Drop all occurrences of InternalSetBuf and InternalGetBuf --- src/bls/bls.cpp | 60 ------------------------------------------------- src/bls/bls.h | 18 --------------- 2 files changed, 78 deletions(-) diff --git a/src/bls/bls.cpp b/src/bls/bls.cpp index d8927cede6bb..6fe738656ce8 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 291fde3e0553..5c12b229e1b8 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -44,9 +44,6 @@ class CBLSWrapper 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; @@ -249,10 +246,6 @@ class CBLSId : public CBLSWrapper 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 @@ -274,10 +267,6 @@ class CBLSSecretKey : public CBLSWrapper @@ -298,9 +287,6 @@ class CBLSPublicKey : public CBLSWrapper& 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 @@ -328,10 +314,6 @@ class CBLSSignature : public CBLSWrapper& 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 From 1a55d8a024faddab8508fc86e2a8029c6247bc59 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 11 Dec 2020 11:46:37 +0100 Subject: [PATCH 6/6] bls: Use `CBLSIdImplicit` instead of `uint256` in some more places --- src/bls/bls.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bls/bls.h b/src/bls/bls.h index 5c12b229e1b8..17b610ab0910 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -220,15 +220,15 @@ struct CBLSIdImplicit : public uint256 { memcpy(begin(), id.begin(), sizeof(uint256)); } - static uint256 FromBytes(const uint8_t* buffer) + static CBLSIdImplicit FromBytes(const uint8_t* buffer) { - uint256 instance; - memcpy(instance.begin(), buffer, sizeof(uint256)); + CBLSIdImplicit instance; + memcpy(instance.begin(), buffer, sizeof(CBLSIdImplicit)); return instance; } void Serialize(uint8_t* buffer) const { - memcpy(buffer, data, sizeof(uint256)); + memcpy(buffer, data, sizeof(CBLSIdImplicit)); } };