diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index b6f2aaf6..2aa39a50 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -2868,8 +2868,7 @@ class BigInteger switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp = new static(); - $temp->value = gmp_xor($this->value, $x->value); - + $temp->value = gmp_xor(gmp_abs($this->value), gmp_abs($x->value)); return $this->_normalize($temp); case self::MODE_BCMATH: $left = $this->toBytes(); @@ -2885,6 +2884,7 @@ class BigInteger $length = max(count($this->value), count($x->value)); $result = $this->copy(); + $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 a7a60012..4c88d402 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -209,6 +209,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()