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

LibCrypto: Fix subtracting two negative SignedBigIntegers

Currently, we get the following results

    -1 - -2 = -1
    -2 - -1 =  1

Correct would be:

    -1 - -2 =  1
    -2 - -1 = -1

This was already attempted to be fixed in 7ed8970, but that change was
incorrect. This directly translates to LibJS BigInts having the same
incorrect behavior - it even was tested.
This commit is contained in:
Linus Groh 2021-11-16 09:17:55 +00:00
parent 014840eeca
commit 58c6a156bf
3 changed files with 17 additions and 4 deletions

View file

@ -543,6 +543,15 @@ TEST_CASE(test_signed_subtraction_simple_subtraction_negative_result)
EXPECT_EQ(num1.minus(num2), Crypto::SignedBigInteger { -20 });
}
TEST_CASE(test_signed_subtraction_both_negative)
{
Crypto::SignedBigInteger num1(-50);
Crypto::SignedBigInteger num2(-70);
EXPECT_EQ(num1.minus(num2), Crypto::SignedBigInteger { 20 });
EXPECT_EQ(num2.minus(num1), Crypto::SignedBigInteger { -20 });
}
TEST_CASE(test_signed_subtraction_simple_subtraction_with_borrow)
{
Crypto::SignedBigInteger num1(Crypto::UnsignedBigInteger { UINT32_MAX });