Coverage Report

Created: 2023-02-22 06:14

/src/cryptofuzz/tests.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "tests.h"
2
#include <fuzzing/datasource/id.hpp>
3
#include <cryptofuzz/repository.h>
4
#include <cryptofuzz/util.h>
5
#include <boost/multiprecision/cpp_int.hpp>
6
#include <iostream>
7
8
namespace cryptofuzz {
9
namespace tests {
10
11
template <class ResultType, class OperationType>
12
11.3k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
11.3k
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
11.3k
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_SCRYPT>(cryptofuzz::operation::KDF_SCRYPT const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
782
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
782
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
782
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_HKDF>(cryptofuzz::operation::KDF_HKDF const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
4.96k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
4.96k
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
4.96k
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_TLS1_PRF>(cryptofuzz::operation::KDF_TLS1_PRF const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
388
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
388
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
388
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_PBKDF>(cryptofuzz::operation::KDF_PBKDF const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
313
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
313
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
313
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_PBKDF1>(cryptofuzz::operation::KDF_PBKDF1 const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
376
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
376
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
376
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_PBKDF2>(cryptofuzz::operation::KDF_PBKDF2 const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
1.55k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
1.55k
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
1.55k
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_ARGON2>(cryptofuzz::operation::KDF_ARGON2 const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
570
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
570
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
570
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_SSH>(cryptofuzz::operation::KDF_SSH const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
410
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
410
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
410
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_X963>(cryptofuzz::operation::KDF_X963 const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
414
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
414
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
414
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_BCRYPT>(cryptofuzz::operation::KDF_BCRYPT const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
152
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
152
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
152
}
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_SP_800_108>(cryptofuzz::operation::KDF_SP_800_108 const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
1.43k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
1.43k
    if ( result != std::nullopt && op.keySize != result->GetSize() ) {
14
        /* TODO include module name in abort message */
15
0
        util::abort({op.Name(), "invalid keySize"});
16
0
    }
17
1.43k
}
18
19
3.15k
static void checkZeroResult(const std::optional<Buffer>& b) {
20
3.15k
    if ( b == std::nullopt ) {
21
0
        return;
22
0
    }
23
24
3.15k
    if ( b->GetSize() >= 16 ) {
25
2.85k
        const std::vector<uint8_t> zeroes(b->GetSize(), 0);
26
2.85k
        if ( b->Get() == zeroes ) {
27
0
            printf("An all-zero hash was returned. This might indicate a bug.\n");
28
0
            abort();
29
0
        }
30
2.85k
    }
31
3.15k
}
32
33
4.58k
void test(const operation::Digest& op, const std::optional<component::Digest>& result) {
34
4.58k
    if ( result == std::nullopt ) {
35
2.49k
        return;
36
2.49k
    }
37
38
2.09k
    {
39
2.09k
        const auto expectedSize = repository::DigestSize(op.digestType.Get());
40
41
2.09k
        if ( expectedSize != std::nullopt ) {
42
1.96k
            if ( result->GetSize() != *expectedSize ) {
43
0
                printf("Expected vs actual digest size: %zu / %zu\n", *expectedSize, result->GetSize());
44
0
                abort();
45
0
            }
46
1.96k
        }
47
2.09k
    }
48
49
2.09k
    checkZeroResult(result);
50
2.09k
}
51
52
2.28k
void test(const operation::HMAC& op, const std::optional<component::MAC>& result) {
53
2.28k
    if ( result == std::nullopt ) {
54
1.22k
        return;
55
1.22k
    }
56
57
1.06k
    {
58
1.06k
        const auto expectedSize = repository::DigestSize(op.digestType.Get());
59
60
1.06k
        if ( expectedSize != std::nullopt ) {
61
857
            if ( result->GetSize() != *expectedSize ) {
62
0
                printf("Expected vs actual digest size: %zu / %zu\n", *expectedSize, result->GetSize());
63
0
                abort();
64
0
            }
65
857
        }
66
1.06k
    }
67
68
1.06k
    checkZeroResult(result);
69
1.06k
}
70
71
2.86k
void test(const operation::UMAC& op, const std::optional<component::MAC>& result) {
72
2.86k
    if ( result == std::nullopt ) {
73
1.47k
        return;
74
1.47k
    }
75
76
1.39k
    if (
77
1.39k
            ( op.type == 0 && result->GetSize() > (32/8) ) ||
78
1.39k
            ( op.type == 1 && result->GetSize() > (64/8) ) ||
79
1.39k
            ( op.type == 2 && result->GetSize() > (96/8) ) ||
80
1.39k
            ( op.type == 3 && result->GetSize() > (128/8) )
81
1.39k
    ) {
82
0
        printf("UMAC: Overlong result: %zu\n", result->GetSize());
83
0
        abort();
84
0
    }
85
1.39k
}
86
87
11.9k
static void test_ChaCha20_Poly1305_IV(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
88
11.9k
    using fuzzing::datasource::ID;
89
90
    /*
91
     * OpenSSL CVE-2019-1543
92
     * https://www.openssl.org/news/secadv/20190306.txt
93
     */
94
95
11.9k
    if ( op.cipher.cipherType.Get() != CF_CIPHER("CHACHA20_POLY1305") ) {
96
11.5k
        return;
97
11.5k
    }
98
99
435
    if ( result == std::nullopt ) {
100
312
        return;
101
312
    }
102
103
123
    if ( op.cipher.iv.GetSize() > 12 ) {
104
0
        abort();
105
0
    }
106
123
}
107
108
11.9k
static void test_XChaCha20_Poly1305_IV(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
109
11.9k
    using fuzzing::datasource::ID;
110
111
11.9k
    if ( op.cipher.cipherType.Get() != CF_CIPHER("XCHACHA20_POLY1305") ) {
112
11.9k
        return;
113
11.9k
    }
114
115
42
    if ( result == std::nullopt ) {
116
42
        return;
117
42
    }
118
119
0
    if ( op.cipher.iv.GetSize() != 24 ) {
120
0
        printf("XChaCha20-Poly1305 succeeded with an IV of %zu bytes large, but only IVs of 24 bytes are valid\n", op.cipher.iv.GetSize());
121
0
        abort();
122
0
    }
123
0
}
124
125
11.9k
static void test_AES_CCM_Wycheproof(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
126
11.9k
    bool fail = false;
127
128
11.9k
    if ( result == std::nullopt ) {
129
8.08k
        return;
130
8.08k
    }
131
132
3.91k
    switch ( op.cipher.cipherType.Get() ) {
133
67
        case CF_CIPHER("AES_128_CCM"):
134
151
        case CF_CIPHER("AES_192_CCM"):
135
196
        case CF_CIPHER("AES_256_CCM"):
136
196
            break;
137
3.71k
        default:
138
3.71k
            return;
139
3.91k
    }
140
141
196
    if ( op.cipher.iv.GetSize() < 7 || op.cipher.iv.GetSize() > 13 ) {
142
0
        printf("AES CCM: Invalid IV size\n");
143
0
        fail = true;
144
0
    }
145
146
196
    if ( result->tag != std::nullopt ) {
147
196
        static const std::vector<size_t> validTagSizes = {4, 6, 8, 10, 12, 14, 16};
148
149
196
        if ( std::find(validTagSizes.begin(), validTagSizes.end(), result->tag->GetSize()) == validTagSizes.end() ) {
150
0
            printf("AES CCM: Invalid tag size\n");
151
0
            fail = true;
152
0
        }
153
196
    }
154
155
196
    if ( fail == true ) {
156
0
        printf("AES CCM tests based on Wycheproof: https://github.com/google/wycheproof/blob/4672ff74d68766e7785c2cac4c597effccef2c5c/testvectors/aes_ccm_test.json#L11\n");
157
0
        abort();
158
0
    }
159
196
}
160
161
11.9k
static void test_AES_GCM_Wycheproof(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
162
11.9k
    bool fail = false;
163
164
11.9k
    if ( result == std::nullopt ) {
165
8.08k
        return;
166
8.08k
    }
167
168
3.91k
    switch ( op.cipher.cipherType.Get() ) {
169
194
        case CF_CIPHER("AES_128_GCM"):
170
296
        case CF_CIPHER("AES_192_GCM"):
171
343
        case CF_CIPHER("AES_256_GCM"):
172
343
            break;
173
3.56k
        default:
174
3.56k
            return;
175
3.91k
    }
176
177
343
    if ( op.cipher.iv.GetSize() == 0 ) {
178
0
        printf("AES GCM: Invalid IV size\n");
179
0
        fail = true;
180
0
    }
181
182
343
    if ( fail == true ) {
183
0
        printf("AES GCM tests based on Wycheproof: https://github.com/google/wycheproof/blob/4672ff74d68766e7785c2cac4c597effccef2c5c/testvectors/aes_gcm_test.json#L13\n");
184
0
        abort();
185
0
    }
186
343
}
187
188
11.9k
void test(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
189
11.9k
    test_ChaCha20_Poly1305_IV(op, result);
190
11.9k
    test_XChaCha20_Poly1305_IV(op, result);
191
11.9k
    test_AES_CCM_Wycheproof(op, result);
192
11.9k
    test_AES_GCM_Wycheproof(op, result);
193
11.9k
}
194
195
9.60k
void test(const operation::SymmetricDecrypt& op, const std::optional<component::Cleartext>& result) {
196
9.60k
    (void)op;
197
9.60k
    (void)result;
198
9.60k
}
199
200
2.48k
void test(const operation::CMAC& op, const std::optional<component::MAC>& result) {
201
2.48k
    (void)op;
202
2.48k
    (void)result;
203
2.48k
}
204
205
782
void test(const operation::KDF_SCRYPT& op, const std::optional<component::Key>& result) {
206
782
    verifyKeySize(op, result);
207
782
}
208
209
4.96k
static void test_HKDF_OutputSize(const operation::KDF_HKDF& op, const std::optional<component::Key>& result) {
210
4.96k
    if ( result == std::nullopt ) {
211
2.85k
        return;
212
2.85k
    }
213
214
2.11k
    const auto expectedSize = repository::DigestSize(op.digestType.Get());
215
216
2.11k
    if ( expectedSize == std::nullopt ) {
217
139
        return;
218
139
    }
219
220
1.97k
    const size_t maxOutputSize = 255 * *expectedSize;
221
222
1.97k
    if ( result->GetSize() > maxOutputSize ) {
223
0
        printf("The output size of HKDF (%zu) is more than 255 * the size of the hash digest (%zu)\n", result->GetSize(), maxOutputSize);
224
0
        abort();
225
0
    }
226
1.97k
}
227
228
4.96k
void test(const operation::KDF_HKDF& op, const std::optional<component::Key>& result) {
229
4.96k
    verifyKeySize(op, result);
230
231
4.96k
    test_HKDF_OutputSize(op, result);
232
4.96k
}
233
234
388
void test(const operation::KDF_TLS1_PRF& op, const std::optional<component::Key>& result) {
235
388
    verifyKeySize(op, result);
236
388
}
237
238
313
void test(const operation::KDF_PBKDF& op, const std::optional<component::Key>& result) {
239
313
    verifyKeySize(op, result);
240
313
}
241
242
376
void test(const operation::KDF_PBKDF1& op, const std::optional<component::Key>& result) {
243
376
    verifyKeySize(op, result);
244
376
}
245
246
1.55k
void test(const operation::KDF_PBKDF2& op, const std::optional<component::Key>& result) {
247
1.55k
    verifyKeySize(op, result);
248
1.55k
}
249
250
570
void test(const operation::KDF_ARGON2& op, const std::optional<component::Key>& result) {
251
570
    verifyKeySize(op, result);
252
570
}
253
254
410
void test(const operation::KDF_SSH& op, const std::optional<component::Key>& result) {
255
410
    verifyKeySize(op, result);
256
410
}
257
258
414
void test(const operation::KDF_X963& op, const std::optional<component::Key>& result) {
259
414
    verifyKeySize(op, result);
260
414
}
261
262
152
void test(const operation::KDF_BCRYPT& op, const std::optional<component::Key>& result) {
263
152
    verifyKeySize(op, result);
264
152
}
265
266
1.43k
void test(const operation::KDF_SP_800_108& op, const std::optional<component::Key>& result) {
267
1.43k
    verifyKeySize(op, result);
268
1.43k
}
269
270
1.38k
static bool IsSpecialCurve(const uint64_t curveID) {
271
1.38k
    switch ( curveID ) {
272
0
        case CF_ECC_CURVE("ed448"):
273
284
        case CF_ECC_CURVE("ed25519"):
274
284
        case CF_ECC_CURVE("x25519"):
275
284
        case CF_ECC_CURVE("x448"):
276
284
            return true;
277
1.10k
        default:
278
1.10k
            return false;
279
1.38k
    }
280
1.38k
}
281
282
1.92k
static void test_ECC_PrivateKey(const uint64_t curveID, const std::string priv) {
283
    /* Disabled until all modules comply by default */
284
1.92k
    return;
285
286
    /* Private key may be 0 with these curves */
287
0
    if ( IsSpecialCurve(curveID) ) {
288
0
        return;
289
0
    }
290
291
0
    if ( priv == "0" ) {
292
0
        std::cout << "0 is an invalid elliptic curve private key" << std::endl;
293
0
        ::abort();
294
0
    }
295
0
}
296
297
298
1.57k
void test(const operation::ECC_PrivateToPublic& op, const std::optional<component::ECC_PublicKey>& result) {
299
1.57k
    if ( result != std::nullopt ) {
300
577
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
301
577
    }
302
1.57k
}
303
304
1.19k
void test(const operation::ECC_ValidatePubkey& op, const std::optional<bool>& result) {
305
1.19k
    (void)op;
306
1.19k
    (void)result;
307
1.19k
}
308
309
1.44k
void test(const operation::ECC_GenerateKeyPair& op, const std::optional<component::ECC_KeyPair>& result) {
310
1.44k
    if ( result != std::nullopt ) {
311
433
        test_ECC_PrivateKey(op.curveType.Get(), result->priv.ToTrimmedString());
312
433
    }
313
1.44k
}
314
315
1.32k
static void test_ECDSA_Signature(const uint64_t curveID, const std::string R, const std::string S) {
316
1.32k
    if ( IsSpecialCurve(curveID) ) {
317
226
        return;
318
226
    }
319
320
1.10k
    const boost::multiprecision::cpp_int r(R), s(S);
321
322
1.10k
    if ( r < 1 ) {
323
0
        std::cout << "ECDSA signature invalid: R < 1" << std::endl;
324
0
        ::abort();
325
0
    }
326
1.10k
    if ( s < 1 ) {
327
0
        std::cout << "ECDSA signature invalid: S < 1" << std::endl;
328
0
        ::abort();
329
0
    }
330
331
1.10k
    const auto O = cryptofuzz::repository::ECC_CurveToOrder(curveID);
332
1.10k
    if ( O == std::nullopt ) {
333
123
        return;
334
123
    }
335
336
978
    const boost::multiprecision::cpp_int o(*O);
337
338
978
    if ( r >= o ) {
339
0
        std::cout << "ECDSA signature invalid: R >= order" << std::endl;
340
0
        ::abort();
341
0
    }
342
343
978
    if ( s >= o ) {
344
0
        std::cout << "ECDSA signature invalid: S >= order" << std::endl;
345
0
        ::abort();
346
0
    }
347
978
}
348
349
0
static void test_BIP340_Schnorr_Signature(const uint64_t curveID, const std::string R, const std::string S) {
350
0
    boost::multiprecision::cpp_int r(R);
351
0
    boost::multiprecision::cpp_int s(S);
352
0
    if ( r < 1 ) {
353
0
        std::cout << "BIP340 Schnorr signature invalid: R < 1" << std::endl;
354
0
        ::abort();
355
0
    }
356
0
    if ( s < 1 ) {
357
0
        std::cout << "BIP340 Schnorr signature invalid: S < 1" << std::endl;
358
0
        ::abort();
359
0
    }
360
361
0
    const auto prime = cryptofuzz::repository::ECC_CurveToPrime(curveID);
362
0
    if ( prime != std::nullopt ) {
363
0
        const boost::multiprecision::cpp_int p(*prime);
364
0
        CF_ASSERT(r < p, "BIP340 Schnorr signature R should be less than curve P");
365
0
    }
366
367
0
    const auto order = cryptofuzz::repository::ECC_CurveToOrder(curveID);
368
0
    if ( order != std::nullopt ) {
369
0
        const boost::multiprecision::cpp_int n(*order);
370
0
        CF_ASSERT(s < n, "BIP340 Schnorr signature S should be less than curve N");
371
0
    }
372
0
}
373
374
149
void test(const operation::ECCSI_Sign& op, const std::optional<component::ECCSI_Signature>& result) {
375
149
    (void)op;
376
149
    (void)result;
377
149
}
378
1.77k
void test(const operation::ECDSA_Sign& op, const std::optional<component::ECDSA_Signature>& result) {
379
1.77k
    if ( result != std::nullopt ) {
380
845
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
381
382
845
        if (
383
845
                op.UseSpecifiedNonce() == true &&
384
845
                !IsSpecialCurve(op.curveType.Get()) &&
385
845
                op.nonce.ToTrimmedString() == "0"
386
845
           ) {
387
0
            std::cout << "0 is an invalid ECDSA nonce" << std::endl;
388
0
            ::abort();
389
0
        }
390
391
845
        test_ECDSA_Signature(op.curveType.Get(),
392
845
                result->signature.first.ToTrimmedString(),
393
845
                result->signature.second.ToTrimmedString());
394
845
    }
395
1.77k
}
396
397
460
void test(const operation::ECGDSA_Sign& op, const std::optional<component::ECGDSA_Signature>& result) {
398
460
    if ( result != std::nullopt ) {
399
65
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
400
401
65
        if (
402
65
                op.UseSpecifiedNonce() == true &&
403
65
                !IsSpecialCurve(op.curveType.Get()) &&
404
65
                op.nonce.ToTrimmedString() == "0"
405
65
           ) {
406
0
            std::cout << "0 is an invalid ECGDSA nonce" << std::endl;
407
0
            ::abort();
408
0
        }
409
410
65
        test_ECDSA_Signature(op.curveType.Get(),
411
65
                result->signature.first.ToTrimmedString(),
412
65
                result->signature.second.ToTrimmedString());
413
65
    }
414
460
}
415
416
158
void test(const operation::ECRDSA_Sign& op, const std::optional<component::ECRDSA_Signature>& result) {
417
158
    if ( result != std::nullopt ) {
418
0
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
419
420
0
        if (
421
0
                op.UseSpecifiedNonce() == true &&
422
0
                !IsSpecialCurve(op.curveType.Get()) &&
423
0
                op.nonce.ToTrimmedString() == "0"
424
0
           ) {
425
0
            std::cout << "0 is an invalid ECRDSA nonce" << std::endl;
426
0
            ::abort();
427
0
        }
428
429
0
        test_ECDSA_Signature(op.curveType.Get(),
430
0
                result->signature.first.ToTrimmedString(),
431
0
                result->signature.second.ToTrimmedString());
432
0
    }
433
158
}
434
435
111
void test(const operation::Schnorr_Sign& op, const std::optional<component::Schnorr_Signature>& result) {
436
111
    if ( result != std::nullopt ) {
437
0
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
438
439
0
        if (
440
0
                op.UseSpecifiedNonce() == true &&
441
0
                !IsSpecialCurve(op.curveType.Get()) &&
442
0
                op.nonce.ToTrimmedString() == "0"
443
0
           ) {
444
0
            std::cout << "0 is an invalid Schnorr nonce" << std::endl;
445
0
            ::abort();
446
0
        }
447
448
0
        test_BIP340_Schnorr_Signature(op.curveType.Get(),
449
0
                result->signature.first.ToTrimmedString(),
450
0
                result->signature.second.ToTrimmedString());
451
0
    }
452
111
}
453
454
113
void test(const operation::ECCSI_Verify& op, const std::optional<bool>& result) {
455
113
    (void)op;
456
113
    (void)result;
457
113
}
458
459
779
void test(const operation::ECDSA_Verify& op, const std::optional<bool>& result) {
460
779
    if ( result != std::nullopt && *result == true ) {
461
13
        test_ECDSA_Signature(op.curveType.Get(),
462
13
                op.signature.signature.first.ToTrimmedString(),
463
13
                op.signature.signature.second.ToTrimmedString());
464
13
    }
465
779
}
466
467
406
void test(const operation::ECGDSA_Verify& op, const std::optional<bool>& result) {
468
406
    if ( result != std::nullopt && *result == true ) {
469
1
        test_ECDSA_Signature(op.curveType.Get(),
470
1
                op.signature.signature.first.ToTrimmedString(),
471
1
                op.signature.signature.second.ToTrimmedString());
472
1
    }
473
406
}
474
475
107
void test(const operation::ECRDSA_Verify& op, const std::optional<bool>& result) {
476
107
    if ( result != std::nullopt && *result == true ) {
477
0
        test_ECDSA_Signature(op.curveType.Get(),
478
0
                op.signature.signature.first.ToTrimmedString(),
479
0
                op.signature.signature.second.ToTrimmedString());
480
0
    }
481
107
}
482
483
116
void test(const operation::Schnorr_Verify& op, const std::optional<bool>& result) {
484
116
    if ( result != std::nullopt && *result == true ) {
485
0
        test_BIP340_Schnorr_Signature(op.curveType.Get(),
486
0
                op.signature.signature.first.ToTrimmedString(),
487
0
                op.signature.signature.second.ToTrimmedString());
488
0
    }
489
116
}
490
491
1.10k
void test(const operation::ECDSA_Recover& op, const std::optional<component::ECC_PublicKey>& result) {
492
1.10k
    if ( result != std::nullopt ) {
493
403
        if ( op.id > 3 ) {
494
0
            std::cout << "Invalid recovery ID" << std::endl;
495
0
            ::abort();
496
0
        }
497
403
    }
498
1.10k
    if ( result != std::nullopt ) {
499
403
        test_ECDSA_Signature(op.curveType.Get(),
500
403
                op.signature.first.ToTrimmedString(),
501
403
                op.signature.second.ToTrimmedString());
502
403
    }
503
1.10k
}
504
505
148
void test(const operation::DSA_Verify& op, const std::optional<bool>& result) {
506
148
    (void)op;
507
508
148
    if ( result == std::nullopt || *result == false ) {
509
148
        return;
510
148
    }
511
512
0
    if ( !op.signature.first.IsPositive() ) {
513
0
        std::cout << "DSA signature must be rejected if R is smaller than 1" << std::endl;
514
0
        ::abort();
515
0
    }
516
0
    if ( !op.signature.second.IsPositive() ) {
517
0
        std::cout << "DSA signature must be rejected is S is smaller than 1" << std::endl;
518
0
        ::abort();
519
0
    }
520
521
    /* Q > R */
522
0
    if ( op.signature.first.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) {
523
0
        std::cout << "DSA signature must be rejected if R is larger than Q" << std::endl;
524
0
        ::abort();
525
0
    }
526
    /* Q > S */
527
0
    if ( op.signature.second.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) {
528
0
        std::cout << "DSA signature must be rejected if S is larger than Q" << std::endl;
529
0
        ::abort();
530
0
    }
531
0
}
532
533
186
void test(const operation::DSA_Sign& op, const std::optional<component::DSA_Signature>& result) {
534
186
    if ( result == std::nullopt ) {
535
186
        return;
536
186
    }
537
538
0
    if ( !result->signature.first.IsPositive() ) {
539
0
        std::cout << "DSA signature R must be larger than 0" << std::endl;
540
0
        ::abort();
541
0
    }
542
0
    if ( !result->signature.second.IsPositive() ) {
543
0
        std::cout << "DSA signature S must be larger than 0" << std::endl;
544
0
        ::abort();
545
0
    }
546
547
    /* Q > R */
548
0
    if ( result->signature.first.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) {
549
0
        std::cout << "DSA signature R must be smaller than P" << std::endl;
550
0
        ::abort();
551
0
    }
552
    /* Q > S */
553
0
    if ( result->signature.second.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) {
554
0
        std::cout << "DSA signature S must be smaller than Q" << std::endl;
555
0
        ::abort();
556
0
    }
557
558
    /* R > 0 */
559
0
    if ( !result->signature.first.IsPositive() ) {
560
0
        std::cout << "DSA signature R must be larger than 0" << std::endl;
561
0
        ::abort();
562
0
    }
563
    /* S > 0 */
564
0
    if ( !result->signature.second.IsPositive() ) {
565
0
        std::cout << "DSA signature R must be larger than 0" << std::endl;
566
0
        ::abort();
567
0
    }
568
0
}
569
570
0
static bool isComposite(const std::string &num) {
571
0
    if ( num.size() == 0 ) {
572
0
        return true;
573
0
    }
574
575
0
    size_t sum = 0;
576
0
    for (char c : num) {
577
0
        sum += c - '0';
578
0
    }
579
0
    if (sum % 3 == 0) {
580
0
        return true;
581
0
    }
582
583
0
    return false;
584
0
}
585
586
587
116
void test(const operation::DSA_GenerateParameters& op, const std::optional<component::DSA_Parameters>& result) {
588
116
    (void)op;
589
590
116
    if ( result == std::nullopt ) {
591
116
        return;
592
116
    }
593
594
    /* Larger than 0 */
595
0
    if ( !result->p.IsPositive() ) {
596
0
        std::cout << "DSA P parameter must be larger than 0" << std::endl;
597
0
        ::abort();
598
0
    }
599
0
    if ( !result->q.IsPositive() ) {
600
0
        std::cout << "DSA Q parameter must be larger than 0" << std::endl;
601
0
        ::abort();
602
0
    }
603
0
    if ( !result->g.IsPositive() ) {
604
0
        std::cout << "DSA G parameter must be larger than 0" << std::endl;
605
0
        ::abort();
606
0
    }
607
608
    /* P > Q */
609
0
    if ( result->q.ToTrimmedString().size() > result->p.ToTrimmedString().size() ) {
610
0
        std::cout << "DSA Q must be smaller than P" << std::endl;
611
0
        ::abort();
612
0
    }
613
614
    /* P > G */
615
0
    if ( result->q.ToTrimmedString().size() > result->p.ToTrimmedString().size() ) {
616
0
        std::cout << "DSA G must be smaller than P" << std::endl;
617
0
        ::abort();
618
0
    }
619
620
    /* G != 1 */
621
0
    if ( result->p.ToTrimmedString() == "1" ) {
622
0
        std::cout << "DSA G must not be 1" << std::endl;
623
0
        ::abort();
624
0
    }
625
626
    /* P, Q must be prime */
627
0
    if ( isComposite(result->p.ToTrimmedString()) ) {
628
0
        std::cout << "DSA P must be prime" << std::endl;
629
0
        ::abort();
630
0
    }
631
632
0
    if ( isComposite(result->q.ToTrimmedString()) ) {
633
0
        std::cout << "DSA Q must be prime" << std::endl;
634
0
        ::abort();
635
0
    }
636
0
}
637
638
96
void test(const operation::DSA_PrivateToPublic& op, const std::optional<component::Bignum>& result) {
639
96
    (void)op;
640
96
    (void)result;
641
96
}
642
643
145
void test(const operation::DSA_GenerateKeyPair& op, const std::optional<component::DSA_KeyPair>& result) {
644
145
    if ( result == std::nullopt ) {
645
145
        return;
646
145
    }
647
648
0
    if ( !result->first.IsPositive() ) {
649
0
        std::cout << "Private key must be larger than 0" << std::endl;
650
0
        ::abort();
651
0
    }
652
653
    /* Q > priv */
654
0
    if ( result->first.ToTrimmedString().size() > op.q.ToTrimmedString().size() ) {
655
0
        std::cout << "Q must be larger than private key" << std::endl;
656
0
        ::abort();
657
0
    }
658
0
}
659
660
137
void test(const operation::ECDH_Derive& op, const std::optional<component::Secret>& result) {
661
137
    (void)op;
662
137
    (void)result;
663
137
}
664
665
114
void test(const operation::ECIES_Encrypt& op, const std::optional<component::Ciphertext>& result) {
666
    /* TODO check minimum size? */
667
114
    (void)op;
668
114
    (void)result;
669
114
}
670
671
124
void test(const operation::ECIES_Decrypt& op, const std::optional<component::Cleartext>& result) {
672
124
    (void)op;
673
124
    (void)result;
674
124
}
675
676
236
void test(const operation::ECC_Point_Add& op, const std::optional<component::ECC_Point>& result) {
677
236
    (void)op;
678
236
    (void)result;
679
236
}
680
681
799
void test(const operation::ECC_Point_Mul& op, const std::optional<component::ECC_Point>& result) {
682
799
    (void)op;
683
799
    (void)result;
684
799
}
685
686
244
void test(const operation::ECC_Point_Neg& op, const std::optional<component::ECC_Point>& result) {
687
244
    (void)op;
688
244
    (void)result;
689
244
}
690
691
201
void test(const operation::ECC_Point_Dbl& op, const std::optional<component::ECC_Point>& result) {
692
201
    (void)op;
693
201
    (void)result;
694
201
}
695
696
273
void test(const operation::ECC_Point_Cmp& op, const std::optional<bool>& result) {
697
273
    (void)op;
698
273
    (void)result;
699
273
}
700
701
114
void test(const operation::DH_GenerateKeyPair& op, const std::optional<component::DH_KeyPair>& result) {
702
114
    (void)op;
703
114
    (void)result;
704
114
}
705
706
588
void test(const operation::DH_Derive& op, const std::optional<component::Bignum>& result) {
707
588
    (void)op;
708
588
    (void)result;
709
588
}
710
711
153
void test(const operation::BLS_PrivateToPublic& op, const std::optional<component::BLS_PublicKey>& result) {
712
153
    (void)op;
713
153
    (void)result;
714
153
}
715
716
128
void test(const operation::BLS_PrivateToPublic_G2& op, const std::optional<component::G2>& result) {
717
128
    (void)op;
718
128
    (void)result;
719
128
}
720
721
98
void test(const operation::BLS_Sign& op, const std::optional<component::BLS_Signature>& result) {
722
98
    (void)op;
723
98
    (void)result;
724
98
}
725
726
126
void test(const operation::BLS_Verify& op, const std::optional<bool>& result) {
727
126
    (void)op;
728
126
    (void)result;
729
126
}
730
731
151
void test(const operation::BLS_BatchSign& op, const std::optional<component::BLS_BatchSignature>& result) {
732
151
    (void)op;
733
151
    (void)result;
734
151
}
735
736
126
void test(const operation::BLS_BatchVerify& op, const std::optional<bool>& result) {
737
126
    (void)op;
738
126
    (void)result;
739
126
}
740
741
114
void test(const operation::BLS_Aggregate_G1& op, const std::optional<component::G1>& result) {
742
114
    (void)op;
743
114
    (void)result;
744
114
}
745
746
117
void test(const operation::BLS_Aggregate_G2& op, const std::optional<component::G2>& result) {
747
117
    (void)op;
748
117
    (void)result;
749
117
}
750
751
108
void test(const operation::BLS_Pairing& op, const std::optional<component::Fp12>& result) {
752
108
    (void)op;
753
108
    (void)result;
754
108
}
755
756
103
void test(const operation::BLS_MillerLoop& op, const std::optional<component::Fp12>& result) {
757
103
    (void)op;
758
103
    (void)result;
759
103
}
760
761
140
void test(const operation::BLS_FinalExp& op, const std::optional<component::Fp12>& result) {
762
140
    (void)op;
763
140
    (void)result;
764
140
}
765
766
122
void test(const operation::BLS_HashToG1& op, const std::optional<component::G1>& result) {
767
122
    (void)op;
768
122
    (void)result;
769
122
}
770
771
116
void test(const operation::BLS_HashToG2& op, const std::optional<component::G2>& result) {
772
116
    (void)op;
773
116
    (void)result;
774
116
}
775
776
149
void test(const operation::BLS_MapToG1& op, const std::optional<component::G1>& result) {
777
149
    (void)op;
778
149
    (void)result;
779
149
}
780
781
113
void test(const operation::BLS_MapToG2& op, const std::optional<component::G2>& result) {
782
113
    (void)op;
783
113
    (void)result;
784
113
}
785
786
111
void test(const operation::BLS_IsG1OnCurve& op, const std::optional<bool>& result) {
787
111
    (void)op;
788
111
    (void)result;
789
111
}
790
791
130
void test(const operation::BLS_IsG2OnCurve& op, const std::optional<bool>& result) {
792
130
    (void)op;
793
130
    (void)result;
794
130
}
795
796
153
void test(const operation::BLS_GenerateKeyPair& op, const std::optional<component::BLS_KeyPair>& result) {
797
153
    (void)op;
798
153
    (void)result;
799
153
}
800
801
109
void test(const operation::BLS_Decompress_G1& op, const std::optional<component::G1>& result) {
802
109
    (void)op;
803
109
    (void)result;
804
109
}
805
806
121
void test(const operation::BLS_Compress_G1& op, const std::optional<component::Bignum>& result) {
807
121
    (void)op;
808
121
    (void)result;
809
121
}
810
811
121
void test(const operation::BLS_Decompress_G2& op, const std::optional<component::G2>& result) {
812
121
    (void)op;
813
121
    (void)result;
814
121
}
815
816
120
void test(const operation::BLS_Compress_G2& op, const std::optional<component::G1>& result) {
817
120
    (void)op;
818
120
    (void)result;
819
120
}
820
821
168
void test(const operation::BLS_G1_Add& op, const std::optional<component::G1>& result) {
822
168
    (void)op;
823
168
    (void)result;
824
168
}
825
826
124
void test(const operation::BLS_G1_Mul& op, const std::optional<component::G1>& result) {
827
124
    (void)op;
828
124
    (void)result;
829
124
}
830
831
159
void test(const operation::BLS_G1_IsEq& op, const std::optional<bool>& result) {
832
159
    (void)op;
833
159
    (void)result;
834
159
}
835
836
122
void test(const operation::BLS_G1_Neg& op, const std::optional<component::G1>& result) {
837
122
    (void)op;
838
122
    (void)result;
839
122
}
840
841
224
void test(const operation::BLS_G2_Add& op, const std::optional<component::G2>& result) {
842
224
    (void)op;
843
224
    (void)result;
844
224
}
845
846
173
void test(const operation::BLS_G2_Mul& op, const std::optional<component::G2>& result) {
847
173
    (void)op;
848
173
    (void)result;
849
173
}
850
851
175
void test(const operation::BLS_G2_IsEq& op, const std::optional<bool>& result) {
852
175
    (void)op;
853
175
    (void)result;
854
175
}
855
856
199
void test(const operation::BLS_G2_Neg& op, const std::optional<component::G2>& result) {
857
199
    (void)op;
858
199
    (void)result;
859
199
}
860
861
103
void test(const operation::Misc& op, const std::optional<Buffer>& result) {
862
103
    (void)op;
863
103
    (void)result;
864
103
}
865
866
108
void test(const operation::SR25519_Verify& op, const std::optional<bool>& result) {
867
108
    (void)op;
868
108
    (void)result;
869
108
}
870
871
namespace BignumCalc {
872
0
    static void Abort(const std::string& message, const std::string& opStr) {
873
0
        std::cout << "BignumCalc ( " << opStr << " ): " << message << std::endl;
874
0
        ::abort();
875
0
    }
876
310
    static void AssertBinary(const component::Bignum& result, const std::string& opStr) {
877
310
        const auto resultStr = result.ToTrimmedString();
878
310
        if ( !(resultStr == "0" || resultStr == "1") ) {
879
0
            Abort("Result must be 0 or 1", opStr);
880
0
        }
881
310
    }
882
110
    static void AssertTertiary(const component::Bignum& result, const std::string& opStr) {
883
110
        const auto resultStr = result.ToTrimmedString();
884
110
        if ( !(resultStr == "0" || resultStr == "1" || resultStr == "-1") ) {
885
0
            Abort("Result must be 0 or 1 or -1", opStr);
886
0
        }
887
110
    }
888
0
    static bool IsEqual(const component::Bignum& A, const component::Bignum& B) {
889
0
        return A.ToTrimmedString() == B.ToTrimmedString();
890
0
    }
891
451
    static bool IsZero(const component::Bignum& A) {
892
451
        return A.ToTrimmedString() == "0";
893
451
    }
894
291
    static bool SmallerThan(const component::Bignum& A, const component::Bignum& B) {
895
291
        return A.ToTrimmedString().size() < B.ToTrimmedString().size();
896
291
    }
897
480
    static bool LargerThan(const component::Bignum& A, const component::Bignum& B) {
898
480
        return A.ToTrimmedString().size() > B.ToTrimmedString().size();
899
480
    }
900
652
    static bool IsEqualOrLargerThan(const component::Bignum& A, const component::Bignum& B) {
901
652
        const auto a = A.ToTrimmedString();
902
652
        const auto b = B.ToTrimmedString();
903
652
        if ( a.size() > b.size() ) {
904
0
            return true;
905
0
        }
906
652
        if ( a.size() == b.size() ) {
907
388
            if ( a == b ) {
908
0
                return true;
909
0
            }
910
388
        }
911
652
        return false;
912
652
    }
913
652
    static void AssertModResult(const component::Bignum& result, const component::Bignum& mod, const std::string& opStr) {
914
652
        if ( IsEqualOrLargerThan(result, mod) ) {
915
0
            Abort("Result is equal to or larger than modulo", opStr);
916
0
        }
917
652
    }
918
114
    static void AssertNotSmallerThan(const component::Bignum& result, const component::Bignum& A, const std::string& opStr) {
919
114
        if ( SmallerThan(result, A) ) {
920
0
            Abort("Result is smaller than the input", opStr);
921
0
        }
922
114
    }
923
    static void AssertNotSmallerThan(
924
            const component::Bignum& result,
925
            const component::Bignum& A,
926
            const component::Bignum& B,
927
133
            const std::string& opStr) {
928
133
        if ( SmallerThan(result, A) && SmallerThan(result, B) ) {
929
0
            Abort("Result is smaller than the input", opStr);
930
0
        }
931
133
    }
932
228
    static void AssertNotLargerThan(const component::Bignum& result, const component::Bignum& A, const std::string& opStr) {
933
228
        if ( LargerThan(result, A) ) {
934
0
            Abort("Result is larger than the input", opStr);
935
0
        }
936
228
    }
937
    static void AssertNotLargerThan(
938
            const component::Bignum& result,
939
            const component::Bignum& A,
940
            const component::Bignum& B,
941
131
            const std::string& opStr) {
942
131
        if ( LargerThan(result, A) && LargerThan(result, B) ) {
943
0
            Abort("Result is larger than the input", opStr);
944
0
        }
945
131
    }
946
}
947
948
12.7k
void test(const operation::BignumCalc& op, const std::optional<component::Bignum>& result) {
949
12.7k
    if ( result == std::nullopt ) {
950
9.14k
        return;
951
9.14k
    }
952
953
3.59k
    using namespace BignumCalc;
954
955
3.59k
    const auto calcOp = op.calcOp.Get();
956
957
    /* Negative numbers are not supported yet */
958
3.59k
    if (    op.bn0.IsNegative() ||
959
3.59k
            op.bn1.IsNegative() ||
960
3.59k
            op.bn2.IsNegative() ) {
961
0
        return;
962
0
    }
963
964
    /* Modular calculations are not supported yet */
965
3.59k
    if ( op.modulo != std::nullopt ) {
966
667
        return;
967
667
    }
968
969
2.93k
    switch ( calcOp ) {
970
17
        case    CF_CALCOP("Add(A,B)"):
971
17
            if (    SmallerThan(*result, op.bn0) ||
972
17
                    SmallerThan(*result, op.bn1) ) {
973
0
                Abort("Result is smaller than its operands", repository::CalcOpToString(calcOp));
974
0
            }
975
17
            break;
976
106
        case    CF_CALCOP("Div(A,B)"):
977
106
            if ( IsZero(op.bn1) ) {
978
0
                Abort("Division by zero should not produce a result", repository::CalcOpToString(calcOp));
979
0
            }
980
981
106
            if ( LargerThan(*result, op.bn0) ) {
982
0
                Abort("Result is larger than the dividend", repository::CalcOpToString(calcOp));
983
0
            }
984
106
            break;
985
26
        case    CF_CALCOP("Mul(A,B)"):
986
26
            if ( IsZero(op.bn0) || IsZero(op.bn1) ) {
987
13
                if ( !IsZero(*result) ) {
988
0
                    Abort("Result of Mul with zero operand is not zero", repository::CalcOpToString(calcOp));
989
0
                }
990
13
            }
991
26
            break;
992
109
        case    CF_CALCOP("Mod(A,B)"):
993
109
            BignumCalc::AssertModResult(*result, op.bn1, "Mod");
994
109
            break;
995
383
        case    CF_CALCOP("ExpMod(A,B,C)"):
996
383
            BignumCalc::AssertModResult(*result, op.bn2, "ExpMod");
997
383
            break;
998
37
        case    CF_CALCOP("AddMod(A,B,C)"):
999
37
            BignumCalc::AssertModResult(*result, op.bn2, "AddMod");
1000
37
            break;
1001
44
        case    CF_CALCOP("SubMod(A,B,C)"):
1002
44
            BignumCalc::AssertModResult(*result, op.bn2, "SubMod");
1003
44
            break;
1004
34
        case    CF_CALCOP("MulMod(A,B,C)"):
1005
34
            BignumCalc::AssertModResult(*result, op.bn2, "MulMod");
1006
34
            break;
1007
45
        case    CF_CALCOP("SqrMod(A,B)"):
1008
45
            BignumCalc::AssertModResult(*result, op.bn1, "SqrMod");
1009
45
            break;
1010
0
        case    CF_CALCOP("SqrtMod(A,B)"):
1011
0
            BignumCalc::AssertModResult(*result, op.bn1, "SqrtMod");
1012
0
            break;
1013
0
        case    CF_CALCOP("ModLShift(A,B,C)"):
1014
0
            BignumCalc::AssertModResult(*result, op.bn2, "ModLShift");
1015
0
            break;
1016
21
        case    CF_CALCOP("Bit(A,B)"):
1017
21
            BignumCalc::AssertBinary(*result, "Bit");
1018
21
            break;
1019
0
        case    CF_CALCOP("IsCoprime(A,B)"):
1020
0
            BignumCalc::AssertBinary(*result, "IsCoprime");
1021
0
            break;
1022
28
        case    CF_CALCOP("IsEq(A,B)"):
1023
28
            BignumCalc::AssertBinary(*result, "IsEq");
1024
28
            break;
1025
11
        case    CF_CALCOP("IsGt(A,B)"):
1026
11
            BignumCalc::AssertBinary(*result, "IsGt");
1027
11
            break;
1028
10
        case    CF_CALCOP("IsGte(A,B)"):
1029
10
            BignumCalc::AssertBinary(*result, "IsGte");
1030
10
            break;
1031
10
        case    CF_CALCOP("IsLt(A,B)"):
1032
10
            BignumCalc::AssertBinary(*result, "IsLt");
1033
10
            break;
1034
2
        case    CF_CALCOP("IsLte(A,B)"):
1035
2
            BignumCalc::AssertBinary(*result, "IsLte");
1036
2
            break;
1037
10
        case    CF_CALCOP("IsEven(A)"):
1038
10
            BignumCalc::AssertBinary(*result, "IsEven");
1039
10
            break;
1040
6
        case    CF_CALCOP("IsOdd(A)"):
1041
6
            BignumCalc::AssertBinary(*result, "IsOdd");
1042
6
            break;
1043
11
        case    CF_CALCOP("IsOne(A)"):
1044
11
            BignumCalc::AssertBinary(*result, "IsOne");
1045
11
            break;
1046
0
        case    CF_CALCOP("IsPow2(A)"):
1047
0
            BignumCalc::AssertBinary(*result, "IsPow2");
1048
0
            break;
1049
168
        case    CF_CALCOP("IsPrime(A)"):
1050
168
            BignumCalc::AssertBinary(*result, "IsPrime");
1051
168
            break;
1052
12
        case    CF_CALCOP("IsZero(A)"):
1053
12
            BignumCalc::AssertBinary(*result, "IsZero");
1054
12
            break;
1055
0
        case    CF_CALCOP("IsSquare(A)"):
1056
0
            BignumCalc::AssertBinary(*result, "IsSquare");
1057
0
            break;
1058
0
        case    CF_CALCOP("IsPower(A)"):
1059
0
            BignumCalc::AssertBinary(*result, "IsPower");
1060
0
            break;
1061
10
        case    CF_CALCOP("IsNeg(A)"):
1062
10
            BignumCalc::AssertBinary(*result, "IsNeg");
1063
10
            break;
1064
11
        case    CF_CALCOP("IsNotZero(A)"):
1065
11
            BignumCalc::AssertBinary(*result, "IsNotZero");
1066
11
            break;
1067
16
        case    CF_CALCOP("Cmp(A,B)"):
1068
16
            BignumCalc::AssertTertiary(*result, "Cmp");
1069
16
            break;
1070
38
        case    CF_CALCOP("CmpAbs(A,B)"):
1071
38
            BignumCalc::AssertTertiary(*result, "CmpAbs");
1072
38
            break;
1073
56
        case    CF_CALCOP("Jacobi(A,B)"):
1074
56
            BignumCalc::AssertTertiary(*result, "Jacobi");
1075
56
            break;
1076
14
        case    CF_CALCOP("Sqr(A)"):
1077
14
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1078
14
            break;
1079
64
        case    CF_CALCOP("RShift(A,B)"):
1080
64
            if ( IsZero(op.bn0) || IsZero(op.bn1) ) {
1081
54
                if ( op.bn0.ToTrimmedString() != result->ToTrimmedString() ) {
1082
0
                    Abort("Zero operand should not alter input", repository::CalcOpToString(calcOp));
1083
0
                }
1084
54
            }
1085
1086
64
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1087
64
            break;
1088
31
        case    CF_CALCOP("LShift1(A)"):
1089
31
            if ( IsZero(op.bn0) ) {
1090
21
                if ( op.bn0.ToTrimmedString() != result->ToTrimmedString() ) {
1091
0
                    Abort("Zero input should remain zero", repository::CalcOpToString(calcOp));
1092
0
                }
1093
21
            }
1094
1095
31
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1096
31
            break;
1097
33
        case    CF_CALCOP("SetBit(A,B)"):
1098
33
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1099
33
            break;
1100
32
        case    CF_CALCOP("ClearBit(A,B)"):
1101
32
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1102
32
            break;
1103
26
        case    CF_CALCOP("Sqrt(A)"):
1104
26
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1105
26
            break;
1106
0
        case    CF_CALCOP("Cbrt(A)"):
1107
0
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1108
0
            break;
1109
36
        case    CF_CALCOP("MulAdd(A,B,C)"):
1110
36
            AssertNotSmallerThan(*result, op.bn2, repository::CalcOpToString(calcOp));
1111
36
            break;
1112
0
        case    CF_CALCOP("Min(A,B)"):
1113
0
        case    CF_CALCOP("Max(A,B)"):
1114
0
            if ( !IsEqual(*result, op.bn0) && !IsEqual(*result, op.bn1) ) {
1115
0
                Abort("Result is not an operand", repository::CalcOpToString(calcOp));
1116
0
            }
1117
0
            break;
1118
0
        case    CF_CALCOP("Mask(A,B)"):
1119
0
            if ( LargerThan(*result, op.bn0) ) {
1120
0
                Abort("Result is larger than input", repository::CalcOpToString(calcOp));
1121
0
            }
1122
0
            break;
1123
0
        case    CF_CALCOP("And(A,B)"):
1124
0
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1125
0
            AssertNotLargerThan(*result, op.bn1, repository::CalcOpToString(calcOp));
1126
0
            break;
1127
0
        case    CF_CALCOP("Or(A,B)"):
1128
0
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1129
0
            AssertNotSmallerThan(*result, op.bn1, repository::CalcOpToString(calcOp));
1130
0
            break;
1131
0
        case    CF_CALCOP("Nthrt(A,B)"):
1132
0
        case    CF_CALCOP("NthrtRem(A,B)"):
1133
0
            if ( IsZero(op.bn1) ) {
1134
0
                Abort("Root of zero should not produce a result", repository::CalcOpToString(calcOp));
1135
0
            }
1136
0
            break;
1137
0
        case    CF_CALCOP("Zero()"):
1138
0
            if ( !IsZero(*result) ) {
1139
0
                Abort("Result should be zero", repository::CalcOpToString(calcOp));
1140
0
            }
1141
0
            break;
1142
131
        case    CF_CALCOP("GCD(A,B)"):
1143
131
            AssertNotLargerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp));
1144
131
            break;
1145
133
        case    CF_CALCOP("LCM(A,B)"):
1146
133
            AssertNotSmallerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp));
1147
133
            break;
1148
162
        case    CF_CALCOP("InvMod(A,B)"):
1149
162
            if ( !IsZero(*result) ) {
1150
106
                AssertNotLargerThan(*result, op.bn1, repository::CalcOpToString(calcOp));
1151
106
            }
1152
162
            break;
1153
0
        case    CF_CALCOP("Exp(A,B)"):
1154
0
            AssertNotSmallerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp));
1155
0
            break;
1156
2.93k
    }
1157
2.93k
}
1158
1159
197
void test(const operation::BignumCalc_Fp2& op, const std::optional<component::Fp2>& result) {
1160
197
    (void)op;
1161
197
    (void)result;
1162
197
}
1163
1164
636
void test(const operation::BignumCalc_Fp12& op, const std::optional<component::Fp12>& result) {
1165
636
    (void)op;
1166
636
    (void)result;
1167
636
}
1168
1169
} /* namespace tests */
1170
} /* namespace cryptofuzz */