Coverage Report

Created: 2026-09-28 06:36

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.26k
    void Reverse(std::array<uint8_t, Size>& v) {
21
1.26k
        std::reverse(v.begin(), v.end());
22
1.26k
    }
void cryptofuzz::module::blst_detail::Reverse<48ul>(std::__1::array<unsigned char, 48ul>&)
Line
Count
Source
20
11.1k
    void Reverse(std::array<uint8_t, Size>& v) {
21
11.1k
        std::reverse(v.begin(), v.end());
22
11.1k
    }
23
24
    template <size_t Size>
25
9.04k
    std::optional<std::array<uint8_t, Size>> ToArray(const component::Bignum& bn, const bool reverse = true) {
26
9.04k
        const auto _ret = util::DecToBin(bn.ToTrimmedString(), Size);
27
9.04k
        if ( _ret == std::nullopt ) {
28
648
            return std::nullopt;
29
648
        }
30
31
8.40k
        std::array<uint8_t, Size> ret;
32
8.40k
        memcpy(ret.data(), _ret->data(), Size);
33
8.40k
        if ( reverse == true ) {
34
8.23k
            Reverse<>(ret);
35
8.23k
        }
36
37
8.40k
        return ret;
38
9.04k
    }
std::__1::optional<std::__1::array<unsigned char, 32ul> > cryptofuzz::module::blst_detail::ToArray<32ul>(cryptofuzz::Bignum const&, bool)
Line
Count
Source
25
1.23k
    std::optional<std::array<uint8_t, Size>> ToArray(const component::Bignum& bn, const bool reverse = true) {
26
1.23k
        const auto _ret = util::DecToBin(bn.ToTrimmedString(), Size);
27
1.23k
        if ( _ret == std::nullopt ) {
28
212
            return std::nullopt;
29
212
        }
30
31
1.02k
        std::array<uint8_t, Size> ret;
32
1.02k
        memcpy(ret.data(), _ret->data(), Size);
33
1.02k
        if ( reverse == true ) {
34
1.02k
            Reverse<>(ret);
35
1.02k
        }
36
37
1.02k
        return ret;
38
1.23k
    }
std::__1::optional<std::__1::array<unsigned char, 48ul> > cryptofuzz::module::blst_detail::ToArray<48ul>(cryptofuzz::Bignum const&, bool)
Line
Count
Source
25
7.81k
    std::optional<std::array<uint8_t, Size>> ToArray(const component::Bignum& bn, const bool reverse = true) {
26
7.81k
        const auto _ret = util::DecToBin(bn.ToTrimmedString(), Size);
27
7.81k
        if ( _ret == std::nullopt ) {
28
436
            return std::nullopt;
29
436
        }
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.21k
            Reverse<>(ret);
35
7.21k
        }
36
37
7.38k
        return ret;
38
7.81k
    }
39
967
    bool To_blst_fr(const component::Bignum& bn, blst_fr& out) {
40
967
        const auto ret = ToArray<32>(bn);
41
42
967
        if ( ret == std::nullopt ) {
43
190
            return false;
44
190
        }
45
46
777
        CF_NORET(blst_fr_from_uint64(&out, (const uint64_t*)ret->data()));
47
777
        return true;
48
967
    }
49
7.64k
    bool To_blst_fp(const component::Bignum& bn, blst_fp& out) {
50
7.64k
        const auto ret = ToArray<48>(bn);
51
52
7.64k
        if ( ret == std::nullopt ) {
53
430
            return false;
54
430
        }
55
56
7.21k
        CF_NORET(blst_fp_from_uint64(&out, (const uint64_t*)ret->data()));
57
7.21k
        return true;
58
7.64k
    }
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
244
    component::Bignum To_component_bignum(const blst_fr& in) {
79
244
        std::array<uint8_t, 32> v;
80
81
244
        blst_uint64_from_fr((uint64_t*)v.data(), &in);
82
83
244
        Reverse<>(v);
84
244
        return util::BinToDec(v.data(), 32);
85
244
    }
86
400
    component::Bignum To_component_bignum(const blst_fp& in) {
87
400
        std::array<uint8_t, 48> v;
88
89
400
        blst_uint64_from_fp((uint64_t*)v.data(), &in);
90
91
400
        Reverse<>(v);
92
400
        return util::BinToDec(v.data(), 48);
93
400
    }
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
234
    boost::multiprecision::cpp_int To_cpp_int(const component::Bignum& in) {
109
234
        return boost::multiprecision::cpp_int(in.ToTrimmedString());
110
234
    }
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
867
                ds(ds) {
119
867
                memcpy(&this->g1, &g1, sizeof(g1));
120
867
            }
121
867
            const blst_p1_affine* Get(void) {
122
867
                return &g1;
123
867
            }
124
867
            component::G1 To_Component_G1(void) {
125
867
                std::array<uint8_t, 48> x, y;
126
127
867
                const blst_p1_affine* ptr = Get();
128
129
867
                blst_uint64_from_fp((uint64_t*)x.data(), &ptr->x);
130
867
                blst_uint64_from_fp((uint64_t*)y.data(), &ptr->y);
131
132
867
                Reverse<>(x);
133
867
                Reverse<>(y);
134
135
867
                return {util::BinToDec(x.data(), 48), util::BinToDec(y.data(), 48)};
136
867
            }
137
    };
138
349
    component::G2 To_G2(const blst_p2_affine& g2) {
139
349
        std::array<uint8_t, 48> x1, y1, x2, y2;
140
141
349
        blst_uint64_from_fp((uint64_t*)x1.data(), &g2.x.fp[0]);
142
349
        blst_uint64_from_fp((uint64_t*)y1.data(), &g2.y.fp[0]);
143
349
        blst_uint64_from_fp((uint64_t*)x2.data(), &g2.x.fp[1]);
144
349
        blst_uint64_from_fp((uint64_t*)y2.data(), &g2.y.fp[1]);
145
146
349
        Reverse<>(x1);
147
349
        Reverse<>(y1);
148
349
        Reverse<>(x2);
149
349
        Reverse<>(y2);
150
151
349
        return {
152
349
            util::BinToDec(x1.data(), 48),
153
349
            util::BinToDec(y1.data(), 48),
154
349
            util::BinToDec(x2.data(), 48),
155
349
            util::BinToDec(y2.data(), 48)
156
349
        };
157
349
    }
