Coverage Report

Created: 2025-04-24 07:09

/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
103k
    std::optional<std::array<uint64_t, 4>> To4U64(const component::Bignum& bn) {
197
103k
        std::optional<std::array<uint64_t, 4>> ret = std::nullopt;
198
199
103k
        const auto bin = util::DecToBin(bn.ToTrimmedString(), 32);
200
103k
        CF_CHECK_NE(bin, std::nullopt);
201
202
103k
        std::array<uint64_t, 4> arr;
203
103k
        memcpy(&arr[0], bin->data(), sizeof(uint64_t));
204
103k
        arr[0] = __bswap_64(arr[0]);
205
103k
        memcpy(&arr[1], bin->data() + 8, sizeof(uint64_t));
206
103k
        arr[1] = __bswap_64(arr[1]);
207
103k
        memcpy(&arr[2], bin->data() + 16, sizeof(uint64_t));
208
103k
        arr[2] = __bswap_64(arr[2]);
209
103k
        memcpy(&arr[3], bin->data() + 24, sizeof(uint64_t));
210
103k
        arr[3] = __bswap_64(arr[3]);
211
212
103k
        std::reverse(arr.begin(), arr.end());
213
103k
        ret = arr;
214
103k
end:
215
103k
        return ret;
216
103k
    }
217
120k
    std::optional<std::array<uint64_t, 6>> To6U64(const component::Bignum& bn) {
218
120k
        std::optional<std::array<uint64_t, 6>> ret = std::nullopt;
219
220
120k
        const auto bin = util::DecToBin(bn.ToTrimmedString(), 48);
221
120k
        CF_CHECK_NE(bin, std::nullopt);
222
223
119k
        std::array<uint64_t, 6> arr;
224
119k
        memcpy(&arr[0], bin->data(), sizeof(uint64_t));
225
119k
        arr[0] = __bswap_64(arr[0]);
226
119k
        memcpy(&arr[1], bin->data() + 8, sizeof(uint64_t));
227
119k
        arr[1] = __bswap_64(arr[1]);
228
119k
        memcpy(&arr[2], bin->data() + 16, sizeof(uint64_t));
229
119k
        arr[2] = __bswap_64(arr[2]);
230
119k
        memcpy(&arr[3], bin->data() + 24, sizeof(uint64_t));
231
119k
        arr[3] = __bswap_64(arr[3]);
232
119k
        memcpy(&arr[4], bin->data() + 32, sizeof(uint64_t));
233
119k
        arr[4] = __bswap_64(arr[4]);
234
119k
        memcpy(&arr[5], bin->data() + 40, sizeof(uint64_t));
235
119k
        arr[5] = __bswap_64(arr[5]);
236
237
119k
        std::reverse(arr.begin(), arr.end());
238
119k
        ret = arr;
239
120k
end:
240
120k
        return ret;
241
119k
    }
242
1.77k
    const component::Bignum From4U64(std::array<uint64_t, 4> u64) {
243
1.77k
        std::reverse(u64.begin(), u64.end());
244
1.77k
        std::vector<uint8_t> bin(32);
245
1.77k
        u64[0] = __bswap_64(u64[0]);
246
1.77k
        u64[1] = __bswap_64(u64[1]);
247
1.77k
        u64[2] = __bswap_64(u64[2]);
248
1.77k
        u64[3] = __bswap_64(u64[3]);
249
1.77k
        memcpy(bin.data(), &u64[0], sizeof(uint64_t));
250
1.77k
        memcpy(bin.data() + 8, &u64[1], sizeof(uint64_t));
251
1.77k
        memcpy(bin.data() + 16, &u64[2], sizeof(uint64_t));
252
1.77k
        memcpy(bin.data() + 24, &u64[3], sizeof(uint64_t));
253
1.77k
        return component::Bignum{util::BinToDec(bin)};
254
1.77k
    }
