Coverage Report

Created: 2023-12-08 07:00

/src/cryptofuzz/modules/arkworks-algebra/module.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "module.h"
2
#include <cryptofuzz/util.h>
3
#include <cryptofuzz/crypto.h>
4
5
extern "C" {
6
    int arkworks_algebra_bignumcalc(
7
            uint64_t op,
8
            uint64_t* bn0_bytes,
9
            uint64_t* bn1_bytes,
10
            uint64_t* bn2_bytes,
11
            uint64_t* result);
12
    int arkworks_algebra_bignumcalc_bn254_fq(
13
            uint64_t op,
14
            uint64_t* bn0_bytes,
15
            uint64_t* bn1_bytes,
16
            uint64_t* bn2_bytes,
17
            uint64_t* result);
18
    int arkworks_algebra_bignumcalc_bn254_fr(
19
            uint64_t op,
20
            uint64_t* bn0_bytes,
21
            uint64_t* bn1_bytes,
22
            uint64_t* bn2_bytes,
23
            uint64_t* result);
24
    int arkworks_algebra_bignumcalc_bls12_381_fr(
25
            uint64_t op,
26
            uint64_t* bn0_bytes,
27
            uint64_t* bn1_bytes,
28
            uint64_t* bn2_bytes,
29
            uint64_t* result);
30
    int arkworks_algebra_bignumcalc_bls12_381_fq(
31
            uint64_t op,
32
            uint64_t* bn0_bytes,
33
            uint64_t* bn1_bytes,
34
            uint64_t* bn2_bytes,
35
            uint64_t* result);
36
    int arkworks_algebra_g1_isoncurve_bn254(
37
            uint64_t* ax_bytes,
38
            uint64_t* ay_bytes);
39
    int arkworks_algebra_g1_privatetopublic_bn254(
40
            uint64_t* priv_bytes,
41
            uint64_t* result_x,
42
            uint64_t* result_y);
43
    int arkworks_algebra_g1_add_bn254(
44
            uint64_t* ax_bytes,
45
            uint64_t* ay_bytes,
46
            uint64_t* bx_bytes,
47
            uint64_t* by_bytes,
48
            uint64_t* result_x,
49
            uint64_t* result_y);
50
    int arkworks_algebra_g1_mul_bn254(
51
            uint64_t* ax_bytes,
52
            uint64_t* ay_bytes,
53
            uint64_t* b_bytes,
54
            uint64_t* result_x,
55
            uint64_t* result_y);
56
    int arkworks_algebra_g1_neg_bn254(
57
            uint64_t* ax_bytes,
58
            uint64_t* ay_bytes,
59
            uint64_t* result_x,
60
            uint64_t* result_y);
61
    int arkworks_algebra_batchverify_bn254(
62
            uint64_t* in_data,
63
            uint64_t num_elements);
64
    int arkworks_algebra_g1_isoncurve_bls12_381(
65
            uint64_t* ax_bytes,
66
            uint64_t* ay_bytes);
67
    int arkworks_algebra_g1_privatetopublic_bls12_381(
68
            uint64_t* priv_bytes,
69
            uint64_t* result_x,
70
            uint64_t* result_y);
71
    int arkworks_algebra_g1_add_bls12_381(
72
            uint64_t* ax_bytes,
73
            uint64_t* ay_bytes,
74
            uint64_t* bx_bytes,
75
            uint64_t* by_bytes,
76
            int affine,
77
            uint64_t* result_x,
78
            uint64_t* result_y);
79
    int arkworks_algebra_g1_mul_bls12_381(
80
            uint64_t* ax_bytes,
81
            uint64_t* ay_bytes,
82
            uint64_t* b_bytes,
83
            int affine,
84
            uint64_t* result_x,
85
            uint64_t* result_y);
86
    int arkworks_algebra_g1_neg_bls12_381(
87
            uint64_t* ax_bytes,
88
            uint64_t* ay_bytes,
89
            int affine,
90
            uint64_t* result_x,
91
            uint64_t* result_y);
92
    int arkworks_algebra_g2_isoncurve_bls12_381(
93
            uint64_t* av_bytes,
94
            uint64_t* aw_bytes,
95
            uint64_t* ax_bytes,
96
            uint64_t* ay_bytes);
97
    int arkworks_algebra_g2_add_bls12_381(
98
            uint64_t* av_bytes,
99
            uint64_t* aw_bytes,
100
            uint64_t* ax_bytes,
101
            uint64_t* ay_bytes,
102
            uint64_t* bv_bytes,
103
            uint64_t* bw_bytes,
104
            uint64_t* bx_bytes,
105
            uint64_t* by_bytes,
106
            int affine,
107
            uint64_t* result_v,
108
            uint64_t* result_w,
109
            uint64_t* result_x,
110
            uint64_t* result_y);
111
    int arkworks_algebra_g2_mul_bls12_381(
112
            uint64_t* av_bytes,
113
            uint64_t* aw_bytes,
114
            uint64_t* ax_bytes,
115
            uint64_t* ay_bytes,
116
            uint64_t* b_bytes,
117
            int affine,
118
            uint64_t* result_v,
119
            uint64_t* result_w,
120
            uint64_t* result_x,
121
            uint64_t* result_y);
122
    int arkworks_algebra_g2_neg_bls12_381(
123
            uint64_t* av_bytes,
124
            uint64_t* aw_bytes,
125
            uint64_t* ax_bytes,
126
            uint64_t* ay_bytes,
127
            int affine,
128
            uint64_t* result_v,
129
            uint64_t* result_w,
130
            uint64_t* result_x,
131
            uint64_t* result_y);
132
    int arkworks_algebra_g1_multiexp_bls12_381(
133
            uint64_t* x,
134
            uint64_t* y,
135
            uint64_t* scalars,
136
            uint64_t num,
137
            uint64_t* result_x,
138
            uint64_t* result_y);
139
    int arkworks_algebra_g1_isoncurve_bls12_377(
140
            uint64_t* ax_bytes,
141
            uint64_t* ay_bytes);
142
    int arkworks_algebra_g1_privatetopublic_bls12_377(
143
            uint64_t* priv_bytes,
144
            uint64_t* result_x,
145
            uint64_t* result_y);
146
    int arkworks_algebra_g1_add_bls12_377(
147
            uint64_t* ax_bytes,
148
            uint64_t* ay_bytes,
149
            uint64_t* bx_bytes,
150
            uint64_t* by_bytes,
151
            uint64_t* result_x,
152
            uint64_t* result_y);
153
    int arkworks_algebra_g1_mul_bls12_377(
154
            uint64_t* ax_bytes,
155
            uint64_t* ay_bytes,
156
            uint64_t* b_bytes,
157
            uint64_t* result_x,
158
            uint64_t* result_y);
159
    int arkworks_algebra_g1_neg_bls12_377(
160
            uint64_t* ax_bytes,
161
            uint64_t* ay_bytes,
162
            uint64_t* result_x,
163
            uint64_t* result_y);
164
    int arkworks_algebra_g2_mul_bls12_377(
165
            uint64_t* av_bytes,
166
            uint64_t* aw_bytes,
167
            uint64_t* ax_bytes,
168
            uint64_t* ay_bytes,
169
            uint64_t* b_bytes,
170
            int affine,
171
            uint64_t* result_v,
172
            uint64_t* result_w,
173
            uint64_t* result_x,
174
            uint64_t* result_y);
175
    int arkworks_algebra_bignumcalc_bls12_377_fq(
176
            uint64_t op,
177
            uint64_t* bn0_bytes,
178
            uint64_t* bn1_bytes,
179
            uint64_t* bn2_bytes,
180
            uint64_t* result);
181
    int arkworks_algebra_bignumcalc_bls12_377_fr(
182
            uint64_t op,
183
            uint64_t* bn0_bytes,
184
            uint64_t* bn1_bytes,
185
            uint64_t* bn2_bytes,
186
            uint64_t* result);
187
}
188
189
namespace cryptofuzz {
190
namespace module {
191
192
arkworks_algebra::arkworks_algebra(void) :
193
4
    Module("arkworks-algebra") { }
194
195
namespace arkworks_algebra_detail {
196
89.6k
    std::optional<std::array<uint64_t, 4>> To4U64(const component::Bignum& bn) {
197
89.6k
        std::optional<std::array<uint64_t, 4>> ret = std::nullopt;
198
199
89.6k
        const auto bin = util::DecToBin(bn.ToTrimmedString(), 32);
200
89.6k
        CF_CHECK_NE(bin, std::nullopt);
201
202
89.1k
        std::array<uint64_t, 4> arr;
203
89.1k
        memcpy(&arr[0], bin->data(), sizeof(uint64_t));
204
89.1k
        arr[0] = __bswap_64(arr[0]);
205
89.1k
        memcpy(&arr[1], bin->data() + 8, sizeof(uint64_t));
206
89.1k
        arr[1] = __bswap_64(arr[1]);
207
89.1k
        memcpy(&arr[2], bin->data() + 16, sizeof(uint64_t));
208
89.1k
        arr[2] = __bswap_64(arr[2]);
209
89.1k
        memcpy(&arr[3], bin->data() + 24, sizeof(uint64_t));
210
89.1k
        arr[3] = __bswap_64(arr[3]);
211
212
89.1k
        std::reverse(arr.begin(), arr.end());
213
89.1k
        ret = arr;
214
89.6k
end:
215
89.6k
        return ret;
216
89.1k
    }
217
81.9k
    std::optional<std::array<uint64_t, 6>> To6U64(const component::Bignum& bn) {
218
81.9k
        std::optional<std::array<uint64_t, 6>> ret = std::nullopt;
219
220
81.9k
        const auto bin = util::DecToBin(bn.ToTrimmedString(), 48);
221
81.9k
        CF_CHECK_NE(bin, std::nullopt);
222
223
81.2k
        std::array<uint64_t, 6> arr;
224
81.2k
        memcpy(&arr[0], bin->data(), sizeof(uint64_t));
225
81.2k
        arr[0] = __bswap_64(arr[0]);
226
81.2k
        memcpy(&arr[1], bin->data() + 8, sizeof(uint64_t));
227
81.2k
        arr[1] = __bswap_64(arr[1]);
228
81.2k
        memcpy(&arr[2], bin->data() + 16, sizeof(uint64_t));
229
81.2k
        arr[2] = __bswap_64(arr[2]);
230
81.2k
        memcpy(&arr[3], bin->data() + 24, sizeof(uint64_t));
231
81.2k
        arr[3] = __bswap_64(arr[3]);
232
81.2k
        memcpy(&arr[4], bin->data() + 32, sizeof(uint64_t));
233
81.2k
        arr[4] = __bswap_64(arr[4]);
234
81.2k
        memcpy(&arr[5], bin->data() + 40, sizeof(uint64_t));
235
81.2k
        arr[5] = __bswap_64(arr[5]);
236
237
81.2k
        std::reverse(arr.begin(), arr.end());
238
81.2k
        ret = arr;
239
81.9k
end:
240
81.9k
        return ret;
241
81.2k
    }
242
1.40k
    const component::Bignum From4U64(std::array<uint64_t, 4> u64) {
243
1.40k
        std::reverse(u64.begin(), u64.end());
244
1.40k
        std::vector<uint8_t> bin(32);
245
1.40k
        u64[0] = __bswap_64(u64[0]);
246
1.40k
        u64[1] = __bswap_64(u64[1]);
247
1.40k
        u64[2] = __bswap_64(u64[2]);
248
1.40k
        u64[3] = __bswap_64(u64[3]);
249
1.40k
        memcpy(bin.data(), &u64[0], sizeof(uint64_t));
250
1.40k
        memcpy(bin.data() + 8, &u64[1], sizeof(uint64_t));
251
1.40k
        memcpy(bin.data() + 16, &u64[2], sizeof(uint64_t));
252
1.40k
        memcpy(bin.data() + 24, &u64[3], sizeof(uint64_t));
253
1.40k
        return component::Bignum{util::BinToDec(bin)};
254
1.40k
    }
255
21.2k
    const component::Bignum From6U64(std::array<uint64_t, 6> u64) {
256
21.2k
        std::reverse(u64.begin(), u64.end());
257
21.2k
        std::vector<uint8_t> bin(48);
258
21.2k
        u64[0] = __bswap_64(u64[0]);
259
21.2k
        u64[1] = __bswap_64(u64[1]);
260
21.2k
        u64[2] = __bswap_64(u64[2]);
261
21.2k
        u64[3] = __bswap_64(u64[3]);
262
21.2k
        u64[4] = __bswap_64(u64[4]);
263
21.2k
        u64[5] = __bswap_64(u64[5]);
264
21.2k
        memcpy(bin.data(), &u64[0], sizeof(uint64_t));
265
21.2k
        memcpy(bin.data() + 8, &u64[1], sizeof(uint64_t));
266
21.2k
        memcpy(bin.data() + 16, &u64[2], sizeof(uint64_t));
267
21.2k
        memcpy(bin.data() + 24, &u64[3], sizeof(uint64_t));
268
21.2k
        memcpy(bin.data() + 32, &u64[4], sizeof(uint64_t));
269
21.2k
        memcpy(bin.data() + 40, &u64[5], sizeof(uint64_t));
270
21.2k
        return component::Bignum{util::BinToDec(bin)};
271
21.2k
    }
272
273
    component::G1 ToG1(
274
            const std::array<uint64_t, 4>& result_x,
275
0
            const std::array<uint64_t, 4>& result_y) {
276
0
        const auto x = arkworks_algebra_detail::From4U64(result_x).ToTrimmedString();
277
0
        auto y = arkworks_algebra_detail::From4U64(result_y).ToTrimmedString();
278
0
        if (x == "0" && y == "1") {
279
0
            y = "0";
280
0
        }
281
282
0
        return component::G1{x, y};
283
0
    }
284
    component::G1 ToG1(
285
            const std::array<uint64_t, 6>& result_x,
286
2.89k
            const std::array<uint64_t, 6>& result_y) {
287
2.89k
        const auto x = arkworks_algebra_detail::From6U64(result_x).ToTrimmedString();
288
2.89k
        auto y = arkworks_algebra_detail::From6U64(result_y).ToTrimmedString();
289
2.89k
        if (x == "0" && y == "1") {
290
51
            y = "0";
291
51
        }
292
293
2.89k
        return component::G1{x, y};
294
2.89k
    }
295
    component::G2 ToG2(
296
            const std::array<uint64_t, 6>& result_v,
297
            const std::array<uint64_t, 6>& result_w,
298
            const std::array<uint64_t, 6>& result_x,
299
3.59k
            const std::array<uint64_t, 6>& result_y) {
300
3.59k
        const auto v = arkworks_algebra_detail::From6U64(result_v).ToTrimmedString();
301
3.59k
        auto w = arkworks_algebra_detail::From6U64(result_w).ToTrimmedString();
302
3.59k
        const auto x = arkworks_algebra_detail::From6U64(result_x).ToTrimmedString();
303
3.59k
        const auto y = arkworks_algebra_detail::From6U64(result_y).ToTrimmedString();
304
305
3.59k
        if (v == "0" && w == "1" && x == "0" && y == "0") {
306
889
            w = "0";
307
889
        }
308
309
3.59k
        return component::G2{v, w, x, y};
310
3.59k
    }
311
}
312
313
2.87k
std::optional<component::BLS_PublicKey> arkworks_algebra::OpBLS_PrivateToPublic(operation::BLS_PrivateToPublic& op) {
314
2.87k
    if (
315
2.87k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
316
2.87k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
317
2.87k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
318
0
        return std::nullopt;
319
0
    }
320
321
2.87k
    std::optional<component::BLS_PublicKey> ret = std::nullopt;
322
323
2.87k
    if ( op.curveType.Get() == CF_ECC_CURVE("alt_bn128") ) {
324
0
        std::optional<std::array<uint64_t, 4>> priv;
325
0
        std::array<uint64_t, 4> result_x, result_y;
326
327
0
        CF_CHECK_NE(priv = arkworks_algebra_detail::To4U64(op.priv), std::nullopt);
328
329
0
        CF_CHECK_NE(arkworks_algebra_g1_privatetopublic_bn254(
330
0
                    priv->data(),
331
0
                    result_x.data(),
332
0
                    result_y.data()
333
0
                    ), -1);
334
335
0
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
336
2.87k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
337
2.87k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
338
2.87k
        std::optional<std::array<uint64_t, 4>> priv;
339
2.87k
        std::array<uint64_t, 6> result_x, result_y;
340
341
2.87k
        CF_CHECK_NE(priv = arkworks_algebra_detail::To4U64(op.priv), std::nullopt);
342
343
2.85k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
344
2.85k
            CF_CHECK_NE(arkworks_algebra_g1_privatetopublic_bls12_381(
345
2.85k
                        priv->data(),
346
2.85k
                        result_x.data(),
347
2.85k
                        result_y.data()
348
2.85k
                        ), -1);
349
2.75k
        } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
350
0
            CF_CHECK_NE(arkworks_algebra_g1_privatetopublic_bls12_377(
351
0
                        priv->data(),
352
0
                        result_x.data(),
353
0
                        result_y.data()
354
0
                        ), -1);
355
0
        } else {
356
0
            CF_UNREACHABLE();
357
0
        }
358
359
2.75k
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
360
2.75k
    } else {
361
0
        CF_UNREACHABLE();
362
0
    }
363
364
2.87k
end:
365
2.87k
    return ret;
366
2.87k
}
367
368
1.23k
std::optional<bool> arkworks_algebra::OpBLS_IsG1OnCurve(operation::BLS_IsG1OnCurve& op) {
369
1.23k
    if (
370
1.23k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
371
1.23k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
372
1.23k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
373
0
        return std::nullopt;
374
0
    }
375
376
1.23k
    std::optional<bool> ret = std::nullopt;
377
378
1.23k
    if ( op.curveType.Get() == CF_ECC_CURVE("alt_bn128") ) {
379
0
        std::optional<std::array<uint64_t, 4>> ax, ay;
380
381
0
        CF_CHECK_NE(ax = arkworks_algebra_detail::To4U64(op.g1.first), std::nullopt);
382
0
        CF_CHECK_NE(ay = arkworks_algebra_detail::To4U64(op.g1.second), std::nullopt);
383
384
0
        const auto r = arkworks_algebra_g1_isoncurve_bn254(
385
0
                    ax->data(),
386
0
                    ay->data());
387
0
        CF_CHECK_NE(r, -1);
388
0
        ret = r == 1;
389
1.23k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
390
1.23k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
391
1.23k
        std::optional<std::array<uint64_t, 6>> ax, ay;
392
393
1.23k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.g1.first), std::nullopt);
394
1.21k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.g1.second), std::nullopt);
395
396
1.19k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
397
1.19k
            const auto r = arkworks_algebra_g1_isoncurve_bls12_381(
398
1.19k
                        ax->data(),
399
1.19k
                        ay->data());
