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 
fuzz(const uint8_t in[],size_t len)11 void fuzz(const uint8_t in[], size_t len)
12    {
13    const size_t max_bits = 512;
14 
15    if(len < 3 || len > 1 + 2*(max_bits/8))
16       return;
17 
18    const uint8_t signs = in[0];
19    const size_t x_len = (len - 1) / 2;
20 
21    Botan::BigInt x = Botan::BigInt::decode(in + 1, x_len);
22    Botan::BigInt y = Botan::BigInt::decode(in + 1 + x_len, len - x_len - 1);
23 
24    if(signs & 1)
25       x.flip_sign();
26    if(signs & 2)
27       y.flip_sign();
28 
29    const Botan::BigInt d1 = x - y;
30    const Botan::BigInt d2 = y - x;
31 
32    FUZZER_ASSERT_TRUE(d1.cmp(d2, false) == 0);
33 
34    const bool is_eq = (x == y);
35    const bool is_lt = (x < y);
36    const bool is_gt = (x > y);
37    const bool is_lte = (x <= y);
38    const bool is_gte = (x >= y);
39 
40    if(is_eq)
41       {
42       FUZZER_ASSERT_TRUE(d1.is_zero());
43       FUZZER_ASSERT_TRUE(d2.is_zero());
44       }
45 
46    if(is_lte)
47       {
48       FUZZER_ASSERT_TRUE(is_lt || is_eq);
49       }
50 
51    if(is_gte)
52       {
53       FUZZER_ASSERT_TRUE(is_gt || is_eq);
54       }
55 
56    if(is_lt)
57       {
58       FUZZER_ASSERT_TRUE(!is_gt);
59       FUZZER_ASSERT_TRUE(d1.is_nonzero());
60       FUZZER_ASSERT_TRUE(d2.is_nonzero());
61       FUZZER_ASSERT_TRUE(d1.is_negative());
62       FUZZER_ASSERT_TRUE(d2.is_positive());
63       }
64 
65    if(is_gt)
66       {
67       FUZZER_ASSERT_TRUE(!is_lt);
68       FUZZER_ASSERT_TRUE(d1.is_nonzero());
69       FUZZER_ASSERT_TRUE(d2.is_nonzero());
70       FUZZER_ASSERT_TRUE(d1.is_positive());
71       FUZZER_ASSERT_TRUE(d2.is_negative());
72       }
73    }
74 
75