Coverage Report

Created: 2026-02-14 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz/modules/blst/module.cpp
Line
Count
Source
1
#include "module.h"
2
#include <cryptofuzz/util.h>
3
#include <cryptofuzz/repository.h>
4
#include <fuzzing/datasource/id.hpp>
5
#include <boost/multiprecision/cpp_int.hpp>
6
7
extern "C" {
8
#include <blst.h>
9
}
10
11
namespace cryptofuzz {
12
namespace module {
13
14
blst::blst(void) :
15
2
    Module("blst") {
16
2
}
17
18
namespace blst_detail {
19
    template <size_t Size>
20
12.3k
    void Reverse(std::array<uint8_t, Size>& v) {
21
12.3k
        std::reverse(v.begin(), v.end());
22
12.3k
    }
void cryptofuzz::module::blst_detail::Reverse<32ul>(std::__1::array<unsigned char, 32ul>&)
Line
Count
Source
20
1.35k
    void Reverse(std::array<uint8_t, Size>& v) {
21
1.35k
        std::reverse(v.begin(), v.end());
22
1.35k
    }
void cryptofuzz::module::blst_detail::Reverse<48ul>(std::__1::array<unsigned char, 48ul>&)
Line
Count
Source
20
11.0k
    void Reverse(std::array<uint8_t, Size>& v) {
21
11.0k
        std::reverse(v.begin(), v.end());
22
11.0k
    }
23
24
    template <size_t Size>
25
9.25k
    std::optional<std::array<uint8_t, Size>> ToArray(const component::Bignum& bn, const bool reverse = true) {
26
9.25k
        const auto _ret = util::DecToBin(bn.ToTrimmedString(), Size);
27
9.25k
        if ( _ret == std::nullopt ) {
28
741
            return std::nullopt;
29
741
        }
30
31
8.51k
        std::array<uint8_t, Size> ret;
32
8.51k
        memcpy(ret.data(), _ret->data(), Size);
33
8.51k
        if ( reverse == true ) {
34
8.33k
            Reverse<>(ret);
35
8.33k
        }
36
37
8.51k
        return ret;
38
9.25k
    }
std::__1::optional<std::__1::array<unsigned char, 32ul> > cryptofuzz::module::blst_detail::ToArray<32ul>(cryptofuzz::Bignum const&, bool)
Line
Count
Source
25
1.41k
    std::optional<std::array<uint8_t, Size>> ToArray(const component::Bignum& bn, const bool reverse = true) {
26
1.41k
        const auto _ret = util::DecToBin(bn.ToTrimmedString(), Size);
27
1.41k
        if ( _ret == std::nullopt ) {
28
281
            return std::nullopt;
29
281
        }
30
31
1.13k
        std::array<uint8_t, Size> ret;
32
1.13k
        memcpy(ret.data(), _ret->data(), Size);
33
1.13k
        if ( reverse == true ) {
34
1.13k
            Reverse<>(ret);
35
1.13k
        }
36
37
1.13k
        return ret;
38
1.41k
    }
std::__1::optional<std::__1::array<unsigned char, 48ul> > cryptofuzz::module::blst_detail::ToArray<48ul>(cryptofuzz::Bignum const&, bool)
Line
Count
Source
25
7.84k
    std::optional<std::array<uint8_t, Size>> ToArray(const component::Bignum& bn, const bool reverse = true) {
26
7.84k
        const auto _ret = util::DecToBin(bn.ToTrimmedString(), Size);
27
7.84k
        if ( _ret == std::nullopt ) {
28
460
            return std::nullopt;
29
460
        }
30
31
7.38k
        std::array<uint8_t, Size> ret;
32
7.38k
        memcpy(ret.data(), _ret->data(), Size);
33
7.38k
        if ( reverse == true ) {
34
7.20k
            Reverse<>(ret);
35
7.20k
        }
36
37
7.38k
        return ret;
38
7.84k
    }
39
1.18k
    bool To_blst_fr(const component::Bignum& bn, blst_fr& out) {
40
1.18k
        const auto ret = ToArray<32>(bn);
41
42
1.18k
        if ( ret == std::nullopt ) {
43
249
            return false;
44
249
        }
45
46
931
        CF_NORET(blst_fr_from_uint64(&out, (const uint64_t*)ret->data()));
47
931
        return true;
48
1.18k
    }
49
7.65k
    bool To_blst_fp(const component::Bignum& bn, blst_fp& out) {
50
7.65k
        const auto ret = ToArray<48>(bn);
51
52
7.65k
        if ( ret == std::nullopt ) {
53
453
            return false;
54
453
        }
55
56
7.20k
        CF_NORET(blst_fp_from_uint64(&out, (const uint64_t*)ret->data()));
57
7.20k
        return true;
58
7.65k
    }
59
0
    bool To_blst_fp2(const component::Fp2& bn, blst_fp2& out) {
60
0
        return  To_blst_fp(bn.first, out.fp[0]) &&
61
0
                To_blst_fp(bn.second, out.fp[1]);
62
0
    }
63
0
    bool To_blst_fp12(const component::Fp12& bn, blst_fp12& out) {
64
0
        return  To_blst_fp(bn.bn1, out.fp6[0].fp2[0].fp[0]) &&
65
0
                To_blst_fp(bn.bn2, out.fp6[0].fp2[0].fp[1]) &&
66
0
                To_blst_fp(bn.bn3, out.fp6[0].fp2[1].fp[0]) &&
67
0
                To_blst_fp(bn.bn4, out.fp6[0].fp2[1].fp[1]) &&
68
0
                To_blst_fp(bn.bn5, out.fp6[0].fp2[2].fp[0]) &&
69
0
                To_blst_fp(bn.bn6, out.fp6[0].fp2[2].fp[1]) &&
70
71
0
                To_blst_fp(bn.bn7, out.fp6[1].fp2[0].fp[0]) &&
72
0
                To_blst_fp(bn.bn8, out.fp6[1].fp2[0].fp[1]) &&
73
0
                To_blst_fp(bn.bn9, out.fp6[1].fp2[1].fp[0]) &&
74
0
                To_blst_fp(bn.bn10, out.fp6[1].fp2[1].fp[1]) &&
75
0
                To_blst_fp(bn.bn11, out.fp6[1].fp2[2].fp[0]) &&
76
0
                To_blst_fp(bn.bn12, out.fp6[1].fp2[2].fp[1]);
77
0
    }
78
226
    component::Bignum To_component_bignum(const blst_fr& in) {
79
226
        std::array<uint8_t, 32> v;
80
81
226
        blst_uint64_from_fr((uint64_t*)v.data(), &in);
82
83
226
        Reverse<>(v);
84
226
        return util::BinToDec(v.data(), 32);
85
226
    }
86
421
    component::Bignum To_component_bignum(const blst_fp& in) {
87
421
        std::array<uint8_t, 48> v;
88
89
421
        blst_uint64_from_fp((uint64_t*)v.data(), &in);
90
91
421
        Reverse<>(v);
92
421
        return util::BinToDec(v.data(), 48);
93
421
    }
94
0
    component::Fp2 To_component_Fp2(const blst_fp2& in) {
95
0
        std::array<uint8_t, 48> v1, v2;
96
97
0
        blst_uint64_from_fp((uint64_t*)v1.data(), &in.fp[0]);
98
0
        Reverse<>(v1);
99
100
0
        blst_uint64_from_fp((uint64_t*)v2.data(), &in.fp[1]);
101
0
        Reverse<>(v2);
102
103
0
        return {
104
0
            util::BinToDec(v1.data(), 48),
105
0
            util::BinToDec(v2.data(), 48),
106
0
        };
107
0
    }
108
260
    boost::multiprecision::cpp_int To_cpp_int(const component::Bignum& in) {
109
260
        return boost::multiprecision::cpp_int(in.ToTrimmedString());
110
260
    }
111
112
    class G1 {
113
        private:
114
            fuzzing::datasource::Datasource& ds;
115
            blst_p1_affine g1;
116
        public:
117
            G1(const blst_p1_affine& g1, fuzzing::datasource::Datasource& ds) :
118
843
                ds(ds) {
119
843
                memcpy(&this->g1, &g1, sizeof(g1));
120
843
            }
121
843
            const blst_p1_affine* Get(void) {
122
843
                return &g1;
123
843
            }
124
843
            component::G1 To_Component_G1(void) {
125
843
                std::array<uint8_t, 48> x, y;
126
127
843
                const blst_p1_affine* ptr = Get();
128
129
843
                blst_uint64_from_fp((uint64_t*)x.data(), &ptr->x);
130
843
                blst_uint64_from_fp((uint64_t*)y.data(), &ptr->y);
131
132
843
                Reverse<>(x);
133
843
                Reverse<>(y);
134
135
843
                return {util::BinToDec(x.data(), 48), util::BinToDec(y.data(), 48)};
136
843
            }
137
    };
138
334
    component::G2 To_G2(const blst_p2_affine& g2) {
139
334
        std::array<uint8_t, 48> x1, y1, x2, y2;
140
141
334
        blst_uint64_from_fp((uint64_t*)x1.data(), &g2.x.fp[0]);
142
334
        blst_uint64_from_fp((uint64_t*)y1.data(), &g2.y.fp[0]);
143
334
        blst_uint64_from_fp((uint64_t*)x2.data(), &g2.x.fp[1]);
144
334
        blst_uint64_from_fp((uint64_t*)y2.data(), &g2.y.fp[1]);
145
146
334
        Reverse<>(x1);
147
334
        Reverse<>(y1);
148
334
        Reverse<>(x2);
149
334
        Reverse<>(y2);
150
151
334
        return {
152
334
            util::BinToDec(x1.data(), 48),
153
334
            util::BinToDec(y1.data(), 48),
154
334
            util::BinToDec(x2.data(), 48),
155
334
            util::BinToDec(y2.data(), 48)
156
334
        };
157
334
    }
158
159
32
    component::Fp12 To_component_Fp12(const blst_fp12& fp12) {
160
32
        std::array<uint8_t, 48> bn1, bn2, bn3, bn4, bn5, bn6, bn7, bn8, bn9, bn10, bn11, bn12;
161
162
32
        blst_uint64_from_fp((uint64_t*)bn1.data(), &fp12.fp6[0].fp2[0].fp[0]);
163
32
        Reverse<>(bn1);
164
165
32
        blst_uint64_from_fp((uint64_t*)bn2.data(), &fp12.fp6[0].fp2[0].fp[1]);
166
32
        Reverse<>(bn2);
167
168
32
        blst_uint64_from_fp((uint64_t*)bn3.data(), &fp12.fp6[0].fp2[1].fp[0]);
169
32
        Reverse<>(bn3);
170
171
32
        blst_uint64_from_fp((uint64_t*)bn4.data(), &fp12.fp6[0].fp2[1].fp[1]);
172
32
        Reverse<>(bn4);
173
174
32
        blst_uint64_from_fp((uint64_t*)bn5.data(), &fp12.fp6[0].fp2[2].fp[0]);
175
32
        Reverse<>(bn5);
176
177
32
        blst_uint64_from_fp((uint64_t*)bn6.data(), &fp12.fp6[0].fp2[2].fp[1]);
178
32
        Reverse<>(bn6);
179
180
32
        blst_uint64_from_fp((uint64_t*)bn7.data(), &fp12.fp6[1].fp2[0].fp[0]);
181
32
        Reverse<>(bn7);
182
183
32
        blst_uint64_from_fp((uint64_t*)bn8.data(), &fp12.fp6[1].fp2[0].fp[1]);
184
32
        Reverse<>(bn8);
185
186
32
        blst_uint64_from_fp((uint64_t*)bn9.data(), &fp12.fp6[1].fp2[1].fp[0]);
187
32
        Reverse<>(bn9);
188
189
32
        blst_uint64_from_fp((uint64_t*)bn10.data(), &fp12.fp6[1].fp2[1].fp[1]);
190
32
        Reverse<>(bn10);
191
192
32
        blst_uint64_from_fp((uint64_t*)bn11.data(), &fp12.fp6[1].fp2[2].fp[0]);
193
32
        Reverse<>(bn11);
194
195
32
        blst_uint64_from_fp((uint64_t*)bn12.data(), &fp12.fp6[1].fp2[2].fp[1]);
196
32
        Reverse<>(bn12);
197
198
32
        return {
199
32
            util::BinToDec(bn1.data(), 48),
200
32
            util::BinToDec(bn2.data(), 48),
201
32
            util::BinToDec(bn3.data(), 48),
202
32
            util::BinToDec(bn4.data(), 48),
203
32
            util::BinToDec(bn5.data(), 48),
204
32
            util::BinToDec(bn6.data(), 48),
205
32
            util::BinToDec(bn7.data(), 48),
206
32
            util::BinToDec(bn8.data(), 48),
207
32
            util::BinToDec(bn9.data(), 48),
208
32
            util::BinToDec(bn10.data(), 48),
209
32
            util::BinToDec(bn11.data(), 48),
210
32
            util::BinToDec(bn12.data(), 48),
211
32
        };
212
32
    }
213
214
233
    bool To_blst_scalar(const component::Bignum& bn, blst_scalar& out) {
215
233
        const auto ret = ToArray<32>(bn);
216
217
233
        if ( ret == std::nullopt ) {
218
32
            return false;
219
32
        }
220
221
201
        CF_NORET(blst_scalar_from_uint64(&out, (const uint64_t*)ret->data()));
222
223
201
        return true;
224
233
    }
225
0
    bool IsLessThan(const blst_fp& A, const blst_fp& B) {
226
0
        std::array<uint8_t, 48> A_words, B_words;
227
228
0
        uint64_t* A_ptr = (uint64_t*)A_words.data();
229
0
        uint64_t* B_ptr = (uint64_t*)B_words.data();
230
231
0
        blst_uint64_from_fp(A_ptr, &A);
232
0
        blst_uint64_from_fp(B_ptr, &B);
233
234
0
        for (int i = 5; i >= 0; i--) {
235
0
            if ( A_ptr[i] == B_ptr[i] ) {
236
0
                continue;
237
0
            }
238
0
            return A_ptr[i] < B_ptr[i];
239
0
        }
240
241
0
        return false;
242
0
    }
243
175
    static size_t isMultipleOf2(const std::string s) {
244
175
        auto i = boost::multiprecision::cpp_int(s);
245
175
        if ( i == 0 || i == 1 ) {
246
3
            return 0;
247
3
        }
248
172
        size_t num = 0;
249
18.4k
        while ( i ) {
250
18.3k
            if ( i != 1 && i & 1 ) {
251
144
                return 0;
252
144
            }
253
18.2k
            i >>= 1;
254
18.2k
            num++;
255
18.2k
        }
256
257
28
        return num - 1;
258
172
    }
259
41
    static bool IsZero(const blst_p2_affine* g2) {
260
41
        blst_p2_affine zero;
261
41
        memset(&zero, 0, sizeof(zero));
262
41
        return memcmp(g2, &zero, sizeof(zero)) == 0;
263
41
    }
264
265
431
    static std::optional<blst_p1_affine> Load_G1_Affine(const component::G1& g1) {
266
431
        std::optional<blst_p1_affine> ret = std::nullopt;
267
268
431
        blst_p1_affine aff_a;
269
270
431
        CF_CHECK_TRUE(blst_detail::To_blst_fp(g1.first, aff_a.x));
271
394
        CF_CHECK_TRUE(blst_detail::To_blst_fp(g1.second, aff_a.y));
272
273
371
        ret = aff_a;
274
431
end:
275
431
        return ret;
276
371
    }
277
278
    static std::optional<blst_p1> Load_G1_Projective(
279
            fuzzing::datasource::Datasource& ds,
280
1.20k
            const component::G1& g1) {
281
1.20k
        std::optional<blst_p1> ret = std::nullopt;
282
283
1.20k
        blst_p1 a;
284
285
1.20k
        const auto proj = util::ToRandomProjective(
286
1.20k
                ds,
287
1.20k
                g1.first.ToTrimmedString(),
288
1.20k
                g1.second.ToTrimmedString(),
289
1.20k
                CF_ECC_CURVE("BLS12_381"));
290
291
1.20k
        CF_CHECK_TRUE(blst_detail::To_blst_fp(proj[0], a.x));
292
1.18k
        CF_CHECK_TRUE(blst_detail::To_blst_fp(proj[1], a.y));
293
1.17k
        CF_CHECK_TRUE(blst_detail::To_blst_fp(proj[2], a.z));
294
295
1.17k
        ret = a;
296
1.20k
end:
297
1.20k
        return ret;
298
1.17k
    }
299
}
300
301
63
std::optional<component::BLS_PublicKey> blst::OpBLS_PrivateToPublic(operation::BLS_PrivateToPublic& op) {
302
63
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
303
        //return std::nullopt;
304
16
    }
305
306
63
    std::optional<component::BLS_PublicKey> ret = std::nullopt;
307
63
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
308
309
63
    blst_scalar priv;
310
63
    blst_p1 pub;
311
63
    blst_p1_affine pub_affine;
312
313
63
    CF_CHECK_TRUE(blst_detail::To_blst_scalar(op.priv, priv));
314
315
    /* blst_sk_to_pk_in_g1 does not reduce the private key,
316
     * so check if it's valid first
317
     */
318
46
    CF_CHECK_TRUE(blst_sk_check(&priv));
319
320
34
    CF_NORET(blst_sk_to_pk_in_g1(&pub, &priv));
321
34
    CF_ASSERT(blst_p1_on_curve(&pub) == true, "Generated pubkey not on curve");
322
34
    CF_ASSERT(blst_p1_in_g1(&pub) == true, "Generated pubkey not in group");
323
324
34
    CF_NORET(blst_p1_to_affine(&pub_affine, &pub));
325
326
34
    {
327
34
        blst_detail::G1 g1(pub_affine, ds);
328
34
        ret = g1.To_Component_G1();
329
34
    }
330
331
63
end:
332
63
    return ret;
333
34
}
334
335
170
std::optional<component::G2> blst::OpBLS_PrivateToPublic_G2(operation::BLS_PrivateToPublic_G2& op) {
336
170
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
337
        //return std::nullopt;
338
151
    }
339
340
170
    std::optional<component::G2> ret = std::nullopt;
341
170
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
342
343
170
    blst_scalar priv;
344
170
    blst_p2 pub;
345
170
    blst_p2_affine pub_affine;
346
347
170
    CF_CHECK_TRUE(blst_detail::To_blst_scalar(op.priv, priv));
348
349
    /* blst_sk_to_pk_in_g2 does not reduce the private key,
350
     * so check if it's valid first
351
     */
352
155
    CF_CHECK_TRUE(blst_sk_check(&priv));
353
354
108
    CF_NORET(blst_sk_to_pk_in_g2(&pub, &priv));
355
108
    CF_ASSERT(blst_p2_on_curve(&pub) == true, "Generated pubkey not on curve");
356
108
    CF_ASSERT(blst_p2_in_g2(&pub) == true, "Generated pubkey not in group");
357
358
108
    CF_NORET(blst_p2_to_affine(&pub_affine, &pub));
359
360
108
    ret = blst_detail::To_G2(pub_affine);
361
362
170
end:
363
170
    return ret;
364
108
}
365
366
34
std::optional<component::G1> blst::OpBLS_HashToG1(operation::BLS_HashToG1& op) {
367
34
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
368
        //return std::nullopt;
369
9
    }
