mirror of
https://github.com/danog/tgseclib.git
synced 2024-11-27 04:34:45 +01:00
BigInteger: make it so you can do $min->random($max)
...and $min->randomPrime($max) as well
This commit is contained in:
parent
90bc07e292
commit
46166c7351
@ -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);
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user