400
1.19k
            CF_CHECK_NE(r, -1);
401
993
            ret = r == 1;
402
993
        } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
403
0
            const auto r = arkworks_algebra_g1_isoncurve_bls12_377(
404
0
                        ax->data(),
405
0
                        ay->data());
406
0
            CF_CHECK_NE(r, -1);
407
0
            ret = r == 1;
408
0
        } else {
409
0
            CF_UNREACHABLE();
410
0
        }
411
1.19k
    } else {
412
0
        CF_UNREACHABLE();
413
0
    }
414
415
1.23k
end:
416
1.23k
    return ret;
417
1.23k
}
418
419
2.33k
std::optional<component::G1> arkworks_algebra::OpBLS_G1_Add(operation::BLS_G1_Add& op) {
420
2.33k
    if (
421
2.33k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
422
2.33k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
423
2.33k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
424
0
        return std::nullopt;
425
0
    }
426
427
2.33k
    std::optional<component::G1> ret = std::nullopt;
428
2.33k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
429
430
2.33k
    if ( op.curveType.Get() == CF_ECC_CURVE("alt_bn128") ) {
431
0
        std::optional<std::array<uint64_t, 4>> ax, ay, bx, by;
432
0
        std::array<uint64_t, 4> result_x, result_y;
433
434
0
        CF_CHECK_NE(ax = arkworks_algebra_detail::To4U64(op.a.first), std::nullopt);
435
0
        CF_CHECK_NE(ay = arkworks_algebra_detail::To4U64(op.a.second), std::nullopt);
436
0
        CF_CHECK_NE(bx = arkworks_algebra_detail::To4U64(op.b.first), std::nullopt);
437
0
        CF_CHECK_NE(by = arkworks_algebra_detail::To4U64(op.b.second), std::nullopt);
438
439
0
        CF_CHECK_NE(arkworks_algebra_g1_add_bn254(
440
0
                    ax->data(),
441
0
                    ay->data(),
442
0
                    bx->data(),
443
0
                    by->data(),
444
0
                    result_x.data(),
445
0
                    result_y.data()
446
0
                    ), -1);
447
448
0
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
449
2.33k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
450
2.33k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
451
2.33k
        std::optional<std::array<uint64_t, 6>> ax, ay, bx, by;
452
2.33k
        std::array<uint64_t, 6> result_x, result_y;
453
454
2.33k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.first), std::nullopt);
455
2.32k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second), std::nullopt);
456
2.30k
        CF_CHECK_NE(bx = arkworks_algebra_detail::To6U64(op.b.first), std::nullopt);
