Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /**
  4.  * PHP Power of Two Modular Exponentiation Engine
  5.  *
  6.  * PHP version 5 and 7
  7.  *
  8.  * @category  Math
  9.  * @package   BigInteger
  10.  * @author    Jim Wigginton <terrafrost@php.net>
  11.  * @copyright 2017 Jim Wigginton
  12.  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  13.  * @link      http://pear.php.net/package/Math_BigInteger
  14.  */
  15.  
  16. namespace phpseclib3\Math\BigInteger\Engines\PHP\Reductions;
  17.  
  18. use phpseclib3\Math\BigInteger\Engines\PHP\Base;
  19.  
  20. /**
  21.  * PHP Power Of Two Modular Exponentiation Engine
  22.  *
  23.  * @package PHP
  24.  * @author  Jim Wigginton <terrafrost@php.net>
  25.  * @access  public
  26.  */
  27. abstract class PowerOfTwo extends Base
  28. {
  29.     /**
  30.      * Prepare a number for use in Montgomery Modular Reductions
  31.      *
  32.      * @param array $x
  33.      * @param array $n
  34.      * @param string $class
  35.      * @return array
  36.      */
  37.     protected static function prepareReduce(array $x, array $n, $class)
  38.     {
  39.         return self::reduce($x, $n, $class);
  40.     }
  41.  
  42.     /**
  43.      * Power Of Two Reduction
  44.      *
  45.      * @param array $x
  46.      * @param array $n
  47.      * @param string $class
  48.      * @return array
  49.      */
  50.     protected static function reduce(array $x, array $n, $class)
  51.     {
  52.         $lhs = new $class();
  53.         $lhs->value = $x;
  54.         $rhs = new $class();
  55.         $rhs->value = $n;
  56.  
  57.         $temp = new $class();
  58.         $temp->value = [1];
  59.  
  60.         $result = $lhs->bitwise_and($rhs->subtract($temp));
  61.         return $result->value;
  62.     }
  63. }
  64.