Botan 3.13.0
Crypto and TLS for C&
bigint.h
Go to the documentation of this file.
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
19namespace Botan {
20
22
23/**
24 * Arbitrary precision integer
25 */
26class 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 BigInt() = default;
46
47 /**
48 * Create a 0-value BigInt
49 */
50 static BigInt zero() { return BigInt(); }
51
52 /**
53 * Create a 1-value BigInt
54 */
55 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 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 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 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 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 BigInt(BigInt&& other) noexcept { this->swap(other); }
184
186
187 /**
188 * Move assignment
189 */
190 BigInt& operator=(BigInt&& other) noexcept {
191 if(this != &other) {
192 this->swap(other);
193 }
194
195 return (*this);
196 }
197
198 /**
199 * Copy assignment
200 */
201 BigInt& operator=(const BigInt&) = default;
202
203 /**
204 * Swap this value with another
205 * @param other BigInt to swap values with
206 */
207 void swap(BigInt& other) noexcept {
208 m_data.swap(other.m_data);
209 std::swap(m_signedness, other.m_signedness);
210 }
211
212 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 BOTAN_DEPRECATED("Deprecated no replacement") void swap_reg(secure_vector<word>& reg) {
219 m_data.swap(reg);
220 // sign left unchanged
221 }
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 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 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 */
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 BigInt& operator++() { return (*this += 1); }
293
294 /**
295 * Decrement operator
296 */
297 BigInt& operator--() { return (*this -= 1); }
298
299 /**
300 * Postfix increment operator
301 */
303 BigInt x = (*this);
304 ++(*this);
305 return x;
306 }
307
308 /**
309 * Postfix decrement operator
310 */
312 BigInt x = (*this);
313 --(*this);
314 return x;
315 }
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 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 BigInt& sub(const word y[], size_t y_words, Sign sign) {
359 return add(y, y_words, sign == Positive ? Negative : Positive);
360 }
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 void clear() {
442 m_data.set_to_zero();
443 m_signedness = Positive;
444 }
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 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 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 int signum() const {
494 if(sig_words() == 0) {
495 return 0;
496 }
497 return (sign() == Negative) ? -1 : 1;
498 }
499
500 /**
501 * Test if the integer is not zero
502 * @result true if the integer is non-zero, false otherwise
503 */
504 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 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 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 void conditionally_set_bit(size_t n, bool set_it) {
527 const size_t which = n / (sizeof(word) * 8);
528 const word mask = static_cast<word>(set_it) << (n % (sizeof(word) * 8));
529 m_data.set_word_at(which, word_at(which) | mask);
530 }
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 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 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 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 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 BOTAN_DEPRECATED("Deprecated no replacement") void set_words(const word w[], size_t len) {
616 m_data.set_words(w, len);
617 }
618
619 /**
620 * Tests if the sign of the integer is negative
621 * @result true, iff the integer has a negative sign
622 */
623 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 BOTAN_DEPRECATED("Use signum() >= 0 or signum() > 0 as appropriate") bool is_positive() const {
634 return signum() >= 0;
635 }
636
637 /**
638 * Return the sign of the integer
639 * @result the sign of the integer
640 */
641 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 */
648 if(sign() == Positive) {
649 return Negative;
650 }
651 return Positive;
652 }
653
654 /**
655 * Flip the sign of this BigInt
656 */
657 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 */
664 if(sign == Negative && is_zero()) {
665 sign = Positive;
666 }
667
668 m_signedness = sign;
669 }
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 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 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 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 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 BOTAN_DEPRECATED("Deprecated no replacement") secure_vector<word>& get_word_vector() {
724 return m_data.mutable_vector();
725 }
726
727 /**
728 * Don't use this function in application code
729 */
730 BOTAN_DEPRECATED("Deprecated no replacement") const secure_vector<word>& get_word_vector() const {
731 return m_data.const_vector();
732 }
733
734 /**
735 * Increase internal register buffer to at least n words
736 * @param n new size of register
737 */
738 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 BOTAN_DEPRECATED("Deprecated no replacement") void resize(size_t s) {
745 m_data.resize(s);
746 set_sign(sign()); // handle possible zero
747 }
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 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 T out(len);
795 this->serialize_to(out);
796 return out;
797 }
798
799 /**
800 * Serialize the value of this BigInt as a big endian encoding.
801 */
802 template <typename T = std::vector<uint8_t>>
803 T serialize() const {
804 return serialize<T>(this->bytes());
805 }
806
807 /**
808 * Store BigInt-value in a given byte array
809 * @param buf destination byte array for the integer value
810 */
811 BOTAN_DEPRECATED("Use BigInt::serialize_to") void binary_encode(uint8_t buf[]) const {
812 this->serialize_to(std::span{buf, this->bytes()});
813 }
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 BOTAN_DEPRECATED("Use BigInt::from_bytes") void binary_decode(const uint8_t buf[], size_t length) {
837 this->assign_from_bytes(std::span{buf, length});
838 }
839
840 /**
841 * Read integer value from a byte vector
842 * @param buf the vector to load from
843 */
844 BOTAN_DEPRECATED("Use BigInt::from_bytes") void binary_decode(std::span<const uint8_t> buf) {
845 this->assign_from_bytes(buf);
846 }
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 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 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 static BigInt power_of_2(size_t n) {
907 BigInt b;
908 b.set_bit(n);
909 return b;
910 }
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 BOTAN_DEPRECATED("Use BigInt::serialize") static std::vector<uint8_t> encode(const BigInt& n) {
918 return n.serialize<std::vector<uint8_t>>(n.bytes());
919 }
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 BOTAN_DEPRECATED("Use BigInt::serialize") static secure_vector<uint8_t> encode_locked(const BigInt& n) {
927 return n.serialize<secure_vector<uint8_t>>(n.bytes());
928 }
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 BOTAN_DEPRECATED("Use BigInt::from_bytes") static BigInt decode(const uint8_t buf[], size_t length) {
944 return BigInt::from_bytes(std::span{buf, length});
945 }
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 BOTAN_DEPRECATED("Use BigInt::from_bytes") static BigInt decode(std::span<const uint8_t> buf) {
953 return BigInt::from_bytes(buf);
954 }
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 static secure_vector<uint8_t> encode_1363(const BigInt& n, size_t bytes) {
982 return n.serialize<secure_vector<uint8_t>>(bytes);
983 }
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 BOTAN_DEPRECATED("Use BigInt::serialize_to") static void encode_1363(std::span<uint8_t> out, const BigInt& n) {
991 n.serialize_to(out);
992 }
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 static void encode_1363(uint8_t out[], size_t bytes, const BigInt& n) {
1002 n.serialize_to(std::span{out, bytes});
1003 }
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 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 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 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 */
1053 BigInt bn;
1054 bn.m_data.swap(words);
1055 return bn;
1056 }
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 word* mutable_data() {
1084 invalidate_sig_words();
1085 return m_reg.data();
1086 }
1087
1088 const word* const_data() const { return m_reg.data(); }
1089
1090 std::span<const word> const_span() const { return std::span{m_reg}; }
1091
1092 secure_vector<word>& mutable_vector() {
1093 invalidate_sig_words();
1094 return m_reg;
1095 }
1096
1097 const secure_vector<word>& const_vector() const { return m_reg; }
1098
1099 word get_word_at(size_t n) const {
1100 if(n < m_reg.size()) {
1101 return m_reg[n];
1102 }
1103 return 0;
1104 }
1105
1106 void set_word_at(size_t i, word w) {
1107 invalidate_sig_words();
1108 if(i >= m_reg.size()) {
1109 if(w == 0) {
1110 return;
1111 }
1112 grow_to(i + 1);
1113 }
1114 m_reg[i] = w;
1115 }
1116
1117 void set_words(const word w[], size_t len) {
1118 invalidate_sig_words();
1119 m_reg.assign(w, w + len);
1120 }
1121
1122 void set_to_zero();
1123
1124 void mask_bits(size_t n);
1125
1126 void grow_to(size_t n) const {
1127 if(n > size()) {
1128 if(n <= m_reg.capacity()) {
1129 m_reg.resize(n);
1130 } else {
1131 m_reg.resize(n + (8 - (n % 8)));
1132 }
1133 }
1134 }
1135
1136 size_t size() const { return m_reg.size(); }
1137
1138 void shrink_to_fit(size_t min_size = 0) {
1139 const size_t words = std::max(min_size, sig_words());
1140 m_reg.resize(words);
1141 }
1142
1143 void resize(size_t s) {
1144 const bool shrinking = s < m_reg.size();
1145 m_reg.resize(s);
1146 if(shrinking) {
1147 invalidate_sig_words();
1148 }
1149 }
1150
1151 void swap(Data& other) noexcept {
1152 m_reg.swap(other.m_reg);
1153 std::swap(m_sig_words, other.m_sig_words);
1154 }
1155
1156 void swap(secure_vector<word>& reg) noexcept {
1157 m_reg.swap(reg);
1158 invalidate_sig_words();
1159 }
1160
1161 void invalidate_sig_words() const noexcept { m_sig_words = sig_words_npos; }
1162
1163 size_t sig_words() const {
1164 if(m_sig_words == sig_words_npos) {
1165 m_sig_words = calc_sig_words();
1166 }
1167 return m_sig_words;
1168 }
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*/
1189inline BigInt operator+(const BigInt& x, const BigInt& y) {
1190 return BigInt::add2(x, y._data(), y.sig_words(), y.sign());
1191}
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*/
1199inline BigInt operator+(const BigInt& x, word y) {
1200 return BigInt::add2(x, &y, 1, BigInt::Positive);
1201}
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*/
1209inline BigInt operator+(word x, const BigInt& y) {
1210 return y + x;
1211}
1212
1213/**
1214* Subtract two integers
1215* @param x the minuend
1216* @param y the subtrahend
1217* @return (x - y)
1218*/
1219inline BigInt operator-(const BigInt& x, const BigInt& y) {
1220 return BigInt::add2(x, y._data(), y.sig_words(), y.reverse_sign());
1221}
1222
1223/**
1224* Subtract a word from an integer
1225* @param x the minuend
1226* @param y the subtrahend
1227* @return (x - y)
1228*/
1229inline BigInt operator-(const BigInt& x, word y) {
1230 return BigInt::add2(x, &y, 1, BigInt::Negative);
1231}
1232
1233/**
1234* Multiply two integers
1235* @param x the first factor
1236* @param y the second factor
1237* @return (x * y)
1238*/
1239BOTAN_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*/
1247BOTAN_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*/
1255inline BigInt operator*(word x, const BigInt& y) {
1256 return y * x;
1257}
1258
1259/**
1260* Divide two integers
1261* @param x the dividend
1262* @param d the divisor
1263* @return (x / d), rounded towards zero
1264*/
1265BOTAN_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*/
1273BOTAN_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*/
1281BOTAN_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*/
1289BOTAN_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*/
1297BOTAN_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*/
1305BOTAN_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*/
1313inline bool operator==(const BigInt& a, const BigInt& b) {
1314 return a.is_equal(b);
1315}
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*/
1323inline bool operator!=(const BigInt& a, const BigInt& b) {
1324 return !a.is_equal(b);
1325}
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*/
1333inline bool operator<=(const BigInt& a, const BigInt& b) {
1334 return (a.cmp(b) <= 0);
1335}
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*/
1343inline bool operator>=(const BigInt& a, const BigInt& b) {
1344 return (a.cmp(b) >= 0);
1345}
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*/
1353inline bool operator<(const BigInt& a, const BigInt& b) {
1354 return a.is_less_than(b);
1355}
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*/
1363inline bool operator>(const BigInt& a, const BigInt& b) {
1364 return b.is_less_than(a);
1365}
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*/
1373inline bool operator==(const BigInt& a, word b) {
1374 return (a.cmp_word(b) == 0);
1375}
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*/
1383inline bool operator!=(const BigInt& a, word b) {
1384 return (a.cmp_word(b) != 0);
1385}
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*/
1393inline bool operator<=(const BigInt& a, word b) {
1394 return (a.cmp_word(b) <= 0);
1395}
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*/
1403inline bool operator>=(const BigInt& a, word b) {
1404 return (a.cmp_word(b) >= 0);
1405}
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*/
1413inline bool operator<(const BigInt& a, word b) {
1414 return (a.cmp_word(b) < 0);
1415}
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*/
1423inline bool operator>(const BigInt& a, word b) {
1424 return (a.cmp_word(b) > 0);
1425}
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*/
1433BOTAN_DEPRECATED("Use BigInt::to_{hex,dec}_string")
1434BOTAN_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*/
1442BOTAN_DEPRECATED("Use BigInt::from_string")
1443BOTAN_PUBLIC_API(2, 0) std::istream& operator>>(std::istream& stream, BigInt& n);
1444
1445} // namespace Botan
1446
1447#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
BigInt & operator++()
Definition bigint.h:292
BigInt & operator=(BigInt &&other) noexcept
Definition bigint.h:190
static BigInt zero()
Definition bigint.h:50
size_t sig_words() const
Definition bigint.h:687
void binary_decode(const uint8_t buf[], size_t length)
Definition bigint.h:836
void conditionally_set_bit(size_t n, bool set_it)
Definition bigint.h:526
void swap(BigInt &other) noexcept
Definition bigint.h:207
bool is_odd() const
Definition bigint.h:487
BigInt()=default
bool is_equal(const BigInt &n) const
Definition bigint.cpp:156
static BigInt decode(const uint8_t buf[], size_t length)
Definition bigint.h:943
BigInt & sub(const word y[], size_t y_words, Sign sign)
Definition bigint.h:358
secure_vector< word > & get_word_vector()
Definition bigint.h:723
void set_word_at(size_t i, word w)
Definition bigint.h:608
word * mutable_data()
Definition bigint.h:712
size_t size() const
Definition bigint.h:681
T serialize() const
Definition bigint.h:803
void grow_to(size_t n) const
Definition bigint.h:738
Sign reverse_sign() const
Definition bigint.h:647
static BigInt _from_words(secure_vector< word > &words)
Definition bigint.h:1052
void resize(size_t s)
Definition bigint.h:744
void flip_sign()
Definition bigint.h:657
static BigInt add2(const BigInt &x, const word y[], size_t y_words, Sign y_sign)
Definition big_ops3.cpp:20
void set_words(const word w[], size_t len)
Definition bigint.h:615
bool is_less_than(const BigInt &n) const
Definition bigint.cpp:164
int32_t cmp(const BigInt &n, bool check_signs=true) const
Definition bigint.cpp:138
BigInt & operator+=(word y)
Definition bigint.h:233
const word * data() const
Definition bigint.h:718
int signum() const
Definition bigint.h:493
static secure_vector< uint8_t > encode_locked(const BigInt &n)
Definition bigint.h:926
void binary_encode(uint8_t buf[]) const
Definition bigint.h:811
static BigInt one()
Definition bigint.h:55
word word_at(size_t n) const
Definition bigint.h:601
void set_bit(size_t n)
Definition bigint.h:516
friend void swap(BigInt &x, BigInt &y) noexcept
Definition bigint.h:212
int32_t cmp_word(word n) const
Definition bigint.cpp:122
void mask_bits(size_t n)
Definition bigint.h:542
static BigInt from_string(std::string_view str)
Definition bigint.cpp:57
void _const_time_unpoison() const
Definition bigint.cpp:559
BigInt(const uint8_t buf[], size_t length)
Definition bigint.h:133
void serialize_to(std::span< uint8_t > out) const
Definition bigint.cpp:395
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:83
static std::vector< uint8_t > encode(const BigInt &n)
Definition bigint.h:917
BigInt operator--(int)
Definition bigint.h:311
void _const_time_poison() const
Definition bigint.cpp:555
bool is_even() const
Definition bigint.h:481
static BigInt power_of_2(size_t n)
Definition bigint.h:906
bool operator!() const
Definition bigint.h:327
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition bigint.h:981
BigInt & operator-=(word y)
Definition bigint.h:245
static BigInt from_u64(uint64_t n)
Definition bigint.cpp:30
void clear()
Definition bigint.h:441
const word * _data() const
Definition bigint.h:1033
Sign sign() const
Definition bigint.h:641
void const_time_poison() const
Definition bigint.h:885
BigInt & add(const word y[], size_t y_words, Sign sign)
Definition big_ops2.cpp:32
BigInt & operator=(const BigInt &)=default
void _assign_from_bytes(std::span< const uint8_t > bytes)
Definition bigint.h:1044
BigInt(std::span< const uint8_t > bytes)
Definition bigint.h:139
BigInt & operator--()
Definition bigint.h:297
bool is_zero() const
Definition bigint.h:510
BigInt operator++(int)
Definition bigint.h:302
bool is_negative() const
Definition bigint.h:623
size_t bytes() const
Definition bigint.cpp:294
bool is_nonzero() const
Definition bigint.h:504
void const_time_unpoison() const
Definition bigint.h:890
bool is_positive() const
Definition bigint.h:633
bool get_bit(size_t n) const
Definition bigint.h:549
T serialize(size_t len) const
Definition bigint.h:790
void swap_reg(secure_vector< word > &reg)
Definition bigint.h:218
std::span< const word > _as_span() const
Definition bigint.h:1023
void set_sign(Sign sign)
Definition bigint.h:663
bool operator>(const ASN1_Time &t1, const ASN1_Time &t2)
bool operator<(const OID &a, const OID &b)
Definition asn1_oid.cpp:175
BigInt square(const BigInt &x)
Definition numthry.cpp:184
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
BigInt abs(const BigInt &n)
Definition numthry.h:22
constexpr auto operator>>=(Strong< T1, Tags... > &a, T2 b)
BigInt operator-(const BigInt &x, const BigInt &y)
Definition bigint.h:1219
constexpr auto operator<<=(Strong< T1, Tags... > &a, T2 b)
bool operator>=(const ASN1_Time &t1, const ASN1_Time &t2)
bool operator<=(const ASN1_Time &t1, const ASN1_Time &t2)
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
Definition secmem.h:168
bool operator!=(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:58
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
bool operator==(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:54
constexpr auto operator*=(Strong< T1, Tags... > &a, T2 b)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131
constexpr auto operator/=(Strong< T1, Tags... > &a, T2 b)
constexpr auto operator-=(Strong< T1, Tags... > &a, T2 b)
uint32_t to_u32bit(std::string_view input)
Definition parsing.cpp:76