Coverage Report

Created: 2026-05-06 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/fuzzer/divide.cpp
Line
Count
Source
1
/*
2
* (C) 2015,2016,2018,2021 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
#include "fuzzers.h"
7
8
#include <botan/internal/divide.h>
9
10
1.65k
void fuzz(std::span<const uint8_t> in) {
11
1.65k
   if(in.size() > 2 * 4096 / 8) {
12
16
      return;
13
16
   }
14
15
   // Save on allocations by making these static
16
1.63k
   static Botan::BigInt x;
17
1.63k
   static Botan::BigInt y;
18
1.63k
   static Botan::BigInt q;
19
1.63k
   static Botan::BigInt r;
20
1.63k
   static Botan::BigInt ct_q;
21
1.63k
   static Botan::BigInt ct_r;
22
1.63k
   static Botan::BigInt z;
23
24
1.63k
   x = Botan::BigInt::from_bytes(in.subspan(0, in.size() / 2));
25
1.63k
   y = Botan::BigInt::from_bytes(in.subspan(in.size() / 2, in.size() - in.size() / 2));
26
27
1.63k
   if(y == 0) {
28
2
      return;
29
2
   }
30
31
1.63k
   Botan::vartime_divide(x, y, q, r);
32
33
1.63k
   FUZZER_ASSERT_TRUE(r < y);
34
35
1.63k
   z = q * y + r;
36
37
1.63k
   FUZZER_ASSERT_EQUAL(z, x);
38
39
1.63k
   Botan::ct_divide(x, y, ct_q, ct_r);
40
41
1.63k
   FUZZER_ASSERT_EQUAL(q, ct_q);
42
1.63k
   FUZZER_ASSERT_EQUAL(r, ct_r);
43
44
   // Now divide by just low word of y
45
46
1.63k
   y = y.word_at(0);
47
1.63k
   if(y == 0) {
48
31
      return;
49
31
   }
50
51
1.60k
   Botan::vartime_divide(x, y, q, r);
52
53
1.60k
   FUZZER_ASSERT_TRUE(r < y);
54
1.60k
   z = q * y + r;
55
1.60k
   FUZZER_ASSERT_EQUAL(z, x);
56
57
1.60k
   Botan::word rw = 0;
58
1.60k
   Botan::ct_divide_word(x, y.word_at(0), ct_q, rw);
59
1.60k
   FUZZER_ASSERT_EQUAL(ct_q, q);
60
1.60k
   FUZZER_ASSERT_EQUAL(rw, r.word_at(0));
61
1.60k
}