mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:47:36 +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:
parent
5963f6f9ff
commit
f4e6f58cc6
5 changed files with 16 additions and 15 deletions
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
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))
|
||||
{
|
||||
}
|
||||
|
@ -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 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue