Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Core/Util/Ecc/Point.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 const STR_PAD_LEFT;
9
10 /**
11 * Copyright (C) 2012 Matyas Danter.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
14 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
15 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
19 * Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
22 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 /**
28 * @internal
29 */
30 final class Point
31 {
32 private function __construct(
33 private BigInteger $x,
34 private BigInteger $y,
35 private BigInteger $order,
36 private bool $infinity = false
37 ) {
38 }
39
40 public static function create(BigInteger $x, BigInteger $y, ?BigInteger $order = null): self
41 {
42 return new self($x, $y, $order ?? BigInteger::zero());
43 }
44
45 public static function infinity(): self
46 {
47 $zero = BigInteger::zero();
48
49 return new self($zero, $zero, $zero, true);
50 }
51
52 public function isInfinity(): bool
53 {
54 return $this->infinity;
55 }
56
57 public function getOrder(): BigInteger
58 {
59 return $this->order;
60 }
61
62 public function getX(): BigInteger
63 {
64 return $this->x;
65 }
66
67 public function getY(): BigInteger
68 {
69 return $this->y;
70 }
71
72 public static function cswap(self $a, self $b, int $cond): void
73 {
74 self::cswapBigInteger($a->x, $b->x, $cond);
75 self::cswapBigInteger($a->y, $b->y, $cond);
76 self::cswapBigInteger($a->order, $b->order, $cond);
77 self::cswapBoolean($a->infinity, $b->infinity, $cond);
78 }
79
80 private static function cswapBoolean(bool &$a, bool &$b, int $cond): void
81 {
82 $sa = BigInteger::of((int) $a);
83 $sb = BigInteger::of((int) $b);
84
85 self::cswapBigInteger($sa, $sb, $cond);
86
87 $a = (bool) $sa->toBase(10);
88 $b = (bool) $sb->toBase(10);
89 }
90
91 private static function cswapBigInteger(BigInteger &$sa, BigInteger &$sb, int $cond): void
92 {
93 $size = max(mb_strlen($sa->toBase(2), '8bit'), mb_strlen($sb->toBase(2), '8bit'));
94 $mask = (string) (1 - $cond);
95 $mask = str_pad('', $size, $mask, STR_PAD_LEFT);
96 $mask = BigInteger::fromBase($mask, 2);
97 $taA = $sa->and($mask);
98 $taB = $sb->and($mask);
99 $sa = $sa->xor($sb)
100 ->xor($taB);
101 $sb = $sa->xor($sb)
102 ->xor($taA);
103 $sa = $sa->xor($sb)
104 ->xor($taB);
105 }
106 }