255
34.8k
    const component::Bignum From6U64(std::array<uint64_t, 6> u64) {
256
34.8k
        std::reverse(u64.begin(), u64.end());
257
34.8k
        std::vector<uint8_t> bin(48);
258
34.8k
        u64[0] = __bswap_64(u64[0]);
259
34.8k
        u64[1] = __bswap_64(u64[1]);
260
34.8k
        u64[2] = __bswap_64(u64[2]);
261
34.8k
        u64[3] = __bswap_64(u64[3]);
262
34.8k
        u64[4] = __bswap_64(u64[4]);
263
34.8k
        u64[5] = __bswap_64(u64[5]);
264
34.8k
        memcpy(bin.data(), &u64[0], sizeof(uint64_t));
265
34.8k
        memcpy(bin.data() + 8, &u64[1], sizeof(uint64_t));
266
34.8k
        memcpy(bin.data() + 16, &u64[2], sizeof(uint64_t));
267
34.8k
        memcpy(bin.data() + 24, &u64[3], sizeof(uint64_t));
268
34.8k
        memcpy(bin.data() + 32, &u64[4], sizeof(uint64_t));
269
34.8k
        memcpy(bin.data() + 40, &u64[5], sizeof(uint64_t));
270
34.8k
        return component::Bignum{util::BinToDec(bin)};
271
34.8k
    }
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
3.19k
            const std::array<uint64_t, 6>& result_y) {
287
3.19k
        const auto x = arkworks_algebra_detail::From6U64(result_x).ToTrimmedString();
288
3.19k
        auto y = arkworks_algebra_detail::From6U64(result_y).ToTrimmedString();
289
3.19k
        if (x == "0" && y == "1") {
290
61
            y = "0";
291
61
        }
292
293
3.19k
        return component::G1{x, y};
294
3.19k
    }
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
6.78k
            const std::array<uint64_t, 6>& result_y) {
300
6.78k
        const auto v = arkworks_algebra_detail::From6U64(result_v).ToTrimmedString();
301
6.78k
        auto w = arkworks_algebra_detail::From6U64(result_w).ToTrimmedString();
302
6.78k
        const auto x = arkworks_algebra_detail::From6U64(result_x).ToTrimmedString();
303
6.78k
        const auto y = arkworks_algebra_detail::From6U64(result_y).ToTrimmedString();
304
305
6.78k
        if (v == "0" && w == "1" && x == "0" && y == "0") {
306
1.79k
            w = "0";
307
1.79k
        }
308
309
6.78k
        return component::G2{v, w, x, y};
310
6.78k
    }
311
}
312
313
3.11k
std::optional<component::BLS_PublicKey> arkworks_algebra::OpBLS_PrivateToPublic(operation::BLS_PrivateToPublic& op) {
314
3.11k
    if (
315
3.11k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
316
3.11k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
317
3.11k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
318
0
        return std::nullopt;
319
0
    }
320
321
3.11k
    std::optional<component::BLS_PublicKey> ret = std::nullopt;
322
323
3.11k
    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
3.11k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
337
3.11k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
338
3.11k
        std::optional<std::array<uint64_t, 4>> priv;
339
3.11k
        std::array<uint64_t, 6> result_x, result_y;
340
341
3.11k
        CF_CHECK_NE(priv = arkworks_algebra_detail::To4U64(op.priv), std::nullopt);
342
343
3.09k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
344
3.09k
            CF_CHECK_NE(arkworks_algebra_g1_privatetopublic_bls12_381(
345
3.09k
                        priv->data(),
346
3.09k
                        result_x.data(),
347
3.09k
                        result_y.data()
348
3.09k
                        ), -1);
349
3.01k
        } 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
3.01k
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
360
3.01k
    } else {
361
0
        CF_UNREACHABLE();
362
0
    }
363
364
3.11k
end:
365
3.11k
    return ret;
366
3.11k
}
367
368
1.87k
std::optional<bool> arkworks_algebra::OpBLS_IsG1OnCurve(operation::BLS_IsG1OnCurve& op) {
369
1.87k
    if (
370
1.87k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
371
1.87k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
372
1.87k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
373
0
        return std::nullopt;
374
0
    }
375
376
1.87k
    std::optional<bool> ret = std::nullopt;
377
378
1.87k
    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.87k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
390
1.87k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
391
1.87k
        std::optional<std::array<uint64_t, 6>> ax, ay;
392
393
1.87k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.g1.first), std::nullopt);
394
1.85k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.g1.second), std::nullopt);
395
396
1.84k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
397
1.84k
            const auto r = arkworks_algebra_g1_isoncurve_bls12_381(
398
1.84k
                        ax->data(),
399
1.84k
                        ay->data());
