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>
|
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
|
|
|
|
2020-04-10 01:00:37 +04:30
|
|
|
#include <AK/ByteBuffer.h>
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2022-11-14 18:20:59 +00:00
|
|
|
#include <AK/Concepts.h>
|
2020-07-27 14:44:40 +02:00
|
|
|
#include <AK/Span.h>
|
2023-01-13 11:40:04 -05:00
|
|
|
#include <AK/String.h>
|
2020-04-07 22:37:42 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
|
|
namespace Crypto {
|
2020-04-09 12:52:25 +03:00
|
|
|
|
|
|
|
|
struct UnsignedDivisionResult;
|
2021-05-13 09:37:14 +02:00
|
|
|
constexpr size_t STARTING_WORD_SIZE = 32;
|
2020-04-09 12:52:25 +03:00
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
class UnsignedBigInteger {
|
|
|
|
|
public:
|
2021-05-12 13:25:55 +02:00
|
|
|
using Word = u32;
|
|
|
|
|
static constexpr size_t BITS_IN_WORD = 32;
|
|
|
|
|
|
2022-08-25 23:35:34 +02:00
|
|
|
// This constructor accepts any unsigned with size up to Word.
|
2022-11-14 18:20:59 +00:00
|
|
|
template<Integral T>
|
|
|
|
|
requires(sizeof(T) <= sizeof(Word))
|
2022-10-17 00:06:11 +02:00
|
|
|
UnsignedBigInteger(T value)
|
2022-08-25 23:35:34 +02:00
|
|
|
{
|
|
|
|
|
m_words.append(static_cast<Word>(value));
|
|
|
|
|
}
|
2020-04-08 19:04:36 +03:00
|
|
|
|
2021-05-12 13:25:55 +02:00
|
|
|
explicit UnsignedBigInteger(Vector<Word, STARTING_WORD_SIZE>&& words)
|
2020-05-03 10:56:00 +02:00
|
|
|
: m_words(move(words))
|
2020-04-08 19:04:36 +03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
explicit UnsignedBigInteger(u8 const* ptr, size_t length);
|
2020-07-22 16:16:05 -06:00
|
|
|
|
2022-08-25 23:37:09 +02:00
|
|
|
explicit UnsignedBigInteger(double value);
|
|
|
|
|
|
2022-08-26 00:49:50 +02:00
|
|
|
explicit UnsignedBigInteger(u64 value)
|
|
|
|
|
{
|
|
|
|
|
static_assert(sizeof(u64) == sizeof(Word) * 2);
|
|
|
|
|
m_words.resize_and_keep_capacity(2);
|
|
|
|
|
m_words[0] = static_cast<Word>(value & 0xFFFFFFFF);
|
|
|
|
|
m_words[1] = static_cast<Word>((value >> 32) & 0xFFFFFFFF);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 10:32:08 -07:00
|
|
|
UnsignedBigInteger() = default;
|
2020-04-07 22:37:42 +03:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] static UnsignedBigInteger create_invalid();
|
2020-04-08 13:07:47 +03:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] static UnsignedBigInteger import_data(StringView data) { return import_data((u8 const*)data.characters_without_null_termination(), data.length()); }
|
|
|
|
|
[[nodiscard]] static UnsignedBigInteger import_data(u8 const* ptr, size_t length)
|
2020-07-22 16:16:05 -06:00
|
|
|
{
|
|
|
|
|
return UnsignedBigInteger(ptr, length);
|
|
|
|
|
}
|
2020-04-10 01:00:37 +04:30
|
|
|
|
2020-07-31 13:33:14 +04:30
|
|
|
size_t export_data(Bytes, bool remove_leading_zeros = false) 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;
|
2023-12-16 17:49:34 +03:30
|
|
|
[[nodiscard]] ByteString to_base_deprecated(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
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] Vector<Word, STARTING_WORD_SIZE> const& words() const { return m_words; }
|
2020-04-07 22:37:42 +03:00
|
|
|
|
2020-05-03 10:57:00 +02:00
|
|
|
void set_to_0();
|
2021-05-12 13:25:55 +02:00
|
|
|
void set_to(Word other);
|
2022-04-01 20:58:27 +03:00
|
|
|
void set_to(UnsignedBigInteger const& other);
|
2020-05-07 12:23:09 +02:00
|
|
|
|
|
|
|
|
void invalidate()
|
|
|
|
|
{
|
|
|
|
|
m_is_invalid = true;
|
|
|
|
|
m_cached_trimmed_length = {};
|
2021-06-08 23:43:44 +03:00
|
|
|
m_cached_hash = 0;
|
2020-05-07 12:23:09 +02:00
|
|
|
}
|
2020-05-03 10:56:00 +02:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] bool is_zero() const;
|
|
|
|
|
[[nodiscard]] bool is_odd() const { return m_words.size() && (m_words[0] & 1); }
|
|
|
|
|
[[nodiscard]] bool is_invalid() const { return m_is_invalid; }
|
2020-05-03 10:56:00 +02:00
|
|
|
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] size_t length() const { return m_words.size(); }
|
2020-05-03 10:56:00 +02:00
|
|
|
// The "trimmed length" is the number of words after trimming leading zeroed words
|
2022-01-04 17:00:53 +01:00
|
|
|
[[nodiscard]] size_t trimmed_length() const;
|
2020-05-03 10:56:00 +02:00
|
|
|
|
2021-04-01 12:02:14 +04:30
|
|
|
void clamp_to_trimmed_length();
|
2021-05-12 10:47:21 +02:00
|
|
|
void resize_with_leading_zeros(size_t num_words);
|
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;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger minus(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_or(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_and(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_xor(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger bitwise_not_fill_to_one_based_index(size_t) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger shift_left(size_t num_bits) const;
|
|
|
|
|
[[nodiscard]] UnsignedBigInteger multiplied_by(UnsignedBigInteger const& other) const;
|
|
|
|
|
[[nodiscard]] UnsignedDivisionResult divided_by(UnsignedBigInteger const& divisor) 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
|
|
|
|
2020-04-09 12:52:25 +03:00
|
|
|
void set_bit_inplace(size_t bit_index);
|
2020-04-07 22:37:42 +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;
|
|
|
|
|
[[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:
|
2021-05-10 20:55:25 +02:00
|
|
|
friend class UnsignedBigIntegerAlgorithms;
|
2020-08-15 22:53:28 +02:00
|
|
|
// Little endian
|
2021-05-12 13:25:55 +02:00
|
|
|
// m_word[0] + m_word[1] * Word::MAX + m_word[2] * Word::MAX * Word::MAX + ...
|
|
|
|
|
Vector<Word, STARTING_WORD_SIZE> m_words;
|
2020-04-08 13:07:47 +03:00
|
|
|
|
2021-06-08 23:43:44 +03:00
|
|
|
mutable u32 m_cached_hash { 0 };
|
|
|
|
|
|
2020-04-08 13:07:47 +03:00
|
|
|
// Used to indicate a negative result, or a result of an invalid operation
|
|
|
|
|
bool m_is_invalid { false };
|
2020-05-07 12:23:09 +02:00
|
|
|
|
|
|
|
|
mutable Optional<size_t> m_cached_trimmed_length;
|
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
|
|
|
};
|
|
|
|
|
|
2020-04-10 01:00:37 +04:30
|
|
|
inline Crypto::UnsignedBigInteger
|
2022-04-01 20:58:27 +03:00
|
|
|
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
|
|
|
}
|