mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-01 12:07:14 +02:00
LibCrypto: Add UnsignedBigInteger subtraction and comparison
This commit is contained in:
Notes:
sideshowbarker
2024-07-19 07:05:57 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/e0cf40518c2 Pull-request: https://github.com/SerenityOS/serenity/pull/1661 Reviewed-by: https://github.com/Dexesttp Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/awesomekling
@@ -64,11 +64,47 @@ UnsignedBigInteger UnsignedBigInteger::add(const UnsignedBigInteger& other)
|
||||
return result;
|
||||
}
|
||||
|
||||
UnsignedBigInteger UnsignedBigInteger::sub(const UnsignedBigInteger& other)
|
||||
{
|
||||
UnsignedBigInteger result;
|
||||
|
||||
if (*this < other) {
|
||||
dbg() << "WARNING: bigint subtraction creates a negative number!";
|
||||
return UnsignedBigInteger::create_invalid();
|
||||
}
|
||||
|
||||
u8 borrow = 0;
|
||||
for (size_t i = 0; i < other.length(); ++i) {
|
||||
ASSERT(!(borrow == 1 && m_words[i] == 0));
|
||||
|
||||
if (m_words[i] - borrow < other.m_words[i]) {
|
||||
u64 after_borrow = static_cast<u64>(m_words[i] - borrow) + (UINT32_MAX + 1);
|
||||
result.m_words.append(static_cast<u32>(after_borrow - static_cast<u64>(other.m_words[i])));
|
||||
borrow = 1;
|
||||
} else {
|
||||
result.m_words.append(m_words[i] - borrow - other.m_words[i]);
|
||||
borrow = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = other.length(); i < length(); ++i) {
|
||||
ASSERT(!(borrow == 1 && m_words[i] == 0));
|
||||
result.m_words.append(m_words[i] - borrow);
|
||||
borrow = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool UnsignedBigInteger::operator==(const UnsignedBigInteger& other) const
|
||||
{
|
||||
if (trimmed_length() != other.trimmed_length()) {
|
||||
return false;
|
||||
}
|
||||
if (is_invalid() != other.is_invalid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < trimmed_length(); ++i) {
|
||||
if (m_words[i] != other.words()[i])
|
||||
return false;
|
||||
@@ -76,6 +112,23 @@ bool UnsignedBigInteger::operator==(const UnsignedBigInteger& other) const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UnsignedBigInteger::operator<(const UnsignedBigInteger& other) const
|
||||
{
|
||||
if (trimmed_length() < other.trimmed_length()) {
|
||||
return true;
|
||||
}
|
||||
if (trimmed_length() > other.trimmed_length()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t length = trimmed_length();
|
||||
if (length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_words[length - 1] < other.m_words[length - 1];
|
||||
}
|
||||
|
||||
size_t UnsignedBigInteger::trimmed_length() const
|
||||
{
|
||||
size_t num_leading_zeroes = 0;
|
||||
@@ -86,4 +139,11 @@ size_t UnsignedBigInteger::trimmed_length() const
|
||||
return length() - num_leading_zeroes;
|
||||
}
|
||||
|
||||
UnsignedBigInteger UnsignedBigInteger::create_invalid()
|
||||
{
|
||||
UnsignedBigInteger invalid(0);
|
||||
invalid.invalidate();
|
||||
return invalid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user