2020-04-07 22:37:42 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2022-02-26 10:32:08 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2022-08-25 17:56:34 +02:00
|
|
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
2025-04-25 20:54:45 +02:00
|
|
|
* Copyright (c) 2025, Altomani Gianluca <altomanigianluca@gmail.com>
|
2020-04-07 22:37:42 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-07 22:37:42 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-05-28 20:40:53 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2023-01-13 11:40:04 -05:00
|
|
|
#include <AK/String.h>
|
2025-04-25 20:54:45 +02:00
|
|
|
#include <LibCrypto/BigInt/TommathForward.h>
|
2020-04-07 22:37:42 +03:00
|
|
|
|
|
|
|
|
namespace Crypto {
|
2020-04-09 12:52:25 +03:00
|
|
|
|
|
|
|
|
struct UnsignedDivisionResult;
|
|
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
class UnsignedBigInteger {
|
|
|
|
|
public:
|
2022-11-14 18:20:59 +00:00
|
|
|
template<Integral T>
|
2022-10-17 00:06:11 +02:00
|
|
|
UnsignedBigInteger(T value)
|
2025-04-25 20:54:45 +02:00
|
|
|
: UnsignedBigInteger(static_cast<u64>(value))
|
2020-04-08 19:04:36 +03:00
|
|
|
{
|
|
|
|
|
}
|
2025-08-05 02:55:17 +03:00
|
|
|
UnsignedBigInteger(ReadonlyBytes);
|
2020-04-08 19:04:36 +03:00
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
explicit UnsignedBigInteger(Vector<u32> const& words);
|
2022-08-25 23:37:09 +02:00
|
|
|
explicit UnsignedBigInteger(double value);
|
2025-04-25 20:54:45 +02:00
|
|
|
explicit UnsignedBigInteger(u64 value);
|
2022-08-25 23:37:09 +02:00
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
UnsignedBigInteger(UnsignedBigInteger const&);
|
2025-07-21 07:31:33 -04:00
|
|
|
UnsignedBigInteger(UnsignedBigInteger&&);
|
|
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
UnsignedBigInteger& operator=(UnsignedBigInteger const&);
|
2025-07-21 07:31:33 -04:00
|
|
|
UnsignedBigInteger& operator=(UnsignedBigInteger&&);
|
2022-08-26 00:49:50 +02:00
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
UnsignedBigInteger();
|
|
|
|
|
~UnsignedBigInteger();
|
2020-04-07 22:37:42 +03:00
|
|
|
|
2025-08-05 02:55:17 +03:00
|
|
|
[[nodiscard]] static UnsignedBigInteger import_data(ReadonlyBytes data) { return UnsignedBigInteger(data); }
|
2020-04-10 01:00:37 +04:30
|
|
|
|
2025-08-05 02:41:11 +03:00
|
|
|
// Exports in big-endian (msb stored first), trimmed (no leading zeros) format
|
|
|
|
|
[[nodiscard]] Bytes export_data(Bytes) const;
|
2020-04-10 01:00:37 +04:30
|
|
|
|
2024-01-12 21:34:23 +00:00
|
|
|
[[nodiscard]] static ErrorOr<UnsignedBigInteger> from_base(u16 N, StringView str);
|
2023-01-13 11:40:04 -05:00
|
|
|
[[nodiscard]] ErrorOr<String> to_base(u16 N) const;
|
2020-05-03 10:56:00 +02:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] u64 to_u64() const;
|
2022-08-25 17:56:34 +02:00
|
|
|
|
|
|
|
|
enum class RoundingMode {
|
|
|
|
|
IEEERoundAndTiesToEvenMantissa,
|
|
|
|
|
RoundTowardZero,
|
|
|
|
|
// “the Number value for x”, https://tc39.es/ecma262/#number-value-for
|
|
|
|
|
ECMAScriptNumberValueFor = IEEERoundAndTiesToEvenMantissa,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] double to_double(RoundingMode rounding_mode = RoundingMode::IEEERoundAndTiesToEvenMantissa) const;
|
2021-06-14 02:02:53 +03:00
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
[[nodiscard]] Vector<u32> words() const;
|
2020-04-07 22:37:42 +03:00
|
|
|
|
2020-05-03 10:57:00 +02:00
|
|
|
void set_to_0();
|
2025-04-25 20:54:45 +02:00
|
|
|
void set_to(u64 other);
|
2022-04-01 20:58:27 +03:00
|
|
|
void set_to(UnsignedBigInteger const& other);
|
2020-05-07 12:23:09 +02:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] bool is_zero() const;
|
2025-04-25 20:54:45 +02:00
|
|
|
[[nodiscard]] bool is_odd() const;
|
2020-05-03 10:56:00 +02:00
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
[[nodiscard]] size_t byte_length() const;
|
2021-04-01 12:02:14 +04:30
|
|
|
|
2022-01-18 08:46:42 -05:00
|
|
|
size_t one_based_index_of_highest_set_bit() const;
|
|
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] UnsignedBigInteger plus(UnsignedBigInteger const& other) const;
|
2025-04-25 21:28:00 +02:00
|
|
|
[[nodiscard]] ErrorOr<UnsignedBigInteger> minus(UnsignedBigInteger const& other) const;
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_or(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_and(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_xor(UnsignedBigInteger const& other) const;
|
2025-04-25 20:54:45 +02:00
|
|
|
[[nodiscard]] ErrorOr<UnsignedBigInteger> bitwise_not_fill_to_one_based_index(size_t) const;
|
|
|
|
|
[[nodiscard]] ErrorOr<UnsignedBigInteger> shift_left(size_t num_bits) const;
|
2024-03-18 00:40:03 +01:00
|
|
|
[[nodiscard]] UnsignedBigInteger shift_right(size_t num_bits) const;
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] UnsignedBigInteger multiplied_by(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedDivisionResult divided_by(UnsignedBigInteger const& divisor) const;
|
2025-04-26 11:17:32 +02:00
|
|
|
[[nodiscard]] UnsignedBigInteger pow(u32 exponent) const;
|
2025-04-26 11:01:48 +02:00
|
|
|
[[nodiscard]] UnsignedBigInteger gcd(UnsignedBigInteger const& other) const;
|
2025-06-01 14:31:15 +02:00
|
|
|
[[nodiscard]] UnsignedBigInteger lcm(UnsignedBigInteger const& other) const;
|
2020-04-09 12:52:25 +03:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] u32 hash() const;
|
2021-06-08 23:43:44 +03:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] bool operator==(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] bool operator!=(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] bool operator<(UnsignedBigInteger const& other) const;
|
2024-11-17 13:27:52 -05:00
|
|
|
[[nodiscard]] bool operator<=(UnsignedBigInteger const& other) const;
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] bool operator>(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] bool operator>=(UnsignedBigInteger const& other) const;
|
2020-04-08 13:07:47 +03:00
|
|
|
|
2022-10-23 16:46:35 +01:00
|
|
|
enum class CompareResult {
|
|
|
|
|
DoubleEqualsBigInt,
|
|
|
|
|
DoubleLessThanBigInt,
|
|
|
|
|
DoubleGreaterThanBigInt
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] CompareResult compare_to_double(double) const;
|
|
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
private:
|
2025-04-25 20:54:45 +02:00
|
|
|
friend class SignedBigInteger;
|
2020-04-08 13:07:47 +03:00
|
|
|
|
2025-04-25 20:54:45 +02:00
|
|
|
mp_int m_mp;
|
|
|
|
|
mutable Optional<u32> m_hash {};
|
2020-04-07 22:37:42 +03:00
|
|
|
};
|
|
|
|
|
|
2020-04-09 12:52:25 +03:00
|
|
|
struct UnsignedDivisionResult {
|
|
|
|
|
Crypto::UnsignedBigInteger quotient;
|
|
|
|
|
Crypto::UnsignedBigInteger remainder;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 13:32:26 +01:00
|
|
|
template<>
|
|
|
|
|
struct AK::Formatter<Crypto::UnsignedBigInteger> : Formatter<StringView> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder&, Crypto::UnsignedBigInteger const&);
|
2021-01-11 13:32:26 +01:00
|
|
|
};
|
|
|
|
|
|
2024-11-17 13:28:41 -05:00
|
|
|
inline Crypto::UnsignedBigInteger operator""_bigint(char const* string, size_t length)
|
2020-04-10 01:00:37 +04:30
|
|
|
{
|
2024-01-12 21:34:23 +00:00
|
|
|
return MUST(Crypto::UnsignedBigInteger::from_base(10, { string, length }));
|
2020-04-10 01:00:37 +04:30
|
|
|
}
|
2024-11-17 13:28:41 -05:00
|
|
|
|
|
|
|
|
inline Crypto::UnsignedBigInteger operator""_bigint(unsigned long long value)
|
|
|
|
|
{
|
2025-04-25 21:28:00 +02:00
|
|
|
return Crypto::UnsignedBigInteger { static_cast<u64>(value) };
|
2024-11-17 13:28:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Crypto::UnsignedBigInteger operator""_bigint(long double value)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(value >= 0);
|
|
|
|
|
VERIFY(value < static_cast<long double>(NumericLimits<double>::max()));
|
|
|
|
|
|
2025-04-25 21:28:00 +02:00
|
|
|
return Crypto::UnsignedBigInteger { static_cast<double>(value) };
|
2024-11-17 13:28:41 -05:00
|
|
|
}
|