158
159
31
    component::Fp12 To_component_Fp12(const blst_fp12& fp12) {
160
31
        std::array<uint8_t, 48> bn1, bn2, bn3, bn4, bn5, bn6, bn7, bn8, bn9, bn10, bn11, bn12;
161
162
31
        blst_uint64_from_fp((uint64_t*)bn1.data(), &fp12.fp6[0].fp2[0].fp[0]);
163
31
        Reverse<>(bn1);
164
165
31
        blst_uint64_from_fp((uint64_t*)bn2.data(), &fp12.fp6[0].fp2[0].fp[1]);
166
31
        Reverse<>(bn2);
167
168
31
        blst_uint64_from_fp((uint64_t*)bn3.data(), &fp12.fp6[0].fp2[1].fp[0]);
169
31
        Reverse<>(bn3);
170
171
31
        blst_uint64_from_fp((uint64_t*)bn4.data(), &fp12.fp6[0].fp2[1].fp[1]);
172
31
        Reverse<>(bn4);
173
174
31
        blst_uint64_from_fp((uint64_t*)bn5.data(), &fp12.fp6[0].fp2[2].fp[0]);
175
31
        Reverse<>(bn5);
176
177
31
        blst_uint64_from_fp((uint64_t*)bn6.data(), &fp12.fp6[0].fp2[2].fp[1]);
178
31
        Reverse<>(bn6);
179
180
31
        blst_uint64_from_fp((uint64_t*)bn7.data(), &fp12.fp6[1].fp2[0].fp[0]);
181
31
        Reverse<>(bn7);
182
183
31
        blst_uint64_from_fp((uint64_t*)bn8.data(), &fp12.fp6[1].fp2[0].fp[1]);
184
31
        Reverse<>(bn8);
185
186
31
        blst_uint64_from_fp((uint64_t*)bn9.data(), &fp12.fp6[1].fp2[1].fp[0]);
187
31
        Reverse<>(bn9);
188
189
31
        blst_uint64_from_fp((uint64_t*)bn10.data(), &fp12.fp6[1].fp2[1].fp[1]);
190
31
        Reverse<>(bn10);
191
192
31
        blst_uint64_from_fp((uint64_t*)bn11.data(), &fp12.fp6[1].fp2[2].fp[0]);
193
31
        Reverse<>(bn11);
194
195
31
        blst_uint64_from_fp((uint64_t*)bn12.data(), &fp12.fp6[1].fp2[2].fp[1]);
196
31
        Reverse<>(bn12);
197
198
31
        return {
199
31
            util::BinToDec(bn1.data(), 48),
200
31
            util::BinToDec(bn2.data(), 48),
201
31
            util::BinToDec(bn3.data(), 48),
202
31
            util::BinToDec(bn4.data(), 48),
203
31
            util::BinToDec(bn5.data(), 48),
204
31
            util::BinToDec(bn6.data(), 48),
205
31
            util::BinToDec(bn7.data(), 48),
206
31
            util::BinToDec(bn8.data(), 48),
207
31
            util::BinToDec(bn9.data(), 48),
208
31
            util::BinToDec(bn10.data(), 48),
209
31
            util::BinToDec(bn11.data(), 48),
210
31
            util::BinToDec(bn12.data(), 48),
211
31
        };
212
31
    }
213
214
265
    bool To_blst_scalar(const component::Bignum& bn, blst_scalar& out) {
215
265
        const auto ret = ToArray<32>(bn);
216
217
265
        if ( ret == std::nullopt ) {
218
22
            return false;
219
22
        }
220
221
243
        CF_NORET(blst_scalar_from_uint64(&out, (const uint64_t*)ret->data()));
222
223
243
        return true;
224
265
    }
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
182
    static size_t isMultipleOf2(const std::string s) {
244
182
        auto i = boost::multiprecision::cpp_int(s);
245
182
        if ( i == 0 || i == 1 ) {
246
5
            return 0;
247
5
        }
248
177
        size_t num = 0;
249
17.4k
        while ( i ) {
250
17.4k
            if ( i != 1 && i & 1 ) {
251
141
                return 0;
252
141
            }
253
17.2k
            i >>= 1;
254
17.2k
            num++;
255
17.2k
        }
256
257
36
        return num - 1;
258
177
    }
259
39
    static bool IsZero(const blst_p2_affine* g2) {
260
39
        blst_p2_affine zero;
261
39
        memset(&zero, 0, sizeof(zero));
262
39
        return memcmp(g2, &zero, sizeof(zero)) == 0;
263
39
    }
264
265
450
    static std::optional<blst_p1_affine> Load_G1_Affine(const component::G1& g1) {
266
450
        std::optional<blst_p1_affine> ret = std::nullopt;
267
268
450
        blst_p1_affine aff_a;
269
270
450
        CF_CHECK_TRUE(blst_detail::To_blst_fp(g1.first, aff_a.x));
271
417
        CF_CHECK_TRUE(blst_detail::To_blst_fp(g1.second, aff_a.y));
272
273
392
        ret = aff_a;
274
450
end:
275
450
        return ret;
276
392
    }
277
278
    static std::optional<blst_p1> Load_G1_Projective(
279
            fuzzing::datasource::Datasource& ds,
280
1.21k
            const component::G1& g1) {
281
1.21k
        std::optional<blst_p1> ret = std::nullopt;
282
283
1.21k
        blst_p1 a;
284
285
1.21k
        const auto proj = util::ToRandomProjective(
286
1.21k
                ds,
287
1.21k
                g1.first.ToTrimmedString(),
288
1.21k
                g1.second.ToTrimmedString(),
289
1.21k
                CF_ECC_CURVE("BLS12_381"));
290
291
1.21k
        CF_CHECK_TRUE(blst_detail::To_blst_fp(proj[0], a.x));
292
1.20k
        CF_CHECK_TRUE(blst_detail::To_blst_fp(proj[1], a.y));
293
1.18k
        CF_CHECK_TRUE(blst_detail::To_blst_fp(proj[2], a.z));
294
295
1.18k
        ret = a;
296
1.21k
end:
297
1.21k
        return ret;
298
1.18k
    }
