From a952d000be09749de212d5807bd99e67415f3777 Mon Sep 17 00:00:00 2001 From: devgianlu Date: Sat, 26 Apr 2025 11:01:48 +0200 Subject: [PATCH] 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`. --- .../LibCrypto/BigFraction/BigFraction.cpp | 2 +- .../LibCrypto/BigInt/UnsignedBigInteger.cpp | 13 +++++++++ .../LibCrypto/BigInt/UnsignedBigInteger.h | 1 + Libraries/LibCrypto/CMakeLists.txt | 1 - .../NumberTheory/ModularFunctions.cpp | 28 ------------------- .../LibCrypto/NumberTheory/ModularFunctions.h | 2 -- .../Userland/Libraries/LibCrypto/BUILD.gn | 1 - 7 files changed, 15 insertions(+), 33 deletions(-) delete mode 100644 Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp diff --git a/Libraries/LibCrypto/BigFraction/BigFraction.cpp b/Libraries/LibCrypto/BigFraction/BigFraction.cpp index 0bf8025faf9..81b091d972a 100644 --- a/Libraries/LibCrypto/BigFraction/BigFraction.cpp +++ b/Libraries/LibCrypto/BigFraction/BigFraction.cpp @@ -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; diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index 641ab4bc3c4..aed8850487e 100644 --- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -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) diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h index 82b61107d68..f626fc93efc 100644 --- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h +++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h @@ -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 try_bitwise_not_fill_to_one_based_index(size_t) const; [[nodiscard]] ErrorOr try_shift_left(size_t num_bits) const; diff --git a/Libraries/LibCrypto/CMakeLists.txt b/Libraries/LibCrypto/CMakeLists.txt index eef134df949..9370208bcd3 100644 --- a/Libraries/LibCrypto/CMakeLists.txt +++ b/Libraries/LibCrypto/CMakeLists.txt @@ -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 diff --git a/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp b/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp deleted file mode 100644 index 5a464d6de82..00000000000 --- a/Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2020, Ali Mohammad Pur - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include -#include - -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; -} - -} diff --git a/Libraries/LibCrypto/NumberTheory/ModularFunctions.h b/Libraries/LibCrypto/NumberTheory/ModularFunctions.h index b63dcc252e6..7a2e0924fc7 100644 --- a/Libraries/LibCrypto/NumberTheory/ModularFunctions.h +++ b/Libraries/LibCrypto/NumberTheory/ModularFunctions.h @@ -34,6 +34,4 @@ static IntegerType Power(IntegerType const& b, IntegerType const& e) return exp; } -UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b); - } diff --git a/Meta/gn/secondary/Userland/Libraries/LibCrypto/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibCrypto/BUILD.gn index 06173b61fbb..dfaa6577daf 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibCrypto/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibCrypto/BUILD.gn @@ -36,7 +36,6 @@ shared_library("LibCrypto") { "Hash/MD5.cpp", "Hash/SHA1.cpp", "Hash/SHA2.cpp", - "NumberTheory/ModularFunctions.cpp", "PK/RSA.cpp", "SecureRandom.cpp", ]