400
1.84k
            CF_CHECK_NE(r, -1);
401
1.48k
            ret = r == 1;
402
1.48k
        } 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.84k
    } else {
412
0
        CF_UNREACHABLE();
413
0
    }
414
415
1.87k
end:
416
1.87k
    return ret;
417
1.87k
}
418
419
3.53k
std::optional<component::G1> arkworks_algebra::OpBLS_G1_Add(operation::BLS_G1_Add& op) {
420
3.53k
    if (
421
3.53k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
422
3.53k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
423
3.53k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
424
0
        return std::nullopt;
425
0
    }
426
427
3.53k
    std::optional<component::G1> ret = std::nullopt;
428
3.53k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
429
430
3.53k
    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
3.53k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
450
3.53k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
451
3.53k
        std::optional<std::array<uint64_t, 6>> ax, ay, bx, by;
452
3.53k
        std::array<uint64_t, 6> result_x, result_y;
453
454
3.53k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.first), std::nullopt);
455
3.52k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second), std::nullopt);
456
3.51k
        CF_CHECK_NE(bx = arkworks_algebra_detail::To6U64(op.b.first), std::nullopt);
457
3.50k
        CF_CHECK_NE(by = arkworks_algebra_detail::To6U64(op.b.second), std::nullopt);
458
459
3.49k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
460
3.49k
            bool affine = true;
461
3.49k
            try {
462
3.49k
                affine = ds.Get<bool>();
463
3.49k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
464
2.02k
            }
465
466
3.49k
            CF_CHECK_NE(arkworks_algebra_g1_add_bls12_381(
467
3.49k
                        ax->data(),
468
3.49k
                        ay->data(),
469
3.49k
                        bx->data(),
470
3.49k
                        by->data(),
471
3.49k
                        affine,
472
3.49k
                        result_x.data(),
473
3.49k
                        result_y.data()
474
3.49k
                        ), -1);
475
33
        } 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
33
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
489
33
    } else {
490
0
        CF_UNREACHABLE();
491
0
    }
492
493
3.53k
end:
494
3.53k
    return ret;
495
3.53k
}
496
497
2.16k
std::optional<component::G1> arkworks_algebra::OpBLS_G1_Mul(operation::BLS_G1_Mul& op) {
498
2.16k
    if (
499
2.16k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
500
2.16k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
501
2.16k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
502
0
        return std::nullopt;
503
0
    }
504
505
2.16k
    std::optional<component::G1> ret = std::nullopt;
506
2.16k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
507
508
2.16k
    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
2.16k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
526
2.16k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
527
2.16k
        std::optional<std::array<uint64_t, 6>> ax, ay;
528
2.16k
        std::optional<std::array<uint64_t, 4>> b;
529
2.16k
        std::array<uint64_t, 6> result_x, result_y;
530
531
2.16k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.first), std::nullopt);
532
2.15k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second), std::nullopt);
533
2.14k
        CF_CHECK_NE(b = arkworks_algebra_detail::To4U64(op.b), std::nullopt);
534
535
2.12k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
536
2.12k
            bool affine = true;
537
2.12k
            try {
538
2.12k
                affine = ds.Get<bool>();
539
2.12k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
540
1.52k
            }
541
542
2.12k
            CF_CHECK_NE(arkworks_algebra_g1_mul_bls12_381(
543
2.12k
                        ax->data(),
544
2.12k
                        ay->data(),
545
2.12k
                        b->data(),
546
2.12k
                        affine,
547
2.12k
                        result_x.data(),
548
2.12k
                        result_y.data()
549
2.12k
                        ), -1);
550
108
        } 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
108
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
563
108
    } else {
564
0
        CF_UNREACHABLE();
565
0
    }
566
567
2.16k
end:
568
2.16k
    return ret;
