BitwiseOperations.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2020-2021, Dex♪ <dexes.ttp@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "UnsignedBigIntegerAlgorithms.h"
  8. #include <AK/BuiltinWrappers.h>
  9. namespace Crypto {
  10. /**
  11. * Complexity: O(N) where N is the number of words in the shorter value
  12. * Method:
  13. * Apply <op> word-wise until words in the shorter value are used up
  14. * then copy the rest of the words verbatim from the longer value.
  15. */
  16. FLATTEN void UnsignedBigIntegerAlgorithms::bitwise_or_without_allocation(
  17. UnsignedBigInteger const& left,
  18. UnsignedBigInteger const& right,
  19. UnsignedBigInteger& output)
  20. {
  21. // If either of the BigInts are invalid, the output is just the other one.
  22. if (left.is_invalid()) {
  23. output.set_to(right);
  24. return;
  25. }
  26. if (right.is_invalid()) {
  27. output.set_to(left);
  28. return;
  29. }
  30. const UnsignedBigInteger *shorter, *longer;
  31. if (left.length() < right.length()) {
  32. shorter = &left;
  33. longer = &right;
  34. } else {
  35. shorter = &right;
  36. longer = &left;
  37. }
  38. output.m_words.resize_and_keep_capacity(longer->length());
  39. size_t longer_offset = longer->length() - shorter->length();
  40. for (size_t i = 0; i < shorter->length(); ++i)
  41. output.m_words[i] = longer->words()[i] | shorter->words()[i];
  42. __builtin_memcpy(output.m_words.data() + shorter->length(), longer->words().data() + shorter->length(), sizeof(u32) * longer_offset);
  43. }
  44. /**
  45. * Complexity: O(N) where N is the number of words in the shorter value
  46. * Method:
  47. * Apply 'and' word-wise until words in the shorter value are used up
  48. * and zero the rest.
  49. */
  50. FLATTEN void UnsignedBigIntegerAlgorithms::bitwise_and_without_allocation(
  51. UnsignedBigInteger const& left,
  52. UnsignedBigInteger const& right,
  53. UnsignedBigInteger& output)
  54. {
  55. // If either of the BigInts are invalid, the output is just the other one.
  56. if (left.is_invalid()) {
  57. output.set_to(right);
  58. return;
  59. }
  60. if (right.is_invalid()) {
  61. output.set_to(left);
  62. return;
  63. }
  64. const UnsignedBigInteger *shorter, *longer;
  65. if (left.length() < right.length()) {
  66. shorter = &left;
  67. longer = &right;
  68. } else {
  69. shorter = &right;
  70. longer = &left;
  71. }
  72. output.m_words.resize_and_keep_capacity(longer->length());
  73. size_t longer_offset = longer->length() - shorter->length();
  74. for (size_t i = 0; i < shorter->length(); ++i)
  75. output.m_words[i] = longer->words()[i] & shorter->words()[i];
  76. __builtin_memset(output.m_words.data() + shorter->length(), 0, sizeof(u32) * longer_offset);
  77. }
  78. /**
  79. * Complexity: O(N) where N is the number of words in the shorter value
  80. * Method:
  81. * Apply 'xor' word-wise until words in the shorter value are used up
  82. * and copy the rest.
  83. */
  84. FLATTEN void UnsignedBigIntegerAlgorithms::bitwise_xor_without_allocation(
  85. UnsignedBigInteger const& left,
  86. UnsignedBigInteger const& right,
  87. UnsignedBigInteger& output)
  88. {
  89. // If either of the BigInts are invalid, the output is just the other one.
  90. if (left.is_invalid()) {
  91. output.set_to(right);
  92. return;
  93. }
  94. if (right.is_invalid()) {
  95. output.set_to(left);
  96. return;
  97. }
  98. const UnsignedBigInteger *shorter, *longer;
  99. if (left.length() < right.length()) {
  100. shorter = &left;
  101. longer = &right;
  102. } else {
  103. shorter = &right;
  104. longer = &left;
  105. }
  106. output.m_words.resize_and_keep_capacity(longer->length());
  107. size_t longer_offset = longer->length() - shorter->length();
  108. for (size_t i = 0; i < shorter->length(); ++i)
  109. output.m_words[i] = longer->words()[i] ^ shorter->words()[i];
  110. __builtin_memcpy(output.m_words.data() + shorter->length(), longer->words().data() + shorter->length(), sizeof(u32) * longer_offset);
  111. }
  112. /**
  113. * Complexity: O(N) where N is the number of words
  114. */
  115. FLATTEN void UnsignedBigIntegerAlgorithms::bitwise_not_without_allocation(
  116. UnsignedBigInteger const& right,
  117. UnsignedBigInteger& output)
  118. {
  119. // If the value is invalid, the output value is invalid as well.
  120. if (right.is_invalid()) {
  121. output.invalidate();
  122. return;
  123. }
  124. if (right.length() == 0) {
  125. output.set_to_0();
  126. return;
  127. }
  128. output.m_words.resize_and_keep_capacity(right.length());
  129. if (right.length() > 1) {
  130. for (size_t i = 0; i < right.length() - 1; ++i)
  131. output.m_words[i] = ~right.words()[i];
  132. }
  133. auto last_word_index = right.length() - 1;
  134. auto last_word = right.words()[last_word_index];
  135. output.m_words[last_word_index] = ((u32)0xffffffffffffffff >> count_leading_zeroes(last_word)) & ~last_word;
  136. }
  137. /**
  138. * Complexity : O(N + num_bits % 8) where N is the number of words in the number
  139. * Shift method :
  140. * Start by shifting by whole words in num_bits (by putting missing words at the start),
  141. * then shift the number's words two by two by the remaining amount of bits.
  142. */
  143. FLATTEN void UnsignedBigIntegerAlgorithms::shift_left_without_allocation(
  144. UnsignedBigInteger const& number,
  145. size_t num_bits,
  146. UnsignedBigInteger& temp_result,
  147. UnsignedBigInteger& temp_plus,
  148. UnsignedBigInteger& output)
  149. {
  150. // We can only do shift operations on individual words
  151. // where the shift amount is <= size of word (32).
  152. // But we do know how to shift by a multiple of word size (e.g 64=32*2)
  153. // So we first shift the result by how many whole words fit in 'num_bits'
  154. shift_left_by_n_words(number, num_bits / UnsignedBigInteger::BITS_IN_WORD, temp_result);
  155. output.set_to(temp_result);
  156. // And now we shift by the leftover amount of bits
  157. num_bits %= UnsignedBigInteger::BITS_IN_WORD;
  158. if (num_bits == 0) {
  159. return;
  160. }
  161. for (size_t i = 0; i < temp_result.length(); ++i) {
  162. u32 current_word_of_temp_result = shift_left_get_one_word(temp_result, num_bits, i);
  163. output.m_words[i] = current_word_of_temp_result;
  164. }
  165. // Shifting the last word can produce a carry
  166. u32 carry_word = shift_left_get_one_word(temp_result, num_bits, temp_result.length());
  167. if (carry_word != 0) {
  168. // output += (carry_word << temp_result.length())
  169. // FIXME : Using temp_plus this way to transform carry_word into a bigint is not
  170. // efficient nor pretty. Maybe we should have an "add_with_shift" method ?
  171. temp_plus.set_to_0();
  172. temp_plus.m_words.append(carry_word);
  173. shift_left_by_n_words(temp_plus, temp_result.length(), temp_result);
  174. add_into_accumulator_without_allocation(output, temp_result);
  175. }
  176. }
  177. void UnsignedBigIntegerAlgorithms::shift_left_by_n_words(
  178. UnsignedBigInteger const& number,
  179. size_t number_of_words,
  180. UnsignedBigInteger& output)
  181. {
  182. // shifting left by N words means just inserting N zeroes to the beginning of the words vector
  183. output.set_to_0();
  184. output.m_words.resize_and_keep_capacity(number_of_words + number.length());
  185. __builtin_memset(output.m_words.data(), 0, number_of_words * sizeof(unsigned));
  186. __builtin_memcpy(&output.m_words.data()[number_of_words], number.m_words.data(), number.m_words.size() * sizeof(unsigned));
  187. }
  188. void UnsignedBigIntegerAlgorithms::shift_right_by_n_words(
  189. UnsignedBigInteger const& number,
  190. size_t number_of_words,
  191. UnsignedBigInteger& output)
  192. {
  193. // shifting right by N words means just not copying the first words
  194. output.set_to_0();
  195. output.m_words.resize_and_keep_capacity(number.length() - number_of_words);
  196. __builtin_memcpy(output.m_words.data(), &number.m_words.data()[number_of_words], (number.m_words.size() - number_of_words) * sizeof(unsigned));
  197. }
  198. /**
  199. * Returns the word at a requested index in the result of a shift operation
  200. */
  201. ALWAYS_INLINE UnsignedBigInteger::Word UnsignedBigIntegerAlgorithms::shift_left_get_one_word(
  202. UnsignedBigInteger const& number,
  203. size_t num_bits,
  204. size_t result_word_index)
  205. {
  206. // "<= length()" (rather than length() - 1) is intentional,
  207. // The result inedx of length() is used when calculating the carry word
  208. VERIFY(result_word_index <= number.length());
  209. VERIFY(num_bits <= UnsignedBigInteger::BITS_IN_WORD);
  210. u32 result = 0;
  211. // we need to check for "num_bits != 0" since shifting right by 32 is apparently undefined behavior!
  212. if (result_word_index > 0 && num_bits != 0) {
  213. result += number.m_words[result_word_index - 1] >> (UnsignedBigInteger::BITS_IN_WORD - num_bits);
  214. }
  215. if (result_word_index < number.length() && num_bits < 32) {
  216. result += number.m_words[result_word_index] << num_bits;
  217. }
  218. return result;
  219. }
  220. }