diff --git a/phpseclib/Math/BigInteger/Engines/GMP.php b/phpseclib/Math/BigInteger/Engines/GMP.php index cdabbb72..17696c17 100644 --- a/phpseclib/Math/BigInteger/Engines/GMP.php +++ b/phpseclib/Math/BigInteger/Engines/GMP.php @@ -387,7 +387,7 @@ class GMP extends Engine public function bitwise_xor(GMP $x) { $temp = new self(); - $temp->value = $this->value ^ $x->value; + $temp->value = gmp_abs($this->value) ^ gmp_abs($x->value); return $this->normalize($temp); } diff --git a/phpseclib/Math/BigInteger/Engines/PHP.php b/phpseclib/Math/BigInteger/Engines/PHP.php index 7ed704eb..1ec77e24 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP.php +++ b/phpseclib/Math/BigInteger/Engines/PHP.php @@ -938,6 +938,7 @@ abstract class PHP extends Engine { $length = max(count($this->value), count($x->value)); $result = clone $this; + $result->is_negative = false; $result->value = array_pad($result->value, $length, 0); $x->value = array_pad($x->value, $length, 0); diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index 174b008a..55637e45 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -198,6 +198,18 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $z = $this->getInstance('BC98BC98BC98BC98BC98BC98', 16); $this->assertSame($z->toHex(), $x->bitwise_XOR($y)->toHex()); + + // @group github1245 + + $a = $this->getInstance(1); + $b = $this->getInstance(-2); + $c = $a->bitwise_xor($b); + $this->assertSame("$c", '3'); + + $a = $this->getInstance('-6725760161961546982'); + $b = $this->getInstance(51); + $c = $a->bitwise_xor($b); + $this->assertSame("$c", '6725760161961546965'); } public function testBitwiseNOT()