mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00

Before: - a separate Word element allocation of the underlying Vector<Word> was necessary for every new word in a multi-word shift - two additional temporary UnsignedBigInteger buffers were allocated and passed through, including in downstream calls (e.g. Multiplication) - an additional allocation and word shift for the carry - FIXME note seems to point to some of these issues After: - main change is in LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp - one single allocation per call, using shift_left_by_n_words - only the input "number" and "output" need to be allocated by the caller - downstream calls are adapted not to allocate or pass temporary buffers - noticeable performance improvement when running TestBigInteger: 0.41-0.42s (before) to 0.28-0.29s (after) Intel Core i9 laptop Bonus: remove unused variables from UnsignedBigInteger::divided_by - These were likely cut-and-paste artifacts from UnsignedBigInteger::multiplied_by; not caught by "unused-varible". NOTE: making this change in a separate commit than shift_right, even if it touches the same file BitwiseOperations.cpp since: - it is a "bonus" addition: not necessary for fixing the shift_right bug, but logically unrelated to the shift_right code - it brings a chain of downstream interface modifications (7 files), unrelated to shift_right
94 lines
2.7 KiB
C++
94 lines
2.7 KiB
C++
/*
|
||
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
||
* Copyright (c) 2020-2021, Dex♪ <dexes.ttp@gmail.com>
|
||
* Copyright (c) 2024, Altomani Gianluca <altomanigianluca@gmail.com>
|
||
*
|
||
* SPDX-License-Identifier: BSD-2-Clause
|
||
*/
|
||
|
||
#include "UnsignedBigIntegerAlgorithms.h"
|
||
|
||
namespace Crypto {
|
||
|
||
void UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(
|
||
UnsignedBigInteger& temp_a,
|
||
UnsignedBigInteger& temp_b,
|
||
UnsignedBigInteger& temp_quotient,
|
||
UnsignedBigInteger& temp_remainder,
|
||
UnsignedBigInteger& output)
|
||
{
|
||
for (;;) {
|
||
if (temp_a == 0) {
|
||
output.set_to(temp_b);
|
||
return;
|
||
}
|
||
|
||
// temp_b %= temp_a
|
||
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);
|
||
return;
|
||
}
|
||
|
||
// temp_a %= temp_b
|
||
divide_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder);
|
||
temp_a.set_to(temp_remainder);
|
||
}
|
||
}
|
||
|
||
void UnsignedBigIntegerAlgorithms::extended_GCD_without_allocation(
|
||
UnsignedBigInteger const& a,
|
||
UnsignedBigInteger const& b,
|
||
UnsignedBigInteger& x,
|
||
UnsignedBigInteger& y,
|
||
UnsignedBigInteger& gcd,
|
||
UnsignedBigInteger& temp_quotient,
|
||
UnsignedBigInteger& temp_1,
|
||
UnsignedBigInteger& temp_2,
|
||
UnsignedBigInteger& temp_shift,
|
||
UnsignedBigInteger& temp_r,
|
||
UnsignedBigInteger& temp_s,
|
||
UnsignedBigInteger& temp_t)
|
||
{
|
||
gcd.set_to(a);
|
||
x.set_to(1);
|
||
y.set_to(0);
|
||
|
||
temp_r.set_to(b);
|
||
temp_s.set_to_0();
|
||
temp_t.set_to(1);
|
||
|
||
while (temp_r != 0) {
|
||
// quotient := old_r div r
|
||
divide_without_allocation(gcd, temp_r, temp_quotient, temp_1);
|
||
|
||
temp_2.set_to(temp_r);
|
||
multiply_without_allocation(temp_quotient, temp_r, temp_shift, temp_1);
|
||
while (gcd < temp_1) {
|
||
add_into_accumulator_without_allocation(gcd, b);
|
||
}
|
||
subtract_without_allocation(gcd, temp_1, temp_r);
|
||
gcd.set_to(temp_2);
|
||
|
||
// (old_s, s) := (s, old_s − quotient × s)
|
||
temp_2.set_to(temp_s);
|
||
multiply_without_allocation(temp_quotient, temp_s, temp_shift, temp_1);
|
||
while (x < temp_1) {
|
||
add_into_accumulator_without_allocation(x, b);
|
||
}
|
||
subtract_without_allocation(x, temp_1, temp_s);
|
||
x.set_to(temp_2);
|
||
|
||
// (old_t, t) := (t, old_t − quotient × t)
|
||
temp_2.set_to(temp_t);
|
||
multiply_without_allocation(temp_quotient, temp_t, temp_shift, temp_1);
|
||
while (y < temp_1) {
|
||
add_into_accumulator_without_allocation(y, b);
|
||
}
|
||
subtract_without_allocation(y, temp_1, temp_t);
|
||
y.set_to(temp_2);
|
||
}
|
||
}
|
||
|
||
}
|