[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/EC/Curves/ -> Curve25519.php (source)

   1  <?php
   2  
   3  /**
   4   * Curve25519
   5   *
   6   * PHP version 5 and 7
   7   *
   8   * @author    Jim Wigginton <terrafrost@php.net>
   9   * @copyright 2019 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\Crypt\EC\Curves;
  15  
  16  use phpseclib3\Crypt\EC\BaseCurves\Montgomery;
  17  use phpseclib3\Math\BigInteger;
  18  
  19  class Curve25519 extends Montgomery
  20  {
  21      public function __construct()
  22      {
  23          // 2^255 - 19
  24          $this->setModulo(new BigInteger('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED', 16));
  25          $this->a24 = $this->factory->newInteger(new BigInteger('121666'));
  26          $this->p = [$this->factory->newInteger(new BigInteger(9))];
  27          // 2^252 + 0x14def9dea2f79cd65812631a5cf5d3ed
  28          $this->setOrder(new BigInteger('1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED', 16));
  29  
  30          /*
  31          $this->setCoefficients(
  32              new BigInteger('486662'), // a
  33          );
  34          $this->setBasePoint(
  35              new BigInteger(9),
  36              new BigInteger('14781619447589544791020593568409986887264606134616475288964881837755586237401')
  37          );
  38          */
  39      }
  40  
  41      /**
  42       * Multiply a point on the curve by a scalar
  43       *
  44       * Modifies the scalar as described at https://tools.ietf.org/html/rfc7748#page-8
  45       *
  46       * @return array
  47       */
  48      public function multiplyPoint(array $p, BigInteger $d)
  49      {
  50          //$r = strrev(sodium_crypto_scalarmult($d->toBytes(), strrev($p[0]->toBytes())));
  51          //return [$this->factory->newInteger(new BigInteger($r, 256))];
  52  
  53          $d = $d->toBytes();
  54          $d &= "\xF8" . str_repeat("\xFF", 30) . "\x7F";
  55          $d = strrev($d);
  56          $d |= "\x40";
  57          $d = new BigInteger($d, -256);
  58  
  59          return parent::multiplyPoint($p, $d);
  60      }
  61  
  62      /**
  63       * Creates a random scalar multiplier
  64       *
  65       * @return BigInteger
  66       */
  67      public function createRandomMultiplier()
  68      {
  69          return BigInteger::random(256);
  70      }
  71  
  72      /**
  73       * Performs range check
  74       */
  75      public function rangeCheck(BigInteger $x)
  76      {
  77          if ($x->getLength() > 256 || $x->isNegative()) {
  78              throw new \RangeException('x must be a positive integer less than 256 bytes in length');
  79          }
  80      }
  81  }