diff options
author | Jack Lloyd <[email protected]> | 2018-04-26 11:40:50 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-04-26 11:40:50 -0400 |
commit | ebb73b99a8ca14c45d64de2619ab0cf6b89b860e (patch) | |
tree | 082b369de1676755043bf03f4c1d9711ea4eae59 /src/lib/math/bigint/bigint.h | |
parent | 5860f9db36fcfa0ae7c3c4e768b446106e228f32 (diff) |
Add BigInt functions for adding, subtracting and comparing with words
Avoids needless allocations for expressions like x - 1 or y <= 4.
Diffstat (limited to 'src/lib/math/bigint/bigint.h')
-rw-r--r-- | src/lib/math/bigint/bigint.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index bb7a69541..33a1252ab 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -165,12 +165,24 @@ class BOTAN_PUBLIC_API(2,0) BigInt final BigInt& operator+=(const BigInt& y); /** + * += operator + * @param y the word to add to this + */ + BigInt& operator+=(word y); + + /** * -= operator * @param y the BigInt to subtract from this */ BigInt& operator-=(const BigInt& y); /** + * -= operator + * @param y the word to subtract from this + */ + BigInt& operator-=(word y); + + /** * *= operator * @param y the BigInt to multiply with this */ @@ -306,6 +318,14 @@ class BOTAN_PUBLIC_API(2,0) BigInt final int32_t cmp(const BigInt& n, bool check_signs = true) const; /** + * Compare this to an integer + * @param n the value to compare with + * @result if (this<n) return -1, if (this>n) return 1, if both + * values are identical return 0 [like Perl's <=> operator] + */ + int32_t cmp_word(word n) const; + + /** * Test if the integer has an even value * @result true if the integer is even, false otherwise */ @@ -700,6 +720,10 @@ class BOTAN_PUBLIC_API(2,0) BigInt final size_t idx); private: + + BigInt& add(const word y[], size_t y_words, Sign sign); + BigInt& sub(const word y[], size_t y_words, Sign sign); + secure_vector<word> m_reg; Sign m_signedness = Positive; }; @@ -708,7 +732,12 @@ class BOTAN_PUBLIC_API(2,0) BigInt final * Arithmetic Operators */ BigInt BOTAN_PUBLIC_API(2,0) operator+(const BigInt& x, const BigInt& y); +BigInt BOTAN_PUBLIC_API(2,7) operator+(const BigInt& x, word y); +BigInt BOTAN_PUBLIC_API(2,7) operator+(word x, const BigInt& y); + BigInt BOTAN_PUBLIC_API(2,0) operator-(const BigInt& x, const BigInt& y); +BigInt BOTAN_PUBLIC_API(2,7) operator-(const BigInt& x, word y); + BigInt BOTAN_PUBLIC_API(2,0) operator*(const BigInt& x, const BigInt& y); BigInt BOTAN_PUBLIC_API(2,0) operator/(const BigInt& x, const BigInt& d); BigInt BOTAN_PUBLIC_API(2,0) operator%(const BigInt& x, const BigInt& m); @@ -732,6 +761,19 @@ inline bool operator<(const BigInt& a, const BigInt& b) inline bool operator>(const BigInt& a, const BigInt& b) { return (a.cmp(b) > 0); } +inline bool operator==(const BigInt& a, word b) + { return (a.cmp_word(b) == 0); } +inline bool operator!=(const BigInt& a, word b) + { return (a.cmp_word(b) != 0); } +inline bool operator<=(const BigInt& a, word b) + { return (a.cmp_word(b) <= 0); } +inline bool operator>=(const BigInt& a, word b) + { return (a.cmp_word(b) >= 0); } +inline bool operator<(const BigInt& a, word b) + { return (a.cmp_word(b) < 0); } +inline bool operator>(const BigInt& a, word b) + { return (a.cmp_word(b) > 0); } + /* * I/O Operators */ |