569
2.16k
}
570
571
2.50k
std::optional<component::G1> arkworks_algebra::OpBLS_G1_Neg(operation::BLS_G1_Neg& op) {
572
2.50k
    if (
573
2.50k
        op.curveType.Get() != CF_ECC_CURVE("alt_bn128") &&
574
2.50k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
575
2.50k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
576
0
        return std::nullopt;
577
0
    }
578
579
2.50k
    std::optional<component::G1> ret = std::nullopt;
580
2.50k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
581
582
2.50k
    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.50k
    } else if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ||
598
2.50k
                op.curveType.Get() == CF_ECC_CURVE("BLS12_377") ) {
599
2.50k
        std::optional<std::array<uint64_t, 6>> ax, ay;
600
2.50k
        std::array<uint64_t, 6> result_x, result_y;
601
602
2.50k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.first), std::nullopt);
603
2.49k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second), std::nullopt);
604
605
2.47k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
606
2.47k
            bool affine = true;
607
2.47k
            try {
608
2.47k
                affine = ds.Get<bool>();
609
2.47k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
610
1.06k
            }
611
612
2.47k
            CF_CHECK_NE(arkworks_algebra_g1_neg_bls12_381(
613
2.47k
                        ax->data(),
614
2.47k
                        ay->data(),
615
2.47k
                        affine,
616
2.47k
                        result_x.data(),
617
2.47k
                        result_y.data()
618
2.47k
                        ), -1);
619
34
        } 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
34
        ret = arkworks_algebra_detail::ToG1(result_x, result_y);
631
34
    } else {
632
0
        CF_UNREACHABLE();
633
0
    }
634
635
2.50k
end:
636
2.50k
    return ret;
637
2.50k
}
638
639
2.46k
std::optional<bool> arkworks_algebra::OpBLS_IsG2OnCurve(operation::BLS_IsG2OnCurve& op) {
640
2.46k
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
641
219
        return std::nullopt;
642
219
    }
643
644
2.24k
    std::optional<bool> ret = std::nullopt;
645
646
2.24k
    {
647
2.24k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
648
649
2.24k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.g2.first.first), std::nullopt);
650
2.23k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.g2.first.second), std::nullopt);
651
2.23k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.g2.second.first), std::nullopt);
652
2.22k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.g2.second.second), std::nullopt);
653
654
2.21k
        const auto r = arkworks_algebra_g2_isoncurve_bls12_381(
655
2.21k
                av->data(),
656
2.21k
                aw->data(),
657
2.21k
                ax->data(),
658
2.21k
                ay->data());
659
2.21k
        CF_CHECK_NE(r, -1);
660
1.81k
        ret = r == 1;
661
1.81k
    }
662
663
2.24k
end:
664
2.24k
    return ret;
665
1.81k
}
666
667
3.63k
std::optional<component::G2> arkworks_algebra::OpBLS_G2_Add(operation::BLS_G2_Add& op) {
668
3.63k
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
669
0
        return std::nullopt;
670
0
    }
671
672
3.63k
    std::optional<component::G2> ret = std::nullopt;
673
3.63k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
674
675
3.63k
    {
676
3.63k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
677
3.63k
        std::optional<std::array<uint64_t, 6>> bv, bw, bx, by;
678
3.63k
        std::array<uint64_t, 6> result_v, result_w, result_x, result_y;
679
680
3.63k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.a.first.first), std::nullopt);
681
3.62k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.a.first.second), std::nullopt);
682
3.62k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.second.first), std::nullopt);
683
3.61k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second.second), std::nullopt);
684
685
3.60k
        CF_CHECK_NE(bv = arkworks_algebra_detail::To6U64(op.b.first.first), std::nullopt);
686
3.59k
        CF_CHECK_NE(bw = arkworks_algebra_detail::To6U64(op.b.first.second), std::nullopt);
687
3.58k
        CF_CHECK_NE(bx = arkworks_algebra_detail::To6U64(op.b.second.first), std::nullopt);
688
3.57k
        CF_CHECK_NE(by = arkworks_algebra_detail::To6U64(op.b.second.second), std::nullopt);
689
690
3.56k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
691
3.56k
            bool affine = true;
692
3.56k
            try {
693
3.56k
                affine = ds.Get<bool>();
694
3.56k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
695
2.06k
            }
