@@ -123,17 +123,25 @@ class CBLSWrapper
123
123
cachedHash.SetNull ();
124
124
}
125
125
126
- std::vector<uint8_t > ToByteVector (const bool specificLegacyScheme) const
126
+ std::array<uint8_t , SerSize> ToBytes (const bool specificLegacyScheme) const
127
+ {
128
+ if (!fValid ) {
129
+ return std::array<uint8_t , SerSize>{};
130
+ }
131
+ return impl.SerializeToArray (specificLegacyScheme);
132
+ }
133
+
134
+ std::vector<uint8_t > ToActualByteVector (const bool specificLegacyScheme) const
127
135
{
128
136
if (!fValid ) {
129
137
return std::vector<uint8_t >(SerSize, 0 );
130
138
}
131
139
return impl.Serialize (specificLegacyScheme);
132
140
}
133
141
134
- std::vector <uint8_t > ToByteVector () const
142
+ std::array <uint8_t , SerSize> ToBytes () const
135
143
{
136
- return ToByteVector (bls::bls_legacy_scheme.load ());
144
+ return ToBytes (bls::bls_legacy_scheme.load ());
137
145
}
138
146
139
147
const uint256& GetHash () const
@@ -167,7 +175,7 @@ class CBLSWrapper
167
175
template <typename Stream>
168
176
inline void Serialize (Stream& s, const bool specificLegacyScheme) const
169
177
{
170
- s.write (AsBytes (Span{ToByteVector (specificLegacyScheme). data (), SerSize }));
178
+ s.write (AsBytes (Span{ToBytes (specificLegacyScheme)}));
171
179
}
172
180
173
181
template <typename Stream>
@@ -206,7 +214,7 @@ class CBLSWrapper
206
214
207
215
inline bool CheckMalleable (Span<uint8_t > vecBytes, const bool specificLegacyScheme) const
208
216
{
209
- if (memcmp (vecBytes.data (), ToByteVector (specificLegacyScheme).data (), SerSize)) {
217
+ if (memcmp (vecBytes.data (), ToBytes (specificLegacyScheme).data (), SerSize)) {
210
218
// TODO not sure if this is actually possible with the BLS libs. I'm assuming here that somewhere deep inside
211
219
// these libs masking might happen, so that 2 different binary representations could result in the same object
212
220
// representation
@@ -222,7 +230,7 @@ class CBLSWrapper
222
230
223
231
inline std::string ToString (const bool specificLegacyScheme) const
224
232
{
225
- std::vector< uint8_t > buf = ToByteVector (specificLegacyScheme);
233
+ auto buf = ToBytes (specificLegacyScheme);
226
234
return HexStr (buf);
227
235
}
228
236
@@ -245,10 +253,12 @@ struct CBLSIdImplicit : public uint256
245
253
memcpy (instance.begin (), buffer, sizeof (CBLSIdImplicit));
246
254
return instance;
247
255
}
248
- [[nodiscard]] std::vector<uint8_t > Serialize (const bool fLegacy ) const
256
+ [[nodiscard]] std::vector<uint8_t > SerializeToVec (const bool fLegacy ) const
249
257
{
250
258
return {begin (), end ()};
251
259
}
260
+ [[nodiscard]] std::array<uint8_t , WIDTH> Serialize (const bool fLegacy ) const { return m_data; }
261
+ [[nodiscard]] std::array<uint8_t , WIDTH> SerializeToArray (const bool fLegacy ) const { return Serialize (fLegacy ); }
252
262
};
253
263
254
264
class CBLSId : public CBLSWrapper <CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
@@ -396,7 +406,7 @@ class CBLSLazyWrapper
396
406
private:
397
407
mutable std::mutex mutex;
398
408
399
- mutable std::vector <uint8_t > vecBytes;
409
+ mutable std::array <uint8_t , BLSObject::SerSize > vecBytes{} ;
400
410
mutable bool bufValid{false };
401
411
mutable bool bufLegacyScheme{true };
402
412
@@ -407,7 +417,6 @@ class CBLSLazyWrapper
407
417
408
418
public:
409
419
CBLSLazyWrapper () :
410
- vecBytes (BLSObject::SerSize, 0 ),
411
420
bufLegacyScheme (bls::bls_legacy_scheme.load())
412
421
{}
413
422
@@ -425,7 +434,6 @@ class CBLSLazyWrapper
425
434
if (r.bufValid ) {
426
435
vecBytes = r.vecBytes ;
427
436
} else {
428
- vecBytes.resize (BLSObject::SerSize);
429
437
std::fill (vecBytes.begin (), vecBytes.end (), 0 );
430
438
}
431
439
objInitialized = r.objInitialized ;
@@ -448,10 +456,9 @@ class CBLSLazyWrapper
448
456
{
449
457
std::unique_lock<std::mutex> l (mutex);
450
458
if (!objInitialized && !bufValid) {
451
- vecBytes.resize (BLSObject::SerSize);
452
459
std::fill (vecBytes.begin (), vecBytes.end (), 0 );
453
460
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
454
- vecBytes = obj.ToByteVector (specificLegacyScheme);
461
+ vecBytes = obj.ToBytes (specificLegacyScheme);
455
462
bufValid = true ;
456
463
bufLegacyScheme = specificLegacyScheme;
457
464
hash.SetNull ();
@@ -533,11 +540,10 @@ class CBLSLazyWrapper
533
540
{
534
541
std::unique_lock<std::mutex> l (mutex);
535
542
if (!objInitialized && !bufValid) {
536
- vecBytes.resize (BLSObject::SerSize);
537
543
std::fill (vecBytes.begin (), vecBytes.end (), 0 );
538
544
hash.SetNull ();
539
545
} else if (!bufValid) {
540
- vecBytes = obj.ToByteVector (bufLegacyScheme);
546
+ vecBytes = obj.ToBytes (bufLegacyScheme);
541
547
bufValid = true ;
542
548
hash.SetNull ();
543
549
}
0 commit comments