summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCrypto/NumberTheory
diff options
context:
space:
mode:
authorDexesTTP <dexes.ttp@gmail.com>2021-05-12 22:47:07 +0200
committerLinus Groh <mail@linusgroh.de>2021-05-13 19:18:07 +0100
commit485adb5e29853cb54a3dc4a314f37e7a5528f57c (patch)
tree32646a81e9541bd028bc43a62b5c6155abfe02a3 /Userland/Libraries/LibCrypto/NumberTheory
parent5071989545a7d60a62eca2928d4859e657c7c10c (diff)
downloadserenity-485adb5e29853cb54a3dc4a314f37e7a5528f57c.zip
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.
Diffstat (limited to 'Userland/Libraries/LibCrypto/NumberTheory')
-rw-r--r--Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp b/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp
index 83212d8b84..a4beb68e45 100644
--- a/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp
+++ b/Userland/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp
@@ -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 };