LibCrypto+LibJS: Better bitwise binary_xor binop

We went through some trouble to make & and | work right. Reimplement ^
in terms of & and | to make ^ work right as well.

This is less fast than a direct implementation, but let's get things
working first.
This commit is contained in:
Nico Weber 2022-01-17 20:03:51 -05:00 committed by Ali Mohammad Pur
parent 013799a4dd
commit d9b6eb29bc
Notes: sideshowbarker 2024-07-18 03:35:30 +09:00
2 changed files with 3 additions and 8 deletions

View File

@ -526,8 +526,8 @@ TEST_CASE(test_signed_bigint_bitwise_xor)
auto num1 = "-3"_sbigint;
auto num2 = "1"_sbigint;
EXPECT_EQ(num1.bitwise_xor(num1), "0"_sbigint);
EXPECT_EQ(num1.bitwise_xor(num2), "-2"_sbigint);
EXPECT_EQ(num2.bitwise_xor(num1), "-2"_sbigint);
EXPECT_EQ(num1.bitwise_xor(num2), "-4"_sbigint);
EXPECT_EQ(num2.bitwise_xor(num1), "-4"_sbigint);
EXPECT_EQ(num2.bitwise_xor(num2), "0"_sbigint);
}

View File

@ -238,12 +238,7 @@ FLATTEN SignedBigInteger SignedBigInteger::bitwise_and(const SignedBigInteger& o
FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const SignedBigInteger& other) const
{
auto result = bitwise_xor(other.unsigned_value());
// The sign bit will have to be XOR'd manually.
result.m_sign = is_negative() ^ other.is_negative();
return result;
return bitwise_or(other).minus(bitwise_and(other));
}
bool SignedBigInteger::operator==(const UnsignedBigInteger& other) const