1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:37:35 +00:00

LibCrypto: Avoid overly big allocs in intermediate ModularPower results

If we don't limit the sizes of the intermediate results, they will grow
indefinitely, causing each iteration to take longer and longer (in both
memcpy time, and algorithm runtime).
While calculating the trimmed length is fairly expensive, it's a small
cost to pay for uniform iteration times.
This commit is contained in:
AnotherTest 2021-04-01 12:02:14 +04:30 committed by Andreas Kling
parent 2020176f0f
commit 2601441486
3 changed files with 17 additions and 1 deletions

View file

@ -156,6 +156,13 @@ size_t UnsignedBigInteger::trimmed_length() const
return m_cached_trimmed_length.value();
}
void UnsignedBigInteger::clamp_to_trimmed_length()
{
auto length = trimmed_length();
if (m_words.size() > length)
m_words.resize(length);
}
FLATTEN UnsignedBigInteger UnsignedBigInteger::plus(const UnsignedBigInteger& other) const
{
UnsignedBigInteger result;
@ -578,7 +585,7 @@ FLATTEN void UnsignedBigInteger::shift_left_without_allocation(
// output += (carry_word << temp_result.length())
// FIXME : Using temp_plus this way to transform carry_word into a bigint is not
// efficient nor pretty. Maybe we should have an "add_with_shift" method ?
// efficient nor pretty. Maybe we should have an "add_with_shift" method ?
temp_plus.set_to_0();
temp_plus.m_words.append(carry_word);
shift_left_by_n_words(temp_plus, temp_result.length(), temp_result);