696
697
3.56k
            CF_CHECK_NE(arkworks_algebra_g2_add_bls12_381(
698
3.56k
                        av->data(),
699
3.56k
                        aw->data(),
700
3.56k
                        ax->data(),
701
3.56k
                        ay->data(),
702
3.56k
                        bv->data(),
703
3.56k
                        bw->data(),
704
3.56k
                        bx->data(),
705
3.56k
                        by->data(),
706
3.56k
                        affine,
707
3.56k
                        result_v.data(),
708
3.56k
                        result_w.data(),
709
3.56k
                        result_x.data(),
710
3.56k
                        result_y.data()
711
3.56k
                        ), -1);
712
11
        ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
713
11
        }
714
3.56k
    }
715
3.63k
end:
716
3.63k
    return ret;
717
3.56k
}
718
719
7.44k
std::optional<component::G2> arkworks_algebra::OpBLS_G2_Mul(operation::BLS_G2_Mul& op) {
720
7.44k
    if (
721
7.44k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_381") &&
722
7.44k
        op.curveType.Get() != CF_ECC_CURVE("BLS12_377") ) {
723
0
        return std::nullopt;
724
0
    }
725
726
7.44k
    std::optional<component::G2> ret = std::nullopt;
727
7.44k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
728
729
7.44k
    {
730
7.44k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
731
7.44k
        std::optional<std::array<uint64_t, 4>> b;
732
7.44k
        std::array<uint64_t, 6> result_v, result_w, result_x, result_y;
733
734
7.44k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.a.first.first), std::nullopt);
735
7.43k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.a.first.second), std::nullopt);
736
7.42k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.second.first), std::nullopt);
737
7.41k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second.second), std::nullopt);
738
7.40k
        CF_CHECK_NE(b = arkworks_algebra_detail::To4U64(op.b), std::nullopt);
739
740
7.39k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
741
7.39k
            bool affine = true;
742
7.39k
            try {
743
7.39k
                affine = ds.Get<bool>();
744
7.39k
            } catch ( fuzzing::datasource::Base::OutOfData ) {
745
6.27k
            }
746
747
7.39k
            CF_CHECK_NE(arkworks_algebra_g2_mul_bls12_381(
748
7.39k
                        av->data(),
749
7.39k
                        aw->data(),
750
7.39k
                        ax->data(),
751
7.39k
                        ay->data(),
752
7.39k
                        b->data(),
753
7.39k
                        affine,
754
7.39k
                        result_v.data(),
755
7.39k
                        result_w.data(),
756
7.39k
                        result_x.data(),
757
7.39k
                        result_y.data()
758
7.39k
                        ), -1);
759
6.76k
            ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
760
6.76k
        } 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
7.39k
    }
782
7.44k
end:
783
7.44k
    return ret;
784
7.39k
}
785
786
2.30k
std::optional<component::G2> arkworks_algebra::OpBLS_G2_Neg(operation::BLS_G2_Neg& op) {
787
2.30k
    if ( op.curveType.Get() != CF_ECC_CURVE("BLS12_381") ) {
788
0
        return std::nullopt;
789
0
    }
790
791
2.30k
    std::optional<component::G2> ret = std::nullopt;
792
2.30k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
793
794
2.30k
    {
795
2.30k
        std::optional<std::array<uint64_t, 6>> av, aw, ax, ay;
796
2.30k
        std::array<uint64_t, 6> result_v, result_w, result_x, result_y;
797
798
2.30k
        CF_CHECK_NE(av = arkworks_algebra_detail::To6U64(op.a.first.first), std::nullopt);
799
2.29k
        CF_CHECK_NE(aw = arkworks_algebra_detail::To6U64(op.a.first.second), std::nullopt);
800
2.28k
        CF_CHECK_NE(ax = arkworks_algebra_detail::To6U64(op.a.second.first), std::nullopt);
801
2.27k
        CF_CHECK_NE(ay = arkworks_algebra_detail::To6U64(op.a.second.second), std::nullopt);
802
803
2.26k
        bool affine = true;
804
2.26k
        try {
805
2.26k
            affine = ds.Get<bool>();
806
2.26k
        } catch ( fuzzing::datasource::Base::OutOfData ) {
807
1.38k
        }
808
809
2.26k
        if ( op.curveType.Get() == CF_ECC_CURVE("BLS12_381") ) {
810
2.26k
            CF_CHECK_NE(arkworks_algebra_g2_neg_bls12_381(
811
2.26k
                        av->data(),
812
2.26k
                        aw->data(),
813
2.26k
                        ax->data(),
814
2.26k
                        ay->data(),
815
2.26k
                        affine,
816
2.26k
                        result_v.data(),
817
2.26k
                        result_w.data(),
818
2.26k
                        result_x.data(),
819
2.26k
                        result_y.data()
820
2.26k
                        ), -1);
821
15
        ret = arkworks_algebra_detail::ToG2(result_v, result_w, result_x, result_y);
822
15
        }
823
2.26k
    }
824
2.30k
end:
825
2.30k
    return ret;
826
2.26k
}
827
828
1.32k
std::optional<bool> arkworks_algebra::OpBLS_BatchVerify(operation::BLS_BatchVerify& op) {
829
1.32k
    std::optional<bool> ret = std::nullopt;
830
831
1.32k
    std::vector<uint64_t> data;
832
833
11.6k
    for (const auto& cur : op.bf.c) {
834
11.6k
        std::optional<std::array<uint64_t, 4>> el;
835
836
11.6k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g1.first), std::nullopt);
837
11.6k
        data.insert(data.end(), el->begin(), el->end());