457
2.29k
        CF_CHECK_NE(by = arkworks_algebra_detail::To6U64(op.b.second), std::nullopt);
458
459
2.27k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
460
2.27k
            bool affine = true;
461
2.27k
            try {
462
2.27k
                affine = ds.Get<bool>();
463
2.27k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
464
784
            }
465
466
2.27k
            CF_CHECK_NE(arkworks_algebra_g1_add_bls12_381(
467
2.27k
                        ax->data(),
468
2.27k
                        ay->data(),
469
2.27k
                        bx->data(),
470
2.27k
                        by->data(),
471
2.27k
                        affine,
472
2.27k
                        result_x.data(),
473
2.27k
                        result_y.data()
474
2.27k
                        ), -1);
475
22
        } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
476
0
            CF_CHECK_NE(arkworks_algebra_g1_add_bls12_377(
477
0
                        ax->data(),
478
0
                        ay->data(),
479
0
                        bx->data(),
480
0
                        by->data(),
481
0
                        result_x.data(),
482
0
                        result_y.data()
483
0
                        ), -1);
484
0
        } else {
485
0
            CF_UNREACHABLE();
486
0
        }
487
488
22
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
489
22
    } else {
490
0
        CF_UNREACHABLE();
491
0
    }
492
493
2.33k
end:
494
2.33k
    return ret;
