Merge branch 'master' into php5

* master:
  BigInteger: one more change to the comments
  BigInteger: update comments
  BigInteger: unit test update
  BigInteger: change argument names for random / randomPrime
  BigInteger: fix unit tests
  BigInteger: fix syntax error
  BigInteger: make it so you can do $min->random($max)

Conflicts:
	phpseclib/Math/BigInteger.php
This commit is contained in:
Andreas Fischer 2014-06-16 17:00:00 +02:00
commit fc417115be
2 changed files with 60 additions and 16 deletions

View File

@ -3048,19 +3048,31 @@ class BigInteger
/** /**
* Generate a random number * Generate a random number
* *
* @param optional Integer $min * Returns a random number between $min and $max where $min and $max
* @param optional Integer $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 * @return \phpseclib\Math\BigInteger
* @access public * @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) { if ($arg1 === false) {
$min = new static(0); return false;
} }
if ($max === false) { if ($arg2 === false) {
$max = new static(0x7FFFFFFF); $max = $arg1;
$min = $this;
} else {
$min = $arg1;
$max = $arg2;
} }
$compare = $max->compare($min); $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, * 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. * give up and return false.
* *
* @param optional Integer $min * @param \phpseclib\Math\BigInteger $arg1
* @param optional Integer $max * @param optional \phpseclib\Math\BigInteger $arg2
* @param optional Integer $timeout * @param optional Integer $timeout
* @return \phpseclib\Math\BigInteger * @return Mixed
* @access public * @access public
* @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. * @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) { if ($arg1 === false) {
$min = new static(0); return false;
} }
if ($max === false) { if ($arg2 === false) {
$max = new static(0x7FFFFFFF); $max = $arg1;
$min = $this;
} else {
$min = $arg1;
$max = $arg2;
} }
$compare = $max->compare($min); $compare = $max->compare($min);
@ -3249,7 +3265,7 @@ class BigInteger
* $t parameter is distributability. BigInteger::randomPrime() can be distributed across multiple pageloads * $t parameter is distributability. BigInteger::randomPrime() can be distributed across multiple pageloads
* on a website instead of just one. * on a website instead of just one.
* *
* @param optional Integer $t * @param optional \phpseclib\Math\BigInteger $t
* @return Boolean * @return Boolean
* @access public * @access public
* @internal Uses the * @internal Uses the

View File

@ -266,6 +266,34 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$this->assertSame('18446744073709551616', (string) $y); $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 * @group github279
*/ */