|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\PNServer\Utils; |
|
5
|
|
|
|
|
6
|
|
|
/* |
|
7
|
|
|
* extracted required classes and functions from package |
|
8
|
|
|
* spomky-labs/jose |
|
9
|
|
|
* https://github.com/Spomky-Labs/Jose |
|
10
|
|
|
* |
|
11
|
|
|
* @package PNServer |
|
12
|
|
|
* @version 1.0.0 |
|
13
|
|
|
* @copyright MIT License - see the copyright below and LICENSE file for details |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
/* |
|
17
|
|
|
* ********************************************************************* |
|
18
|
|
|
* Copyright (C) 2012 Matyas Danter. |
|
19
|
|
|
* |
|
20
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
21
|
|
|
* a copy of this software and associated documentation files (the "Software"), |
|
22
|
|
|
* to deal in the Software without restriction, including without limitation |
|
23
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
24
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
|
25
|
|
|
* Software is furnished to do so, subject to the following conditions: |
|
26
|
|
|
* |
|
27
|
|
|
* The above copyright notice and this permission notice shall be included |
|
28
|
|
|
* in all copies or substantial portions of the Software. |
|
29
|
|
|
* |
|
30
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
31
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
32
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
33
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
|
34
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|
35
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
36
|
|
|
* OTHER DEALINGS IN THE SOFTWARE. |
|
37
|
|
|
* *********************************************************************** |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
class Point |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var \GMP */ |
|
43
|
|
|
private \GMP $x; |
|
44
|
|
|
/** @var \GMP */ |
|
45
|
|
|
private \GMP $y; |
|
46
|
|
|
/** @var \GMP */ |
|
47
|
|
|
private \GMP $order; |
|
48
|
|
|
/** @var bool */ |
|
49
|
|
|
private $infinity = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Initialize a new instance. |
|
53
|
|
|
* @throws \RuntimeException when either the curve does not contain the given coordinates or |
|
54
|
|
|
* when order is not null and P(x, y) * order is not equal to infinity |
|
55
|
|
|
*/ |
|
56
|
|
|
private function __construct(\GMP $x, \GMP $y, \GMP $order, bool $infinity = false) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->x = $x; |
|
59
|
|
|
$this->y = $y; |
|
60
|
|
|
$this->order = $order; |
|
61
|
|
|
$this->infinity = $infinity; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Point |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function create(\GMP $x, \GMP $y, \GMP $order = null) : Point |
|
68
|
|
|
{ |
|
69
|
|
|
return new self($x, $y, null === $order ? \gmp_init(0, 10) : $order); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Point |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function infinity() : Point |
|
76
|
|
|
{ |
|
77
|
|
|
$zero = \gmp_init(0, 10); |
|
78
|
|
|
|
|
79
|
|
|
return new self($zero, $zero, $zero, true); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function isInfinity() : bool |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->infinity; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getOrder() : \GMP |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->order; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getX() : \GMP |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->x; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getY() : \GMP |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->y; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Point $a |
|
104
|
|
|
* @param Point $b |
|
105
|
|
|
* @param int $cond |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function cswap(Point $a, Point $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 - (int) ($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
|
|
|
|