SignedBigInteger.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SignedBigInteger.h"
  7. #include <AK/StringBuilder.h>
  8. namespace Crypto {
  9. SignedBigInteger SignedBigInteger::import_data(const u8* ptr, size_t length)
  10. {
  11. bool sign = *ptr;
  12. auto unsigned_data = UnsignedBigInteger::import_data(ptr + 1, length - 1);
  13. return { move(unsigned_data), sign };
  14. }
  15. size_t SignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) const
  16. {
  17. // FIXME: Support this:
  18. // m <0XX> -> m <XX> (if remove_leading_zeros)
  19. VERIFY(!remove_leading_zeros);
  20. data[0] = m_sign;
  21. auto bytes_view = data.slice(1, data.size() - 1);
  22. return m_unsigned_data.export_data(bytes_view, remove_leading_zeros) + 1;
  23. }
  24. SignedBigInteger SignedBigInteger::from_base10(StringView str)
  25. {
  26. bool sign = false;
  27. if (str.length() > 1) {
  28. auto maybe_sign = str[0];
  29. if (maybe_sign == '-') {
  30. str = str.substring_view(1, str.length() - 1);
  31. sign = true;
  32. }
  33. if (maybe_sign == '+')
  34. str = str.substring_view(1, str.length() - 1);
  35. }
  36. auto unsigned_data = UnsignedBigInteger::from_base10(str);
  37. return { move(unsigned_data), sign };
  38. }
  39. String SignedBigInteger::to_base10() const
  40. {
  41. StringBuilder builder;
  42. if (m_sign)
  43. builder.append('-');
  44. builder.append(m_unsigned_data.to_base10());
  45. return builder.to_string();
  46. }
  47. FLATTEN SignedBigInteger SignedBigInteger::plus(const SignedBigInteger& other) const
  48. {
  49. // If both are of the same sign, just add the unsigned data and return.
  50. if (m_sign == other.m_sign)
  51. return { other.m_unsigned_data.plus(m_unsigned_data), m_sign };
  52. // One value is signed while the other is not.
  53. return m_sign ? other.minus(this->m_unsigned_data) : minus(other.m_unsigned_data);
  54. }
  55. FLATTEN SignedBigInteger SignedBigInteger::minus(const SignedBigInteger& other) const
  56. {
  57. // If the signs are different, convert the op to an addition.
  58. if (m_sign != other.m_sign) {
  59. // -x - y = - (x + y)
  60. // x - -y = (x + y)
  61. SignedBigInteger result { other.m_unsigned_data.plus(this->m_unsigned_data) };
  62. if (m_sign)
  63. result.negate();
  64. return result;
  65. }
  66. if (!m_sign) {
  67. // Both operands are positive.
  68. // x - y = - (y - x)
  69. if (m_unsigned_data < other.m_unsigned_data) {
  70. // The result will be negative.
  71. return { other.m_unsigned_data.minus(m_unsigned_data), true };
  72. }
  73. // The result will be either zero, or positive.
  74. return SignedBigInteger { m_unsigned_data.minus(other.m_unsigned_data) };
  75. }
  76. // Both operands are negative.
  77. // -x - -y = y - x
  78. if (m_unsigned_data < other.m_unsigned_data) {
  79. // The result will be positive.
  80. return SignedBigInteger { other.m_unsigned_data.minus(m_unsigned_data), true };
  81. }
  82. // The result will be either zero, or negative.
  83. // y - x = - (x - y)
  84. return SignedBigInteger { m_unsigned_data.minus(other.m_unsigned_data) };
  85. }
  86. FLATTEN SignedBigInteger SignedBigInteger::plus(const UnsignedBigInteger& other) const
  87. {
  88. if (m_sign) {
  89. if (other < m_unsigned_data)
  90. return { m_unsigned_data.minus(other), true };
  91. return { other.minus(m_unsigned_data), false };
  92. }
  93. return { m_unsigned_data.plus(other), false };
  94. }
  95. FLATTEN SignedBigInteger SignedBigInteger::minus(const UnsignedBigInteger& other) const
  96. {
  97. if (m_sign)
  98. return { m_unsigned_data.plus(m_unsigned_data), true };
  99. if (other < m_unsigned_data)
  100. return { m_unsigned_data.minus(other), false };
  101. return { other.minus(m_unsigned_data), true };
  102. }
  103. FLATTEN SignedBigInteger SignedBigInteger::bitwise_or(const UnsignedBigInteger& other) const
  104. {
  105. return { unsigned_value().bitwise_or(other), m_sign };
  106. }
  107. FLATTEN SignedBigInteger SignedBigInteger::bitwise_and(const UnsignedBigInteger& other) const
  108. {
  109. return { unsigned_value().bitwise_and(other), false };
  110. }
  111. FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const UnsignedBigInteger& other) const
  112. {
  113. return { unsigned_value().bitwise_xor(other), m_sign };
  114. }
  115. FLATTEN SignedBigInteger SignedBigInteger::bitwise_not() const
  116. {
  117. return { unsigned_value().bitwise_not(), !m_sign };
  118. }
  119. FLATTEN SignedBigInteger SignedBigInteger::bitwise_or(const SignedBigInteger& other) const
  120. {
  121. auto result = bitwise_or(other.unsigned_value());
  122. // The sign bit will have to be OR'd manually.
  123. if (other.is_negative())
  124. result.negate();
  125. return result;
  126. }
  127. FLATTEN SignedBigInteger SignedBigInteger::bitwise_and(const SignedBigInteger& other) const
  128. {
  129. auto result = bitwise_and(other.unsigned_value());
  130. // The sign bit will have to be AND'd manually.
  131. result.m_sign = is_negative() || other.is_negative();
  132. return result;
  133. }
  134. FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const SignedBigInteger& other) const
  135. {
  136. auto result = bitwise_xor(other.unsigned_value());
  137. // The sign bit will have to be XOR'd manually.
  138. result.m_sign = is_negative() ^ other.is_negative();
  139. return result;
  140. }
  141. bool SignedBigInteger::operator==(const UnsignedBigInteger& other) const
  142. {
  143. if (m_sign)
  144. return false;
  145. return m_unsigned_data == other;
  146. }
  147. bool SignedBigInteger::operator!=(const UnsignedBigInteger& other) const
  148. {
  149. if (m_sign)
  150. return true;
  151. return m_unsigned_data != other;
  152. }
  153. bool SignedBigInteger::operator<(const UnsignedBigInteger& other) const
  154. {
  155. if (m_sign)
  156. return true;
  157. return m_unsigned_data < other;
  158. }
  159. FLATTEN SignedBigInteger SignedBigInteger::shift_left(size_t num_bits) const
  160. {
  161. return SignedBigInteger { m_unsigned_data.shift_left(num_bits), m_sign };
  162. }
  163. FLATTEN SignedBigInteger SignedBigInteger::multiplied_by(const SignedBigInteger& other) const
  164. {
  165. bool result_sign = m_sign ^ other.m_sign;
  166. return { m_unsigned_data.multiplied_by(other.m_unsigned_data), result_sign };
  167. }
  168. FLATTEN SignedDivisionResult SignedBigInteger::divided_by(const SignedBigInteger& divisor) const
  169. {
  170. // Aa / Bb -> (A^B)q, Ar
  171. bool result_sign = m_sign ^ divisor.m_sign;
  172. auto unsigned_division_result = m_unsigned_data.divided_by(divisor.m_unsigned_data);
  173. return {
  174. { move(unsigned_division_result.quotient), result_sign },
  175. { move(unsigned_division_result.remainder), m_sign }
  176. };
  177. }
  178. u32 SignedBigInteger::hash() const
  179. {
  180. return m_unsigned_data.hash() * (1 - (2 * m_sign));
  181. }
  182. void SignedBigInteger::set_bit_inplace(size_t bit_index)
  183. {
  184. m_unsigned_data.set_bit_inplace(bit_index);
  185. }
  186. bool SignedBigInteger::operator==(const SignedBigInteger& other) const
  187. {
  188. if (is_invalid() != other.is_invalid())
  189. return false;
  190. if (m_unsigned_data == 0 && other.m_unsigned_data == 0)
  191. return true;
  192. return m_sign == other.m_sign && m_unsigned_data == other.m_unsigned_data;
  193. }
  194. bool SignedBigInteger::operator!=(const SignedBigInteger& other) const
  195. {
  196. return !(*this == other);
  197. }
  198. bool SignedBigInteger::operator<(const SignedBigInteger& other) const
  199. {
  200. if (m_sign ^ other.m_sign)
  201. return m_sign;
  202. if (m_sign)
  203. return other.m_unsigned_data < m_unsigned_data;
  204. return m_unsigned_data < other.m_unsigned_data;
  205. }
  206. }