diff options
author | Jack Lloyd <[email protected]> | 2018-12-01 08:54:24 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-01 12:12:58 -0500 |
commit | f780cde67afac7b6213c801fb0edcc2eccdffe59 (patch) | |
tree | 67c96decf93426ed995cba92af261e1c43287092 /src/lib/math/bigint | |
parent | 1e9e5d2f3bdac32838ad99b5718cad46cca693f3 (diff) |
Add BigInt::mod_mul
Diffstat (limited to 'src/lib/math/bigint')
-rw-r--r-- | src/lib/math/bigint/big_ops2.cpp | 38 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.cpp | 2 | ||||
-rw-r--r-- | src/lib/math/bigint/bigint.h | 14 |
3 files changed, 41 insertions, 13 deletions
diff --git a/src/lib/math/bigint/big_ops2.cpp b/src/lib/math/bigint/big_ops2.cpp index 6ce14f8f1..5e4ee949c 100644 --- a/src/lib/math/bigint/big_ops2.cpp +++ b/src/lib/math/bigint/big_ops2.cpp @@ -126,19 +126,39 @@ BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>& swap_reg(ws); } #else - // is t < s or not? - const auto is_lt = bigint_ct_is_lt(data(), mod_sw, s.data(), mod_sw); + if(mod_sw == 4) + bigint_mod_sub_n<4>(mutable_data(), s.data(), mod.data(), ws.data()); + else + bigint_mod_sub(mutable_data(), s.data(), mod.data(), mod_sw, ws.data()); +#endif + + return (*this); + } - // ws = p - s - const word borrow = bigint_sub3(ws.data(), mod.data(), mod_sw, s.data(), mod_sw); +BigInt& BigInt::mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws) + { + BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive"); + BOTAN_ARG_CHECK(y < 16, "y too large"); - // Compute either (t - s) or (t + (p - s)) depending on mask - const word carry = bigint_cnd_addsub(is_lt, mutable_data(), ws.data(), s.data(), mod_sw); + BOTAN_DEBUG_ASSERT(*this < mod); - BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0); - BOTAN_UNUSED(carry, borrow); -#endif + switch(y) + { + case 2: + *this <<= 1; + break; + case 4: + *this <<= 2; + break; + case 8: + *this <<= 3; + break; + default: + *this *= static_cast<word>(y); + break; + } + this->reduce_below(mod, ws); return (*this); } diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp index 667035686..d64082476 100644 --- a/src/lib/math/bigint/bigint.cpp +++ b/src/lib/math/bigint/bigint.cpp @@ -335,8 +335,6 @@ void BigInt::reduce_below(const BigInt& p, secure_vector<word>& ws) for(;;) { word borrow = bigint_sub3(ws.data(), data(), p_words + 1, p.data(), p_words); - - //CT::unpoison(borrow); // fixme if(borrow) break; diff --git a/src/lib/math/bigint/bigint.h b/src/lib/math/bigint/bigint.h index 58c45dd67..9b385348e 100644 --- a/src/lib/math/bigint/bigint.h +++ b/src/lib/math/bigint/bigint.h @@ -328,7 +328,17 @@ class BOTAN_PUBLIC_API(2,0) BigInt final BigInt& mod_sub(const BigInt& y, const BigInt& mod, secure_vector<word>& ws); /** - * Return *this below mod + * Set *this to (*this * y) % mod + * This function assumes *this is >= 0 && < mod + * y should be small, less than 16 + * @param y the small integer to multiply by + * @param mod the positive modulus + * @param ws a temp workspace + */ + BigInt& mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws); + + /** + * Return *this % mod * * Assumes that *this is (if anything) only slightly larger than * mod and performs repeated subtractions. It should not be used if @@ -933,7 +943,7 @@ class BOTAN_PUBLIC_API(2,0) BigInt final if(n > size()) { if(n <= m_reg.capacity()) - m_reg.resize(m_reg.capacity()); + m_reg.resize(n); else m_reg.resize(n + (8 - (n % 8))); } |