SignedBigInteger.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_base(u16 N, StringView str)
  25. {
  26. auto sign = false;
  27. if (str.length() > 1) {
  28. auto maybe_sign = str[0];
  29. if (maybe_sign == '-') {
  30. str = str.substring_view(1);
  31. sign = true;
  32. }
  33. if (maybe_sign == '+')
  34. str = str.substring_view(1);
  35. }
  36. auto unsigned_data = UnsignedBigInteger::from_base(N, str);
  37. return { move(unsigned_data), sign };
  38. }
  39. String SignedBigInteger::to_base(u16 N) const
  40. {
  41. StringBuilder builder;
  42. if (m_sign)
  43. builder.append('-');
  44. builder.append(m_unsigned_data.to_base(N));
  45. return builder.to_string();
  46. }
  47. u64 SignedBigInteger::to_u64() const
  48. {
  49. u64 unsigned_value = m_unsigned_data.to_u64();
  50. if (!m_sign)
  51. return unsigned_value;
  52. return ~(unsigned_value - 1); // equivalent to `-unsigned_value`, but doesn't trigger UBSAN
  53. }
  54. double SignedBigInteger::to_double() const
  55. {
  56. double unsigned_value = m_unsigned_data.to_double();
  57. if (!m_sign)
  58. return unsigned_value;
  59. return -unsigned_value;
  60. }
  61. FLATTEN SignedBigInteger SignedBigInteger::plus(const SignedBigInteger& other) const
  62. {
  63. // If both are of the same sign, just add the unsigned data and return.
  64. if (m_sign == other.m_sign)
  65. return { other.m_unsigned_data.plus(m_unsigned_data), m_sign };
  66. // One value is signed while the other is not.
  67. return m_sign ? other.minus(this->m_unsigned_data) : minus(other.m_unsigned_data);
  68. }
  69. FLATTEN SignedBigInteger SignedBigInteger::minus(const SignedBigInteger& other) const
  70. {
  71. // If the signs are different, convert the op to an addition.
  72. if (m_sign != other.m_sign) {
  73. // -x - y = - (x + y)
  74. // x - -y = (x + y)
  75. SignedBigInteger result { other.m_unsigned_data.plus(this->m_unsigned_data) };
  76. if (m_sign)
  77. result.negate();
  78. return result;
  79. }
  80. if (!m_sign) {
  81. // Both operands are positive.
  82. // x - y = - (y - x)
  83. if (m_unsigned_data < other.m_unsigned_data) {
  84. // The result will be negative.
  85. return { other.m_unsigned_data.minus(m_unsigned_data), true };
  86. }
  87. // The result will be either zero, or positive.
  88. return SignedBigInteger { m_unsigned_data.minus(other.m_unsigned_data) };
  89. }
  90. // Both operands are negative.
  91. // -x - -y = y - x
  92. if (m_unsigned_data < other.m_unsigned_data) {
  93. // The result will be positive.
  94. return SignedBigInteger { other.m_unsigned_data.minus(m_unsigned_data) };
  95. }
  96. // y - x = - (x - y)
  97. if (m_unsigned_data > other.m_unsigned_data) {
  98. // The result will be negative.
  99. return SignedBigInteger { m_unsigned_data.minus(other.m_unsigned_data), true };
  100. }
  101. // Both operands have the same magnitude, the result is positive zero.
  102. return SignedBigInteger { 0 };
  103. }
  104. FLATTEN SignedBigInteger SignedBigInteger::plus(const UnsignedBigInteger& other) const
  105. {
  106. if (m_sign) {
  107. if (other < m_unsigned_data)
  108. return { m_unsigned_data.minus(other), true };
  109. return { other.minus(m_unsigned_data), false };
  110. }
  111. return { m_unsigned_data.plus(other), false };
  112. }
  113. FLATTEN SignedBigInteger SignedBigInteger::minus(const UnsignedBigInteger& other) const
  114. {
  115. if (m_sign)
  116. return { m_unsigned_data.plus(m_unsigned_data), true };
  117. if (other < m_unsigned_data)
  118. return { m_unsigned_data.minus(other), false };
  119. return { other.minus(m_unsigned_data), true };
  120. }
  121. FLATTEN SignedBigInteger SignedBigInteger::bitwise_or(const UnsignedBigInteger& other) const
  122. {
  123. return { unsigned_value().bitwise_or(other), m_sign };
  124. }
  125. FLATTEN SignedBigInteger SignedBigInteger::bitwise_and(const UnsignedBigInteger& other) const
  126. {
  127. return { unsigned_value().bitwise_and(other), false };
  128. }
  129. FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const UnsignedBigInteger& other) const
  130. {
  131. return { unsigned_value().bitwise_xor(other), m_sign };
  132. }
  133. FLATTEN SignedBigInteger SignedBigInteger::bitwise_not() const
  134. {
  135. return { unsigned_value().bitwise_not(), !m_sign };
  136. }
  137. FLATTEN SignedBigInteger SignedBigInteger::multiplied_by(UnsignedBigInteger const& other) const
  138. {
  139. return { unsigned_value().multiplied_by(other), m_sign };
  140. }
  141. FLATTEN SignedDivisionResult SignedBigInteger::divided_by(UnsignedBigInteger const& divisor) const
  142. {
  143. auto division_result = unsigned_value().divided_by(divisor);
  144. return {
  145. { move(division_result.quotient), m_sign },
  146. { move(division_result.remainder), m_sign },
  147. };
  148. }
  149. FLATTEN SignedBigInteger SignedBigInteger::bitwise_or(const SignedBigInteger& other) const
  150. {
  151. auto result = bitwise_or(other.unsigned_value());
  152. // The sign bit will have to be OR'd manually.
  153. result.m_sign = is_negative() || other.is_negative();
  154. return result;
  155. }
  156. FLATTEN SignedBigInteger SignedBigInteger::bitwise_and(const SignedBigInteger& other) const
  157. {
  158. auto result = bitwise_and(other.unsigned_value());
  159. // The sign bit will have to be AND'd manually.
  160. result.m_sign = is_negative() && other.is_negative();
  161. return result;
  162. }
  163. FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const SignedBigInteger& other) const
  164. {
  165. auto result = bitwise_xor(other.unsigned_value());
  166. // The sign bit will have to be XOR'd manually.
  167. result.m_sign = is_negative() ^ other.is_negative();
  168. return result;
  169. }
  170. bool SignedBigInteger::operator==(const UnsignedBigInteger& other) const
  171. {
  172. if (m_sign)
  173. return false;
  174. return m_unsigned_data == other;
  175. }
  176. bool SignedBigInteger::operator!=(const UnsignedBigInteger& other) const
  177. {
  178. if (m_sign)
  179. return true;
  180. return m_unsigned_data != other;
  181. }
  182. bool SignedBigInteger::operator<(const UnsignedBigInteger& other) const
  183. {
  184. if (m_sign)
  185. return true;
  186. return m_unsigned_data < other;
  187. }
  188. bool SignedBigInteger::operator>(const UnsignedBigInteger& other) const
  189. {
  190. return *this != other && !(*this < other);
  191. }
  192. FLATTEN SignedBigInteger SignedBigInteger::shift_left(size_t num_bits) const
  193. {
  194. return SignedBigInteger { m_unsigned_data.shift_left(num_bits), m_sign };
  195. }
  196. FLATTEN SignedBigInteger SignedBigInteger::multiplied_by(const SignedBigInteger& other) const
  197. {
  198. bool result_sign = m_sign ^ other.m_sign;
  199. return { m_unsigned_data.multiplied_by(other.m_unsigned_data), result_sign };
  200. }
  201. FLATTEN SignedDivisionResult SignedBigInteger::divided_by(const SignedBigInteger& divisor) const
  202. {
  203. // Aa / Bb -> (A^B)q, Ar
  204. bool result_sign = m_sign ^ divisor.m_sign;
  205. auto unsigned_division_result = m_unsigned_data.divided_by(divisor.m_unsigned_data);
  206. return {
  207. { move(unsigned_division_result.quotient), result_sign },
  208. { move(unsigned_division_result.remainder), m_sign }
  209. };
  210. }
  211. u32 SignedBigInteger::hash() const
  212. {
  213. return m_unsigned_data.hash() * (1 - (2 * m_sign));
  214. }
  215. void SignedBigInteger::set_bit_inplace(size_t bit_index)
  216. {
  217. m_unsigned_data.set_bit_inplace(bit_index);
  218. }
  219. bool SignedBigInteger::operator==(const SignedBigInteger& other) const
  220. {
  221. if (is_invalid() != other.is_invalid())
  222. return false;
  223. if (m_unsigned_data == 0 && other.m_unsigned_data == 0)
  224. return true;
  225. return m_sign == other.m_sign && m_unsigned_data == other.m_unsigned_data;
  226. }
  227. bool SignedBigInteger::operator!=(const SignedBigInteger& other) const
  228. {
  229. return !(*this == other);
  230. }
  231. bool SignedBigInteger::operator<(const SignedBigInteger& other) const
  232. {
  233. if (m_sign ^ other.m_sign)
  234. return m_sign;
  235. if (m_sign)
  236. return other.m_unsigned_data < m_unsigned_data;
  237. return m_unsigned_data < other.m_unsigned_data;
  238. }
  239. bool SignedBigInteger::operator<=(const SignedBigInteger& other) const
  240. {
  241. return *this < other || *this == other;
  242. }
  243. bool SignedBigInteger::operator>(const SignedBigInteger& other) const
  244. {
  245. return *this != other && !(*this < other);
  246. }
  247. bool SignedBigInteger::operator>=(const SignedBigInteger& other) const
  248. {
  249. return !(*this < other);
  250. }
  251. }