mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-17 05:12:58 +00:00
LibCrypto: Fix bug in big int subtraction
A regression test was added to the suite. This commit also generally simplifies the subtraction method.
This commit is contained in:
parent
0d2777752e
commit
709c691f38
|
@ -79,25 +79,19 @@ UnsignedBigInteger UnsignedBigInteger::sub(const UnsignedBigInteger& other) cons
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 borrow = 0;
|
u8 borrow = 0;
|
||||||
for (size_t i = 0; i < other.length(); ++i) {
|
for (size_t i = 0; i < length(); ++i) {
|
||||||
// This assertion should not fail, because we verified that *this>other at the beginning of the function
|
u32 other_word = (i < other.length()) ? other.m_words[i] : 0;
|
||||||
ASSERT(!(borrow == 1 && m_words[i] == 0));
|
i64 temp = static_cast<i64>(m_words[i]) - static_cast<i64>(other_word) - static_cast<i64>(borrow);
|
||||||
|
// If temp < 0, we had an underflow
|
||||||
if (m_words[i] - borrow < other.m_words[i]) {
|
borrow = (temp >= 0) ? 0 : 1;
|
||||||
u64 after_borrow = static_cast<u64>(m_words[i] - borrow) + (UINT32_MAX + 1);
|
if (temp < 0) {
|
||||||
result.m_words.append(static_cast<u32>(after_borrow - static_cast<u64>(other.m_words[i])));
|
temp += (UINT32_MAX + 1);
|
||||||
borrow = 1;
|
|
||||||
} else {
|
|
||||||
result.m_words.append(m_words[i] - borrow - other.m_words[i]);
|
|
||||||
borrow = 0;
|
|
||||||
}
|
}
|
||||||
|
result.m_words.append(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = other.length(); i < length(); ++i) {
|
// This assertion should not fail, because we verified that *this>=other at the beginning of the function
|
||||||
ASSERT(!(borrow == 1 && m_words[i] == 0));
|
ASSERT(borrow == 0);
|
||||||
result.m_words.append(m_words[i] - borrow);
|
|
||||||
borrow = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -915,6 +915,15 @@ void bigint_subtraction()
|
||||||
// this test only verifies that we don't crash on an assertion
|
// this test only verifies that we don't crash on an assertion
|
||||||
PASS;
|
PASS;
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
I_TEST((BigInteger | Subtraction Regerssion 1));
|
||||||
|
auto num = Crypto::UnsignedBigInteger { 1 }.shift_left(256);
|
||||||
|
if (num.sub(1).words() == Vector<u32> { 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 0 }) {
|
||||||
|
PASS;
|
||||||
|
} else {
|
||||||
|
FAIL(Incorrect Result);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bigint_multiplication()
|
void bigint_multiplication()
|
||||||
|
|
Loading…
Reference in a new issue