UnsignedBigInteger.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/ByteString.h>
  11. #include <AK/Concepts.h>
  12. #include <AK/Span.h>
  13. #include <AK/String.h>
  14. #include <AK/Types.h>
  15. #include <AK/Vector.h>
  16. namespace Crypto {
  17. struct UnsignedDivisionResult;
  18. constexpr size_t STARTING_WORD_SIZE = 32;
  19. class UnsignedBigInteger {
  20. public:
  21. using Word = u32;
  22. static constexpr size_t BITS_IN_WORD = 32;
  23. // This constructor accepts any unsigned with size up to Word.
  24. template<Integral T>
  25. requires(sizeof(T) <= sizeof(Word))
  26. UnsignedBigInteger(T value)
  27. {
  28. m_words.append(static_cast<Word>(value));
  29. }
  30. explicit UnsignedBigInteger(Vector<Word, STARTING_WORD_SIZE>&& words)
  31. : m_words(move(words))
  32. {
  33. }
  34. explicit UnsignedBigInteger(u8 const* ptr, size_t length);
  35. explicit UnsignedBigInteger(double value);
  36. explicit UnsignedBigInteger(u64 value)
  37. {
  38. static_assert(sizeof(u64) == sizeof(Word) * 2);
  39. m_words.resize_and_keep_capacity(2);
  40. m_words[0] = static_cast<Word>(value & 0xFFFFFFFF);
  41. m_words[1] = static_cast<Word>((value >> 32) & 0xFFFFFFFF);
  42. }
  43. UnsignedBigInteger() = default;
  44. [[nodiscard]] static UnsignedBigInteger create_invalid();
  45. [[nodiscard]] static UnsignedBigInteger import_data(StringView data) { return import_data((u8 const*)data.characters_without_null_termination(), data.length()); }
  46. [[nodiscard]] static UnsignedBigInteger import_data(u8 const* ptr, size_t length)
  47. {
  48. return UnsignedBigInteger(ptr, length);
  49. }
  50. size_t export_data(Bytes, bool remove_leading_zeros = false) const;
  51. [[nodiscard]] static ErrorOr<UnsignedBigInteger> from_base(u16 N, StringView str);
  52. [[nodiscard]] ErrorOr<String> to_base(u16 N) const;
  53. [[nodiscard]] ByteString to_base_deprecated(u16 N) const;
  54. [[nodiscard]] u64 to_u64() const;
  55. enum class RoundingMode {
  56. IEEERoundAndTiesToEvenMantissa,
  57. RoundTowardZero,
  58. // “the Number value for x”, https://tc39.es/ecma262/#number-value-for
  59. ECMAScriptNumberValueFor = IEEERoundAndTiesToEvenMantissa,
  60. };
  61. [[nodiscard]] double to_double(RoundingMode rounding_mode = RoundingMode::IEEERoundAndTiesToEvenMantissa) const;
  62. [[nodiscard]] Vector<Word, STARTING_WORD_SIZE> const& words() const { return m_words; }
  63. void set_to_0();
  64. void set_to(Word other);
  65. void set_to(UnsignedBigInteger const& other);
  66. void invalidate()
  67. {
  68. m_is_invalid = true;
  69. m_cached_trimmed_length = {};
  70. m_cached_hash = 0;
  71. }
  72. [[nodiscard]] bool is_zero() const;
  73. [[nodiscard]] bool is_odd() const { return m_words.size() && (m_words[0] & 1); }
  74. [[nodiscard]] bool is_invalid() const { return m_is_invalid; }
  75. [[nodiscard]] size_t length() const { return m_words.size(); }
  76. // The "trimmed length" is the number of words after trimming leading zeroed words
  77. [[nodiscard]] size_t trimmed_length() const;
  78. void clamp_to_trimmed_length();
  79. void resize_with_leading_zeros(size_t num_words);
  80. size_t one_based_index_of_highest_set_bit() const;
  81. [[nodiscard]] UnsignedBigInteger plus(UnsignedBigInteger const& other) const;
  82. [[nodiscard]] UnsignedBigInteger minus(UnsignedBigInteger const& other) const;
  83. [[nodiscard]] UnsignedBigInteger bitwise_or(UnsignedBigInteger const& other) const;
  84. [[nodiscard]] UnsignedBigInteger bitwise_and(UnsignedBigInteger const& other) const;
  85. [[nodiscard]] UnsignedBigInteger bitwise_xor(UnsignedBigInteger const& other) const;
  86. [[nodiscard]] UnsignedBigInteger bitwise_not_fill_to_one_based_index(size_t) const;
  87. [[nodiscard]] UnsignedBigInteger shift_left(size_t num_bits) const;
  88. [[nodiscard]] UnsignedBigInteger multiplied_by(UnsignedBigInteger const& other) const;
  89. [[nodiscard]] UnsignedDivisionResult divided_by(UnsignedBigInteger const& divisor) const;
  90. [[nodiscard]] u32 hash() const;
  91. void set_bit_inplace(size_t bit_index);
  92. [[nodiscard]] bool operator==(UnsignedBigInteger const& other) const;
  93. [[nodiscard]] bool operator!=(UnsignedBigInteger const& other) const;
  94. [[nodiscard]] bool operator<(UnsignedBigInteger const& other) const;
  95. [[nodiscard]] bool operator>(UnsignedBigInteger const& other) const;
  96. [[nodiscard]] bool operator>=(UnsignedBigInteger const& other) const;
  97. enum class CompareResult {
  98. DoubleEqualsBigInt,
  99. DoubleLessThanBigInt,
  100. DoubleGreaterThanBigInt
  101. };
  102. [[nodiscard]] CompareResult compare_to_double(double) const;
  103. private:
  104. friend class UnsignedBigIntegerAlgorithms;
  105. // Little endian
  106. // m_word[0] + m_word[1] * Word::MAX + m_word[2] * Word::MAX * Word::MAX + ...
  107. Vector<Word, STARTING_WORD_SIZE> m_words;
  108. mutable u32 m_cached_hash { 0 };
  109. // Used to indicate a negative result, or a result of an invalid operation
  110. bool m_is_invalid { false };
  111. mutable Optional<size_t> m_cached_trimmed_length;
  112. };
  113. struct UnsignedDivisionResult {
  114. Crypto::UnsignedBigInteger quotient;
  115. Crypto::UnsignedBigInteger remainder;
  116. };
  117. }
  118. template<>
  119. struct AK::Formatter<Crypto::UnsignedBigInteger> : Formatter<StringView> {
  120. ErrorOr<void> format(FormatBuilder&, Crypto::UnsignedBigInteger const&);
  121. };
  122. inline Crypto::UnsignedBigInteger
  123. operator""_bigint(char const* string, size_t length)
  124. {
  125. return MUST(Crypto::UnsignedBigInteger::from_base(10, { string, length }));
  126. }