Coverage Report

Created: 2022-09-23 06:05

/src/botan/src/fuzzer/bn_cmp.cpp
Line
Count
Source
1
/*
2
* (C) 2021 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include "fuzzers.h"
8
9
#include <botan/bigint.h>
10
11
void fuzz(const uint8_t in[], size_t len)
12
786
   {
13
786
   const size_t max_bits = 512;
14
15
786
   if(len < 3 || len > 1 + 2*(max_bits/8))
16
31
      return;
17
18
755
   const uint8_t signs = in[0];
19
755
   const size_t x_len = (len - 1) / 2;
20
21
755
   Botan::BigInt x = Botan::BigInt::decode(in + 1, x_len);
22
755
   Botan::BigInt y = Botan::BigInt::decode(in + 1 + x_len, len - x_len - 1);
23
24
755
   if(signs & 1)
25
387
      x.flip_sign();
26
755
   if(signs & 2)
27
379
      y.flip_sign();
28
29
755
   const Botan::BigInt d1 = x - y;
30
755
   const Botan::BigInt d2 = y - x;
31
32
755
   FUZZER_ASSERT_TRUE(d1.cmp(d2, false) == 0);
33
34
755
   const bool is_eq = (x == y);
35
755
   const bool is_lt = (x < y);
36
755
   const bool is_gt = (x > y);
37
755
   const bool is_lte = (x <= y);
38
755
   const bool is_gte = (x >= y);
39
40
755
   if(is_eq)
41
5
      {
42
5
      FUZZER_ASSERT_TRUE(d1.is_zero());
43
5
      FUZZER_ASSERT_TRUE(d2.is_zero());
44
5
      }
45
46
755
   if(is_lte)
47
373
      {
48
373
      FUZZER_ASSERT_TRUE(is_lt || is_eq);
49
373
      }
50
51
755
   if(is_gte)
52
387
      {
53
387
      FUZZER_ASSERT_TRUE(is_gt || is_eq);
54
387
      }
55
56
755
   if(is_lt)
57
368
      {
58
368
      FUZZER_ASSERT_TRUE(!is_gt);
59
368
      FUZZER_ASSERT_TRUE(d1.is_nonzero());
60
368
      FUZZER_ASSERT_TRUE(d2.is_nonzero());
61
368
      FUZZER_ASSERT_TRUE(d1.is_negative());
62
368
      FUZZER_ASSERT_TRUE(d2.is_positive());
63
368
      }
64
65
755
   if(is_gt)
66
382
      {
67
382
      FUZZER_ASSERT_TRUE(!is_lt);
68
382
      FUZZER_ASSERT_TRUE(d1.is_nonzero());
69
382
      FUZZER_ASSERT_TRUE(d2.is_nonzero());
70
382
      FUZZER_ASSERT_TRUE(d1.is_positive());
71
382
      FUZZER_ASSERT_TRUE(d2.is_negative());
72
382
      }
73
755
   }
74