370
371
34
    std::optional<component::G1> ret = std::nullopt;
372
34
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
373
374
34
    blst_p1 g1;
375
34
    blst_p1_affine g1_affine;
376
377
34
    CF_NORET(blst_hash_to_g1(
378
34
            &g1,
379
34
            op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
380
34
            op.dest.GetPtr(&ds), op.dest.GetSize(),
381
34
            op.aug.GetPtr(&ds), op.aug.GetSize()));
382
383
34
    CF_ASSERT(blst_p1_on_curve(&g1) == true, "Generated g1 not on curve");
384
34
    CF_ASSERT(blst_p1_in_g1(&g1) == true, "Generated g1 not in group");
385
386
34
    CF_NORET(blst_p1_to_affine(&g1_affine, &g1));
387
388
34
    {
389
34
        blst_detail::G1 _g1(g1_affine, ds);
390
34
        ret = _g1.To_Component_G1();
391
34
    }
392
393
34
    return ret;
394
34
}
395
396
48
std::optional<component::G2> blst::OpBLS_HashToG2(operation::BLS_HashToG2& op) {
397
48
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
398
        //return std::nullopt;
399
13
    }
400
401
48
    std::optional<component::G2> ret = std::nullopt;
402
48
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
403
404
48
    blst_p2 g2;
405
48
    blst_p2_affine g2_affine;
406
407
48
    CF_NORET(blst_hash_to_g2(
408
48
            &g2,
409
48
            op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
410
48
            op.dest.GetPtr(&ds), op.dest.GetSize(),
411
48
            op.aug.GetPtr(&ds), op.aug.GetSize()));
412
413
48
    CF_ASSERT(blst_p2_on_curve(&g2) == true, "Generated g2 not on curve");
414
48
    CF_ASSERT(blst_p2_in_g2(&g2) == true, "Generated g2 not in group");
415
416
48
    CF_NORET(blst_p2_to_affine(&g2_affine, &g2));
417
418
48
    ret = blst_detail::To_G2(g2_affine);
419
420
48
    return ret;
421
48
}
422
423
0
std::optional<component::G1> blst::OpBLS_MapToG1(operation::BLS_MapToG1& op) {
424
0
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
425
        //return std::nullopt;
426
0
    }
427
428
0
    std::optional<component::G1> ret = std::nullopt;
429
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
430
431
0
    blst_p1 g1;
432
0
    blst_p1_affine g1_affine;
433
0
    blst_fp u, v;
434
435
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.u, u));
436
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.v, v));
437
438
0
    CF_NORET(blst_map_to_g1(&g1, &u, &v));
439
440
0
    CF_ASSERT(blst_p1_on_curve(&g1) == true, "Generated g1 not on curve");
441
0
    CF_ASSERT(blst_p1_in_g1(&g1) == true, "Generated g1 not in group");
442
443
0
    CF_NORET(blst_p1_to_affine(&g1_affine, &g1));
444
445
0
    {
446
0
        blst_detail::G1 _g1(g1_affine, ds);
447
0
        ret = _g1.To_Component_G1();
448
0
    }
449
450
0
end:
451
0
    return ret;
452
0
}
453
454
0
std::optional<component::G2> blst::OpBLS_MapToG2(operation::BLS_MapToG2& op) {
455
0
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
456
        //return std::nullopt;
457
0
    }
458
459
0
    std::optional<component::G2> ret = std::nullopt;
460
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
461
462
0
    blst_p2 g2;
463
0
    blst_p2_affine g2_affine;
464
0
    blst_fp2 u, v;
465
466
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.u, u));
467
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.v, v));
468
469
0
    CF_NORET(blst_map_to_g2(&g2, &u, &v));
470
471
0
    CF_ASSERT(blst_p2_on_curve(&g2) == true, "Generated g2 not on curve");
472
0
    CF_ASSERT(blst_p2_in_g2(&g2) == true, "Generated g2 not in group");
473
474
0
    CF_NORET(blst_p2_to_affine(&g2_affine, &g2));
475
476
0
    ret = blst_detail::To_G2(g2_affine);
477
478
0
end:
479
0
    return ret;
