From 46166c73515a06ea5400553336faa2f71ac50e79 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 14 Jun 2014 14:07:33 -0500 Subject: [PATCH] BigInteger: make it so you can do $min->random($max) ...and $min->randomPrime($max) as well --- phpseclib/Math/BigInteger.php | 26 ++++++++++++++++--------- tests/Unit/Math/BigInteger/TestCase.php | 18 +++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 4fd88909..cf90d556 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3088,19 +3088,26 @@ class Math_BigInteger /** * Generate a random number * - * @param optional Integer $min + * Returns a random number between $min and $max where $min and $max + * can be defined using one of the two methods: + * + * $generator->random($min, $max) + * $min->random($max) + * + * @param Integer $min * @param optional Integer $max * @return Math_BigInteger * @access public */ - function random($min = false, $max = false) + function random($min, $max = false) { if ($min === false) { - $min = new Math_BigInteger(0); + return new Math_BigInteger(0); } if ($max === false) { - $max = new Math_BigInteger(0x7FFFFFFF); + $max = $min->copy(); + $min = $this->copy(); } $compare = $max->compare($min); @@ -3163,21 +3170,22 @@ class Math_BigInteger * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, * give up and return false. * - * @param optional Integer $min + * @param Integer $min * @param optional Integer $max * @param optional Integer $timeout - * @return Math_BigInteger + * @return Mixed * @access public * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. */ - function randomPrime($min = false, $max = false, $timeout = false) + function randomPrime($min, $max = false, $timeout = false) { if ($min === false) { - $min = new Math_BigInteger(0); + return false } if ($max === false) { - $max = new Math_BigInteger(0x7FFFFFFF); + $max = $min->copy(); + $min = $this->copy(); } $compare = $max->compare($min); diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index d233dfb4..cdc8b2e5 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -266,6 +266,24 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertSame('18446744073709551616', (string) $y); } + public function testRandom() + { + $min = $this->getInstance(0); + $max = $this->getInstance('18446744073709551616'); + + $rand1 = $min->random($min, $max); + // technically $rand1 can equal $min but with the $min and $max we've + // chosen it's just not that likely + $this->assertTrue($rand1->compareTo($min) > 0); + $this->assertTrue($rand1->compareTo($max) < 0); + + $rand2 = $min->random($max); + $this->assertTrue($rand1->compareTo($min) > 0); + $this->assertTrue($rand1->compareTo($max) < 0); + + $this->assertFalse($rand1->equals($rand2)); + } + /** * @group github279 */