mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
LibCrypto: Remove now unused temporary BigInt buffers
Plus 1 drive-by division->shift optimization
This commit is contained in:
parent
c96d44e9cf
commit
70cfa60f56
Notes:
sideshowbarker
2024-07-17 16:23:55 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/70cfa60f56 Pull-request: https://github.com/SerenityOS/serenity/pull/23619 Issue: https://github.com/SerenityOS/serenity/issues/23575 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/DanShaders ✅
7 changed files with 19 additions and 42 deletions
|
@ -22,10 +22,6 @@ using AK::Detail::dword;
|
|||
FLATTEN void UnsignedBigIntegerAlgorithms::divide_without_allocation(
|
||||
UnsignedBigInteger const& numerator,
|
||||
UnsignedBigInteger const& denominator,
|
||||
[[maybe_unused]] UnsignedBigInteger& temp_shift_result,
|
||||
[[maybe_unused]] UnsignedBigInteger& temp_shift_plus,
|
||||
[[maybe_unused]] UnsignedBigInteger& temp_shift,
|
||||
[[maybe_unused]] UnsignedBigInteger& temp_minus,
|
||||
UnsignedBigInteger& quotient,
|
||||
UnsignedBigInteger& remainder)
|
||||
{
|
||||
|
|
|
@ -12,10 +12,6 @@ namespace Crypto {
|
|||
void UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(
|
||||
UnsignedBigInteger& temp_a,
|
||||
UnsignedBigInteger& temp_b,
|
||||
UnsignedBigInteger& temp_1,
|
||||
UnsignedBigInteger& temp_2,
|
||||
UnsignedBigInteger& temp_3,
|
||||
UnsignedBigInteger& temp_4,
|
||||
UnsignedBigInteger& temp_quotient,
|
||||
UnsignedBigInteger& temp_remainder,
|
||||
UnsignedBigInteger& output)
|
||||
|
@ -27,7 +23,7 @@ void UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(
|
|||
}
|
||||
|
||||
// temp_b %= temp_a
|
||||
divide_without_allocation(temp_b, temp_a, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
divide_without_allocation(temp_b, temp_a, temp_quotient, temp_remainder);
|
||||
temp_b.set_to(temp_remainder);
|
||||
if (temp_b == 0) {
|
||||
output.set_to(temp_a);
|
||||
|
@ -35,7 +31,7 @@ void UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(
|
|||
}
|
||||
|
||||
// temp_a %= temp_b
|
||||
divide_without_allocation(temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
divide_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder);
|
||||
temp_a.set_to(temp_remainder);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,6 @@ void UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(
|
|||
UnsignedBigInteger const& a,
|
||||
UnsignedBigInteger const& b,
|
||||
UnsignedBigInteger& temp_1,
|
||||
UnsignedBigInteger& temp_2,
|
||||
UnsignedBigInteger& temp_3,
|
||||
UnsignedBigInteger& temp_4,
|
||||
UnsignedBigInteger& temp_minus,
|
||||
UnsignedBigInteger& temp_quotient,
|
||||
UnsignedBigInteger& temp_d,
|
||||
|
@ -87,7 +84,7 @@ void UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(
|
|||
}
|
||||
|
||||
// return x % b
|
||||
divide_without_allocation(temp_x, b, temp_1, temp_2, temp_3, temp_4, temp_quotient, result);
|
||||
divide_without_allocation(temp_x, b, temp_quotient, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ void UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(
|
|||
UnsignedBigInteger& temp_1,
|
||||
UnsignedBigInteger& temp_2,
|
||||
UnsignedBigInteger& temp_3,
|
||||
UnsignedBigInteger& temp_4,
|
||||
UnsignedBigInteger& temp_multiply,
|
||||
UnsignedBigInteger& temp_quotient,
|
||||
UnsignedBigInteger& temp_remainder,
|
||||
|
@ -27,17 +26,16 @@ void UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(
|
|||
if (ep.words()[0] % 2 == 1) {
|
||||
// exp = (exp * base) % m;
|
||||
multiply_without_allocation(exp, base, temp_1, temp_2, temp_3, temp_multiply);
|
||||
divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
divide_without_allocation(temp_multiply, m, temp_quotient, temp_remainder);
|
||||
exp.set_to(temp_remainder);
|
||||
}
|
||||
|
||||
// ep = ep / 2;
|
||||
divide_u16_without_allocation(ep, 2, temp_quotient, temp_remainder);
|
||||
ep.set_to(temp_quotient);
|
||||
ep.set_to(ep.shift_right(1));
|
||||
|
||||
// base = (base * base) % m;
|
||||
multiply_without_allocation(base, base, temp_1, temp_2, temp_3, temp_multiply);
|
||||
divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
divide_without_allocation(temp_multiply, m, 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
|
||||
|
@ -208,13 +206,13 @@ void UnsignedBigIntegerAlgorithms::montgomery_modular_power_with_minimal_allocat
|
|||
|
||||
// rr = ( 2 ^ (2 * modulo.length() * BITS_IN_WORD) ) % modulo
|
||||
shift_left_by_n_words(one, 2 * num_words, x);
|
||||
divide_without_allocation(x, modulo, temp_z, one, z, zz, temp_extra, rr);
|
||||
divide_without_allocation(x, modulo, temp_extra, rr);
|
||||
rr.resize_with_leading_zeros(num_words);
|
||||
|
||||
// x = base [% modulo, if x doesn't already fit in modulo's words]
|
||||
x.set_to(base);
|
||||
if (x.trimmed_length() > num_words)
|
||||
divide_without_allocation(base, modulo, temp_z, one, z, zz, temp_extra, x);
|
||||
divide_without_allocation(base, modulo, temp_extra, x);
|
||||
x.resize_with_leading_zeros(num_words);
|
||||
|
||||
one.set_to(1);
|
||||
|
@ -274,11 +272,10 @@ void UnsignedBigIntegerAlgorithms::montgomery_modular_power_with_minimal_allocat
|
|||
dbgln("Encountered the modulo branch during a montgomery modular power. Params : {} - {} - {}", base, exponent, modulo);
|
||||
// We just clobber all the other temporaries that we don't need for the division.
|
||||
// This is wasteful, but we're on the edgiest of cases already.
|
||||
divide_without_allocation(zz, modulo, temp_z, rr, z, x, temp_extra, result);
|
||||
divide_without_allocation(zz, modulo, temp_extra, result);
|
||||
}
|
||||
|
||||
result.clamp_to_trimmed_length();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,12 +24,12 @@ public:
|
|||
static void shift_left_without_allocation(UnsignedBigInteger const& number, size_t bits_to_shift_by, UnsignedBigInteger& temp_result, UnsignedBigInteger& temp_plus, UnsignedBigInteger& output);
|
||||
static void shift_right_without_allocation(UnsignedBigInteger const& number, size_t num_bits, UnsignedBigInteger& output);
|
||||
static void multiply_without_allocation(UnsignedBigInteger const& left, UnsignedBigInteger const& right, UnsignedBigInteger& temp_shift_result, UnsignedBigInteger& temp_shift_plus, UnsignedBigInteger& temp_shift, UnsignedBigInteger& output);
|
||||
static void divide_without_allocation(UnsignedBigInteger const& numerator, UnsignedBigInteger const& denominator, UnsignedBigInteger& temp_shift_result, UnsignedBigInteger& temp_shift_plus, UnsignedBigInteger& temp_shift, UnsignedBigInteger& temp_minus, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder);
|
||||
static void divide_without_allocation(UnsignedBigInteger const& numerator, UnsignedBigInteger const& denominator, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder);
|
||||
static void divide_u16_without_allocation(UnsignedBigInteger const& numerator, UnsignedBigInteger::Word denominator, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder);
|
||||
|
||||
static void destructive_GCD_without_allocation(UnsignedBigInteger& temp_a, UnsignedBigInteger& temp_b, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_2, UnsignedBigInteger& temp_3, UnsignedBigInteger& temp_4, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_remainder, UnsignedBigInteger& output);
|
||||
static void modular_inverse_without_allocation(UnsignedBigInteger const& a_, UnsignedBigInteger const& b, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_2, UnsignedBigInteger& temp_3, UnsignedBigInteger& temp_4, UnsignedBigInteger& temp_minus, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_d, UnsignedBigInteger& temp_u, UnsignedBigInteger& temp_v, UnsignedBigInteger& temp_x, UnsignedBigInteger& result);
|
||||
static void destructive_modular_power_without_allocation(UnsignedBigInteger& ep, UnsignedBigInteger& base, UnsignedBigInteger const& m, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_2, UnsignedBigInteger& temp_3, UnsignedBigInteger& temp_4, UnsignedBigInteger& temp_multiply, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_remainder, UnsignedBigInteger& result);
|
||||
static void destructive_GCD_without_allocation(UnsignedBigInteger& temp_a, UnsignedBigInteger& temp_b, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_remainder, UnsignedBigInteger& output);
|
||||
static void modular_inverse_without_allocation(UnsignedBigInteger const& a_, UnsignedBigInteger const& b, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_minus, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_d, UnsignedBigInteger& temp_u, UnsignedBigInteger& temp_v, UnsignedBigInteger& temp_x, UnsignedBigInteger& result);
|
||||
static void destructive_modular_power_without_allocation(UnsignedBigInteger& ep, UnsignedBigInteger& base, UnsignedBigInteger const& m, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_2, UnsignedBigInteger& temp_3, UnsignedBigInteger& temp_multiply, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_remainder, UnsignedBigInteger& result);
|
||||
static void montgomery_modular_power_with_minimal_allocations(UnsignedBigInteger const& base, UnsignedBigInteger const& exponent, UnsignedBigInteger const& modulo, UnsignedBigInteger& temp_z0, UnsignedBigInteger& temp_rr, UnsignedBigInteger& temp_one, UnsignedBigInteger& temp_z, UnsignedBigInteger& temp_zz, UnsignedBigInteger& temp_x, UnsignedBigInteger& temp_extra, UnsignedBigInteger& result);
|
||||
|
||||
private:
|
||||
|
|
|
@ -530,7 +530,7 @@ FLATTEN UnsignedDivisionResult UnsignedBigInteger::divided_by(UnsignedBigInteger
|
|||
UnsignedBigInteger temp_shift;
|
||||
UnsignedBigInteger temp_minus;
|
||||
|
||||
UnsignedBigIntegerAlgorithms::divide_without_allocation(*this, divisor, temp_shift_result, temp_shift_plus, temp_shift, temp_minus, quotient, remainder);
|
||||
UnsignedBigIntegerAlgorithms::divide_without_allocation(*this, divisor, quotient, remainder);
|
||||
|
||||
return UnsignedDivisionResult { quotient, remainder };
|
||||
}
|
||||
|
|
|
@ -24,9 +24,6 @@ UnsignedBigInteger ModularInverse(UnsignedBigInteger const& a_, UnsignedBigInteg
|
|||
return { 1 };
|
||||
|
||||
UnsignedBigInteger temp_1;
|
||||
UnsignedBigInteger temp_2;
|
||||
UnsignedBigInteger temp_3;
|
||||
UnsignedBigInteger temp_4;
|
||||
UnsignedBigInteger temp_minus;
|
||||
UnsignedBigInteger temp_quotient;
|
||||
UnsignedBigInteger temp_d;
|
||||
|
@ -35,7 +32,7 @@ UnsignedBigInteger ModularInverse(UnsignedBigInteger const& a_, UnsignedBigInteg
|
|||
UnsignedBigInteger temp_x;
|
||||
UnsignedBigInteger result;
|
||||
|
||||
UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(a_, b, temp_1, temp_2, temp_3, temp_4, temp_minus, temp_quotient, temp_d, temp_u, temp_v, temp_x, result);
|
||||
UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(a_, b, temp_1, temp_minus, temp_quotient, temp_d, temp_u, temp_v, temp_x, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -65,12 +62,11 @@ UnsignedBigInteger ModularPower(UnsignedBigInteger const& b, UnsignedBigInteger
|
|||
UnsignedBigInteger temp_1;
|
||||
UnsignedBigInteger temp_2;
|
||||
UnsignedBigInteger temp_3;
|
||||
UnsignedBigInteger temp_4;
|
||||
UnsignedBigInteger temp_multiply;
|
||||
UnsignedBigInteger temp_quotient;
|
||||
UnsignedBigInteger temp_remainder;
|
||||
|
||||
UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(ep, base, m, temp_1, temp_2, temp_3, temp_4, temp_multiply, temp_quotient, temp_remainder, result);
|
||||
UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(ep, base, m, temp_1, temp_2, temp_3, temp_multiply, temp_quotient, temp_remainder, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -79,15 +75,11 @@ UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
|
|||
{
|
||||
UnsignedBigInteger temp_a { a };
|
||||
UnsignedBigInteger temp_b { b };
|
||||
UnsignedBigInteger temp_1;
|
||||
UnsignedBigInteger temp_2;
|
||||
UnsignedBigInteger temp_3;
|
||||
UnsignedBigInteger temp_4;
|
||||
UnsignedBigInteger temp_quotient;
|
||||
UnsignedBigInteger temp_remainder;
|
||||
UnsignedBigInteger output;
|
||||
|
||||
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder, output);
|
||||
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, output);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -99,20 +91,19 @@ UnsignedBigInteger LCM(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
|
|||
UnsignedBigInteger temp_1;
|
||||
UnsignedBigInteger temp_2;
|
||||
UnsignedBigInteger temp_3;
|
||||
UnsignedBigInteger temp_4;
|
||||
UnsignedBigInteger temp_quotient;
|
||||
UnsignedBigInteger temp_remainder;
|
||||
UnsignedBigInteger gcd_output;
|
||||
UnsignedBigInteger output { 0 };
|
||||
|
||||
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder, gcd_output);
|
||||
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, gcd_output);
|
||||
if (gcd_output == 0) {
|
||||
dbgln_if(NT_DEBUG, "GCD is zero");
|
||||
return output;
|
||||
}
|
||||
|
||||
// output = (a / gcd_output) * b
|
||||
UnsignedBigIntegerAlgorithms::divide_without_allocation(a, gcd_output, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
UnsignedBigIntegerAlgorithms::divide_without_allocation(a, gcd_output, temp_quotient, temp_remainder);
|
||||
UnsignedBigIntegerAlgorithms::multiply_without_allocation(temp_quotient, b, temp_1, temp_2, temp_3, output);
|
||||
|
||||
dbgln_if(NT_DEBUG, "quot: {} rem: {} out: {}", temp_quotient, temp_remainder, output);
|
||||
|
|
Loading…
Reference in a new issue