• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13011071422

28 Jan 2025 01:04PM UTC coverage: 91.258% (+0.01%) from 91.248%
13011071422

push

github

web-flow
Merge pull request #4588 from randombit/jack/faster-redc-setup

Reduce overhead from computing modular reduction parameters

94083 of 103096 relevant lines covered (91.26%)

11357671.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.45
/src/lib/math/bigint/divide.cpp
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
void sign_fixup(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r) {
355,478✔
22
   q.cond_flip_sign(x.sign() != y.sign());
355,478✔
23

24
   if(x.is_negative() && r.is_nonzero()) {
357,051✔
25
      q -= 1;
1,569✔
26
      r = y.abs() - r;
4,707✔
27
   }
28
}
355,478✔
29

30
inline bool division_check(word q, word y2, word y1, word x3, word x2, word x1) {
2,817,666✔
31
   /*
32
   Compute (y3,y2,y1) = (y2,y1) * q
33
   and return true if (y3,y2,y1) > (x3,x2,x1)
34
   */
35

36
   word y3 = 0;
2,817,666✔
37
   y1 = word_madd2(q, y1, &y3);
2,817,666✔
38
   y2 = word_madd2(q, y2, &y3);
2,817,666✔
39

40
   const word x[3] = {x1, x2, x3};
2,817,666✔
41
   const word y[3] = {y1, y2, y3};
2,817,666✔
42

43
   return bigint_ct_is_lt(x, 3, y, 3).as_bool();
2,817,666✔
44
}
45

46
}  // namespace
47

48
void ct_divide(const BigInt& x, const BigInt& y, BigInt& q_out, BigInt& r_out) {
6,642✔
49
   if(y.is_zero()) {
11,260✔
50
      throw Invalid_Argument("ct_divide: cannot divide by zero");
×
51
   }
52

53
   const size_t x_words = x.sig_words();
6,642✔
54
   const size_t y_words = y.sig_words();
6,642✔
55

56
   const size_t x_bits = x.bits();
6,642✔
57

58
   BigInt q = BigInt::with_capacity(x_words);
6,642✔
59
   BigInt r = BigInt::with_capacity(y_words);
6,642✔
60
   BigInt t = BigInt::with_capacity(y_words);  // a temporary
6,642✔
61

62
   for(size_t i = 0; i != x_bits; ++i) {
6,043,280✔
63
      const size_t b = x_bits - 1 - i;
6,036,638✔
64
      const bool x_b = x.get_bit(b);
6,036,638✔
65

66
      r <<= 1;
6,036,638✔
67
      r.conditionally_set_bit(0, x_b);
6,036,638✔
68

69
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r._data(), r.size(), y._data(), y_words) == 0;
6,036,638✔
70

71
      q.conditionally_set_bit(b, r_gte_y);
6,036,638✔
72
      r.ct_cond_swap(r_gte_y, t);
6,036,638✔
73
   }
74

75
   sign_fixup(x, y, q, r);
6,642✔
76
   r_out = r;
6,642✔
77
   q_out = q;
6,642✔
78
}
19,921✔
79

80
BigInt ct_divide_pow2k(size_t k, const BigInt& y) {
114,893✔
81
   BOTAN_ARG_CHECK(!y.is_zero(), "Cannot divide by zero");
115,271✔
82
   BOTAN_ARG_CHECK(!y.is_negative(), "Negative divisor not supported");
114,893✔
83
   BOTAN_ARG_CHECK(k > 1, "Invalid k");
114,893✔
84

85
   const size_t x_bits = k + 1;
114,893✔
86
   const size_t y_bits = y.bits();
114,893✔
87

88
   if(x_bits < y_bits) {
114,893✔
89
      return BigInt::zero();
258✔
90
   }
91

92
   BOTAN_ASSERT_NOMSG(y_bits >= 1);
114,635✔
93
   const size_t x_words = (x_bits + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS;
114,635✔
94
   const size_t y_words = y.sig_words();
114,635✔
95

96
   BigInt q = BigInt::with_capacity(x_words);
114,635✔
97
   BigInt r = BigInt::with_capacity(y_words + 1);
114,635✔
98
   BigInt t = BigInt::with_capacity(y_words + 1);  // a temporary
114,635✔
99

100
   r.set_bit(y_bits - 1);
114,635✔
101
   for(size_t i = y_bits - 1; i != x_bits; ++i) {
34,374,655✔
102
      const size_t b = x_bits - 1 - i;
34,260,020✔
103

104
      if(i >= y_bits) {
34,260,020✔
105
         bigint_shl1(r.mutable_data(), r.size(), r.size(), 1);
34,145,385✔
106
      }
107

108
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r._data(), r.size(), y._data(), y_words) == 0;
34,260,020✔
109

110
      q.conditionally_set_bit(b, r_gte_y);
34,260,020✔
111

112
      bigint_cnd_swap(static_cast<word>(r_gte_y), r.mutable_data(), t.mutable_data(), y_words + 1);
34,260,020✔
113
   }
114

115
   // No need for sign fixup
116

117
   return q;
114,635✔
118
}
229,270✔
119

