SignedBigInteger.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Span.h>
  9. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  10. namespace Crypto {
  11. struct SignedDivisionResult;
  12. class SignedBigInteger {
  13. public:
  14. template<typename T>
  15. requires(IsSigned<T> && sizeof(T) <= sizeof(i32))
  16. SignedBigInteger(T value)
  17. : m_sign(value < 0)
  18. , m_unsigned_data(abs(static_cast<i32>(value)))
  19. {
  20. }
  21. SignedBigInteger(UnsignedBigInteger&& unsigned_data, bool sign)
  22. : m_sign(sign)
  23. , m_unsigned_data(move(unsigned_data))
  24. {
  25. ensure_sign_is_valid();
  26. }
  27. explicit SignedBigInteger(UnsignedBigInteger unsigned_data)
  28. : m_sign(false)
  29. , m_unsigned_data(move(unsigned_data))
  30. {
  31. }
  32. SignedBigInteger()
  33. : m_sign(false)
  34. , m_unsigned_data()
  35. {
  36. }
  37. explicit SignedBigInteger(double value);
  38. explicit SignedBigInteger(i64 value)
  39. : m_sign(value < 0)
  40. , m_unsigned_data(value < 0 ? static_cast<u64>(-(value + 1)) + 1 : static_cast<u64>(value))
  41. {
  42. }
  43. [[nodiscard]] static SignedBigInteger create_invalid()
  44. {
  45. return { UnsignedBigInteger::create_invalid(), false };
  46. }
  47. [[nodiscard]] static SignedBigInteger import_data(StringView data) { return import_data((u8 const*)data.characters_without_null_termination(), data.length()); }
  48. [[nodiscard]] static SignedBigInteger import_data(u8 const* ptr, size_t length);
  49. size_t export_data(Bytes, bool remove_leading_zeros = false) const;
  50. [[nodiscard]] static SignedBigInteger from_base(u16 N, StringView str);
  51. [[nodiscard]] String to_base(u16 N) const;
  52. [[nodiscard]] u64 to_u64() const;
  53. [[nodiscard]] double to_double(UnsignedBigInteger::RoundingMode rounding_mode = UnsignedBigInteger::RoundingMode::IEEERoundAndTiesToEvenMantissa) const;
  54. [[nodiscard]] UnsignedBigInteger const& unsigned_value() const { return m_unsigned_data; }
  55. [[nodiscard]] Vector<u32, STARTING_WORD_SIZE> const words() const { return m_unsigned_data.words(); }
  56. [[nodiscard]] bool is_positive() const { return !is_negative() && !is_zero(); }
  57. [[nodiscard]] bool is_negative() const { return m_sign; }
  58. [[nodiscard]] bool is_zero() const { return m_unsigned_data.is_zero(); }
  59. void negate()
  60. {
  61. if (!m_unsigned_data.is_zero())
  62. m_sign = !m_sign;
  63. }
  64. void set_to_0()
  65. {
  66. m_unsigned_data.set_to_0();
  67. m_sign = false;
  68. }
  69. void set_to(i32 other)
  70. {
  71. m_unsigned_data.set_to((u32)other);
  72. m_sign = other < 0;
  73. }
  74. void set_to(SignedBigInteger const& other)
  75. {
  76. m_unsigned_data.set_to(other.m_unsigned_data);
  77. m_sign = other.m_sign;
  78. }
  79. void invalidate()
  80. {
  81. m_unsigned_data.invalidate();
  82. }
  83. [[nodiscard]] bool is_invalid() const { return m_unsigned_data.is_invalid(); }
  84. // These get + 1 byte for the sign.
  85. [[nodiscard]] size_t length() const { return m_unsigned_data.length() + 1; }
  86. [[nodiscard]] size_t trimmed_length() const { return m_unsigned_data.trimmed_length() + 1; };
  87. [[nodiscard]] SignedBigInteger plus(SignedBigInteger const& other) const;
  88. [[nodiscard]] SignedBigInteger minus(SignedBigInteger const& other) const;
  89. [[nodiscard]] SignedBigInteger bitwise_or(SignedBigInteger const& other) const;
  90. [[nodiscard]] SignedBigInteger bitwise_and(SignedBigInteger const& other) const;
  91. [[nodiscard]] SignedBigInteger bitwise_xor(SignedBigInteger const& other) const;
  92. [[nodiscard]] SignedBigInteger bitwise_not() const;
  93. [[nodiscard]] SignedBigInteger shift_left(size_t num_bits) const;
  94. [[nodiscard]] SignedBigInteger multiplied_by(SignedBigInteger const& other) const;
  95. [[nodiscard]] SignedDivisionResult divided_by(SignedBigInteger const& divisor) const;
  96. [[nodiscard]] SignedBigInteger plus(UnsignedBigInteger const& other) const;
  97. [[nodiscard]] SignedBigInteger minus(UnsignedBigInteger const& other) const;
  98. [[nodiscard]] SignedBigInteger multiplied_by(UnsignedBigInteger const& other) const;
  99. [[nodiscard]] SignedDivisionResult divided_by(UnsignedBigInteger const& divisor) const;
  100. [[nodiscard]] SignedBigInteger negated_value() const;
  101. [[nodiscard]] u32 hash() const;
  102. void set_bit_inplace(size_t bit_index);
  103. [[nodiscard]] bool operator==(SignedBigInteger const& other) const;
  104. [[nodiscard]] bool operator!=(SignedBigInteger const& other) const;
  105. [[nodiscard]] bool operator<(SignedBigInteger const& other) const;
  106. [[nodiscard]] bool operator<=(SignedBigInteger const& other) const;
  107. [[nodiscard]] bool operator>(SignedBigInteger const& other) const;
  108. [[nodiscard]] bool operator>=(SignedBigInteger const& other) const;
  109. [[nodiscard]] bool operator==(UnsignedBigInteger const& other) const;
  110. [[nodiscard]] bool operator!=(UnsignedBigInteger const& other) const;
  111. [[nodiscard]] bool operator<(UnsignedBigInteger const& other) const;
  112. [[nodiscard]] bool operator>(UnsignedBigInteger const& other) const;
  113. enum class CompareResult {
  114. DoubleEqualsBigInt,
  115. DoubleLessThanBigInt,
  116. DoubleGreaterThanBigInt
  117. };
  118. [[nodiscard]] CompareResult compare_to_double(double) const;
  119. private:
  120. void ensure_sign_is_valid()
  121. {
  122. if (m_sign && m_unsigned_data.is_zero())
  123. m_sign = false;
  124. }
  125. bool m_sign { false };
  126. UnsignedBigInteger m_unsigned_data;
  127. };
  128. struct SignedDivisionResult {
  129. Crypto::SignedBigInteger quotient;
  130. Crypto::SignedBigInteger remainder;
  131. };
  132. }
  133. template<>
  134. struct AK::Formatter<Crypto::SignedBigInteger> : AK::Formatter<Crypto::UnsignedBigInteger> {
  135. ErrorOr<void> format(FormatBuilder&, Crypto::SignedBigInteger const&);
  136. };
  137. inline Crypto::SignedBigInteger
  138. operator""_sbigint(char const* string, size_t length)
  139. {
  140. return Crypto::SignedBigInteger::from_base(10, { string, length });
  141. }