diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/math/bigint/bigint.cpp | 19 | ||||
-rw-r--r-- | src/math/bigint/divide.cpp | 26 | ||||
-rw-r--r-- | src/math/mp/mp_core.h | 6 | ||||
-rw-r--r-- | src/math/mp/mp_misc.cpp | 33 |
4 files changed, 43 insertions, 41 deletions
diff --git a/src/math/bigint/bigint.cpp b/src/math/bigint/bigint.cpp index 7ff1183a3..45c351256 100644 --- a/src/math/bigint/bigint.cpp +++ b/src/math/bigint/bigint.cpp @@ -107,16 +107,23 @@ void BigInt::grow_to(size_t n) /* * Comparison Function */ -s32bit BigInt::cmp(const BigInt& n, bool check_signs) const +s32bit BigInt::cmp(const BigInt& other, bool check_signs) const { if(check_signs) { - if(n.is_positive() && this->is_negative()) return -1; - if(n.is_negative() && this->is_positive()) return 1; - if(n.is_negative() && this->is_negative()) - return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words())); + if(other.is_positive() && this->is_negative()) + return -1; + + if(other.is_negative() && this->is_positive()) + return 1; + + if(other.is_negative() && this->is_negative()) + return (-bigint_cmp(this->data(), this->sig_words(), + other.data(), other.sig_words())); } - return bigint_cmp(data(), sig_words(), n.data(), n.sig_words()); + + return bigint_cmp(this->data(), this->sig_words(), + other.data(), other.sig_words()); } /* diff --git a/src/math/bigint/divide.cpp b/src/math/bigint/divide.cpp index c54eb463b..c8b82422a 100644 --- a/src/math/bigint/divide.cpp +++ b/src/math/bigint/divide.cpp @@ -7,6 +7,7 @@ #include <botan/divide.h> #include <botan/internal/mp_core.h> +#include <botan/internal/mp_asmi.h> namespace Botan { @@ -26,6 +27,29 @@ void sign_fixup(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r) q.flip_sign(); } +bool division_check(word q, word y2, word y1, + word x3, word x2, word x1) + { + // Compute (y3,y2,y1) = (y2,y1) * q + + word y3 = 0; + y1 = word_madd2(q, y1, &y3); + y2 = word_madd2(q, y2, &y3); + + // Return (y3,y2,y1) >? (x3,x2,x1) + + if(y3 > x3) return true; + if(y3 < x3) return false; + + if(y2 > x2) return true; + if(y2 < x2) return false; + + if(y1 > x1) return true; + if(y1 < x1) return false; + + return false; + } + } /* @@ -92,7 +116,7 @@ void divide(const BigInt& x, const BigInt& y_arg, BigInt& q, BigInt& r) else q_words[j-t-1] = bigint_divop(x_j0, x_j1, y_t); - while(bigint_divcore(q_words[j-t-1], + while(division_check(q_words[j-t-1], y_t, y.word_at(t-1), x_j0, x_j1, r.word_at(j-2))) { diff --git a/src/math/mp/mp_core.h b/src/math/mp/mp_core.h index c879f42ee..579f3fef4 100644 --- a/src/math/mp/mp_core.h +++ b/src/math/mp/mp_core.h @@ -126,12 +126,6 @@ void bigint_monty_sqr(word z[], size_t z_size, const word p[], size_t p_size, word p_dash, word workspace[]); -/* -* Division operation -*/ -size_t bigint_divcore(word q, word y2, word y1, - word x3, word x2, word x1); - /** * Compare x and y */ diff --git a/src/math/mp/mp_misc.cpp b/src/math/mp/mp_misc.cpp index 0232f01d6..2aff00592 100644 --- a/src/math/mp/mp_misc.cpp +++ b/src/math/mp/mp_misc.cpp @@ -13,29 +13,6 @@ namespace Botan { extern "C" { /* -* Core Division Operation -*/ -size_t bigint_divcore(word q, word y2, word y1, - word x3, word x2, word x1) - { - // Compute (y2,y1) * q - - word y3 = 0; - y1 = word_madd2(q, y1, &y3); - y2 = word_madd2(q, y2, &y3); - - // Return (y3,y2,y1) >? (x3,x2,x1) - - if(y3 > x3) return 1; - if(y3 < x3) return 0; - if(y2 > x2) return 1; - if(y2 < x2) return 0; - if(y1 > x1) return 1; - if(y1 < x1) return 0; - return 0; - } - -/* * Compare two MP integers */ s32bit bigint_cmp(const word x[], size_t x_size, @@ -50,11 +27,11 @@ s32bit bigint_cmp(const word x[], size_t x_size, x_size--; } - for(size_t j = x_size; j > 0; --j) + for(size_t i = x_size; i > 0; --i) { - if(x[j-1] > y[j-1]) + if(x[i-1] > y[i-1]) return 1; - if(x[j-1] < y[j-1]) + if(x[i-1] < y[i-1]) return -1; } @@ -68,12 +45,12 @@ word bigint_divop(word n1, word n0, word d) { word high = n1 % d, quotient = 0; - for(size_t j = 0; j != MP_WORD_BITS; ++j) + for(size_t i = 0; i != MP_WORD_BITS; ++i) { word high_top_bit = (high & MP_WORD_TOP_BIT); high <<= 1; - high |= (n0 >> (MP_WORD_BITS-1-j)) & 1; + high |= (n0 >> (MP_WORD_BITS-1-i)) & 1; quotient <<= 1; if(high_top_bit || high >= d) |