From b94307583b2e52ab593051c7ef850af9f3b257c4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 17 Nov 2024 13:28:41 -0500 Subject: [PATCH] LibCrypto: Add user-defined literals to convert numbers to a BigInt It is much more convenient to define constants with: 1000_bigint Than with: Crypto::UnsignedBigInteger { 1000 } --- .../LibCrypto/BigInt/UnsignedBigInteger.h | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h index bf606d8fc95..adac4695c2b 100644 --- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h +++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h @@ -172,8 +172,26 @@ struct AK::Formatter : Formatter { ErrorOr format(FormatBuilder&, Crypto::UnsignedBigInteger const&); }; -inline Crypto::UnsignedBigInteger -operator""_bigint(char const* string, size_t length) +inline Crypto::UnsignedBigInteger operator""_bigint(char const* string, size_t length) { return MUST(Crypto::UnsignedBigInteger::from_base(10, { string, length })); } + +inline Crypto::UnsignedBigInteger operator""_bigint(unsigned long long value) +{ + auto result = Crypto::UnsignedBigInteger { static_cast(value) }; + VERIFY(!result.is_invalid()); + + return result; +} + +inline Crypto::UnsignedBigInteger operator""_bigint(long double value) +{ + VERIFY(value >= 0); + VERIFY(value < static_cast(NumericLimits::max())); + + auto result = Crypto::UnsignedBigInteger { static_cast(value) }; + VERIFY(!result.is_invalid()); + + return result; +}