diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index ed28c78c..36e6ce99 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -2694,7 +2694,14 @@ class BigInteger { switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: - return gmp_cmp($this->value, $y->value); + $r = gmp_cmp($this->value, $y->value); + if ($r < -1) { + $r = -1; + } + if ($r > 1) { + $r = 1; + } + return $r; case self::MODE_BCMATH: return bccomp($this->value, $y->value, 0); } diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index 0b9ef016..92c72222 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -182,6 +182,9 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase // c < d $this->assertLessThan(0, $c->compare($d)); $this->assertGreaterThan(0, $d->compare($c)); + + $this->assertSame(-1, $this->getInstance(-999)->compare($this->getInstance(370))); + $this->assertSame(1, $this->getInstance(999)->compare($this->getInstance(-700))); } public function testBitwiseAND()