mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
LibCrypto: Move GCD
to method of UnsignedBigInteger
Having it as a method instead of a free function is necessary for the next commits and generally allows for optimizations that require deeper access into the `UnsignedBigInteger`.
This commit is contained in:
parent
e251b451ef
commit
a952d000be
Notes:
github-actions[bot]
2025-05-23 09:58:43 +00:00
Author: https://github.com/devgianlu
Commit: a952d000be
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4507
Reviewed-by: https://github.com/gmta ✅
7 changed files with 15 additions and 33 deletions
|
@ -221,7 +221,7 @@ BigFraction BigFraction::rounded(unsigned rounding_threshold) const
|
|||
|
||||
void BigFraction::reduce()
|
||||
{
|
||||
auto const gcd = NumberTheory::GCD(m_numerator.unsigned_value(), m_denominator);
|
||||
auto const gcd = m_numerator.unsigned_value().gcd(m_denominator);
|
||||
|
||||
if (gcd == 1)
|
||||
return;
|
||||
|
|
|
@ -547,6 +547,19 @@ FLATTEN UnsignedDivisionResult UnsignedBigInteger::divided_by(UnsignedBigInteger
|
|||
return UnsignedDivisionResult { quotient, remainder };
|
||||
}
|
||||
|
||||
FLATTEN UnsignedBigInteger UnsignedBigInteger::gcd(UnsignedBigInteger const& other) const
|
||||
{
|
||||
UnsignedBigInteger temp_a { *this };
|
||||
UnsignedBigInteger temp_b { other };
|
||||
UnsignedBigInteger temp_quotient;
|
||||
UnsignedBigInteger temp_remainder;
|
||||
UnsignedBigInteger output;
|
||||
|
||||
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, output);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
u32 UnsignedBigInteger::hash() const
|
||||
{
|
||||
if (m_cached_hash != 0)
|
||||
|
|
|
@ -109,6 +109,7 @@ public:
|
|||
[[nodiscard]] UnsignedBigInteger as_n_bits(size_t n) const;
|
||||
[[nodiscard]] UnsignedBigInteger multiplied_by(UnsignedBigInteger const& other) const;
|
||||
[[nodiscard]] UnsignedDivisionResult divided_by(UnsignedBigInteger const& divisor) const;
|
||||
[[nodiscard]] UnsignedBigInteger gcd(UnsignedBigInteger const& other) const;
|
||||
|
||||
[[nodiscard]] ErrorOr<UnsignedBigInteger> try_bitwise_not_fill_to_one_based_index(size_t) const;
|
||||
[[nodiscard]] ErrorOr<UnsignedBigInteger> try_shift_left(size_t num_bits) const;
|
||||
|
|
|
@ -26,7 +26,6 @@ set(SOURCES
|
|||
Hash/PBKDF2.cpp
|
||||
Hash/SHA1.cpp
|
||||
Hash/SHA2.cpp
|
||||
NumberTheory/ModularFunctions.cpp
|
||||
PK/RSA.cpp
|
||||
PK/EC.cpp
|
||||
SecureRandom.cpp
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/Random.h>
|
||||
#include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h>
|
||||
#include <LibCrypto/NumberTheory/ModularFunctions.h>
|
||||
#include <LibCrypto/SecureRandom.h>
|
||||
|
||||
namespace Crypto::NumberTheory {
|
||||
|
||||
UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
|
||||
{
|
||||
UnsignedBigInteger temp_a { a };
|
||||
UnsignedBigInteger temp_b { b };
|
||||
UnsignedBigInteger temp_quotient;
|
||||
UnsignedBigInteger temp_remainder;
|
||||
UnsignedBigInteger output;
|
||||
|
||||
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, output);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
|
@ -34,6 +34,4 @@ static IntegerType Power(IntegerType const& b, IntegerType const& e)
|
|||
return exp;
|
||||
}
|
||||
|
||||
UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b);
|
||||
|
||||
}
|
||||
|
|
|
@ -36,7 +36,6 @@ shared_library("LibCrypto") {
|
|||
"Hash/MD5.cpp",
|
||||
"Hash/SHA1.cpp",
|
||||
"Hash/SHA2.cpp",
|
||||
"NumberTheory/ModularFunctions.cpp",
|
||||
"PK/RSA.cpp",
|
||||
"SecureRandom.cpp",
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue