/src/Botan-3.13.0/build/include/public/botan/bigint.h
Line | Count | Source |
1 | | /* |
2 | | * BigInt |
3 | | * (C) 1999-2008,2012,2018 Jack Lloyd |
4 | | * 2007 FlexSecure |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_BIGINT_H_ |
10 | | #define BOTAN_BIGINT_H_ |
11 | | |
12 | | #include <botan/secmem.h> |
13 | | #include <botan/types.h> |
14 | | #include <iosfwd> |
15 | | #include <span> |
16 | | #include <string> |
17 | | #include <string_view> |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | | class RandomNumberGenerator; |
22 | | |
23 | | /** |
24 | | * Arbitrary precision integer |
25 | | */ |
26 | | class BOTAN_PUBLIC_API(2, 0) BigInt final { |
27 | | public: |
28 | | /** |
29 | | * Base enumerator for encoding and decoding |
30 | | */ |
31 | | enum Base : uint16_t /* NOLINT(*-use-enum-class) */ { |
32 | | Decimal BOTAN_DEPRECATED("All functions using this enum are deprecated") = 10, |
33 | | Hexadecimal BOTAN_DEPRECATED("All functions using this enum are deprecated") = 16, |
34 | | Binary BOTAN_DEPRECATED("All functions using this enum are deprecated") = 256 |
35 | | }; |
36 | | |
37 | | /** |
38 | | * Sign symbol definitions for positive and negative numbers |
39 | | */ |
40 | | enum Sign : uint8_t /* NOLINT(*-use-enum-class) */ { Negative = 0, Positive = 1 }; |
41 | | |
42 | | /** |
43 | | * Create empty (zero) BigInt |
44 | | */ |
45 | 3.84M | BigInt() = default; |
46 | | |
47 | | /** |
48 | | * Create a 0-value BigInt |
49 | | */ |
50 | 75.3k | static BigInt zero() { return BigInt(); } |
51 | | |
52 | | /** |
53 | | * Create a 1-value BigInt |
54 | | */ |
55 | 0 | static BigInt one() { return BigInt::from_u64(1); } |
56 | | |
57 | | /** |
58 | | * Create BigInt from an unsigned 64 bit integer |
59 | | * @param n initial value of this BigInt |
60 | | */ |
61 | | static BigInt from_u64(uint64_t n); |
62 | | |
63 | | /** |
64 | | * Create BigInt from a word (limb) |
65 | | * @param n initial value of this BigInt |
66 | | */ |
67 | | //BOTAN_DEPRECATED("Use BigInt::from_u64 instead") |
68 | | static BigInt from_word(word n); |
69 | | |
70 | | /** |
71 | | * Create BigInt from a signed 32 bit integer |
72 | | * @param n initial value of this BigInt |
73 | | */ |
74 | | BOTAN_DEPRECATED("Use BigInt::from_u64 plus negation if required instead") static BigInt from_s32(int32_t n); |
75 | | |
76 | | /** |
77 | | * Create BigInt from an unsigned 64 bit integer |
78 | | * @param n initial value of this BigInt |
79 | | * |
80 | | * Prefer BigInt::from_u64 |
81 | | */ |
82 | | BigInt(uint64_t n); // NOLINT(*-explicit-conversions) TODO(Botan4) make this explicit |
83 | | |
84 | | /** |
85 | | * Copy Constructor |
86 | | * @param other the BigInt to copy |
87 | | */ |
88 | 784k | BigInt(const BigInt& other) = default; |
89 | | |
90 | | /** |
91 | | * Create BigInt from a string. If the string starts with 0x the |
92 | | * rest of the string will be interpreted as hexadecimal digits. |
93 | | * Otherwise, it will be interpreted as a decimal number. |
94 | | * |
95 | | * @param str the string to parse for an integer value |
96 | | */ |
97 | | //BOTAN_DEPRECATED("Use BigInt::from_string") |
98 | 162 | explicit BigInt(std::string_view str) { *this = BigInt::from_string(str); } |
99 | | |
100 | | /** |
101 | | * Create BigInt from a string. |
102 | | * |
103 | | * If the string starts with 0x the rest of the string will be |
104 | | * interpreted as hexadecimal digits. Otherwise, it will be |
105 | | * interpreted as a decimal number. |
106 | | * |
107 | | * A prefix of "-" will result in a negative integer |
108 | | * |
109 | | * @param str the string to parse for an integer value |
110 | | */ |
111 | | static BigInt from_string(std::string_view str); |
112 | | |
113 | | /** |
114 | | * Create BigInt from a sequence of digits |
115 | | * |
116 | | * The string is interpreted as a sequence of digits in base @p radix. |
117 | | * |
118 | | * Each character must be interpretable as such a digit; there is no support |
119 | | * for whitespace or prefixes (eg '0x' or '-'). |
120 | | * |
121 | | * Currently radix must be 10 or 16. |
122 | | * |
123 | | * @param digits the sequence of digits |
124 | | * @param radix the base |
125 | | */ |
126 | | static BigInt from_radix_digits(std::string_view digits, size_t radix); |
127 | | |
128 | | /** |
129 | | * Create a BigInt from an integer in a byte array |
130 | | * @param buf the byte array holding the value |
131 | | * @param length size of buf |
132 | | */ |
133 | 20.2k | BigInt(const uint8_t buf[], size_t length) { assign_from_bytes(std::span{buf, length}); } |
134 | | |
135 | | /** |
136 | | * Create a BigInt from an integer in a byte array |
137 | | * @param bytes the byte vector holding the value |
138 | | */ |
139 | 32.2k | explicit BigInt(std::span<const uint8_t> bytes) { assign_from_bytes(bytes); } |
140 | | |
141 | | /** |
142 | | * Create a BigInt from an integer in a byte array |
143 | | * @param buf the byte array holding the value |
144 | | * @param length size of buf |
145 | | * @param base is the number base of the integer in buf |
146 | | */ |
147 | | BOTAN_DEPRECATED("For hex/decimal use from_string") BigInt(const uint8_t buf[], size_t length, Base base); |
148 | | |
149 | | /** |
150 | | * Create a BigInt from an integer in a byte array |
151 | | * |
152 | | * Note this function is primarily used for implementing signature |
153 | | * schemes and is not useful in typical applications. |
154 | | * |
155 | | * @param buf the byte array holding the value |
156 | | * @param length size of buf |
157 | | * @param max_bits if the resulting integer is more than max_bits, |
158 | | * it will be shifted so it is at most max_bits in length. |
159 | | */ |
160 | | BOTAN_DEPRECATED("Deprecated no replacement") |
161 | | static BigInt from_bytes_with_max_bits(const uint8_t buf[], size_t length, size_t max_bits); |
162 | | |
163 | | /** |
164 | | * @brief Create a random BigInt of the specified size |
165 | | * |
166 | | * @param rng random number generator |
167 | | * @param bits size in bits |
168 | | * @param set_high_bit if true, the highest bit is always set |
169 | | * |
170 | | * @see randomize |
171 | | */ |
172 | | BigInt(RandomNumberGenerator& rng, size_t bits, bool set_high_bit = true); |
173 | | |
174 | | /** |
175 | | * Create BigInt of specified size, all zeros |
176 | | * @param n size of the internal register in words |
177 | | */ |
178 | | BOTAN_DEPRECATED("Deprecated no replacement") static BigInt with_capacity(size_t n); |
179 | | |
180 | | /** |
181 | | * Move constructor |
182 | | */ |
183 | 546k | BigInt(BigInt&& other) noexcept { this->swap(other); } |
184 | | |
185 | 5.23M | ~BigInt() { _const_time_unpoison(); } |
186 | | |
187 | | /** |
188 | | * Move assignment |
189 | | */ |
190 | 369k | BigInt& operator=(BigInt&& other) noexcept { |
191 | 369k | if(this != &other) { |
192 | 369k | this->swap(other); |
193 | 369k | } |
194 | | |
195 | 369k | return (*this); |
196 | 369k | } |
197 | | |
198 | | /** |
199 | | * Copy assignment |
200 | | */ |
201 | 160k | BigInt& operator=(const BigInt&) = default; |
202 | | |
203 | | /** |
204 | | * Swap this value with another |
205 | | * @param other BigInt to swap values with |
206 | | */ |
207 | 6.95M | void swap(BigInt& other) noexcept { |
208 | 6.95M | m_data.swap(other.m_data); |
209 | 6.95M | std::swap(m_signedness, other.m_signedness); |
210 | 6.95M | } |
211 | | |
212 | 0 | friend void swap(BigInt& x, BigInt& y) noexcept { x.swap(y); } |
213 | | |
214 | | /** |
215 | | * Swap the internal register with the provided one, leaving the sign unchanged |
216 | | * @param reg the register to swap with |
217 | | */ |
218 | 8.93M | BOTAN_DEPRECATED("Deprecated no replacement") void swap_reg(secure_vector<word>& reg) { |
219 | 8.93M | m_data.swap(reg); |
220 | | // sign left unchanged |
221 | 8.93M | } |
222 | | |
223 | | /** |
224 | | * += operator |
225 | | * @param y the BigInt to add to this |
226 | | */ |
227 | | BigInt& operator+=(const BigInt& y); |
228 | | |
229 | | /** |
230 | | * += operator |
231 | | * @param y the word to add to this |
232 | | */ |
233 | 0 | BigInt& operator+=(word y) { return add(&y, 1, Positive); } |
234 | | |
235 | | /** |
236 | | * -= operator |
237 | | * @param y the BigInt to subtract from this |
238 | | */ |
239 | | BigInt& operator-=(const BigInt& y); |
240 | | |
241 | | /** |
242 | | * -= operator |
243 | | * @param y the word to subtract from this |
244 | | */ |
245 | 0 | BigInt& operator-=(word y) { return sub(&y, 1, Positive); } |
246 | | |
247 | | /** |
248 | | * *= operator |
249 | | * @param y the BigInt to multiply with this |
250 | | */ |
251 | | BigInt& operator*=(const BigInt& y); |
252 | | |
253 | | /** |
254 | | * *= operator |
255 | | * @param y the word to multiply with this |
256 | | */ |
257 | | BigInt& operator*=(word y); |
258 | | |
259 | | /** |
260 | | * /= operator |
261 | | * @param y the BigInt to divide this by |
262 | | */ |
263 | | BigInt& operator/=(const BigInt& y); |
264 | | |
265 | | /** |
266 | | * Modulo operator |
267 | | * @param y the modulus to reduce this by |
268 | | */ |
269 | | BigInt& operator%=(const BigInt& y); |
270 | | |
271 | | /** |
272 | | * Modulo operator |
273 | | * @param y the modulus (word) to reduce this by |
274 | | */ |
275 | | word operator%=(word y); |
276 | | |
277 | | /** |
278 | | * Left shift operator |
279 | | * @param shift the number of bits to shift this left by |
280 | | */ |
281 | | BigInt& operator<<=(size_t shift); |
282 | | |
283 | | /** |
284 | | * Right shift operator |
285 | | * @param shift the number of bits to shift this right by |
286 | | */ |
287 | | BigInt& operator>>=(size_t shift); |
288 | | |
289 | | /** |
290 | | * Increment operator |
291 | | */ |
292 | 0 | BigInt& operator++() { return (*this += 1); } |
293 | | |
294 | | /** |
295 | | * Decrement operator |
296 | | */ |
297 | 0 | BigInt& operator--() { return (*this -= 1); } |
298 | | |
299 | | /** |
300 | | * Postfix increment operator |
301 | | */ |
302 | 0 | BigInt operator++(int) { |
303 | 0 | BigInt x = (*this); |
304 | 0 | ++(*this); |
305 | 0 | return x; |
306 | 0 | } |
307 | | |
308 | | /** |
309 | | * Postfix decrement operator |
310 | | */ |
311 | 0 | BigInt operator--(int) { |
312 | 0 | BigInt x = (*this); |
313 | 0 | --(*this); |
314 | 0 | return x; |
315 | 0 | } |
316 | | |
317 | | /** |
318 | | * Unary negation operator |
319 | | * @return negative this |
320 | | */ |
321 | | BigInt operator-() const; |
322 | | |
323 | | /** |
324 | | * ! operator |
325 | | * @return true iff this is zero, otherwise false |
326 | | */ |
327 | 0 | bool operator!() const { return is_zero(); } |
328 | | |
329 | | //BOTAN_DEPRECATED("Just use operator+/operator-") |
330 | | /** |
331 | | * Add a signed word array to an integer |
332 | | * @param x the first addend |
333 | | * @param y the words of the second addend |
334 | | * @param y_words the number of words in y |
335 | | * @param y_sign the sign of the second addend |
336 | | * @return the sum |
337 | | */ |
338 | | static BigInt add2(const BigInt& x, const word y[], size_t y_words, Sign y_sign); |
339 | | |
340 | | //BOTAN_DEPRECATED("Just use operator+/operator-") |
341 | | /** |
342 | | * Add a signed word array to *this |
343 | | * @param y the words of the addend |
344 | | * @param y_words the number of words in y |
345 | | * @param sign the sign of the addend |
346 | | * @return reference to *this |
347 | | */ |
348 | | BigInt& add(const word y[], size_t y_words, Sign sign); |
349 | | |
350 | | //BOTAN_DEPRECATED("Just use operator+/operator-") |
351 | | /** |
352 | | * Subtract a signed word array from *this |
353 | | * @param y the words of the subtrahend |
354 | | * @param y_words the number of words in y |
355 | | * @param sign the sign of the subtrahend |
356 | | * @return reference to *this |
357 | | */ |
358 | 1.15M | BigInt& sub(const word y[], size_t y_words, Sign sign) { |
359 | 1.15M | return add(y, y_words, sign == Positive ? Negative : Positive); |
360 | 1.15M | } |
361 | | |
362 | | /** |
363 | | * Multiply this with y |
364 | | * @param y the BigInt to multiply with this |
365 | | * @param ws a temp workspace |
366 | | */ |
367 | | BOTAN_DEPRECATED("Just use operator*") BigInt& mul(const BigInt& y, secure_vector<word>& ws); |
368 | | |
369 | | /** |
370 | | * Square value of *this |
371 | | * @param ws a temp workspace |
372 | | */ |
373 | | BOTAN_DEPRECATED("Deprecated no replacement") BigInt& square(secure_vector<word>& ws); |
374 | | |
375 | | /** |
376 | | * Set *this to y - *this |
377 | | * @param y the BigInt to subtract from as a sequence of words |
378 | | * @param y_words length of y in words |
379 | | * @param ws a temp workspace |
380 | | */ |
381 | | BOTAN_DEPRECATED("Deprecated no replacement") |
382 | | BigInt& rev_sub(const word y[], size_t y_words, secure_vector<word>& ws); |
383 | | |
384 | | /** |
385 | | * Set *this to (*this + y) % mod |
386 | | * This function assumes *this is >= 0 && < mod |
387 | | * @param y the BigInt to add - assumed y >= 0 and y < mod |
388 | | * @param mod the positive modulus |
389 | | * @param ws a temp workspace |
390 | | */ |
391 | | BOTAN_DEPRECATED("Deprecated no replacement") |
392 | | BigInt& mod_add(const BigInt& y, const BigInt& mod, secure_vector<word>& ws); |
393 | | |
394 | | /** |
395 | | * Set *this to (*this - y) % mod |
396 | | * This function assumes *this is >= 0 && < mod |
397 | | * @param y the BigInt to subtract - assumed y >= 0 and y < mod |
398 | | * @param mod the positive modulus |
399 | | * @param ws a temp workspace |
400 | | */ |
401 | | BOTAN_DEPRECATED("Deprecated no replacement") |
402 | | BigInt& mod_sub(const BigInt& y, const BigInt& mod, secure_vector<word>& ws); |
403 | | |
404 | | /** |
405 | | * Set *this to (*this * y) % mod |
406 | | * This function assumes *this is >= 0 && < mod |
407 | | * y should be small, less than 16 |
408 | | * @param y the small integer to multiply by |
409 | | * @param mod the positive modulus |
410 | | * @param ws a temp workspace |
411 | | */ |
412 | | BOTAN_DEPRECATED("Deprecated no replacement") |
413 | | BigInt& mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws); |
414 | | |
415 | | /** |
416 | | * Return *this % mod |
417 | | * |
418 | | * Assumes that *this is (if anything) only slightly larger than |
419 | | * mod and performs repeated subtractions. It should not be used if |
420 | | * *this is much larger than mod, instead use modulo operator. |
421 | | */ |
422 | | BOTAN_DEPRECATED("Deprecated no replacement") size_t reduce_below(const BigInt& mod, secure_vector<word>& ws); |
423 | | |
424 | | /** |
425 | | * Return *this % mod |
426 | | * |
427 | | * Assumes that *this is (if anything) only slightly larger than mod and |
428 | | * performs repeated subtractions. It should not be used if *this is much |
429 | | * larger than mod, instead use modulo operator. |
430 | | * |
431 | | * Performs exactly bound subtractions, so if *this is >= bound*mod then the |
432 | | * result will not be fully reduced. If bound is zero, nothing happens. |
433 | | */ |
434 | | BOTAN_DEPRECATED("Deprecated no replacement") |
435 | | void ct_reduce_below(const BigInt& mod, secure_vector<word>& ws, size_t bound); |
436 | | |
437 | | /** |
438 | | * Zeroize the BigInt. The size of the underlying register is not |
439 | | * modified. |
440 | | */ |
441 | 892k | void clear() { |
442 | 892k | m_data.set_to_zero(); |
443 | 892k | m_signedness = Positive; |
444 | 892k | } |
445 | | |
446 | | /** |
447 | | * Compare this to another BigInt |
448 | | * @param n the BigInt value to compare with |
449 | | * @param check_signs include sign in comparison? |
450 | | * @result if (this<n) return -1, if (this>n) return 1, if both |
451 | | * values are identical return 0 [like Perl's <=> operator] |
452 | | */ |
453 | | int32_t cmp(const BigInt& n, bool check_signs = true) const; |
454 | | |
455 | | /** |
456 | | * Compare this to another BigInt |
457 | | * @param n the BigInt value to compare with |
458 | | * @result true if this == n or false otherwise |
459 | | */ |
460 | | bool is_equal(const BigInt& n) const; |
461 | | |
462 | | /** |
463 | | * Compare this to another BigInt |
464 | | * @param n the BigInt value to compare with |
465 | | * @result true if this < n or false otherwise |
466 | | */ |
467 | | bool is_less_than(const BigInt& n) const; |
468 | | |
469 | | /** |
470 | | * Compare this to an integer |
471 | | * @param n the value to compare with |
472 | | * @result if (this<n) return -1, if (this>n) return 1, if both |
473 | | * values are identical return 0 [like Perl's <=> operator] |
474 | | */ |
475 | | int32_t cmp_word(word n) const; |
476 | | |
477 | | /** |
478 | | * Test if the integer has an even value |
479 | | * @result true if the integer is even, false otherwise |
480 | | */ |
481 | 180k | bool is_even() const { return !get_bit(0); } |
482 | | |
483 | | /** |
484 | | * Test if the integer has an odd value |
485 | | * @result true if the integer is odd, false otherwise |
486 | | */ |
487 | 53.0k | bool is_odd() const { return get_bit(0); } |
488 | | |
489 | | /** |
490 | | * Return the signum of this integer |
491 | | * @result -1 if negative, 0 if zero, 1 if positive |
492 | | */ |
493 | 58.1M | int signum() const { |
494 | 58.1M | if(sig_words() == 0) { |
495 | 1.98k | return 0; |
496 | 1.98k | } |
497 | 58.1M | return (sign() == Negative) ? -1 : 1; |
498 | 58.1M | } |
499 | | |
500 | | /** |
501 | | * Test if the integer is not zero |
502 | | * @result true if the integer is non-zero, false otherwise |
503 | | */ |
504 | 0 | BOTAN_DEPRECATED("Use signum() != 0") bool is_nonzero() const { return signum() != 0; } |
505 | | |
506 | | /** |
507 | | * Test if the integer is zero |
508 | | * @result true if the integer is zero, false otherwise |
509 | | */ |
510 | 5.06M | bool is_zero() const { return sig_words() == 0; } |
511 | | |
512 | | /** |
513 | | * Set bit at specified position |
514 | | * @param n bit position to set |
515 | | */ |
516 | 117k | void set_bit(size_t n) { conditionally_set_bit(n, true); } |
517 | | |
518 | | /** |
519 | | * Conditionally set bit at specified position. Note if set_it is |
520 | | * false, nothing happens, and if the bit is already set, it |
521 | | * remains set. |
522 | | * |
523 | | * @param n bit position to set |
524 | | * @param set_it if the bit should be set |
525 | | */ |
526 | 117k | void conditionally_set_bit(size_t n, bool set_it) { |
527 | 117k | const size_t which = n / (sizeof(word) * 8); |
528 | 117k | const word mask = static_cast<word>(set_it) << (n % (sizeof(word) * 8)); |
529 | 117k | m_data.set_word_at(which, word_at(which) | mask); |
530 | 117k | } |
531 | | |
532 | | /** |
533 | | * Clear bit at specified position |
534 | | * @param n bit position to clear |
535 | | */ |
536 | | void clear_bit(size_t n); |
537 | | |
538 | | /** |
539 | | * Clear all but the lowest n bits |
540 | | * @param n amount of bits to keep |
541 | | */ |
542 | 0 | BOTAN_DEPRECATED("Deprecated no replacement") void mask_bits(size_t n) { m_data.mask_bits(n); } |
543 | | |
544 | | /** |
545 | | * Return bit value at specified position |
546 | | * @param n the bit offset to test |
547 | | * @result true, if the bit at position n is set, false otherwise |
548 | | */ |
549 | 233k | bool get_bit(size_t n) const { return ((word_at(n / (sizeof(word) * 8)) >> (n % (sizeof(word) * 8))) & 1) == 1; } |
550 | | |
551 | | /** |
552 | | * Return (a maximum of) 32 bits of the complete value |
553 | | * @param offset the offset to start extracting |
554 | | * @param length amount of bits to extract (starting at offset) |
555 | | * @result the integer extracted from the register starting at |
556 | | * offset with specified length |
557 | | */ |
558 | | BOTAN_DEPRECATED("Deprecated no replacement") uint32_t get_substring(size_t offset, size_t length) const; |
559 | | |
560 | | /** |
561 | | * Convert this value into a uint32_t, if it is in the range |
562 | | * [0 ... 2**32-1], or otherwise throw an exception. |
563 | | * @result the value as a uint32_t if conversion is possible |
564 | | */ |
565 | | BOTAN_DEPRECATED("Deprecated no replacement") uint32_t to_u32bit() const; |
566 | | |
567 | | /** |
568 | | * Convert this value to a decimal string. |
569 | | * Warning: decimal conversions are relatively slow |
570 | | * |
571 | | * If the integer is zero then "0" is returned. |
572 | | * If the integer is negative then "-" is prefixed. |
573 | | */ |
574 | | std::string to_dec_string() const; |
575 | | |
576 | | /** |
577 | | * Convert this value to a hexadecimal string. |
578 | | * |
579 | | * If the integer is negative then "-" is prefixed. |
580 | | * Then a prefix of "0x" is added. |
581 | | * Follows is a sequence of hexadecimal characters in uppercase. |
582 | | * |
583 | | * The number of hexadecimal characters is always an even number, |
584 | | * with a zero prefix being included if necessary. |
585 | | * For example encoding the integer "5" results in "0x05" |
586 | | */ |
587 | | std::string to_hex_string() const; |
588 | | |
589 | | /** |
590 | | * Return a byte of the big-endian encoding of this integer |
591 | | * @param n the offset to get a byte from |
592 | | * @result byte at offset n |
593 | | */ |
594 | | uint8_t byte_at(size_t n) const; |
595 | | |
596 | | /** |
597 | | * Return the word at a specified position of the internal register |
598 | | * @param n position in the register |
599 | | * @return value at position n |
600 | | */ |
601 | 11.8M | word word_at(size_t n) const { return m_data.get_word_at(n); } |
602 | | |
603 | | /** |
604 | | * Set the word at a specified position of the internal register |
605 | | * @param i position in the register |
606 | | * @param w the value to set |
607 | | */ |
608 | 5.51M | BOTAN_DEPRECATED("Deprecated no replacement") void set_word_at(size_t i, word w) { m_data.set_word_at(i, w); } |
609 | | |
610 | | /** |
611 | | * Replace the internal register with the given words |
612 | | * @param w the words to set |
613 | | * @param len the number of words in w |
614 | | */ |
615 | 1.38M | BOTAN_DEPRECATED("Deprecated no replacement") void set_words(const word w[], size_t len) { |
616 | 1.38M | m_data.set_words(w, len); |
617 | 1.38M | } |
618 | | |
619 | | /** |
620 | | * Tests if the sign of the integer is negative |
621 | | * @result true, iff the integer has a negative sign |
622 | | */ |
623 | 0 | BOTAN_DEPRECATED("Use signum() < 0") bool is_negative() const { return signum() < 0; } |
624 | | |
625 | | /** |
626 | | * Tests if the sign of the integer is positive |
627 | | * |
628 | | * Note that this is testing the sign, thus it returns true also for zero |
629 | | * Prefer signum which is unambiguous |
630 | | * |
631 | | * @result true, iff the integer has a positive sign |
632 | | */ |
633 | 0 | BOTAN_DEPRECATED("Use signum() >= 0 or signum() > 0 as appropriate") bool is_positive() const { |
634 | 0 | return signum() >= 0; |
635 | 0 | } |
636 | | |
637 | | /** |
638 | | * Return the sign of the integer |
639 | | * @result the sign of the integer |
640 | | */ |
641 | 61.9M | Sign sign() const { return (m_signedness); } |
642 | | |
643 | | /** |
644 | | * Return the sign opposite to that of this integer |
645 | | * @result the opposite sign of the represented integer value |
646 | | */ |
647 | 331 | Sign reverse_sign() const { |
648 | 331 | if(sign() == Positive) { |
649 | 331 | return Negative; |
650 | 331 | } |
651 | 0 | return Positive; |
652 | 331 | } |
653 | | |
654 | | /** |
655 | | * Flip the sign of this BigInt |
656 | | */ |
657 | 0 | BOTAN_DEPRECATED("Deprecated no replacement") void flip_sign() { set_sign(reverse_sign()); } |
658 | | |
659 | | /** |
660 | | * Set sign of the integer |
661 | | * @param sign new Sign to set |
662 | | */ |
663 | 1.28M | void set_sign(Sign sign) { |
664 | 1.28M | if(sign == Negative && is_zero()) { |
665 | 0 | sign = Positive; |
666 | 0 | } |
667 | | |
668 | 1.28M | m_signedness = sign; |
669 | 1.28M | } |
670 | | |
671 | | /** |
672 | | * Return the absolute value of this integer |
673 | | * @result absolute (positive) value of this |
674 | | */ |
675 | | BigInt abs() const; |
676 | | |
677 | | /** |
678 | | * Give size of internal register |
679 | | * @result size of internal register in words |
680 | | */ |
681 | 147M | size_t size() const { return m_data.size(); } |
682 | | |
683 | | /** |
684 | | * Return how many words we need to hold this value |
685 | | * @result significant words of the represented integer value |
686 | | */ |
687 | 88.6M | size_t sig_words() const { return m_data.sig_words(); } |
688 | | |
689 | | /** |
690 | | * Give byte length of the integer |
691 | | * @result byte length of the represented integer value |
692 | | */ |
693 | | size_t bytes() const; |
694 | | |
695 | | /** |
696 | | * Get the bit length of the integer |
697 | | * @result bit length of the represented integer value |
698 | | */ |
699 | | size_t bits() const; |
700 | | |
701 | | /** |
702 | | * Get the number of high bits unset in the top (allocated) word |
703 | | * of this integer. Returns (sizeof(word) * 8) only iff *this is |
704 | | * zero. Ignores sign. |
705 | | */ |
706 | | BOTAN_DEPRECATED("Deprecated no replacement") size_t top_bits_free() const; |
707 | | |
708 | | /** |
709 | | * Return a mutable pointer to the register |
710 | | * @result a pointer to the start of the internal register |
711 | | */ |
712 | 72.3M | BOTAN_DEPRECATED("Deprecated no replacement") word* mutable_data() { return m_data.mutable_data(); } |
713 | | |
714 | | /** |
715 | | * Return a const pointer to the register |
716 | | * @result a pointer to the start of the internal register |
717 | | */ |
718 | 716 | BOTAN_DEPRECATED("Deprecated no replacement") const word* data() const { return m_data.const_data(); } |
719 | | |
720 | | /** |
721 | | * Don't use this function in application code |
722 | | */ |
723 | 5.51M | BOTAN_DEPRECATED("Deprecated no replacement") secure_vector<word>& get_word_vector() { |
724 | 5.51M | return m_data.mutable_vector(); |
725 | 5.51M | } |
726 | | |
727 | | /** |
728 | | * Don't use this function in application code |
729 | | */ |
730 | 0 | BOTAN_DEPRECATED("Deprecated no replacement") const secure_vector<word>& get_word_vector() const { |
731 | 0 | return m_data.const_vector(); |
732 | 0 | } |
733 | | |
734 | | /** |
735 | | * Increase internal register buffer to at least n words |
736 | | * @param n new size of register |
737 | | */ |
738 | 26.2M | BOTAN_DEPRECATED("Deprecated no replacement") void grow_to(size_t n) const { m_data.grow_to(n); } |
739 | | |
740 | | /** |
741 | | * Resize the internal register, adjusting the sign if the value becomes zero |
742 | | * @param s the new size of the register in words |
743 | | */ |
744 | 0 | BOTAN_DEPRECATED("Deprecated no replacement") void resize(size_t s) { |
745 | 0 | m_data.resize(s); |
746 | 0 | set_sign(sign()); // handle possible zero |
747 | 0 | } |
748 | | |
749 | | /** |
750 | | * Fill BigInt with a random number with size of bitsize |
751 | | * |
752 | | * If \p set_high_bit is true, the highest bit will be set, which causes |
753 | | * the entropy to be \a bits-1. Otherwise the highest bit is randomly chosen |
754 | | * by the rng, causing the entropy to be \a bits. |
755 | | * |
756 | | * @param rng the random number generator to use |
757 | | * @param bitsize number of bits the created random value should have |
758 | | * @param set_high_bit if true, the highest bit is always set |
759 | | */ |
760 | | void randomize(RandomNumberGenerator& rng, size_t bitsize, bool set_high_bit = true); |
761 | | |
762 | | /** |
763 | | * Serialize the absolute value of this BigInt as a big endian |
764 | | * encoding. |
765 | | * |
766 | | * If out is smaller than the total bytes of the BigInt then |
767 | | * an exception is thrown. |
768 | | * |
769 | | * If out is larger than the total bytes of the BigInt then the |
770 | | * necessary number of zeros are prefixed to produce the desired |
771 | | * output length |
772 | | * |
773 | | * Zero-padding the binary encoding is useful to ensure that other |
774 | | * applications correctly parse the encoded value as "positive integer", |
775 | | * as a leading 1-bit may be interpreted as a sign bit. It also is |
776 | | * necessary when using a fixed size encoding for the integers. |
777 | | * |
778 | | * @param out destination byte span for the integer value |
779 | | */ |
780 | | void serialize_to(std::span<uint8_t> out) const; |
781 | | |
782 | | /** |
783 | | * Serialize the value of this BigInt as a big endian encoding, |
784 | | * always returning the specified number of bytes. |
785 | | * |
786 | | * Throws if the BigInt is too large to encode in the length |
787 | | * specified. |
788 | | */ |
789 | | template <typename T = std::vector<uint8_t>> |
790 | 111k | T serialize(size_t len) const { |
791 | | // TODO this supports std::vector and secure_vector |
792 | | // it would be nice if this also could work with std::array as in |
793 | | // bn.serialize_to<std::array<uint8_t, 32>>(32); |
794 | 111k | T out(len); |
795 | 111k | this->serialize_to(out); |
796 | 111k | return out; |
797 | 111k | } std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > Botan::BigInt::serialize<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(unsigned long) const Line | Count | Source | 790 | 111k | T serialize(size_t len) const { | 791 | | // TODO this supports std::vector and secure_vector | 792 | | // it would be nice if this also could work with std::array as in | 793 | | // bn.serialize_to<std::array<uint8_t, 32>>(32); | 794 | 111k | T out(len); | 795 | 111k | this->serialize_to(out); | 796 | 111k | return out; | 797 | 111k | } |
Unexecuted instantiation: std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > Botan::BigInt::serialize<std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > >(unsigned long) const |
798 | | |
799 | | /** |
800 | | * Serialize the value of this BigInt as a big endian encoding. |
801 | | */ |
802 | | template <typename T = std::vector<uint8_t>> |
803 | 18.9k | T serialize() const { |
804 | 18.9k | return serialize<T>(this->bytes()); |
805 | 18.9k | } std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > Botan::BigInt::serialize<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >() const Line | Count | Source | 803 | 18.9k | T serialize() const { | 804 | 18.9k | return serialize<T>(this->bytes()); | 805 | 18.9k | } |
Unexecuted instantiation: std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > Botan::BigInt::serialize<std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > >() const |
806 | | |
807 | | /** |
808 | | * Store BigInt-value in a given byte array |
809 | | * @param buf destination byte array for the integer value |
810 | | */ |
811 | 0 | BOTAN_DEPRECATED("Use BigInt::serialize_to") void binary_encode(uint8_t buf[]) const { |
812 | 0 | this->serialize_to(std::span{buf, this->bytes()}); |
813 | 0 | } |
814 | | |
815 | | /** |
816 | | * Store BigInt-value in a given byte array. If len is less than |
817 | | * the size of the value, then it will be truncated. If len is |
818 | | * greater than the size of the value, it will be zero-padded. |
819 | | * If len exactly equals this->bytes(), this function behaves identically |
820 | | * to binary_encode. |
821 | | * |
822 | | * Zero-padding the binary encoding is useful to ensure that other |
823 | | * applications correctly parse the encoded value as "positive integer", |
824 | | * as a leading 1-bit may be interpreted as a sign bit. |
825 | | * |
826 | | * @param buf destination byte array for the integer value |
827 | | * @param len how many bytes to write |
828 | | */ |
829 | | BOTAN_DEPRECATED("Use BigInt::serialize_to") void binary_encode(uint8_t buf[], size_t len) const; |
830 | | |
831 | | /** |
832 | | * Read integer value from a byte array with given size |
833 | | * @param buf byte array buffer containing the integer |
834 | | * @param length size of buf |
835 | | */ |
836 | 0 | BOTAN_DEPRECATED("Use BigInt::from_bytes") void binary_decode(const uint8_t buf[], size_t length) { |
837 | 0 | this->assign_from_bytes(std::span{buf, length}); |
838 | 0 | } |
839 | | |
840 | | /** |
841 | | * Read integer value from a byte vector |
842 | | * @param buf the vector to load from |
843 | | */ |
844 | 0 | BOTAN_DEPRECATED("Use BigInt::from_bytes") void binary_decode(std::span<const uint8_t> buf) { |
845 | 0 | this->assign_from_bytes(buf); |
846 | 0 | } |
847 | | |
848 | | /** |
849 | | * Place the value into out, zero-padding up to size words |
850 | | * Throw if *this cannot be represented in size words |
851 | | */ |
852 | | BOTAN_DEPRECATED("Deprecated no replacement") void encode_words(word out[], size_t size) const; |
853 | | |
854 | | /** |
855 | | * If predicate is true assign other to *this |
856 | | * Uses a masked operation to avoid side channels |
857 | | */ |
858 | | BOTAN_DEPRECATED("Deprecated no replacement") void ct_cond_assign(bool predicate, const BigInt& other); |
859 | | |
860 | | /** |
861 | | * If predicate is true swap *this and other |
862 | | * Uses a masked operation to avoid side channels |
863 | | */ |
864 | | BOTAN_DEPRECATED("Deprecated no replacement") void ct_cond_swap(bool predicate, BigInt& other); |
865 | | |
866 | | /** |
867 | | * If predicate is true add value to *this |
868 | | */ |
869 | | BOTAN_DEPRECATED("Deprecated no replacement") void ct_cond_add(bool predicate, const BigInt& value); |
870 | | |
871 | | /** |
872 | | * Shift @p shift bits to the left, runtime is independent of |
873 | | * the value of @p shift. |
874 | | */ |
875 | | BOTAN_DEPRECATED("Deprecated no replacement") void ct_shift_left(size_t shift); |
876 | | |
877 | | /** |
878 | | * If predicate is true flip the sign of *this |
879 | | */ |
880 | | void cond_flip_sign(bool predicate); |
881 | | |
882 | | /** |
883 | | * Mark this value as secret for constant time analysis tooling |
884 | | */ |
885 | 0 | BOTAN_DEPRECATED("replaced by internal API") void const_time_poison() const { _const_time_poison(); } |
886 | | |
887 | | /** |
888 | | * Mark this value as no longer secret for constant time analysis tooling |
889 | | */ |
890 | 0 | BOTAN_DEPRECATED("replaced by internal API") void const_time_unpoison() const { _const_time_unpoison(); } |
891 | | |
892 | | /** |
893 | | * Generate a random integer within a range |
894 | | * @param rng a random number generator |
895 | | * @param min the minimum value (must be non-negative) |
896 | | * @param max the maximum value (must be non-negative and > min) |
897 | | * @return random integer in [min,max) |
898 | | */ |
899 | | static BigInt random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max); |
900 | | |
901 | | /** |
902 | | * Create a power of two |
903 | | * @param n the power of two to create |
904 | | * @return bigint representing 2^n |
905 | | */ |
906 | 52.7k | static BigInt power_of_2(size_t n) { |
907 | 52.7k | BigInt b; |
908 | 52.7k | b.set_bit(n); |
909 | 52.7k | return b; |
910 | 52.7k | } |
911 | | |
912 | | /** |
913 | | * Encode the integer value from a BigInt to a std::vector of bytes |
914 | | * @param n the BigInt to use as integer source |
915 | | * @result secure_vector of bytes containing the bytes of the integer |
916 | | */ |
917 | 0 | BOTAN_DEPRECATED("Use BigInt::serialize") static std::vector<uint8_t> encode(const BigInt& n) { |
918 | 0 | return n.serialize<std::vector<uint8_t>>(n.bytes()); |
919 | 0 | } |
920 | | |
921 | | /** |
922 | | * Encode the integer value from a BigInt to a secure_vector of bytes |
923 | | * @param n the BigInt to use as integer source |
924 | | * @result secure_vector of bytes containing the bytes of the integer |
925 | | */ |
926 | 0 | BOTAN_DEPRECATED("Use BigInt::serialize") static secure_vector<uint8_t> encode_locked(const BigInt& n) { |
927 | 0 | return n.serialize<secure_vector<uint8_t>>(n.bytes()); |
928 | 0 | } |
929 | | |
930 | | /** |
931 | | * Create a BigInt from an integer in a byte array |
932 | | * @param bytes the binary value to load |
933 | | * @result BigInt representing the integer in the byte array |
934 | | */ |
935 | | static BigInt from_bytes(std::span<const uint8_t> bytes); |
936 | | |
937 | | /** |
938 | | * Create a BigInt from an integer in a byte array |
939 | | * @param buf the binary value to load |
940 | | * @param length size of buf |
941 | | * @result BigInt representing the integer in the byte array |
942 | | */ |
943 | 7.13k | BOTAN_DEPRECATED("Use BigInt::from_bytes") static BigInt decode(const uint8_t buf[], size_t length) { |
944 | 7.13k | return BigInt::from_bytes(std::span{buf, length}); |
945 | 7.13k | } |
946 | | |
947 | | /** |
948 | | * Create a BigInt from an integer in a byte array |
949 | | * @param buf the binary value to load |
950 | | * @result BigInt representing the integer in the byte array |
951 | | */ |
952 | 0 | BOTAN_DEPRECATED("Use BigInt::from_bytes") static BigInt decode(std::span<const uint8_t> buf) { |
953 | 0 | return BigInt::from_bytes(buf); |
954 | 0 | } |
955 | | |
956 | | /** |
957 | | * Create a BigInt from an integer in a byte array |
958 | | * @param buf the binary value to load |
959 | | * @param length size of buf |
960 | | * @param base number-base of the integer in buf |
961 | | * @result BigInt representing the integer in the byte array |
962 | | */ |
963 | | BOTAN_DEPRECATED("For decimal/hex use from_string") |
964 | | static BigInt decode(const uint8_t buf[], size_t length, Base base); |
965 | | |
966 | | /** |
967 | | * Create a BigInt from an integer in a byte array |
968 | | * @param buf the binary value to load |
969 | | * @param base number-base of the integer in buf |
970 | | * @result BigInt representing the integer in the byte array |
971 | | */ |
972 | | BOTAN_DEPRECATED("For decimal/hex use from_string") static BigInt decode(std::span<const uint8_t> buf, Base base); |
973 | | |
974 | | /** |
975 | | * Encode a BigInt to a byte array according to IEEE 1363 |
976 | | * @param n the BigInt to encode |
977 | | * @param bytes the length of the resulting secure_vector<uint8_t> |
978 | | * @result a secure_vector<uint8_t> containing the encoded BigInt |
979 | | */ |
980 | | BOTAN_DEPRECATED("Use BigInt::serialize") |
981 | 0 | static secure_vector<uint8_t> encode_1363(const BigInt& n, size_t bytes) { |
982 | 0 | return n.serialize<secure_vector<uint8_t>>(bytes); |
983 | 0 | } |
984 | | |
985 | | /** |
986 | | * Encode an integer as a fixed length big-endian string per IEEE 1363 |
987 | | * @param out the buffer to write to; its size determines the encoding length |
988 | | * @param n the integer to encode |
989 | | */ |
990 | 0 | BOTAN_DEPRECATED("Use BigInt::serialize_to") static void encode_1363(std::span<uint8_t> out, const BigInt& n) { |
991 | 0 | n.serialize_to(out); |
992 | 0 | } |
993 | | |
994 | | /** |
995 | | * Encode an integer as a fixed length big-endian string per IEEE 1363 |
996 | | * @param out the buffer to write to |
997 | | * @param bytes the length of the encoding |
998 | | * @param n the integer to encode |
999 | | */ |
1000 | | BOTAN_DEPRECATED("Use BigInt::serialize_to") |
1001 | 19.7k | static void encode_1363(uint8_t out[], size_t bytes, const BigInt& n) { |
1002 | 19.7k | n.serialize_to(std::span{out, bytes}); |
1003 | 19.7k | } |
1004 | | |
1005 | | /** |
1006 | | * Encode two BigInt to a byte array according to IEEE 1363 |
1007 | | * @param n1 the first BigInt to encode |
1008 | | * @param n2 the second BigInt to encode |
1009 | | * @param bytes the length of the encoding of each single BigInt |
1010 | | * @result a secure_vector<uint8_t> containing the concatenation of the two encoded BigInt |
1011 | | */ |
1012 | | BOTAN_DEPRECATED("Deprecated no replacement") |
1013 | | static secure_vector<uint8_t> encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes); |
1014 | | |
1015 | | /** |
1016 | | * Return a span over the register |
1017 | | * |
1018 | | * @warning this is an implementation detail which is not for |
1019 | | * public use and not covered by SemVer. |
1020 | | * |
1021 | | * @result span over the internal register |
1022 | | */ |
1023 | 202k | std::span<const word> _as_span() const { return m_data.const_span(); } |
1024 | | |
1025 | | /** |
1026 | | * Return a const pointer to the register |
1027 | | * |
1028 | | * @warning this is an implementation detail which is not for |
1029 | | * public use and not covered by SemVer. |
1030 | | * |
1031 | | * @result a pointer to the start of the internal register |
1032 | | */ |
1033 | 154M | const word* _data() const { return m_data.const_data(); } |
1034 | | |
1035 | | /** |
1036 | | * Read integer value from a byte vector (big endian) |
1037 | | * |
1038 | | * @warning this is an implementation detail which is not for |
1039 | | * public use and not covered by SemVer. In applications use |
1040 | | * BigInt::from_bytes |
1041 | | * |
1042 | | * @param bytes the span of bytes to load |
1043 | | */ |
1044 | 492k | void _assign_from_bytes(std::span<const uint8_t> bytes) { assign_from_bytes(bytes); } |
1045 | | |
1046 | | /** |
1047 | | * Create a BigInt from a word vector |
1048 | | * |
1049 | | * @warning this is an implementation detail which is not for |
1050 | | * public use and not covered by SemVer. |
1051 | | */ |
1052 | 219k | static BigInt _from_words(secure_vector<word>& words) { |
1053 | 219k | BigInt bn; |
1054 | 219k | bn.m_data.swap(words); |
1055 | 219k | return bn; |
1056 | 219k | } |
1057 | | |
1058 | | /** |
1059 | | * Mark this BigInt as holding secret data |
1060 | | * |
1061 | | * @warning this is an implementation detail which is not for |
1062 | | * public use and not covered by SemVer. |
1063 | | */ |
1064 | | void _const_time_poison() const; |
1065 | | |
1066 | | /** |
1067 | | * Mark this BigInt as no longer holding secret data |
1068 | | * |
1069 | | * @warning this is an implementation detail which is not for |
1070 | | * public use and not covered by SemVer. |
1071 | | */ |
1072 | | void _const_time_unpoison() const; |
1073 | | |
1074 | | private: |
1075 | | /** |
1076 | | * Read integer value from a byte vector (big endian) |
1077 | | * @param bytes the span of bytes to load |
1078 | | */ |
1079 | | void assign_from_bytes(std::span<const uint8_t> bytes); |
1080 | | |
1081 | | class Data final { |
1082 | | public: |
1083 | 73.5M | word* mutable_data() { |
1084 | 73.5M | invalidate_sig_words(); |
1085 | 73.5M | return m_reg.data(); |
1086 | 73.5M | } |
1087 | | |
1088 | 159M | const word* const_data() const { return m_reg.data(); } |
1089 | | |
1090 | 202k | std::span<const word> const_span() const { return std::span{m_reg}; } |
1091 | | |
1092 | 5.51M | secure_vector<word>& mutable_vector() { |
1093 | 5.51M | invalidate_sig_words(); |
1094 | 5.51M | return m_reg; |
1095 | 5.51M | } |
1096 | | |
1097 | 0 | const secure_vector<word>& const_vector() const { return m_reg; } |
1098 | | |
1099 | 11.8M | word get_word_at(size_t n) const { |
1100 | 11.8M | if(n < m_reg.size()) { |
1101 | 11.7M | return m_reg[n]; |
1102 | 11.7M | } |
1103 | 117k | return 0; |
1104 | 11.8M | } |
1105 | | |
1106 | 5.63M | void set_word_at(size_t i, word w) { |
1107 | 5.63M | invalidate_sig_words(); |
1108 | 5.63M | if(i >= m_reg.size()) { |
1109 | 5.63M | if(w == 0) { |
1110 | 5.51M | return; |
1111 | 5.51M | } |
1112 | 117k | grow_to(i + 1); |
1113 | 117k | } |
1114 | 117k | m_reg[i] = w; |
1115 | 117k | } |
1116 | | |
1117 | 1.38M | void set_words(const word w[], size_t len) { |
1118 | 1.38M | invalidate_sig_words(); |
1119 | 1.38M | m_reg.assign(w, w + len); |
1120 | 1.38M | } |
1121 | | |
1122 | | void set_to_zero(); |
1123 | | |
1124 | | void mask_bits(size_t n); |
1125 | | |
1126 | 26.4M | void grow_to(size_t n) const { |
1127 | 26.4M | if(n > size()) { |
1128 | 2.09M | if(n <= m_reg.capacity()) { |
1129 | 0 | m_reg.resize(n); |
1130 | 2.09M | } else { |
1131 | 2.09M | m_reg.resize(n + (8 - (n % 8))); |
1132 | 2.09M | } |
1133 | 2.09M | } |
1134 | 26.4M | } |
1135 | | |
1136 | 180M | size_t size() const { return m_reg.size(); } |
1137 | | |
1138 | 0 | void shrink_to_fit(size_t min_size = 0) { |
1139 | 0 | const size_t words = std::max(min_size, sig_words()); |
1140 | 0 | m_reg.resize(words); |
1141 | 0 | } |
1142 | | |
1143 | 0 | void resize(size_t s) { |
1144 | 0 | const bool shrinking = s < m_reg.size(); |
1145 | 0 | m_reg.resize(s); |
1146 | 0 | if(shrinking) { |
1147 | 0 | invalidate_sig_words(); |
1148 | 0 | } |
1149 | 0 | } |
1150 | | |
1151 | 6.95M | void swap(Data& other) noexcept { |
1152 | 6.95M | m_reg.swap(other.m_reg); |
1153 | 6.95M | std::swap(m_sig_words, other.m_sig_words); |
1154 | 6.95M | } |
1155 | | |
1156 | 10.0M | void swap(secure_vector<word>& reg) noexcept { |
1157 | 10.0M | m_reg.swap(reg); |
1158 | 10.0M | invalidate_sig_words(); |
1159 | 10.0M | } |
1160 | | |
1161 | 96.1M | void invalidate_sig_words() const noexcept { m_sig_words = sig_words_npos; } |
1162 | | |
1163 | 88.6M | size_t sig_words() const { |
1164 | 88.6M | if(m_sig_words == sig_words_npos) { |
1165 | 36.7M | m_sig_words = calc_sig_words(); |
1166 | 36.7M | } |
1167 | 88.6M | return m_sig_words; |
1168 | 88.6M | } |
1169 | | |
1170 | | private: |
1171 | | static const size_t sig_words_npos = static_cast<size_t>(-1); |
1172 | | |
1173 | | size_t calc_sig_words() const; |
1174 | | |
1175 | | mutable secure_vector<word> m_reg; |
1176 | | mutable size_t m_sig_words = sig_words_npos; |
1177 | | }; |
1178 | | |
1179 | | Data m_data; |
1180 | | Sign m_signedness = Positive; |
1181 | | }; |
1182 | | |
1183 | | /** |
1184 | | * Add two integers |
1185 | | * @param x the first addend |
1186 | | * @param y the second addend |
1187 | | * @return (x + y) |
1188 | | */ |
1189 | 40.6k | inline BigInt operator+(const BigInt& x, const BigInt& y) { |
1190 | 40.6k | return BigInt::add2(x, y._data(), y.sig_words(), y.sign()); |
1191 | 40.6k | } |
1192 | | |
1193 | | /** |
1194 | | * Add a word to an integer |
1195 | | * @param x the first addend |
1196 | | * @param y the second addend |
1197 | | * @return (x + y) |
1198 | | */ |
1199 | 0 | inline BigInt operator+(const BigInt& x, word y) { |
1200 | 0 | return BigInt::add2(x, &y, 1, BigInt::Positive); |
1201 | 0 | } |
1202 | | |
1203 | | /** |
1204 | | * Add an integer to a word |
1205 | | * @param x the first addend |
1206 | | * @param y the second addend |
1207 | | * @return (x + y) |
1208 | | */ |
1209 | 0 | inline BigInt operator+(word x, const BigInt& y) { |
1210 | 0 | return y + x; |
1211 | 0 | } |
1212 | | |
1213 | | /** |
1214 | | * Subtract two integers |
1215 | | * @param x the minuend |
1216 | | * @param y the subtrahend |
1217 | | * @return (x - y) |
1218 | | */ |
1219 | 331 | inline BigInt operator-(const BigInt& x, const BigInt& y) { |
1220 | 331 | return BigInt::add2(x, y._data(), y.sig_words(), y.reverse_sign()); |
1221 | 331 | } |
1222 | | |
1223 | | /** |
1224 | | * Subtract a word from an integer |
1225 | | * @param x the minuend |
1226 | | * @param y the subtrahend |
1227 | | * @return (x - y) |
1228 | | */ |
1229 | 27 | inline BigInt operator-(const BigInt& x, word y) { |
1230 | 27 | return BigInt::add2(x, &y, 1, BigInt::Negative); |
1231 | 27 | } |
1232 | | |
1233 | | /** |
1234 | | * Multiply two integers |
1235 | | * @param x the first factor |
1236 | | * @param y the second factor |
1237 | | * @return (x * y) |
1238 | | */ |
1239 | | BOTAN_PUBLIC_API(2, 0) BigInt operator*(const BigInt& x, const BigInt& y); |
1240 | | |
1241 | | /** |
1242 | | * Multiply an integer by a word |
1243 | | * @param x the first factor |
1244 | | * @param y the second factor |
1245 | | * @return (x * y) |
1246 | | */ |
1247 | | BOTAN_PUBLIC_API(2, 8) BigInt operator*(const BigInt& x, word y); |
1248 | | |
1249 | | /** |
1250 | | * Multiply a word by an integer |
1251 | | * @param x the first factor |
1252 | | * @param y the second factor |
1253 | | * @return (x * y) |
1254 | | */ |
1255 | 1.13M | inline BigInt operator*(word x, const BigInt& y) { |
1256 | 1.13M | return y * x; |
1257 | 1.13M | } |
1258 | | |
1259 | | /** |
1260 | | * Divide two integers |
1261 | | * @param x the dividend |
1262 | | * @param d the divisor |
1263 | | * @return (x / d), rounded towards zero |
1264 | | */ |
1265 | | BOTAN_PUBLIC_API(2, 0) BigInt operator/(const BigInt& x, const BigInt& d); |
1266 | | |
1267 | | /** |
1268 | | * Divide an integer by a word |
1269 | | * @param x the dividend |
1270 | | * @param m the divisor |
1271 | | * @return (x / m), rounded towards zero |
1272 | | */ |
1273 | | BOTAN_PUBLIC_API(2, 0) BigInt operator/(const BigInt& x, word m); |
1274 | | |
1275 | | /** |
1276 | | * Reduce an integer modulo another |
1277 | | * @param x the value to reduce |
1278 | | * @param m the modulus |
1279 | | * @return (x % m) |
1280 | | */ |
1281 | | BOTAN_PUBLIC_API(2, 0) BigInt operator%(const BigInt& x, const BigInt& m); |
1282 | | |
1283 | | /** |
1284 | | * Reduce an integer modulo a word |
1285 | | * @param x the value to reduce |
1286 | | * @param m the modulus |
1287 | | * @return (x % m) |
1288 | | */ |
1289 | | BOTAN_PUBLIC_API(2, 0) word operator%(const BigInt& x, word m); |
1290 | | |
1291 | | /** |
1292 | | * Shift an integer left |
1293 | | * @param x the value to shift |
1294 | | * @param n the number of bits to shift by |
1295 | | * @return (x << n) |
1296 | | */ |
1297 | | BOTAN_PUBLIC_API(2, 0) BigInt operator<<(const BigInt& x, size_t n); |
1298 | | |
1299 | | /** |
1300 | | * Shift an integer right |
1301 | | * @param x the value to shift |
1302 | | * @param n the number of bits to shift by |
1303 | | * @return (x >> n) |
1304 | | */ |
1305 | | BOTAN_PUBLIC_API(2, 0) BigInt operator>>(const BigInt& x, size_t n); |
1306 | | |
1307 | | /** |
1308 | | * Compare two integers |
1309 | | * @param a the first operand |
1310 | | * @param b the second operand |
1311 | | * @return true if a is equal to b |
1312 | | */ |
1313 | 43.3k | inline bool operator==(const BigInt& a, const BigInt& b) { |
1314 | 43.3k | return a.is_equal(b); |
1315 | 43.3k | } |
1316 | | |
1317 | | /** |
1318 | | * Compare two integers |
1319 | | * @param a the first operand |
1320 | | * @param b the second operand |
1321 | | * @return true if a is not equal to b |
1322 | | */ |
1323 | 19.2k | inline bool operator!=(const BigInt& a, const BigInt& b) { |
1324 | 19.2k | return !a.is_equal(b); |
1325 | 19.2k | } |
1326 | | |
1327 | | /** |
1328 | | * Compare two integers |
1329 | | * @param a the first operand |
1330 | | * @param b the second operand |
1331 | | * @return true if a is less than or equal to b |
1332 | | */ |
1333 | 0 | inline bool operator<=(const BigInt& a, const BigInt& b) { |
1334 | 0 | return (a.cmp(b) <= 0); |
1335 | 0 | } |
1336 | | |
1337 | | /** |
1338 | | * Compare two integers |
1339 | | * @param a the first operand |
1340 | | * @param b the second operand |
1341 | | * @return true if a is greater than or equal to b |
1342 | | */ |
1343 | 795k | inline bool operator>=(const BigInt& a, const BigInt& b) { |
1344 | 795k | return (a.cmp(b) >= 0); |
1345 | 795k | } |
1346 | | |
1347 | | /** |
1348 | | * Compare two integers |
1349 | | * @param a the first operand |
1350 | | * @param b the second operand |
1351 | | * @return true if a is less than b |
1352 | | */ |
1353 | 201k | inline bool operator<(const BigInt& a, const BigInt& b) { |
1354 | 201k | return a.is_less_than(b); |
1355 | 201k | } |
1356 | | |
1357 | | /** |
1358 | | * Compare two integers |
1359 | | * @param a the first operand |
1360 | | * @param b the second operand |
1361 | | * @return true if a is greater than b |
1362 | | */ |
1363 | 0 | inline bool operator>(const BigInt& a, const BigInt& b) { |
1364 | 0 | return b.is_less_than(a); |
1365 | 0 | } |
1366 | | |
1367 | | /** |
1368 | | * Compare an integer with a word |
1369 | | * @param a the first operand |
1370 | | * @param b the second operand |
1371 | | * @return true if a is equal to b |
1372 | | */ |
1373 | 22.9k | inline bool operator==(const BigInt& a, word b) { |
1374 | 22.9k | return (a.cmp_word(b) == 0); |
1375 | 22.9k | } |
1376 | | |
1377 | | /** |
1378 | | * Compare an integer with a word |
1379 | | * @param a the first operand |
1380 | | * @param b the second operand |
1381 | | * @return true if a is not equal to b |
1382 | | */ |
1383 | 27 | inline bool operator!=(const BigInt& a, word b) { |
1384 | 27 | return (a.cmp_word(b) != 0); |
1385 | 27 | } |
1386 | | |
1387 | | /** |
1388 | | * Compare an integer with a word |
1389 | | * @param a the first operand |
1390 | | * @param b the second operand |
1391 | | * @return true if a is less than or equal to b |
1392 | | */ |
1393 | 43.9k | inline bool operator<=(const BigInt& a, word b) { |
1394 | 43.9k | return (a.cmp_word(b) <= 0); |
1395 | 43.9k | } |
1396 | | |
1397 | | /** |
1398 | | * Compare an integer with a word |
1399 | | * @param a the first operand |
1400 | | * @param b the second operand |
1401 | | * @return true if a is greater than or equal to b |
1402 | | */ |
1403 | 26.5k | inline bool operator>=(const BigInt& a, word b) { |
1404 | 26.5k | return (a.cmp_word(b) >= 0); |
1405 | 26.5k | } |
1406 | | |
1407 | | /** |
1408 | | * Compare an integer with a word |
1409 | | * @param a the first operand |
1410 | | * @param b the second operand |
1411 | | * @return true if a is less than b |
1412 | | */ |
1413 | 402k | inline bool operator<(const BigInt& a, word b) { |
1414 | 402k | return (a.cmp_word(b) < 0); |
1415 | 402k | } |
1416 | | |
1417 | | /** |
1418 | | * Compare an integer with a word |
1419 | | * @param a the first operand |
1420 | | * @param b the second operand |
1421 | | * @return true if a is greater than b |
1422 | | */ |
1423 | 38.1k | inline bool operator>(const BigInt& a, word b) { |
1424 | 38.1k | return (a.cmp_word(b) > 0); |
1425 | 38.1k | } |
1426 | | |
1427 | | /** |
1428 | | * Write an integer to an output stream |
1429 | | * @param stream the stream to write to |
1430 | | * @param n the integer to write |
1431 | | * @return reference to the stream |
1432 | | */ |
1433 | | BOTAN_DEPRECATED("Use BigInt::to_{hex,dec}_string") |
1434 | | BOTAN_PUBLIC_API(2, 0) std::ostream& operator<<(std::ostream& stream, const BigInt& n); |
1435 | | |
1436 | | /** |
1437 | | * Read an integer from an input stream |
1438 | | * @param stream the stream to read from |
1439 | | * @param n set to the integer which was read |
1440 | | * @return reference to the stream |
1441 | | */ |
1442 | | BOTAN_DEPRECATED("Use BigInt::from_string") |
1443 | | BOTAN_PUBLIC_API(2, 0) std::istream& operator>>(std::istream& stream, BigInt& n); |
1444 | | |
1445 | | } // namespace Botan |
1446 | | |
1447 | | #endif |