480
0
}
481
482
0
std::optional<component::BLS_Signature> blst::OpBLS_Sign(operation::BLS_Sign& op) {
483
0
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
484
        //return std::nullopt;
485
0
    }
486
487
0
    std::optional<component::BLS_Signature> ret;
488
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
489
490
0
    blst_scalar priv;
491
0
    blst_p1 pub;
492
0
    blst_p1_affine pub_affine;
493
0
    blst_p2_affine hash_affine;
494
0
    blst_p2 hash;
495
0
    blst_p2 signature;
496
0
    blst_p2_affine signature_affine;
497
498
0
    CF_CHECK_TRUE(blst_detail::To_blst_scalar(op.priv, priv));
499
0
    CF_CHECK_TRUE(blst_sk_check(&priv));
500
501
0
    CF_NORET(blst_sk_to_pk_in_g1(&pub, &priv));
502
0
    CF_ASSERT(blst_p1_on_curve(&pub) == true, "Generated pubkey not on curve");
503
0
    CF_ASSERT(blst_p1_in_g1(&pub) == true, "Generated pubkey not in group");
504
0
    CF_NORET(blst_p1_to_affine(&pub_affine, &pub));
505
506
0
    if ( op.hashOrPoint == true ) {
507
0
        CF_NORET(blst_hash_to_g2(
508
0
                &hash,
509
0
                op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
510
0
                op.dest.GetPtr(&ds), op.dest.GetSize(),
511
0
                op.aug.GetPtr(&ds), op.aug.GetSize()));
512
0
    } else {
513
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(op.point.first.first, hash_affine.x.fp[0]));
514
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(op.point.first.second, hash_affine.y.fp[0]));
515
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(op.point.second.first, hash_affine.x.fp[1]));
516
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(op.point.second.second, hash_affine.y.fp[1]));
517
0
        CF_NORET(blst_p2_from_affine(&hash, &hash_affine));
518
0
    }
519
0
    CF_NORET(blst_sign_pk_in_g1(&signature, &hash, &priv));
520
521
0
    if ( op.hashOrPoint == true ) {
522
0
        CF_ASSERT(blst_p2_on_curve(&signature) == true, "Generated signature not on curve");
523
0
        CF_ASSERT(blst_p2_in_g2(&signature) == true, "Generated signature not in group");
524
0
    } else {
525
0
        if ( blst_p2_affine_on_curve(&hash_affine) ) {
526
0
            CF_ASSERT(blst_p2_on_curve(&signature) == true, "Generated signature not on curve");
527
0
        }
528
529
0
        if ( blst_p2_affine_in_g2(&hash_affine) ) {
530
0
            CF_ASSERT(blst_p2_in_g2(&signature) == true, "Generated signature not in group");
531
0
        }
532
0
    }
533
534
0
    CF_NORET(blst_p2_to_affine(&signature_affine, &signature));
535
536
0
    {
537
0
        blst_detail::G1 g1(pub_affine, ds);
538
0
        ret = {blst_detail::To_G2(signature_affine), g1.To_Component_G1()};
539
0
    }
540
541
0
    if ( op.hashOrPoint == true ) {
542
0
        if ( blst_core_verify_pk_in_g1(
543
0
                    &pub_affine, &signature_affine,
544
0
                    true,
545
0
                    op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
546
0
                    op.dest.GetPtr(&ds), op.dest.GetSize(),
547
0
                    op.aug.GetPtr(&ds), op.aug.GetSize()) != BLST_SUCCESS ) {
548
0
            abort();
549
0
        }
550
0
    }
551
552
0
end:
553
0
    return ret;
554
0
}
555
556
namespace blst_detail {
557
    std::optional<bool> Verify(
558
            Datasource& ds,
559
            const component::Cleartext& msg,
560
            const component::Cleartext& dst,
561
            const component::Cleartext& aug,
562
            const component::G1& pub,
563
            const component::G2& sig,
564
0
            const bool hashOrEncode) {
565
0
    std::optional<bool> ret = std::nullopt;
566
567
0
    blst_p1_affine pub_affine;
568
0
    blst_p2_affine sig_affine;
569
570
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(pub.first, pub_affine.x));
571
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(pub.second, pub_affine.y));
572
573
0
    if ( !(blst_p1_affine_on_curve(&pub_affine) && blst_p1_affine_in_g1(&pub_affine)) ) {
574
0
        return false;
575
0
    }
576
577
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.first.first, sig_affine.x.fp[0]));
578
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.first.second, sig_affine.y.fp[0]));
579
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.second.first, sig_affine.x.fp[1]));
580
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.second.second, sig_affine.y.fp[1]));
581
582
0
    if ( !(blst_p2_affine_on_curve(&sig_affine) && blst_p2_affine_in_g2(&sig_affine)) ) {
583
0
        return false;
584
0
    }
585
586
0
    ret = blst_core_verify_pk_in_g1(
587
0
                &pub_affine, &sig_affine,
588
0
                hashOrEncode,
589
0
                msg.GetPtr(&ds), msg.GetSize(),
590
0
                dst.GetPtr(&ds), dst.GetSize(),
591
0
                aug.GetPtr(&ds), aug.GetSize()) == BLST_SUCCESS;
592
593
0
end:
594
0
    return ret;
595
0
}
596
}
597
598
0
std::optional<bool> blst::OpBLS_Verify(operation::BLS_Verify& op) {
599
0
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
600
        //return std::nullopt;
601
0
    }
602
0
    if ( op.hashOrPoint == false ) {
603
0
        return std::nullopt;
604
0
    }
605
606
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
607
608
0
    std::optional<bool> ret = std::nullopt;
609
610
0
    blst_p1_affine pub;
611
0
    blst_p2_affine sig;
612
613
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.pub.first, pub.x));
614
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.pub.second, pub.y));
615
616
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.signature.first.first, sig.x.fp[0]));
617
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.signature.first.second, sig.y.fp[0]));
618
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.signature.second.first, sig.x.fp[1]));
619
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.signature.second.second, sig.y.fp[1]));
620
621
0
    ret = blst_core_verify_pk_in_g1(
622
0
                &pub, &sig,
623
0
                true,
624
0
                op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
625
0
                op.dest.GetPtr(&ds), op.dest.GetSize(),
626
0
                nullptr, 0) == BLST_SUCCESS;
627
628
0
end:
629
0
    return ret;
630
0
}
631
632
0
std::optional<bool> blst::OpBLS_BatchVerify(operation::BLS_BatchVerify& op) {
633
0
    std::optional<bool> ret = std::nullopt;
634
635
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
636
637
0
    const blst_fp12 one = *blst_fp12_one();
638
0
    blst_fp12 f = *blst_fp12_one();
639
640
0
    for (const auto& cur : op.bf.c) {
641
0
            blst_p1_affine g1_affine;
642
0
            blst_p2_affine g2_affine;
643
644
            /* Load G1 */
645
0
            CF_CHECK_TRUE(blst_detail::To_blst_fp(cur.g1.first, g1_affine.x));
646
0
            CF_CHECK_TRUE(blst_detail::To_blst_fp(cur.g1.second, g1_affine.y));
647
648
0
            CF_CHECK_TRUE(blst_p1_affine_on_curve(&g1_affine) && blst_p1_affine_in_g1(&g1_affine));
649
650
            /* Load G2 */
651
0
            CF_CHECK_TRUE(blst_detail::To_blst_fp(cur.g2.first.first, g2_affine.x.fp[0]));
652
0
            CF_CHECK_TRUE(blst_detail::To_blst_fp(cur.g2.first.second, g2_affine.y.fp[0]));
653
0
            CF_CHECK_TRUE(blst_detail::To_blst_fp(cur.g2.second.first, g2_affine.x.fp[1]));
654
0
            CF_CHECK_TRUE(blst_detail::To_blst_fp(cur.g2.second.second, g2_affine.y.fp[1]));
655
656
0
            CF_CHECK_TRUE(blst_p2_affine_on_curve(&g2_affine) && blst_p2_affine_in_g2(&g2_affine));
657
658
0
            blst_fp12 tmp;
659
0
            CF_NORET(blst_miller_loop(&tmp, &g2_affine, &g1_affine));
660
0
            CF_NORET(blst_fp12_mul(&f, &f, &tmp));
661
0
    }
662
663
0
    CF_NORET(blst_final_exp(&f, &f));
664
665
0
    ret = blst_fp12_is_equal(&f, &one) == 1;
666
667
0
end:
668
0
    return ret;
669
0
}
670
671
55
std::optional<bool> blst::OpBLS_IsG1OnCurve(operation::BLS_IsG1OnCurve& op) {
672
55
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
673
674
55
    bool useAffine = true;
675
676
55
    try {
677
55
        useAffine = ds.Get<bool>();
678
55
    } catch ( fuzzing::datasource::Base::OutOfData ) { }
679
680
55
    if ( useAffine ) {
681
35
        std::optional<blst_p1_affine> aff_g1;
682
35
        CF_CHECK_NE(aff_g1 = blst_detail::Load_G1_Affine(op.g1), std::nullopt);
683
26
        return
684
26
            !blst_p1_affine_is_inf(&*aff_g1) &&
685
13
            blst_p1_affine_on_curve(&*aff_g1) &&
686
4
            blst_p1_affine_in_g1(&*aff_g1);
687
35
    } else {
688
20
        std::optional<blst_p1> g1;
689
20
        CF_CHECK_NE(g1 = blst_detail::Load_G1_Projective(ds, op.g1), std::nullopt);
690
15
        return
691
15
            !blst_p1_is_inf(&*g1) &&
692
15
            blst_p1_on_curve(&*g1) &&
693
3
            blst_p1_in_g1(&*g1);
694
20
    }
695
14
end:
696
14
    return false;
697
55
}
698
699
66
std::optional<bool> blst::OpBLS_IsG2OnCurve(operation::BLS_IsG2OnCurve& op) {
700
66
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
701
702
66
    bool useAffine = true;
703
704
66
    try {
705
66
        useAffine = ds.Get<bool>();
706
66
    } catch ( fuzzing::datasource::Base::OutOfData ) { }
707
708
66
    blst_p2 g2;
709
66
    blst_p2_affine g2_affine;
710
711
66
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.first, g2_affine.x.fp[0]));
712
53
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.second, g2_affine.y.fp[0]));
713
52
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.first, g2_affine.x.fp[1]));
714
43
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.second, g2_affine.y.fp[1]));
715
716
41
    CF_CHECK_FALSE(blst_detail::IsZero(&g2_affine));
717
718
26
    if ( useAffine ) {
719
19
        return blst_p2_affine_on_curve(&g2_affine) && blst_p2_affine_in_g2(&g2_affine);
720
19
    } else {
721
7
        CF_NORET(blst_p2_from_affine(&g2, &g2_affine));
722
7
        return blst_p2_on_curve(&g2) && blst_p2_in_g2(&g2);
723
7
    }
724
725
40
end:
726
40
    return false;
