[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * secp192r1
   5   *
   6   * This is the NIST P-192 curve
   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\Crypt\EC\Curves;
  17  
  18  use phpseclib3\Crypt\EC\BaseCurves\Prime;
  19  use phpseclib3\Math\BigInteger;
  20  
  21  class secp192r1 extends Prime
  22  {
  23      public function __construct()
  24      {
  25          $modulo = new BigInteger('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF', 16);
  26          $this->setModulo($modulo);
  27  
  28          // algorithm 2.27 from http://diamond.boisestate.edu/~liljanab/MATH308/GuideToECC.pdf#page=66
  29          /* in theory this should be faster than regular modular reductions save for one small issue.
  30             to convert to / from base-2**8 with BCMath you have to call bcmul() and bcdiv() a lot.
  31             to convert to / from base-2**8 with PHP64 you have to call base256_rshift() a lot.
  32             in short, converting to / from base-2**8 is pretty expensive and that expense is
  33             enough to offset whatever else might be gained by a simplified reduction algorithm.
  34             now, if PHP supported unsigned integers things might be different. no bit-shifting
  35             would be required for the PHP engine and it'd be a lot faster. but as is, BigInteger
  36             uses base-2**31 or base-2**26 depending on whether or not the system is has a 32-bit
  37             or a 64-bit OS.
  38          */
  39          /*
  40          $m_length = $this->getLengthInBytes();
  41          $this->setReduction(function($c) use ($m_length) {
  42              $cBytes = $c->toBytes();
  43              $className = $this->className;
  44  
  45              if (strlen($cBytes) > 2 * $m_length) {
  46                  list(, $r) = $c->divide($className::$modulo);
  47                  return $r;
  48              }
  49  
  50              $c = str_pad($cBytes, 48, "\0", STR_PAD_LEFT);
  51              $c = array_reverse(str_split($c, 8));
  52  
  53              $null = "\0\0\0\0\0\0\0\0";
  54              $s1 = new BigInteger($c[2] . $c[1] . $c[0], 256);
  55              $s2 = new BigInteger($null . $c[3] . $c[3], 256);
  56              $s3 = new BigInteger($c[4] . $c[4] . $null, 256);
  57              $s4 = new BigInteger($c[5] . $c[5] . $c[5], 256);
  58  
  59              $r = $s1->add($s2)->add($s3)->add($s4);
  60              while ($r->compare($className::$modulo) >= 0) {
  61                  $r = $r->subtract($className::$modulo);
  62              }
  63  
  64              return $r;
  65          });
  66          */
  67  
  68          $this->setCoefficients(
  69              new BigInteger('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC', 16),
  70              new BigInteger('64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1', 16)
  71          );
  72          $this->setBasePoint(
  73              new BigInteger('188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012', 16),
  74              new BigInteger('07192B95FFC8DA78631011ED6B24CDD573F977A11E794811', 16)
  75          );
  76          $this->setOrder(new BigInteger('FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831', 16));
  77      }
  78  }