1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Core\Util\Ecc; |
15
|
|
|
|
16
|
|
|
use GMP; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ********************************************************************* |
20
|
|
|
* Copyright (C) 2012 Matyas Danter. |
21
|
|
|
* |
22
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
23
|
|
|
* a copy of this software and associated documentation files (the "Software"), |
24
|
|
|
* to deal in the Software without restriction, including without limitation |
25
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
26
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
27
|
|
|
* Software is furnished to do so, subject to the following conditions: |
28
|
|
|
* |
29
|
|
|
* The above copyright notice and this permission notice shall be included |
30
|
|
|
* in all copies or substantial portions of the Software. |
31
|
|
|
* |
32
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
33
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
34
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
35
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
36
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
37
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
38
|
|
|
* OTHER DEALINGS IN THE SOFTWARE. |
39
|
|
|
* *********************************************************************** |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @internal |
44
|
|
|
*/ |
45
|
|
|
class Point |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var GMP |
49
|
|
|
*/ |
50
|
|
|
private $x; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var GMP |
54
|
|
|
*/ |
55
|
|
|
private $y; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var GMP |
59
|
|
|
*/ |
60
|
|
|
private $order; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
private $infinity = false; |
66
|
|
|
|
67
|
|
|
private function __construct(GMP $x, GMP $y, GMP $order, bool $infinity = false) |
68
|
|
|
{ |
69
|
|
|
$this->x = $x; |
70
|
|
|
$this->y = $y; |
71
|
|
|
$this->order = $order; |
72
|
|
|
$this->infinity = $infinity; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function create(GMP $x, GMP $y, ?GMP $order = null): self |
76
|
|
|
{ |
77
|
|
|
return new self($x, $y, null === $order ? gmp_init(0, 10) : $order); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function infinity(): self |
81
|
|
|
{ |
82
|
|
|
$zero = gmp_init(0, 10); |
83
|
|
|
|
84
|
|
|
return new self($zero, $zero, $zero, true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function isInfinity(): bool |
88
|
|
|
{ |
89
|
|
|
return $this->infinity; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getOrder(): GMP |
93
|
|
|
{ |
94
|
|
|
return $this->order; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getX(): GMP |
98
|
|
|
{ |
99
|
|
|
return $this->x; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getY(): GMP |
103
|
|
|
{ |
104
|
|
|
return $this->y; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function cswap(self $a, self $b, int $cond): void |
108
|
|
|
{ |
109
|
|
|
self::cswapGMP($a->x, $b->x, $cond); |
110
|
|
|
self::cswapGMP($a->y, $b->y, $cond); |
111
|
|
|
self::cswapGMP($a->order, $b->order, $cond); |
112
|
|
|
self::cswapBoolean($a->infinity, $b->infinity, $cond); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private static function cswapBoolean(bool &$a, bool &$b, int $cond): void |
116
|
|
|
{ |
117
|
|
|
$sa = gmp_init((int) $a, 10); |
118
|
|
|
$sb = gmp_init((int) $b, 10); |
119
|
|
|
|
120
|
|
|
self::cswapGMP($sa, $sb, $cond); |
121
|
|
|
|
122
|
|
|
$a = (bool) gmp_strval($sa, 10); |
123
|
|
|
$b = (bool) gmp_strval($sb, 10); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private static function cswapGMP(GMP &$sa, GMP &$sb, int $cond): void |
127
|
|
|
{ |
128
|
|
|
$size = max(mb_strlen(gmp_strval($sa, 2), '8bit'), mb_strlen(gmp_strval($sb, 2), '8bit')); |
129
|
|
|
$mask = (string) (1 - $cond); |
130
|
|
|
$mask = str_pad('', $size, $mask, STR_PAD_LEFT); |
131
|
|
|
$mask = gmp_init($mask, 2); |
132
|
|
|
$taA = Math::bitwiseAnd($sa, $mask); |
133
|
|
|
$taB = Math::bitwiseAnd($sb, $mask); |
134
|
|
|
$sa = Math::bitwiseXor(Math::bitwiseXor($sa, $sb), $taB); |
135
|
|
|
$sb = Math::bitwiseXor(Math::bitwiseXor($sa, $sb), $taA); |
136
|
|
|
$sa = Math::bitwiseXor(Math::bitwiseXor($sa, $sb), $taB); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|