727
26
}
728
729
0
std::optional<component::BLS_KeyPair> blst::OpBLS_GenerateKeyPair(operation::BLS_GenerateKeyPair& op) {
730
0
    std::optional<component::BLS_KeyPair> ret = std::nullopt;
731
0
    if ( op.ikm.GetSize() < 32 ) {
732
0
        return std::nullopt;
733
0
    }
734
735
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
736
737
0
    blst_scalar priv;
738
0
    blst_p1 pub;
739
0
    blst_p1_affine pub_affine;
740
741
0
    CF_NORET(blst_keygen(&priv, op.ikm.GetPtr(&ds), op.ikm.GetSize(), op.info.GetPtr(&ds), op.info.GetSize()));
742
0
    CF_ASSERT(blst_sk_check(&priv) == true, "blst_keygen generated invalid private key");
743
744
0
    CF_NORET(blst_sk_to_pk_in_g1(&pub, &priv));
745
0
    CF_NORET(blst_p1_to_affine(&pub_affine, &pub));
746
747
0
    {
748
0
        std::array<uint8_t, 32> priv_bytes;
749
0
        CF_NORET(blst_uint64_from_scalar((uint64_t*)priv_bytes.data(), &priv));
750
751
0
        blst_detail::Reverse<>(priv_bytes);
752
0
        blst_detail::G1 g1(pub_affine, ds);
753
754
0
        {
755
0
            const auto priv = util::BinToDec(priv_bytes.data(), 32);
756
0
            const auto pub = g1.To_Component_G1();
757
758
0
            CF_ASSERT(blst_p1_affine_on_curve(&pub_affine) == true, "Generated public key not on curve");
759
0
            CF_ASSERT(blst_p1_affine_in_g1(&pub_affine), "Generated public key not in group");
760
761
0
            ret = {priv, pub};
762
0
        }
763
0
    }
764
765
0
    return ret;
766
0
}
767
768
0
std::optional<component::G1> blst::OpBLS_Aggregate_G1(operation::BLS_Aggregate_G1& op) {
769
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
770
0
    std::optional<component::G1> ret = std::nullopt;
771
772
0
    blst_p1 res;
773
0
    blst_p1_affine res_affine;
774
775
0
    bool first = true;
776
0
    for (const auto& sig : op.points.points) {
777
0
        uint8_t serialized[96];
778
0
        blst_p1_affine a;
779
0
        blst_p1 a_;
780
781
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.first, a.x));
782
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.second, a.y));
783
784
0
        CF_NORET(blst_p1_from_affine(&a_, &a));
785
786
0
        CF_NORET(blst_p1_serialize(serialized, &a_));
787
788
0
        CF_CHECK_EQ(
789
0
                blst_aggregate_in_g1(
790
0
                    &res,
791
0
                    first == true ? nullptr : &res,
792
0
                    serialized), BLST_SUCCESS);
793
794
0
        first = false;
795
0
    }
796
797
0
    if ( first == false ) {
798
0
        CF_NORET(blst_p1_to_affine(&res_affine, &res));
799
0
        CF_ASSERT(blst_p1_affine_on_curve(&res_affine) && blst_p1_affine_in_g1(&res_affine), "Aggregate created invalid point");
800
0
        blst_detail::G1 _g1(res_affine, ds);
801
0
        ret = _g1.To_Component_G1();
802
0
    }
803
804
0
end:
805
0
    return ret;
806
0
}
807
808
0
std::optional<component::G2> blst::OpBLS_Aggregate_G2(operation::BLS_Aggregate_G2& op) {
809
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
810
0
    std::optional<component::G2> ret = std::nullopt;
811
812
0
    blst_p2 res;
813
0
    blst_p2_affine res_affine;
814
815
0
    bool first = true;
816
0
    for (const auto& sig : op.points.points) {
817
0
        uint8_t serialized[192];
818
0
        blst_p2_affine a;
819
0
        blst_p2 a_;
820
821
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.first.first, a.x.fp[0]));
822
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.first.second, a.y.fp[0]));
823
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.second.first, a.x.fp[1]));
824
0
        CF_CHECK_TRUE(blst_detail::To_blst_fp(sig.second.second, a.y.fp[1]));
825
826
0
        CF_NORET(blst_p2_from_affine(&a_, &a));
827
828
0
        CF_NORET(blst_p2_serialize(serialized, &a_));
829
830
0
        CF_CHECK_EQ(
831
0
                blst_aggregate_in_g2(
832
0
                    &res,
833
0
                    first == true ? nullptr : &res,
834
0
                    serialized), BLST_SUCCESS);
835
836
0
        first = false;
837
0
    }
838
839
0
    if ( first == false ) {
840
0
        CF_NORET(blst_p2_to_affine(&res_affine, &res));
841
0
        CF_ASSERT(blst_p2_affine_on_curve(&res_affine) && blst_p2_affine_in_g2(&res_affine), "Aggregate created invalid point");
842
0
        ret = blst_detail::To_G2(res_affine);
843
0
    }
844
845
0
end:
846
0
    return ret;
847
0
}
848
849
41
std::optional<component::Fp12> blst::OpBLS_Pairing(operation::BLS_Pairing& op) {
850
41
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
851
41
    std::optional<component::Fp12> ret = std::nullopt;
852
853
41
    blst_p1_affine g1_affine;
854
41
    blst_p2_affine g2_affine;
855
41
    blst_fp12 out;
856
857
41
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g1.first, g1_affine.x));
858
37
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g1.second, g1_affine.y));
859
860
36
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.first, g2_affine.x.fp[0]));
861
35
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.second, g2_affine.y.fp[0]));
862
34
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.first, g2_affine.x.fp[1]));
863
33
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.second, g2_affine.y.fp[1]));
864
865
32
    CF_NORET(blst_miller_loop(&out, &g2_affine, &g1_affine));
866
32
    CF_NORET(blst_final_exp(&out, &out));
867
868
32
    ret = blst_detail::To_component_Fp12(out);
869
870
41
end:
871
41
    return ret;
872
32
}
873
874
0
std::optional<component::Fp12> blst::OpBLS_MillerLoop(operation::BLS_MillerLoop& op) {
875
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
876
0
    std::optional<component::Fp12> ret = std::nullopt;
877
878
0
    blst_p1_affine g1_affine;
879
0
    blst_p2_affine g2_affine;
880
0
    blst_fp12 out;
881
882
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g1.first, g1_affine.x));
883
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g1.second, g1_affine.y));
884
885
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.first, g2_affine.x.fp[0]));
886
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.second, g2_affine.y.fp[0]));
887
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.first, g2_affine.x.fp[1]));
888
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.second, g2_affine.y.fp[1]));
889
890
0
    CF_NORET(blst_miller_loop(&out, &g2_affine, &g1_affine));
891
892
0
    ret = blst_detail::To_component_Fp12(out);
893
894
0
end:
895
0
    return ret;
896
0
}
897
898
0
std::optional<component::Fp12> blst::OpBLS_FinalExp(operation::BLS_FinalExp& op) {
899
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
900
0
    std::optional<component::Fp12> ret = std::nullopt;
901
902
0
    blst_fp12 out;
903
904
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.fp12, out));
905
906
0
    CF_NORET(blst_final_exp(&out, &out));
907
908
0
    ret = blst_detail::To_component_Fp12(out);
909
910
0
end:
911
0
    return ret;