495
2.33k
}
496
497
1.56k
std::optional<component::G1> arkworks_algebra::OpBLS_G1_Mul(operation::BLS_G1_Mul& op) {
498
1.56k
    if (
499
1.56k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
500
1.56k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
501
1.56k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
502
0
        return std::nullopt;
503
0
    }
504
505
1.56k
    std::optional<component::G1> ret = std::nullopt;
506
1.56k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
507
508
1.56k
    if ( op.curveType.Get() == CF_ECC_CURVE("alt_bn128") ) {
509
0
        std::optional<std::array<uint64_t, 4>> ax, ay, b;
510
0
        std::array<uint64_t, 4> result_x, result_y;
511
512
0
        CF_CHECK_NE(ax = arkworks_algebra_detail::To4U64(op.a.first), std::nullopt);
513
0
        CF_CHECK_NE(ay = arkworks_algebra_detail::To4U64(op.a.second), std::nullopt);
514
0
        CF_CHECK_NE(b = arkworks_algebra_detail::To4U64(op.b), std::nullopt);
515
516
0
        CF_CHECK_NE(arkworks_algebra_g1_mul_bn254(
517
0
                    ax->data(),
518
0
                    ay->data(),
519
0
                    b->data(),
520
0
                    result_x.data(),
521
0
                    result_y.data()
522
0
                    ), -1);
523
524
0
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
525
1.56k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
526
1.56k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
527
1.56k
        std::optional<std::array<uint64_t, 6>> ax, ay;
528
1.56k
        std::optional<std::array<uint64_t, 4>> b;
529
1.56k
        std::array<uint64_t, 6> result_x, result_y;
530
531
1.56k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.first), std::nullopt);
532
1.55k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second), std::nullopt);
533
1.54k
        CF_CHECK_NE(b = arkworks_algebra_detail::To4U64(op.b), std::nullopt);
