mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:37:35 +00:00
LibCrypto: Add UnsignedBigInteger division
The division operation returns both the quotient and the remainder.
This commit is contained in:
parent
2959c4a5e9
commit
0d2777752e
3 changed files with 113 additions and 6 deletions
|
@ -30,6 +30,9 @@
|
|||
#include <AK/Vector.h>
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
struct UnsignedDivisionResult;
|
||||
|
||||
class UnsignedBigInteger {
|
||||
public:
|
||||
UnsignedBigInteger(u32 x) { m_words.append(x); }
|
||||
|
@ -49,7 +52,10 @@ public:
|
|||
UnsignedBigInteger sub(const UnsignedBigInteger& other) const;
|
||||
UnsignedBigInteger multiply(const UnsignedBigInteger& other) const;
|
||||
UnsignedBigInteger shift_left(size_t num_bits) const;
|
||||
UnsignedBigInteger shift_left_by_n_words(const size_t number_of_words) const;
|
||||
|
||||
UnsignedDivisionResult divide(const UnsignedBigInteger& divisor) const;
|
||||
|
||||
void set_bit_inplace(size_t bit_index);
|
||||
|
||||
size_t length() const { return m_words.size(); }
|
||||
|
||||
|
@ -63,6 +69,7 @@ public:
|
|||
bool is_invalid() const { return m_is_invalid; }
|
||||
|
||||
private:
|
||||
UnsignedBigInteger shift_left_by_n_words(const size_t number_of_words) const;
|
||||
u32 shift_left_get_one_word(const size_t num_bits, const size_t result_word_index) const;
|
||||
|
||||
static constexpr size_t BITS_IN_WORD = 32;
|
||||
|
@ -72,6 +79,11 @@ private:
|
|||
bool m_is_invalid { false };
|
||||
};
|
||||
|
||||
struct UnsignedDivisionResult {
|
||||
Crypto::UnsignedBigInteger quotient;
|
||||
Crypto::UnsignedBigInteger remainder;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, const Crypto::UnsignedBigInteger value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue