diff options
author | DexesTTP <dexes.ttp@gmail.com> | 2021-05-12 13:25:55 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-13 19:18:07 +0100 |
commit | f4e6f58cc64bc24544f1e0ba4fb39f4688d03fd4 (patch) | |
tree | 3a9826af0f288338f759e56bda5b3d8be9bcbd46 /Userland/Libraries/LibCrypto | |
parent | 5963f6f9ffa2538ed0a55bfae2ddb8f5c50ec29a (diff) | |
download | serenity-f4e6f58cc64bc24544f1e0ba4fb39f4688d03fd4.zip |
LibCrypto: Add the UnsignedBigInteger::Word alias
This makes it clearer which variables are operating on words instead
of directly operating on raw values.
Diffstat (limited to 'Userland/Libraries/LibCrypto')
5 files changed, 16 insertions, 15 deletions
diff --git a/Userland/Libraries/LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp b/Userland/Libraries/LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp index 01675349e2..cd88a94603 100644 --- a/Userland/Libraries/LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp @@ -220,7 +220,7 @@ ALWAYS_INLINE void UnsignedBigIntegerAlgorithms::shift_left_by_n_words( /** * Returns the word at a requested index in the result of a shift operation */ -ALWAYS_INLINE u32 UnsignedBigIntegerAlgorithms::shift_left_get_one_word( +ALWAYS_INLINE UnsignedBigInteger::Word UnsignedBigIntegerAlgorithms::shift_left_get_one_word( UnsignedBigInteger const& number, size_t num_bits, size_t result_word_index) diff --git a/Userland/Libraries/LibCrypto/BigInt/Algorithms/Division.cpp b/Userland/Libraries/LibCrypto/BigInt/Algorithms/Division.cpp index 1607404db6..8a54b1641a 100644 --- a/Userland/Libraries/LibCrypto/BigInt/Algorithms/Division.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/Algorithms/Division.cpp @@ -54,12 +54,12 @@ FLATTEN void UnsignedBigIntegerAlgorithms::divide_without_allocation( */ FLATTEN void UnsignedBigIntegerAlgorithms::divide_u16_without_allocation( UnsignedBigInteger const& numerator, - u32 denominator, + UnsignedBigInteger::Word denominator, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder) { VERIFY(denominator < (1 << 16)); - u32 remainder_word = 0; + UnsignedBigInteger::Word remainder_word = 0; auto numerator_length = numerator.trimmed_length(); quotient.set_to_0(); quotient.m_words.resize(numerator_length); diff --git a/Userland/Libraries/LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h b/Userland/Libraries/LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h index 9f740aaebc..29a16ce19a 100644 --- a/Userland/Libraries/LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h +++ b/Userland/Libraries/LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h @@ -21,7 +21,7 @@ public: static void shift_left_without_allocation(UnsignedBigInteger const& number, size_t bits_to_shift_by, UnsignedBigInteger& temp_result, UnsignedBigInteger& temp_plus, UnsignedBigInteger& output); static void multiply_without_allocation(UnsignedBigInteger const& left, UnsignedBigInteger const& right, UnsignedBigInteger& temp_shift_result, UnsignedBigInteger& temp_shift_plus, UnsignedBigInteger& temp_shift, UnsignedBigInteger& temp_plus, UnsignedBigInteger& output); static void divide_without_allocation(UnsignedBigInteger const& numerator, UnsignedBigInteger const& denominator, UnsignedBigInteger& temp_shift_result, UnsignedBigInteger& temp_shift_plus, UnsignedBigInteger& temp_shift, UnsignedBigInteger& temp_minus, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder); - static void divide_u16_without_allocation(UnsignedBigInteger const& numerator, u32 denominator, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder); + static void divide_u16_without_allocation(UnsignedBigInteger const& numerator, UnsignedBigInteger::Word denominator, UnsignedBigInteger& quotient, UnsignedBigInteger& remainder); static void destructive_GCD_without_allocation(UnsignedBigInteger& temp_a, UnsignedBigInteger& temp_b, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_2, UnsignedBigInteger& temp_3, UnsignedBigInteger& temp_4, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_remainder, UnsignedBigInteger& output); static void modular_inverse_without_allocation(UnsignedBigInteger const& a_, UnsignedBigInteger const& b, UnsignedBigInteger& temp_1, UnsignedBigInteger& temp_2, UnsignedBigInteger& temp_3, UnsignedBigInteger& temp_4, UnsignedBigInteger& temp_plus, UnsignedBigInteger& temp_minus, UnsignedBigInteger& temp_quotient, UnsignedBigInteger& temp_d, UnsignedBigInteger& temp_u, UnsignedBigInteger& temp_v, UnsignedBigInteger& temp_x, UnsignedBigInteger& result); @@ -29,7 +29,7 @@ public: private: ALWAYS_INLINE static void shift_left_by_n_words(UnsignedBigInteger const& number, size_t number_of_words, UnsignedBigInteger& output); - ALWAYS_INLINE static u32 shift_left_get_one_word(UnsignedBigInteger const& number, size_t num_bits, size_t result_word_index); + ALWAYS_INLINE static UnsignedBigInteger::Word shift_left_get_one_word(UnsignedBigInteger const& number, size_t num_bits, size_t result_word_index); }; } diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index 79ef86c76a..15547aaaff 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -43,7 +43,7 @@ size_t UnsignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) co if (word_count > 0) { ssize_t leading_zeros = -1; if (remove_leading_zeros) { - u32 word = m_words[word_count - 1]; + UnsignedBigInteger::Word word = m_words[word_count - 1]; for (size_t i = 0; i < sizeof(u32); i++) { u8 byte = (u8)(word >> ((sizeof(u32) - i - 1) * 8)); data[out++] = byte; @@ -108,7 +108,7 @@ void UnsignedBigInteger::set_to_0() m_cached_trimmed_length = {}; } -void UnsignedBigInteger::set_to(u32 other) +void UnsignedBigInteger::set_to(UnsignedBigInteger::Word other) { m_is_invalid = false; m_words.resize_and_keep_capacity(1); diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h index e2d467fc42..eb73686e5c 100644 --- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h +++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h @@ -19,9 +19,12 @@ constexpr size_t STARTING_WORD_SIZE = 512; class UnsignedBigInteger { public: - UnsignedBigInteger(u32 x) { m_words.append(x); } + using Word = u32; + static constexpr size_t BITS_IN_WORD = 32; + + UnsignedBigInteger(Word x) { m_words.append(x); } - explicit UnsignedBigInteger(Vector<u32, STARTING_WORD_SIZE>&& words) + explicit UnsignedBigInteger(Vector<Word, STARTING_WORD_SIZE>&& words) : m_words(move(words)) { } @@ -43,10 +46,10 @@ public: static UnsignedBigInteger from_base10(const String& str); String to_base10() const; - const Vector<u32, STARTING_WORD_SIZE>& words() const { return m_words; } + const Vector<Word, STARTING_WORD_SIZE>& words() const { return m_words; } void set_to_0(); - void set_to(u32 other); + void set_to(Word other); void set_to(const UnsignedBigInteger& other); void invalidate() @@ -81,11 +84,9 @@ public: private: friend class UnsignedBigIntegerAlgorithms; - - static constexpr size_t BITS_IN_WORD = 32; // Little endian - // m_word[0] + m_word[1] * 256 + m_word[2] * 65536 + ... - Vector<u32, STARTING_WORD_SIZE> m_words; + // m_word[0] + m_word[1] * Word::MAX + m_word[2] * Word::MAX * Word::MAX + ... + Vector<Word, STARTING_WORD_SIZE> m_words; // Used to indicate a negative result, or a result of an invalid operation bool m_is_invalid { false }; |