6 #ifndef PIVX_BLS_BLS_WRAPPER_H
7 #define PIVX_BLS_BLS_WRAPPER_H
18 #include <privatekey.hpp>
19 #include <elements.hpp>
20 #include <schemes.hpp>
21 #include <threshold.hpp>
29 #define BLS_CURVE_ID_SIZE 32
30 #define BLS_CURVE_SECKEY_SIZE 32
31 #define BLS_CURVE_PUBKEY_SIZE 48
32 #define BLS_CURVE_SIG_SIZE 96
37 template <
typename ImplType,
size_t _SerSize,
typename C>
66 std::swap(
impl, ref.impl);
67 std::swap(
fValid, ref.fValid);
72 std::swap(
impl, ref.impl);
73 std::swap(
fValid, ref.fValid);
84 return !((*this) == r);
99 if (vecBytes.size() !=
SerSize) {
103 if (std::all_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c == 0; })) {
107 impl = ImplType::FromBytes(bls::Bytes(vecBytes));
119 return std::vector<uint8_t>(
SerSize, 0);
121 return impl.Serialize();
133 template <
typename Stream>
139 template <
typename Stream>
142 std::vector<uint8_t> vecBytes(
SerSize, 0);
143 s.read((
char*)vecBytes.data(),
SerSize);
147 throw std::ios_base::failure(
"malleable BLS object");
193 using CBLSWrapper::operator=;
194 using CBLSWrapper::operator==;
195 using CBLSWrapper::operator!=;
204 using CBLSWrapper::operator=;
205 using CBLSWrapper::operator==;
206 using CBLSWrapper::operator!=;
211 #ifndef BUILD_BITCOIN_INTERNAL
218 bool Recover(
const std::vector<CBLSSecretKey>& keys,
const std::vector<CBLSId>& ids);
227 using CBLSWrapper::operator=;
228 using CBLSWrapper::operator==;
229 using CBLSWrapper::operator!=;
247 using CBLSWrapper::operator==;
248 using CBLSWrapper::operator!=;
266 bool Recover(
const std::vector<CBLSSignature>& sigs,
const std::vector<CBLSId>& ids);
269 #ifndef BUILD_BITCOIN_INTERNAL
271 template<
typename BLSObject>
299 std::unique_lock<std::mutex> l(r.
mutex);
316 template<
typename Stream>
319 std::unique_lock<std::mutex> l(
mutex);
321 throw std::ios_base::failure(
"obj and buf not initialized");
331 template<
typename Stream>
334 std::unique_lock<std::mutex> l(
mutex);
335 s.read((
char*)
vecBytes.data(), BLSObject::SerSize);
341 void Set(
const BLSObject& _obj)
343 std::unique_lock<std::mutex> l(
mutex);
350 const BLSObject&
Get()
const
352 std::unique_lock<std::mutex> l(
mutex);
353 static BLSObject invalidObj;
383 return !(*
this == r);
388 std::unique_lock<std::mutex> l(
mutex);
std::vector< CBLSId > BLSIdVector
std::shared_ptr< BLSIdVector > BLSIdVectorPtr
std::vector< CBLSPublicKey > BLSVerificationVector
std::vector< CBLSPublicKey > BLSPublicKeyVector
std::shared_ptr< BLSSignatureVector > BLSSignatureVectorPtr
CBLSLazyWrapper< CBLSSignature > CBLSLazySignature
CBLSLazyWrapper< CBLSPublicKey > CBLSLazyPublicKey
std::shared_ptr< BLSVerificationVector > BLSVerificationVectorPtr
std::shared_ptr< BLSPublicKeyVector > BLSPublicKeyVectorPtr
std::shared_ptr< BLSSecretKeyVector > BLSSecretKeyVectorPtr
std::vector< CBLSSecretKey > BLSSecretKeyVector
std::vector< CBLSSignature > BLSSignatureVector
CBLSLazyWrapper< CBLSSecretKey > CBLSLazySecretKey
std::vector< uint8_t > vecBytes
void Unserialize(Stream &s)
bool operator==(const CBLSLazyWrapper &r) const
void Serialize(Stream &s) const
const BLSObject & Get() const
bool operator!=(const CBLSLazyWrapper &r) const
CBLSLazyWrapper(const CBLSLazyWrapper &r)
void Set(const BLSObject &_obj)
CBLSLazyWrapper & operator=(const CBLSLazyWrapper &r)
void AggregateInsecure(const CBLSPublicKey &o)
bool PublicKeyShare(const std::vector< CBLSPublicKey > &mpk, const CBLSId &id)
bool DHKeyExchange(const CBLSSecretKey &sk, const CBLSPublicKey &pk)
CBLSPublicKey GetPublicKey() const
bool Recover(const std::vector< CBLSSecretKey > &keys, const std::vector< CBLSId > &ids)
void AggregateInsecure(const CBLSSecretKey &o)
bool SecretKeyShare(const std::vector< CBLSSecretKey > &msk, const CBLSId &id)
CBLSSignature Sign(const uint256 &hash) const
void AggregateInsecure(const CBLSSignature &o)
bool VerifyInsecure(const CBLSPublicKey &pubKey, const uint256 &hash) const
static CBLSSignature AggregateSecure(const std::vector< CBLSSignature > &sigs, const std::vector< CBLSPublicKey > &pks, const uint256 &hash)
CBLSSignature(const CBLSSignature &)=default
bool Recover(const std::vector< CBLSSignature > &sigs, const std::vector< CBLSId > &ids)
void SubInsecure(const CBLSSignature &o)
CBLSSignature & operator=(const CBLSSignature &)=default
bool VerifyInsecureAggregated(const std::vector< CBLSPublicKey > &pubKeys, const std::vector< uint256 > &hashes) const
bool VerifySecureAggregated(const std::vector< CBLSPublicKey > &pks, const uint256 &hash) const
bool operator!=(const C &r) const
void SetByteVector(const std::vector< uint8_t > &vecBytes)
CBLSWrapper(const CBLSWrapper &ref)=default
std::vector< uint8_t > ToByteVector() const
bool operator==(const C &r) const
void Unserialize(Stream &s, bool checkMalleable=true)
constexpr size_t GetSerSize() const
bool CheckMalleable(const std::vector< uint8_t > &vecBytes) const
static const size_t SerSize
std::string ToString() const
const uint256 & GetHash() const
CBLSWrapper & operator=(const CBLSWrapper &ref)=default
CBLSWrapper & operator=(CBLSWrapper &&ref)
void Serialize(Stream &s) const
CBLSWrapper(CBLSWrapper &&ref)
CBLSWrapper(const std::vector< unsigned char > &vecBytes)
A writer stream (for serialization) that computes a 256-bit hash.
void write(const char *pch, size_t size)
std::vector< uint8_t > buffer
void * memcpy(void *a, const void *b, size_t c)
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
std::vector< uint8_t > Serialize() const
CBLSIdImplicit(const uint256 &id)
static CBLSIdImplicit FromBytes(const uint8_t *buffer)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.