diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 1ed9e216..652d0b18 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3048,19 +3048,31 @@ class BigInteger /** * Generate a random number * - * @param optional Integer $min - * @param optional Integer $max + * Returns a random number between $min and $max where $min and $max + * can be defined using one of the two methods: + * + * $min->random($max) + * $max->random($min) + * + * @param \phpseclib\Math\BigInteger $arg1 + * @param optional \phpseclib\Math\BigInteger $arg2 * @return \phpseclib\Math\BigInteger * @access public + * @internal The API for creating random numbers used to be $a->random($min, $max), where $a was a BigInteger object. + * That method is still supported for BC purposes. */ - function random($min = false, $max = false) + function random($arg1, $arg2 = false) { - if ($min === false) { - $min = new static(0); + if ($arg1 === false) { + return false; } - if ($max === false) { - $max = new static(0x7FFFFFFF); + if ($arg2 === false) { + $max = $arg1; + $min = $this; + } else { + $min = $arg1; + $max = $arg2; } $compare = $max->compare($min); @@ -3123,21 +3135,25 @@ class 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 optional Integer $max + * @param \phpseclib\Math\BigInteger $arg1 + * @param optional \phpseclib\Math\BigInteger $arg2 * @param optional Integer $timeout - * @return \phpseclib\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($arg1, $arg2 = false, $timeout = false) { - if ($min === false) { - $min = new static(0); + if ($arg1 === false) { + return false; } - if ($max === false) { - $max = new static(0x7FFFFFFF); + if ($arg2 === false) { + $max = $arg1; + $min = $this; + } else { + $min = $arg1; + $max = $arg2; } $compare = $max->compare($min); @@ -3249,7 +3265,7 @@ class BigInteger * $t parameter is distributability. BigInteger::randomPrime() can be distributed across multiple pageloads * on a website instead of just one. * - * @param optional Integer $t + * @param optional \phpseclib\Math\BigInteger $t * @return Boolean * @access public * @internal Uses the diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index 69459ca2..b0947667 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -266,6 +266,34 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertSame('18446744073709551616', (string) $y); } + public function testRandomTwoArgument() + { + $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->compare($min) > 0); + $this->assertTrue($rand1->compare($max) < 0); + } + + public function testRandomOneArgument() + { + $min = $this->getInstance(0); + $max = $this->getInstance('18446744073709551616'); + + $rand1 = $min->random($max); + $this->assertTrue($rand1->compare($min) > 0); + $this->assertTrue($rand1->compare($max) < 0); + + $rand2 = $max->random($min); + $this->assertTrue($rand2->compare($min) > 0); + $this->assertTrue($rand2->compare($max) < 0); + + $this->assertFalse($rand1->equals($rand2)); + } + /** * @group github279 */