534
535
1.50k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
536
1.50k
            bool affine = true;
537
1.50k
            try {
538
1.50k
                affine = ds.Get<bool>();
539
1.50k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
540
762
            }
541
542
1.50k
            CF_CHECK_NE(arkworks_algebra_g1_mul_bls12_381(
543
1.50k
                        ax->data(),
544
1.50k
                        ay->data(),
545
1.50k
                        b->data(),
546
1.50k
                        affine,
547
1.50k
                        result_x.data(),
548
1.50k
                        result_y.data()
549
1.50k
                        ), -1);
550
76
        } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
551
0
            CF_CHECK_NE(arkworks_algebra_g1_mul_bls12_377(
552
0
                        ax->data(),
553
0
                        ay->data(),
554
0
                        b->data(),
555
0
                        result_x.data(),
556
0
                        result_y.data()
557
0
                        ), -1);
558
0
        } else {
559
0
            CF_UNREACHABLE();
560
0
        }
561
562
76
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
563
76
    } else {
564
0
        CF_UNREACHABLE();
565
0
    }
566
567
1.56k
end:
568
1.56k
    return ret;
569
1.56k
}
570
571
2.24k
std::optional<component::G1> arkworks_algebra::OpBLS_G1_Neg(operation::BLS_G1_Neg& op) {
572
2.24k
    if (
573
2.24k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
574
2.24k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
575
2.24k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
576
0
        return std::nullopt;
577
0
    }
578
579
2.24k
    std::optional<component::G1> ret = std::nullopt;
580
2.24k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
581
582
2.24k
    if ( op.curveType.Get() == CF_ECC_CURVE("alt_bn128") ) {
583
0
        std::optional<std::array<uint64_t, 4>> ax, ay;
584
0
        std::array<uint64_t, 4> result_x, result_y;
585
586
0
        CF_CHECK_NE(ax = arkworks_algebra_detail::To4U64(op.a.first), std::nullopt);
587
0
        CF_CHECK_NE(ay = arkworks_algebra_detail::To4U64(op.a.second), std::nullopt);
588
589
0
        CF_CHECK_NE(arkworks_algebra_g1_neg_bn254(
590
0
                    ax->data(),
591
0
                    ay->data(),
592
0
                    result_x.data(),
593
0
                    result_y.data()
594
0
                    ), -1);
595
596
0
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
597
2.24k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
598
2.24k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
599
2.24k
        std::optional<std::array<uint64_t, 6>> ax, ay;
600
2.24k
        std::array<uint64_t, 6> result_x, result_y;
601
602
2.24k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.first), std::nullopt);
603
2.22k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second), std::nullopt);
604
605
2.18k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
606
2.18k
            bool affine = true;
607
2.18k
            try {
608
2.18k
                affine = ds.Get<bool>();
609
2.18k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
610
802
            }
611
612
2.18k
            CF_CHECK_NE(arkworks_algebra_g1_neg_bls12_381(
613
2.18k
                        ax->data(),
614
2.18k
                        ay->data(),
615
2.18k
                        affine,
616
2.18k
                        result_x.data(),
617
2.18k
                        result_y.data()
618
2.18k
                        ), -1);
619
42
        } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
620
0
            CF_CHECK_NE(arkworks_algebra_g1_neg_bls12_377(
621
0
                        ax->data(),
622
0
                        ay->data(),
623
0
                        result_x.data(),
624
0
                        result_y.data()
625
0
                        ), -1);
626
0
        } else {
627
0
            CF_UNREACHABLE();
628
0
        }
629
630
42
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
631
42
    } else {
632
0
        CF_UNREACHABLE();
633
0
    }
634
635
2.24k
end:
636
2.24k
    return ret;
637
2.24k
}
638
639
1.88k
std::optional<bool> arkworks_algebra::OpBLS_IsG2OnCurve(operation::BLS_IsG2OnCurve& op) {
640
1.88k
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
641
173
        return std::nullopt;
642
173
    }
643
644
1.71k
    std::optional<bool> ret = std::nullopt;
645
646
1.71k
    {
647
1.71k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
648
649
1.71k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.g2.first.first), std::nullopt);
650
1.70k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.g2.first.second), std::nullopt);
651
1.69k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.g2.second.first), std::nullopt);
652
1.68k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.g2.second.second), std::nullopt);
653
654
1.68k
        const auto r = arkworks_algebra_g2_isoncurve_bls12_381(
655
1.68k
                av->data(),
656
1.68k
                aw->data(),
657
1.68k
                ax->data(),
658
1.68k
                ay->data());
