1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

LibCrypto: Add the UnsignedBigInteger::Word alias

This makes it clearer which variables are operating on words instead
of directly operating on raw values.
This commit is contained in:
DexesTTP 2021-05-12 13:25:55 +02:00 committed by Linus Groh
parent 5963f6f9ff
commit f4e6f58cc6
5 changed files with 16 additions and 15 deletions

View file

@ -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 * 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, UnsignedBigInteger const& number,
size_t num_bits, size_t num_bits,
size_t result_word_index) size_t result_word_index)

View file

@ -54,12 +54,12 @@ FLATTEN void UnsignedBigIntegerAlgorithms::divide_without_allocation(
*/ */
FLATTEN void UnsignedBigIntegerAlgorithms::divide_u16_without_allocation( FLATTEN void UnsignedBigIntegerAlgorithms::divide_u16_without_allocation(
UnsignedBigInteger const& numerator, UnsignedBigInteger const& numerator,
u32 denominator, UnsignedBigInteger::Word denominator,
UnsignedBigInteger& quotient, UnsignedBigInteger& quotient,
UnsignedBigInteger& remainder) UnsignedBigInteger& remainder)
{ {
VERIFY(denominator < (1 << 16)); VERIFY(denominator < (1 << 16));
u32 remainder_word = 0; UnsignedBigInteger::Word remainder_word = 0;
auto numerator_length = numerator.trimmed_length(); auto numerator_length = numerator.trimmed_length();
quotient.set_to_0(); quotient.set_to_0();
quotient.m_words.resize(numerator_length); quotient.m_words.resize(numerator_length);

View file

@ -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 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 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_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 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); 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: private:
ALWAYS_INLINE static void shift_left_by_n_words(UnsignedBigInteger const& number, size_t number_of_words, UnsignedBigInteger& output); 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);
}; };
} }

View file

@ -43,7 +43,7 @@ size_t UnsignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) co
if (word_count > 0) { if (word_count > 0) {
ssize_t leading_zeros = -1; ssize_t leading_zeros = -1;
if (remove_leading_zeros) { 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++) { for (size_t i = 0; i < sizeof(u32); i++) {
u8 byte = (u8)(word >> ((sizeof(u32) - i - 1) * 8)); u8 byte = (u8)(word >> ((sizeof(u32) - i - 1) * 8));
data[out++] = byte; data[out++] = byte;
@ -108,7 +108,7 @@ void UnsignedBigInteger::set_to_0()
m_cached_trimmed_length = {}; m_cached_trimmed_length = {};
} }
void UnsignedBigInteger::set_to(u32 other) void UnsignedBigInteger::set_to(UnsignedBigInteger::Word other)
{ {
m_is_invalid = false; m_is_invalid = false;
m_words.resize_and_keep_capacity(1); m_words.resize_and_keep_capacity(1);

View file

@ -19,9 +19,12 @@ constexpr size_t STARTING_WORD_SIZE = 512;
class UnsignedBigInteger { class UnsignedBigInteger {
public: public:
UnsignedBigInteger(u32 x) { m_words.append(x); } using Word = u32;
static constexpr size_t BITS_IN_WORD = 32;
explicit UnsignedBigInteger(Vector<u32, STARTING_WORD_SIZE>&& words) UnsignedBigInteger(Word x) { m_words.append(x); }
explicit UnsignedBigInteger(Vector<Word, STARTING_WORD_SIZE>&& words)
: m_words(move(words)) : m_words(move(words))
{ {
} }
@ -43,10 +46,10 @@ public:
static UnsignedBigInteger from_base10(const String& str); static UnsignedBigInteger from_base10(const String& str);
String to_base10() const; 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_0();
void set_to(u32 other); void set_to(Word other);
void set_to(const UnsignedBigInteger& other); void set_to(const UnsignedBigInteger& other);
void invalidate() void invalidate()
@ -81,11 +84,9 @@ public:
private: private:
friend class UnsignedBigIntegerAlgorithms; friend class UnsignedBigIntegerAlgorithms;
static constexpr size_t BITS_IN_WORD = 32;
// Little endian // Little endian
// m_word[0] + m_word[1] * 256 + m_word[2] * 65536 + ... // m_word[0] + m_word[1] * Word::MAX + m_word[2] * Word::MAX * Word::MAX + ...
Vector<u32, STARTING_WORD_SIZE> m_words; Vector<Word, STARTING_WORD_SIZE> m_words;
// Used to indicate a negative result, or a result of an invalid operation // Used to indicate a negative result, or a result of an invalid operation
bool m_is_invalid { false }; bool m_is_invalid { false };