1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-11-27 04:34:45 +01:00

- the triple equals in Math_BigIinteger::equals() made it so float(1) != int(1), so all instances where float(1) might occur have been removed

- Crypt_RSA::_blind() should now be faster.


git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@95 21d32557-59b3-4da0-833f-c5933fad653e
This commit is contained in:
Jim Wigginton 2010-03-01 17:28:19 +00:00
parent c0b0886762
commit 1539cd4a01
2 changed files with 33 additions and 26 deletions

View File

@ -62,7 +62,7 @@
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMIX Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: RSA.php,v 1.13 2010-02-28 06:57:00 terrafrost Exp $
* @version $Id: RSA.php,v 1.14 2010-03-01 17:28:19 terrafrost Exp $
* @link http://phpseclib.sourceforge.net
*/
@ -504,6 +504,7 @@ class Crypt_RSA {
));
}
}
if ($i == $num_primes) {
list($min, $temp) = $absoluteMin->divide($n);
if (!$temp->equals($this->zero)) {
@ -1311,9 +1312,21 @@ class Crypt_RSA {
$m = $m->add($r->multiply($h));
}
} else {
$smallest = $this->primes[1];
for ($i = 2; $i <= $num_primes; $i++) {
if ($smallest->compare($this->primes[$i]) > 0) {
$smallest = $this->primes[$i];
}
}
$one = new Math_BigInteger(1);
$one->setRandomGenerator('crypt_random');
$r = $one->random($one, $smallest->subtract($one));
$m_i = array(
1 => $this->_blind($x, 1),
2 => $this->_blind($x, 2)
1 => $this->_blind($x, $r, 1),
2 => $this->_blind($x, $r, 2)
);
$h = $m_i[1]->subtract($m_i[2]);
$h = $h->multiply($this->coefficients[2]);
@ -1322,7 +1335,7 @@ class Crypt_RSA {
$r = $this->primes[1];
for ($i = 3; $i <= $num_primes; $i++) {
$m_i = $this->_blind($x, $i);
$m_i = $this->_blind($x, $r, $i);
$r = $r->multiply($this->primes[$i - 1]);
@ -1345,18 +1358,12 @@ class Crypt_RSA {
*
* @access private
* @param Math_BigInteger $x
* @param Math_BigInteger $r
* @param Integer $i
* @return Math_BigInteger
*/
function _blind($x, $i)
function _blind($x, $r, $i)
{
static $one;
if (!isset($one)) {
$one = new Math_BigInteger(1);
$one->setRandomGenerator('crypt_random');
}
$r = $one->random($one, $this->primes[$i]->subtract($one));
$x = $x->multiply($r->modPow($this->publicExponent, $this->primes[$i]));
$x = $x->modPow($this->exponents[$i], $this->primes[$i]);

View File

@ -67,7 +67,7 @@
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVI Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: BigInteger.php,v 1.30 2010-02-26 03:40:26 terrafrost Exp $
* @version $Id: BigInteger.php,v 1.31 2010-03-01 17:28:19 terrafrost Exp $
* @link http://pear.php.net/package/Math_BigInteger
*/
@ -862,7 +862,7 @@ class Math_BigInteger {
$temp = (int) ($sum / 0x4000000);
$value[$i] = $sum - 0x4000000 * $temp; // eg. a faster alternative to fmod($sum, 0x4000000)
$value[$i] = (int) ($sum - 0x4000000 * $temp); // eg. a faster alternative to fmod($sum, 0x4000000)
$value[$j] = $temp;
}
@ -998,7 +998,7 @@ class Math_BigInteger {
$temp = (int) ($sum / 0x4000000);
$x_value[$i] = $sum - 0x4000000 * $temp;
$x_value[$i] = (int) ($sum - 0x4000000 * $temp);
$x_value[$j] = $temp;
}
@ -1145,7 +1145,7 @@ class Math_BigInteger {
for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0
$temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
$carry = (int) ($temp / 0x4000000);
$product_value[$j] = $temp - 0x4000000 * $carry;
$product_value[$j] = (int) ($temp - 0x4000000 * $carry);
}
$product_value[$j] = $carry;
@ -1158,7 +1158,7 @@ class Math_BigInteger {
for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) {
$temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
$carry = (int) ($temp / 0x4000000);
$product_value[$k] = $temp - 0x4000000 * $carry;
$product_value[$k] = (int) ($temp - 0x4000000 * $carry);
}
$product_value[$k] = $carry;
@ -1246,13 +1246,13 @@ class Math_BigInteger {
$temp = $square_value[$i2] + $value[$i] * $value[$i];
$carry = (int) ($temp / 0x4000000);
$square_value[$i2] = $temp - 0x4000000 * $carry;
$square_value[$i2] = (int) ($temp - 0x4000000 * $carry);
// note how we start from $i+1 instead of 0 as we do in multiplication.
for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) {
$temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry;
$carry = (int) ($temp / 0x4000000);
$square_value[$k] = $temp - 0x4000000 * $carry;
$square_value[$k] = (int) ($temp - 0x4000000 * $carry);
}
// the following line can yield values larger 2**15. at this point, PHP should switch
@ -1520,7 +1520,7 @@ class Math_BigInteger {
for ($i = count($dividend) - 1; $i >= 0; --$i) {
$temp = 0x4000000 * $carry + $dividend[$i];
$result[$i] = (int) ($temp / $divisor);
$carry = $temp - $divisor * $result[$i];
$carry = (int) ($temp - $divisor * $result[$i]);
}
return array($result, $carry);
@ -2090,7 +2090,7 @@ class Math_BigInteger {
for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0, $k = $i
$temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
$carry = (int) ($temp / 0x4000000);
$product_value[$j] = $temp - 0x4000000 * $carry;
$product_value[$j] = (int) ($temp - 0x4000000 * $carry);
}
if ($j < $stop) {
@ -2106,7 +2106,7 @@ class Math_BigInteger {
for ($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k) {
$temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
$carry = (int) ($temp / 0x4000000);
$product_value[$k] = $temp - 0x4000000 * $carry;
$product_value[$k] = (int) ($temp - 0x4000000 * $carry);
}
if ($k < $stop) {
@ -2154,7 +2154,7 @@ class Math_BigInteger {
for ($i = 0; $i < $k; ++$i) {
$temp = $result[MATH_BIGINTEGER_VALUE][$i] * $cache[MATH_BIGINTEGER_DATA][$key];
$temp = $temp - 0x4000000 * ((int) ($temp / 0x4000000));
$temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000)));
$temp = $this->_regularMultiply(array($temp), $n);
$temp = array_merge($this->_array_repeat(0, $i), $temp);
$result = $this->_add($result[MATH_BIGINTEGER_VALUE], false, $temp, false);
@ -2206,9 +2206,9 @@ class Math_BigInteger {
$a = array(MATH_BIGINTEGER_VALUE => $this->_array_repeat(0, $n + 1));
for ($i = 0; $i < $n; ++$i) {
$temp = $a[MATH_BIGINTEGER_VALUE][0] + $x[$i] * $y[0];
$temp = $temp - 0x4000000 * ((int) ($temp / 0x4000000));
$temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000)));
$temp = $temp * $cache[MATH_BIGINTEGER_DATA][$key];
$temp = $temp - 0x4000000 * ((int) ($temp / 0x4000000));
$temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000)));
$temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false);
$a = $this->_add($a[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false);
$a[MATH_BIGINTEGER_VALUE] = array_slice($a[MATH_BIGINTEGER_VALUE], 1);
@ -3316,7 +3316,7 @@ class Math_BigInteger {
for ($i = 0; $i < count($this->value); ++$i) {
$temp = $this->value[$i] * $shift + $carry;
$carry = (int) ($temp / 0x4000000);
$this->value[$i] = $temp - $carry * 0x4000000;
$this->value[$i] = (int) ($temp - $carry * 0x4000000);
}
if ( $carry ) {