Coverage Report

Created: 2023-09-25 06:34

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