From d0014cbb20dd227fb0934341711edc7631548675 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Wed, 19 Jun 2019 22:21:24 -0500 Subject: [PATCH 1/2] Tests/BigInteger: add two more divide tests --- tests/Unit/Math/BigInteger/TestCase.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index d6985fd3..356b6419 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -106,6 +106,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() From 5e9d05a9f41aefc382b8f4da7eec00c5e31a0a4e Mon Sep 17 00:00:00 2001 From: terrafrost Date: Wed, 19 Jun 2019 22:29:31 -0500 Subject: [PATCH 2/2] BigInteger: fix issues with divide method --- phpseclib/Math/BigInteger.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 9d41a5c2..000f2bcc 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -1586,7 +1586,9 @@ class Math_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); @@ -3628,6 +3630,7 @@ class Math_BigInteger $value = &$result->value; if (!count($value)) { + $result->is_negative = false; return $result; }