.htaccess
9 months ago
Base.php
9 months ago
Binary.php
9 months ago
KoblitzPrime.php
9 months ago
Montgomery.php
9 months ago
Prime.php
9 months ago
TwistedEdwards.php
9 months ago
index.html
9 months ago
web.config
9 months ago
Base.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Curve methods common to all curves |
| 5 | * |
| 6 | * PHP version 5 and 7 |
| 7 | * |
| 8 | * @author Jim Wigginton <terrafrost@php.net> |
| 9 | * @copyright 2017 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 | declare(strict_types=1); |
| 15 | |
| 16 | namespace phpseclib3\Crypt\EC\BaseCurves; |
| 17 | |
| 18 | use phpseclib3\Exception\RangeException; |
| 19 | use phpseclib3\Exception\RuntimeException; |
| 20 | use phpseclib3\Math\BigInteger; |
| 21 | use phpseclib3\Math\FiniteField\Integer; |
| 22 | |
| 23 | /** |
| 24 | * Base |
| 25 | * |
| 26 | * @author Jim Wigginton <terrafrost@php.net> |
| 27 | */ |
| 28 | abstract class Base |
| 29 | { |
| 30 | /** |
| 31 | * The Order |
| 32 | * |
| 33 | * @var BigInteger |
| 34 | */ |
| 35 | protected $order; |
| 36 | |
| 37 | /** |
| 38 | * Finite Field Integer factory |
| 39 | * |
| 40 | * @var Integer |
| 41 | */ |
| 42 | protected $factory; |
| 43 | |
| 44 | /** |
| 45 | * Returns a random integer |
| 46 | * |
| 47 | * @return object |
| 48 | */ |
| 49 | public function randomInteger() |
| 50 | { |
| 51 | return $this->factory->randomInteger(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Converts a BigInteger to a \phpseclib3\Math\FiniteField\Integer integer |
| 56 | * |
| 57 | * @return object |
| 58 | */ |
| 59 | public function convertInteger(BigInteger $x) |
| 60 | { |
| 61 | return $this->factory->newInteger($x); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the length, in bytes, of the modulo |
| 66 | * |
| 67 | * @return integer |
| 68 | */ |
| 69 | public function getLengthInBytes(): int |
| 70 | { |
| 71 | return $this->factory->getLengthInBytes(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the length, in bits, of the modulo |
| 76 | * |
| 77 | * @return integer |
| 78 | */ |
| 79 | public function getLength(): int |
| 80 | { |
| 81 | return $this->factory->getLength(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Multiply a point on the curve by a scalar |
| 86 | * |
| 87 | * Uses the montgomery ladder technique as described here: |
| 88 | * |
| 89 | * https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Montgomery_ladder |
| 90 | * https://github.com/phpecc/phpecc/issues/16#issuecomment-59176772 |
| 91 | */ |
| 92 | public function multiplyPoint(array $p, BigInteger $d): array |
| 93 | { |
| 94 | $alreadyInternal = isset($p[2]); |
| 95 | $r = $alreadyInternal ? |
| 96 | [[], $p] : |
| 97 | [[], $this->convertToInternal($p)]; |
| 98 | |
| 99 | $d = $d->toBits(); |
| 100 | for ($i = 0; $i < strlen($d); $i++) { |
| 101 | $d_i = (int) $d[$i]; |
| 102 | $r[1 - $d_i] = $this->addPoint($r[0], $r[1]); |
| 103 | $r[$d_i] = $this->doublePoint($r[$d_i]); |
| 104 | } |
| 105 | |
| 106 | return $alreadyInternal ? $r[0] : $this->convertToAffine($r[0]); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a random scalar multiplier |
| 111 | */ |
| 112 | public function createRandomMultiplier(): BigInteger |
| 113 | { |
| 114 | static $one; |
| 115 | if (!isset($one)) { |
| 116 | $one = new BigInteger(1); |
| 117 | } |
| 118 | |
| 119 | return BigInteger::randomRange($one, $this->order->subtract($one)); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Performs range check |
| 124 | */ |
| 125 | public function rangeCheck(BigInteger $x): void |
| 126 | { |
| 127 | static $zero; |
| 128 | if (!isset($zero)) { |
| 129 | $zero = new BigInteger(); |
| 130 | } |
| 131 | |
| 132 | if (!isset($this->order)) { |
| 133 | throw new RuntimeException('setOrder needs to be called before this method'); |
| 134 | } |
| 135 | if ($x->compare($this->order) > 0 || $x->compare($zero) <= 0) { |
| 136 | throw new RangeException('x must be between 1 and the order of the curve'); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets the Order |
| 142 | */ |
| 143 | public function setOrder(BigInteger $order): void |
| 144 | { |
| 145 | $this->order = $order; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the Order |
| 150 | */ |
| 151 | public function getOrder(): BigInteger |
| 152 | { |
| 153 | return $this->order; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Use a custom defined modular reduction function |
| 158 | * |
| 159 | * @return object |
| 160 | */ |
| 161 | public function setReduction(callable $func) |
| 162 | { |
| 163 | $this->factory->setReduction($func); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the affine point |
| 168 | * |
| 169 | * @return object[] |
| 170 | */ |
| 171 | public function convertToAffine(array $p): array |
| 172 | { |
| 173 | return $p; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Converts an affine point to a jacobian coordinate |
| 178 | * |
| 179 | * @return object[] |
| 180 | */ |
| 181 | public function convertToInternal(array $p): array |
| 182 | { |
| 183 | return $p; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Negates a point |
| 188 | * |
| 189 | * @return object[] |
| 190 | */ |
| 191 | public function negatePoint(array $p): array |
| 192 | { |
| 193 | $temp = [ |
| 194 | $p[0], |
| 195 | $p[1]->negate(), |
| 196 | ]; |
| 197 | if (isset($p[2])) { |
| 198 | $temp[] = $p[2]; |
| 199 | } |
| 200 | return $temp; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Multiply and Add Points |
| 205 | * |
| 206 | * @return int[] |
| 207 | */ |
| 208 | public function multiplyAddPoints(array $points, array $scalars): array |
| 209 | { |
| 210 | $p1 = $this->convertToInternal($points[0]); |
| 211 | $p2 = $this->convertToInternal($points[1]); |
| 212 | $p1 = $this->multiplyPoint($p1, $scalars[0]); |
| 213 | $p2 = $this->multiplyPoint($p2, $scalars[1]); |
| 214 | $r = $this->addPoint($p1, $p2); |
| 215 | return $this->convertToAffine($r); |
| 216 | } |
| 217 | } |
| 218 |