912
0
}
913
914
namespace blst_detail {
915
    template <class T>
916
331
    bool UseParamTwice(fuzzing::datasource::Datasource& ds, const T* A, const T* B) {
917
331
        if ( memcmp(A, B, sizeof(T)) != 0 ) {
918
308
            return false;
919
308
        }
920
921
23
        try {
922
23
            return ds.Get<bool>();
923
23
        } catch ( fuzzing::datasource::Base::OutOfData ) {
924
15
        }
925
926
15
        return false;
927
23
    }
bool cryptofuzz::module::blst_detail::UseParamTwice<blst_fr>(fuzzing::datasource::Datasource&, blst_fr const*, blst_fr const*)
Line
Count
Source
916
146
    bool UseParamTwice(fuzzing::datasource::Datasource& ds, const T* A, const T* B) {
917
146
        if ( memcmp(A, B, sizeof(T)) != 0 ) {
918
135
            return false;
919
135
        }
920
921
11
        try {
922
11
            return ds.Get<bool>();
923
11
        } catch ( fuzzing::datasource::Base::OutOfData ) {
924
8
        }
925
926
8
        return false;
927
11
    }
bool cryptofuzz::module::blst_detail::UseParamTwice<blst_fp>(fuzzing::datasource::Datasource&, blst_fp const*, blst_fp const*)
Line
Count
Source
916
185
    bool UseParamTwice(fuzzing::datasource::Datasource& ds, const T* A, const T* B) {
917
185
        if ( memcmp(A, B, sizeof(T)) != 0 ) {
918
173
            return false;
919
173
        }
920
921
12
        try {
922
12
            return ds.Get<bool>();
923
12
        } catch ( fuzzing::datasource::Base::OutOfData ) {
924
7
        }
925
926
7
        return false;
927
12
    }
Unexecuted instantiation: bool cryptofuzz::module::blst_detail::UseParamTwice<blst_fp2>(fuzzing::datasource::Datasource&, blst_fp2 const*, blst_fp2 const*)
Unexecuted instantiation: bool cryptofuzz::module::blst_detail::UseParamTwice<blst_fp12>(fuzzing::datasource::Datasource&, blst_fp12 const*, blst_fp12 const*)
928
929
576
    uint8_t GetMod3(fuzzing::datasource::Datasource& ds) {
930
576
        try {
931
576
            return ds.Get<uint8_t>() % 3;
932
576
        } catch ( fuzzing::datasource::Base::OutOfData ) {
933
489
        }
934
935
489
        return 0;
936
576
    }
937
938
576
    #define PREPARE_RESULT() {resultIdx = GetMod3(ds);}
939
320
    #define RESULT_PTR() (resultIdx == 0 ? &result : (resultIdx == 1 ? &A : &B))
940
514
    #define RESULT() (resultIdx == 0 ? result : (resultIdx == 1 ? A : B))
941
114
    #define PARAM_B() (UseParamTwice(ds, &A, &B) ? &A : &B)
942
943
2.66k
    std::optional<component::Bignum> OpBignumCalc_order(operation::BignumCalc& op) {
944
2.66k
        std::optional<component::Bignum> ret = std::nullopt;
945
2.66k
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
946
2.66k
        blst_fr result, A, B;
947
2.66k
        uint8_t resultIdx;
948
2.66k
        static const blst_fr zero = {0};
949
950
2.66k
        switch ( op.calcOp.Get() ) {
951
21
            case    CF_CALCOP("Add(A,B)"):
952
21
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
953
16
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
954
14
                PREPARE_RESULT();
955
14
                blst_fr_add(RESULT_PTR(), &A, PARAM_B());
956
14
                ret = blst_detail::To_component_bignum(RESULT());
957
14
                break;
958
157
            case    CF_CALCOP("Sub(A,B)"):
959
157
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
960
145
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
961
83
                PREPARE_RESULT();
962
83
                blst_fr_sub(RESULT_PTR(), &A, PARAM_B());
963
83
                ret = blst_detail::To_component_bignum(RESULT());
964
83
                break;
965
152
            case    CF_CALCOP("Mul(A,B)"):
966
152
                {
967
152
                    CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
968
969
100
                    size_t shiftCount;
970
100
                    uint8_t which = 0;
971
100
                    try {
972
100
                        which = ds.Get<uint8_t>() % 3;
973
100
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
974
38
                    }
975
100
                    if ( which == 1 ) {
976
15
                        if ( op.bn1.ToTrimmedString() != "3" ) {
977
11
                            which = 0;
978
11
                        }
979
85
                    } else if ( which == 2 ) {
980
43
                        shiftCount = blst_detail::isMultipleOf2(op.bn1.ToTrimmedString());
981
43
                        if ( shiftCount == 0 ) {
982
35
                            which = 0;
983
35
                        }
984
43
                    }
985
986
100
                    switch ( which ) {
987
88
                        case    0:
988
88
                            CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
989
49
                            PREPARE_RESULT();
990
49
                            CF_NORET(blst_fr_mul(RESULT_PTR(), &A, PARAM_B()));
991
49
                            break;
992
4
                        case    1:
993
4
                            PREPARE_RESULT();
994
4
                            CF_NORET(blst_fr_mul_by_3(RESULT_PTR(), &A));
995
4
                            break;
996
8
                        case    2:
997
8
                            PREPARE_RESULT();
998
8
                            blst_fr_lshift(RESULT_PTR(), &A, shiftCount);
999
8
                            ret = blst_detail::To_component_bignum(RESULT());
1000
8
                            break;
1001
100
                    }
1002
1003
61
                    ret = blst_detail::To_component_bignum(RESULT());
1004
61
                }
1005
0
                break;
1006
49
            case    CF_CALCOP("Sqr(A)"):
1007
49
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1008
16
                PREPARE_RESULT();
1009
16
                blst_fr_sqr(RESULT_PTR(), &A);
1010
16
                ret = blst_detail::To_component_bignum(RESULT());
1011
16
                break;
1012
22
            case    CF_CALCOP("InvMod(A,B)"):
1013
22
                {
1014
22
                    CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1015
1016
12
                    bool which = false;
1017
12
                    try {
1018
12
                        which = ds.Get<bool>();
1019
12
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1020
5
                    }
1021
1022
12
                    if ( which ) {
1023
4
                        PREPARE_RESULT();
1024
4
                        CF_NORET(blst_fr_eucl_inverse(RESULT_PTR(), &A));
1025
8
                    } else {
1026
8
                        PREPARE_RESULT();
1027
8
                        CF_NORET(blst_fr_inverse(RESULT_PTR(), &A));
1028
8
                    }
1029
1030
12
                    ret = blst_detail::To_component_bignum(RESULT());
1031
12
                }
1032
0
                break;
1033
10
            case    CF_CALCOP("LShift1(A)"):
1034
10
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1035
7
                PREPARE_RESULT();
1036
7
                blst_fr_lshift(RESULT_PTR(), &A, 1);
1037
7
                ret = blst_detail::To_component_bignum(RESULT());
1038
7
                break;
1039
197
            case    CF_CALCOP("RShift(A,B)"):
1040
197
                {
1041
197
                    CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1042
185
                    const auto B_cpp_int = blst_detail::To_cpp_int(op.bn1);
1043
185
                    size_t count = static_cast<size_t>(B_cpp_int);
1044
185
                    CF_CHECK_EQ(count, B_cpp_int);
1045
183
                    CF_CHECK_GT(count, 0);
1046
173
                    CF_CHECK_LT(count, 1024000);
1047
1048
92
                    PREPARE_RESULT();
1049
92
                    blst_fr_rshift(RESULT_PTR(), &A, count);
1050
1051
92
                    {
1052
92
                        CF_CHECK_EQ(count, 1);
1053
75
                        const auto A_cpp_int = blst_detail::To_cpp_int(op.bn0);
1054
75
                        CF_CHECK_LT(A_cpp_int, boost::multiprecision::cpp_int("52435875175126190479447740508185965837690552500527637822603658699938581184513"));
1055
19
                        CF_CHECK_EQ(A_cpp_int % 2, 1);
1056
12
                        ret = blst_detail::To_component_bignum(RESULT());
1057
12
                    }
1058
12
                }
1059
0
                break;
1060
18
            case    CF_CALCOP("Not(A)"):
1061
18
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1062
12
                PREPARE_RESULT();
1063
12
                blst_fr_cneg(RESULT_PTR(), &A, true);
1064
12
                ret = blst_detail::To_component_bignum(RESULT());
1065
12
                break;
1066
3
            case    CF_CALCOP("Set(A)"):
1067
3
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1068
1
                ret = blst_detail::To_component_bignum(A);
1069
1
                break;
1070
150
            case    CF_CALCOP("IsEq(A,B)"):
1071
150
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1072
143
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
1073
141
                ret = memcmp(&A, &B, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1074
141
                break;
1075
9
            case    CF_CALCOP("IsZero(A)"):
1076
9
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1077
7
                ret = memcmp(&A, &zero, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1078
7
                break;
1079
2.66k
        }
1080
1081
2.66k
end:
1082
2.66k
        return ret;
1083
2.66k
    }
1084
1085
2.37k
    std::optional<component::Bignum> OpBignumCalc_prime(operation::BignumCalc& op) {
1086
2.37k
        std::optional<component::Bignum> ret = std::nullopt;
1087
2.37k
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1088
2.37k
        blst_fp result, A, B;
1089
2.37k
        uint8_t resultIdx;
1090
2.37k
        static const blst_fp zero = {0};
1091
1092
2.37k
        switch ( op.calcOp.Get() ) {
1093
8
            case    CF_CALCOP("Add(A,B)"):
1094
8
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1095
4
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1096
3
                PREPARE_RESULT();
1097
3
                blst_fp_add(RESULT_PTR(), &A, PARAM_B());
1098
3
                ret = blst_detail::To_component_bignum(RESULT());
1099
3
                break;
1100
29
            case    CF_CALCOP("Sub(A,B)"):
1101
29
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1102
18
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1103
14
                PREPARE_RESULT();
1104
14
                blst_fp_sub(RESULT_PTR(), &A, PARAM_B());
1105
14
                ret = blst_detail::To_component_bignum(RESULT());
1106
14
                break;
1107
270
            case    CF_CALCOP("Mul(A,B)"):
1108
270
                {
1109
270
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1110
1111
223
                    size_t shiftCount;
1112
223
                    uint8_t which = 0;
1113
223
                    try {
1114
223
                        which = ds.Get<uint8_t>() % 4;
1115
223
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1116
70
                    }
1117
1118
223
                    if ( which == 1 ) {
1119
13
                        if ( op.bn1.ToTrimmedString() != "3" ) {
1120
10
                            which = 0;
1121
10
                        }
1122
210
                    } else if ( which == 2 ) {
1123
16
                        if ( op.bn1.ToTrimmedString() != "8" ) {
1124
12
                            which = 0;
1125
12
                        }
1126
194
                    } else if ( which == 3 ) {
1127
122
                        shiftCount = blst_detail::isMultipleOf2(op.bn1.ToTrimmedString());
1128
122
                        if ( shiftCount == 0 ) {
1129
112
                            which = 0;
1130
112
                        }
1131
122
                    }
1132
1133
223
                    switch ( which ) {
1134
206
                        case    0:
1135
206
                            CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1136
168
                            PREPARE_RESULT();
1137
168
                            CF_NORET(blst_fp_mul(RESULT_PTR(), &A, PARAM_B()));
1138
168
                            break;
1139
3
                        case    1:
1140
3
                            PREPARE_RESULT();
1141
3
                            CF_NORET(blst_fp_mul_by_3(RESULT_PTR(), &A));
1142
3
                            break;
1143
4
                        case    2:
1144
4
                            PREPARE_RESULT();
1145
4
                            CF_NORET(blst_fp_mul_by_8(RESULT_PTR(), &A));
1146
4
                            break;
1147
10
                        case    3:
1148
10
                            size_t shiftCount;
1149
10
                            CF_CHECK_NE(shiftCount = blst_detail::isMultipleOf2(op.bn1.ToTrimmedString()), 0);
1150
1151
10
                            PREPARE_RESULT();
1152
10
                            blst_fp_lshift(RESULT_PTR(), &A, shiftCount);
1153
10
                            ret = blst_detail::To_component_bignum(RESULT());
1154
10
                            break;
1155
223
                    }
1156
1157
185
                    ret = blst_detail::To_component_bignum(RESULT());
1158
185
                }
1159
0
                break;
1160
6
            case    CF_CALCOP("LShift1(A)"):
1161
6
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1162
4
                PREPARE_RESULT();
1163
4
                blst_fp_lshift(RESULT_PTR(), &A, 1);
1164
4
                ret = blst_detail::To_component_bignum(RESULT());
1165
4
                break;
1166
90
            case    CF_CALCOP("Sqr(A)"):
1167
90
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1168
46
                PREPARE_RESULT();
1169
46
                blst_fp_sqr(RESULT_PTR(), &A);
1170
46
                ret = blst_detail::To_component_bignum(RESULT());
1171
46
                break;
1172
22
            case    CF_CALCOP("InvMod(A,B)"):
1173
22
                {
1174
22
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1175
1176
16
                    bool which = false;
1177
16
                    try {
1178
16
                        which = ds.Get<bool>();
1179
16
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1180
10
                    }
1181
1182
16
                    if ( which ) {
1183
3
                        PREPARE_RESULT();
1184
3
                        CF_NORET(blst_fp_eucl_inverse(RESULT_PTR(), &A));
1185
13
                    } else {
1186
13
                        PREPARE_RESULT();
1187
13
                        CF_NORET(blst_fp_inverse(RESULT_PTR(), &A));
1188
13
                    }
1189
1190
16
                    ret = blst_detail::To_component_bignum(RESULT());
1191
16
                }
1192
0
                break;
1193
211
            case    CF_CALCOP("Sqrt(A)"):
1194
211
                {
1195
211
                    blst_fp result2;
1196
211
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1197
1198
210
                    if ( blst_fp_sqrt(&result, &A) == true ) {
1199
131
                        CF_NORET(blst_fp_sqr(&result, &result));
1200
131
                        ret = blst_detail::To_component_bignum(result);
1201
131
                    } else {
1202
79
                        CF_NORET(blst_fp_cneg(&A, &A, 1));
1203
79
                        CF_ASSERT(blst_fp_sqrt(&A, &A) == true, "Square root must exist");
1204
1205
79
                        ret = std::string("0");
1206
79
                    }
1207
210
                }
1208
210
                break;
1209
210
            case    CF_CALCOP("IsSquare(A)"):
1210
3
                {
1211
3
                    blst_fp tmp;
1212
1213
3
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1214
2
                    const bool is_square = blst_fp_is_square(&A);
1215
1216
2
                    if ( !is_square ) {
1217
1
                        CF_NORET(blst_fp_cneg(&tmp, &A, 1));
1218
1
                        CF_ASSERT(blst_fp_is_square(&tmp) == true, "Must be square");
1219
1
                    } else {
1220
1
                        CF_ASSERT(blst_fp_sqrt(&tmp, &A) == true, "Square root must exist");
1221
1
                    }
1222
1223
2
                    CF_ASSERT(
1224
2
                            is_square ==
1225
2
                            blst_fp_sqrt(&result, &A), "");
1226
2
                    ret = is_square ? std::string("1") : std::string("0");
1227
2
                }
1228
0
                break;
1229
13
            case    CF_CALCOP("Not(A)"):
1230
13
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1231
11
                PREPARE_RESULT();
1232
11
                blst_fp_cneg(RESULT_PTR(), &A, true);
1233
11
                ret = blst_detail::To_component_bignum(RESULT());
1234
11
                break;
1235
2
            case    CF_CALCOP("Set(A)"):
1236
2
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1237
1
                ret = blst_detail::To_component_bignum(A);
1238
1
                break;
1239
69
            case    CF_CALCOP("IsEq(A,B)"):
1240
69
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1241
63
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1242
59
                ret = memcmp(&A, &B, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1243
59
                break;
1244
7
            case    CF_CALCOP("IsZero(A)"):
1245
7
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1246
6
                ret = memcmp(&A, &zero, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1247
6
                break;
1248
2.37k
        }
1249
1250
2.37k
end:
1251
2.37k
        return ret;
1252
2.37k
    }
1253
1254
    #undef PREPARE_RESULT
1255
    #undef RESULT_PTR
1256
    #undef RESULT
1257
    #undef PARAM_B
1258
}
1259
1260
5.04k
std::optional<component::Bignum> blst::OpBignumCalc(operation::BignumCalc& op) {
1261
5.04k
    if ( op.modulo == std::nullopt ) {
1262
0
        return std::nullopt;
1263
0
    }
1264
1265
    /* TODO optimize this */
1266
5.04k
    if ( op.modulo->ToTrimmedString() == "52435875175126190479447740508185965837690552500527637822603658699938581184513" ) {
1267
2.66k
        return blst_detail::OpBignumCalc_order(op);
1268
2.66k
    } else if ( op.modulo->ToTrimmedString() == "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" ) {
1269
2.37k
        return blst_detail::OpBignumCalc_prime(op);
1270
2.37k
    } else {
1271
0
        return std::nullopt;
1272
0
    }
1273
5.04k
}
1274
1275
namespace blst_detail {
1276
0
    #define PREPARE_RESULT() {resultIdx = GetMod3(ds);}
1277
0
    #define RESULT_PTR() (resultIdx == 0 ? &result : (resultIdx == 1 ? &A : &B))
1278
0
    #define RESULT() (resultIdx == 0 ? result : (resultIdx == 1 ? A : B))
1279
0
    #define PARAM_B() (UseParamTwice(ds, &A, &B) ? &A : &B)
1280
1281
0
    std::optional<component::Fp2> OpBignumCalc_Fp2_prime(operation::BignumCalc_Fp2& op) {
1282
0
        std::optional<component::Fp2> ret = std::nullopt;
1283
0
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1284
0
        blst_fp2 result, A, B;
1285
0
        uint8_t resultIdx;
1286
1287
0
        switch ( op.calcOp.Get() ) {
1288
0
            case    CF_CALCOP("Add(A,B)"):
1289
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1290
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn1, B));
1291
0
                PREPARE_RESULT();
1292
0
                blst_fp2_add(RESULT_PTR(), &A, PARAM_B());
1293
0
                ret = blst_detail::To_component_Fp2(RESULT());
1294
0
                break;
1295
0
            case    CF_CALCOP("Sub(A,B)"):
1296
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1297
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn1, B));
1298
0
                PREPARE_RESULT();
1299
0
                blst_fp2_sub(RESULT_PTR(), &A, PARAM_B());
1300
0
                ret = blst_detail::To_component_Fp2(RESULT());
1301
0
                break;
1302
0
            case    CF_CALCOP("Mul(A,B)"):
1303
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1304
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn1, B));
1305
0
                PREPARE_RESULT();
1306
0
                CF_NORET(blst_fp2_mul(RESULT_PTR(), &A, PARAM_B()));
1307
1308
0
                ret = blst_detail::To_component_Fp2(RESULT());
1309
0
                break;
1310
0
            case    CF_CALCOP("LShift1(A)"):
1311
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1312
0
                PREPARE_RESULT();
1313
0
                blst_fp2_lshift(RESULT_PTR(), &A, 1);
1314
0
                ret = blst_detail::To_component_Fp2(RESULT());
1315
0
                break;
1316
0
            case    CF_CALCOP("Sqr(A)"):
1317
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1318
0
                PREPARE_RESULT();
1319
0
                blst_fp2_sqr(RESULT_PTR(), &A);
1320
0
                ret = blst_detail::To_component_Fp2(RESULT());
1321
0
                break;
1322
0
            case    CF_CALCOP("InvMod(A,B)"):
1323
0
                {
1324
0
                    CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1325
1326
0
                    bool which = false;
1327
0
                    try {
1328
0
                        which = ds.Get<bool>();
1329
0
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1330
0
                    }
1331
1332
0
                    if ( which ) {
1333
0
                        PREPARE_RESULT();
1334
0
                        CF_NORET(blst_fp2_eucl_inverse(RESULT_PTR(), &A));
1335
0
                    } else {
1336
0
                        PREPARE_RESULT();
1337
0
                        CF_NORET(blst_fp2_inverse(RESULT_PTR(), &A));
1338
0
                    }
1339
1340
0
                    ret = blst_detail::To_component_Fp2(RESULT());
1341
0
                }
1342
0
                break;
1343
0
            case    CF_CALCOP("Sqrt(A)"):
1344
0
                {
1345
0
                    blst_fp result2;
1346
0
                    CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1347
1348
0
                    if ( blst_fp2_sqrt(&result, &A) == true ) {
1349
0
                        CF_NORET(blst_fp2_sqr(&result, &result));
1350
0
                        ret = blst_detail::To_component_Fp2(result);
1351
0
                    } else {
1352
0
                        ret = { std::string("0"), std::string("0") };
1353
0
                    }
1354
0
                }
1355
0
                break;
1356
0
            case    CF_CALCOP("IsSquare(A)"):
1357
0
                {
1358
0
                    CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1359
0
                    const bool is_square = blst_fp2_is_square(&A);
1360
0
                    CF_ASSERT(
1361
0
                            is_square ==
1362
0
                            blst_fp2_sqrt(&result, &A), "");
1363
0
                }
1364
0
                break;
1365
0
            case    CF_CALCOP("Not(A)"):
1366
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1367
0
                PREPARE_RESULT();
1368
0
                blst_fp2_cneg(RESULT_PTR(), &A, true);
1369
0
                ret = blst_detail::To_component_Fp2(RESULT());
1370
0
                break;
1371
0
            case    CF_CALCOP("Set(A)"):
1372
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp2(op.bn0, A));
1373
0
                ret = blst_detail::To_component_Fp2(A);
