From d86c61de780f44a3b4c1f76d27508cb616513c7d Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 3 Jan 2016 10:42:44 -0600 Subject: [PATCH] BigInteger: add getprecision --- phpseclib/Math/BigInteger.php | 18 ++++++++++++++++++ tests/Unit/Math/BigInteger/TestCase.php | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index b8b1b26b..e057f546 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -2751,6 +2751,12 @@ class BigInteger */ function setPrecision($bits) { + if ($bits < 1) { + $this->precision = -1; + $this->bitmask = false; + + return; + } $this->precision = $bits; if (MATH_BIGINTEGER_MODE != self::MODE_BCMATH) { $this->bitmask = new static(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256); @@ -2762,6 +2768,18 @@ class BigInteger $this->value = $temp->value; } + /** + * Get Precision + * + * @return int + * @see self::setPrecision() + * @access public + */ + function getPrecision() + { + return $this->precision; + } + /** * Logical And * diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index c922a775..b13fde5e 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -348,6 +348,17 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $num = $this->getInstance(50); $str = print_r($num, true); $this->assertContains('[value] => 0x32', $str); - return $str; + } + + public function testPrecision() + { + $a = $this->getInstance(50); + $this->assertSame($a->getPrecision(), -1); + $b = $a; + $c = clone $a; + $b->setPrecision(4); + $this->assertSame($a->getPrecision(), 4); + $this->assertSame($b->getPrecision(), 4); + $this->assertSame($c->getPrecision(), -1); } }