299
}
300
301
69
std::optional<component::BLS_PublicKey> blst::OpBLS_PrivateToPublic(operation::BLS_PrivateToPublic& op) {
302
69
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
303
        //return std::nullopt;
304
19
    }
305
306
69
    std::optional<component::BLS_PublicKey> ret = std::nullopt;
307
69
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
308
309
69
    blst_scalar priv;
310
69
    blst_p1 pub;
311
69
    blst_p1_affine pub_affine;
312
313
69
    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
55
    CF_CHECK_TRUE(blst_sk_check(&priv));
319
320
42
    CF_NORET(blst_sk_to_pk_in_g1(&pub, &priv));
321
42
    CF_ASSERT(blst_p1_on_curve(&pub) == true, "Generated pubkey not on curve");
322
42
    CF_ASSERT(blst_p1_in_g1(&pub) == true, "Generated pubkey not in group");
323
324
42
    CF_NORET(blst_p1_to_affine(&pub_affine, &pub));
325
326
42
    {
327
42
        blst_detail::G1 g1(pub_affine, ds);
328
42
        ret = g1.To_Component_G1();
329
42
    }
330
331
69
end:
332
69
    return ret;
333
42
}
334
335
196
std::optional<component::G2> blst::OpBLS_PrivateToPublic_G2(operation::BLS_PrivateToPublic_G2& op) {
336
196
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
337
        //return std::nullopt;
338
186
    }
339
340
196
    std::optional<component::G2> ret = std::nullopt;
341
196
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
342
343
196
    blst_scalar priv;
344
196
    blst_p2 pub;
345
196
    blst_p2_affine pub_affine;
346
347
196
    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
188
    CF_CHECK_TRUE(blst_sk_check(&priv));
353
354
132
    CF_NORET(blst_sk_to_pk_in_g2(&pub, &priv));
355
132
    CF_ASSERT(blst_p2_on_curve(&pub) == true, "Generated pubkey not on curve");
356
132
    CF_ASSERT(blst_p2_in_g2(&pub) == true, "Generated pubkey not in group");
357
358
132
    CF_NORET(blst_p2_to_affine(&pub_affine, &pub));
359
360
132
    ret = blst_detail::To_G2(pub_affine);
361
362
196
end:
363
196
    return ret;
364
132
}
365
366
47
std::optional<component::G1> blst::OpBLS_HashToG1(operation::BLS_HashToG1& op) {
367
47
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
368
        //return std::nullopt;
369
17
    }
370
371
47
    std::optional<component::G1> ret = std::nullopt;
372
47
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
373
374
47
    blst_p1 g1;
375
47
    blst_p1_affine g1_affine;
376
377
47
    CF_NORET(blst_hash_to_g1(
378
47
            &g1,
379
47
            op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
380
47
            op.dest.GetPtr(&ds), op.dest.GetSize(),
381
47
            op.aug.GetPtr(&ds), op.aug.GetSize()));
382
383
47
    CF_ASSERT(blst_p1_on_curve(&g1) == true, "Generated g1 not on curve");
384
47
    CF_ASSERT(blst_p1_in_g1(&g1) == true, "Generated g1 not in group");
385
386
47
    CF_NORET(blst_p1_to_affine(&g1_affine, &g1));
387
388
47
    {
389
47
        blst_detail::G1 _g1(g1_affine, ds);
390
47
        ret = _g1.To_Component_G1();
391
47
    }
392
393
47
    return ret;
394
47
}
395
396
53
std::optional<component::G2> blst::OpBLS_HashToG2(operation::BLS_HashToG2& op) {
397
53
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
398
        //return std::nullopt;
399
9
    }
400
401
53
    std::optional<component::G2> ret = std::nullopt;
402
53
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
403
404
53
    blst_p2 g2;
405
53
    blst_p2_affine g2_affine;
406
407
53
    CF_NORET(blst_hash_to_g2(
408
53
            &g2,
409
53
            op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
410
53
            op.dest.GetPtr(&ds), op.dest.GetSize(),
411
53
            op.aug.GetPtr(&ds), op.aug.GetSize()));
412
413
53
    CF_ASSERT(blst_p2_on_curve(&g2) == true, "Generated g2 not on curve");
414
53
    CF_ASSERT(blst_p2_in_g2(&g2) == true, "Generated g2 not in group");
415
416
53
    CF_NORET(blst_p2_to_affine(&g2_affine, &g2));
417
418
53
    ret = blst_detail::To_G2(g2_affine);
419
420
53
    return ret;
421
53
}
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
34
        std::optional<blst_p1_affine> aff_g1;
682
34
        CF_CHECK_NE(aff_g1 = blst_detail::Load_G1_Affine(op.g1), std::nullopt);
683
24
        return
684
24
            !blst_p1_affine_is_inf(&*aff_g1) &&
685
12
            blst_p1_affine_on_curve(&*aff_g1) &&
686
5
            blst_p1_affine_in_g1(&*aff_g1);
687
34
    } else {
688
21
        std::optional<blst_p1> g1;
689
21
        CF_CHECK_NE(g1 = blst_detail::Load_G1_Projective(ds, op.g1), std::nullopt);
690
17
        return
691
17
            !blst_p1_is_inf(&*g1) &&
692
17
            blst_p1_on_curve(&*g1) &&
693
2
            blst_p1_in_g1(&*g1);
694
21
    }
695
14
end:
696
14
    return false;
697
55
}
698
699
58
std::optional<bool> blst::OpBLS_IsG2OnCurve(operation::BLS_IsG2OnCurve& op) {
700
58
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
701
702
58
    bool useAffine = true;
703
704
58
    try {
705
58
        useAffine = ds.Get<bool>();
706
58
    } catch ( fuzzing::datasource::Base::OutOfData ) { }
707
708
58
    blst_p2 g2;
709
58
    blst_p2_affine g2_affine;
710
711
58
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.first, g2_affine.x.fp[0]));
712
54
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.first.second, g2_affine.y.fp[0]));
713
51
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.first, g2_affine.x.fp[1]));
714
42
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.second, g2_affine.y.fp[1]));
715
716
39
    CF_CHECK_FALSE(blst_detail::IsZero(&g2_affine));
