Coverage Report

Created: 2024-02-25 06:16

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