120
void ct_divide_word(const BigInt& x, word y, BigInt& q_out, word& r_out) {
594,685✔
121
   if(y == 0) {
594,685✔
122
      throw Invalid_Argument("ct_divide_word: cannot divide by zero");
×
123
   }
124

125
   const size_t x_words = x.sig_words();
594,685✔
126
   const size_t x_bits = x.bits();
594,685✔
127

128
   BigInt q = BigInt::with_capacity(x_words);
594,685✔
129
   word r = 0;
130

131
   for(size_t i = 0; i != x_bits; ++i) {
59,200,113✔
132
      const size_t b = x_bits - 1 - i;
58,605,428✔
133
      const bool x_b = x.get_bit(b);
58,605,428✔
134

135
      const auto r_carry = CT::Mask<word>::expand_top_bit(r);
58,605,428✔
136

137
      r <<= 1;
58,605,428✔
138
      r += x_b;
58,605,428✔
139

140
      const auto r_gte_y = CT::Mask<word>::is_gte(r, y) | r_carry;
58,605,428✔
141
      q.conditionally_set_bit(b, r_gte_y.as_bool());
58,605,428✔
142
      r = r_gte_y.select(r - y, r);
58,605,428✔
143
   }
144

145
   if(x.is_negative()) {
594,685✔
146
      q.flip_sign();
19✔
147
      if(r != 0) {
19✔
148
         --q;
15✔
149
         r = y - r;
15✔
150
      }
151
   }
152

153
   r_out = r;
594,685✔
154
   q_out = q;
594,685✔
155
}
594,685✔
156

157
word ct_mod_word(const BigInt& x, word y) {
4,334✔
158
   BOTAN_ARG_CHECK(x.is_positive(), "The argument x must be positive");
4,334✔
159
   BOTAN_ARG_CHECK(y != 0, "Cannot divide by zero");
4,334✔
160

161
   const size_t x_bits = x.bits();
4,334✔
162

163
   word r = 0;
4,334✔
164

165
   for(size_t i = 0; i != x_bits; ++i) {
3,018,772✔
166
      const size_t b = x_bits - 1 - i;
3,014,438✔
167
      const bool x_b = x.get_bit(b);
3,014,438✔
168

169
      const auto r_carry = CT::Mask<word>::expand_top_bit(r);
3,014,438✔
170

171
      r <<= 1;
3,014,438✔
172
      r += x_b;
3,014,438✔
173

174
      const auto r_gte_y = CT::Mask<word>::is_gte(r, y) | r_carry;
3,014,438✔
175
      r = r_gte_y.select(r - y, r);
3,014,438✔
176
   }
177

178
   return r;
4,334✔
179
}
180

181
BigInt ct_modulo(const BigInt& x, const BigInt& y) {
6,476✔
182
   if(y.is_negative() || y.is_zero()) {
12,952✔
183
      throw Invalid_Argument("ct_modulo requires y > 0");
×
184
   }
185

186
   const size_t y_words = y.sig_words();
6,476✔
187

188
   const size_t x_bits = x.bits();
6,476✔
189

190
   BigInt r = BigInt::with_capacity(y_words);
6,476✔
191
   BigInt t = BigInt::with_capacity(y_words);
6,476✔
192

193
   for(size_t i = 0; i != x_bits; ++i) {
8,605,675✔
194
      const size_t b = x_bits - 1 - i;
8,599,199✔
195
      const bool x_b = x.get_bit(b);
8,599,199✔
196

197
      r <<= 1;
8,599,199✔
198
      r.conditionally_set_bit(0, x_b);
8,599,199✔
199

200
      const bool r_gte_y = bigint_sub3(t.mutable_data(), r._data(), r.size(), y._data(), y_words) == 0;
8,599,199✔
201

202
      r.ct_cond_swap(r_gte_y, t);
8,599,199✔
203
   }
204

205
   if(x.is_negative()) {
6,476✔
206
      if(r.is_nonzero()) {
1,208✔
207
         r = y - r;
1,204✔
208
      }
209
   }
210

211
   return r;
6,476✔
212
}
6,476✔
213