717
718
22
    if ( useAffine ) {
719
17
        return blst_p2_affine_on_curve(&g2_affine) && blst_p2_affine_in_g2(&g2_affine);
720
17
    } else {
721
5
        CF_NORET(blst_p2_from_affine(&g2, &g2_affine));
722
5
        return blst_p2_on_curve(&g2) && blst_p2_in_g2(&g2);
723
5
    }
724
725
36
end:
726
36
    return false;
727
22
}
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
40
std::optional<component::Fp12> blst::OpBLS_Pairing(operation::BLS_Pairing& op) {
850
40
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
851
40
    std::optional<component::Fp12> ret = std::nullopt;
852
853
40
    blst_p1_affine g1_affine;
854
40
    blst_p2_affine g2_affine;
855
40
    blst_fp12 out;
856
857
40
    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
33
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.first, g2_affine.x.fp[1]));
863
32
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.g2.second.second, g2_affine.y.fp[1]));
864
865
31
    CF_NORET(blst_miller_loop(&out, &g2_affine, &g1_affine));
866
31
    CF_NORET(blst_final_exp(&out, &out));
867
868
31
    ret = blst_detail::To_component_Fp12(out);
869
870
40
end:
871
40
    return ret;
872
31
}
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
338
    bool UseParamTwice(fuzzing::datasource::Datasource& ds, const T* A, const T* B) {
917
338
        if ( memcmp(A, B, sizeof(T)) != 0 ) {
918
311
            return false;
919
311
        }
920
921
27
        try {
922
27
            return ds.Get<bool>();
923
27
        } catch ( fuzzing::datasource::Base::OutOfData ) {
924
19
        }
925
926
19
        return false;
927
27
    }
bool cryptofuzz::module::blst_detail::UseParamTwice<blst_fr>(fuzzing::datasource::Datasource&, blst_fr const*, blst_fr const*)
Line
Count
Source
916
153
    bool UseParamTwice(fuzzing::datasource::Datasource& ds, const T* A, const T* B) {
917
153
        if ( memcmp(A, B, sizeof(T)) != 0 ) {
918
135
            return false;
919
135
        }
920
921
18
        try {
922
18
            return ds.Get<bool>();
923
18
        } catch ( fuzzing::datasource::Base::OutOfData ) {
924
13
        }
925
926
13
        return false;
927
18
    }
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
176
            return false;
919
176
        }
920
921
9
        try {
922
9
            return ds.Get<bool>();
923
9
        } catch ( fuzzing::datasource::Base::OutOfData ) {
924
6
        }
925
926
6
        return false;
927
9
    }
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
574
    uint8_t GetMod3(fuzzing::datasource::Datasource& ds) {
930
574
        try {
931
574
            return ds.Get<uint8_t>() % 3;
932
574
        } catch ( fuzzing::datasource::Base::OutOfData ) {
933
476
        }
934
935
476
        return 0;
936
574
    }
937
938
574
    #define PREPARE_RESULT() {resultIdx = GetMod3(ds);}
939
337
    #define RESULT_PTR() (resultIdx == 0 ? &result : (resultIdx == 1 ? &A : &B))
940
521
    #define RESULT() (resultIdx == 0 ? result : (resultIdx == 1 ? A : B))
941
135
    #define PARAM_B() (UseParamTwice(ds, &A, &B) ? &A : &B)
