/src/botan/src/fuzzer/mp_fuzzers.h
Line | Count | Source |
1 | | /* |
2 | | * (C) 2023 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #ifndef BOTAN_FUZZER_MP_HELPERS_H_ |
8 | | #define BOTAN_FUZZER_MP_HELPERS_H_ |
9 | | |
10 | | #include "fuzzers.h" |
11 | | |
12 | | #include <botan/internal/mp_core.h> |
13 | | #include <string_view> |
14 | | |
15 | | using Botan::word; |
16 | | |
17 | 0 | inline std::string format_word_vec(std::string_view name, const word x[], size_t x_len) { |
18 | 0 | std::ostringstream oss; |
19 | 0 | oss << name << " = "; |
20 | |
|
21 | 0 | constexpr size_t width = 2 * sizeof(word); |
22 | |
|
23 | 0 | for(size_t i = 0; i != x_len; ++i) { |
24 | 0 | oss << std::uppercase << std::setw(width) << std::setfill('0') << std::hex << x[i] << " "; |
25 | 0 | } |
26 | |
|
27 | 0 | oss << "\n"; |
28 | 0 | return oss.str(); |
29 | 0 | } |
30 | | |
31 | 0 | inline void dump_word_vec(std::string_view name, const word x[], size_t x_len) { |
32 | 0 | std::cerr << format_word_vec(name, x, x_len); |
33 | 0 | } |
34 | | |
35 | 1.02k | inline void compare_word_vec(const word x[], size_t x_len, const word y[], size_t y_len, const char* comparing) { |
36 | 1.02k | const size_t common_words = std::min(x_len, y_len); |
37 | | |
38 | 21.4k | for(size_t i = 0; i != common_words; ++i) { |
39 | 20.3k | if(x[i] != y[i]) { |
40 | 0 | dump_word_vec("x", x, x_len); |
41 | 0 | dump_word_vec("y", y, y_len); |
42 | 0 | FUZZER_WRITE_AND_CRASH("Comparison failed " << comparing); |
43 | 0 | } |
44 | 20.3k | } |
45 | | |
46 | | // all other words must be zero |
47 | 1.06k | for(size_t i = common_words; i != x_len; ++i) { |
48 | 39 | if(x[i] != 0) { |
49 | 0 | dump_word_vec("x", x, x_len); |
50 | 0 | dump_word_vec("y", y, y_len); |
51 | 0 | FUZZER_WRITE_AND_CRASH("Unexpected non-zero in high words of x " << comparing); |
52 | 0 | } |
53 | 39 | } |
54 | 4.46k | for(size_t i = common_words; i != y_len; ++i) { |
55 | 3.43k | if(y[i] != 0) { |
56 | 0 | dump_word_vec("x", x, x_len); |
57 | 0 | dump_word_vec("y", y, y_len); |
58 | 0 | FUZZER_WRITE_AND_CRASH("Unexpected non-zero in high words of y " << comparing); |
59 | 0 | } |
60 | 3.43k | } |
61 | 1.02k | } |
62 | | |
63 | | #endif |