Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Core/Util/Ecc/Curve.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Core\Util\Ecc;
6
7 use Brick\Math\BigInteger;
8 use RuntimeException;
9 use Stringable;
10 use const STR_PAD_LEFT;
11
12 /**
13 * @internal
14 */
15 final class Curve implements Stringable
16 {
17 public function __construct(
18 private readonly int $size,
19 private readonly BigInteger $prime,
20 private readonly BigInteger $a,
21 private readonly BigInteger $b,
22 private readonly Point $generator
23 ) {
24 }
25
26 public function __toString(): string
27 {
28 return 'curve(' . Math::toString($this->getA()) . ', ' . Math::toString($this->getB()) . ', ' . Math::toString(
29 $this->getPrime()
30 ) . ')';
31 }
32
33 public function getA(): BigInteger
34 {
35 return $this->a;
36 }
37
38 public function getB(): BigInteger
39 {
40 return $this->b;
41 }
42
43 public function getPrime(): BigInteger
44 {
45 return $this->prime;
46 }
47
48 public function getSize(): int
49 {
50 return $this->size;
51 }
52
53 public function getPoint(BigInteger $x, BigInteger $y, ?BigInteger $order = null): Point
54 {
55 if (! $this->contains($x, $y)) {
56 throw new RuntimeException('Curve ' . $this->__toString() . ' does not contain point (' . Math::toString(
57 $x
58 ) . ', ' . Math::toString($y) . ')');
59 }
60 $point = Point::create($x, $y, $order);
61 if ($order !== null) {
62 $mul = $this->mul($point, $order);
63 if (! $mul->isInfinity()) {
64 throw new RuntimeException('SELF * ORDER MUST EQUAL INFINITY.');
65 }
66 }
67
68 return $point;
69 }
70
71 public function getPublicKeyFrom(BigInteger $x, BigInteger $y): PublicKey
72 {
73 $zero = BigInteger::zero();
74 if ($x->compareTo($zero) < 0 || $y->compareTo($zero) < 0 || $this->generator->getOrder()->compareTo(
75 $x
76 ) <= 0 || $this->generator->getOrder()
77 ->compareTo($y) <= 0) {
78 throw new RuntimeException('Generator point has x and y out of range.');
79 }
80 $point = $this->getPoint($x, $y);
81
82 return new PublicKey($point);
83 }
84
85 public function contains(BigInteger $x, BigInteger $y): bool
86 {
87 return Math::equals(
88 ModularArithmetic::sub(
89 $y->power(2),
90 Math::add(Math::add($x->power(3), $this->getA()->multipliedBy($x)), $this->getB()),
91 $this->getPrime()
92 ),
93 BigInteger::zero()
94 );
95 }
96
97 public function add(Point $one, Point $two): Point
98 {
99 if ($two->isInfinity()) {
100 return clone $one;
101 }
102
103 if ($one->isInfinity()) {
104 return clone $two;
105 }
106
107 if ($two->getX()->isEqualTo($one->getX())) {
108 if ($two->getY()->isEqualTo($one->getY())) {
109 return $this->getDouble($one);
110 }
111
112 return Point::infinity();
113 }
114
115 $slope = ModularArithmetic::div(
116 $two->getY()
117 ->minus($one->getY()),
118 $two->getX()
119 ->minus($one->getX()),
120 $this->getPrime()
121 );
122
123 $xR = ModularArithmetic::sub($slope->power(2)->minus($one->getX()), $two->getX(), $this->getPrime());
124
125 $yR = ModularArithmetic::sub(
126 $slope->multipliedBy($one->getX()->minus($xR)),
127 $one->getY(),
128 $this->getPrime()
129 );
130
131 return $this->getPoint($xR, $yR, $one->getOrder());
132 }
133
134 public function mul(Point $one, BigInteger $n): Point
135 {
136 if ($one->isInfinity()) {
137 return Point::infinity();
138 }
139
140 /** @var BigInteger $zero */
141 $zero = BigInteger::zero();
142 if ($one->getOrder()->compareTo($zero) > 0) {
143 $n = $n->mod($one->getOrder());
144 }
145
146 if ($n->isEqualTo($zero)) {
147 return Point::infinity();
148 }
149
150 /** @var Point[] $r */
151 $r = [Point::infinity(), clone $one];
152
153 $k = $this->getSize();
154 $n1 = str_pad(Math::baseConvert(Math::toString($n), 10, 2), $k, '0', STR_PAD_LEFT);
155
156 for ($i = 0; $i < $k; ++$i) {
157 $j = $n1[$i];
158 Point::cswap($r[0], $r[1], $j ^ 1);
159 $r[0] = $this->add($r[0], $r[1]);
160 $r[1] = $this->getDouble($r[1]);
161 Point::cswap($r[0], $r[1], $j ^ 1);
162 }
163
164 $this->validate($r[0]);
165
166 return $r[0];
167 }
168
169 public function cmp(self $other): int
170 {
171 $equalsA = $this->getA()
172 ->isEqualTo($other->getA());
173 $equalsB = $this->getB()
174 ->isEqualTo($other->getB());
175 $equalsPrime = $this->getPrime()
176 ->isEqualTo($other->getPrime());
177 $equal = $equalsA && $equalsB && $equalsPrime;
178
179 return $equal ? 0 : 1;
180 }
181
182 public function equals(self $other): bool
183 {
184 return $this->cmp($other) === 0;
185 }
186
187 public function getDouble(Point $point): Point
188 {
189 if ($point->isInfinity()) {
190 return Point::infinity();
191 }
192
193 $a = $this->getA();
194 $threeX2 = BigInteger::of(3)->multipliedBy($point->getX()->power(2));
195
196 $tangent = ModularArithmetic::div(
197 $threeX2->plus($a),
198 BigInteger::of(2)->multipliedBy($point->getY()),
199 $this->getPrime()
200 );
201
202 $x3 = ModularArithmetic::sub(
203 $tangent->power(2),
204 BigInteger::of(2)->multipliedBy($point->getX()),
205 $this->getPrime()
206 );
207
208 $y3 = ModularArithmetic::sub(
209 $tangent->multipliedBy($point->getX()->minus($x3)),
210 $point->getY(),
211 $this->getPrime()
212 );
213
214 return $this->getPoint($x3, $y3, $point->getOrder());
215 }
216
217 public function createPrivateKey(): PrivateKey
218 {
219 return PrivateKey::create($this->generate());
220 }
221
222 public function createPublicKey(PrivateKey $privateKey): PublicKey
223 {
224 $point = $this->mul($this->generator, $privateKey->getSecret());
225
226 return new PublicKey($point);
227 }
228
229 public function getGenerator(): Point
230 {
231 return $this->generator;
232 }
233
234 private function validate(Point $point): void
235 {
236 if (! $point->isInfinity() && ! $this->contains($point->getX(), $point->getY())) {
237 throw new RuntimeException('Invalid point');
238 }
239 }
240
241 private function generate(): BigInteger
242 {
243 $max = $this->generator->getOrder();
244 $numBits = $this->bnNumBits($max);
245 $numBytes = (int) ceil($numBits / 8);
246 // Generate an integer of size >= $numBits
247 $bytes = BigInteger::randomBits($numBytes);
248 $mask = BigInteger::of(2)->power($numBits)->minus(1);
249
250 return $bytes->and($mask);
251 }
252
253 /**
254 * Returns the number of bits used to store this number. Non-significant upper bits are not counted.
255 *
256 * @see https://www.openssl.org/docs/crypto/BN_num_bytes.html
257 */
258 private function bnNumBits(BigInteger $x): int
259 {
260 $zero = BigInteger::of(0);
261 if ($x->isEqualTo($zero)) {
262 return 0;
263 }
264 $log2 = 0;
265 while (! $x->isEqualTo($zero)) {
266 $x = $x->shiftedRight(1);
267 ++$log2;
268 }
269
270 return $log2;
271 }
272 }