Coverage Report

Created: 2020-09-16 07:52

/src/botan/src/lib/math/bigint/divide.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Division Algorithm
3
* (C) 1999-2007,2012,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/divide.h>
9
#include <botan/internal/mp_core.h>
10
#include <botan/internal/mp_madd.h>
11
#include <botan/internal/ct_utils.h>
12
#include <botan/internal/bit_ops.h>
13
14
namespace Botan {
15
16
namespace {
17
18
/*
19
* Handle signed operands, if necessary
20
*/
21
void sign_fixup(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r)
22
2.74M
   {
23
2.74M
   q.cond_flip_sign(x.sign() != y.sign());
24
2.74M
25
2.74M
   if(x.is_negative() && r.is_nonzero())
26
1.27k
      {
27
1.27k
      q -= 1;
28
1.27k
      r = y.abs() - r;
29
1.27k
      }
30
2.74M
   }
31
32
inline bool division_check(word q, word y2, word y1,
33
                           word x3, word x2, word x1)
34
6.07M
   {
35
   /*
36
   Compute (y3,y2,y1) = (y2,y1) * q
37
   and return true if (y3,y2,y1) > (x3,x2,x1)
38
   */
39
6.07M
40
6.07M
   word y3 = 0;
41
6.07M
   y1 = word_madd2(q, y1, &y3);
42
6.07M
   y2 = word_madd2(q, y2, &y3);
43
6.07M
44
6.07M
   const word x[3] = { x1, x2, x3 };
45
6.07M
   const word y[3] = { y1, y2, y3 };
46
6.07M
47
6.07M
   return bigint_ct_is_lt(x, 3, y, 3).is_set();
48
6.07M
   }
49
50
}
51
52
void ct_divide(const BigInt& x, const BigInt& y, BigInt& q_out, BigInt& r_out)
53
105k
   {
54
105k
   const size_t x_words = x.sig_words();
55
105k
   const size_t y_words = y.sig_words();
56
105k
57
105k
   const size_t x_bits = x.bits();
58
105k
59
105k
   BigInt q(BigInt::Positive, x_words);
60
105k
   BigInt r(BigInt::Positive, y_words);
61
105k
   BigInt t(BigInt::Positive, y_words); // a temporary
62
105k
63
155M
   for(size_t i = 0; i != x_bits; ++i)
64
155M
      {
65
155M
      const size_t b = x_bits - 1 - i;
66
155M
      const bool x_b = x.get_bit(b);
67
155M
68
155M
      r *= 2;
69
155M
      r.conditionally_set_bit(0, x_b);
70
155M
71
155M
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
72
155M
73
155M
      q.conditionally_set_bit(b, r_gte_y);
74
155M
      r.ct_cond_swap(r_gte_y, t);
75
155M
      }
76
105k
77
105k
   sign_fixup(x, y, q, r);
78
105k
   r_out = r;
79
105k
   q_out = q;
80
105k
   }
81
82
void ct_divide_u8(const BigInt& x, uint8_t y, BigInt& q_out, uint8_t& r_out)
83
765
   {
84
765
   const size_t x_words = x.sig_words();
85
765
   const size_t x_bits = x.bits();
86
765
87
765
   BigInt q(BigInt::Positive, x_words);
88
765
   uint32_t r = 0;
89
765
90
666k
   for(size_t i = 0; i != x_bits; ++i)
91
665k
      {
92
665k
      const size_t b = x_bits - 1 - i;
93
665k
      const bool x_b = x.get_bit(b);
94
665k
95
665k
      r *= 2;
96
665k
      r += x_b;
97
665k
98
665k
      const auto r_gte_y = CT::Mask<uint32_t>::is_gte(r, y);
99
665k
100
665k
      q.conditionally_set_bit(b, r_gte_y.is_set());
101
665k
      r = r_gte_y.select(r - y, r);
102
665k
      }
103
765
104
765
   if(x.is_negative())
105
0
      {
106
0
      q.flip_sign();
107
0
      if(r != 0)
108
0
         {
109
0
         --q;
110
0
         r = y - r;
111
0
         }
112
0
      }
113
765
114
765
   r_out = static_cast<uint8_t>(r);
115
765
   q_out = q;
116
765
   }
117
118
BigInt ct_modulo(const BigInt& x, const BigInt& y)
119
1.91k
   {
120
1.91k
   if(y.is_negative() || y.is_zero())
121
0
      throw Invalid_Argument("ct_modulo requires y > 0");
122
1.91k
123
1.91k
   const size_t y_words = y.sig_words();
124
1.91k
125
1.91k
   const size_t x_bits = x.bits();
126
1.91k
127
1.91k
   BigInt r(BigInt::Positive, y_words);
128
1.91k
   BigInt t(BigInt::Positive, y_words);
129
1.91k
130
2.06M
   for(size_t i = 0; i != x_bits; ++i)
131
2.06M
      {
132
2.06M
      const size_t b = x_bits - 1 - i;
133
2.06M
      const bool x_b = x.get_bit(b);
134
2.06M
135
2.06M
      r *= 2;
136
2.06M
      r.conditionally_set_bit(0, x_b);
137
2.06M
138
2.06M
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
139
2.06M
140
2.06M
      r.ct_cond_swap(r_gte_y, t);
141
2.06M
      }
142
1.91k
143
1.91k
   if(x.is_negative())
144
525
      {
145
525
      if(r.is_nonzero())
146
408
         {
147
408
         r = y - r;
148
408
         }
149
525
      }
150
1.91k
151
1.91k
   return r;
152
1.91k
   }
153
154
/*
155
* Solve x = q * y + r
156
*
157
* See Handbook of Applied Cryptography section 14.2.5
158
*/
159
void divide(const BigInt& x, const BigInt& y_arg, BigInt& q_out, BigInt& r_out)
160
2.63M
   {
161
2.63M
   if(y_arg.is_zero())
162
0
      throw BigInt::DivideByZero();
163
2.63M
164
2.63M
   const size_t y_words = y_arg.sig_words();
165
2.63M
166
2.63M
   BOTAN_ASSERT_NOMSG(y_words > 0);
167
2.63M
168
2.63M
   BigInt y = y_arg;
169
2.63M
170
2.63M
   BigInt r = x;
171
2.63M
   BigInt q = 0;
172
2.63M
   secure_vector<word> ws;
173
2.63M
174
2.63M
   r.set_sign(BigInt::Positive);
175
2.63M
   y.set_sign(BigInt::Positive);
176
2.63M
177
   // Calculate shifts needed to normalize y with high bit set
178
2.63M
   const size_t shifts = y.top_bits_free();
179
2.63M
180
2.63M
   y <<= shifts;
181
2.63M
   r <<= shifts;
182
2.63M
183
   // we know y has not changed size, since we only shifted up to set high bit
184
2.63M
   const size_t t = y_words - 1;
185
2.63M
   const size_t n = std::max(y_words, r.sig_words()) - 1; // r may have changed size however
186
2.63M
187
2.63M
   BOTAN_ASSERT_NOMSG(n >= t);
188
2.63M
189
2.63M
   q.grow_to(n - t + 1);
190
2.63M
191
2.63M
   word* q_words = q.mutable_data();
192
2.63M
193
2.63M
   BigInt shifted_y = y << (BOTAN_MP_WORD_BITS * (n-t));
194
2.63M
195
   // Set q_{n-t} to number of times r > shifted_y
196
2.63M
   q_words[n-t] = r.reduce_below(shifted_y, ws);
197
2.63M
198
2.63M
   const word y_t0  = y.word_at(t);
199
2.63M
   const word y_t1  = y.word_at(t-1);
200
2.63M
   BOTAN_DEBUG_ASSERT((y_t0 >> (BOTAN_MP_WORD_BITS-1)) == 1);
201
2.63M
202
5.67M
   for(size_t j = n; j != t; --j)
203
3.03M
      {
204
3.03M
      const word x_j0  = r.word_at(j);
205
3.03M
      const word x_j1 = r.word_at(j-1);
206
3.03M
      const word x_j2 = r.word_at(j-2);
207
3.03M
208
3.03M
      word qjt = bigint_divop(x_j0, x_j1, y_t0);
209
3.03M
210
3.03M
      qjt = CT::Mask<word>::is_equal(x_j0, y_t0).select(MP_WORD_MAX, qjt);
211
3.03M
212
      // Per HAC 14.23, this operation is required at most twice
213
3.03M
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
214
3.03M
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
215
3.03M
      BOTAN_DEBUG_ASSERT(division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2) == false);
216
3.03M
217
3.03M
      shifted_y >>= BOTAN_MP_WORD_BITS;
218
      // Now shifted_y == y << (BOTAN_MP_WORD_BITS * (j-t-1))
219
3.03M
220
      // TODO this sequence could be better
221
3.03M
      r -= qjt * shifted_y;
222
3.03M
      qjt -= r.is_negative();
223
3.03M
      r += static_cast<word>(r.is_negative()) * shifted_y;
224
3.03M
225
3.03M
      q_words[j-t-1] = qjt;
226
3.03M
      }
227
2.63M
228
2.63M
   r >>= shifts;
229
2.63M
230
2.63M
   sign_fixup(x, y_arg, q, r);
231
2.63M
232
2.63M
   r_out = r;
233
2.63M
   q_out = q;
234
2.63M
   }
235
236
}