Coverage Report

Created: 2024-11-29 06:10

/src/botan/src/fuzzer/mp_redc.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2023 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include "mp_fuzzers.h"
8
9
namespace {
10
11
template <size_t N>
12
12
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
12
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
12
   word z[2 * N] = {0};
16
17
12
   word z_script[2 * N] = {0};
18
12
   word z_ref[2 * N] = {0};
19
12
   word p[N] = {0};
20
12
   word p_dash = 0;
21
22
12
   word ws[2 * (N + 1)] = {0};
23
24
12
   std::memcpy(z, in.data(), sizeof(z));
25
12
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
12
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
372
   for(size_t i = 0; i != 2 * N; ++i) {
29
360
      z_script[i] = z_ref[i] = z[i];
30
360
   }
31
32
12
   if(N == 4) {
33
2
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
10
   } else if(N == 6) {
35
2
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
8
   } else if(N == 8) {
37
2
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
6
   } else if(N == 16) {
39
2
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
4
   } else if(N == 24) {
41
2
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
2
   } else if(N == 32) {
43
2
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
2
   } else {
45
0
      std::abort();
46
0
   }
47
48
12
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
372
   for(size_t i = 0; i != 2 * N; ++i) {
51
360
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
360
   }
60
12
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
12
}
mp_redc.cpp:void (anonymous namespace)::fuzz_mp_redc<4ul>(std::__1::span<unsigned char const, 18446744073709551615ul>)
Line
Count
Source
12
2
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
2
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
2
   word z[2 * N] = {0};
16
17
2
   word z_script[2 * N] = {0};
18
2
   word z_ref[2 * N] = {0};
19
2
   word p[N] = {0};
20
2
   word p_dash = 0;
21
22
2
   word ws[2 * (N + 1)] = {0};
23
24
2
   std::memcpy(z, in.data(), sizeof(z));
25
2
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
2
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
18
   for(size_t i = 0; i != 2 * N; ++i) {
29
16
      z_script[i] = z_ref[i] = z[i];
30
16
   }
31
32
2
   if(N == 4) {
33
2
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
2
   } else if(N == 6) {
35
0
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
0
   } else if(N == 8) {
37
0
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
0
   } else if(N == 16) {
39
0
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
0
   } else if(N == 24) {
41
0
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
0
   } else if(N == 32) {
43
0
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
0
   } else {
45
0
      std::abort();
46
0
   }
47
48
2
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
18
   for(size_t i = 0; i != 2 * N; ++i) {
51
16
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
16
   }
60
2
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
2
}
mp_redc.cpp:void (anonymous namespace)::fuzz_mp_redc<6ul>(std::__1::span<unsigned char const, 18446744073709551615ul>)
Line
Count
Source
12
2
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
2
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
2
   word z[2 * N] = {0};
16
17
2
   word z_script[2 * N] = {0};
18
2
   word z_ref[2 * N] = {0};
19
2
   word p[N] = {0};
20
2
   word p_dash = 0;
21
22
2
   word ws[2 * (N + 1)] = {0};
23
24
2
   std::memcpy(z, in.data(), sizeof(z));
25
2
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
2
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
26
   for(size_t i = 0; i != 2 * N; ++i) {
29
24
      z_script[i] = z_ref[i] = z[i];
30
24
   }
31
32
2
   if(N == 4) {
33
0
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
2
   } else if(N == 6) {
35
2
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
2
   } else if(N == 8) {
37
0
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
0
   } else if(N == 16) {
39
0
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
0
   } else if(N == 24) {
41
0
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
0
   } else if(N == 32) {
43
0
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
0
   } else {
45
0
      std::abort();
46
0
   }
47
48
2
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
26
   for(size_t i = 0; i != 2 * N; ++i) {
51
24
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
24
   }
60
2
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
2
}
mp_redc.cpp:void (anonymous namespace)::fuzz_mp_redc<8ul>(std::__1::span<unsigned char const, 18446744073709551615ul>)
Line
Count
Source
12
2
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
2
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
2
   word z[2 * N] = {0};
16
17
2
   word z_script[2 * N] = {0};
18
2
   word z_ref[2 * N] = {0};
19
2
   word p[N] = {0};
20
2
   word p_dash = 0;
21
22
2
   word ws[2 * (N + 1)] = {0};
23
24
2
   std::memcpy(z, in.data(), sizeof(z));
25
2
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
2
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
34
   for(size_t i = 0; i != 2 * N; ++i) {
29
32
      z_script[i] = z_ref[i] = z[i];
30
32
   }
31
32
2
   if(N == 4) {
33
0
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
2
   } else if(N == 6) {
35
0
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
2
   } else if(N == 8) {
37
2
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
2
   } else if(N == 16) {
39
0
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
0
   } else if(N == 24) {
41
0
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
0
   } else if(N == 32) {
43
0
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
0
   } else {
45
0
      std::abort();
46
0
   }
47
48
2
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
34
   for(size_t i = 0; i != 2 * N; ++i) {
51
32
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
32
   }
60
2
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
2
}
mp_redc.cpp:void (anonymous namespace)::fuzz_mp_redc<16ul>(std::__1::span<unsigned char const, 18446744073709551615ul>)
Line
Count
Source
12
2
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
2
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
2
   word z[2 * N] = {0};
16
17
2
   word z_script[2 * N] = {0};
18
2
   word z_ref[2 * N] = {0};
19
2
   word p[N] = {0};
20
2
   word p_dash = 0;
21
22
2
   word ws[2 * (N + 1)] = {0};
23
24
2
   std::memcpy(z, in.data(), sizeof(z));
