PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / EC / BaseCurves / Base.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / EC / BaseCurves Last commit date
.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