Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Core\Util;
6
7 use Brick\Math\BigInteger as BrickBigInteger;
8 use InvalidArgumentException;
9 use function chr;
10
11 /**
12 * @internal
13 */
14 final class BigInteger
15 {
16 private function __construct(
17 private readonly BrickBigInteger $value
18 ) {
19 }
20
21 public static function createFromBinaryString(string $value): self
22 {
23 $res = unpack('H*', $value);
24 if ($res === false) {
25 throw new InvalidArgumentException('Unable to convert the value');
26 }
27 $data = current($res);
28
29 return new self(BrickBigInteger::fromBase($data, 16));
30 }
31
32 public static function createFromDecimal(int $value): self
33 {
34 return new self(BrickBigInteger::of($value));
35 }
36
37 public static function createFromBigInteger(BrickBigInteger $value): self
38 {
39 return new self($value);
40 }
41
42 /**
43 * Converts a BigInteger to a binary string.
44 */
45 public function toBytes(): string
46 {
47 if ($this->value->isEqualTo(BrickBigInteger::zero())) {
48 return '';
49 }
50
51 $temp = $this->value->toBase(16);
52 $temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0' . $temp : $temp;
53 $temp = hex2bin($temp);
54 if ($temp === false) {
55 throw new InvalidArgumentException('Unable to convert the value into bytes');
56 }
57
58 return ltrim($temp, chr(0));
59 }
60
61 /**
62 * Adds two BigIntegers.
63 */
64 public function add(self $y): self
65 {
66 $value = $this->value->plus($y->value);
67
68 return new self($value);
69 }
70
71 /**
72 * Subtracts two BigIntegers.
73 */
74 public function subtract(self $y): self
75 {
76 $value = $this->value->minus($y->value);
77
78 return new self($value);
79 }
80
81 /**
82 * Multiplies two BigIntegers.
83 */
84 public function multiply(self $x): self
85 {
86 $value = $this->value->multipliedBy($x->value);
87
88 return new self($value);
89 }
90
91 /**
92 * Divides two BigIntegers.
93 */
94 public function divide(self $x): self
95 {
96 $value = $this->value->dividedBy($x->value);
97
98 return new self($value);
99 }
100
101 /**
102 * Performs modular exponentiation.
103 */
104 public function modPow(self $e, self $n): self
105 {
106 $value = $this->value->modPow($e->value, $n->value);
107
108 return new self($value);
109 }
110
111 /**
112 * Performs modular exponentiation.
113 */
114 public function mod(self $d): self
115 {
116 $value = $this->value->mod($d->value);
117
118 return new self($value);
119 }
120
121 public function modInverse(self $m): self
122 {
123 return new self($this->value->modInverse($m->value));
124 }
125
126 /**
127 * Compares two numbers.
128 */
129 public function compare(self $y): int
130 {
131 return $this->value->compareTo($y->value);
132 }
133
134 public function equals(self $y): bool
135 {
136 return $this->value->isEqualTo($y->value);
137 }
138
139 public static function random(self $y): self
140 {
141 return new self(BrickBigInteger::randomRange(0, $y->value));
142 }
143
144 public function gcd(self $y): self
145 {
146 return new self($this->value->gcd($y->value));
147 }
148
149 public function lowerThan(self $y): bool
150 {
151 return $this->value->isLessThan($y->value);
152 }
153
154 public function isEven(): bool
155 {
156 return $this->value->isEven();
157 }
158
159 public function get(): BrickBigInteger
160 {
161 return $this->value;
162 }
163 }