SignedBigInteger.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. [[nodiscard]] static SignedBigInteger create_invalid()
  39. {
  40. return { UnsignedBigInteger::create_invalid(), false };
  41. }
  42. [[nodiscard]] static SignedBigInteger import_data(StringView data) { return import_data((u8 const*)data.characters_without_null_termination(), data.length()); }
  43. [[nodiscard]] static SignedBigInteger import_data(u8 const* ptr, size_t length);
  44. [[nodiscard]] static SignedBigInteger create_from(i64 value)
  45. {
  46. auto sign = false;
  47. u64 unsigned_value;
  48. if (value < 0) {
  49. unsigned_value = static_cast<u64>(-(value + 1)) + 1;
  50. sign = true;
  51. } else {
  52. unsigned_value = value;
  53. }
  54. return SignedBigInteger { UnsignedBigInteger::create_from(unsigned_value), sign };
  55. }
  56. size_t export_data(Bytes, bool remove_leading_zeros = false) const;
  57. [[nodiscard]] static SignedBigInteger from_base(u16 N, StringView str);
  58. [[nodiscard]] String to_base(u16 N) const;
  59. [[nodiscard]] u64 to_u64() const;
  60. [[nodiscard]] double to_double(UnsignedBigInteger::RoundingMode rounding_mode = UnsignedBigInteger::RoundingMode::IEEERoundAndTiesToEvenMantissa) const;
  61. [[nodiscard]] UnsignedBigInteger const& unsigned_value() const { return m_unsigned_data; }
  62. [[nodiscard]] Vector<u32, STARTING_WORD_SIZE> const words() const { return m_unsigned_data.words(); }
  63. [[nodiscard]] bool is_negative() const { return m_sign; }
  64. [[nodiscard]] bool is_zero() const { return m_unsigned_data.is_zero(); }
  65. void negate()
  66. {
  67. if (!m_unsigned_data.is_zero())
  68. m_sign = !m_sign;
  69. }
  70. void set_to_0()
  71. {
  72. m_unsigned_data.set_to_0();
  73. m_sign = false;
  74. }
  75. void set_to(i32 other)
  76. {
  77. m_unsigned_data.set_to((u32)other);
  78. m_sign = other < 0;
  79. }
  80. void set_to(SignedBigInteger const& other)
  81. {
  82. m_unsigned_data.set_to(other.m_unsigned_data);
  83. m_sign = other.m_sign;
  84. }
  85. void invalidate()
  86. {
  87. m_unsigned_data.invalidate();
  88. }
  89. [[nodiscard]] bool is_invalid() const { return m_unsigned_data.is_invalid(); }
  90. // These get + 1 byte for the sign.
  91. [[nodiscard]] size_t length() const { return m_unsigned_data.length() + 1; }
  92. [[nodiscard]] size_t trimmed_length() const { return m_unsigned_data.trimmed_length() + 1; };
  93. [[nodiscard]] SignedBigInteger plus(SignedBigInteger const& other) const;
  94. [[nodiscard]] SignedBigInteger minus(SignedBigInteger const& other) const;
  95. [[nodiscard]] SignedBigInteger bitwise_or(SignedBigInteger const& other) const;
  96. [[nodiscard]] SignedBigInteger bitwise_and(SignedBigInteger const& other) const;
  97. [[nodiscard]] SignedBigInteger bitwise_xor(SignedBigInteger const& other) const;
  98. [[nodiscard]] SignedBigInteger bitwise_not() const;
  99. [[nodiscard]] SignedBigInteger shift_left(size_t num_bits) const;
  100. [[nodiscard]] SignedBigInteger multiplied_by(SignedBigInteger const& other) const;
  101. [[nodiscard]] SignedDivisionResult divided_by(SignedBigInteger const& divisor) const;
  102. [[nodiscard]] SignedBigInteger plus(UnsignedBigInteger const& other) const;
  103. [[nodiscard]] SignedBigInteger minus(UnsignedBigInteger const& other) const;
  104. [[nodiscard]] SignedBigInteger multiplied_by(UnsignedBigInteger const& other) const;
  105. [[nodiscard]] SignedDivisionResult divided_by(UnsignedBigInteger const& divisor) const;
  106. [[nodiscard]] u32 hash() const;
  107. void set_bit_inplace(size_t bit_index);
  108. [[nodiscard]] bool operator==(SignedBigInteger const& other) const;
  109. [[nodiscard]] bool operator!=(SignedBigInteger const& other) const;
  110. [[nodiscard]] bool operator<(SignedBigInteger const& other) const;
  111. [[nodiscard]] bool operator<=(SignedBigInteger const& other) const;
  112. [[nodiscard]] bool operator>(SignedBigInteger const& other) const;
  113. [[nodiscard]] bool operator>=(SignedBigInteger const& other) const;
  114. [[nodiscard]] bool operator==(UnsignedBigInteger const& other) const;
  115. [[nodiscard]] bool operator!=(UnsignedBigInteger const& other) const;
  116. [[nodiscard]] bool operator<(UnsignedBigInteger const& other) const;
  117. [[nodiscard]] bool operator>(UnsignedBigInteger const& other) const;
  118. enum class CompareResult {
  119. DoubleEqualsBigInt,
  120. DoubleLessThanBigInt,
  121. DoubleGreaterThanBigInt
  122. };
  123. [[nodiscard]] CompareResult compare_to_double(double) const;
  124. private:
  125. void ensure_sign_is_valid()
  126. {
  127. if (m_sign && m_unsigned_data.is_zero())
  128. m_sign = false;
  129. }
  130. bool m_sign { false };
  131. UnsignedBigInteger m_unsigned_data;
  132. };
  133. struct SignedDivisionResult {
  134. Crypto::SignedBigInteger quotient;
  135. Crypto::SignedBigInteger remainder;
  136. };
  137. }
  138. template<>
  139. struct AK::Formatter<Crypto::SignedBigInteger> : AK::Formatter<Crypto::UnsignedBigInteger> {
  140. ErrorOr<void> format(FormatBuilder&, Crypto::SignedBigInteger const&);
  141. };
  142. inline Crypto::SignedBigInteger
  143. operator""_sbigint(char const* string, size_t length)
  144. {
  145. return Crypto::SignedBigInteger::from_base(10, { string, length });
  146. }