659
1.68k
        CF_CHECK_NE(r, -1);
660
1.40k
        ret = r == 1;
661
1.40k
    }
662
663
1.71k
end:
664
1.71k
    return ret;
665
1.40k
}
666
667
2.32k
std::optional<component::G2> arkworks_algebra::OpBLS_G2_Add(operation::BLS_G2_Add& op) {
668
2.32k
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
669
0
        return std::nullopt;
670
0
    }
671
672
2.32k
    std::optional<component::G2> ret = std::nullopt;
673
2.32k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
674
675
2.32k
    {
676
2.32k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
677
2.32k
        std::optional<std::array<uint64_t, 6>> bv, bw, bx, by;
678
2.32k
        std::array<uint64_t, 6> result_v, result_w, result_x, result_y;
679
680
2.32k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.a.first.first), std::nullopt);
681
2.30k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.a.first.second), std::nullopt);
682
2.29k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.second.first), std::nullopt);
683
2.28k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second.second), std::nullopt);
684
685
2.27k
        CF_CHECK_NE(bv = arkworks_algebra_detail::To6U64(op.b.first.first), std::nullopt);
686
2.26k
        CF_CHECK_NE(bw = arkworks_algebra_detail::To6U64(op.b.first.second), std::nullopt);
687
2.25k
        CF_CHECK_NE(bx = arkworks_algebra_detail::To6U64(op.b.second.first), std::nullopt);
688
2.24k
        CF_CHECK_NE(by = arkworks_algebra_detail::To6U64(op.b.second.second), std::nullopt);
689
690
2.23k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
691
2.23k
            bool affine = true;
692
2.23k
            try {
693
2.23k
                affine = ds.Get<bool>();
694
2.23k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
695
946
            }
696
697
2.23k
            CF_CHECK_NE(arkworks_algebra_g2_add_bls12_381(
698
2.23k
                        av->data(),
699
2.23k
                        aw->data(),
700
2.23k
                        ax->data(),
701
2.23k
                        ay->data(),
702
2.23k
                        bv->data(),
703
2.23k
                        bw->data(),
704
2.23k
                        bx->data(),
705
2.23k
                        by->data(),
706
2.23k
                        affine,
707
2.23k
                        result_v.data(),
708
2.23k
                        result_w.data(),
709
2.23k
                        result_x.data(),
710
2.23k
                        result_y.data()
711
2.23k
                        ), -1);
712
9
        ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
713
9
        }
714
2.23k
    }
715
2.32k
end:
716
2.32k
    return ret;
717
2.23k
}
718
719
3.85k
std::optional<component::G2> arkworks_algebra::OpBLS_G2_Mul(operation::BLS_G2_Mul& op) {
720
3.85k
    if (
721
3.85k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
722
3.85k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
723
0
        return std::nullopt;
724
0
    }
725
726
3.85k
    std::optional<component::G2> ret = std::nullopt;
727
3.85k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
728
729
3.85k
    {
730
3.85k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
731
3.85k
        std::optional<std::array<uint64_t, 4>> b;
732
3.85k
        std::array<uint64_t, 6> result_v, result_w, result_x, result_y;
733
734
3.85k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.a.first.first), std::nullopt);
735
3.84k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.a.first.second), std::nullopt);
736
3.82k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.second.first), std::nullopt);
737
3.81k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second.second), std::nullopt);
738
3.79k
        CF_CHECK_NE(b = arkworks_algebra_detail::To4U64(op.b), std::nullopt);
739
740
3.76k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
741
3.76k
            bool affine = true;
742
3.76k
            try {
743
3.76k
                affine = ds.Get<bool>();
744
3.76k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
745
2.87k
            }
746
747
3.76k
            CF_CHECK_NE(arkworks_algebra_g2_mul_bls12_381(
748
3.76k
                        av->data(),
749
3.76k
                        aw->data(),
750
3.76k
                        ax->data(),
751
3.76k
                        ay->data(),
752
3.76k
                        b->data(),
753
3.76k
                        affine,
754
3.76k
                        result_v.data(),
755
3.76k
                        result_w.data(),
756
3.76k
                        result_x.data(),
757
3.76k
                        result_y.data()
758
3.76k
                        ), -1);
759
3.56k
            ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
760
3.56k
        } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
761
0
            bool affine = true;
762
0
            try {
763
0
                affine = ds.Get<bool>();
764
0
            } catch ( fuzzing::datasource::Base::OutOfData ) {
765
0
            }
766
767
0
            CF_CHECK_NE(arkworks_algebra_g2_mul_bls12_377(
768
0
                        av->data(),
769
0
                        aw->data(),
770
0
                        ax->data(),
771
0
                        ay->data(),
772
0
                        b->data(),
773
0
                        affine,
774
0
                        result_v.data(),
775
0
                        result_w.data(),
776
0
                        result_x.data(),
777
0
                        result_y.data()
778
0
                        ), -1);
779
0
            ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
780
0
        }
781
3.76k
    }
782
3.85k
end:
783
3.85k
    return ret;
784
3.76k
}
785
786
2.02k
std::optional<component::G2> arkworks_algebra::OpBLS_G2_Neg(operation::BLS_G2_Neg& op) {
787
2.02k
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
788
0
        return std::nullopt;
789
0
    }
790
791
2.02k
    std::optional<component::G2> ret = std::nullopt;
792
2.02k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
793
794
2.02k
    {
795
2.02k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
796
2.02k
        std::array<uint64_t, 6> result_v, result_w, result_x, result_y;
797
798
2.02k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.a.first.first), std::nullopt);
799
2.01k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.a.first.second), std::nullopt);
800
2.00k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.second.first), std::nullopt);
801
1.99k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second.second), std::nullopt);
802
803
1.98k
        bool affine = true;
804
1.98k
        try {
805
1.98k
            affine = ds.Get<bool>();
806
1.98k
        } catch ( fuzzing::datasource::Base::OutOfData ) {
807
869
        }