942
943
2.20k
    std::optional<component::Bignum> OpBignumCalc_order(operation::BignumCalc& op) {
944
2.20k
        std::optional<component::Bignum> ret = std::nullopt;
945
2.20k
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
946
2.20k
        blst_fr result, A, B;
947
2.20k
        uint8_t resultIdx;
948
2.20k
        static const blst_fr zero = {0};
949
950
2.20k
        switch ( op.calcOp.Get() ) {
951
22
            case    CF_CALCOP("Add(A,B)"):
952
22
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
953
19
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
954
17
                PREPARE_RESULT();
955
17
                blst_fr_add(RESULT_PTR(), &A, PARAM_B());
956
17
                ret = blst_detail::To_component_bignum(RESULT());
957
17
                break;
958
119
            case    CF_CALCOP("Sub(A,B)"):
959
119
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
960
103
                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
146
            case    CF_CALCOP("Mul(A,B)"):
966
146
                {
967
146
                    CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
968
969
95
                    size_t shiftCount;
970
95
                    uint8_t which = 0;
971
95
                    try {
972
95
                        which = ds.Get<uint8_t>() % 3;
973
95
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
974
28
                    }
975
95
                    if ( which == 1 ) {
976
16
                        if ( op.bn1.ToTrimmedString() != "3" ) {
977
12
                            which = 0;
978
12
                        }
979
79
                    } else if ( which == 2 ) {
980
48
                        shiftCount = blst_detail::isMultipleOf2(op.bn1.ToTrimmedString());
981
48
                        if ( shiftCount == 0 ) {
982
40
                            which = 0;
983
40
                        }
984
48
                    }
985
986
95
                    switch ( which ) {
987
83
                        case    0:
988
83
                            CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
989
53
                            PREPARE_RESULT();
990
53
                            CF_NORET(blst_fr_mul(RESULT_PTR(), &A, PARAM_B()));
991
53
                            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
95
                    }
1002
1003
65
                    ret = blst_detail::To_component_bignum(RESULT());
1004
65
                }
1005
0
                break;
1006
61
            case    CF_CALCOP("Sqr(A)"):
1007
61
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1008
24
                PREPARE_RESULT();
1009
24
                blst_fr_sqr(RESULT_PTR(), &A);
1010
24
                ret = blst_detail::To_component_bignum(RESULT());
1011
24
                break;
1012
18
            case    CF_CALCOP("InvMod(A,B)"):
1013
18
                {
1014
18
                    CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1015
1016
13
                    bool which = false;
1017
13
                    try {
1018
13
                        which = ds.Get<bool>();
1019
13
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1020
5
                    }
1021
1022
13
                    if ( which ) {
1023
4
                        PREPARE_RESULT();
1024
4
                        CF_NORET(blst_fr_eucl_inverse(RESULT_PTR(), &A));
1025
9
                    } else {
1026
9
                        PREPARE_RESULT();
1027
9
                        CF_NORET(blst_fr_inverse(RESULT_PTR(), &A));
1028
9
                    }
1029
1030
13
                    ret = blst_detail::To_component_bignum(RESULT());
1031
13
                }
1032
0
                break;
1033
14
            case    CF_CALCOP("LShift1(A)"):
1034
14
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1035
11
                PREPARE_RESULT();
1036
11
                blst_fr_lshift(RESULT_PTR(), &A, 1);
1037
11
                ret = blst_detail::To_component_bignum(RESULT());
1038
11
                break;
1039
178
            case    CF_CALCOP("RShift(A,B)"):
1040
178
                {
1041
178
                    CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1042
165
                    const auto B_cpp_int = blst_detail::To_cpp_int(op.bn1);
1043
165
                    size_t count = static_cast<size_t>(B_cpp_int);
1044
165
                    CF_CHECK_EQ(count, B_cpp_int);
1045
163
                    CF_CHECK_GT(count, 0);
1046
152
                    CF_CHECK_LT(count, 1024000);
1047
1048
84
                    PREPARE_RESULT();
1049
84
                    blst_fr_rshift(RESULT_PTR(), &A, count);
1050
1051
84
                    {
1052
84
                        CF_CHECK_EQ(count, 1);
1053
69
                        const auto A_cpp_int = blst_detail::To_cpp_int(op.bn0);
1054
69
                        CF_CHECK_LT(A_cpp_int, boost::multiprecision::cpp_int("52435875175126190479447740508185965837690552500527637822603658699938581184513"));
1055
27
                        CF_CHECK_EQ(A_cpp_int % 2, 1);
1056
9
                        ret = blst_detail::To_component_bignum(RESULT());
1057
9
                    }
1058
9
                }
1059
0
                break;
1060
14
            case    CF_CALCOP("Not(A)"):
1061
14
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1062
13
                PREPARE_RESULT();
1063
13
                blst_fr_cneg(RESULT_PTR(), &A, true);
1064
13
                ret = blst_detail::To_component_bignum(RESULT());
1065
13
                break;
1066
2
            case    CF_CALCOP("Set(A)"):
1067
2
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1068
1
                ret = blst_detail::To_component_bignum(A);
1069
1
                break;
1070
93
            case    CF_CALCOP("IsEq(A,B)"):
1071
93
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1072
89
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn1, B));
1073
87
                ret = memcmp(&A, &B, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1074
87
                break;
1075
6
            case    CF_CALCOP("IsZero(A)"):
1076
6
                CF_CHECK_TRUE(blst_detail::To_blst_fr(op.bn0, A));
1077
4
                ret = memcmp(&A, &zero, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1078
4
                break;
1079
2.20k
        }
1080
1081
2.20k
end:
1082
2.20k
        return ret;
1083
2.20k
    }
