UnsignedBigInteger.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/LogStream.h>
  29. #include <AK/String.h>
  30. #include <AK/Types.h>
  31. #include <AK/Vector.h>
  32. namespace Crypto {
  33. struct UnsignedDivisionResult;
  34. constexpr size_t STARTING_WORD_SIZE = 512;
  35. class UnsignedBigInteger {
  36. public:
  37. UnsignedBigInteger(u32 x) { m_words.append(x); }
  38. explicit UnsignedBigInteger(AK::Vector<u32, STARTING_WORD_SIZE>&& words)
  39. : m_words(move(words))
  40. {
  41. }
  42. UnsignedBigInteger() {}
  43. static UnsignedBigInteger create_invalid();
  44. static UnsignedBigInteger import_data(const AK::StringView& data) { return import_data((const u8*)data.characters_without_null_termination(), data.length()); }
  45. static UnsignedBigInteger import_data(const u8* ptr, size_t length);
  46. size_t export_data(AK::ByteBuffer& data);
  47. size_t export_data(const u8* ptr, size_t length)
  48. {
  49. auto buffer = ByteBuffer::wrap(ptr, length);
  50. return export_data(buffer);
  51. }
  52. static UnsignedBigInteger from_base10(const String& str);
  53. String to_base10() const;
  54. const AK::Vector<u32, STARTING_WORD_SIZE>& words() const { return m_words; }
  55. void set_to_0();
  56. void set_to(u32 other);
  57. void set_to(const UnsignedBigInteger& other);
  58. void invalidate() { m_is_invalid = true; }
  59. bool is_invalid() const { return m_is_invalid; }
  60. size_t length() const { return m_words.size(); }
  61. // The "trimmed length" is the number of words after trimming leading zeroed words
  62. size_t trimmed_length() const;
  63. UnsignedBigInteger plus(const UnsignedBigInteger& other) const;
  64. UnsignedBigInteger minus(const UnsignedBigInteger& other) const;
  65. UnsignedBigInteger shift_left(size_t num_bits) const;
  66. UnsignedBigInteger multiplied_by(const UnsignedBigInteger& other) const;
  67. UnsignedDivisionResult divided_by(const UnsignedBigInteger& divisor) const;
  68. void set_bit_inplace(size_t bit_index);
  69. static void add_without_allocation(const UnsignedBigInteger& left, const UnsignedBigInteger& right, UnsignedBigInteger& output);
  70. static void subtract_without_allocation(const UnsignedBigInteger& left, const UnsignedBigInteger& right, UnsignedBigInteger& output);
  71. static void shift_left_without_allocation(const UnsignedBigInteger& number, size_t bits_to_shift_by, UnsignedBigInteger& temp_result, UnsignedBigInteger& temp_plus, UnsignedBigInteger& output);
  72. static void multiply_without_allocation(const UnsignedBigInteger& left, const UnsignedBigInteger& right, UnsignedBigInteger& temp_shift_result, UnsignedBigInteger& temp_shift_plus, UnsignedBigInteger& temp_shift, UnsignedBigInteger& temp_plus, UnsignedBigInteger& output);
  73. static void divide_without_allocation(const UnsignedBigInteger& numerator, const UnsignedBigInteger& denominator, UnsignedBigInteger& temp_shift_result, UnsignedBigInteger& temp_shift_plus, UnsignedBigInteger& temp_shift, UnsignedBigInteger& temp_minus, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder);
  74. static void divide_u16_without_allocation(const UnsignedBigInteger& numerator, u32 denominator, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder);
  75. bool operator==(const UnsignedBigInteger& other) const;
  76. bool operator!=(const UnsignedBigInteger& other) const;
  77. bool operator<(const UnsignedBigInteger& other) const;
  78. private:
  79. ALWAYS_INLINE static void shift_left_by_n_words(const UnsignedBigInteger& number, size_t number_of_words, UnsignedBigInteger& output);
  80. ALWAYS_INLINE static u32 shift_left_get_one_word(const UnsignedBigInteger& number, size_t num_bits, size_t result_word_index);
  81. static constexpr size_t BITS_IN_WORD = 32;
  82. AK::Vector<u32, STARTING_WORD_SIZE> m_words;
  83. // Used to indicate a negative result, or a result of an invalid operation
  84. bool m_is_invalid { false };
  85. };
  86. struct UnsignedDivisionResult {
  87. Crypto::UnsignedBigInteger quotient;
  88. Crypto::UnsignedBigInteger remainder;
  89. };
  90. }
  91. inline const LogStream&
  92. operator<<(const LogStream& stream, const Crypto::UnsignedBigInteger value)
  93. {
  94. if (value.is_invalid()) {
  95. stream << "Invalid BigInt";
  96. return stream;
  97. }
  98. for (int i = value.length() - 1; i >= 0; --i) {
  99. stream << value.words()[i] << "|";
  100. }
  101. return stream;
  102. }
  103. inline Crypto::UnsignedBigInteger
  104. operator""_bigint(const char* string, size_t length)
  105. {
  106. return Crypto::UnsignedBigInteger::from_base10({ string, length });
  107. }