1374
0
                break;
1375
0
        }
1376
1377
0
end:
1378
0
        return ret;
1379
0
    }
1380
1381
    #undef PREPARE_RESULT
1382
    #undef RESULT_PTR
1383
    #undef RESULT
1384
    #undef PARAM_B
1385
}
1386
1387
0
std::optional<component::Fp2> blst::OpBignumCalc_Fp2(operation::BignumCalc_Fp2& op) {
1388
0
    return blst_detail::OpBignumCalc_Fp2_prime(op);
1389
0
}
1390
1391
namespace blst_detail {
1392
0
    #define PREPARE_RESULT() {resultIdx = GetMod3(ds);}
1393
    #define RESULT_PTR() (resultIdx == 0 ? &result : (resultIdx == 1 ? &A : &B))
1394
0
    #define RESULT() (resultIdx == 0 ? result : (resultIdx == 1 ? A : B))
1395
    #define PARAM_B() (UseParamTwice(ds, &A, &B) ? &A : &B)
1396
1397
0
    std::optional<component::Fp12> OpBignumCalc_Fp12_prime(operation::BignumCalc_Fp12& op) {
1398
0
        std::optional<component::Fp12> ret = std::nullopt;
1399
0
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1400
0
        blst_fp12 result, A, B;
1401
0
        uint8_t resultIdx;
1402
1403
0
        switch ( op.calcOp.Get() ) {
1404
0
            case    CF_CALCOP("Mul(A,B)"):
1405
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1406
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn1, B));
1407
0
                PREPARE_RESULT();
1408
0
                CF_NORET(blst_fp12_mul(RESULT_PTR(), &A, PARAM_B()));
1409
1410
0
                ret = blst_detail::To_component_Fp12(RESULT());
1411
0
                break;
1412
0
            case    CF_CALCOP("Conjugate(A)"):
1413
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1414
0
                CF_NORET(blst_fp12_conjugate(&A));
1415
0
                ret = blst_detail::To_component_Fp12(A);
1416
0
                break;
1417
0
            case    CF_CALCOP("Sqr(A)"):
1418
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1419
0
                PREPARE_RESULT();
1420
0
                CF_NORET(blst_fp12_sqr(RESULT_PTR(), &A));
1421
0
                ret = blst_detail::To_component_Fp12(RESULT());
1422
0
                break;
1423
0
            case    CF_CALCOP("CyclotomicSqr(A)"):
1424
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1425
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn1, B));
1426
0
                CF_NORET(blst_fp12_cyclotomic_sqr(&A, &B));
1427
0
                ret = blst_detail::To_component_Fp12(A);
1428
0
                break;
1429
0
            case    CF_CALCOP("InvMod(A,B)"):
1430
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1431
1432
0
                PREPARE_RESULT();
1433
0
                CF_NORET(blst_fp12_inverse(RESULT_PTR(), &A));
1434
1435
0
                ret = blst_detail::To_component_Fp12(RESULT());
1436
0
                break;
1437
0
            case    CF_CALCOP("One()"):
1438
0
                ret = blst_detail::To_component_Fp12(*(blst_fp12_one()));
1439
0
                break;
1440
0
            case    CF_CALCOP("IsOne(A)"):
1441
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1442
0
                if ( blst_fp12_is_one(&A) ) {
1443
0
                    ret = blst_detail::To_component_Fp12(*(blst_fp12_one()));
1444
0
                } else {
1445
0
                    memset(&result, 0, sizeof(result));
1446
0
                    ret = blst_detail::To_component_Fp12(result);
1447
0
                }
1448
0
                break;
1449
0
            case    CF_CALCOP("IsEq(A,B)"):
1450
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn0, A));
1451
0
                CF_CHECK_TRUE(blst_detail::To_blst_fp12(op.bn1, B));
1452
1453
0
                if ( blst_fp12_is_equal(&A, &B) ) {
1454
0
                    ret = blst_detail::To_component_Fp12(*(blst_fp12_one()));
1455
0
                } else {
1456
0
                    memset(&result, 0, sizeof(result));
1457
0
                    ret = blst_detail::To_component_Fp12(result);
1458
0
                }
1459
0
                break;
1460
0
        }
1461
1462
0
end:
1463
0
        return ret;
1464
0
    }
1465
1466
    #undef PREPARE_RESULT
1467
    #undef RESULT_PTR
1468
    #undef RESULT
1469
    #undef PARAM_B
1470
}
1471
1472
0
std::optional<component::Fp12> blst::OpBignumCalc_Fp12(operation::BignumCalc_Fp12& op) {
1473
0
    return blst_detail::OpBignumCalc_Fp12_prime(op);
1474
0
}
1475
1476
188
std::optional<component::G1> blst::OpBLS_Decompress_G1(operation::BLS_Decompress_G1& op) {
1477
188
    std::optional<component::G1> ret = std::nullopt;
1478
188
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1479
1480
188
    blst_p1_affine point;
1481
188
    std::optional<std::array<uint8_t, 48>> compressed = std::nullopt;
1482
1483
188
    CF_CHECK_NE(compressed = blst_detail::ToArray<48>(op.compressed, false), std::nullopt);
1484
1485
181
    if ( blst_p1_uncompress(&point, compressed->data()) == BLST_SUCCESS ) {
1486
15
        blst_detail::G1 g1(point, ds);
1487
15
        ret = g1.To_Component_G1();
1488
166
    } else {
1489
166
        ret = component::G1{"0", "0"};
1490
166
    }
1491
1492
188
end:
1493
188
    return ret;
1494
181
}
1495
1496
41
std::optional<component::Bignum> blst::OpBLS_Compress_G1(operation::BLS_Compress_G1& op) {
1497
41
    std::optional<component::Bignum> ret = std::nullopt;
1498
1499
41
    std::array<uint8_t, 48> out;
1500
41
    blst_p1_affine point;
1501
1502
41
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.first, point.x));
1503
38
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.second, point.y));
1504
    //CF_CHECK_FALSE(blst_detail::IsZero(&point));
1505
1506
37
    CF_CHECK_TRUE(blst_p1_affine_on_curve(&point));
1507
1508
27
    CF_NORET(blst_p1_affine_compress(out.data(), &point));
1509
27
    ret = util::BinToDec(out.data(), 48);
1510
1511
41
end:
1512
41
    return ret;
