1
0
mirror of https://github.com/danog/tgseclib.git synced 2025-01-21 13:31:13 +01:00

Merge branch 'biginteger-revamp'

This commit is contained in:
terrafrost 2016-01-16 22:59:19 -06:00
commit ad4c3670d3
4 changed files with 233 additions and 277 deletions

View File

@ -512,15 +512,13 @@ class RSA
$finalMax = $max; $finalMax = $max;
extract(self::_generateMinMax($temp)); extract(self::_generateMinMax($temp));
$generator = new BigInteger(); $n = clone self::$one;
$n = self::$one->copy();
if (!empty($partial)) { if (!empty($partial)) {
extract(unserialize($partial)); extract(unserialize($partial));
} else { } else {
$exponents = $coefficients = $primes = array(); $exponents = $coefficients = $primes = array();
$lcm = array( $lcm = array(
'top' => self::$one->copy(), 'top' => clone self::$one,
'bottom' => false 'bottom' => false
); );
} }
@ -552,9 +550,9 @@ class RSA
if (!$temp->equals(self::$zero)) { if (!$temp->equals(self::$zero)) {
$min = $min->add(self::$one); // ie. ceil() $min = $min->add(self::$one); // ie. ceil()
} }
$primes[$i] = $generator->randomPrime($min, $finalMax, $timeout); $primes[$i] = BigInteger::randomPrime($min, $finalMax, $timeout);
} else { } else {
$primes[$i] = $generator->randomPrime($min, $max, $timeout); $primes[$i] = BigInteger::randomPrime($min, $max, $timeout);
} }
if ($primes[$i] === false) { // if we've reached the timeout if ($primes[$i] === false) { // if we've reached the timeout
@ -703,13 +701,13 @@ class RSA
} }
if (is_object($key->modulus)) { if (is_object($key->modulus)) {
$this->modulus = $key->modulus->copy(); $this->modulus = clone $key->modulus;
} }
if (is_object($key->exponent)) { if (is_object($key->exponent)) {
$this->exponent = $key->exponent->copy(); $this->exponent = clone $key->exponent;
} }
if (is_object($key->publicExponent)) { if (is_object($key->publicExponent)) {
$this->publicExponent = $key->publicExponent->copy(); $this->publicExponent = clone $key->publicExponent;
} }
$this->primes = array(); $this->primes = array();
@ -717,13 +715,13 @@ class RSA
$this->coefficients = array(); $this->coefficients = array();
foreach ($this->primes as $prime) { foreach ($this->primes as $prime) {
$this->primes[] = $prime->copy(); $this->primes[] = clone $prime;
} }
foreach ($this->exponents as $exponent) { foreach ($this->exponents as $exponent) {
$this->exponents[] = $exponent->copy(); $this->exponents[] = clone $exponent;
} }
foreach ($this->coefficients as $coefficient) { foreach ($this->coefficients as $coefficient) {
$this->coefficients[] = $coefficient->copy(); $this->coefficients[] = clone $coefficient;
} }
return true; return true;
@ -1370,7 +1368,7 @@ class RSA
} }
} }
$r = self::$one->random(self::$one, $smallest->subtract(self::$one)); $r = BigInteger::random(self::$one, $smallest->subtract(self::$one));
$m_i = array( $m_i = array(
1 => $this->_blind($x, $r, 1), 1 => $this->_blind($x, $r, 1),

File diff suppressed because it is too large Load Diff

View File

@ -1495,7 +1495,7 @@ class SSH2
$max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength $max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength
$max = $max->subtract($one); $max = $max->subtract($one);
$x = $one->random($one, $max); $x = BigInteger::random($one, $max);
$e = $g->modPow($x, $prime); $e = $g->modPow($x, $prime);
$eBytes = $e->toBytes(true); $eBytes = $e->toBytes(true);

View File

@ -217,6 +217,11 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$z = $this->getInstance('11111111111111111111111', 16); $z = $this->getInstance('11111111111111111111111', 16);
$this->assertSame($z->toHex(), $x->bitwise_NOT()->toHex()); $this->assertSame($z->toHex(), $x->bitwise_NOT()->toHex());
$a = $this->getInstance(0);
$a->bitwise_not();
$this->assertSame($a->toString(), '0');
} }
public function testBitwiseLeftShift() public function testBitwiseLeftShift()
@ -268,29 +273,13 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$min = $this->getInstance(0); $min = $this->getInstance(0);
$max = $this->getInstance('18446744073709551616'); $max = $this->getInstance('18446744073709551616');
$rand1 = $min->random($min, $max); $rand1 = \phpseclib\Math\BigInteger::random($min, $max);
// technically $rand1 can equal $min but with the $min and $max we've // technically $rand1 can equal $min but with the $min and $max we've
// chosen it's just not that likely // chosen it's just not that likely
$this->assertTrue($rand1->compare($min) > 0); $this->assertTrue($rand1->compare($min) > 0);
$this->assertTrue($rand1->compare($max) < 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
*/ */
@ -326,8 +315,8 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
Code for generation of $alicePrivate and $bobPrivate. Code for generation of $alicePrivate and $bobPrivate.
$one = $this->getInstance(1); $one = $this->getInstance(1);
$max = $one->bitwise_leftShift(512)->subtract($one); $max = $one->bitwise_leftShift(512)->subtract($one);
$alicePrivate = $one->random($one, $max); $alicePrivate = \phpseclib\Math\BigInteger::random($one, $max);
$bobPrivate = $one->random($one, $max); $bobPrivate = \phpseclib\Math\BigInteger::random($one, $max);
var_dump($alicePrivate->toHex(), $bobPrivate->toHex()); var_dump($alicePrivate->toHex(), $bobPrivate->toHex());
*/ */
@ -364,6 +353,20 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$num = $this->getInstance(50); $num = $this->getInstance(50);
$str = print_r($num, true); $str = print_r($num, true);
$this->assertContains('[value] => 0x32', $str); $this->assertContains('[value] => 0x32', $str);
return $str; }
public function testPrecision()
{
$a = $this->getInstance(51);
$this->assertSame($a->getPrecision(), -1);
$b = $a;
$c = clone $a;
$b->setPrecision(1);
$this->assertSame($a->getPrecision(), 1);
$this->assertSame("$a", '1');
$this->assertSame($b->getPrecision(), 1);
$this->assertSame("$b", '1');
$this->assertSame($c->getPrecision(), -1);
$this->assertSame("$c", '51');
} }
} }