|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jose\Component\Core\Util\Ecc\Primitives; |
|
4
|
|
|
|
|
5
|
|
|
use Jose\Component\Core\Util\Ecc\Math\GmpMath; |
|
6
|
|
|
use Jose\Component\Core\Util\Ecc\Math\ModularArithmetic; |
|
7
|
|
|
use Jose\Component\Core\Util\Ecc\Util\BinaryString; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* ********************************************************************* |
|
11
|
|
|
* Copyright (C) 2012 Matyas Danter |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
14
|
|
|
* a copy of this software and associated documentation files (the "Software"), |
|
15
|
|
|
* to deal in the Software without restriction, including without limitation |
|
16
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
17
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
|
18
|
|
|
* Software is furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included |
|
21
|
|
|
* in all copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
24
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
26
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
|
27
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|
28
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
29
|
|
|
* OTHER DEALINGS IN THE SOFTWARE. |
|
30
|
|
|
* *********************************************************************** |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* This class is where the elliptic curve arithmetic takes place. |
|
35
|
|
|
* The important methods are: |
|
36
|
|
|
* - add: adds two points according to ec arithmetic |
|
37
|
|
|
* - double: doubles a point on the ec field mod p |
|
38
|
|
|
* - mul: uses double and add to achieve multiplication The rest of the methods are there for supporting the ones above. |
|
39
|
|
|
*/ |
|
40
|
|
|
class Point |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var CurveFp |
|
44
|
|
|
*/ |
|
45
|
|
|
private $curve; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var GmpMath |
|
49
|
|
|
*/ |
|
50
|
|
|
private $adapter; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var ModularArithmetic |
|
54
|
|
|
*/ |
|
55
|
|
|
private $modAdapter; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var \GMP |
|
59
|
|
|
*/ |
|
60
|
|
|
private $x; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var \GMP |
|
64
|
|
|
*/ |
|
65
|
|
|
private $y; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var \GMP |
|
69
|
|
|
*/ |
|
70
|
|
|
private $order; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var bool |
|
74
|
|
|
*/ |
|
75
|
|
|
private $infinity = false; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Initialize a new instance |
|
79
|
|
|
* |
|
80
|
|
|
* @param GmpMath $adapter |
|
81
|
|
|
* @param CurveFp $curve |
|
82
|
|
|
* @param \GMP $x |
|
83
|
|
|
* @param \GMP $y |
|
84
|
|
|
* @param \GMP $order |
|
85
|
|
|
* @param bool $infinity |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \RuntimeException when either the curve does not contain the given coordinates or |
|
88
|
|
|
* when order is not null and P(x, y) * order is not equal to infinity. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct(GmpMath $adapter, CurveFp $curve, \GMP $x, \GMP $y, \GMP $order = null, $infinity = false) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->adapter = $adapter; |
|
93
|
|
|
$this->modAdapter = $curve->getModAdapter(); |
|
94
|
|
|
$this->curve = $curve; |
|
95
|
|
|
$this->x = $x; |
|
96
|
|
|
$this->y = $y; |
|
97
|
|
|
$this->order = $order !== null ? $order : gmp_init(0, 10); |
|
98
|
|
|
$this->infinity = (bool) $infinity; |
|
99
|
|
|
if (! $infinity && ! $curve->contains($x, $y)) { |
|
100
|
|
|
throw new \RuntimeException("Curve " . $curve . " does not contain point (" . $adapter->toString($x) . ", " . $adapter->toString($y) . ")"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (!is_null($order)) { |
|
104
|
|
|
$mul = $this->mul($order); |
|
105
|
|
|
if (!$mul->isInfinity()) { |
|
106
|
|
|
throw new \RuntimeException("SELF * ORDER MUST EQUAL INFINITY. (" . (string)$mul . " found instead)"); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return GmpMath |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getAdapter() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->adapter; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritDoc} |
|
121
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::isInfinity() |
|
122
|
|
|
*/ |
|
123
|
|
|
public function isInfinity() |
|
124
|
|
|
{ |
|
125
|
|
|
return (bool) $this->infinity; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritDoc} |
|
130
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::getCurve() |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getCurve() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->curve; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritDoc} |
|
139
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::getOrder() |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getOrder() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->order; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* {@inheritDoc} |
|
148
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::getX() |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getX() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->x; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritDoc} |
|
157
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::getY() |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getY() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->y; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritDoc} |
|
166
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::add() |
|
167
|
|
|
* @return self |
|
168
|
|
|
*/ |
|
169
|
|
|
public function add(Point $addend) |
|
170
|
|
|
{ |
|
171
|
|
|
if (! $this->curve->equals($addend->getCurve())) { |
|
172
|
|
|
throw new \RuntimeException("The Elliptic Curves do not match."); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($addend->isInfinity()) { |
|
176
|
|
|
return clone $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($this->isInfinity()) { |
|
180
|
|
|
return clone $addend; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$math = $this->adapter; |
|
184
|
|
|
$modMath = $this->modAdapter; |
|
185
|
|
|
|
|
186
|
|
|
if ($math->equals($addend->getX(), $this->x)) { |
|
187
|
|
|
if ($math->equals($addend->getY(), $this->y)) { |
|
188
|
|
|
return $this->getDouble(); |
|
189
|
|
|
} else { |
|
190
|
|
|
return $this->curve->getInfinity(); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$slope = $modMath->div( |
|
195
|
|
|
$math->sub($addend->getY(), $this->y), |
|
196
|
|
|
$math->sub($addend->getX(), $this->x) |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
$xR = $modMath->sub( |
|
200
|
|
|
$math->sub($math->pow($slope, 2), $this->x), |
|
201
|
|
|
$addend->getX() |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
|
|
$yR = $modMath->sub( |
|
205
|
|
|
$math->mul($slope, $math->sub($this->x, $xR)), |
|
206
|
|
|
$this->y |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
return $this->curve->getPoint($xR, $yR, $this->order); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* {@inheritDoc} |
|
214
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::cmp() |
|
215
|
|
|
*/ |
|
216
|
|
|
public function cmp(Point $other) |
|
217
|
|
|
{ |
|
218
|
|
|
if ($other->isInfinity() && $this->isInfinity()) { |
|
219
|
|
|
return 0; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if ($other->isInfinity() || $this->isInfinity()) { |
|
223
|
|
|
return 1; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
$math = $this->adapter; |
|
227
|
|
|
$equal = ($math->equals($this->x, $other->getX())); |
|
228
|
|
|
$equal &= ($math->equals($this->y, $other->getY())); |
|
229
|
|
|
$equal &= $this->isInfinity() == $other->isInfinity(); |
|
230
|
|
|
$equal &= $this->curve->equals($other->getCurve()); |
|
231
|
|
|
|
|
232
|
|
|
if ($equal) { |
|
233
|
|
|
return 0; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return 1; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritDoc} |
|
241
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::equals() |
|
242
|
|
|
*/ |
|
243
|
|
|
public function equals(Point $other) |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->cmp($other) == 0; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* {@inheritDoc} |
|
250
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::mul() |
|
251
|
|
|
*/ |
|
252
|
|
|
public function mul(\GMP $n) |
|
253
|
|
|
{ |
|
254
|
|
|
if ($this->isInfinity()) { |
|
255
|
|
|
return $this->curve->getInfinity(); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$zero = gmp_init(0, 10); |
|
259
|
|
|
if ($this->adapter->cmp($this->order, $zero) > 0) { |
|
260
|
|
|
$n = $this->adapter->mod($n, $this->order); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if ($this->adapter->equals($n, $zero)) { |
|
|
|
|
|
|
264
|
|
|
return $this->curve->getInfinity(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** @var Point[] $r */ |
|
268
|
|
|
$r = [ |
|
269
|
|
|
$this->curve->getInfinity(), |
|
270
|
|
|
clone $this |
|
271
|
|
|
]; |
|
272
|
|
|
|
|
273
|
|
|
$k = $this->curve->getSize(); |
|
274
|
|
|
$n = str_pad($this->adapter->baseConvert($this->adapter->toString($n), 10, 2), $k, '0', STR_PAD_LEFT); |
|
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
for ($i = 0; $i < $k; $i++) { |
|
277
|
|
|
$j = $n[$i]; |
|
278
|
|
|
|
|
279
|
|
|
$this->cswap($r[0], $r[1], $j ^ 1); |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
$r[0] = $r[0]->add($r[1]); |
|
282
|
|
|
$r[1] = $r[1]->getDouble(); |
|
283
|
|
|
|
|
284
|
|
|
$this->cswap($r[0], $r[1], $j ^ 1); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$r[0]->validate(); |
|
288
|
|
|
|
|
289
|
|
|
return $r[0]; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param Point $a |
|
294
|
|
|
* @param Point $b |
|
295
|
|
|
* @param int $cond |
|
296
|
|
|
*/ |
|
297
|
|
|
private function cswap(self $a, self $b, $cond) |
|
298
|
|
|
{ |
|
299
|
|
|
$this->cswapValue($a->x, $b->x, $cond); |
|
300
|
|
|
$this->cswapValue($a->y, $b->y, $cond); |
|
301
|
|
|
$this->cswapValue($a->order, $b->order, $cond); |
|
302
|
|
|
$this->cswapValue($a->infinity, $b->infinity, $cond); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param $a |
|
307
|
|
|
* @param $b |
|
308
|
|
|
* @param $cond |
|
309
|
|
|
*/ |
|
310
|
|
|
public function cswapValue(& $a, & $b, $cond) |
|
311
|
|
|
{ |
|
312
|
|
|
$isGMP = is_object($a) && $a instanceof \GMP; |
|
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
$sa = $isGMP ? $a : gmp_init(intval($a), 10); |
|
315
|
|
|
$sb = $isGMP ? $b : gmp_init(intval($b), 10); |
|
316
|
|
|
$size = max(BinaryString::length(gmp_strval($sa, 2)), BinaryString::length(gmp_strval($sb, 2))); |
|
317
|
|
|
|
|
318
|
|
|
$mask = 1 - intval($cond); |
|
319
|
|
|
$mask = str_pad('', $size, $mask, STR_PAD_LEFT); |
|
320
|
|
|
$mask = gmp_init($mask, 2); |
|
321
|
|
|
|
|
322
|
|
|
$taA = $this->adapter->bitwiseAnd($sa, $mask); |
|
323
|
|
|
$taB = $this->adapter->bitwiseAnd($sb, $mask); |
|
324
|
|
|
|
|
325
|
|
|
$sa = $this->adapter->bitwiseXor($this->adapter->bitwiseXor($sa, $sb), $taB); |
|
326
|
|
|
$sb = $this->adapter->bitwiseXor($this->adapter->bitwiseXor($sa, $sb), $taA); |
|
327
|
|
|
$sa = $this->adapter->bitwiseXor($this->adapter->bitwiseXor($sa, $sb), $taB); |
|
328
|
|
|
|
|
329
|
|
|
$a = $isGMP ? $sa : (bool) gmp_strval($sa, 10); |
|
330
|
|
|
$b = $isGMP ? $sb : (bool) gmp_strval($sb, 10); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* |
|
335
|
|
|
*/ |
|
336
|
|
|
private function validate() |
|
337
|
|
|
{ |
|
338
|
|
|
if (! $this->infinity && ! $this->curve->contains($this->x, $this->y)) { |
|
339
|
|
|
throw new \RuntimeException('Invalid point'); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* {@inheritDoc} |
|
345
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::getDouble() |
|
346
|
|
|
* @return self |
|
347
|
|
|
*/ |
|
348
|
|
|
public function getDouble() |
|
349
|
|
|
{ |
|
350
|
|
|
if ($this->isInfinity()) { |
|
351
|
|
|
return $this->curve->getInfinity(); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
$math = $this->adapter; |
|
355
|
|
|
$modMath = $this->modAdapter; |
|
356
|
|
|
|
|
357
|
|
|
$a = $this->curve->getA(); |
|
358
|
|
|
$threeX2 = $math->mul(gmp_init(3, 10), $math->pow($this->x, 2)); |
|
359
|
|
|
|
|
360
|
|
|
$tangent = $modMath->div( |
|
361
|
|
|
$math->add($threeX2, $a), |
|
362
|
|
|
$math->mul(gmp_init(2, 10), $this->y) |
|
363
|
|
|
); |
|
364
|
|
|
|
|
365
|
|
|
$x3 = $modMath->sub( |
|
366
|
|
|
$math->pow($tangent, 2), |
|
367
|
|
|
$math->mul(gmp_init(2, 10), $this->x) |
|
368
|
|
|
); |
|
369
|
|
|
|
|
370
|
|
|
$y3 = $modMath->sub( |
|
371
|
|
|
$math->mul($tangent, $math->sub($this->x, $x3)), |
|
372
|
|
|
$this->y |
|
373
|
|
|
); |
|
374
|
|
|
|
|
375
|
|
|
return $this->curve->getPoint($x3, $y3, $this->order); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* {@inheritDoc} |
|
380
|
|
|
* @see \Jose\Component\Core\Util\Ecc\Point::__toString() |
|
381
|
|
|
*/ |
|
382
|
|
|
public function __toString() |
|
383
|
|
|
{ |
|
384
|
|
|
if ($this->infinity) { |
|
385
|
|
|
return '[ (infinity) on ' . (string) $this->curve . ' ]'; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
return "[ (" . $this->adapter->toString($this->x) . "," . $this->adapter->toString($this->y) . ') on ' . (string) $this->curve . ' ]'; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* @return array |
|
393
|
|
|
*/ |
|
394
|
|
|
public function __debugInfo() |
|
395
|
|
|
{ |
|
396
|
|
|
$info = [ |
|
397
|
|
|
'x' => $this->adapter->toString($this->x), |
|
398
|
|
|
'y' => $this->adapter->toString($this->y), |
|
399
|
|
|
'z' => $this->adapter->toString($this->order), |
|
400
|
|
|
'curve' => $this->curve |
|
401
|
|
|
]; |
|
402
|
|
|
|
|
403
|
|
|
if ($this->infinity) { |
|
404
|
|
|
$info['x'] = 'inf (' . $info['x'] . ')'; |
|
405
|
|
|
$info['y'] = 'inf (' . $info['y'] . ')'; |
|
406
|
|
|
$info['z'] = 'inf (' . $info['z'] . ')'; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
return $info; |
|
410
|
|
|
} |
|
411
|
|
|
} |
|
412
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.