[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Curve448
   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 Curve448 extends Montgomery
  20  {
  21      public function __construct()
  22      {
  23          // 2^448 - 2^224 - 1
  24          $this->setModulo(new BigInteger(
  25              'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE' .
  26              'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
  27              16
  28          ));
  29          $this->a24 = $this->factory->newInteger(new BigInteger('39081'));
  30          $this->p = [$this->factory->newInteger(new BigInteger(5))];
  31          // 2^446 - 0x8335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d
  32          $this->setOrder(new BigInteger(
  33              '3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' .
  34              '7CCA23E9C44EDB49AED63690216CC2728DC58F552378C292AB5844F3',
  35              16
  36          ));
  37  
  38          /*
  39          $this->setCoefficients(
  40              new BigInteger('156326'), // a
  41          );
  42          $this->setBasePoint(
  43              new BigInteger(5),
  44              new BigInteger(
  45                  '355293926785568175264127502063783334808976399387714271831880898' .
  46                  '435169088786967410002932673765864550910142774147268105838985595290' .
  47                  '606362')
  48          );
  49          */
  50      }
  51  
  52      /**
  53       * Multiply a point on the curve by a scalar
  54       *
  55       * Modifies the scalar as described at https://tools.ietf.org/html/rfc7748#page-8
  56       *
  57       * @return array
  58       */
  59      public function multiplyPoint(array $p, BigInteger $d)
  60      {
  61          //$r = strrev(sodium_crypto_scalarmult($d->toBytes(), strrev($p[0]->toBytes())));
  62          //return [$this->factory->newInteger(new BigInteger($r, 256))];
  63  
  64          $d = $d->toBytes();
  65          $d[0] = $d[0] & "\xFC";
  66          $d = strrev($d);
  67          $d |= "\x80";
  68          $d = new BigInteger($d, 256);
  69  
  70          return parent::multiplyPoint($p, $d);
  71      }
  72  
  73      /**
  74       * Creates a random scalar multiplier
  75       *
  76       * @return BigInteger
  77       */
  78      public function createRandomMultiplier()
  79      {
  80          return BigInteger::random(446);
  81      }
  82  
  83      /**
  84       * Performs range check
  85       */
  86      public function rangeCheck(BigInteger $x)
  87      {
  88          if ($x->getLength() > 448 || $x->isNegative()) {
  89              throw new \RangeException('x must be a positive integer less than 446 bytes in length');
  90          }
  91      }
  92  }