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
13.2k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
13.2k
    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
13.2k
}
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
779
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
779
    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
779
}
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
6.08k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
6.08k
    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
6.08k
}
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
662
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
662
    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
662
}
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
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_PBKDF1>(cryptofuzz::operation::KDF_PBKDF1 const&, std::__1::optional<cryptofuzz::Buffer> const&)
Line
Count
Source
12
491
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
491
    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
491
}
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.74k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
1.74k
    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.74k
}
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
642
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
642
    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
642
}
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
406
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
406
    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
406
}
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
349
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
349
    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
349
}
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
147
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
147
    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
147
}
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.53k
void verifyKeySize(const OperationType& op, const ResultType& result) {
13
1.53k
    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.53k
}
18
19
3.72k
static void checkZeroResult(const std::optional<Buffer>& b) {
20
3.72k
    if ( b == std::nullopt ) {
21
0
        return;
22
0
    }
23
24
3.72k
    if ( b->GetSize() >= 16 ) {
25
3.18k
        const std::vector<uint8_t> zeroes(b->GetSize(), 0);
26
3.18k
        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
3.18k
    }
31
3.72k
}
32
33
6.16k
void test(const operation::Digest& op, const std::optional<component::Digest>& result) {
34
6.16k
    if ( result == std::nullopt ) {
35
3.71k
        return;
36
3.71k
    }
37
38
2.45k
    {
39
2.45k
        const auto expectedSize = repository::DigestSize(op.digestType.Get());
40
41
2.45k
        if ( expectedSize != std::nullopt ) {
42
2.34k
            if ( result->GetSize() != *expectedSize ) {
43
0
                printf("Expected vs actual digest size: %zu / %zu\n", *expectedSize, result->GetSize());
44
0
                abort();
45
0
            }
46
2.34k
        }
47
2.45k
    }
48
49
2.45k
    checkZeroResult(result);
50
2.45k
}
51
52
3.06k
void test(const operation::HMAC& op, const std::optional<component::MAC>& result) {
53
3.06k
    if ( result == std::nullopt ) {
54
1.79k
        return;
55
1.79k
    }
56
57
1.26k
    {
58
1.26k
        const auto expectedSize = repository::DigestSize(op.digestType.Get());
59
60
1.26k
        if ( expectedSize != std::nullopt ) {
61
973
            if ( result->GetSize() != *expectedSize ) {
62
0
                printf("Expected vs actual digest size: %zu / %zu\n", *expectedSize, result->GetSize());
63
0
                abort();
64
0
            }
65
973
        }
66
1.26k
    }
67
68
1.26k
    checkZeroResult(result);
69
1.26k
}
70
71
3.19k
void test(const operation::UMAC& op, const std::optional<component::MAC>& result) {
72
3.19k
    if ( result == std::nullopt ) {
73
1.57k
        return;
74
1.57k
    }
75
76
1.62k
    if (
77
1.62k
            ( op.type == 0 && result->GetSize() > (32/8) ) ||
78
1.62k
            ( op.type == 1 && result->GetSize() > (64/8) ) ||
79
1.62k
            ( op.type == 2 && result->GetSize() > (96/8) ) ||
80
1.62k
            ( op.type == 3 && result->GetSize() > (128/8) )
81
1.62k
    ) {
82
0
        printf("UMAC: Overlong result: %zu\n", result->GetSize());
83
0
        abort();
84
0
    }
85
1.62k
}
86
87
15.6k
static void test_ChaCha20_Poly1305_IV(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
88
15.6k
    using fuzzing::datasource::ID;
89
90
    /*
91
     * OpenSSL CVE-2019-1543
92
     * https://www.openssl.org/news/secadv/20190306.txt
93
     */
94
95
15.6k
    if ( op.cipher.cipherType.Get() != CF_CIPHER("CHACHA20_POLY1305") ) {
96
15.2k
        return;
97
15.2k
    }
98
99
388
    if ( result == std::nullopt ) {
100
275
        return;
101
275
    }
102
103
113
    if ( op.cipher.iv.GetSize() > 12 ) {
104
0
        abort();
105
0
    }
106
113
}
107
108
15.6k
static void test_XChaCha20_Poly1305_IV(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
109
15.6k
    using fuzzing::datasource::ID;
110
111
15.6k
    if ( op.cipher.cipherType.Get() != CF_CIPHER("XCHACHA20_POLY1305") ) {
112
15.5k
        return;
113
15.5k
    }
114
115
72
    if ( result == std::nullopt ) {
116
72
        return;
117
72
    }
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
15.6k
static void test_AES_CCM_Wycheproof(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
126
15.6k
    bool fail = false;
127
128
15.6k
    if ( result == std::nullopt ) {
129
10.1k
        return;
130
10.1k
    }
131
132
5.42k
    switch ( op.cipher.cipherType.Get() ) {
133
72
        case CF_CIPHER("AES_128_CCM"):
134
142
        case CF_CIPHER("AES_192_CCM"):
135
183
        case CF_CIPHER("AES_256_CCM"):
136
183
            break;
137
5.24k
        default:
138
5.24k
            return;
139
5.42k
    }
140
141
183
    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
183
    if ( result->tag != std::nullopt ) {
147
183
        static const std::vector<size_t> validTagSizes = {4, 6, 8, 10, 12, 14, 16};
148
149
183
        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
183
    }
154
155
183
    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
183
}
160
161
15.6k
static void test_AES_GCM_Wycheproof(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
162
15.6k
    bool fail = false;
163
164
15.6k
    if ( result == std::nullopt ) {
165
10.1k
        return;
166
10.1k
    }
167
168
5.42k
    switch ( op.cipher.cipherType.Get() ) {
169
119
        case CF_CIPHER("AES_128_GCM"):
170
306
        case CF_CIPHER("AES_192_GCM"):
171
399
        case CF_CIPHER("AES_256_GCM"):
172
399
            break;
173
5.03k
        default:
174
5.03k
            return;
175
5.42k
    }
176
177
399
    if ( op.cipher.iv.GetSize() == 0 ) {
178
0
        printf("AES GCM: Invalid IV size\n");
179
0
        fail = true;
180
0
    }
181
182
399
    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
399
}
187
188
15.6k
void test(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) {
189
15.6k
    test_ChaCha20_Poly1305_IV(op, result);
190
15.6k
    test_XChaCha20_Poly1305_IV(op, result);
191
15.6k
    test_AES_CCM_Wycheproof(op, result);
192
15.6k
    test_AES_GCM_Wycheproof(op, result);
193
15.6k
}
194
195
12.1k
void test(const operation::SymmetricDecrypt& op, const std::optional<component::Cleartext>& result) {
196
12.1k
    (void)op;
197
12.1k
    (void)result;
198
12.1k
}
199
200
3.47k
void test(const operation::CMAC& op, const std::optional<component::MAC>& result) {
201
3.47k
    (void)op;
202
3.47k
    (void)result;
203
3.47k
}
204
205
779
void test(const operation::KDF_SCRYPT& op, const std::optional<component::Key>& result) {
206
779
    verifyKeySize(op, result);
207
779
}
208
209
6.08k
static void test_HKDF_OutputSize(const operation::KDF_HKDF& op, const std::optional<component::Key>& result) {
210
6.08k
    if ( result == std::nullopt ) {
211
3.64k
        return;
212
3.64k
    }
213
214
2.44k
    const auto expectedSize = repository::DigestSize(op.digestType.Get());
215
216
2.44k
    if ( expectedSize == std::nullopt ) {
217
226
        return;
218
226
    }
219
220
2.21k
    const size_t maxOutputSize = 255 * *expectedSize;
221
222
2.21k
    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
2.21k
}
227
228
6.08k
void test(const operation::KDF_HKDF& op, const std::optional<component::Key>& result) {
229
6.08k
    verifyKeySize(op, result);
230
231
6.08k
    test_HKDF_OutputSize(op, result);
232
6.08k
}
233
234
662
void test(const operation::KDF_TLS1_PRF& op, const std::optional<component::Key>& result) {
235
662
    verifyKeySize(op, result);
236
662
}
237
238
414
void test(const operation::KDF_PBKDF& op, const std::optional<component::Key>& result) {
239
414
    verifyKeySize(op, result);
240
414
}
241
242
491
void test(const operation::KDF_PBKDF1& op, const std::optional<component::Key>& result) {
243
491
    verifyKeySize(op, result);
244
491
}
245
246
1.74k
void test(const operation::KDF_PBKDF2& op, const std::optional<component::Key>& result) {
247
1.74k
    verifyKeySize(op, result);
248
1.74k
}
249
250
642
void test(const operation::KDF_ARGON2& op, const std::optional<component::Key>& result) {
251
642
    verifyKeySize(op, result);
252
642
}
253
254
406
void test(const operation::KDF_SSH& op, const std::optional<component::Key>& result) {
255
406
    verifyKeySize(op, result);
256
406
}
257
258
349
void test(const operation::KDF_X963& op, const std::optional<component::Key>& result) {
259
349
    verifyKeySize(op, result);
260
349
}
261
262
147
void test(const operation::KDF_BCRYPT& op, const std::optional<component::Key>& result) {
263
147
    verifyKeySize(op, result);
264
147
}
265
266
1.53k
void test(const operation::KDF_SP_800_108& op, const std::optional<component::Key>& result) {
267
1.53k
    verifyKeySize(op, result);
268
1.53k
}
269
270
1.47k
static bool IsSpecialCurve(const uint64_t curveID) {
271
1.47k
    switch ( curveID ) {
272
0
        case CF_ECC_CURVE("ed448"):
273
287
        case CF_ECC_CURVE("ed25519"):
274
287
        case CF_ECC_CURVE("x25519"):
275
287
        case CF_ECC_CURVE("x448"):
276
287
            return true;
277
1.18k
        default:
278
1.18k
            return false;
279
1.47k
    }
280
1.47k
}
281
282
1.60k
static void test_ECC_PrivateKey(const uint64_t curveID, const std::string priv) {
283
    /* Disabled until all modules comply by default */
284
1.60k
    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
2.51k
void test(const operation::ECC_PrivateToPublic& op, const std::optional<component::ECC_PublicKey>& result) {
299
2.51k
    if ( result != std::nullopt ) {
300
519
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
301
519
    }
302
2.51k
}
303
304
527
void test(const operation::ECC_ValidatePubkey& op, const std::optional<bool>& result) {
305
527
    (void)op;
306
527
    (void)result;
307
527
}
308
309
1.48k
void test(const operation::ECC_GenerateKeyPair& op, const std::optional<component::ECC_KeyPair>& result) {
310
1.48k
    if ( result != std::nullopt ) {
311
272
        test_ECC_PrivateKey(op.curveType.Get(), result->priv.ToTrimmedString());
312
272
    }
313
1.48k
}
314
315
1.45k
static void test_ECDSA_Signature(const uint64_t curveID, const std::string R, const std::string S) {
316
1.45k
    if ( IsSpecialCurve(curveID) ) {
317
263
        return;
318
263
    }
319
320
1.18k
    const boost::multiprecision::cpp_int r(R), s(S);
321
322
1.18k
    if ( r < 1 ) {
323
0
        std::cout << "ECDSA signature invalid: R < 1" << std::endl;
324
0
        ::abort();
325
0
    }
326
1.18k
    if ( s < 1 ) {
327
0
        std::cout << "ECDSA signature invalid: S < 1" << std::endl;
328
0
        ::abort();
329
0
    }
330
331
1.18k
    const auto O = cryptofuzz::repository::ECC_CurveToOrder(curveID);
332
1.18k
    if ( O == std::nullopt ) {
333
140
        return;
334
140
    }
335
336
1.04k
    const boost::multiprecision::cpp_int o(*O);
337
338
1.04k
    if ( r >= o ) {
339
0
        std::cout << "ECDSA signature invalid: R >= order" << std::endl;
340
0
        ::abort();
341
0
    }
342
343
1.04k
    if ( s >= o ) {
344
0
        std::cout << "ECDSA signature invalid: S >= order" << std::endl;
345
0
        ::abort();
346
0
    }
347
1.04k
}
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
186
void test(const operation::ECCSI_Sign& op, const std::optional<component::ECCSI_Signature>& result) {
375
186
    (void)op;
376
186
    (void)result;
377
186
}
378
1.62k
void test(const operation::ECDSA_Sign& op, const std::optional<component::ECDSA_Signature>& result) {
379
1.62k
    if ( result != std::nullopt ) {
380
741
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
381
382
741
        if (
383
741
                op.UseSpecifiedNonce() == true &&
384
741
                !IsSpecialCurve(op.curveType.Get()) &&
385
741
                op.nonce.ToTrimmedString() == "0"
386
741
           ) {
387
0
            std::cout << "0 is an invalid ECDSA nonce" << std::endl;
388
0
            ::abort();
389
0
        }
390
391
741
        test_ECDSA_Signature(op.curveType.Get(),
392
741
                result->signature.first.ToTrimmedString(),
393
741
                result->signature.second.ToTrimmedString());
394
741
    }
395
1.62k
}
396
397
508
void test(const operation::ECGDSA_Sign& op, const std::optional<component::ECGDSA_Signature>& result) {
398
508
    if ( result != std::nullopt ) {
399
68
        test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString());
400
401
68
        if (
402
68
                op.UseSpecifiedNonce() == true &&
403
68
                !IsSpecialCurve(op.curveType.Get()) &&
404
68
                op.nonce.ToTrimmedString() == "0"
405
68
           ) {
406
0
            std::cout << "0 is an invalid ECGDSA nonce" << std::endl;
407
0
            ::abort();
408
0
        }
409
410
68
        test_ECDSA_Signature(op.curveType.Get(),
411
68
                result->signature.first.ToTrimmedString(),
412
68
                result->signature.second.ToTrimmedString());
413
68
    }
414
508
}
415
416
149
void test(const operation::ECRDSA_Sign& op, const std::optional<component::ECRDSA_Signature>& result) {
417
149
    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
149
}
434
435
181
void test(const operation::Schnorr_Sign& op, const std::optional<component::Schnorr_Signature>& result) {
436
181
    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
181
}
453
454
144
void test(const operation::ECCSI_Verify& op, const std::optional<bool>& result) {
455
144
    (void)op;
456
144
    (void)result;
457
144
}
458
459
863
void test(const operation::ECDSA_Verify& op, const std::optional<bool>& result) {
460
863
    if ( result != std::nullopt && *result == true ) {
461
32
        test_ECDSA_Signature(op.curveType.Get(),
462
32
                op.signature.signature.first.ToTrimmedString(),
463
32
                op.signature.signature.second.ToTrimmedString());
464
32
    }
465
863
}
466
467
354
void test(const operation::ECGDSA_Verify& op, const std::optional<bool>& result) {
468
354
    if ( result != std::nullopt && *result == true ) {
469
10
        test_ECDSA_Signature(op.curveType.Get(),
470
10
                op.signature.signature.first.ToTrimmedString(),
471
10
                op.signature.signature.second.ToTrimmedString());
472
10
    }
473
354
}
474
475
113
void test(const operation::ECRDSA_Verify& op, const std::optional<bool>& result) {
476
113
    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
113
}
482
483
135
void test(const operation::Schnorr_Verify& op, const std::optional<bool>& result) {
484
135
    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
135
}
490
491
1.47k
void test(const operation::ECDSA_Recover& op, const std::optional<component::ECC_PublicKey>& result) {
492
1.47k
    if ( result != std::nullopt ) {
493
601
        if ( op.id > 3 ) {
494
0
            std::cout << "Invalid recovery ID" << std::endl;
495
0
            ::abort();
496
0
        }
497
601
    }
498
1.47k
    if ( result != std::nullopt ) {
499
601
        test_ECDSA_Signature(op.curveType.Get(),
500
601
                op.signature.first.ToTrimmedString(),
501
601
                op.signature.second.ToTrimmedString());
502
601
    }
503
1.47k
}
504
505
176
void test(const operation::DSA_Verify& op, const std::optional<bool>& result) {
506
176
    (void)op;
507
508
176
    if ( result == std::nullopt || *result == false ) {
509
176
        return;
510
176
    }
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
174
void test(const operation::DSA_Sign& op, const std::optional<component::DSA_Signature>& result) {
534
174
    if ( result == std::nullopt ) {
535
174
        return;
536
174
    }
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
122
void test(const operation::DSA_GenerateParameters& op, const std::optional<component::DSA_Parameters>& result) {
588
122
    (void)op;
589
590
122
    if ( result == std::nullopt ) {
591
122
        return;
592
122
    }
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
139
void test(const operation::DSA_PrivateToPublic& op, const std::optional<component::Bignum>& result) {
639
139
    (void)op;
640
139
    (void)result;
641
139
}
642
643
177
void test(const operation::DSA_GenerateKeyPair& op, const std::optional<component::DSA_KeyPair>& result) {
644
177
    if ( result == std::nullopt ) {
645
177
        return;
646
177
    }
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
129
void test(const operation::ECDH_Derive& op, const std::optional<component::Secret>& result) {
661
129
    (void)op;
662
129
    (void)result;
663
129
}
664
665
104
void test(const operation::ECIES_Encrypt& op, const std::optional<component::Ciphertext>& result) {
666
    /* TODO check minimum size? */
667
104
    (void)op;
668
104
    (void)result;
669
104
}
670
671
147
void test(const operation::ECIES_Decrypt& op, const std::optional<component::Cleartext>& result) {
672
147
    (void)op;
673
147
    (void)result;
674
147
}
675
676
268
void test(const operation::ECC_Point_Add& op, const std::optional<component::ECC_Point>& result) {
677
268
    (void)op;
678
268
    (void)result;
679
268
}
680
681
2.00k
void test(const operation::ECC_Point_Mul& op, const std::optional<component::ECC_Point>& result) {
682
2.00k
    (void)op;
683
2.00k
    (void)result;
684
2.00k
}
685
686
218
void test(const operation::ECC_Point_Neg& op, const std::optional<component::ECC_Point>& result) {
687
218
    (void)op;
688
218
    (void)result;
689
218
}
690
691
218
void test(const operation::ECC_Point_Dbl& op, const std::optional<component::ECC_Point>& result) {
692
218
    (void)op;
693
218
    (void)result;
694
218
}
695
696
265
void test(const operation::ECC_Point_Cmp& op, const std::optional<bool>& result) {
697
265
    (void)op;
698
265
    (void)result;
699
265
}
700
701
175
void test(const operation::DH_GenerateKeyPair& op, const std::optional<component::DH_KeyPair>& result) {
702
175
    (void)op;
703
175
    (void)result;
704
175
}
705
706
616
void test(const operation::DH_Derive& op, const std::optional<component::Bignum>& result) {
707
616
    (void)op;
708
616
    (void)result;
709
616
}
710
711
150
void test(const operation::BLS_PrivateToPublic& op, const std::optional<component::BLS_PublicKey>& result) {
712
150
    (void)op;
713
150
    (void)result;
714
150
}
715
716
147
void test(const operation::BLS_PrivateToPublic_G2& op, const std::optional<component::G2>& result) {
717
147
    (void)op;
718
147
    (void)result;
719
147
}
720
721
155
void test(const operation::BLS_Sign& op, const std::optional<component::BLS_Signature>& result) {
722
155
    (void)op;
723
155
    (void)result;
724
155
}
725
726
164
void test(const operation::BLS_Verify& op, const std::optional<bool>& result) {
727
164
    (void)op;
728
164
    (void)result;
729
164
}
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
159
void test(const operation::BLS_BatchVerify& op, const std::optional<bool>& result) {
737
159
    (void)op;
738
159
    (void)result;
739
159
}
740
741
152
void test(const operation::BLS_Aggregate_G1& op, const std::optional<component::G1>& result) {
742
152
    (void)op;
743
152
    (void)result;
744
152
}
745
746
121
void test(const operation::BLS_Aggregate_G2& op, const std::optional<component::G2>& result) {
747
121
    (void)op;
748
121
    (void)result;
749
121
}
750
751
101
void test(const operation::BLS_Pairing& op, const std::optional<component::Fp12>& result) {
752
101
    (void)op;
753
101
    (void)result;
754
101
}
755
756
121
void test(const operation::BLS_MillerLoop& op, const std::optional<component::Fp12>& result) {
757
121
    (void)op;
758
121
    (void)result;
759
121
}
760
761
174
void test(const operation::BLS_FinalExp& op, const std::optional<component::Fp12>& result) {
762
174
    (void)op;
763
174
    (void)result;
764
174
}
765
766
134
void test(const operation::BLS_HashToG1& op, const std::optional<component::G1>& result) {
767
134
    (void)op;
768
134
    (void)result;
769
134
}
770
771
131
void test(const operation::BLS_HashToG2& op, const std::optional<component::G2>& result) {
772
131
    (void)op;
773
131
    (void)result;
774
131
}
775
776
157
void test(const operation::BLS_MapToG1& op, const std::optional<component::G1>& result) {
777
157
    (void)op;
778
157
    (void)result;
779
157
}
780
781
135
void test(const operation::BLS_MapToG2& op, const std::optional<component::G2>& result) {
782
135
    (void)op;
783
135
    (void)result;
784
135
}
785
786
136
void test(const operation::BLS_IsG1OnCurve& op, const std::optional<bool>& result) {
787
136
    (void)op;
788
136
    (void)result;
789
136
}
790
791
177
void test(const operation::BLS_IsG2OnCurve& op, const std::optional<bool>& result) {
792
177
    (void)op;
793
177
    (void)result;
794
177
}
795
796
131
void test(const operation::BLS_GenerateKeyPair& op, const std::optional<component::BLS_KeyPair>& result) {
797
131
    (void)op;
798
131
    (void)result;
799
131
}
800
801
118
void test(const operation::BLS_Decompress_G1& op, const std::optional<component::G1>& result) {
802
118
    (void)op;
803
118
    (void)result;
804
118
}
805
806
119
void test(const operation::BLS_Compress_G1& op, const std::optional<component::Bignum>& result) {
807
119
    (void)op;
808
119
    (void)result;
809
119
}
810
811
104
void test(const operation::BLS_Decompress_G2& op, const std::optional<component::G2>& result) {
812
104
    (void)op;
813
104
    (void)result;
814
104
}
815
816
138
void test(const operation::BLS_Compress_G2& op, const std::optional<component::G1>& result) {
817
138
    (void)op;
818
138
    (void)result;
819
138
}
820
821
160
void test(const operation::BLS_G1_Add& op, const std::optional<component::G1>& result) {
822
160
    (void)op;
823
160
    (void)result;
824
160
}
825
826
134
void test(const operation::BLS_G1_Mul& op, const std::optional<component::G1>& result) {
827
134
    (void)op;
828
134
    (void)result;
829
134
}
830
831
163
void test(const operation::BLS_G1_IsEq& op, const std::optional<bool>& result) {
832
163
    (void)op;
833
163
    (void)result;
834
163
}
835
836
159
void test(const operation::BLS_G1_Neg& op, const std::optional<component::G1>& result) {
837
159
    (void)op;
838
159
    (void)result;
839
159
}
840
841
226
void test(const operation::BLS_G2_Add& op, const std::optional<component::G2>& result) {
842
226
    (void)op;
843
226
    (void)result;
844
226
}
845
846
189
void test(const operation::BLS_G2_Mul& op, const std::optional<component::G2>& result) {
847
189
    (void)op;
848
189
    (void)result;
849
189
}
850
851
223
void test(const operation::BLS_G2_IsEq& op, const std::optional<bool>& result) {
852
223
    (void)op;
853
223
    (void)result;
854
223
}
855
856
158
void test(const operation::BLS_G2_Neg& op, const std::optional<component::G2>& result) {
857
158
    (void)op;
858
158
    (void)result;
859
158
}
860
861
119
void test(const operation::Misc& op, const std::optional<Buffer>& result) {
862
119
    (void)op;
863
119
    (void)result;
864
119
}
865
866
131
void test(const operation::SR25519_Verify& op, const std::optional<bool>& result) {
867
131
    (void)op;
868
131
    (void)result;
869
131
}
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
368
    static void AssertBinary(const component::Bignum& result, const std::string& opStr) {
877
368
        const auto resultStr = result.ToTrimmedString();
878
368
        if ( !(resultStr == "0" || resultStr == "1") ) {
879
0
            Abort("Result must be 0 or 1", opStr);
880
0
        }
881
368
    }
882
99
    static void AssertTertiary(const component::Bignum& result, const std::string& opStr) {
883
99
        const auto resultStr = result.ToTrimmedString();
884
99
        if ( !(resultStr == "0" || resultStr == "1" || resultStr == "-1") ) {
885
0
            Abort("Result must be 0 or 1 or -1", opStr);
886
0
        }
887
99
    }
888
0
    static bool IsEqual(const component::Bignum& A, const component::Bignum& B) {
889
0
        return A.ToTrimmedString() == B.ToTrimmedString();
890
0
    }
891
626
    static bool IsZero(const component::Bignum& A) {
892
626
        return A.ToTrimmedString() == "0";
893
626
    }
894
362
    static bool SmallerThan(const component::Bignum& A, const component::Bignum& B) {
895
362
        return A.ToTrimmedString().size() < B.ToTrimmedString().size();
896
362
    }
897
548
    static bool LargerThan(const component::Bignum& A, const component::Bignum& B) {
898
548
        return A.ToTrimmedString().size() > B.ToTrimmedString().size();
899
548
    }
900
811
    static bool IsEqualOrLargerThan(const component::Bignum& A, const component::Bignum& B) {
901
811
        const auto a = A.ToTrimmedString();
902
811
        const auto b = B.ToTrimmedString();
903
811
        if ( a.size() > b.size() ) {
904
0
            return true;
905
0
        }
906
811
        if ( a.size() == b.size() ) {
907
629
            if ( a == b ) {
908
0
                return true;
909
0
            }
910
629
        }
911
811
        return false;
912
811
    }
913
811
    static void AssertModResult(const component::Bignum& result, const component::Bignum& mod, const std::string& opStr) {
914
811
        if ( IsEqualOrLargerThan(result, mod) ) {
915
0
            Abort("Result is equal to or larger than modulo", opStr);
916
0
        }
917
811
    }
918
150
    static void AssertNotSmallerThan(const component::Bignum& result, const component::Bignum& A, const std::string& opStr) {
919
150
        if ( SmallerThan(result, A) ) {
920
0
            Abort("Result is smaller than the input", opStr);
921
0
        }
922
150
    }
923
    static void AssertNotSmallerThan(
924
            const component::Bignum& result,
925
            const component::Bignum& A,
926
            const component::Bignum& B,
927
148
            const std::string& opStr) {
928
148
        if ( SmallerThan(result, A) && SmallerThan(result, B) ) {
929
0
            Abort("Result is smaller than the input", opStr);
930
0
        }
931
148
    }
932
278
    static void AssertNotLargerThan(const component::Bignum& result, const component::Bignum& A, const std::string& opStr) {
933
278
        if ( LargerThan(result, A) ) {
934
0
            Abort("Result is larger than the input", opStr);
935
0
        }
936
278
    }
937
    static void AssertNotLargerThan(
938
            const component::Bignum& result,
939
            const component::Bignum& A,
940
            const component::Bignum& B,
941
129
            const std::string& opStr) {
942
129
        if ( LargerThan(result, A) && LargerThan(result, B) ) {
943
0
            Abort("Result is larger than the input", opStr);
944
0
        }
945
129
    }
946
}
947
948
17.2k
void test(const operation::BignumCalc& op, const std::optional<component::Bignum>& result) {
949
17.2k
    if ( result == std::nullopt ) {
950
13.1k
        return;
951
13.1k
    }
952
953
4.12k
    using namespace BignumCalc;
954
955
4.12k
    const auto calcOp = op.calcOp.Get();
956
957
    /* Negative numbers are not supported yet */
958
4.12k
    if (    op.bn0.IsNegative() ||
959
4.12k
            op.bn1.IsNegative() ||
960
4.12k
            op.bn2.IsNegative() ) {
961
0
        return;
962
0
    }
963
964
    /* Modular calculations are not supported yet */
965
4.12k
    if ( op.modulo != std::nullopt ) {
966
760
        return;
967
760
    }
968
969
3.36k
    switch ( calcOp ) {
970
27
        case    CF_CALCOP("Add(A,B)"):
971
27
            if (    SmallerThan(*result, op.bn0) ||
972
27
                    SmallerThan(*result, op.bn1) ) {
973
0
                Abort("Result is smaller than its operands", repository::CalcOpToString(calcOp));
974
0
            }
975
27
            break;
976
130
        case    CF_CALCOP("Div(A,B)"):
977
130
            if ( IsZero(op.bn1) ) {
978
0
                Abort("Division by zero should not produce a result", repository::CalcOpToString(calcOp));
979
0
            }
980
981
130
            if ( LargerThan(*result, op.bn0) ) {
982
0
                Abort("Result is larger than the dividend", repository::CalcOpToString(calcOp));
983
0
            }
984
130
            break;
985
41
        case    CF_CALCOP("Mul(A,B)"):
986
41
            if ( IsZero(op.bn0) || IsZero(op.bn1) ) {
987
30
                if ( !IsZero(*result) ) {
988
0
                    Abort("Result of Mul with zero operand is not zero", repository::CalcOpToString(calcOp));
989
0
                }
990
30
            }
991
41
            break;
992
114
        case    CF_CALCOP("Mod(A,B)"):
993
114
            BignumCalc::AssertModResult(*result, op.bn1, "Mod");
994
114
            break;
995
440
        case    CF_CALCOP("ExpMod(A,B,C)"):
996
440
            BignumCalc::AssertModResult(*result, op.bn2, "ExpMod");
997
440
            break;
998
60
        case    CF_CALCOP("AddMod(A,B,C)"):
999
60
            BignumCalc::AssertModResult(*result, op.bn2, "AddMod");
1000
60
            break;
1001
84
        case    CF_CALCOP("SubMod(A,B,C)"):
1002
84
            BignumCalc::AssertModResult(*result, op.bn2, "SubMod");
1003
84
            break;
1004
62
        case    CF_CALCOP("MulMod(A,B,C)"):
1005
62
            BignumCalc::AssertModResult(*result, op.bn2, "MulMod");
1006
62
            break;
1007
51
        case    CF_CALCOP("SqrMod(A,B)"):
1008
51
            BignumCalc::AssertModResult(*result, op.bn1, "SqrMod");
1009
51
            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
30
        case    CF_CALCOP("Bit(A,B)"):
1017
30
            BignumCalc::AssertBinary(*result, "Bit");
1018
30
            break;
1019
0
        case    CF_CALCOP("IsCoprime(A,B)"):
1020
0
            BignumCalc::AssertBinary(*result, "IsCoprime");
1021
0
            break;
1022
11
        case    CF_CALCOP("IsEq(A,B)"):
1023
11
            BignumCalc::AssertBinary(*result, "IsEq");
1024
11
            break;
1025
10
        case    CF_CALCOP("IsGt(A,B)"):
1026
10
            BignumCalc::AssertBinary(*result, "IsGt");
1027
10
            break;
1028
19
        case    CF_CALCOP("IsGte(A,B)"):
1029
19
            BignumCalc::AssertBinary(*result, "IsGte");
1030
19
            break;
1031
11
        case    CF_CALCOP("IsLt(A,B)"):
1032
11
            BignumCalc::AssertBinary(*result, "IsLt");
1033
11
            break;
1034
13
        case    CF_CALCOP("IsLte(A,B)"):
1035
13
            BignumCalc::AssertBinary(*result, "IsLte");
1036
13
            break;
1037
15
        case    CF_CALCOP("IsEven(A)"):
1038
15
            BignumCalc::AssertBinary(*result, "IsEven");
1039
15
            break;
1040
10
        case    CF_CALCOP("IsOdd(A)"):
1041
10
            BignumCalc::AssertBinary(*result, "IsOdd");
1042
10
            break;
1043
10
        case    CF_CALCOP("IsOne(A)"):
1044
10
            BignumCalc::AssertBinary(*result, "IsOne");
1045
10
            break;
1046
0
        case    CF_CALCOP("IsPow2(A)"):
1047
0
            BignumCalc::AssertBinary(*result, "IsPow2");
1048
0
            break;
1049
205
        case    CF_CALCOP("IsPrime(A)"):
1050
205
            BignumCalc::AssertBinary(*result, "IsPrime");
1051
205
            break;
1052
14
        case    CF_CALCOP("IsZero(A)"):
1053
14
            BignumCalc::AssertBinary(*result, "IsZero");
1054
14
            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
10
        case    CF_CALCOP("IsNotZero(A)"):
1065
10
            BignumCalc::AssertBinary(*result, "IsNotZero");
1066
10
            break;
1067
16
        case    CF_CALCOP("Cmp(A,B)"):
1068
16
            BignumCalc::AssertTertiary(*result, "Cmp");
1069
16
            break;
1070
43
        case    CF_CALCOP("CmpAbs(A,B)"):
1071
43
            BignumCalc::AssertTertiary(*result, "CmpAbs");
1072
43
            break;
1073
40
        case    CF_CALCOP("Jacobi(A,B)"):
1074
40
            BignumCalc::AssertTertiary(*result, "Jacobi");
1075
40
            break;
1076
45
        case    CF_CALCOP("Sqr(A)"):
1077
45
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1078
45
            break;
1079
102
        case    CF_CALCOP("RShift(A,B)"):
1080
102
            if ( IsZero(op.bn0) || IsZero(op.bn1) ) {
1081
89
                if ( op.bn0.ToTrimmedString() != result->ToTrimmedString() ) {
1082
0
                    Abort("Zero operand should not alter input", repository::CalcOpToString(calcOp));
1083
0
                }
1084
89
            }
1085
1086
102
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1087
102
            break;
1088
32
        case    CF_CALCOP("LShift1(A)"):
1089
32
            if ( IsZero(op.bn0) ) {
1090
22
                if ( op.bn0.ToTrimmedString() != result->ToTrimmedString() ) {
1091
0
                    Abort("Zero input should remain zero", repository::CalcOpToString(calcOp));
1092
0
                }
1093
22
            }
1094
1095
32
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1096
32
            break;
1097
48
        case    CF_CALCOP("SetBit(A,B)"):
1098
48
            AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1099
48
            break;
1100
36
        case    CF_CALCOP("ClearBit(A,B)"):
1101
36
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1102
36
            break;
1103
20
        case    CF_CALCOP("Sqrt(A)"):
1104
20
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1105
20
            break;
1106
0
        case    CF_CALCOP("Cbrt(A)"):
1107
0
            AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp));
1108
0
            break;
1109
25
        case    CF_CALCOP("MulAdd(A,B,C)"):
1110
25
            AssertNotSmallerThan(*result, op.bn2, repository::CalcOpToString(calcOp));
1111
25
            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
129
        case    CF_CALCOP("GCD(A,B)"):
1143
129
            AssertNotLargerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp));
1144
129
            break;
1145
148
        case    CF_CALCOP("LCM(A,B)"):
1146
148
            AssertNotSmallerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp));
1147
148
            break;
1148
195
        case    CF_CALCOP("InvMod(A,B)"):
1149
195
            if ( !IsZero(*result) ) {
1150
120
                AssertNotLargerThan(*result, op.bn1, repository::CalcOpToString(calcOp));
1151
120
            }
1152
195
            break;
1153
0
        case    CF_CALCOP("Exp(A,B)"):
1154
0
            AssertNotSmallerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp));
1155
0
            break;
1156
3.36k
    }
1157
3.36k
}
1158
1159
218
void test(const operation::BignumCalc_Fp2& op, const std::optional<component::Fp2>& result) {
1160
218
    (void)op;
1161
218
    (void)result;
1162
218
}
1163
1164
728
void test(const operation::BignumCalc_Fp12& op, const std::optional<component::Fp12>& result) {
1165
728
    (void)op;
1166
728
    (void)result;
1167
728
}
1168
1169
} /* namespace tests */
1170
} /* namespace cryptofuzz */