Coverage Report

Created: 2026-04-12 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.4.0/src/lib/math/bigint/divide.cpp
Line
Count
Source
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
94.4k
void sign_fixup(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r) {
22
94.4k
   q.cond_flip_sign(x.sign() != y.sign());
23
24
94.4k
   if(x.is_negative() && r.is_nonzero()) {
25
0
      q -= 1;
26
0
      r = y.abs() - r;
27
0
   }
28
94.4k
}
29
30
531k
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
531k
   word y3 = 0;
37
531k
   y1 = word_madd2(q, y1, &y3);
38
531k
   y2 = word_madd2(q, y2, &y3);
39
40
531k
   const word x[3] = {x1, x2, x3};
41
531k
   const word y[3] = {y1, y2, y3};
42
43
531k
   return bigint_ct_is_lt(x, 3, y, 3).as_bool();
44
531k
}
45
46
}  // namespace
47
48
80.2k
void ct_divide(const BigInt& x, const BigInt& y, BigInt& q_out, BigInt& r_out) {
49
80.2k
   if(y.is_zero()) {
50
0
      throw Invalid_Argument("ct_divide: cannot divide by zero");
51
0
   }
52
53
80.2k
   const size_t x_words = x.sig_words();
54
80.2k
   const size_t y_words = y.sig_words();
55
56
80.2k
   const size_t x_bits = x.bits();
57
58
80.2k
   BigInt q = BigInt::with_capacity(x_words);
59
80.2k
   BigInt r = BigInt::with_capacity(y_words);
60
80.2k
   BigInt t = BigInt::with_capacity(y_words);  // a temporary
61
62
120M
   for(size_t i = 0; i != x_bits; ++i) {
63
120M
      const size_t b = x_bits - 1 - i;
64
120M
      const bool x_b = x.get_bit(b);
65
66
120M
      r *= 2;
67
120M
      r.conditionally_set_bit(0, x_b);
68
69
120M
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
70
71
120M
      q.conditionally_set_bit(b, r_gte_y);
72
120M
      r.ct_cond_swap(r_gte_y, t);
73
120M
   }
74
75
80.2k
   sign_fixup(x, y, q, r);
76
80.2k
   r_out = r;
77
80.2k
   q_out = q;
78
80.2k
}
79
80
0
void ct_divide_word(const BigInt& x, word y, BigInt& q_out, word& r_out) {
81
0
   if(y == 0) {
82
0
      throw Invalid_Argument("ct_divide_word: cannot divide by zero");
83
0
   }
84
85
0
   const size_t x_words = x.sig_words();
86
0
   const size_t x_bits = x.bits();
87
88
0
   BigInt q = BigInt::with_capacity(x_words);
89
0
   word r = 0;
90
91
0
   for(size_t i = 0; i != x_bits; ++i) {
92
0
      const size_t b = x_bits - 1 - i;
93
0
      const bool x_b = x.get_bit(b);
94
95
0
      const auto r_carry = CT::Mask<word>::expand(r >> (BOTAN_MP_WORD_BITS - 1));
96
97
0
      r *= 2;
98
0
      r += x_b;
99
100
0
      const auto r_gte_y = CT::Mask<word>::is_gte(r, y) | r_carry;
101
0
      q.conditionally_set_bit(b, r_gte_y.as_bool());
102
0
      r = r_gte_y.select(r - y, r);
103
0
   }
104
105
0
   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
0
   r_out = r;
114
0
   q_out = q;
115
0
}
116
117
6.12k
BigInt ct_modulo(const BigInt& x, const BigInt& y) {
118
6.12k
   if(y.is_negative() || y.is_zero()) {
119
0
      throw Invalid_Argument("ct_modulo requires y > 0");
120
0
   }
121
122
6.12k
   const size_t y_words = y.sig_words();
123
124
6.12k
   const size_t x_bits = x.bits();
125
126
6.12k
   BigInt r = BigInt::with_capacity(y_words);
127
6.12k
   BigInt t = BigInt::with_capacity(y_words);
128
129
985k
   for(size_t i = 0; i != x_bits; ++i) {
130
979k
      const size_t b = x_bits - 1 - i;
131
979k
      const bool x_b = x.get_bit(b);
132
133
979k
      r *= 2;
134
979k
      r.conditionally_set_bit(0, x_b);
135
136
979k
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r.data(), r.size(), y.data(), y_words) == 0;
137
138
979k
      r.ct_cond_swap(r_gte_y, t);
139
979k
   }