1084
1085
2.15k
    std::optional<component::Bignum> OpBignumCalc_prime(operation::BignumCalc& op) {
1086
2.15k
        std::optional<component::Bignum> ret = std::nullopt;
1087
2.15k
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1088
2.15k
        blst_fp result, A, B;
1089
2.15k
        uint8_t resultIdx;
1090
2.15k
        static const blst_fp zero = {0};
1091
1092
2.15k
        switch ( op.calcOp.Get() ) {
1093
12
            case    CF_CALCOP("Add(A,B)"):
1094
12
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1095
9
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1096
6
                PREPARE_RESULT();
1097
6
                blst_fp_add(RESULT_PTR(), &A, PARAM_B());
1098
6
                ret = blst_detail::To_component_bignum(RESULT());
1099
6
                break;
1100
45
            case    CF_CALCOP("Sub(A,B)"):
1101
45
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1102
33
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1103
29
                PREPARE_RESULT();
1104
29
                blst_fp_sub(RESULT_PTR(), &A, PARAM_B());
1105
29
                ret = blst_detail::To_component_bignum(RESULT());
1106
29
                break;
1107
266
            case    CF_CALCOP("Mul(A,B)"):
1108
266
                {
1109
266
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1110
1111
213
                    size_t shiftCount;
1112
213
                    uint8_t which = 0;
1113
213
                    try {
1114
213
                        which = ds.Get<uint8_t>() % 4;
1115
213
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1116
64
                    }
1117
1118
213
                    if ( which == 1 ) {
1119
12
                        if ( op.bn1.ToTrimmedString() != "3" ) {
1120
9
                            which = 0;
1121
9
                        }
1122
201
                    } else if ( which == 2 ) {
1123
17
                        if ( op.bn1.ToTrimmedString() != "8" ) {
1124
12
                            which = 0;
1125
12
                        }
1126
184
                    } else if ( which == 3 ) {
1127
120
                        shiftCount = blst_detail::isMultipleOf2(op.bn1.ToTrimmedString());
1128
120
                        if ( shiftCount == 0 ) {
1129
106
                            which = 0;
1130
106
                        }
1131
120
                    }
1132
1133
213
                    switch ( which ) {
1134
191
                        case    0:
1135
191
                            CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1136
150
                            PREPARE_RESULT();
1137
150
                            CF_NORET(blst_fp_mul(RESULT_PTR(), &A, PARAM_B()));
1138
150
                            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
5
                        case    2:
1144
5
                            PREPARE_RESULT();
1145
5
                            CF_NORET(blst_fp_mul_by_8(RESULT_PTR(), &A));
1146
5
                            break;
1147
14
                        case    3:
1148
14
                            size_t shiftCount;
1149
14
                            CF_CHECK_NE(shiftCount = blst_detail::isMultipleOf2(op.bn1.ToTrimmedString()), 0);
1150
1151
14
                            PREPARE_RESULT();
1152
14
                            blst_fp_lshift(RESULT_PTR(), &A, shiftCount);
1153
14
                            ret = blst_detail::To_component_bignum(RESULT());
1154
14
                            break;
1155
213
                    }
1156
1157
172
                    ret = blst_detail::To_component_bignum(RESULT());
1158
172
                }
1159
0
                break;
1160
10
            case    CF_CALCOP("LShift1(A)"):
1161
10
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1162
6
                PREPARE_RESULT();
1163
6
                blst_fp_lshift(RESULT_PTR(), &A, 1);
1164
6
                ret = blst_detail::To_component_bignum(RESULT());
1165
6
                break;
1166
73
            case    CF_CALCOP("Sqr(A)"):
1167
73
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1168
34
                PREPARE_RESULT();
1169
34
                blst_fp_sqr(RESULT_PTR(), &A);
1170
34
                ret = blst_detail::To_component_bignum(RESULT());
1171
34
                break;
1172
16
            case    CF_CALCOP("InvMod(A,B)"):
1173
16
                {
1174
16
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1175
1176
9
                    bool which = false;
1177
9
                    try {
1178
9
                        which = ds.Get<bool>();
1179
9
                    } catch ( fuzzing::datasource::Base::OutOfData ) {
1180
3
                    }
1181
1182
9
                    if ( which ) {
1183
4
                        PREPARE_RESULT();
1184
4
                        CF_NORET(blst_fp_eucl_inverse(RESULT_PTR(), &A));
1185
5
                    } else {
1186
5
                        PREPARE_RESULT();
1187
5
                        CF_NORET(blst_fp_inverse(RESULT_PTR(), &A));
1188
5
                    }
1189
1190
9
                    ret = blst_detail::To_component_bignum(RESULT());
1191
9
                }
1192
0
                break;
1193
198
            case    CF_CALCOP("Sqrt(A)"):
1194
198
                {
1195
198
                    blst_fp result2;
1196
198
                    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1197
1198
197
                    if ( blst_fp_sqrt(&result, &A) == true ) {
1199
121
                        CF_NORET(blst_fp_sqr(&result, &result));
1200
121
                        ret = blst_detail::To_component_bignum(result);
1201
121
                    } else {
1202
76
                        CF_NORET(blst_fp_cneg(&A, &A, 1));
1203
76
                        CF_ASSERT(blst_fp_sqrt(&A, &A) == true, "Square root must exist");
1204
1205
76
                        ret = std::string("0");
1206
76
                    }
1207
197
                }
1208
197
                break;
1209
197
            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
10
            case    CF_CALCOP("Not(A)"):
1230
10
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1231
8
                PREPARE_RESULT();
1232
8
                blst_fp_cneg(RESULT_PTR(), &A, true);
1233
8
                ret = blst_detail::To_component_bignum(RESULT());
1234
8
                break;
1235
3
            case    CF_CALCOP("Set(A)"):
1236
3
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1237
1
                ret = blst_detail::To_component_bignum(A);
1238
1
                break;
1239
53
            case    CF_CALCOP("IsEq(A,B)"):
1240
53
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn0, A));
1241
47
                CF_CHECK_TRUE(blst_detail::To_blst_fp(op.bn1, B));
1242
42
                ret = memcmp(&A, &B, sizeof(A)) == 0 ? std::string("1") : std::string("0");
1243
42
                break;
1244
9
            case    CF_CALCOP("IsZero(A)"):
1245
9
                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.15k
        }
1249
1250
2.15k
end:
1251
2.15k
        return ret;
1252
2.15k
    }
1253
1254
    #undef PREPARE_RESULT
1255
    #undef RESULT_PTR
1256
    #undef RESULT
1257
    #undef PARAM_B
1258
}
1259
1260
4.36k
std::optional<component::Bignum> blst::OpBignumCalc(operation::BignumCalc& op) {
1261
4.36k
    if ( op.modulo == std::nullopt ) {
1262
0
        return std::nullopt;
1263
0
    }
1264
1265
    /* TODO optimize this */
1266
4.36k
    if ( op.modulo->ToTrimmedString() == "52435875175126190479447740508185965837690552500527637822603658699938581184513" ) {
1267
2.20k
        return blst_detail::OpBignumCalc_order(op);
1268
2.20k
    } else if ( op.modulo->ToTrimmedString() == "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" ) {
1269
2.15k
        return blst_detail::OpBignumCalc_prime(op);
1270
2.15k
    } else {
1271
0
        return std::nullopt;
1272
0
    }
1273
4.36k
}
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
175
std::optional<component::G1> blst::OpBLS_Decompress_G1(operation::BLS_Decompress_G1& op) {
1477
175
    std::optional<component::G1> ret = std::nullopt;
1478
175
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1479
1480
175
    blst_p1_affine point;
1481
175
    std::optional<std::array<uint8_t, 48>> compressed = std::nullopt;
1482
1483
175
    CF_CHECK_NE(compressed = blst_detail::ToArray<48>(op.compressed, false), std::nullopt);
1484
1485
169
    if ( blst_p1_uncompress(&point, compressed->data()) == BLST_SUCCESS ) {
1486
11
        blst_detail::G1 g1(point, ds);
1487
11
        ret = g1.To_Component_G1();
1488
158
    } else {
1489
158
        ret = component::G1{"0", "0"};
1490
158
    }
1491
1492
175
end:
1493
175
    return ret;
1494
169
}
1495
1496
48
std::optional<component::Bignum> blst::OpBLS_Compress_G1(operation::BLS_Compress_G1& op) {
1497
48
    std::optional<component::Bignum> ret = std::nullopt;
1498
1499
48
    std::array<uint8_t, 48> out;
1500
48
    blst_p1_affine point;
1501
1502
48
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.first, point.x));
1503
44
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.uncompressed.second, point.y));
1504
    //CF_CHECK_FALSE(blst_detail::IsZero(&point));
1505
1506
42
    CF_CHECK_TRUE(blst_p1_affine_on_curve(&point));
1507
1508
32
    CF_NORET(blst_p1_affine_compress(out.data(), &point));
