/src/Botan-3.4.0/src/lib/math/bigint/bigint.cpp
Line | Count | Source |
1 | | /* |
2 | | * BigInt Base |
3 | | * (C) 1999-2011,2012,2014,2019 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/bigint.h> |
9 | | |
10 | | #include <botan/internal/bit_ops.h> |
11 | | #include <botan/internal/ct_utils.h> |
12 | | #include <botan/internal/loadstor.h> |
13 | | #include <botan/internal/mp_core.h> |
14 | | #include <botan/internal/rounding.h> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | 118k | BigInt::BigInt(uint64_t n) { |
19 | 118k | #if BOTAN_MP_WORD_BITS == 64 |
20 | 118k | m_data.set_word_at(0, n); |
21 | | #else |
22 | | m_data.set_word_at(1, static_cast<word>(n >> 32)); |
23 | | m_data.set_word_at(0, static_cast<word>(n)); |
24 | | #endif |
25 | 118k | } |
26 | | |
27 | | //static |
28 | 0 | BigInt BigInt::from_u64(uint64_t n) { |
29 | 0 | BigInt bn; |
30 | |
|
31 | 0 | #if BOTAN_MP_WORD_BITS == 64 |
32 | 0 | bn.set_word_at(0, n); |
33 | | #else |
34 | | bn.set_word_at(1, static_cast<word>(n >> 32)); |
35 | | bn.set_word_at(0, static_cast<word>(n)); |
36 | | #endif |
37 | |
|
38 | 0 | return bn; |
39 | 0 | } |
40 | | |
41 | | //static |
42 | 3.50k | BigInt BigInt::from_word(word n) { |
43 | 3.50k | BigInt bn; |
44 | 3.50k | bn.set_word_at(0, n); |
45 | 3.50k | return bn; |
46 | 3.50k | } |
47 | | |
48 | | //static |
49 | 0 | BigInt BigInt::from_s32(int32_t n) { |
50 | 0 | if(n >= 0) { |
51 | 0 | return BigInt::from_u64(static_cast<uint64_t>(n)); |
52 | 0 | } else { |
53 | 0 | return -BigInt::from_u64(static_cast<uint64_t>(-n)); |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | //static |
58 | 2.06M | BigInt BigInt::with_capacity(size_t size) { |
59 | 2.06M | BigInt bn; |
60 | 2.06M | bn.grow_to(size); |
61 | 2.06M | return bn; |
62 | 2.06M | } |
63 | | |
64 | | /* |
65 | | * Construct a BigInt from a string |
66 | | */ |
67 | 159 | BigInt::BigInt(std::string_view str) { |
68 | 159 | Base base = Decimal; |
69 | 159 | size_t markers = 0; |
70 | 159 | bool negative = false; |
71 | | |
72 | 159 | if(str.length() > 0 && str[0] == '-') { |
73 | 0 | markers += 1; |
74 | 0 | negative = true; |
75 | 0 | } |
76 | | |
77 | 159 | if(str.length() > markers + 2 && str[markers] == '0' && str[markers + 1] == 'x') { |
78 | 153 | markers += 2; |
79 | 153 | base = Hexadecimal; |
80 | 153 | } |
81 | | |
82 | 159 | *this = decode(cast_char_ptr_to_uint8(str.data()) + markers, str.length() - markers, base); |
83 | | |
84 | 159 | if(negative) { |
85 | 0 | set_sign(Negative); |
86 | 159 | } else { |
87 | 159 | set_sign(Positive); |
88 | 159 | } |
89 | 159 | } |
90 | | |
91 | 202k | BigInt::BigInt(const uint8_t input[], size_t length) { |
92 | 202k | binary_decode(input, length); |
93 | 202k | } |
94 | | |
95 | | /* |
96 | | * Construct a BigInt from an encoded BigInt |
97 | | */ |
98 | 0 | BigInt::BigInt(const uint8_t input[], size_t length, Base base) { |
99 | 0 | *this = decode(input, length, base); |
100 | 0 | } |
101 | | |
102 | | //static |
103 | 69.2k | BigInt BigInt::from_bytes_with_max_bits(const uint8_t input[], size_t length, size_t max_bits) { |
104 | 69.2k | const size_t input_bits = 8 * length; |
105 | | |
106 | 69.2k | BigInt bn; |
107 | 69.2k | bn.binary_decode(input, length); |
108 | | |
109 | 69.2k | if(input_bits > max_bits) { |
110 | 2.61k | const size_t bits_to_shift = input_bits - max_bits; |
111 | | |
112 | 2.61k | bn >>= bits_to_shift; |
113 | 2.61k | } |
114 | | |
115 | 69.2k | return bn; |
116 | 69.2k | } |
117 | | |
118 | | /* |
119 | | * Construct a BigInt from an encoded BigInt |
120 | | */ |
121 | 0 | BigInt::BigInt(RandomNumberGenerator& rng, size_t bits, bool set_high_bit) { |
122 | 0 | randomize(rng, bits, set_high_bit); |
123 | 0 | } |
124 | | |
125 | 0 | uint8_t BigInt::byte_at(size_t n) const { |
126 | 0 | return get_byte_var(sizeof(word) - (n % sizeof(word)) - 1, word_at(n / sizeof(word))); |
127 | 0 | } |
128 | | |
129 | 444k | int32_t BigInt::cmp_word(word other) const { |
130 | 444k | if(is_negative()) { |
131 | 0 | return -1; // other is positive ... |
132 | 0 | } |
133 | | |
134 | 444k | const size_t sw = this->sig_words(); |
135 | 444k | if(sw > 1) { |
136 | 415k | return 1; // must be larger since other is just one word ... |
137 | 415k | } |
138 | | |
139 | 29.7k | return bigint_cmp(this->data(), sw, &other, 1); |
140 | 444k | } |
141 | | |
142 | | /* |
143 | | * Comparison Function |
144 | | */ |
145 | 350k | int32_t BigInt::cmp(const BigInt& other, bool check_signs) const { |
146 | 350k | if(check_signs) { |
147 | 350k | if(other.is_positive() && this->is_negative()) { |
148 | 0 | return -1; |
149 | 0 | } |
150 | | |
151 | 350k | if(other.is_negative() && this->is_positive()) { |
152 | 0 | return 1; |
153 | 0 | } |
154 | | |
155 | 350k | if(other.is_negative() && this->is_negative()) { |
156 | 0 | return (-bigint_cmp(this->data(), this->size(), other.data(), other.size())); |
157 | 0 | } |
158 | 350k | } |
159 | | |
160 | 350k | return bigint_cmp(this->data(), this->size(), other.data(), other.size()); |
161 | 350k | } |
162 | | |
163 | 474k | bool BigInt::is_equal(const BigInt& other) const { |
164 | 474k | if(this->sign() != other.sign()) { |
165 | 0 | return false; |
166 | 0 | } |
167 | | |
168 | 474k | return bigint_ct_is_eq(this->data(), this->sig_words(), other.data(), other.sig_words()).as_bool(); |
169 | 474k | } |
170 | | |
171 | 235k | bool BigInt::is_less_than(const BigInt& other) const { |
172 | 235k | if(this->is_negative() && other.is_positive()) { |
173 | 0 | return true; |
174 | 0 | } |
175 | | |
176 | 235k | if(this->is_positive() && other.is_negative()) { |
177 | 0 | return false; |
178 | 0 | } |
179 | | |
180 | 235k | if(other.is_negative() && this->is_negative()) { |
181 | 0 | return bigint_ct_is_lt(other.data(), other.sig_words(), this->data(), this->sig_words()).as_bool(); |
182 | 0 | } |
183 | | |
184 | 235k | return bigint_ct_is_lt(this->data(), this->sig_words(), other.data(), other.sig_words()).as_bool(); |
185 | 235k | } |
186 | | |
187 | 59.6k | void BigInt::encode_words(word out[], size_t size) const { |
188 | 59.6k | const size_t words = sig_words(); |
189 | | |
190 | 59.6k | if(words > size) { |
191 | 0 | throw Encoding_Error("BigInt::encode_words value too large to encode"); |
192 | 0 | } |
193 | | |
194 | 59.6k | clear_mem(out, size); |
195 | 59.6k | copy_mem(out, data(), words); |
196 | 59.6k | } |
197 | | |
198 | 63.4M | size_t BigInt::Data::calc_sig_words() const { |
199 | 63.4M | const size_t sz = m_reg.size(); |
200 | 63.4M | size_t sig = sz; |
201 | | |
202 | 63.4M | word sub = 1; |
203 | | |
204 | 1.03G | for(size_t i = 0; i != sz; ++i) { |
205 | 967M | const word w = m_reg[sz - i - 1]; |
206 | 967M | sub &= ct_is_zero(w); |
207 | 967M | sig -= sub; |
208 | 967M | } |
209 | | |
210 | | /* |
211 | | * This depends on the data so is poisoned, but unpoison it here as |
212 | | * later conditionals are made on the size. |
213 | | */ |
214 | 63.4M | CT::unpoison(sig); |
215 | | |
216 | 63.4M | return sig; |
217 | 63.4M | } |
218 | | |
219 | | /* |
220 | | * Return bits {offset...offset+length} |
221 | | */ |
222 | 21.8M | uint32_t BigInt::get_substring(size_t offset, size_t length) const { |
223 | 21.8M | if(length == 0 || length > 32) { |
224 | 0 | throw Invalid_Argument("BigInt::get_substring invalid substring length"); |
225 | 0 | } |
226 | | |
227 | 21.8M | const uint32_t mask = 0xFFFFFFFF >> (32 - length); |
228 | | |
229 | 21.8M | const size_t word_offset = offset / BOTAN_MP_WORD_BITS; |
230 | 21.8M | const size_t wshift = (offset % BOTAN_MP_WORD_BITS); |
231 | | |
232 | | /* |
233 | | * The substring is contained within one or at most two words. The |
234 | | * offset and length are not secret, so we can perform conditional |
235 | | * operations on those values. |
236 | | */ |
237 | 21.8M | const word w0 = word_at(word_offset); |
238 | | |
239 | 21.8M | if(wshift == 0 || (offset + length) / BOTAN_MP_WORD_BITS == word_offset) { |
240 | 21.2M | return static_cast<uint32_t>(w0 >> wshift) & mask; |
241 | 21.2M | } else { |
242 | 640k | const word w1 = word_at(word_offset + 1); |
243 | 640k | return static_cast<uint32_t>((w0 >> wshift) | (w1 << (BOTAN_MP_WORD_BITS - wshift))) & mask; |
244 | 640k | } |
245 | 21.8M | } |
246 | | |
247 | | /* |
248 | | * Convert this number to a uint32_t, if possible |
249 | | */ |
250 | 0 | uint32_t BigInt::to_u32bit() const { |
251 | 0 | if(is_negative()) { |
252 | 0 | throw Encoding_Error("BigInt::to_u32bit: Number is negative"); |
253 | 0 | } |
254 | 0 | if(bits() > 32) { |
255 | 0 | throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert"); |
256 | 0 | } |
257 | | |
258 | 0 | uint32_t out = 0; |
259 | 0 | for(size_t i = 0; i != 4; ++i) { |
260 | 0 | out = (out << 8) | byte_at(3 - i); |
261 | 0 | } |
262 | 0 | return out; |
263 | 0 | } |
264 | | |
265 | | /* |
266 | | * Clear bit number n |
267 | | */ |
268 | 0 | void BigInt::clear_bit(size_t n) { |
269 | 0 | const size_t which = n / BOTAN_MP_WORD_BITS; |
270 | |
|
271 | 0 | if(which < size()) { |
272 | 0 | const word mask = ~(static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS)); |
273 | 0 | m_data.set_word_at(which, word_at(which) & mask); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | 256k | size_t BigInt::bytes() const { |
278 | 256k | return round_up(bits(), 8) / 8; |
279 | 256k | } |
280 | | |
281 | 731k | size_t BigInt::top_bits_free() const { |
282 | 731k | const size_t words = sig_words(); |
283 | | |
284 | 731k | const word top_word = word_at(words - 1); |
285 | 731k | const size_t bits_used = high_bit(top_word); |
286 | 731k | CT::unpoison(bits_used); |
287 | 731k | return BOTAN_MP_WORD_BITS - bits_used; |
288 | 731k | } |
289 | | |
290 | 724k | size_t BigInt::bits() const { |
291 | 724k | const size_t words = sig_words(); |
292 | | |
293 | 724k | if(words == 0) { |
294 | 5.22k | return 0; |
295 | 5.22k | } |
296 | | |
297 | 719k | const size_t full_words = (words - 1) * BOTAN_MP_WORD_BITS; |
298 | 719k | const size_t top_bits = BOTAN_MP_WORD_BITS - top_bits_free(); |
299 | | |
300 | 719k | return full_words + top_bits; |
301 | 724k | } |
302 | | |
303 | | /* |
304 | | * Return the negation of this number |
305 | | */ |
306 | 0 | BigInt BigInt::operator-() const { |
307 | 0 | BigInt x = (*this); |
308 | 0 | x.flip_sign(); |
309 | 0 | return x; |
310 | 0 | } |
311 | | |
312 | 74.0M | size_t BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws) { |
313 | 74.0M | if(p.is_negative() || this->is_negative()) { |
314 | 0 | throw Invalid_Argument("BigInt::reduce_below both values must be positive"); |
315 | 0 | } |
316 | | |
317 | 74.0M | const size_t p_words = p.sig_words(); |
318 | | |
319 | 74.0M | if(size() < p_words + 1) { |
320 | 0 | grow_to(p_words + 1); |
321 | 0 | } |
322 | | |
323 | 74.0M | if(ws.size() < p_words + 1) { |
324 | 12.4k | ws.resize(p_words + 1); |
325 | 12.4k | } |
326 | | |
327 | 74.0M | clear_mem(ws.data(), ws.size()); |
328 | | |
329 | 74.0M | size_t reductions = 0; |
330 | | |
331 | 194M | for(;;) { |
332 | 194M | word borrow = bigint_sub3(ws.data(), data(), p_words + 1, p.data(), p_words); |
333 | 194M | if(borrow) { |
334 | 74.0M | break; |
335 | 74.0M | } |
336 | | |
337 | 120M | ++reductions; |
338 | 120M | swap_reg(ws); |
339 | 120M | } |
340 | | |
341 | 74.0M | return reductions; |
342 | 74.0M | } |
343 | | |
344 | 421k | void BigInt::ct_reduce_below(const BigInt& mod, secure_vector<word>& ws, size_t bound) { |
345 | 421k | if(mod.is_negative() || this->is_negative()) { |
346 | 0 | throw Invalid_Argument("BigInt::ct_reduce_below both values must be positive"); |
347 | 0 | } |
348 | | |
349 | 421k | const size_t mod_words = mod.sig_words(); |
350 | | |
351 | 421k | grow_to(mod_words); |
352 | | |
353 | 421k | const size_t sz = size(); |
354 | | |
355 | 421k | ws.resize(sz); |
356 | | |
357 | 421k | clear_mem(ws.data(), sz); |
358 | | |
359 | 1.26M | for(size_t i = 0; i != bound; ++i) { |
360 | 842k | word borrow = bigint_sub3(ws.data(), data(), sz, mod.data(), mod_words); |
361 | | |
362 | 842k | CT::Mask<word>::is_zero(borrow).select_n(mutable_data(), ws.data(), data(), sz); |
363 | 842k | } |
364 | 421k | } |
365 | | |
366 | | /* |
367 | | * Return the absolute value of this number |
368 | | */ |
369 | 0 | BigInt BigInt::abs() const { |
370 | 0 | BigInt x = (*this); |
371 | 0 | x.set_sign(Positive); |
372 | 0 | return x; |
373 | 0 | } |
374 | | |
375 | 37.5k | void BigInt::binary_encode(uint8_t buf[]) const { |
376 | 37.5k | this->binary_encode(buf, bytes()); |
377 | 37.5k | } |
378 | | |
379 | | /* |
380 | | * Encode this number into bytes |
381 | | */ |
382 | 87.3k | void BigInt::binary_encode(uint8_t output[], size_t len) const { |
383 | 87.3k | const size_t full_words = len / sizeof(word); |
384 | 87.3k | const size_t extra_bytes = len % sizeof(word); |
385 | | |
386 | 650k | for(size_t i = 0; i != full_words; ++i) { |
387 | 562k | const word w = word_at(i); |
388 | 562k | store_be(w, output + (len - (i + 1) * sizeof(word))); |
389 | 562k | } |
390 | | |
391 | 87.3k | if(extra_bytes > 0) { |
392 | 31.1k | const word w = word_at(full_words); |
393 | | |
394 | 167k | for(size_t i = 0; i != extra_bytes; ++i) { |
395 | 136k | output[extra_bytes - i - 1] = get_byte_var(sizeof(word) - i - 1, w); |
396 | 136k | } |
397 | 31.1k | } |
398 | 87.3k | } |
399 | | |
400 | | /* |
401 | | * Set this number to the value in buf |
402 | | */ |
403 | 570k | void BigInt::binary_decode(const uint8_t buf[], size_t length) { |
404 | 570k | clear(); |
405 | | |
406 | 570k | const size_t full_words = length / sizeof(word); |
407 | 570k | const size_t extra_bytes = length % sizeof(word); |
408 | | |
409 | 570k | secure_vector<word> reg((round_up(full_words + (extra_bytes > 0 ? 1 : 0), 8))); |
410 | | |
411 | 4.10M | for(size_t i = 0; i != full_words; ++i) { |
412 | 3.53M | reg[i] = load_be<word>(buf + length - sizeof(word) * (i + 1), 0); |
413 | 3.53M | } |
414 | | |
415 | 570k | if(extra_bytes > 0) { |
416 | 888k | for(size_t i = 0; i != extra_bytes; ++i) { |
417 | 680k | reg[full_words] = (reg[full_words] << 8) | buf[i]; |
418 | 680k | } |
419 | 207k | } |
420 | | |
421 | 570k | m_data.swap(reg); |
422 | 570k | } |
423 | | |
424 | 62 | void BigInt::ct_cond_add(bool predicate, const BigInt& value) { |
425 | 62 | if(this->is_negative() || value.is_negative()) { |
426 | 0 | throw Invalid_Argument("BigInt::ct_cond_add requires both values to be positive"); |
427 | 0 | } |
428 | 62 | this->grow_to(1 + value.sig_words()); |
429 | | |
430 | 62 | bigint_cnd_add(static_cast<word>(predicate), this->mutable_data(), this->size(), value.data(), value.sig_words()); |
431 | 62 | } |
432 | | |
433 | 0 | void BigInt::ct_shift_left(size_t shift) { |
434 | 0 | auto shl_bit = [](const BigInt& a, BigInt& result) { |
435 | 0 | BOTAN_DEBUG_ASSERT(a.size() + 1 == result.size()); |
436 | 0 | bigint_shl2(result.mutable_data(), a.data(), a.size(), 1); |
437 | | // shl2 may have shifted a bit into the next word, which must be dropped |
438 | 0 | clear_mem(result.mutable_data() + result.size() - 1, 1); |
439 | 0 | }; |
440 | |
|
441 | 0 | auto shl_word = [](const BigInt& a, BigInt& result) { |
442 | | // the most significant word is not copied, aka. shifted out |
443 | 0 | bigint_shl2(result.mutable_data(), a.data(), a.size() - 1 /* ignore msw */, BOTAN_MP_WORD_BITS); |
444 | | // we left-shifted by a full word, the least significant word must be zero'ed |
445 | 0 | clear_mem(result.mutable_data(), 1); |
446 | 0 | }; |
447 | |
|
448 | 0 | BOTAN_ASSERT_NOMSG(size() > 0); |
449 | |
|
450 | 0 | constexpr size_t bits_in_word = sizeof(word) * 8; |
451 | 0 | const size_t word_shift = shift >> ceil_log2(bits_in_word); // shift / bits_in_word |
452 | 0 | const size_t bit_shift = shift & ((1 << ceil_log2(bits_in_word)) - 1); // shift % bits_in_word |
453 | 0 | const size_t iterations = std::max(size(), bits_in_word) - 1; // uint64_t i; i << 64 is undefined behaviour |
454 | | |
455 | | // In every iteration, shift one bit and one word to the left and use the |
456 | | // shift results only when they are within the shift range. |
457 | 0 | BigInt tmp; |
458 | 0 | tmp.resize(size() + 1 /* to hold the shifted-out word */); |
459 | 0 | for(size_t i = 0; i < iterations; ++i) { |
460 | 0 | shl_bit(*this, tmp); |
461 | 0 | ct_cond_assign(i < bit_shift, tmp); |
462 | 0 | shl_word(*this, tmp); |
463 | 0 | ct_cond_assign(i < word_shift, tmp); |
464 | 0 | } |
465 | 0 | } |
466 | | |
467 | 110M | void BigInt::ct_cond_swap(bool predicate, BigInt& other) { |
468 | 110M | const size_t max_words = std::max(size(), other.size()); |
469 | 110M | grow_to(max_words); |
470 | 110M | other.grow_to(max_words); |
471 | | |
472 | 110M | bigint_cnd_swap(static_cast<word>(predicate), this->mutable_data(), other.mutable_data(), max_words); |
473 | 110M | } |
474 | | |
475 | 1.34M | void BigInt::cond_flip_sign(bool predicate) { |
476 | | // This code is assuming Negative == 0, Positive == 1 |
477 | | |
478 | 1.34M | const auto mask = CT::Mask<uint8_t>::expand(predicate); |
479 | | |
480 | 1.34M | const uint8_t current_sign = static_cast<uint8_t>(sign()); |
481 | | |
482 | 1.34M | const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign); |
483 | | |
484 | 1.34M | set_sign(static_cast<Sign>(new_sign)); |
485 | 1.34M | } |
486 | | |
487 | 226k | void BigInt::ct_cond_assign(bool predicate, const BigInt& other) { |
488 | 226k | const size_t t_words = size(); |
489 | 226k | const size_t o_words = other.size(); |
490 | | |
491 | 226k | if(o_words < t_words) { |
492 | 1.61k | grow_to(o_words); |
493 | 1.61k | } |
494 | | |
495 | 226k | const size_t r_words = std::max(t_words, o_words); |
496 | | |
497 | 226k | const auto mask = CT::Mask<word>::expand(predicate); |
498 | | |
499 | 2.06M | for(size_t i = 0; i != r_words; ++i) { |
500 | 1.83M | const word o_word = other.word_at(i); |
501 | 1.83M | const word t_word = this->word_at(i); |
502 | 1.83M | this->set_word_at(i, mask.select(o_word, t_word)); |
503 | 1.83M | } |
504 | | |
505 | 226k | const bool different_sign = sign() != other.sign(); |
506 | 226k | cond_flip_sign(predicate && different_sign); |
507 | 226k | } |
508 | | |
509 | | #if defined(BOTAN_HAS_VALGRIND) |
510 | | void BigInt::const_time_poison() const { |
511 | | CT::poison(m_data.const_data(), m_data.size()); |
512 | | } |
513 | | |
514 | | void BigInt::const_time_unpoison() const { |
515 | | CT::unpoison(m_data.const_data(), m_data.size()); |
516 | | } |
517 | | #endif |
518 | | |
519 | | } // namespace Botan |