aboutsummaryrefslogtreecommitdiffstats
path: root/src/fuzzer/gcd.cpp
blob: 081440b4094ea3b641ec5038b2ac74456c993c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* (C) 2021 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "fuzzers.h"
#include <botan/numthry.h>

namespace {

Botan::BigInt ref_gcd(Botan::BigInt a,
                      Botan::BigInt b)
   {
   Botan::BigInt t;
   while(b != 0)
      {
      t = a % b;
      t.swap(b);
      t.swap(a);
      }
   return a;
   }

}

void fuzz(const uint8_t in[], size_t len)
   {
   static const size_t max_bits = 4096;

   if(2*len*8 > max_bits)
      return;

   const Botan::BigInt x = Botan::BigInt::decode(in, len / 2);
   const Botan::BigInt y = Botan::BigInt::decode(in + len / 2, len - (len / 2));

   const Botan::BigInt ref = ref_gcd(x, y);
   const Botan::BigInt lib = Botan::gcd(x, y);

   if(ref != lib)
      {
      FUZZER_WRITE_AND_CRASH("X = " << x << "\n"
                             << "Y = " << y << "\n"
                             << "L = " << lib << "\n"
                             << "R = " << ref << "\n");
      }
   }