[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Math/ -> PrimeField.php (source)

   1  <?php
   2  
   3  /**
   4   * Prime Finite Fields
   5   *
   6   * Utilizes the factory design pattern
   7   *
   8   * PHP version 5 and 7
   9   *
  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;
  17  
  18  use phpseclib3\Math\Common\FiniteField;
  19  use phpseclib3\Math\PrimeField\Integer;
  20  
  21  /**
  22   * Prime Finite Fields
  23   *
  24   * @author  Jim Wigginton <terrafrost@php.net>
  25   */
  26  class PrimeField extends FiniteField
  27  {
  28      /**
  29       * Instance Counter
  30       *
  31       * @var int
  32       */
  33      private static $instanceCounter = 0;
  34  
  35      /**
  36       * Keeps track of current instance
  37       *
  38       * @var int
  39       */
  40      protected $instanceID;
  41  
  42      /**
  43       * Default constructor
  44       */
  45      public function __construct(BigInteger $modulo)
  46      {
  47          if (!$modulo->isPrime()) {
  48              throw new \UnexpectedValueException('PrimeField requires a prime number be passed to the constructor');
  49          }
  50  
  51          $this->instanceID = self::$instanceCounter++;
  52          Integer::setModulo($this->instanceID, $modulo);
  53          Integer::setRecurringModuloFunction($this->instanceID, $modulo->createRecurringModuloFunction());
  54      }
  55  
  56      /**
  57       * Use a custom defined modular reduction function
  58       *
  59       * @return void
  60       */
  61      public function setReduction(\Closure $func)
  62      {
  63          $this->reduce = $func->bindTo($this, $this);
  64      }
  65  
  66      /**
  67       * Returns an instance of a dynamically generated PrimeFieldInteger class
  68       *
  69       * @return Integer
  70       */
  71      public function newInteger(BigInteger $num)
  72      {
  73          return new Integer($this->instanceID, $num);
  74      }
  75  
  76      /**
  77       * Returns an integer on the finite field between one and the prime modulo
  78       *
  79       * @return Integer
  80       */
  81      public function randomInteger()
  82      {
  83          static $one;
  84          if (!isset($one)) {
  85              $one = new BigInteger(1);
  86          }
  87  
  88          return new Integer($this->instanceID, BigInteger::randomRange($one, Integer::getModulo($this->instanceID)));
  89      }
  90  
  91      /**
  92       * Returns the length of the modulo in bytes
  93       *
  94       * @return int
  95       */
  96      public function getLengthInBytes()
  97      {
  98          return Integer::getModulo($this->instanceID)->getLengthInBytes();
  99      }
 100  
 101      /**
 102       * Returns the length of the modulo in bits
 103       *
 104       * @return int
 105       */
 106      public function getLength()
 107      {
 108          return Integer::getModulo($this->instanceID)->getLength();
 109      }
 110  
 111      /**
 112       *  Destructor
 113       */
 114      public function __destruct()
 115      {
 116          Integer::cleanupCache($this->instanceID);
 117      }
 118  }