Subversion Repositories oidplus

Rev

Rev 874 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /**
  4.  * Modular Exponentiation Engine
  5.  *
  6.  * PHP version 5 and 7
  7.  *
  8.  * @author    Jim Wigginton <terrafrost@php.net>
  9.  * @copyright 2017 Jim Wigginton
  10.  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  11.  * @link      http://pear.php.net/package/Math_BigInteger
  12.  */
  13.  
  14. namespace phpseclib3\Math\BigInteger\Engines\BCMath;
  15.  
  16. use phpseclib3\Math\BigInteger\Engines\BCMath;
  17.  
  18. /**
  19.  * Sliding Window Exponentiation Engine
  20.  *
  21.  * @author  Jim Wigginton <terrafrost@php.net>
  22.  */
  23. abstract class Base extends BCMath
  24. {
  25.     /**
  26.      * Cache constants
  27.      *
  28.      * $cache[self::VARIABLE] tells us whether or not the cached data is still valid.
  29.      *
  30.      */
  31.     const VARIABLE = 0;
  32.     /**
  33.      * $cache[self::DATA] contains the cached data.
  34.      *
  35.      */
  36.     const DATA = 1;
  37.  
  38.     /**
  39.      * Test for engine validity
  40.      *
  41.      * @return bool
  42.      */
  43.     public static function isValidEngine()
  44.     {
  45.         return static::class != __CLASS__;
  46.     }
  47.  
  48.     /**
  49.      * Performs modular exponentiation.
  50.      *
  51.      * @param BCMath $x
  52.      * @param BCMath $e
  53.      * @param BCMath $n
  54.      * @param string $class
  55.      * @return BCMath
  56.      */
  57.     protected static function powModHelper(BCMath $x, BCMath $e, BCMath $n, $class)
  58.     {
  59.         if (empty($e->value)) {
  60.             $temp = new $class();
  61.             $temp->value = '1';
  62.             return $x->normalize($temp);
  63.         }
  64.  
  65.         return $x->normalize(static::slidingWindow($x, $e, $n, $class));
  66.     }
  67.  
  68.     /**
  69.      * Modular reduction preparation
  70.      *
  71.      * @param string $x
  72.      * @param string $n
  73.      * @param string $class
  74.      * @see self::slidingWindow()
  75.      * @return string
  76.      */
  77.     protected static function prepareReduce($x, $n, $class)
  78.     {
  79.         return static::reduce($x, $n);
  80.     }
  81.  
  82.     /**
  83.      * Modular multiply
  84.      *
  85.      * @param string $x
  86.      * @param string $y
  87.      * @param string $n
  88.      * @param string $class
  89.      * @see self::slidingWindow()
  90.      * @return string
  91.      */
  92.     protected static function multiplyReduce($x, $y, $n, $class)
  93.     {
  94.         return static::reduce(bcmul($x, $y), $n);
  95.     }
  96.  
  97.     /**
  98.      * Modular square
  99.      *
  100.      * @param string $x
  101.      * @param string $n
  102.      * @param string $class
  103.      * @see self::slidingWindow()
  104.      * @return string
  105.      */
  106.     protected static function squareReduce($x, $n, $class)
  107.     {
  108.         return static::reduce(bcmul($x, $x), $n);
  109.     }
  110. }
  111.