140
141
6.12k
   if(x.is_negative()) {
142
0
      if(r.is_nonzero()) {
143
0
         r = y - r;
144
0
      }
145
0
   }
146
147
6.12k
   return r;
148
6.12k
}
149
150
/*
151
* Solve x = q * y + r
152
*
153
* See Handbook of Applied Cryptography section 14.2.5
154
*/
155
14.2k
void vartime_divide(const BigInt& x, const BigInt& y_arg, BigInt& q_out, BigInt& r_out) {
156
14.2k
   if(y_arg.is_zero()) {
157
0
      throw Invalid_Argument("vartime_divide: cannot divide by zero");
158
0
   }
159
160
14.2k
   const size_t y_words = y_arg.sig_words();
161
162
14.2k
   BOTAN_ASSERT_NOMSG(y_words > 0);
163
164
14.2k
   BigInt y = y_arg;
165
166
14.2k
   BigInt r = x;
167
14.2k
   BigInt q = BigInt::zero();
168
14.2k
   secure_vector<word> ws;
169
170
14.2k
   r.set_sign(BigInt::Positive);
171
14.2k
   y.set_sign(BigInt::Positive);
172
173
   // Calculate shifts needed to normalize y with high bit set
174
14.2k
   const size_t shifts = y.top_bits_free();
175
176
14.2k
   y <<= shifts;
177
14.2k
   r <<= shifts;
178
179
   // we know y has not changed size, since we only shifted up to set high bit
180
14.2k
   const size_t t = y_words - 1;
181
14.2k
   const size_t n = std::max(y_words, r.sig_words()) - 1;  // r may have changed size however
182
183
14.2k
   BOTAN_ASSERT_NOMSG(n >= t);
184
185
14.2k
   q.grow_to(n - t + 1);
186
187
14.2k
   word* q_words = q.mutable_data();
188
189
14.2k
   BigInt shifted_y = y << (BOTAN_MP_WORD_BITS * (n - t));
190
191
   // Set q_{n-t} to number of times r > shifted_y
192
14.2k
   q_words[n - t] = r.reduce_below(shifted_y, ws);
193
194
14.2k
   const word y_t0 = y.word_at(t);
195
14.2k
   const word y_t1 = y.word_at(t - 1);
196
14.2k
   BOTAN_DEBUG_ASSERT((y_t0 >> (BOTAN_MP_WORD_BITS - 1)) == 1);
197
198
279k
   for(size_t j = n; j != t; --j) {
199
265k
      const word x_j0 = r.word_at(j);
200
265k
      const word x_j1 = r.word_at(j - 1);
201
265k
      const word x_j2 = r.word_at(j - 2);
202
203
265k
      word qjt = bigint_divop_vartime(x_j0, x_j1, y_t0);
204
205
265k
      qjt = CT::Mask<word>::is_equal(x_j0, y_t0).select(WordInfo<word>::max, qjt);
206
207
      // Per HAC 14.23, this operation is required at most twice
208
265k
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
209
265k
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
210
265k
      BOTAN_DEBUG_ASSERT(division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2) == false);
211
212
265k
      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
265k
      r -= qjt * shifted_y;
217
265k
      qjt -= r.is_negative();
218
265k
      r += static_cast<word>(r.is_negative()) * shifted_y;
219
220
265k
      q_words[j - t - 1] = qjt;
221
265k
   }
222
223
14.2k
   r >>= shifts;
224
225
14.2k
   sign_fixup(x, y_arg, q, r);
226
227
14.2k
   r_out = r;
228
14.2k
   q_out = q;
229
14.2k
}
230
231
}  // namespace Botan