LibCrypto: Add the montgomery modular power algorithm

This algorithm allows for much faster computations of modular powers
(around a 5x-10x speedup of the Crypto test). However, it is only valid
for odd modulo values, and therefore the old algorithm must be kept for
computations involving even modulo values.
This commit is contained in:
DexesTTP 2021-05-12 22:47:07 +02:00 committed by Linus Groh
parent 5071989545
commit 485adb5e29
Notes: sideshowbarker 2024-07-18 18:13:40 +09:00
5 changed files with 264 additions and 2 deletions

View file

@ -37,6 +37,20 @@ UnsignedBigInteger ModularPower(const UnsignedBigInteger& b, const UnsignedBigIn
if (m == 1)
return 0;
if (m.is_odd()) {
UnsignedBigInteger temp_z0 { 0 };
UnsignedBigInteger temp_rr { 0 };
UnsignedBigInteger temp_one { 0 };
UnsignedBigInteger temp_z { 0 };
UnsignedBigInteger temp_zz { 0 };
UnsignedBigInteger temp_x { 0 };
UnsignedBigInteger temp_extra { 0 };
UnsignedBigInteger result;
UnsignedBigIntegerAlgorithms::montgomery_modular_power_with_minimal_allocations(b, e, m, temp_z0, temp_rr, temp_one, temp_z, temp_zz, temp_x, temp_extra, result);
return result;
}
UnsignedBigInteger ep { e };
UnsignedBigInteger base { b };