From c87d10365bec1d5f7314e9333826039070e3f280 Mon Sep 17 00:00:00 2001 From: davidot Date: Thu, 25 Aug 2022 23:35:34 +0200 Subject: [PATCH] LibCrypto: Make the constructors of (Un)SignedBigInteger templated This means it can take any (un)signed word of size at most Word. This means the constructor can be disambiguated if we were to add a double constructor :^). This requires a change in just one test. --- Tests/LibCrypto/TestBigInteger.cpp | 2 +- Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h | 8 +++++--- Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h | 8 +++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Tests/LibCrypto/TestBigInteger.cpp b/Tests/LibCrypto/TestBigInteger.cpp index 3a947127398..e28fdf8b758 100644 --- a/Tests/LibCrypto/TestBigInteger.cpp +++ b/Tests/LibCrypto/TestBigInteger.cpp @@ -73,7 +73,7 @@ TEST_CASE(test_unsigned_bigint_basic_add_to_accumulator) TEST_CASE(test_unsigned_bigint_basic_add_to_empty_accumulator) { - Crypto::UnsignedBigInteger num1({}); + Crypto::UnsignedBigInteger num1 {}; Crypto::UnsignedBigInteger num2(10); Crypto::UnsignedBigIntegerAlgorithms::add_into_accumulator_without_allocation(num1, num2); EXPECT_EQ(num1.words(), Vector { 10 }); diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h index 4a881df0a40..6e0b48ee23b 100644 --- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h @@ -16,9 +16,11 @@ struct SignedDivisionResult; class SignedBigInteger { public: - SignedBigInteger(i32 x) - : m_sign(x < 0) - , m_unsigned_data(abs(x)) + template + requires(IsSigned && sizeof(T) <= sizeof(i32)) + SignedBigInteger(T value) + : m_sign(value < 0) + , m_unsigned_data(abs(static_cast(value))) { } diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h index fd413308dbc..780853a0878 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h @@ -24,7 +24,13 @@ public: using Word = u32; static constexpr size_t BITS_IN_WORD = 32; - UnsignedBigInteger(Word x) { m_words.append(x); } + // This constructor accepts any unsigned with size up to Word. + template + requires(IsIntegral && sizeof(T) <= sizeof(Word)) + UnsignedBigInteger(T value) + { + m_words.append(static_cast(value)); + } explicit UnsignedBigInteger(Vector&& words) : m_words(move(words))