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