diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 0f60b8a9..57e14d2f 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -1561,7 +1561,9 @@ class BigInteger $temp_value = array($quotient_value[$q_index]); $temp = $temp->multiply($y); $temp_value = &$temp->value; - $temp_value = array_merge($adjust, $temp_value); + if ($temp_value !== []) { + $temp_value = array_merge($adjust, $temp_value); + } $x = $x->subtract($temp); @@ -3582,6 +3584,7 @@ class BigInteger $value = &$result->value; if (!count($value)) { + $result->is_negative = false; return $result; } diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index 132031f8..02656fcc 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -103,6 +103,22 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertSame('95627922070', (string) $q); $this->assertSame('10688759725', (string) $r); + + $x = $this->getInstance('3369993333393829974333376885877453834204643052817571560137951281152'); + $y = $this->getInstance('4294967296'); + + list($q, $r) = $x->divide($y); + + $this->assertSame('784637716923335095479473677900958302012794430558004314112', (string) $q); + $this->assertSame('0', (string) $r); + + $x = $this->getInstance('3369993333393829974333376885877453834204643052817571560137951281153'); + $y = $this->getInstance('4294967296'); + + list($q, $r) = $x->divide($y); + + $this->assertSame('784637716923335095479473677900958302012794430558004314112', (string) $q); + $this->assertSame('1', (string) $r); } public function testModPow()