aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/catchy/test_bigint.cpp
blob: 7ea2d6370772328053aa41bd6f83125483cd981b (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// (C) 2015 Simon Warta (Kullo GmbH)
// Botan is released under the Simplified BSD License (see license.txt)

#include "catch.hpp"

#include <botan/build.h>

#if defined(BOTAN_HAS_BIGINT)

#include <botan/bigint.h>

using namespace Botan;

TEST_CASE("Bigint basics", "[bigint]")
   {
   SECTION("in 0-bit border")
      {
      BigInt a(0u);
      CHECK(( a.bits() == 0 ));
      CHECK(( a.bytes() == 0 ));
      CHECK(( a.to_u32bit() == 0 ));
      }
   SECTION("above 0-bit border")
      {
      BigInt a(1u);
      CHECK(( a.bits() == 1 ));
      CHECK(( a.bytes() == 1 ));
      CHECK(( a.to_u32bit() == 1 ));
      }
   SECTION("in 8-bit border")
      {
      BigInt a(255u);
      CHECK(( a.bits() == 8 ));
      CHECK(( a.bytes() == 1 ));
      CHECK(( a.to_u32bit() == 255 ));
      }
   SECTION("above 8-bit border")
      {
      BigInt a(256u);
      CHECK(( a.bits() == 9 ));
      CHECK(( a.bytes() == 2 ));
      CHECK(( a.to_u32bit() == 256 ));
      }
   SECTION("in 16-bit border")
      {
      BigInt a(65535u);
      CHECK(( a.bits() == 16 ));
      CHECK(( a.bytes() == 2 ));
      CHECK(( a.to_u32bit() == 65535 ));
      }
   SECTION("above 16-bit border")
      {
      BigInt a(65536u);
      CHECK(( a.bits() == 17 ));
      CHECK(( a.bytes() == 3 ));
      CHECK(( a.to_u32bit() == 65536 ));
      }
   SECTION("in 32-bit border")
      {
      BigInt a(4294967295u);
      CHECK(( a.bits() == 32 ));
      CHECK(( a.bytes() == 4 ));
      CHECK(( a.to_u32bit() == 4294967295u ));
      }
   SECTION("above 32-bit border")
      {
      BigInt a(4294967296u);
      CHECK(( a.bits() == 33 ));
      CHECK(( a.bytes() == 5 ));
      CHECK_THROWS( a.to_u32bit() );
      }
   }

TEST_CASE("Bigint random_integer", "[bigint]")
   {
   RandomNumberGenerator *rng = RandomNumberGenerator::make_rng();

   SECTION("min is 0")
      {
      // 0–9
      const size_t MIN = 0;
      const size_t MAX = 10; // excluded
      const int ITERATIONS = 10000;

      std::vector<int> counts(MAX, 0);
      std::vector<double> ratios(MAX, 1.0);

      for (size_t i = 0; i < ITERATIONS; i++)
         {
         BigInt b = BigInt::random_integer(*rng, MIN, MAX);
         size_t x = b.to_u32bit();
         counts[x]++;
         }

      std::stringstream debug;
      for (size_t d = MIN; d < MAX; ++d)
         {
         auto ratio = static_cast<double>(counts[d]) / ITERATIONS;
         ratios[d] = ratio;

         if (!debug.str().empty())
            {
            debug << ", ";
            }
         debug << d << ": " << std::setprecision(3) << ratio;
         }

      INFO( debug.str() )

      // Have ~ 10 % on each digit from 0-9
      CHECK(( 0.085 <= ratios[0] )); CHECK(( ratios[0] <= 0.115 ));
      CHECK(( 0.085 <= ratios[1] )); CHECK(( ratios[1] <= 0.115 ));
      CHECK(( 0.085 <= ratios[2] )); CHECK(( ratios[2] <= 0.115 ));
      CHECK(( 0.085 <= ratios[3] )); CHECK(( ratios[3] <= 0.115 ));
      CHECK(( 0.085 <= ratios[4] )); CHECK(( ratios[4] <= 0.115 ));
      CHECK(( 0.085 <= ratios[5] )); CHECK(( ratios[5] <= 0.115 ));
      CHECK(( 0.085 <= ratios[6] )); CHECK(( ratios[6] <= 0.115 ));
      CHECK(( 0.085 <= ratios[7] )); CHECK(( ratios[7] <= 0.115 ));
      CHECK(( 0.085 <= ratios[8] )); CHECK(( ratios[8] <= 0.115 ));
      CHECK(( 0.085 <= ratios[9] )); CHECK(( ratios[9] <= 0.115 ));
      //CHECK( false );
      }

   SECTION("min is 10")
      {
      // 10–19
      const size_t MIN = 10;
      const size_t MAX = 20; // excluded
      const size_t ITERATIONS = 10000;

      std::vector<int> counts(MAX, 0);
      std::vector<double> ratios(MAX, 1.0);

      for (size_t i = 0; i < ITERATIONS; i++)
         {
         BigInt b = BigInt::random_integer(*rng, MIN, MAX);
         size_t x = b.to_u32bit();
         counts[x]++;
         }

      std::stringstream debug;
      for (size_t d = MIN; d < MAX; ++d)
         {
         auto ratio = static_cast<double>(counts[d]) / ITERATIONS;
         ratios[d] = ratio;

         if (!debug.str().empty())
            {
            debug << ", ";
            }
         debug << d << ": " << std::setprecision(3) << ratio;
         }

      INFO( debug.str() )

      // Have ~ 10 % on each digit from 10-19
      CHECK(( 0.085 <= ratios[10] )); CHECK(( ratios[10] <= 0.115 ));
      CHECK(( 0.085 <= ratios[11] )); CHECK(( ratios[11] <= 0.115 ));
      CHECK(( 0.085 <= ratios[12] )); CHECK(( ratios[12] <= 0.115 ));
      CHECK(( 0.085 <= ratios[13] )); CHECK(( ratios[13] <= 0.115 ));
      CHECK(( 0.085 <= ratios[14] )); CHECK(( ratios[14] <= 0.115 ));
      CHECK(( 0.085 <= ratios[15] )); CHECK(( ratios[15] <= 0.115 ));
      CHECK(( 0.085 <= ratios[16] )); CHECK(( ratios[16] <= 0.115 ));
      CHECK(( 0.085 <= ratios[17] )); CHECK(( ratios[17] <= 0.115 ));
      CHECK(( 0.085 <= ratios[18] )); CHECK(( ratios[18] <= 0.115 ));
      CHECK(( 0.085 <= ratios[19] )); CHECK(( ratios[19] <= 0.115 ));
      //CHECK( false );
      }
   }

#endif