838
839
11.6k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g1.second), std::nullopt);
840
11.6k
        data.insert(data.end(), el->begin(), el->end());
841
842
11.6k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.first.first), std::nullopt);
843
11.5k
        data.insert(data.end(), el->begin(), el->end());
844
845
11.5k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.first.second), std::nullopt);
846
11.5k
        data.insert(data.end(), el->begin(), el->end());
847
848
11.5k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.second.first), std::nullopt);
849
11.5k
        data.insert(data.end(), el->begin(), el->end());
850
851
11.5k
        CF_CHECK_NE(el = arkworks_algebra_detail::To4U64(cur.g2.second.second), std::nullopt);
852
11.5k
        data.insert(data.end(), el->begin(), el->end());
853
11.5k
    }
854
855
1.22k
    arkworks_algebra_batchverify_bn254(data.data(), op.bf.c.size());
856
1.32k
end:
857
1.32k
    return ret;
858
1.22k
}
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
24.2k
std::optional<component::Bignum> arkworks_algebra::OpBignumCalc(operation::BignumCalc& op) {
911
24.2k
    if ( op.modulo == std::nullopt ) {
912
11.3k
        return std::nullopt;
913
11.3k
    }
914
915
12.8k
    uint8_t mod = 0;
916
12.8k
    if ( op.modulo->ToTrimmedString() ==
917
12.8k
            "115792089237316195423570985008687907853269984665640564039457584007913129639936" ) {
918
0
        mod = 1;
919
12.8k
    } else if ( op.modulo->ToTrimmedString() ==
920
12.8k
            "21888242871839275222246405745257275088696311157297823662689037894645226208583" ) {
921
0
        mod = 2;
922
12.8k
    } else if ( op.modulo->ToTrimmedString() ==
923
12.8k
            "21888242871839275222246405745257275088548364400416034343698204186575808495617" ) {
924
0
        mod = 3;
925
12.8k
    } else if ( op.modulo->ToTrimmedString() ==
926
12.8k
            "52435875175126190479447740508185965837690552500527637822603658699938581184513" ) {
927
7.29k
        mod = 4;
928
7.29k
    } else if ( op.modulo->ToTrimmedString() ==
929
5.57k
            "8444461749428370424248824938781546531375899335154063827935233455917409239041" ) {
930
0
        mod = 5;
931
5.57k
    } else if ( op.modulo->ToTrimmedString() ==
932
5.57k
            "258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177" ) {
933
0
        mod = 6;
934
5.57k
    } else if ( op.modulo->ToTrimmedString() ==
935
5.57k
            "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" ) {
936
5.57k
        mod = 7;
937
5.57k
    } else {
938
0
        return std::nullopt;
939
0
    }
940
941
12.8k
    std::optional<component::Bignum> ret = std::nullopt;
942
943
12.8k
    std::optional<std::array<uint64_t, 4>> bn0, bn1, bn2;
944
12.8k
    std::optional<std::array<uint64_t, 6>> bn0_6, bn1_6, bn2_6;
945
12.8k
    std::array<uint64_t, 4> result;
946
12.8k
    std::array<uint64_t, 6> result_6;
947
12.8k
    if (mod != 6 && mod != 7) {
948
7.29k
        CF_CHECK_NE(bn0 = arkworks_algebra_detail::To4U64(op.bn0), std::nullopt);
949
7.12k
        CF_CHECK_NE(bn1 = arkworks_algebra_detail::To4U64(op.bn1), std::nullopt);
950
7.04k
        CF_CHECK_NE(bn2 = arkworks_algebra_detail::To4U64(op.bn2), std::nullopt);
951
6.97k
    } else {
952
5.57k
        CF_CHECK_NE(bn0_6 = arkworks_algebra_detail::To6U64(op.bn0), std::nullopt);
953
5.43k
        CF_CHECK_NE(bn1_6 = arkworks_algebra_detail::To6U64(op.bn1), std::nullopt);
954
5.37k
        CF_CHECK_NE(bn2_6 = arkworks_algebra_detail::To6U64(op.bn2), std::nullopt);
955
5.36k
    }
956
957
12.3k
    static const std::map<uint64_t, uint64_t> LUT = {
958
12.3k
        { CF_CALCOP("Add(A,B)"), 0 },
959
12.3k
        { CF_CALCOP("Sub(A,B)"), 1 },
960
12.3k
        { CF_CALCOP("LShift1(A)"), 2 },
961
12.3k
        { CF_CALCOP("LShift(A)"), 3 },
962
12.3k
        { CF_CALCOP("RShift(A,B)"), 4 },
963
12.3k
        { CF_CALCOP("InvMod(A,B)"), 5 },
964
12.3k
        { CF_CALCOP("Sqr(A)"), 6 },
965
12.3k
        { CF_CALCOP("Sqrt(A)"), 7 },
966
12.3k
        { CF_CALCOP("Mul(A,B)"), 8 },
967
12.3k
        { CF_CALCOP("Neg(A)"), 9 },
968
12.3k
    };
969
970
12.3k
    CF_CHECK_TRUE(LUT.find(op.calcOp.Get()) != LUT.end());
971
972
4.01k
    {
973
4.01k
        int res;
974
4.01k
        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
2.36k
            case    4:
1003
2.36k
                res = arkworks_algebra_bignumcalc_bls12_381_fr(
1004
2.36k
                        LUT.at(op.calcOp.Get()),
1005
2.36k
                        bn0->data(),
1006
2.36k
                        bn1->data(),
1007
2.36k
                        bn2->data(),
1008
2.36k
                        result.data()
1009
2.36k
                );
1010
2.36k
                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.65k
            case    7:
1030
1.65k
                res = arkworks_algebra_bignumcalc_bls12_381_fq(
1031
1.65k
                        LUT.at(op.calcOp.Get()),
1032
1.65k
                        bn0_6->data(),
1033
1.65k
                        bn1_6->data(),
1034
1.65k
                        bn2_6->data(),
1035
1.65k
                        result_6.data()
1036
1.65k
                );
1037
1.65k
                break;
1038
0
            default:
1039
0
                CF_UNREACHABLE();
1040
4.01k
        }
1041
1042
4.01k
        CF_CHECK_NE(res, -1);
1043
1044
3.10k
        if (mod != 6 && mod != 7) {
1045
1.77k
            ret = arkworks_algebra_detail::From4U64(result);
1046
1.77k
        } else {
1047
1.33k
            ret = arkworks_algebra_detail::From6U64(result_6);
1048
1.33k
        }
1049
3.10k
    }
1050
1051
12.8k
end:
1052
12.8k
    return ret;
1053
3.10k
}
1054
1055
12.8k
bool arkworks_algebra::SupportsModularBignumCalc(void) const {
1056
12.8k
    return true;
1057
12.8k
}
1058
1059
} /* namespace module */
1060
} /* namespace cryptofuzz */