1509
32
    ret = util::BinToDec(out.data(), 48);
1510
1511
48
end:
1512
48
    return ret;
1513
32
}
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
108
    aff_a = *aff_tmp;
1576
1577
108
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.b), std::nullopt);
1578
95
    aff_b = *aff_tmp;
1579
1580
95
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1581
95
    a = *tmp;
1582
1583
95
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.b), std::nullopt);
1584
95
    b = *tmp;
1585
1586
95
    eq = blst_p1_is_equal(&a, &b);
1587
1588
95
    try {
1589
95
        mode = ds.Get<uint8_t>() % 3;
1590
95
    } catch ( fuzzing::datasource::Base::OutOfData ) {
1591
72
    }
1592
1593
    /* Modes:
1594
     *
1595
     * 0 = pure add
1596
     * 1 = pure double
1597
     * 2 = mixed add and double
1598
     */
1599
1600
95
    if ( mode == 0 && eq ) {
1601
29
        mode = 1;
1602
66
    } else if ( mode == 1 && !eq ) {
1603
4
        mode = 0;
1604
4
    }
1605
1606
95
    try {
1607
95
        doAffine = ds.Get<bool>();
1608
95
    } catch ( fuzzing::datasource::Base::OutOfData ) {
1609
80
    }
1610
1611
95
    switch ( mode ) {
1612
57
        case    0:
1613
57
            if ( doAffine == true ) {
1614
54
                bool doMulti = false;
1615
54
                try {
1616
54
                    doMulti = ds.Get<bool>();
1617
54
                } catch ( fuzzing::datasource::Base::OutOfData ) {
1618
51
                }
1619
1620
54
                if ( doMulti == false ) {
1621
52
                    CF_NORET(blst_p1_add_affine(&result, &a, &aff_b));
1622
52
                } 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
54
            } else {
1627
3
                CF_NORET(blst_p1_add(&result, &a, &b));
1628
3
            }
1629
57
            break;
1630
57
        case    1:
1631
32
            CF_NORET(blst_p1_double(&result, &a));
1632
32
            break;
1633
6
        case    2:
1634
6
            if ( doAffine == true ) {
1635
4
                CF_NORET(blst_p1_add_or_double_affine(&result, &a, &aff_b));
1636
4
            } else {
1637
2
                CF_NORET(blst_p1_add_or_double(&result, &a, &b));
1638
2
            }
1639
6
            break;
1640
0
        default:
1641
0
            CF_UNREACHABLE();
1642
95
    }
1643
1644
95
    CF_CHECK_TRUE(
1645
95
            !blst_p1_affine_is_inf(&aff_a) &&
1646
95
            blst_p1_affine_on_curve(&aff_a) &&
1647
95
            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
14
    CF_NORET(blst_p1_to_affine(&aff_result, &result));
1654
1655
14
    {
1656
14
        blst_detail::G1 g1(aff_result, ds);
1657
14
        ret = g1.To_Component_G1();
1658
14
    }
1659
1660
121
end:
1661
121
    return ret;
1662
14
}
1663
1664
99
std::optional<component::G1> blst::OpBLS_G1_Mul(operation::BLS_G1_Mul& op) {
1665
99
    std::optional<component::G1> ret = std::nullopt;
1666
99
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1667
1668
99
    blst_p1_affine aff_result;
1669
99
    std::optional<blst_p1> tmp;
1670
99
    blst_p1 a, result;
1671
99
    std::optional<std::vector<uint8_t>> b;
1672
1673
99
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1674
94
    a = *tmp;
1675
1676
94
    CF_CHECK_NE(b = util::DecToBin(op.b.ToTrimmedString()), std::nullopt);
1677
1678
94
    {
1679
94
        std::vector<uint8_t> b_reversed = util::AddLeadingZeroes(ds, *b);
1680
94
        CF_NORET(std::reverse(b_reversed.begin(), b_reversed.end()));
1681
1682
94
        Buffer B(b_reversed);
1683
1684
94
        CF_NORET(blst_p1_mult(&result, &a, B.GetPtr(&ds), B.GetSize() * 8));
1685
94
    }
1686
1687
94
    CF_NORET(blst_p1_to_affine(&aff_result, &result));
1688
1689
94
    CF_CHECK_TRUE(
1690
94
            !blst_p1_is_inf(&a) &&
1691
94
            blst_p1_on_curve(&a) &&
1692
94
            blst_p1_in_g1(&a));
1693
1694
11
    {
1695
11
        blst_detail::G1 g1(aff_result, ds);
1696
11
        ret = g1.To_Component_G1();
1697
11
    }
1698
1699
99
end:
1700
99
    return ret;
1701
11
}
1702
1703
95
std::optional<bool> blst::OpBLS_G1_IsEq(operation::BLS_G1_IsEq& op) {
1704
95
    std::optional<bool> ret = std::nullopt;
1705
95
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1706
1707
95
    std::optional<blst_p1_affine> aff_tmp;
1708
95
    blst_p1_affine aff_a, aff_b;
1709
1710
95
    std::optional<blst_p1> tmp;
1711
95
    blst_p1 a, b;
1712
1713
95
    bool doAffine = false;
1714
1715
95
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.a), std::nullopt);
1716
92
    aff_a = *aff_tmp;
1717
1718
92
    CF_CHECK_NE(aff_tmp = blst_detail::Load_G1_Affine(op.b), std::nullopt);
1719
73
    aff_b = *aff_tmp;
1720
1721
73
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1722
73
    a = *tmp;
1723
1724
73
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.b), std::nullopt);
1725
73
    b = *tmp;
1726
1727
73
    try {
1728
73
        doAffine = ds.Get<bool>();
1729
73
    } catch ( fuzzing::datasource::Base::OutOfData ) {
1730
46
    }
1731
1732
73
    if ( doAffine == true ) {
1733
14
        ret = blst_p1_affine_is_equal(&aff_a, &aff_b);
1734
59
    } else {
1735
59
        ret = blst_p1_is_equal(&a, &b);
1736
59
    }
1737
1738
95
end:
1739
95
    return ret;