808
809
1.98k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
810
1.98k
            CF_CHECK_NE(arkworks_algebra_g2_neg_bls12_381(
811
1.98k
                        av->data(),
812
1.98k
                        aw->data(),
813
1.98k
                        ax->data(),
814
1.98k
                        ay->data(),
815
1.98k
                        affine,
816
1.98k
                        result_v.data(),
817
1.98k
                        result_w.data(),
818
1.98k
                        result_x.data(),
819
1.98k
                        result_y.data()
820
1.98k
                        ), -1);
821
14
        ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
822
14
        }
823
1.98k
    }
824
2.02k
end:
825
2.02k
    return ret;
826
1.98k
}
827
828
754
std::optional<bool> arkworks_algebra::OpBLS_BatchVerify(operation::BLS_BatchVerify& op) {
829
754
    std::optional<bool> ret = std::nullopt;
830
831
754
    std::vector<uint64_t> data;
832
833
11.1k
    for (const auto& cur : op.bf.c) {
834
11.1k
        std::optional<std::array<uint64_t, 4>> el;
835
836
11.1k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g1.first), std::nullopt);
837
11.0k
        data.insert(data.end(), el->begin(), el->end());
838
839
11.0k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g1.second), std::nullopt);
840
11.0k
        data.insert(data.end(), el->begin(), el->end());
841
842
11.0k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.first.first), std::nullopt);
843
11.0k
        data.insert(data.end(), el->begin(), el->end());
844
845
11.0k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.first.second), std::nullopt);
846
11.0k
        data.insert(data.end(), el->begin(), el->end());
847
848
11.0k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.second.first), std::nullopt);
849
11.0k
        data.insert(data.end(), el->begin(), el->end());
850
851
11.0k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.second.second), std::nullopt);
852
11.0k
        data.insert(data.end(), el->begin(), el->end());
853
11.0k
    }
854
855
665
    arkworks_algebra_batchverify_bn254(data.data(), op.bf.c.size());
856
754
end:
857
754
    return ret;
858
665
}
859
860
0
std::optional<component::G1> arkworks_algebra::OpBLS_G1_MultiExp(operation::BLS_G1_MultiExp& op) {
861
0
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
862
0
        return std::nullopt;
863
0
    }
864
865
0
    std::optional<component::G1> ret = std::nullopt;
866
867
0
    const size_t num = op.points_scalars.points_scalars.size();
868
869
0
    std::vector<uint64_t> x, y, scalars;
870
871
0
    for (size_t i = 0; i < num; i++) {
872
0
        std::optional<std::array<uint64_t, 6>> ax, ay;
873
0
        std::optional<std::array<uint64_t, 4>> b;
874
875
0
        const auto& cur = op.points_scalars.points_scalars[i];
876
0
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(cur.first.first), std::nullopt);
877
0
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(cur.first.second), std::nullopt);
878
879
        /* Workaround for https://github.com/arkworks-rs/algebra/issues/656 */
880
0
        CF_CHECK_TRUE(cur.second.IsLessThan("52435875175126190479447740508185965837690552500527637822603658699938581184513"));
881
882
0
        CF_CHECK_NE(b = arkworks_algebra_detail::To4U64(cur.second), std::nullopt);
883
884
0
        for (const auto v : *ax) {
885
0
            x.push_back(v);
886
0
        }
887
0
        for (const auto v : *ay) {
888
0
            y.push_back(v);
889
0
        }
890
0
        for (const auto v : *b) {
891
0
            scalars.push_back(v);
892
0
        }
893
0
    }
894
895
0
    std::array<uint64_t, 6> result_x, result_y;
896
0
    CF_CHECK_EQ(arkworks_algebra_g1_multiexp_bls12_381(
897
0
                x.data(),
898
0
                y.data(),
899
0
                scalars.data(),
900
0
                num,
901
0
                result_x.data(),
902
0
                result_y.data()), 0);
903
904
0
    ret = arkworks_algebra_detail::ToG1(result_x, result_y);
905
906
0
end:
907
0
    return ret;
