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