11 #ifndef BUILD_BITCOIN_INTERNAL
18 static std::unique_ptr<bls::CoreMPL> pScheme(
new bls::BasicSchemeMPL);
40 std::vector<bls::PrivateKey> v;
41 v.reserve(sks.size());
42 for (
auto& sk : sks) {
43 v.emplace_back(sk.impl);
47 ret.
impl = bls::PrivateKey::Aggregate(v);
53 #ifndef BUILD_BITCOIN_INTERNAL
56 unsigned char buf[32];
60 impl = bls::PrivateKey::FromBytes(bls::Bytes((
const uint8_t*)buf,
SerSize));
79 std::vector<bls::PrivateKey> mskVec;
80 mskVec.reserve(msk.size());
85 mskVec.emplace_back(sk.impl);
132 if (keys.empty() || ids.empty() || keys.size() != ids.size()) {
136 std::vector<bls::PrivateKey> keysVec;
137 std::vector<bls::Bytes> idsVec;
138 keysVec.reserve(keys.size());
139 idsVec.reserve(keys.size());
141 for (
size_t i = 0; i < keys.size(); i++) {
145 keysVec.emplace_back(keys[i].
impl);
146 idsVec.emplace_back(ids[i].
impl.begin(), ids[i].impl.size());
150 impl = bls::Threshold::PrivateKeyRecover(keysVec, idsVec);
173 std::vector<bls::G1Element> vecPublicKeys;
174 vecPublicKeys.reserve(pks.size());
175 for (
auto& pk : pks) {
176 vecPublicKeys.emplace_back(pk.impl);
180 ret.
impl = pScheme->Aggregate(vecPublicKeys);
195 std::vector<bls::G1Element> mpkVec;
196 mpkVec.reserve(mpk.size());
201 mpkVec.emplace_back(pk.impl);
242 std::vector<bls::G2Element> v;
243 v.reserve(sigs.size());
244 for (
auto& pk : sigs) {
245 v.emplace_back(pk.impl);
249 ret.
impl = pScheme->Aggregate(v);
256 const std::vector<CBLSPublicKey>& pks,
259 if (sigs.size() != pks.size() || sigs.empty()) {
263 std::vector<bls::G1Element> vecPublicKeys;
264 vecPublicKeys.reserve(pks.size());
265 for (
auto& pk : pks) {
266 vecPublicKeys.push_back(pk.impl);
269 std::vector<bls::G2Element> vecSignatures;
270 vecSignatures.reserve(pks.size());
271 for (
auto& sig : sigs) {
272 vecSignatures.push_back(sig.impl);
276 ret.
impl = pScheme->AggregateSecure(vecPublicKeys, vecSignatures, bls::Bytes(hash.
begin(), hash.
size()));
296 return pScheme->Verify(pubKey.
impl, bls::Bytes(hash.
begin(), hash.
size()),
impl);
307 assert(!pubKeys.empty() && !hashes.empty() && pubKeys.size() == hashes.size());
309 std::vector<bls::G1Element> pubKeyVec;
310 std::vector<bls::Bytes> hashes2;
311 hashes2.reserve(hashes.size());
312 pubKeyVec.reserve(pubKeys.size());
313 for (
size_t i = 0; i < pubKeys.size(); i++) {
314 auto& p = pubKeys[i];
318 pubKeyVec.push_back(p.impl);
319 hashes2.emplace_back(hashes[i].begin(), hashes[i].size());
323 return pScheme->AggregateVerify(pubKeyVec, hashes2,
impl);
335 std::vector<bls::G1Element> vecPublicKeys;
336 vecPublicKeys.reserve(pks.size());
337 for (
const auto& pk : pks) {
338 vecPublicKeys.push_back(pk.impl);
341 return pScheme->VerifySecure(vecPublicKeys,
impl, bls::Bytes(hash.
begin(), hash.
size()));
349 if (sigs.empty() || ids.empty() || sigs.size() != ids.size()) {
353 std::vector<bls::G2Element> sigsVec;
354 std::vector<bls::Bytes> idsVec;
355 sigsVec.reserve(sigs.size());
356 idsVec.reserve(sigs.size());
358 for (
size_t i = 0; i < sigs.size(); i++) {
362 sigsVec.emplace_back(sigs[i].
impl);
363 idsVec.emplace_back(ids[i].
impl.begin(), ids[i].impl.size());
367 impl = bls::Threshold::SignatureRecover(sigsVec, idsVec);
377 #ifndef BUILD_BITCOIN_INTERNAL
379 static std::once_flag init_flag;
381 static void create_secure_allocator()
389 secure_allocator_instance = &a;
394 std::call_once(init_flag, create_secure_allocator);
395 return *secure_allocator_instance;
398 static void* secure_allocate(
size_t n)
400 uint8_t* ptr = get_secure_allocator().allocate(n +
sizeof(
size_t));
402 return ptr +
sizeof(size_t);
405 static void secure_free(
void* p)
411 uint8_t* ptr = (uint8_t*)p -
sizeof(
size_t);
412 size_t n = *(
size_t*)ptr;
413 return get_secure_allocator().deallocate(ptr, n);
419 if (!bls::BLS::Init()) {
422 #ifndef BUILD_BITCOIN_INTERNAL
423 bls::BLS::SetSecureAllocator(secure_allocate, secure_free);
#define BLS_CURVE_ID_SIZE
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)
bool Recover(const std::vector< CBLSSignature > &sigs, const std::vector< CBLSId > &ids)
void SubInsecure(const CBLSSignature &o)
bool VerifyInsecureAggregated(const std::vector< CBLSPublicKey > &pubKeys, const std::vector< uint256 > &hashes) const
bool VerifySecureAggregated(const std::vector< CBLSPublicKey > &pks, const uint256 &hash) const
friend class CBLSPublicKey
friend class CBLSSignature
friend class CBLSSecretKey
static const size_t SerSize
static LockedPoolManager & Instance()
Return the current instance, or create it once.
unsigned int size() const
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...