summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCrypto
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2022-01-17 12:22:51 -0500
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2022-01-18 20:04:06 +0330
commit945d9623223b307fe3026a5a9753c3ca003646a1 (patch)
tree9994319c8388f2faffe9c4419362f9585147944f /Userland/Libraries/LibCrypto
parentec37eadb394a2aef8ccc3326611ee0b2fb3ce8c7 (diff)
downloadserenity-945d9623223b307fe3026a5a9753c3ca003646a1.zip
LibJS+LibCrypto: Fix SignedBitInteger::bitwise_not and use it in LibJS
Bitwise operators are defined on two's complement, but SignedBitInteger uses sign-magnitude. Correctly convert between the two. Let LibJS delegate to SignedBitInteger for bitwise_not, like it does for all other bitwise_ operations on bigints. No behavior change (LibJS is now the only client of SignedBitInteger::bitwise_not()).
Diffstat (limited to 'Userland/Libraries/LibCrypto')
-rw-r--r--Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp7
1 files changed, 6 insertions, 1 deletions
diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp
index 0787f90eb1..9d860f4c43 100644
--- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp
+++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.cpp
@@ -160,7 +160,12 @@ FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const UnsignedBigInteger&
FLATTEN SignedBigInteger SignedBigInteger::bitwise_not() const
{
- return { unsigned_value().bitwise_not(), !m_sign };
+ // Bitwise operators assume two's complement, while SignedBigInteger uses sign-magnitude.
+ // In two's complement, -x := ~x + 1.
+ // Hence, ~x == -x -1 == -(x + 1).
+ SignedBigInteger result = plus(SignedBigInteger { 1 });
+ result.negate();
+ return result;
}
FLATTEN SignedBigInteger SignedBigInteger::multiplied_by(UnsignedBigInteger const& other) const