SignedBigInteger.h 5.8 KB

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