SignedBigInteger.h 5.9 KB

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