Coverage Report

Created: 2026-08-14 07:11

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