From 46166c73515a06ea5400553336faa2f71ac50e79 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 14 Jun 2014 14:07:33 -0500 Subject: [PATCH 1/7] 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 */ From f8f0bd44b754414f46e44eb1501ed054fd946d02 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 14 Jun 2014 19:20:05 -0500 Subject: [PATCH 2/7] BigInteger: fix syntax error --- phpseclib/Math/BigInteger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index cf90d556..04f682a9 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3180,7 +3180,7 @@ class Math_BigInteger function randomPrime($min, $max = false, $timeout = false) { if ($min === false) { - return false + return false; } if ($max === false) { From 281a8c669e34ae047731ddc8887f83d4dc73d028 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 14 Jun 2014 20:47:57 -0500 Subject: [PATCH 3/7] BigInteger: fix unit tests --- tests/Unit/Math/BigInteger/TestCase.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index cdc8b2e5..5b86645e 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -274,12 +274,12 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $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); + $this->assertTrue($rand1->compare($min) > 0); + $this->assertTrue($rand1->compare($max) < 0); $rand2 = $min->random($max); - $this->assertTrue($rand1->compareTo($min) > 0); - $this->assertTrue($rand1->compareTo($max) < 0); + $this->assertTrue($rand1->compare($min) > 0); + $this->assertTrue($rand1->compare($max) < 0); $this->assertFalse($rand1->equals($rand2)); } From 34a971d31707d9908d0699156ab41d08585b4bac Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 14 Jun 2014 23:49:57 -0500 Subject: [PATCH 4/7] BigInteger: change argument names for random / randomPrime --- phpseclib/Math/BigInteger.php | 36 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 04f682a9..2282cdd9 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3094,20 +3094,23 @@ class Math_BigInteger * $generator->random($min, $max) * $min->random($max) * - * @param Integer $min - * @param optional Integer $max + * @param Integer $arg1 + * @param optional Integer $arg2 * @return Math_BigInteger * @access public */ - function random($min, $max = false) + function random($arg1, $arg2 = false) { - if ($min === false) { - return new Math_BigInteger(0); + if ($arg1 === false) { + return false; } - if ($max === false) { - $max = $min->copy(); - $min = $this->copy(); + if ($arg2 === false) { + $max = $arg1; + $min = $this; + } else { + $min = $arg1; + $max = $arg2; } $compare = $max->compare($min); @@ -3170,22 +3173,25 @@ 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 Integer $min - * @param optional Integer $max + * @param Integer $arg1 + * @param optional Integer $arg2 * @param optional Integer $timeout * @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, $max = false, $timeout = false) + function randomPrime($arg1, $arg2 = false, $timeout = false) { - if ($min === false) { + if ($arg1 === false) { return false; } - if ($max === false) { - $max = $min->copy(); - $min = $this->copy(); + if ($arg2 === false) { + $max = $arg1; + $min = $this; + } else { + $min = $arg1; + $max = $arg2; } $compare = $max->compare($min); From 92e6b2352870c37153793256f536ba17fd796de6 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 15 Jun 2014 12:13:56 -0500 Subject: [PATCH 5/7] BigInteger: unit test update --- tests/Unit/Math/BigInteger/TestCase.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Math/BigInteger/TestCase.php b/tests/Unit/Math/BigInteger/TestCase.php index 5b86645e..87addfd4 100644 --- a/tests/Unit/Math/BigInteger/TestCase.php +++ b/tests/Unit/Math/BigInteger/TestCase.php @@ -266,7 +266,7 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase $this->assertSame('18446744073709551616', (string) $y); } - public function testRandom() + public function testRandomTwoArgument() { $min = $this->getInstance(0); $max = $this->getInstance('18446744073709551616'); @@ -276,11 +276,21 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase // chosen it's just not that likely $this->assertTrue($rand1->compare($min) > 0); $this->assertTrue($rand1->compare($max) < 0); + } - $rand2 = $min->random($max); + 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)); } From 0eb0ae7ff7ccfc03920baae93630e93bb158fbaf Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 16 Jun 2014 09:09:26 -0500 Subject: [PATCH 6/7] BigInteger: update comments --- phpseclib/Math/BigInteger.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 2282cdd9..2ae53baf 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3091,13 +3091,15 @@ class Math_BigInteger * 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) + * $max->random($min) * - * @param Integer $arg1 - * @param optional Integer $arg2 + * @param Math_BigInteger $arg1 + * @param optional Math_Integer $arg2 * @return Math_BigInteger * @access public + * @internal The API for creating random numbers used to be $a->random($min, $max), where $a was a Math_BigInteger object. + * That method is still supported for BC purposes. */ function random($arg1, $arg2 = false) { @@ -3173,8 +3175,8 @@ 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 Integer $arg1 - * @param optional Integer $arg2 + * @param Math_BigInteger $arg1 + * @param optional Math_BigInteger $arg2 * @param optional Integer $timeout * @return Mixed * @access public @@ -3303,7 +3305,7 @@ class Math_BigInteger * $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed across multiple pageloads * on a website instead of just one. * - * @param optional Integer $t + * @param optional Math_BigInteger $t * @return Boolean * @access public * @internal Uses the From 4b9eb4af27efa9b0bdc072820bcddeae7aa846fc Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 16 Jun 2014 09:23:34 -0500 Subject: [PATCH 7/7] BigInteger: one more change to the comments --- phpseclib/Math/BigInteger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 2ae53baf..71fe5eef 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -3095,7 +3095,7 @@ class Math_BigInteger * $max->random($min) * * @param Math_BigInteger $arg1 - * @param optional Math_Integer $arg2 + * @param optional Math_BigInteger $arg2 * @return Math_BigInteger * @access public * @internal The API for creating random numbers used to be $a->random($min, $max), where $a was a Math_BigInteger object.