forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbls.cpp
More file actions
437 lines (367 loc) · 10.6 KB
/
bls.cpp
File metadata and controls
437 lines (367 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (c) 2018-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bls/bls.h>
#include <random.h>
#ifndef BUILD_BITCOIN_INTERNAL
#include <support/allocators/mt_pooled_secure.h>
#endif
#include <cassert>
#include <cstring>
namespace bls {
std::atomic<bool> bls_legacy_scheme = std::atomic<bool>(true);
}
static const std::unique_ptr<bls::CoreMPL> pSchemeLegacy{std::make_unique<bls::LegacySchemeMPL>()};
static const std::unique_ptr<bls::CoreMPL> pScheme(std::make_unique<bls::BasicSchemeMPL>());
static const std::unique_ptr<bls::CoreMPL>& Scheme(const bool fLegacy)
{
return fLegacy ? pSchemeLegacy : pScheme;
}
CBLSId::CBLSId(const uint256& nHash) : CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>()
{
impl = nHash;
fValid = true;
cachedHash.SetNull();
}
void CBLSSecretKey::AggregateInsecure(const CBLSSecretKey& o)
{
assert(IsValid() && o.IsValid());
impl = bls::PrivateKey::Aggregate({impl, o.impl});
cachedHash.SetNull();
}
CBLSSecretKey CBLSSecretKey::AggregateInsecure(Span<CBLSSecretKey> sks)
{
if (sks.empty()) {
return {};
}
std::vector<bls::PrivateKey> v;
v.reserve(sks.size());
for (const auto& sk : sks) {
v.emplace_back(sk.impl);
}
CBLSSecretKey ret;
ret.impl = bls::PrivateKey::Aggregate(v);
ret.fValid = true;
ret.cachedHash.SetNull();
return ret;
}
#ifndef BUILD_BITCOIN_INTERNAL
void CBLSSecretKey::MakeNewKey()
{
unsigned char buf[SerSize];
while (true) {
GetStrongRandBytes({buf, sizeof(buf)});
try {
impl = bls::PrivateKey::FromBytes(bls::Bytes(reinterpret_cast<const uint8_t*>(buf), SerSize));
if (impl == bls::PrivateKey()) {
continue;
}
break;
} catch (...) {
}
}
fValid = true;
cachedHash.SetNull();
}
#endif
bool CBLSSecretKey::SecretKeyShare(Span<CBLSSecretKey> msk, const CBLSId& _id)
{
fValid = false;
cachedHash.SetNull();
if (!_id.IsValid()) {
return false;
}
std::vector<bls::PrivateKey> mskVec;
mskVec.reserve(msk.size());
for (const CBLSSecretKey& sk : msk) {
if (!sk.IsValid()) {
return false;
}
mskVec.emplace_back(sk.impl);
}
try {
impl = bls::Threshold::PrivateKeyShare(mskVec, bls::Bytes(_id.impl.begin(), _id.impl.size()));
} catch (...) {
return false;
}
fValid = true;
cachedHash.SetNull();
return true;
}
CBLSPublicKey CBLSSecretKey::GetPublicKey() const
{
if (!IsValid()) {
return {};
}
CBLSPublicKey pubKey;
pubKey.impl = impl.GetG1Element();
pubKey.fValid = true;
pubKey.cachedHash.SetNull();
return pubKey;
}
CBLSSignature CBLSSecretKey::Sign(const uint256& hash, const bool specificLegacyScheme) const
{
if (!IsValid()) {
return {};
}
CBLSSignature sigRet;
try {
sigRet.impl = Scheme(specificLegacyScheme)->Sign(impl, bls::Bytes(hash.begin(), hash.size()));
sigRet.fValid = true;
} catch (...) {
sigRet.fValid = false;
}
sigRet.cachedHash.SetNull();
return sigRet;
}
void CBLSPublicKey::AggregateInsecure(const CBLSPublicKey& o)
{
assert(IsValid() && o.IsValid());
try {
impl = Scheme(bls::bls_legacy_scheme.load())->Aggregate({impl, o.impl});
} catch (...) {
fValid = false;
}
cachedHash.SetNull();
}
CBLSPublicKey CBLSPublicKey::AggregateInsecure(Span<CBLSPublicKey> pks)
{
if (pks.empty()) {
return {};
}
std::vector<bls::G1Element> vecPublicKeys;
vecPublicKeys.reserve(pks.size());
for (const auto& pk : pks) {
vecPublicKeys.emplace_back(pk.impl);
}
CBLSPublicKey ret;
try {
ret.impl = Scheme(bls::bls_legacy_scheme.load())->Aggregate(vecPublicKeys);
ret.fValid = true;
} catch (...) {
ret.fValid = false;
}
ret.cachedHash.SetNull();
return ret;
}
bool CBLSPublicKey::PublicKeyShare(Span<CBLSPublicKey> mpk, const CBLSId& _id)
{
fValid = false;
cachedHash.SetNull();
if (!_id.IsValid()) {
return false;
}
std::vector<bls::G1Element> mpkVec;
mpkVec.reserve(mpk.size());
for (const CBLSPublicKey& pk : mpk) {
if (!pk.IsValid()) {
return false;
}
mpkVec.emplace_back(pk.impl);
}
try {
impl = bls::Threshold::PublicKeyShare(mpkVec, bls::Bytes(_id.impl.begin(), _id.impl.size()));
} catch (...) {
return false;
}
fValid = true;
cachedHash.SetNull();
return true;
}
bool CBLSPublicKey::DHKeyExchange(const CBLSSecretKey& sk, const CBLSPublicKey& pk)
{
fValid = false;
cachedHash.SetNull();
if (!sk.IsValid() || !pk.IsValid()) {
return false;
}
impl = sk.impl * pk.impl;
fValid = true;
cachedHash.SetNull();
return true;
}
void CBLSSignature::AggregateInsecure(const CBLSSignature& o)
{
assert(IsValid() && o.IsValid());
try {
impl = Scheme(bls::bls_legacy_scheme.load())->Aggregate({impl, o.impl});
} catch (...) {
fValid = false;
}
cachedHash.SetNull();
}
CBLSSignature CBLSSignature::AggregateInsecure(Span<CBLSSignature> sigs)
{
if (sigs.empty()) {
return {};
}
std::vector<bls::G2Element> v;
v.reserve(sigs.size());
for (const auto& pk : sigs) {
v.emplace_back(pk.impl);
}
CBLSSignature ret;
try {
ret.impl = Scheme(bls::bls_legacy_scheme.load())->Aggregate(v);
ret.fValid = true;
} catch (...) {
ret.fValid = false;
}
ret.cachedHash.SetNull();
return ret;
}
CBLSSignature CBLSSignature::AggregateSecure(Span<CBLSSignature> sigs,
Span<CBLSPublicKey> pks,
const uint256& hash)
{
if (sigs.size() != pks.size() || sigs.empty()) {
return {};
}
std::vector<bls::G1Element> vecPublicKeys;
vecPublicKeys.reserve(pks.size());
for (const auto& pk : pks) {
vecPublicKeys.push_back(pk.impl);
}
std::vector<bls::G2Element> vecSignatures;
vecSignatures.reserve(pks.size());
for (const auto& sig : sigs) {
vecSignatures.push_back(sig.impl);
}
CBLSSignature ret;
try {
ret.impl = Scheme(bls::bls_legacy_scheme.load())->AggregateSecure(vecPublicKeys, vecSignatures, bls::Bytes(hash.begin(), hash.size()));
ret.fValid = true;
} catch (...) {
ret.fValid = false;
}
ret.cachedHash.SetNull();
return ret;
}
void CBLSSignature::SubInsecure(const CBLSSignature& o)
{
assert(IsValid() && o.IsValid());
impl = impl + o.impl.Negate();
cachedHash.SetNull();
}
bool CBLSSignature::VerifyInsecure(const CBLSPublicKey& pubKey, const uint256& hash, const bool specificLegacyScheme) const
{
if (!IsValid() || !pubKey.IsValid()) {
return false;
}
try {
return Scheme(specificLegacyScheme)->Verify(pubKey.impl, bls::Bytes(hash.begin(), hash.size()), impl);
} catch (...) {
return false;
}
}
bool CBLSSignature::VerifyInsecure(const CBLSPublicKey& pubKey, const uint256& hash) const
{
return VerifyInsecure(pubKey, hash, bls::bls_legacy_scheme.load());
}
bool CBLSSignature::VerifyInsecureAggregated(Span<CBLSPublicKey> pubKeys, Span<uint256> hashes) const
{
if (!IsValid()) {
return false;
}
assert(!pubKeys.empty() && !hashes.empty() && pubKeys.size() == hashes.size());
std::vector<bls::G1Element> pubKeyVec;
std::vector<bls::Bytes> hashes2;
hashes2.reserve(hashes.size());
pubKeyVec.reserve(pubKeys.size());
for (size_t i = 0; i < pubKeys.size(); i++) {
const auto& p = pubKeys[i];
if (!p.IsValid()) {
return false;
}
pubKeyVec.push_back(p.impl);
hashes2.emplace_back(hashes[i].begin(), hashes[i].size());
}
try {
return Scheme(bls::bls_legacy_scheme.load())->AggregateVerify(pubKeyVec, hashes2, impl);
} catch (...) {
return false;
}
}
bool CBLSSignature::VerifySecureAggregated(Span<CBLSPublicKey> pks, const uint256& hash) const
{
if (pks.empty()) {
return false;
}
std::vector<bls::G1Element> vecPublicKeys;
vecPublicKeys.reserve(pks.size());
for (const auto& pk : pks) {
vecPublicKeys.push_back(pk.impl);
}
try {
return Scheme(bls::bls_legacy_scheme.load())->VerifySecure(vecPublicKeys, impl, bls::Bytes(hash.begin(), hash.size()));
} catch (...) {
return false;
}
}
bool CBLSSignature::Recover(Span<CBLSSignature> sigs, Span<CBLSId> ids)
{
fValid = false;
cachedHash.SetNull();
if (sigs.empty() || ids.empty() || sigs.size() != ids.size()) {
return false;
}
std::vector<bls::G2Element> sigsVec;
std::vector<bls::Bytes> idsVec;
sigsVec.reserve(sigs.size());
idsVec.reserve(sigs.size());
for (size_t i = 0; i < sigs.size(); i++) {
if (!sigs[i].IsValid() || !ids[i].IsValid()) {
return false;
}
sigsVec.emplace_back(sigs[i].impl);
idsVec.emplace_back(ids[i].impl.begin(), ids[i].impl.size());
}
try {
impl = bls::Threshold::SignatureRecover(sigsVec, idsVec);
} catch (...) {
return false;
}
fValid = true;
cachedHash.SetNull();
return true;
}
#ifndef BUILD_BITCOIN_INTERNAL
static std::once_flag init_flag;
static mt_pooled_secure_allocator<uint8_t>* secure_allocator_instance;
static void create_secure_allocator()
{
// make sure LockedPoolManager is initialized first (ensures destruction order)
LockedPoolManager::Instance();
// static variable in function scope ensures it's initialized when first accessed
// and destroyed before LockedPoolManager
static mt_pooled_secure_allocator<uint8_t> a(sizeof(bn_t) + sizeof(size_t));
secure_allocator_instance = &a;
}
static mt_pooled_secure_allocator<uint8_t>& get_secure_allocator()
{
std::call_once(init_flag, create_secure_allocator);
return *secure_allocator_instance;
}
static void* secure_allocate(size_t n)
{
uint8_t* ptr = get_secure_allocator().allocate(n + sizeof(size_t));
*reinterpret_cast<size_t*>(ptr) = n;
return ptr + sizeof(size_t);
}
static void secure_free(void* p)
{
if (p == nullptr) {
return;
}
uint8_t* ptr = reinterpret_cast<uint8_t*>(p) - sizeof(size_t);
size_t n = *reinterpret_cast<size_t*>(ptr);
return get_secure_allocator().deallocate(ptr, n);
}
#endif
bool BLSInit()
{
#ifndef BUILD_BITCOIN_INTERNAL
bls::BLS::SetSecureAllocator(secure_allocate, secure_free);
#endif
return true;
}