Coverage Report

Created: 2020-02-14 15:38

/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.68M
   {
23
2.68M
   q.cond_flip_sign(x.sign() != y.sign());
24
2.68M
25
2.68M
   if(x.is_negative() && r.is_nonzero())
26
1.26k
      {
27
1.26k
      q -= 1;
28
1.26k
      r = y.abs() - r;
29
1.26k
      }
30
2.68M
   }
31
32
inline bool division_check(word q, word y2, word y1,
33
                           word x3, word x2, word x1)
34
5.99M
   {
35
5.99M
   /*
36
5.99M
   Compute (y3,y2,y1) = (y2,y1) * q
37
5.99M
   and return true if (y3,y2,y1) > (x3,x2,x1)
38
5.99M
   */
39
5.99M
40
5.99M
   word y3 = 0;
41
5.99M
   y1 = word_madd2(q, y1, &y3);
42
5.99M
   y2 = word_madd2(q, y2, &y3);
43
5.99M
44
5.99M
   const word x[3] = { x1, x2, x3 };
45
5.99M
   const word y[3] = { y1, y2, y3 };
46
5.99M
47
5.99M
   return bigint_ct_is_lt(x, 3, y, 3).is_set();
48
5.99M
   }
49
50
}
51
52
void ct_divide(const BigInt& x, const BigInt& y, BigInt& q_out, BigInt& r_out)
53
103k
   {
54
103k
   const size_t x_words = x.sig_words();
55
103k
   const size_t y_words = y.sig_words();
56
103k
57
103k
   const size_t x_bits = x.bits();
58
103k
59
103k
   BigInt q(BigInt::Positive, x_words);
60
103k
   BigInt r(BigInt::Positive, y_words);
61
103k
   BigInt t(BigInt::Positive, y_words); // a temporary
62
103k
63
157M
   for(size_t i = 0; i != x_bits; ++i)
64
157M
      {
65
157M
      const size_t b = x_bits - 1 - i;
66
157M
      const bool x_b = x.get_bit(b);
67
157M
68
157M
      r *= 2;
69
157M
      r.conditionally_set_bit(0, x_b);
70
157M
71
157M
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
72
157M
73
157M
      q.conditionally_set_bit(b, r_gte_y);
74
157M
      r.ct_cond_swap(r_gte_y, t);
75
157M
      }
76
103k
77
103k
   sign_fixup(x, y, q, r);
78
103k
   r_out = r;
79
103k
   q_out = q;
80
103k
   }
81
82
void ct_divide_u8(const BigInt& x, uint8_t y, BigInt& q_out, uint8_t& r_out)
83
796
   {
84
796
   const size_t x_words = x.sig_words();
85
796
   const size_t x_bits = x.bits();
86
796
87
796
   BigInt q(BigInt::Positive, x_words);
88
796
   uint32_t r = 0;
89
796
90
696k
   for(size_t i = 0; i != x_bits; ++i)
91
695k
      {
92
695k
      const size_t b = x_bits - 1 - i;
93
695k
      const bool x_b = x.get_bit(b);
94
695k
95
695k
      r *= 2;
96
695k
      r += x_b;
97
695k
98
695k
      const auto r_gte_y = CT::Mask<uint32_t>::is_gte(r, y);
99
695k
100
695k
      q.conditionally_set_bit(b, r_gte_y.is_set());
101
695k
      r = r_gte_y.select(r - y, r);
102
695k
      }
103
796
104
796
   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
796
114
796
   r_out = static_cast<uint8_t>(r);
115
796
   q_out = q;
116
796
   }