908
0
}
909
910
20.6k
std::optional<component::Bignum> arkworks_algebra::OpBignumCalc(operation::BignumCalc& op) {
911
20.6k
    if ( op.modulo == std::nullopt ) {
912
10.5k
        return std::nullopt;
913
10.5k
    }
914
915
10.0k
    uint8_t mod = 0;
916
10.0k
    if ( op.modulo->ToTrimmedString() ==
917
10.0k
            "115792089237316195423570985008687907853269984665640564039457584007913129639936" ) {
918
0
        mod = 1;
919
10.0k
    } else if ( op.modulo->ToTrimmedString() ==
920
10.0k
            "21888242871839275222246405745257275088696311157297823662689037894645226208583" ) {
921
0
        mod = 2;
922
10.0k
    } else if ( op.modulo->ToTrimmedString() ==
923
10.0k
            "21888242871839275222246405745257275088548364400416034343698204186575808495617" ) {
924
0
        mod = 3;
925
10.0k
    } else if ( op.modulo->ToTrimmedString() ==
926
10.0k
            "52435875175126190479447740508185965837690552500527637822603658699938581184513" ) {
927
5.13k
        mod = 4;
928
5.13k
    } else if ( op.modulo->ToTrimmedString() ==
929
4.88k
            "8444461749428370424248824938781546531375899335154063827935233455917409239041" ) {
930
0
        mod = 5;
931
4.88k
    } else if ( op.modulo->ToTrimmedString() ==
932
4.88k
            "258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177" ) {
933
0
        mod = 6;
934
4.88k
    } else if ( op.modulo->ToTrimmedString() ==
935
4.88k
            "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" ) {
936
4.88k
        mod = 7;
937
4.88k
    } else {
938
0
        return std::nullopt;
939
0
    }
940
941
10.0k
    std::optional<component::Bignum> ret = std::nullopt;
942
943
10.0k
    std::optional<std::array<uint64_t, 4>> bn0, bn1, bn2;
944
10.0k
    std::optional<std::array<uint64_t, 6>> bn0_6, bn1_6, bn2_6;
945
10.0k
    std::array<uint64_t, 4> result;
946
10.0k
    std::array<uint64_t, 6> result_6;
947
10.0k
    if (mod != 6 && mod != 7) {
948
5.13k
        CF_CHECK_NE(bn0 = arkworks_algebra_detail::To4U64(op.bn0), std::nullopt);
949
4.96k
        CF_CHECK_NE(bn1 = arkworks_algebra_detail::To4U64(op.bn1), std::nullopt);
950
4.84k
        CF_CHECK_NE(bn2 = arkworks_algebra_detail::To4U64(op.bn2), std::nullopt);
951
4.88k
    } else {
952
4.88k
        CF_CHECK_NE(bn0_6 = arkworks_algebra_detail::To6U64(op.bn0), std::nullopt);
953
4.70k
        CF_CHECK_NE(bn1_6 = arkworks_algebra_detail::To6U64(op.bn1), std::nullopt);
954
4.63k
        CF_CHECK_NE(bn2_6 = arkworks_algebra_detail::To6U64(op.bn2), std::nullopt);
955
4.60k
    }
956
957
9.41k
    static const std::map<uint64_t, uint64_t> LUT = {
958
9.41k
        { CF_CALCOP("Add(A,B)"), 0 },
959
9.41k
        { CF_CALCOP("Sub(A,B)"), 1 },
960
9.41k
        { CF_CALCOP("LShift1(A)"), 2 },
961
9.41k
        { CF_CALCOP("LShift(A)"), 3 },
962
9.41k
        { CF_CALCOP("RShift(A,B)"), 4 },
963
9.41k
        { CF_CALCOP("InvMod(A,B)"), 5 },
964
9.41k
        { CF_CALCOP("Sqr(A)"), 6 },
965
9.41k
        { CF_CALCOP("Sqrt(A)"), 7 },
966
9.41k
        { CF_CALCOP("Mul(A,B)"), 8 },
967
9.41k
        { CF_CALCOP("Neg(A)"), 9 },
968
9.41k
    };
969
970
9.41k
    CF_CHECK_TRUE(LUT.find(op.calcOp.Get()) != LUT.end());
971
972
3.21k
    {
973
3.21k
        int res;
974
3.21k
        switch ( mod ) {
975
0
            case    1:
976
0
                res = arkworks_algebra_bignumcalc(
977
0
                        LUT.at(op.calcOp.Get()),
978
0
                        bn0->data(),
979
0
                        bn1->data(),
980
0
                        bn2->data(),
981
0
                        result.data()
982
0
                );
983
0
                break;
984
0
            case    2:
985
0
                res = arkworks_algebra_bignumcalc_bn254_fq(
986
0
                        LUT.at(op.calcOp.Get()),
987
0
                        bn0->data(),
988
0
                        bn1->data(),
989
0
                        bn2->data(),
990
0
                        result.data()
991
0
                );
992
0
                break;
993
0
            case    3:
994
0
                res = arkworks_algebra_bignumcalc_bn254_fr(
995
0
                        LUT.at(op.calcOp.Get()),
996
0
                        bn0->data(),
997
0
                        bn1->data(),
998
0
                        bn2->data(),
999
0
                        result.data()
1000
0
                );
1001
0
                break;
1002
1.93k
            case    4:
1003
1.93k
                res = arkworks_algebra_bignumcalc_bls12_381_fr(
1004
1.93k
                        LUT.at(op.calcOp.Get()),
1005
1.93k
                        bn0->data(),
1006
1.93k
                        bn1->data(),
1007
1.93k
                        bn2->data(),
1008
1.93k
                        result.data()
1009
1.93k
                );
1010
1.93k
                break;
1011
0
            case    5:
1012
0
                res = arkworks_algebra_bignumcalc_bls12_377_fr(
1013
0
                        LUT.at(op.calcOp.Get()),
1014
0
                        bn0->data(),
1015
0
                        bn1->data(),
1016
0
                        bn2->data(),
1017
0
                        result.data()
1018
0
                );
1019
0
                break;
1020
0
            case    6:
1021
0
                res = arkworks_algebra_bignumcalc_bls12_377_fq(
1022
0
                        LUT.at(op.calcOp.Get()),
1023
0
                        bn0_6->data(),
1024
0
                        bn1_6->data(),
1025
0
                        bn2_6->data(),
1026
0
                        result_6.data()
1027
0
                );
1028
0
                break;
1029
1.27k
            case    7:
1030
1.27k
                res = arkworks_algebra_bignumcalc_bls12_381_fq(
1031
1.27k
                        LUT.at(op.calcOp.Get()),
1032
1.27k
                        bn0_6->data(),
1033
1.27k
                        bn1_6->data(),
1034
1.27k
                        bn2_6->data(),
1035
1.27k
                        result_6.data()
1036
1.27k
                );
1037
1.27k
                break;
1038
0
            default:
1039
0
                CF_UNREACHABLE();
1040
3.21k
        }
1041
1042
3.21k
        CF_CHECK_NE(res, -1);
1043
1044
2.45k
        if (mod != 6 && mod != 7) {
1045
1.40k
            ret = arkworks_algebra_detail::From4U64(result);
1046
1.40k
        } else {
1047
1.05k
            ret = arkworks_algebra_detail::From6U64(result_6);
1048
1.05k
        }
1049
2.45k
    }
1050
1051
10.0k
end:
1052
10.0k
    return ret;
1053
2.45k
}
1054
1055
10.0k
bool arkworks_algebra::SupportsModularBignumCalc(void) const {
1056
10.0k
    return true;
1057
10.0k
}
1058
1059
} /* namespace module */
1060
} /* namespace cryptofuzz */