SignedBigInteger.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Span.h>
  8. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  9. namespace Crypto {
  10. struct SignedDivisionResult;
  11. class SignedBigInteger {
  12. public:
  13. SignedBigInteger(i32 x)
  14. : m_sign(x < 0)
  15. , m_unsigned_data(abs(x))
  16. {
  17. }
  18. SignedBigInteger(UnsignedBigInteger&& unsigned_data, bool sign)
  19. : m_sign(sign)
  20. , m_unsigned_data(move(unsigned_data))
  21. {
  22. }
  23. explicit SignedBigInteger(UnsignedBigInteger unsigned_data)
  24. : m_sign(false)
  25. , m_unsigned_data(move(unsigned_data))
  26. {
  27. }
  28. SignedBigInteger()
  29. : m_sign(false)
  30. , m_unsigned_data()
  31. {
  32. }
  33. static SignedBigInteger create_invalid()
  34. {
  35. return { UnsignedBigInteger::create_invalid(), false };
  36. }
  37. static SignedBigInteger import_data(const StringView& data) { return import_data((const u8*)data.characters_without_null_termination(), data.length()); }
  38. static SignedBigInteger import_data(const u8* ptr, size_t length);
  39. static SignedBigInteger create_from(i64 value)
  40. {
  41. auto sign = false;
  42. u64 unsigned_value;
  43. if (value < 0) {
  44. unsigned_value = static_cast<u64>(-(value + 1)) + 1;
  45. sign = true;
  46. } else {
  47. unsigned_value = value;
  48. }
  49. return SignedBigInteger { UnsignedBigInteger::create_from(unsigned_value), sign };
  50. }
  51. size_t export_data(Bytes, bool remove_leading_zeros = false) const;
  52. static SignedBigInteger from_base(u16 N, StringView str);
  53. String to_base(u16 N) const;
  54. u64 to_u64() const;
  55. const UnsignedBigInteger& unsigned_value() const { return m_unsigned_data; }
  56. const Vector<u32, STARTING_WORD_SIZE> words() const { return m_unsigned_data.words(); }
  57. bool is_negative() const { return m_sign; }
  58. void negate() { m_sign = !m_sign; }
  59. void set_to_0() { m_unsigned_data.set_to_0(); }
  60. void set_to(i32 other)
  61. {
  62. m_unsigned_data.set_to((u32)other);
  63. m_sign = other < 0;
  64. }
  65. void set_to(const SignedBigInteger& other)
  66. {
  67. m_unsigned_data.set_to(other.m_unsigned_data);
  68. m_sign = other.m_sign;
  69. }
  70. void invalidate()
  71. {
  72. m_unsigned_data.invalidate();
  73. }
  74. bool is_invalid() const { return m_unsigned_data.is_invalid(); }
  75. // These get + 1 byte for the sign.
  76. size_t length() const { return m_unsigned_data.length() + 1; }
  77. size_t trimmed_length() const { return m_unsigned_data.trimmed_length() + 1; };
  78. SignedBigInteger plus(const SignedBigInteger& other) const;
  79. SignedBigInteger minus(const SignedBigInteger& other) const;
  80. SignedBigInteger bitwise_or(const SignedBigInteger& other) const;
  81. SignedBigInteger bitwise_and(const SignedBigInteger& other) const;
  82. SignedBigInteger bitwise_xor(const SignedBigInteger& other) const;
  83. SignedBigInteger bitwise_not() const;
  84. SignedBigInteger shift_left(size_t num_bits) const;
  85. SignedBigInteger multiplied_by(const SignedBigInteger& other) const;
  86. SignedDivisionResult divided_by(const SignedBigInteger& divisor) const;
  87. SignedBigInteger plus(const UnsignedBigInteger& other) const;
  88. SignedBigInteger minus(const UnsignedBigInteger& other) const;
  89. SignedBigInteger bitwise_or(const UnsignedBigInteger& other) const;
  90. SignedBigInteger bitwise_and(const UnsignedBigInteger& other) const;
  91. SignedBigInteger bitwise_xor(const UnsignedBigInteger& other) const;
  92. SignedBigInteger multiplied_by(const UnsignedBigInteger& other) const;
  93. SignedDivisionResult divided_by(const UnsignedBigInteger& divisor) const;
  94. u32 hash() const;
  95. void set_bit_inplace(size_t bit_index);
  96. bool operator==(const SignedBigInteger& other) const;
  97. bool operator!=(const SignedBigInteger& other) const;
  98. bool operator<(const SignedBigInteger& other) const;
  99. bool operator>(const SignedBigInteger& other) const;
  100. bool operator==(const UnsignedBigInteger& other) const;
  101. bool operator!=(const UnsignedBigInteger& other) const;
  102. bool operator<(const UnsignedBigInteger& other) const;
  103. bool operator>(const UnsignedBigInteger& other) const;
  104. private:
  105. bool m_sign { false };
  106. UnsignedBigInteger m_unsigned_data;
  107. };
  108. struct SignedDivisionResult {
  109. Crypto::SignedBigInteger quotient;
  110. Crypto::SignedBigInteger remainder;
  111. };
  112. }
  113. inline Crypto::SignedBigInteger
  114. operator""_sbigint(const char* string, size_t length)
  115. {
  116. return Crypto::SignedBigInteger::from_base(10, { string, length });
  117. }