214
/*
215
* Solve x = q * y + r
216
*
217
* See Handbook of Applied Cryptography section 14.2.5
218
*/
219
void vartime_divide(const BigInt& x, const BigInt& y_arg, BigInt& q_out, BigInt& r_out) {
348,836✔
220
   if(y_arg.is_zero()) {
348,912✔
221
      throw Invalid_Argument("vartime_divide: cannot divide by zero");
×
222
   }
223

224
   const size_t y_words = y_arg.sig_words();
348,836✔
225

226
   BOTAN_ASSERT_NOMSG(y_words > 0);
348,836✔
227

228
   BigInt y = y_arg;
348,836✔
229

230
   BigInt r = x;
348,836✔
231
   BigInt q = BigInt::zero();
348,836✔
232
   secure_vector<word> ws;
348,836✔
233

234
   r.set_sign(BigInt::Positive);
348,836✔
235
   y.set_sign(BigInt::Positive);
348,836✔
236

237
   // Calculate shifts needed to normalize y with high bit set
238
   const size_t shifts = y.top_bits_free();
348,836✔
239

240
   y <<= shifts;
348,836✔
241
   r <<= shifts;
348,836✔
242

243
   // we know y has not changed size, since we only shifted up to set high bit
244
   const size_t t = y_words - 1;
348,836✔
245
   const size_t n = std::max(y_words, r.sig_words()) - 1;  // r may have changed size however
697,672✔
246

247
   BOTAN_ASSERT_NOMSG(n >= t);
348,836✔
248

249
   q.grow_to(n - t + 1);
348,836✔
250

251
   word* q_words = q.mutable_data();
348,836✔
252

253
   BigInt shifted_y = y << (BOTAN_MP_WORD_BITS * (n - t));
348,836✔
254

255
   // Set q_{n-t} to number of times r > shifted_y
256
   q_words[n - t] = r.reduce_below(shifted_y, ws);
348,836✔
257

258
   const word y_t0 = y.word_at(t);
348,836✔
259
   const word y_t1 = y.word_at(t - 1);
348,836✔
260
   BOTAN_DEBUG_ASSERT((y_t0 >> (BOTAN_MP_WORD_BITS - 1)) == 1);
348,836✔
261

262
   for(size_t j = n; j != t; --j) {
1,757,669✔
263
      const word x_j0 = r.word_at(j);
1,408,833✔
264
      const word x_j1 = r.word_at(j - 1);
1,408,833✔
265
      const word x_j2 = r.word_at(j - 2);
1,408,833✔
266

267
      word qjt = bigint_divop_vartime(x_j0, x_j1, y_t0);
1,408,833✔
268

269
      qjt = CT::Mask<word>::is_equal(x_j0, y_t0).select(WordInfo<word>::max, qjt);
1,408,833✔
270

271
      // Per HAC 14.23, this operation is required at most twice
272
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
1,408,833✔
273
      qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
1,408,833✔
274
      BOTAN_DEBUG_ASSERT(division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2) == false);
1,408,833✔
275

276
      shifted_y >>= BOTAN_MP_WORD_BITS;
1,408,833✔
277
      // Now shifted_y == y << (BOTAN_MP_WORD_BITS * (j-t-1))
278

279
      // TODO this sequence could be better
280
      r -= qjt * shifted_y;
1,408,833✔
281
      qjt -= r.is_negative();
1,408,833✔
282
      r += static_cast<word>(r.is_negative()) * shifted_y;
1,408,833✔
283

284
      q_words[j - t - 1] = qjt;
1,408,833✔
285
   }
286

287
   r >>= shifts;
348,836✔
288

289
   sign_fixup(x, y_arg, q, r);
348,836✔
290

291
   r_out = r;
348,836✔
292
   q_out = q;
348,836✔
293
}
1,744,180✔
294

295
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc