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 / TwistedEdwards.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
TwistedEdwards.php
216 lines
1 <?php
2
3 /**
4 * Curves over a*x^2 + y^2 = 1 + d*x^2*y^2
5 *
6 * http://www.secg.org/SEC2-Ver-1.0.pdf provides for curves with custom parameters.
7 * ie. the coefficients can be arbitrary set through specially formatted keys, etc.
8 * As such, Prime.php is built very generically and it's not able to take full
9 * advantage of curves with 0 coefficients to produce simplified point doubling,
10 * point addition. Twisted Edwards curves, in contrast, do not have a way, currently,
11 * to customize them. As such, we can omit the super generic stuff from this class
12 * and let the named curves (Ed25519 and Ed448) define their own custom tailored
13 * point addition and point doubling methods.
14 *
15 * More info:
16 *
17 * https://en.wikipedia.org/wiki/Twisted_Edwards_curve
18 *
19 * PHP version 5 and 7
20 *
21 * @author Jim Wigginton <terrafrost@php.net>
22 * @copyright 2017 Jim Wigginton
23 * @license http://www.opensource.org/licenses/mit-license.html MIT License
24 * @link http://pear.php.net/package/Math_BigInteger
25 */
26
27 declare(strict_types=1);
28
29 namespace phpseclib3\Crypt\EC\BaseCurves;
30
31 use phpseclib3\Exception\RuntimeException;
32 use phpseclib3\Exception\UnexpectedValueException;
33 use phpseclib3\Math\BigInteger;
34 use phpseclib3\Math\PrimeField;
35 use phpseclib3\Math\PrimeField\Integer as PrimeInteger;
36
37 /**
38 * Curves over a*x^2 + y^2 = 1 + d*x^2*y^2
39 *
40 * @author Jim Wigginton <terrafrost@php.net>
41 */
42 class TwistedEdwards extends Base
43 {
44 /**
45 * The modulo
46 *
47 * @var BigInteger
48 */
49 protected $modulo;
50
51 /**
52 * Cofficient for x^2
53 *
54 * @var object
55 */
56 protected $a;
57
58 /**
59 * Cofficient for x^2*y^2
60 *
61 * @var object
62 */
63 protected $d;
64
65 /**
66 * Base Point
67 *
68 * @var object[]
69 */
70 protected $p;
71
72 /**
73 * The number zero over the specified finite field
74 *
75 * @var object
76 */
77 protected $zero;
78
79 /**
80 * The number one over the specified finite field
81 *
82 * @var object
83 */
84 protected $one;
85
86 /**
87 * The number two over the specified finite field
88 *
89 * @var object
90 */
91 protected $two;
92
93 /**
94 * Sets the modulo
95 */
96 public function setModulo(BigInteger $modulo): void
97 {
98 $this->modulo = $modulo;
99 $this->factory = new PrimeField($modulo);
100 $this->zero = $this->factory->newInteger(new BigInteger(0));
101 $this->one = $this->factory->newInteger(new BigInteger(1));
102 $this->two = $this->factory->newInteger(new BigInteger(2));
103 }
104
105 /**
106 * Set coefficients a and b
107 */
108 public function setCoefficients(BigInteger $a, BigInteger $d): void
109 {
110 if (!isset($this->factory)) {
111 throw new RuntimeException('setModulo needs to be called before this method');
112 }
113 $this->a = $this->factory->newInteger($a);
114 $this->d = $this->factory->newInteger($d);
115 }
116
117 /**
118 * Set x and y coordinates for the base point
119 */
120 public function setBasePoint($x, $y): void
121 {
122 switch (true) {
123 case !$x instanceof BigInteger && !$x instanceof PrimeInteger:
124 throw new UnexpectedValueException('Argument 1 passed to Prime::setBasePoint() must be an instance of either BigInteger or PrimeField\Integer');
125 case !$y instanceof BigInteger && !$y instanceof PrimeInteger:
126 throw new UnexpectedValueException('Argument 2 passed to Prime::setBasePoint() must be an instance of either BigInteger or PrimeField\Integer');
127 }
128 if (!isset($this->factory)) {
129 throw new RuntimeException('setModulo needs to be called before this method');
130 }
131 $this->p = [
132 $x instanceof BigInteger ? $this->factory->newInteger($x) : $x,
133 $y instanceof BigInteger ? $this->factory->newInteger($y) : $y,
134 ];
135 }
136
137 /**
138 * Returns the a coefficient
139 *
140 * @return PrimeInteger
141 */
142 public function getA()
143 {
144 return $this->a;
145 }
146
147 /**
148 * Returns the a coefficient
149 *
150 * @return PrimeInteger
151 */
152 public function getD()
153 {
154 return $this->d;
155 }
156
157 /**
158 * Retrieve the base point as an array
159 */
160 public function getBasePoint(): array
161 {
162 if (!isset($this->factory)) {
163 throw new RuntimeException('setModulo needs to be called before this method');
164 }
165 /*
166 if (!isset($this->p)) {
167 throw new \phpseclib3\Exception\RuntimeException('setBasePoint needs to be called before this method');
168 }
169 */
170 return $this->p;
171 }
172
173 /**
174 * Returns the affine point
175 *
176 * @return PrimeInteger[]
177 */
178 public function convertToAffine(array $p): array
179 {
180 if (!isset($p[2])) {
181 return $p;
182 }
183 [$x, $y, $z] = $p;
184 $z = $this->one->divide($z);
185 return [
186 $x->multiply($z),
187 $y->multiply($z),
188 ];
189 }
190
191 /**
192 * Returns the modulo
193 */
194 public function getModulo(): BigInteger
195 {
196 return $this->modulo;
197 }
198
199 /**
200 * Tests whether or not the x / y values satisfy the equation
201 *
202 * @return boolean
203 */
204 public function verifyPoint(array $p): bool
205 {
206 [$x, $y] = $p;
207 $x2 = $x->multiply($x);
208 $y2 = $y->multiply($y);
209
210 $lhs = $this->a->multiply($x2)->add($y2);
211 $rhs = $this->d->multiply($x2)->multiply($y2)->add($this->one);
212
213 return $lhs->equals($rhs);
214 }
215 }
216