1513
27
}
1514
1515
0
std::optional<component::G2> blst::OpBLS_Decompress_G2(operation::BLS_Decompress_G2& op) {
1516
0
    std::optional<component::G2> ret = std::nullopt;
1517
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1518
1519
0
    blst_p2_affine point;
1520
0
    std::optional<std::array<uint8_t, 48>> compressed_x = std::nullopt;
1521
0
    std::optional<std::array<uint8_t, 48>> compressed_y = std::nullopt;
1522
0
    std::array<uint8_t, 96> compressed;
1523
1524
0
    CF_CHECK_NE(compressed_x = blst_detail::ToArray<48>(op.compressed.first, false), std::nullopt);
1525
0
    CF_CHECK_NE(compressed_y = blst_detail::ToArray<48>(op.compressed.second, false), std::nullopt);
1526
1527
0
    memcpy(compressed.data(), compressed_x->data(), 48);
1528
0
    memcpy(compressed.data() + 48, compressed_y->data(), 48);
1529
1530
0
    if ( blst_p2_uncompress(&point, compressed.data()) == BLST_SUCCESS ) {
1531
0
        ret = blst_detail::To_G2(point);
1532
0
    } else {
1533
0
        ret = component::G2{"0", "0", "0", "0"};
1534
0
    }
1535
1536
0
end:
1537
0
    return ret;
1538
0
}
1539
1540
0
std::optional<component::G1> blst::OpBLS_Compress_G2(operation::BLS_Compress_G2& op) {
1541
0
    std::optional<component::G1> ret = std::nullopt;
1542
1543
0
    std::array<uint8_t, 96> out;
1544
0
    blst_p2_affine point;
1545
1546
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.first.first, point.x.fp[0]));
1547
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.first.second, point.y.fp[0]));
1548
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.second.first, point.x.fp[1]));
1549
0
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.second.second, point.y.fp[1]));
1550
1551
0
    CF_CHECK_TRUE(blst_p2_affine_on_curve(&point));
1552
1553
0
    CF_NORET(blst_p2_affine_compress(out.data(), &point));
1554
0
    ret = { util::BinToDec(out.data(), 48), util::BinToDec(out.data() + 48, 48) };
1555
1556
0
end:
1557
0
    return ret;
1558
0
}
1559
1560
121
std::optional<component::G1> blst::OpBLS_G1_Add(operation::BLS_G1_Add& op) {
1561
121
    std::optional<component::G1> ret = std::nullopt;
1562
121
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1563
1564
121
    std::optional<blst_p1_affine> aff_tmp;
1565
121
    blst_p1_affine aff_a, aff_b, aff_result;
1566
1567
121
    std::optional<blst_p1> tmp;
1568
121
    blst_p1 a, b, result;
1569
1570
121
    bool eq;
1571
121
    uint8_t mode = 0;
1572
121
    bool doAffine = true;
1573
1574
121
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.a), std::nullopt);
1575
107
    aff_a = *aff_tmp;
1576
1577
107
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.b), std::nullopt);
1578
98
    aff_b = *aff_tmp;
1579
1580
98
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1581
98
    a = *tmp;
1582
1583
98
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.b), std::nullopt);
1584
98
    b = *tmp;
1585
1586
98
    eq = blst_p1_is_equal(&a, &b);
1587
1588
98
    try {
1589
98
        mode = ds.Get<uint8_t>() % 3;
1590
98
    } catch ( fuzzing::datasource::Base::OutOfData ) {
1591
67
    }
1592
1593
    /* Modes:
1594
     *
1595
     * 0 = pure add
1596
     * 1 = pure double
1597
     * 2 = mixed add and double
1598
     */
1599
1600
98
    if ( mode == 0 && eq ) {
1601
22
        mode = 1;
1602
76
    } else if ( mode == 1 && !eq ) {
1603
6
        mode = 0;
1604
6
    }
1605
1606
98
    try {
1607
98
        doAffine = ds.Get<bool>();
1608
98
    } catch ( fuzzing::datasource::Base::OutOfData ) {
1609
79
    }
1610
1611
98
    switch ( mode ) {
1612
65
        case    0:
1613
65
            if ( doAffine == true ) {
1614
55
                bool doMulti = false;
1615
55
                try {
1616
55
                    doMulti = ds.Get<bool>();
1617
55
                } catch ( fuzzing::datasource::Base::OutOfData ) {
1618
51
                }
1619
1620
55
                if ( doMulti == false ) {
1621
53
                    CF_NORET(blst_p1_add_affine(&result, &a, &aff_b));
1622
53
                } else {
1623
2
                    const blst_p1_affine *const points[2] = {&aff_a, &aff_b};
1624
2
                    CF_NORET(blst_p1s_add(&result, points, 2));
1625
2
                }
1626
55
            } else {
1627
10
                CF_NORET(blst_p1_add(&result, &a, &b));
1628
10
            }
1629
65
            break;
1630
65
        case    1:
1631
24
            CF_NORET(blst_p1_double(&result, &a));
1632
24
            break;
1633
9
        case    2:
1634
9
            if ( doAffine == true ) {
1635
6
                CF_NORET(blst_p1_add_or_double_affine(&result, &a, &aff_b));
1636
6
            } else {
1637
3
                CF_NORET(blst_p1_add_or_double(&result, &a, &b));
1638
3
            }
1639
9
            break;
1640
0
        default:
1641
0
            CF_UNREACHABLE();
1642
98
    }
1643
1644
98
    CF_CHECK_TRUE(
1645
98
            !blst_p1_affine_is_inf(&aff_a) &&
1646
98
            blst_p1_affine_on_curve(&aff_a) &&
1647
98
            blst_p1_affine_in_g1(&aff_a));
1648
18
    CF_CHECK_TRUE(
1649
18
            !blst_p1_affine_is_inf(&aff_b) &&
1650
18
            blst_p1_affine_on_curve(&aff_b) &&
1651
18
            blst_p1_affine_in_g1(&aff_b));
1652
1653
12
    CF_NORET(blst_p1_to_affine(&aff_result, &result));
1654
1655
12
    {
1656
12
        blst_detail::G1 g1(aff_result, ds);
1657
12
        ret = g1.To_Component_G1();
1658
12
    }
1659
1660
121
end:
1661
121
    return ret;
1662
12
}
1663
1664
115
std::optional<component::G1> blst::OpBLS_G1_Mul(operation::BLS_G1_Mul& op) {
1665
115
    std::optional<component::G1> ret = std::nullopt;
1666
115
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1667
1668
115
    blst_p1_affine aff_result;
1669
115
    std::optional<blst_p1> tmp;
1670
115
    blst_p1 a, result;
1671
115
    std::optional<std::vector<uint8_t>> b;
1672
1673
115
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1674
102
    a = *tmp;
1675
1676
102
    CF_CHECK_NE(b = util::DecToBin(op.b.ToTrimmedString()), std::nullopt);
1677
1678
102
    {
1679
102
        std::vector<uint8_t> b_reversed = util::AddLeadingZeroes(ds, *b);
1680
102
        CF_NORET(std::reverse(b_reversed.begin(), b_reversed.end()));
1681
1682
102
        Buffer B(b_reversed);
1683
1684
102
        CF_NORET(blst_p1_mult(&result, &a, B.GetPtr(&ds), B.GetSize() * 8));
1685
102
    }
1686
1687
102
    CF_NORET(blst_p1_to_affine(&aff_result, &result));
1688
1689
102
    CF_CHECK_TRUE(
1690
102
            !blst_p1_is_inf(&a) &&
1691
102
            blst_p1_on_curve(&a) &&
1692
102
            blst_p1_in_g1(&a));
1693
1694
13
    {
1695
13
        blst_detail::G1 g1(aff_result, ds);
1696
13
        ret = g1.To_Component_G1();
1697
13
    }
1698
1699
115
end:
1700
115
    return ret;
1701
13
}
1702
1703
89
std::optional<bool> blst::OpBLS_G1_IsEq(operation::BLS_G1_IsEq& op) {
1704
89
    std::optional<bool> ret = std::nullopt;
1705
89
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1706
1707
89
    std::optional<blst_p1_affine> aff_tmp;
1708
89
    blst_p1_affine aff_a, aff_b;
1709
1710
89
    std::optional<blst_p1> tmp;
1711
89
    blst_p1 a, b;
1712
1713
89
    bool doAffine = false;
1714
1715
89
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.a), std::nullopt);
1716
79
    aff_a = *aff_tmp;
1717
1718
79
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.b), std::nullopt);
1719
61
    aff_b = *aff_tmp;
1720
1721
61
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1722
61
    a = *tmp;
1723
1724
61
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.b), std::nullopt);
1725
61
    b = *tmp;
1726
1727
61
    try {
1728
61
        doAffine = ds.Get<bool>();
1729
61
    } catch ( fuzzing::datasource::Base::OutOfData ) {
1730
45
    }
1731
1732
61
    if ( doAffine == true ) {
1733
4
        ret = blst_p1_affine_is_equal(&aff_a, &aff_b);
1734
57
    } else {
1735
57
        ret = blst_p1_is_equal(&a, &b);
1736
57
    }
1737
1738
89
end:
1739
89
    return ret;
1740
61
}
1741
1742
748
std::optional<component::G1> blst::OpBLS_G1_Neg(operation::BLS_G1_Neg& op) {
1743
748
    std::optional<component::G1> ret = std::nullopt;
1744
748
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1745
1746
748
    blst_p1_affine aff_result;
1747
748
    std::optional<blst_p1> tmp;
1748
748
    blst_p1 a;
1749
1750
748
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1751
735
    a = *tmp;
1752
1753
735
    CF_NORET(blst_p1_cneg(&a, true));
1754
1755
735
    CF_NORET(blst_p1_to_affine(&aff_result, &a));
1756
1757
735
    {
1758
735
        blst_detail::G1 g1(aff_result, ds);
1759
735
        ret = g1.To_Component_G1();
1760
735
    }
1761
1762
748
end:
1763
748
    return ret;
1764
735
}
1765
1766
113
std::optional<component::G2> blst::OpBLS_G2_Add(operation::BLS_G2_Add& op) {
1767
113
    std::optional<component::G2> ret = std::nullopt;
1768
113
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1769
1770
113
    blst_p2_affine a, b, result_;
1771
113
    blst_p2 a_, b_, result;
1772
113
    bool doDouble = false;
1773
1774
113
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1775
102
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1776
100
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1777
90
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1778
1779
86
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.first, b.x.fp[0]));
1780
76
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.second, b.y.fp[0]));
1781
72
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.first, b.x.fp[1]));
1782
61
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.second, b.y.fp[1]));
1783
1784
57
    CF_NORET(blst_p2_from_affine(&a_, &a));
1785
57
    CF_NORET(blst_p2_from_affine(&b_, &b));
1786
1787
57
    if ( blst_p2_is_equal(&a_, &b_) ) {
1788
12
        try {
1789
12
            doDouble = ds.Get<bool>();
1790
12
        } catch ( fuzzing::datasource::Base::OutOfData ) {
1791
8
        }
1792
12
    }
1793
1794
57
    if ( doDouble == false ) {
1795
54
        CF_NORET(blst_p2_add_or_double_affine(&result, &a_, &b));
1796
54
    } else {
1797
3
        CF_NORET(blst_p2_double(&result, &a_));
1798
3
    }
1799
1800
1801
57
    CF_NORET(blst_p2_to_affine(&result_, &result));
1802
1803
57
    ret = blst_detail::To_G2(result_);
1804
1805
113
end:
1806
113
    return ret;
