mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:38:11 +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:
parent
2020176f0f
commit
2601441486
3 changed files with 17 additions and 1 deletions
|
@ -150,6 +150,13 @@ UnsignedBigInteger ModularPower(const UnsignedBigInteger& b, const UnsignedBigIn
|
|||
UnsignedBigInteger::multiply_without_allocation(base, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
|
||||
UnsignedBigInteger::divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
base.set_to(temp_remainder);
|
||||
|
||||
// Note that not clamping here would cause future calculations (multiply, specifically) to allocate even more unused space
|
||||
// which would then persist through the temp bigints, and significantly slow down later loops.
|
||||
// To avoid that, we can clamp to a specific max size, or just clamp to the min needed amount of space.
|
||||
ep.clamp_to_trimmed_length();
|
||||
exp.clamp_to_trimmed_length();
|
||||
base.clamp_to_trimmed_length();
|
||||
}
|
||||
return exp;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue