diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-02-06 13:17:34 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-06 15:49:54 +0000 |
commit | b0d6399f60760e25a55ec9e8e95a1ad322b74b22 (patch) | |
tree | e31fa3275b81bfa9c4fe6376cb533526596a8d68 /Tests/LibCrypto/TestBigInteger.cpp | |
parent | 4d785b9aa0cbca1f6ce108a87d3232a58a2069bb (diff) | |
download | serenity-b0d6399f60760e25a55ec9e8e95a1ad322b74b22.zip |
LibCrypto: Do not allow signed big integers to be negative zero
If a big integer were to become negative zero, set the sign to instead
be positive. This prevents odd scenarios where users of signed big ints
would falsely think the result of some big int arithmetic is negative.
Diffstat (limited to 'Tests/LibCrypto/TestBigInteger.cpp')
-rw-r--r-- | Tests/LibCrypto/TestBigInteger.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Tests/LibCrypto/TestBigInteger.cpp b/Tests/LibCrypto/TestBigInteger.cpp index 7af6eb4a1d..94beca9553 100644 --- a/Tests/LibCrypto/TestBigInteger.cpp +++ b/Tests/LibCrypto/TestBigInteger.cpp @@ -642,3 +642,19 @@ TEST_CASE(test_signed_multiplication_with_two_big_numbers) EXPECT_EQ(result.unsigned_value().words(), expected_results); EXPECT(result.is_negative()); } + +TEST_CASE(test_negative_zero_is_not_allowed) +{ + Crypto::SignedBigInteger zero(Crypto::UnsignedBigInteger(0), true); + EXPECT(!zero.is_negative()); + + zero.negate(); + EXPECT(!zero.is_negative()); + + Crypto::SignedBigInteger positive_five(Crypto::UnsignedBigInteger(5), false); + Crypto::SignedBigInteger negative_five(Crypto::UnsignedBigInteger(5), true); + zero = positive_five.plus(negative_five); + + EXPECT(zero.unsigned_value().is_zero()); + EXPECT(!zero.is_negative()); +} |