1807
57
}
1808
1809
116
std::optional<component::G2> blst::OpBLS_G2_Mul(operation::BLS_G2_Mul& op) {
1810
116
    std::optional<component::G2> ret = std::nullopt;
1811
116
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1812
1813
116
    blst_p2_affine a, result_;
1814
116
    blst_p2 a_, result;
1815
116
    std::optional<std::vector<uint8_t>> b;
1816
116
    bool doDouble = false;
1817
1818
116
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1819
114
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1820
111
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1821
101
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1822
92
    CF_CHECK_NE(b = util::DecToBin(op.b.ToTrimmedString()), std::nullopt);
1823
1824
92
    if ( !(blst_p2_affine_on_curve(&a) && blst_p2_affine_in_g2(&a)) ) {
1825
13
        return ret;
1826
13
    }
1827
1828
79
    CF_NORET(blst_p2_from_affine(&a_, &a));
1829
1830
79
    if ( op.b.ToTrimmedString() == "2" ) {
1831
5
        try {
1832
5
            doDouble = ds.Get<bool>();
1833
5
        } catch ( fuzzing::datasource::Base::OutOfData ) {
1834
3
        }
1835
5
    }
1836
1837
79
    if ( doDouble == false ) {
1838
78
        std::vector<uint8_t> b_reversed = util::AddLeadingZeroes(ds, *b);
1839
78
        CF_NORET(std::reverse(b_reversed.begin(), b_reversed.end()));
1840
1841
78
        Buffer B(b_reversed);
1842
1843
78
        CF_NORET(blst_p2_mult(&result, &a_, B.GetPtr(&ds), B.GetSize() * 8));
1844
78
    } else {
1845
1
        CF_NORET(blst_p2_double(&result, &a_));
1846
1
    }
1847
1848
79
    CF_NORET(blst_p2_to_affine(&result_, &result));
1849
1850
79
    ret = blst_detail::To_G2(result_);
1851
1852
103
end:
1853
103
    return ret;
1854
79
}
1855
1856
71
std::optional<bool> blst::OpBLS_G2_IsEq(operation::BLS_G2_IsEq& op) {
1857
71
    std::optional<bool> ret = std::nullopt;
1858
1859
71
    blst_p2_affine a, b;
1860
71
    blst_p2 a_, b_;
1861
1862
71
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1863
63
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1864
51
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1865
45
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1866
1867
44
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.first, b.x.fp[0]));
1868
37
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.second, b.y.fp[0]));
1869
30
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.first, b.x.fp[1]));
1870
29
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.second, b.y.fp[1]));
1871
1872
27
    CF_NORET(blst_p2_from_affine(&a_, &a));
1873
27
    CF_NORET(blst_p2_from_affine(&b_, &b));
1874
1875
27
    ret = blst_p2_is_equal(&a_, &b_);
1876
1877
71
end:
1878
71
    return ret;
1879
27
}
1880
1881
69
std::optional<component::G2> blst::OpBLS_G2_Neg(operation::BLS_G2_Neg& op) {
1882
69
    std::optional<component::G2> ret = std::nullopt;
1883
69
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1884
1885
69
    blst_p2_affine a;
1886
69
    blst_p2 a_;
1887
1888
69
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1889
62
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1890
53
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1891
43
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1892
1893
42
    CF_NORET(blst_p2_from_affine(&a_, &a));
1894
1895
42
    CF_NORET(blst_p2_cneg(&a_, true));
1896
1897
42
    CF_NORET(blst_p2_to_affine(&a, &a_));
1898
1899
42
    ret = blst_detail::To_G2(a);
1900
1901
69
end:
1902
69
    return ret;
1903
42
}
1904
1905
0
std::optional<component::G1> blst::OpBLS_G1_MultiExp(operation::BLS_G1_MultiExp& op) {
1906
0
    std::optional<component::G1> ret = std::nullopt;
1907
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1908
1909
0
    const size_t num = op.points_scalars.points_scalars.size();
1910
    /* blst_p1s_mult_pippenger crashes with num == 0
1911
     * blst_p1s_mult_pippenger OOB read with num == 1
1912
     */
1913
0
    if ( num < 2 ) return std::nullopt;
1914
1915
0
    blst_p1_affine* points = (blst_p1_affine*)util::malloc(num * sizeof(blst_p1_affine));
1916
0
    const blst_p1_affine* points_ptrs[2] = {points, nullptr};
1917
0
    blst_scalar* scalars = (blst_scalar*)util::malloc(num * sizeof(blst_scalar));
1918
0
    const uint8_t* scalars_ptrs[2] = {(uint8_t*)scalars, nullptr};
1919
1920
0
    uint8_t* scratch = util::malloc(blst_p1s_mult_pippenger_scratch_sizeof(num));
1921
1922
0
    bool points_are_valid = true;
1923
1924
0
    for (size_t i = 0; i < num; i++) {
1925
0
        const auto& cur = op.points_scalars.points_scalars[i];
1926
1927
0
        std::optional<blst_p1_affine> point;
1928
0
        CF_CHECK_NE(point = blst_detail::Load_G1_Affine(cur.first), std::nullopt);
1929
0
        points[i] = *point;
1930
1931
0
        points_are_valid &=
1932
0
            !blst_p1_affine_is_inf(&*point) &&
1933
0
            blst_p1_affine_on_curve(&*point) &&
1934
0
            blst_p1_affine_in_g1(&*point);
1935
1936
0
        CF_CHECK_TRUE(blst_detail::To_blst_scalar(cur.second, scalars[i]));
1937
0
    }
1938
1939
0
    {
1940
0
        blst_p1 res;
1941
0
        CF_NORET(blst_p1s_mult_pippenger(
1942
0
                    &res,
1943
0
                    points_ptrs, num,
1944
0
                    scalars_ptrs, sizeof(blst_scalar) * 8,
1945
0
                    (limb_t*)scratch));
1946
1947
0
        CF_CHECK_TRUE(points_are_valid);
1948
1949
0
        blst_p1_affine res_affine;
1950
0
        CF_NORET(blst_p1_to_affine(&res_affine, &res));
1951
0
        blst_detail::G1 g1(res_affine, ds);
1952
0
        ret = g1.To_Component_G1();
1953
0
    }
1954
1955
0
end:
1956
0
    util::free(points);
1957
0
    util::free(scalars);
1958
0
    util::free(scratch);
1959
1960
0
    return ret;
1961
0
}
1962
1963
0
std::optional<Buffer> blst::OpMisc(operation::Misc& op) {
1964
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1965
1966
0
    try {
1967
0
        switch ( op.operation.Get() ) {
1968
0
            case    0:
1969
0
                {
1970
0
                    const auto data = ds.GetData(0, 48, 48);
1971
0
                    blst_p1_affine point;
1972
0
                    CF_CHECK_EQ(blst_p1_uncompress(&point, data.data()), BLST_SUCCESS);
1973
0
                    uint8_t out[48];
1974
0
                    CF_NORET(blst_p1_affine_compress(out, &point));
1975
0
                    if ( blst_p1_affine_on_curve(&point) ) {
1976
0
                        CF_ASSERT(memcmp(data.data(), out, data.size()) == 0, "Serialization asymmetry");
1977
0
                    }
1978
0
                }
1979
0
                break;
1980
0
            case    1:
1981
0
                {
1982
0
                    const auto data = ds.GetData(0, 96, 96);
1983
0
                    blst_p1_affine point;
1984
0
                    CF_CHECK_EQ(blst_p1_deserialize(&point, data.data()), BLST_SUCCESS);
1985
0
                    uint8_t out[96];
1986
0
                    CF_NORET(blst_p1_affine_serialize(out, &point));
1987
0
                    if ( blst_p1_affine_on_curve(&point) ) {
1988
0
                        blst_p1_affine point2;
1989
0
                        CF_ASSERT(blst_p1_deserialize(&point2, out) == BLST_SUCCESS, "Cannot deserialize serialized point");
1990
1991
0
                        uint8_t out2[96];
1992
0
                        CF_NORET(blst_p1_affine_serialize(out2, &point2));
1993
0
                        CF_ASSERT(memcmp(out, out2, sizeof(out)) == 0, "Serialization asymmetry");
1994
                        //CF_ASSERT(memcmp(data.data(), out, data.size()) == 0, "Serialization asymmetry");
1995
0
                    }
1996
0
                }
1997
0
                break;
1998
0
            case    2:
1999
0
                {
2000
0
                    const auto data = ds.GetData(0, 96, 96);
2001
0
                    blst_p2_affine point;
2002
0
                    CF_CHECK_EQ(blst_p2_uncompress(&point, data.data()), BLST_SUCCESS);
2003
0
                    uint8_t out[96];
2004
0
                    CF_NORET(blst_p2_affine_compress(out, &point));
2005
0
                    if ( blst_p2_affine_on_curve(&point) ) {
2006
0
                        CF_ASSERT(memcmp(data.data(), out, data.size()) == 0, "Serialization asymmetry");
2007
0
                    }
2008
0
                }
2009
0
                break;
2010
0
            case    3:
2011
0
                {
2012
0
                    const auto data = ds.GetData(0, 192, 192);
2013
0
                    blst_p2_affine point;
2014
0
                    CF_CHECK_EQ(blst_p2_deserialize(&point, data.data()), BLST_SUCCESS);
2015
0
                    uint8_t out[192];
2016
0
                    CF_NORET(blst_p2_affine_serialize(out, &point));
2017
0
                    if ( blst_p2_affine_on_curve(&point) ) {
2018
0
                        blst_p2_affine point2;
2019
0
                        CF_ASSERT(blst_p2_deserialize(&point2, out) == BLST_SUCCESS, "Cannot deserialize serialized point");
2020
2021
0
                        uint8_t out2[192];
2022
0
                        CF_NORET(blst_p2_affine_serialize(out2, &point2));
2023
0
                        CF_ASSERT(memcmp(out, out2, sizeof(out)) == 0, "Serialization asymmetry");
2024
                        //CF_ASSERT(memcmp(data.data(), out, data.size()) == 0, "Serialization asymmetry");
2025
0
                    }
2026
0
                }
2027
0
                break;
2028
0
            case    4:
2029
0
                {
2030
0
                    blst_p1_affine point;
2031
0
                    {
2032
0
                        const auto data = ds.GetData(0, 96, 96);
2033
0
                        CF_CHECK_EQ(blst_p1_deserialize(&point, data.data()), BLST_SUCCESS);
2034
0
                    }
2035
2036
0
                    blst_fp6 Qlines[68];
2037
0
                    {
2038
0
                        const auto data = ds.GetData(0, sizeof(Qlines), sizeof(Qlines));
2039
0
                        memcpy(Qlines, data.data(), sizeof(Qlines));
2040
0
                    }
2041
2042
0
                    blst_fp12 out;
2043
0
                    CF_NORET(blst_miller_loop_lines(&out, Qlines, &point));
2044
0
                }
2045
0
                break;
2046
0
            case    5:
2047
0
                {
2048
0
                    blst_p2_affine point;
2049
0
                    {
2050
0
                        const auto data = ds.GetData(0, 192, 192);
2051
0
                        CF_CHECK_EQ(blst_p2_deserialize(&point, data.data()), BLST_SUCCESS);
2052
0
                    }
2053
2054
0
                    blst_fp6 Qlines[68];
2055
0
                    CF_NORET(blst_precompute_lines(Qlines, &point));
2056
0
                }
2057
0
                break;
2058
0
            case    6:
2059
0
                {
2060
0
                    blst_fp12 out;
2061
0
                    blst_p1_affine p1;
2062
0
                    blst_p2_affine p2;
2063
2064
0
                    {
2065
0
                        const auto data = ds.GetData(0, 96, 96);
2066
0
                        CF_CHECK_EQ(blst_p1_deserialize(&p1, data.data()), BLST_SUCCESS);
2067
0
                    }
2068
2069
0
                    {
2070
0
                        const auto data = ds.GetData(0, 192, 192);
2071
0
                        CF_CHECK_EQ(blst_p2_deserialize(&p2, data.data()), BLST_SUCCESS);
2072
0
                    }
2073
2074
0
                    CF_NORET(blst_miller_loop(&out, &p2, &p1));
2075
0
                }
2076
0
                break;
2077
0
        }
2078
0
    } catch ( fuzzing::datasource::Base::OutOfData ) { }
2079
2080
0
end:
2081
0
    return std::nullopt;
2082
0
}
2083
2084
5.06k
bool blst::SupportsModularBignumCalc(void) const {
2085
5.06k
    return true;
2086
5.06k
}
2087
2088
} /* namespace module */
2089
} /* namespace cryptofuzz */