Пример #1
0
 /**
 * Exponentiate with or without Chinese Remainder Theorem
 *
 * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.1 RFC3447#section-5.1.2}.
 *
 * @access private
 * @param MathBigInteger $x
 * @return MathBigInteger
 */
 function _exponentiate($x)
 {
     if (empty($this->primes) || empty($this->coefficients) || empty($this->exponents)) {
         return $x->modPow($this->exponent, $this->modulus);
     }
     $num_primes = count($this->primes);
     if (defined('CRYPT_RSA_DISABLE_BLINDING')) {
         $m_i = array(1 => $x->modPow($this->exponents[1], $this->primes[1]), 2 => $x->modPow($this->exponents[2], $this->primes[2]));
         $h = $m_i[1]->subtract($m_i[2]);
         $h = $h->multiply($this->coefficients[2]);
         list(, $h) = $h->divide($this->primes[1]);
         $m = $m_i[2]->add($h->multiply($this->primes[2]));
         $r = $this->primes[1];
         for ($i = 3; $i <= $num_primes; $i++) {
             $m_i = $x->modPow($this->exponents[$i], $this->primes[$i]);
             $r = $r->multiply($this->primes[$i - 1]);
             $h = $m_i->subtract($m);
             $h = $h->multiply($this->coefficients[$i]);
             list(, $h) = $h->divide($this->primes[$i]);
             $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 MathBigInteger(1);
         $one->setRandomGenerator('crypt_random');
         $r = $one->random($one, $smallest->subtract($one));
         $m_i = array(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]);
         list(, $h) = $h->divide($this->primes[1]);
         $m = $m_i[2]->add($h->multiply($this->primes[2]));
         $r = $this->primes[1];
         for ($i = 3; $i <= $num_primes; $i++) {
             $m_i = $this->_blind($x, $r, $i);
             $r = $r->multiply($this->primes[$i - 1]);
             $h = $m_i->subtract($m);
             $h = $h->multiply($this->coefficients[$i]);
             list(, $h) = $h->divide($this->primes[$i]);
             $m = $m->add($r->multiply($h));
         }
     }
     return $m;
 }