25
2
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
2
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
66
   for(size_t i = 0; i != 2 * N; ++i) {
29
64
      z_script[i] = z_ref[i] = z[i];
30
64
   }
31
32
2
   if(N == 4) {
33
0
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
2
   } else if(N == 6) {
35
0
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
2
   } else if(N == 8) {
37
0
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
2
   } else if(N == 16) {
39
2
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
2
   } else if(N == 24) {
41
0
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
0
   } else if(N == 32) {
43
0
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
0
   } else {
45
0
      std::abort();
46
0
   }
47
48
2
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
66
   for(size_t i = 0; i != 2 * N; ++i) {
51
64
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
64
   }
60
2
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
2
}
mp_redc.cpp:void (anonymous namespace)::fuzz_mp_redc<24ul>(std::__1::span<unsigned char const, 18446744073709551615ul>)
Line
Count
Source
12
2
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
2
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
2
   word z[2 * N] = {0};
16
17
2
   word z_script[2 * N] = {0};
18
2
   word z_ref[2 * N] = {0};
19
2
   word p[N] = {0};
20
2
   word p_dash = 0;
21
22
2
   word ws[2 * (N + 1)] = {0};
23
24
2
   std::memcpy(z, in.data(), sizeof(z));
25
2
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
2
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
98
   for(size_t i = 0; i != 2 * N; ++i) {
29
96
      z_script[i] = z_ref[i] = z[i];
30
96
   }
31
32
2
   if(N == 4) {
33
0
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
2
   } else if(N == 6) {
35
0
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
2
   } else if(N == 8) {
37
0
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
2
   } else if(N == 16) {
39
0
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
2
   } else if(N == 24) {
41
2
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
2
   } else if(N == 32) {
43
0
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
0
   } else {
45
0
      std::abort();
46
0
   }
47
48
2
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
98
   for(size_t i = 0; i != 2 * N; ++i) {
51
96
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
96
   }
60
2
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
2
}
mp_redc.cpp:void (anonymous namespace)::fuzz_mp_redc<32ul>(std::__1::span<unsigned char const, 18446744073709551615ul>)
Line
Count
Source
12
2
void fuzz_mp_redc(std::span<const uint8_t> in) {
13
2
   FUZZER_ASSERT_EQUAL(in.size(), (N * 3 + 1) * sizeof(word));
14
15
2
   word z[2 * N] = {0};
16
17
2
   word z_script[2 * N] = {0};
18
2
   word z_ref[2 * N] = {0};
19
2
   word p[N] = {0};
20
2
   word p_dash = 0;
21
22
2
   word ws[2 * (N + 1)] = {0};
23
24
2
   std::memcpy(z, in.data(), sizeof(z));
25
2
   std::memcpy(p, in.data() + sizeof(z), sizeof(p));
26
2
   std::memcpy(&p_dash, in.data() + sizeof(z) + sizeof(p), sizeof(p_dash));
27
28
130
   for(size_t i = 0; i != 2 * N; ++i) {
29
128
      z_script[i] = z_ref[i] = z[i];
30
128
   }
31
32
2
   if(N == 4) {
33
0
      Botan::bigint_monty_redc_4(z_script, p, p_dash, ws);
34
2
   } else if(N == 6) {
35
0
      Botan::bigint_monty_redc_6(z_script, p, p_dash, ws);
36
2
   } else if(N == 8) {
37
0
      Botan::bigint_monty_redc_8(z_script, p, p_dash, ws);
38
2
   } else if(N == 16) {
39
0
      Botan::bigint_monty_redc_16(z_script, p, p_dash, ws);
40
2
   } else if(N == 24) {
41
0
      Botan::bigint_monty_redc_24(z_script, p, p_dash, ws);
42
2
   } else if(N == 32) {
43
2
      Botan::bigint_monty_redc_32(z_script, p, p_dash, ws);
44
2
   } else {
45
0
      std::abort();
46
0
   }
47
48
2
   Botan::bigint_monty_redc_generic(z_ref, 2 * N, p, N, p_dash, ws);
49
50
130
   for(size_t i = 0; i != 2 * N; ++i) {
51
128
      if(z_script[i] != z_ref[i]) {
52
0
         dump_word_vec("input", z, 2 * N);
53
0
         dump_word_vec("z_script", z_script, 2 * N);
54
0
         dump_word_vec("z_ref", z_ref, 2 * N);
55
0
         dump_word_vec("p", p, N);
56
0
         dump_word_vec("p_dash", &p_dash, 1);
57
0
         std::abort();
58
0
      }
59
128
   }
60
2
   compare_word_vec(z_script, 2 * N, z_ref, 2 * N, "redc generic vs specialized");
61
2
}
62
63
}  // namespace
64
65
49
void fuzz(std::span<const uint8_t> in) {
66
49
   if(in.empty() || in.size() % sizeof(word) != 0) {
67
35
      return;
68
35
   }
69
70
14
   const size_t words = in.size() / sizeof(word);
71
72
14
   switch(words) {
73
2
      case 4 * 3 + 1:
74
2
         return fuzz_mp_redc<4>(in);
75
2
      case 6 * 3 + 1:
76
2
         return fuzz_mp_redc<6>(in);
77
2
      case 8 * 3 + 1:
78
2
         return fuzz_mp_redc<8>(in);
79
2
      case 16 * 3 + 1:
80
2
         return fuzz_mp_redc<16>(in);
81
2
      case 24 * 3 + 1:
82
2
         return fuzz_mp_redc<24>(in);
83
2
      case 32 * 3 + 1:
84
2
         return fuzz_mp_redc<32>(in);
85
2
      default:
86
2
         return;
87
14
   }
88
14
}