1740
73
}
1741
1742
755
std::optional<component::G1> blst::OpBLS_G1_Neg(operation::BLS_G1_Neg& op) {
1743
755
    std::optional<component::G1> ret = std::nullopt;
1744
755
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1745
1746
755
    blst_p1_affine aff_result;
1747
755
    std::optional<blst_p1> tmp;
1748
755
    blst_p1 a;
1749
1750
755
    CF_CHECK_NE(tmp = blst_detail::Load_G1_Projective(ds, op.a), std::nullopt);
1751
742
    a = *tmp;
1752
1753
742
    CF_NORET(blst_p1_cneg(&a, true));
1754
1755
742
    CF_NORET(blst_p1_to_affine(&aff_result, &a));
1756
1757
742
    {
1758
742
        blst_detail::G1 g1(aff_result, ds);
1759
742
        ret = g1.To_Component_G1();
1760
742
    }
1761
1762
755
end:
1763
755
    return ret;
1764
742
}
1765
1766
95
std::optional<component::G2> blst::OpBLS_G2_Add(operation::BLS_G2_Add& op) {
1767
95
    std::optional<component::G2> ret = std::nullopt;
1768
95
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1769
1770
95
    blst_p2_affine a, b, result_;
1771
95
    blst_p2 a_, b_, result;
1772
95
    bool doDouble = false;
1773
1774
95
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1775
85
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1776
84
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1777
77
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1778
1779
75
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.first, b.x.fp[0]));
1780
66
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.second, b.y.fp[0]));
1781
61
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.first, b.x.fp[1]));
1782
55
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.second, b.y.fp[1]));
1783
1784
46
    CF_NORET(blst_p2_from_affine(&a_, &a));
1785
46
    CF_NORET(blst_p2_from_affine(&b_, &b));
1786
1787
46
    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
46
    if ( doDouble == false ) {
1795
43
        CF_NORET(blst_p2_add_or_double_affine(&result, &a_, &b));
1796
43
    } else {
1797
3
        CF_NORET(blst_p2_double(&result, &a_));
1798
3
    }
1799
1800
1801
46
    CF_NORET(blst_p2_to_affine(&result_, &result));
1802
1803
46
    ret = blst_detail::To_G2(result_);
1804
1805
95
end:
1806
95
    return ret;
1807
46
}
1808
1809
109
std::optional<component::G2> blst::OpBLS_G2_Mul(operation::BLS_G2_Mul& op) {
1810
109
    std::optional<component::G2> ret = std::nullopt;
1811
109
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1812
1813
109
    blst_p2_affine a, result_;
1814
109
    blst_p2 a_, result;
1815
109
    std::optional<std::vector<uint8_t>> b;
1816
109
    bool doDouble = false;
1817
1818
109
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1819
106
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1820
103
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1821
96
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1822
84
    CF_CHECK_NE(b = util::DecToBin(op.b.ToTrimmedString()), std::nullopt);
1823
1824
84
    if ( !(blst_p2_affine_on_curve(&a) && blst_p2_affine_in_g2(&a)) ) {
1825
14
        return ret;
1826
14
    }
1827
1828
70
    CF_NORET(blst_p2_from_affine(&a_, &a));
1829
1830
70
    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
70
    if ( doDouble == false ) {
1838
69
        std::vector<uint8_t> b_reversed = util::AddLeadingZeroes(ds, *b);
1839
69
        CF_NORET(std::reverse(b_reversed.begin(), b_reversed.end()));
1840
1841
69
        Buffer B(b_reversed);
1842
1843
69
        CF_NORET(blst_p2_mult(&result, &a_, B.GetPtr(&ds), B.GetSize() * 8));
1844
69
    } else {
1845
1
        CF_NORET(blst_p2_double(&result, &a_));
1846
1
    }
1847
1848
70
    CF_NORET(blst_p2_to_affine(&result_, &result));
1849
1850
70
    ret = blst_detail::To_G2(result_);
1851
1852
95
end:
1853
95
    return ret;
1854
70
}
1855
1856
79
std::optional<bool> blst::OpBLS_G2_IsEq(operation::BLS_G2_IsEq& op) {
1857
79
    std::optional<bool> ret = std::nullopt;
1858
1859
79
    blst_p2_affine a, b;
1860
79
    blst_p2 a_, b_;
1861
1862
79
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1863
75
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1864
65
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1865
60
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1866
1867
54
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.first, b.x.fp[0]));
1868
46
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.first.second, b.y.fp[0]));
1869
38
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.first, b.x.fp[1]));
1870
37
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.b.second.second, b.y.fp[1]));
1871
1872
36
    CF_NORET(blst_p2_from_affine(&a_, &a));
1873
36
    CF_NORET(blst_p2_from_affine(&b_, &b));
1874
1875
36
    ret = blst_p2_is_equal(&a_, &b_);
1876
1877
79
end:
1878
79
    return ret;
1879
36
}
1880
1881
61
std::optional<component::G2> blst::OpBLS_G2_Neg(operation::BLS_G2_Neg& op) {
1882
61
    std::optional<component::G2> ret = std::nullopt;
1883
61
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1884
1885
61
    blst_p2_affine a;
1886
61
    blst_p2 a_;
1887
1888
61
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.first, a.x.fp[0]));
1889
57
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.first.second, a.y.fp[0]));
1890
49
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.first, a.x.fp[1]));
1891
49
    CF_CHECK_TRUE(blst_detail::To_blst_fp(op.a.second.second, a.y.fp[1]));
1892
1893
48
    CF_NORET(blst_p2_from_affine(&a_, &a));
1894
1895
48
    CF_NORET(blst_p2_cneg(&a_, true));
1896
1897
48
    CF_NORET(blst_p2_to_affine(&a, &a_));
1898
1899
48
    ret = blst_detail::To_G2(a);
1900
1901
61
end:
1902
61
    return ret;
1903
48
}
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
4.38k
bool blst::SupportsModularBignumCalc(void) const {
2085
4.38k
    return true;
2086
4.38k
}
2087
2088
} /* namespace module */
2089
} /* namespace cryptofuzz */