Coverage Report

Created: 2022-08-24 06:31

/src/cryptofuzz/components.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <cryptofuzz/crypto.h>
2
#include <cryptofuzz/generic.h>
3
#include <cryptofuzz/components.h>
4
#include <cryptofuzz/util.h>
5
#include <boost/multiprecision/cpp_int.hpp>
6
#include <cryptofuzz/repository.h>
7
#include "third_party/json/json.hpp"
8
#include "config.h"
9
10
namespace cryptofuzz {
11
12
/* Type */
13
14
Type::Type(Datasource& ds) :
15
    type ( ds.Get<uint64_t>(0) )
16
62.1k
{ }
17
18
Type::Type(const Type& other) :
19
    type(other.type)
20
210k
{ }
21
22
Type::Type(nlohmann::json json) :
23
    type(json.get<uint64_t>())
24
0
{ }
25
26
131k
uint64_t Type::Get(void) const {
27
131k
    return type;
28
131k
}
29
30
3.91k
bool Type::Is(const uint64_t t) const {
31
3.91k
    return type == t;
32
3.91k
}
33
34
0
bool Type::Is(const std::vector<uint64_t> t) const {
35
0
    return std::find(t.begin(), t.end(), type) != t.end();
36
0
}
37
38
0
nlohmann::json Type::ToJSON(void) const {
39
0
    nlohmann::json j;
40
    /* Store as string, not as number, because JavaScript's number
41
     * type has only 53 bits of precision.
42
     */
43
0
    j = std::to_string(type);
44
0
    return j;
45
0
}
46
47
0
bool Type::operator==(const Type& rhs) const {
48
0
    return type == rhs.type;
49
0
}
50
51
0
void Type::Serialize(Datasource& ds) const {
52
0
    ds.Put<>(type);
53
0
}
54
55
/* Buffer */
56
57
Buffer::Buffer(Datasource& ds) :
58
    data( ds.GetData(0, 0, (10*1024*1024)) )
59
236k
{ }
60
61
0
Buffer::Buffer(nlohmann::json json) {
62
0
    const auto s = json.get<std::string>();
63
0
    boost::algorithm::unhex(s, std::back_inserter(data));
64
0
}
65
66
Buffer::Buffer(const std::vector<uint8_t>& data) :
67
    data(data)
68
853
{ }
69
70
Buffer::Buffer(const uint8_t* data, const size_t size) :
71
    data(data, data + size)
72
73.8k
{ }
73
74
997
Buffer::Buffer(void) { }
75
76
1.99k
std::vector<uint8_t> Buffer::Get(void) const {
77
1.99k
    return data;
78
1.99k
}
79
80
76.9k
const uint8_t* Buffer::GetPtr(fuzzing::datasource::Datasource* ds) const {
81
76.9k
    if ( data.size() == 0 ) {
82
16.0k
        return util::GetNullPtr(ds);
83
60.9k
    } else {
84
60.9k
        return data.data();
85
60.9k
    }
86
76.9k
}
87
88
159k
std::vector<uint8_t>& Buffer::GetVectorPtr(void) {
89
159k
    return data;
90
159k
}
91
92
16.2k
const std::vector<uint8_t>& Buffer::GetConstVectorPtr(void) const {
93
16.2k
    return data;
94
16.2k
}
95
96
126k
size_t Buffer::GetSize(void) const {
97
126k
    return data.size();
98
126k
}
99
100
17.3k
bool Buffer::operator==(const Buffer& rhs) const {
101
17.3k
    return data == rhs.data;
102
17.3k
}
103
104
0
nlohmann::json Buffer::ToJSON(void) const {
105
0
    nlohmann::json j;
106
0
    std::string asHex;
107
0
    boost::algorithm::hex(data, std::back_inserter(asHex));
108
0
    j = asHex;
109
0
    return j;
110
0
}
111
112
379
std::string Buffer::ToHex(void) const {
113
379
    std::string asHex;
114
379
    boost::algorithm::hex(data, std::back_inserter(asHex));
115
379
    return asHex;
116
379
}
117
118
0
void Buffer::Serialize(Datasource& ds) const {
119
0
    ds.PutData(data);
120
0
}
121
122
0
Datasource Buffer::AsDatasource(void) const {
123
0
    return Datasource(data.data(), data.size());
124
0
}
125
126
0
std::string Buffer::AsString(void) const {
127
0
    return std::string(data.data(), data.data() + data.size());
128
0
}
129
130
0
Buffer Buffer::ECDSA_Pad(const size_t retSize) const {
131
0
    size_t bufSize = GetSize();
132
133
0
    if ( bufSize > retSize ) {
134
0
        bufSize = retSize;
135
0
    }
136
137
0
    std::vector<uint8_t> ret(retSize);
138
139
0
    if ( retSize != 0 ) {
140
0
        const size_t delta = retSize - bufSize;
141
142
0
        if ( delta != 0 ) {
143
0
            memset(ret.data(), 0, delta);
144
0
        }
145
146
0
        if ( bufSize != 0 ) {
147
0
            memcpy(ret.data() + delta, GetPtr(), bufSize);
148
0
        }
149
0
    }
150
151
0
    return Buffer(ret);
152
0
}
153
154
/* Randomly modify an ECDSA input in such a way that it remains equivalent
155
 * to ECDSA verify/sign functions
156
 */
157
853
Buffer Buffer::ECDSA_RandomPad(Datasource& ds, const Type& curveType) const {
158
853
    const auto numBits = cryptofuzz::repository::ECC_CurveToBits(curveType.Get());
159
853
    if ( numBits == std::nullopt ) {
160
        /* The size of this curve is not known, so return the original buffer */
161
203
        return Buffer(data);
162
203
    }
163
164
650
    if ( *numBits % 8 != 0 ) {
165
        /* Curve sizes which are not a byte multiple are currently not supported,
166
         * so return the original buffer
167
         */
168
79
        return Buffer(data);
169
79
    }
170
171
571
    const size_t numBytes = (*numBits + 7) / 8;
172
173
571
    std::vector<uint8_t> stripped;
174
571
    {
175
571
        size_t startPos;
176
571
        const size_t endPos = GetSize() > numBytes ? numBytes : GetSize();
177
178
982
        for (startPos = 0; startPos < endPos; startPos++) {
179
919
            if ( data[startPos] != 0 ) {
180
508
                break;
181
508
            }
182
919
        }
183
571
        const auto& ref = GetConstVectorPtr();
184
185
571
        stripped.insert(std::end(stripped), std::begin(ref) + startPos, std::begin(ref) + endPos);
186
571
    }
187
188
    /* Decide how many bytes to insert */
189
571
    uint16_t numInserts = 0;
190
571
    try {
191
571
        numInserts = ds.Get<uint16_t>();
192
571
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
193
194
571
    std::vector<uint8_t> ret;
195
196
    /* Left-pad the input until it is the curve size */
197
571
    {
198
571
        if ( stripped.size() < numBytes ) {
199
556
            const size_t needed = numBytes - stripped.size();
200
556
            const std::vector<uint8_t> zeroes(numInserts > needed ? needed : numInserts, 0);
201
556
            ret.insert(std::end(ret), std::begin(zeroes), std::end(zeroes));
202
556
            numInserts -= zeroes.size();
203
556
        }
204
571
    }
205
206
    /* Insert the input */
207
571
    ret.insert(std::end(ret), std::begin(stripped), std::end(stripped));
208
209
    /* Right-pad the input with random bytes (if available) or zeroes */
210
571
    if ( numInserts > 0 ) {
211
51
        std::vector<uint8_t> toInsert;
212
51
        try {
213
51
            toInsert = ds.GetData(0, numInserts, numInserts);
214
51
        } catch ( fuzzing::datasource::Datasource::OutOfData ) {
215
43
            toInsert = std::vector<uint8_t>(numInserts, 0);
216
43
        }
217
51
        ret.insert(std::end(ret), std::begin(toInsert), std::end(toInsert));
218
51
    }
219
220
571
    return Buffer(ret);
221
571
}
222
223
0
Buffer Buffer::SHA256(void) const {
224
0
    const auto hash = crypto::sha256(Get());
225
0
    return Buffer(hash);
226
0
}
227
228
0
bool Buffer::IsZero(void) const {
229
0
    for (size_t i = 0; i < data.size(); i++) {
230
0
        if ( data[i] != 0 ) {
231
0
            return false;
232
0
        }
233
0
    }
234
235
0
    return true;
236
0
}
237
238
/* Bignum */
239
240
Bignum::Bignum(Datasource& ds) :
241
148k
    data(ds) {
242
148k
    transform();
243
148k
}
244
245
Bignum::Bignum(nlohmann::json json) :
246
    Bignum(json.get<std::string>())
247
0
{
248
0
}
249
250
Bignum::Bignum(const std::string s) :
251
    data((const uint8_t*)s.data(), s.size())
252
7.68k
{ }
253
254
148k
void Bignum::transform(void) {
255
148k
    auto& ptr = data.GetVectorPtr();
256
257
13.7M
    for (size_t i = 0; i < ptr.size(); i++) {
258
13.5M
        if ( isdigit(ptr[i]) ) continue;
259
7.06M
        if ( config::kNegativeIntegers == true ) {
260
0
            if ( i == 0 && ptr[i] == '-') continue;
261
0
        }
262
7.06M
        ptr[i] %= 10;
263
7.06M
        ptr[i] += '0';
264
7.06M
    }
265
148k
}
266
267
1.03k
bool Bignum::operator==(const Bignum& rhs) const {
268
1.03k
    return data == rhs.data;
269
1.03k
}
270
271
19.1k
size_t Bignum::GetSize(void) const {
272
19.1k
    return data.GetSize();
273
19.1k
}
274
275
0
bool Bignum::IsZero(void) const {
276
0
    return ToTrimmedString() == "0";
277
0
}
278
279
20.2k
bool Bignum::IsNegative(void) const {
280
20.2k
    return data.GetSize() && data.GetConstVectorPtr()[0] == '-';
281
20.2k
}
282
283
23
bool Bignum::IsGreaterThan(const std::string& other) const {
284
23
    CF_ASSERT(IsNegative() == false, "IsGreaterThan on negative numbers not supported");
285
23
    const auto s = ToTrimmedString();
286
23
    if ( s.size() > other.size() ) {
287
2
        return true;
288
21
    } else if ( s.size() < other.size() ) {
289
0
        return false;
290
21
    } else {
291
22
        for (size_t i = 0; i < s.size(); i++) {
292
22
            const int a = s[i];
293
22
            const int b = other[i];
294
22
            if ( a > b ) {
295
11
                return true;
296
11
            } else if ( a < b ) {
297
10
                return false;
298
10
            }
299
22
        }
300
21
    }
301
302
0
    CF_ASSERT(s == other, "Logic error");
303
0
    return false;
304
0
}
305
306
52
bool Bignum::IsLessThan(const std::string& other) const {
307
52
    boost::multiprecision::cpp_int A(ToTrimmedString());
308
52
    boost::multiprecision::cpp_int B(other);
309
52
    return A < B;
310
52
}
311
312
0
void Bignum::ToPositive(void) {
313
0
    if ( !IsNegative() ) {
314
0
        return;
315
0
    }
316
317
0
    data.GetVectorPtr().erase(data.GetVectorPtr().begin());
318
0
}
319
320
13
void Bignum::SubFrom(const std::string& v) {
321
13
    boost::multiprecision::cpp_int A(ToTrimmedString());
322
13
    boost::multiprecision::cpp_int B(v);
323
13
    boost::multiprecision::cpp_int res = B - A;
324
13
    const auto s = res.str();
325
13
    data = {(const uint8_t*)s.data(), s.size()};
326
13
}
327
328
27.8k
std::string Bignum::ToString(void) const {
329
27.8k
    const auto ptr = data.GetPtr();
330
27.8k
    return std::string(ptr, ptr + data.GetSize());
331
27.8k
}
332
333
27.8k
std::string Bignum::ToTrimmedString(void) const {
334
27.8k
    auto s = ToString();
335
27.8k
    trim_left_if(s, boost::is_any_of("0"));
336
337
27.8k
    if ( s == "" ) {
338
5.62k
        return "0";
339
22.2k
    } else {
340
22.2k
        return s;
341
22.2k
    }
342
27.8k
}
343
344
/* Prefix the string with a pseudo-random amount of zeroes */
345
15.0k
std::string Bignum::ToString(Datasource& ds) const {
346
15.0k
    std::string zeros;
347
348
15.0k
    try {
349
122k
        while ( ds.Get<bool>() == true ) {
350
107k
            zeros += "0";
351
107k
        }
352
15.0k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
353
354
15.0k
    auto s = ToTrimmedString();
355
15.0k
    const bool isNegative = IsNegative();
356
15.0k
    if ( s.size() && s[0] == '-' ) {
357
0
        s.erase(0, 1);
358
0
    }
359
15.0k
    return (isNegative ? "-" : "") + zeros + s;
360
15.0k
}
361
362
0
std::optional<std::vector<uint8_t>> Bignum::ToBin(std::optional<size_t> size) const {
363
0
    return util::DecToBin(ToTrimmedString(), size);
364
0
}
365
366
0
nlohmann::json Bignum::ToJSON(void) const {
367
0
    return ToString();
368
0
}
369
370
0
void Bignum::Serialize(Datasource& ds) const {
371
0
    data.Serialize(ds);
372
0
}
373
374
namespace component {
375
/* SymmetricCipher */
376
377
SymmetricCipher::SymmetricCipher(Datasource& ds) :
378
    iv(ds),
379
    key(ds),
380
    cipherType(ds)
381
15.8k
{ }
382
383
SymmetricCipher::SymmetricCipher(nlohmann::json json) :
384
    iv(json["iv"]),
385
    key(json["key"]),
386
    cipherType(json["cipherType"])
387
0
{ }
388
389
0
nlohmann::json SymmetricCipher::ToJSON(void) const {
390
0
    nlohmann::json j;
391
0
    j["iv"] = iv.ToJSON();
392
0
    j["key"] = key.ToJSON();
393
0
    j["cipherType"] = cipherType.ToJSON();
394
0
    return j;
395
0
}
396
397
0
bool SymmetricCipher::operator==(const SymmetricCipher& rhs) const {
398
0
    return
399
0
        (iv == rhs.iv) &&
400
0
        (key == rhs.key) &&
401
0
        (cipherType == rhs.cipherType);
402
0
}
403
0
void SymmetricCipher::Serialize(Datasource& ds) const {
404
0
    iv.Serialize(ds);
405
0
    key.Serialize(ds);
406
0
    cipherType.Serialize(ds);
407
0
}
408
409
/* Ciphertext */
410
411
Ciphertext::Ciphertext(Datasource& ds) :
412
    ciphertext(ds),
413
    tag( ds.Get<bool>() ? std::nullopt : std::make_optional<Tag>(ds) )
414
0
{ }
415
416
Ciphertext::Ciphertext(Buffer ciphertext, std::optional<Tag> tag) :
417
    ciphertext(ciphertext),
418
    tag(tag)
419
1.12k
{ }
420
421
812
bool Ciphertext::operator==(const Ciphertext& rhs) const {
422
812
    return (ciphertext == rhs.ciphertext) && (tag == rhs.tag);
423
812
}
424
425
0
void Ciphertext::Serialize(Datasource& ds) const {
426
0
    ciphertext.Serialize(ds);
427
0
    if ( tag == std::nullopt ) {
428
0
        ds.Put<bool>(true);
429
0
    } else {
430
0
        ds.Put<bool>(false);
431
0
        tag->Serialize(ds);
432
0
    }
433
0
}
434
435
/* BignumPair */
436
437
BignumPair::BignumPair(Datasource& ds) :
438
    first(ds),
439
    second(ds)
440
31.7k
{ }
441
442
BignumPair::BignumPair(const std::string first, const std::string second) :
443
    first(first),
444
    second(second)
445
2.35k
{ }
446
447
BignumPair::BignumPair(nlohmann::json json) :
448
    first(json[0].get<std::string>()),
449
    second(json[1].get<std::string>())
450
0
{ }
451
452
453
138
bool BignumPair::operator==(const BignumPair& rhs) const {
454
138
    return
455
138
        (first == rhs.first) &&
456
138
        (second == rhs.second);
457
138
}
458
459
0
void BignumPair::Serialize(Datasource& ds) const {
460
0
    first.Serialize(ds);
461
0
    second.Serialize(ds);
462
0
}
463
464
0
nlohmann::json BignumPair::ToJSON(void) const {
465
0
    return std::vector<nlohmann::json>{first.ToJSON(), second.ToJSON()};
466
0
}
467
468
/* ECC_KeyPair */
469
470
ECC_KeyPair::ECC_KeyPair(Datasource& ds) :
471
    priv(ds),
472
    pub(ds)
473
0
{ }
474
475
ECC_KeyPair::ECC_KeyPair(ECC_PrivateKey priv, BignumPair pub) :
476
    priv(priv),
477
    pub(pub)
478
809
{ }
479
480
0
bool ECC_KeyPair::operator==(const ECC_KeyPair& rhs) const {
481
0
    return
482
0
        (priv == rhs.priv) &&
483
0
        (pub == rhs.pub);
484
0
}
485
486
0
void ECC_KeyPair::Serialize(Datasource& ds) const {
487
0
    priv.Serialize(ds);
488
0
    pub.Serialize(ds);
489
0
}
490
491
0
nlohmann::json ECC_KeyPair::ToJSON(void) const {
492
0
    return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()};
493
0
}
494
495
/* ECDSA_Signature */
496
ECDSA_Signature::ECDSA_Signature(Datasource& ds) :
497
    signature(ds),
498
    pub(ds)
499
2.03k
{ }
500
501
ECDSA_Signature::ECDSA_Signature(BignumPair signature, ECC_PublicKey pub) :
502
    signature(signature),
503
    pub(pub)
504
379
{ }
505
506
ECDSA_Signature::ECDSA_Signature(nlohmann::json json) :
507
    signature(json["signature"]),
508
    pub(json["pub"])
509
0
{ }
510
511
0
bool ECDSA_Signature::operator==(const ECDSA_Signature& rhs) const {
512
0
    return
513
0
        (signature == rhs.signature) &&
514
0
        (pub == rhs.pub);
515
0
}
516
517
0
void ECDSA_Signature::Serialize(Datasource& ds) const {
518
0
    signature.Serialize(ds);
519
0
    pub.Serialize(ds);
520
0
}
521
522
0
nlohmann::json ECDSA_Signature::ToJSON(void) const {
523
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
524
0
}
525
526
/* MACType */
527
528
MACType::MACType(Datasource& ds) :
529
    mode(ds.Get<bool>()),
530
    type(ds)
531
675
{ }
532
533
MACType::MACType(nlohmann::json json) :
534
    mode(json["mode"].get<bool>()),
535
    type(json["type"])
536
0
{ }
537
538
0
nlohmann::json MACType::ToJSON(void) const {
539
0
    nlohmann::json j;
540
0
    j["mode"] = mode;
541
0
    j["type"] = type.ToJSON();
542
0
    return j;
543
0
}
544
545
0
bool MACType::operator==(const MACType& rhs) const {
546
0
    return
547
0
        (mode == rhs.mode) &&
548
0
        (type == rhs.type);
549
0
}
550
551
0
void MACType::Serialize(Datasource& ds) const {
552
0
    ds.Put<>(mode);
553
0
    type.Serialize(ds);
554
0
}
555
556
G2::G2(nlohmann::json json) :
557
    first(json[0]),
558
0
    second(json[1]) {
559
0
}
560
561
0
nlohmann::json G2::ToJSON(void) const {
562
0
    return std::vector<nlohmann::json>{
563
0
        first.first.ToJSON(), first.second.ToJSON(),
564
0
        second.first.ToJSON(), second.second.ToJSON()
565
0
    };
566
0
}
567
568
0
void G2::Serialize(Datasource& ds) const {
569
0
    first.Serialize(ds);
570
0
    second.Serialize(ds);
571
0
}
572
573
0
nlohmann::json Fp12::ToJSON(void) const {
574
0
    return std::vector<nlohmann::json>{
575
0
        bn1.ToJSON(),
576
0
        bn2.ToJSON(),
577
0
        bn3.ToJSON(),
578
0
        bn4.ToJSON(),
579
0
        bn5.ToJSON(),
580
0
        bn6.ToJSON(),
581
0
        bn7.ToJSON(),
582
0
        bn8.ToJSON(),
583
0
        bn9.ToJSON(),
584
0
        bn10.ToJSON(),
585
0
        bn11.ToJSON(),
586
0
        bn12.ToJSON(),
587
0
    };
588
0
}
589
590
Fp12::Fp12(nlohmann::json json) :
591
    bn1(json[0].get<std::string>()),
592
    bn2(json[1].get<std::string>()),
593
    bn3(json[2].get<std::string>()),
594
    bn4(json[3].get<std::string>()),
595
    bn5(json[4].get<std::string>()),
596
    bn6(json[5].get<std::string>()),
597
    bn7(json[6].get<std::string>()),
598
    bn8(json[7].get<std::string>()),
599
    bn9(json[8].get<std::string>()),
600
    bn10(json[9].get<std::string>()),
601
    bn11(json[10].get<std::string>()),
602
    bn12(json[11].get<std::string>())
603
0
{ }
604
605
0
void Fp12::Serialize(Datasource& ds) const {
606
0
    bn1.Serialize(ds);
607
0
    bn2.Serialize(ds);
608
0
    bn3.Serialize(ds);
609
0
    bn4.Serialize(ds);
610
0
    bn5.Serialize(ds);
611
0
    bn6.Serialize(ds);
612
0
    bn7.Serialize(ds);
613
0
    bn8.Serialize(ds);
614
0
    bn9.Serialize(ds);
615
0
    bn10.Serialize(ds);
616
0
    bn11.Serialize(ds);
617
0
    bn12.Serialize(ds);
618
0
}
619
620
/* BLS_Signature */
621
BLS_Signature::BLS_Signature(Datasource& ds) :
622
    signature(ds),
623
    pub(ds)
624
0
{ }
625
626
BLS_Signature::BLS_Signature(G2 signature, ECC_PublicKey pub) :
627
    signature(signature),
628
    pub(pub)
629
0
{ }
630
631
BLS_Signature::BLS_Signature(nlohmann::json json) :
632
    signature(json["signature"]),
633
    pub(json["pub"])
634
0
{ }
635
636
0
bool BLS_Signature::operator==(const BLS_Signature& rhs) const {
637
0
    return
638
0
        (signature == rhs.signature) &&
639
0
        (pub == rhs.pub);
640
0
}
641
642
0
void BLS_Signature::Serialize(Datasource& ds) const {
643
0
    signature.Serialize(ds);
644
0
    pub.Serialize(ds);
645
0
}
646
647
0
nlohmann::json BLS_Signature::ToJSON(void) const {
648
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
649
0
}
650
651
/* BLS_BatchSign_Vector */
652
653
548
BLS_BatchSign_Vector::BLS_BatchSign_Vector(Datasource& ds) {
654
548
    const auto num = ds.Get<uint32_t>(0);
655
780
    for (size_t i = 0; i < num; i++) {
656
232
        c.push_back( BatchSign_single{{ds}, {ds}} );
657
232
    }
658
548
}
659
660
0
BLS_BatchSign_Vector::BLS_BatchSign_Vector(nlohmann::json json) {
661
0
    for (const auto& j : json) {
662
0
        c.push_back( BatchSign_single{
663
0
                j["priv"],
664
0
                {j["g1_x"], j["g1_y"]} });
665
0
    }
666
0
}
667
668
0
void BLS_BatchSign_Vector::Serialize(Datasource& ds) const {
669
0
    ds.Put<uint32_t>(c.size());
670
0
    for (const auto& component : c) {
671
0
        component.priv.Serialize(ds);
672
0
        component.g1.Serialize(ds);
673
0
    }
674
0
}
675
676
/* BLS_BatchSignature */
677
678
BLS_BatchSignature::BLS_BatchSignature(std::vector< std::pair<G1, G2> > msgpub) :
679
    msgpub(msgpub)
680
0
{ }
681
682
0
bool BLS_BatchSignature::operator==(const BLS_BatchSignature& rhs) const {
683
0
    return
684
0
        (msgpub == rhs.msgpub);
685
0
}
686
687
0
void BLS_BatchSignature::Serialize(Datasource& ds) const {
688
0
    ds.Put<uint32_t>(msgpub.size());
689
0
    for (const auto& component : msgpub) {
690
0
        component.first.Serialize(ds);
691
0
        component.second.Serialize(ds);
692
0
    }
693
0
}
694
695
0
nlohmann::json BLS_BatchSignature::ToJSON(void) const {
696
0
    return {}; /* TODO */
697
0
}
698
699
700
/* BLS_KeyPair */
701
702
BLS_KeyPair::BLS_KeyPair(Datasource& ds) :
703
    priv(ds),
704
    pub(ds)
705
0
{ }
706
707
BLS_KeyPair::BLS_KeyPair(BLS_PrivateKey priv, BignumPair pub) :
708
    priv(priv),
709
    pub(pub)
710
0
{ }
711
712
0
bool BLS_KeyPair::operator==(const BLS_KeyPair& rhs) const {
713
0
    return
714
0
        (priv == rhs.priv) &&
715
0
        (pub == rhs.pub);
716
0
}
717
718
0
void BLS_KeyPair::Serialize(Datasource& ds) const {
719
0
    priv.Serialize(ds);
720
0
    pub.Serialize(ds);
721
0
}
722
723
0
nlohmann::json BLS_KeyPair::ToJSON(void) const {
724
0
    return std::vector<nlohmann::json>{priv.ToJSON(), pub.ToJSON()};
725
0
}
726
727
/* BLS_BatchVerify_Vector */
728
729
510
BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(Datasource& ds) {
730
510
    const auto num = ds.Get<uint32_t>(0);
731
1.19k
    for (size_t i = 0; i < num; i++) {
732
681
        c.push_back( BatchVerify_single{{ds}, {ds}} );
733
681
    }
734
510
}
735
736
0
BLS_BatchVerify_Vector::BLS_BatchVerify_Vector(nlohmann::json json) {
737
0
    for (const auto& j : json) {
738
0
        c.push_back( BatchVerify_single{
739
0
                {j["g1_x"], j["g1_y"]},
740
0
                {j["g2_v"], j["g2_w"], j["g2_x"], j["g2_y"]} });
741
0
    }
742
0
}
743
744
0
void BLS_BatchVerify_Vector::Serialize(Datasource& ds) const {
745
0
    ds.Put<uint32_t>(c.size());
746
0
    for (const auto& component : c) {
747
0
        component.g1.Serialize(ds);
748
0
        component.g2.Serialize(ds);
749
0
    }
750
0
}
751
752
0
nlohmann::json BLS_BatchVerify_Vector::ToJSON(void) const {
753
0
    nlohmann::json j = nlohmann::json::array();
754
0
    for (const auto& cur : c) {
755
0
        nlohmann::json j;
756
0
        j["g1_x"] = cur.g1.first.ToJSON();
757
0
        j["g1_y"] = cur.g1.second.ToJSON();
758
759
0
        j["g2_v"] = cur.g2.first.first.ToJSON();
760
0
        j["g2_w"] = cur.g2.first.second.ToJSON();
761
0
        j["g2_x"] = cur.g2.second.first.ToJSON();
762
0
        j["g2_y"] = cur.g2.second.second.ToJSON();
763
0
    }
764
0
    return j;
765
0
}
766
767
/* BLS_G1_Vector */
768
769
309
BLS_G1_Vector::BLS_G1_Vector(Datasource& ds) {
770
309
    const auto num = ds.Get<uint32_t>(0);
771
819
    for (size_t i = 0; i < num; i++) {
772
510
        points.push_back( component::G1(ds) );
773
510
    }
774
309
}
775
776
0
BLS_G1_Vector::BLS_G1_Vector(nlohmann::json json) {
777
0
    for (const auto& j : json) {
778
0
        points.push_back( component::G1{j["x"], j["y"]} );
779
0
    }
780
0
}
781
782
0
void BLS_G1_Vector::Serialize(Datasource& ds) const {
783
0
    ds.Put<uint32_t>(points.size());
784
0
    for (const auto& signature : points) {
785
0
        signature.Serialize(ds);
786
0
    }
787
0
}
788
789
/* BLS_G2_Vector */
790
791
309
BLS_G2_Vector::BLS_G2_Vector(Datasource& ds) {
792
309
    const auto num = ds.Get<uint32_t>(0);
793
365
    for (size_t i = 0; i < num; i++) {
794
56
        points.push_back( component::G2(ds) );
795
56
    }
796
309
}
797
798
0
BLS_G2_Vector::BLS_G2_Vector(nlohmann::json json) {
799
0
    for (const auto& j : json) {
800
0
        points.push_back( component::G2{j["v"], j["w"], j["x"], j["y"]} );
801
0
    }
802
0
}
803
804
0
void BLS_G2_Vector::Serialize(Datasource& ds) const {
805
0
    ds.Put<uint32_t>(points.size());
806
0
    for (const auto& signature : points) {
807
0
        signature.Serialize(ds);
808
0
    }
809
0
}
810
811
/* SR25519_Signature */
812
SR25519_Signature::SR25519_Signature(Datasource& ds) :
813
    signature(ds),
814
    pub(ds)
815
245
{ }
816
817
SR25519_Signature::SR25519_Signature(BignumPair signature, Bignum pub) :
818
    signature(signature),
819
    pub(pub)
820
0
{ }
821
822
SR25519_Signature::SR25519_Signature(nlohmann::json json) :
823
    signature(json["signature"]),
824
    pub(json["pub"])
825
0
{ }
826
827
0
bool SR25519_Signature::operator==(const SR25519_Signature& rhs) const {
828
0
    return
829
0
        (signature == rhs.signature) &&
830
0
        (pub == rhs.pub);
831
0
}
832
833
0
void SR25519_Signature::Serialize(Datasource& ds) const {
834
0
    signature.Serialize(ds);
835
0
    pub.Serialize(ds);
836
0
}
837
838
0
nlohmann::json SR25519_Signature::ToJSON(void) const {
839
0
    return std::vector<nlohmann::json>{signature.ToJSON(), pub.ToJSON()};
840
0
}
841
842
} /* namespace component */
843
844
} /* namespace cryptofuzz */