117
118
BigInt ct_modulo(const BigInt& x, const BigInt& y)
119
456
   {
120
456
   if(y.is_negative() || y.is_zero())
121
0
      throw Invalid_Argument("ct_modulo requires y > 0");
122
456
123
456
   const size_t y_words = y.sig_words();
124
456
125
456
   const size_t x_bits = x.bits();
126
456
127
456
   BigInt r(BigInt::Positive, y_words);
128
456
   BigInt t(BigInt::Positive, y_words);
129
456
130
500k
   for(size_t i = 0; i != x_bits; ++i)
131
499k
      {
132
499k
      const size_t b = x_bits - 1 - i;
133
499k
      const bool x_b = x.get_bit(b);
134
499k
135
499k
      r *= 2;
136
499k
      r.conditionally_set_bit(0, x_b);
137
499k
138
499k
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
139
499k
140
499k
      r.ct_cond_swap(r_gte_y, t);
141
499k
      }
142
456
143
456
   if(x.is_negative())
144
204
      {
145
204
      if(r.is_nonzero())
146
177
         {
147
177
         r = y - r;
148
177
         }
149
204
      }
150
456
151
456
   return r;
152
456
   }
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.57M
   {
161
2.57M
   if(y_arg.is_zero())
162
0
      throw BigInt::DivideByZero();
163
2.57M
164
2.57M
   const size_t y_words = y_arg.sig_words();
165
2.57M
166
2.57M
   BOTAN_ASSERT_NOMSG(y_words > 0);
167
2.57M
168
2.57M
   BigInt y = y_arg;
169
2.57M
170
2.57M
   BigInt r = x;
171
2.57M
   BigInt q = 0;
172
2.57M
   secure_vector<word> ws;
173
2.57M
174
2.57M
   r.set_sign(BigInt::Positive);
175
2.57M
   y.set_sign(BigInt::Positive);
176
2.57M
177
2.57M
   // Calculate shifts needed to normalize y with high bit set
178
2.57M
   const size_t shifts = y.top_bits_free();
179
2.57M
180
2.57M
   y <<= shifts;
181
2.57M
   r <<= shifts;
182
2.57M
183
2.57M
   // we know y has not changed size, since we only shifted up to set high bit
184
2.57M
   const size_t t = y_words - 1;
185
2.57M
   const size_t n = std::max(y_words, r.sig_words()) - 1; // r may have changed size however
186
2.57M
187
2.57M
   BOTAN_ASSERT_NOMSG(n >= t);
188
2.57M
189
2.57M
   q.grow_to(n - t + 1);
190
2.57M
191
2.57M
   word* q_words = q.mutable_data();
192
2.57M
193
2.57M
   BigInt shifted_y = y << (BOTAN_MP_WORD_BITS * (n-t));
194
2.57M
195
2.57M
   // Set q_{n-t} to number of times r > shifted_y
196
2.57M
   q_words[n-t] = r.reduce_below(shifted_y, ws);
197
2.57M
198
2.57M
   const word y_t0  = y.word_at(t);
199
2.57M
   const word y_t1  = y.word_at(t-1);
200
2.57M
   BOTAN_DEBUG_ASSERT((y_t0 >> (BOTAN_MP_WORD_BITS-1)) == 1);
201
2.57M
202
5.57M
   for(size_t j = n; j != t; --j)
203
2.99M
      {
204
2.99M
      const word x_j0  = r.word_at(j);
205
2.99M
      const word x_j1 = r.word_at(j-1);
206
2.99M
      const word x_j2 = r.word_at(j-2);
207
2.99M
208
2.99M
      word qjt = bigint_divop(x_j0, x_j1, y_t0);
209
2.99M
210
2.99M
      qjt = CT::Mask<word>::is_equal(x_j0, y_t0).select(MP_WORD_MAX, qjt);
211
2.99M
212
2.99M
      // Per HAC 14.23, this operation is required at most twice
213
2.99M
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
214
2.99M
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
215
2.99M
      BOTAN_DEBUG_ASSERT(division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2) == false);
216
2.99M
217
2.99M
      shifted_y >>= BOTAN_MP_WORD_BITS;
218
2.99M
      // Now shifted_y == y << (BOTAN_MP_WORD_BITS * (j-t-1))
219
2.99M
220
2.99M
      // TODO this sequence could be better
221
2.99M
      r -= qjt * shifted_y;
222
2.99M
      qjt -= r.is_negative();
223
2.99M
      r += static_cast<word>(r.is_negative()) * shifted_y;
224
2.99M
225
2.99M
      q_words[j-t-1] = qjt;
226
2.99M
      }
227
2.57M
228
2.57M
   r >>= shifts;
229
2.57M
230
2.57M
   sign_fixup(x, y_arg, q, r);
231
2.57M
232
2.57M
   r_out = r;
233
2.57M
   q_out = q;
234
2.57M
   }
235
236
}