PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / phpseclib3 / Math / BigInteger / Engines / PHP32.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Math / BigInteger / Engines Last commit date
BCMath 9 months ago GMP 9 months ago PHP 9 months ago .htaccess 9 months ago BCMath.php 9 months ago Engine.php 9 months ago GMP.php 9 months ago OpenSSL.php 9 months ago PHP.php 9 months ago PHP32.php 9 months ago PHP64.php 9 months ago index.html 9 months ago web.config 9 months ago
PHP32.php
318 lines
1 <?php
2
3 /**
4 * Pure-PHP 32-bit BigInteger Engine
5 *
6 * PHP version 5 and 7
7 *
8 * @author Jim Wigginton <terrafrost@php.net>
9 * @copyright 2017 Jim Wigginton
10 * @license http://www.opensource.org/licenses/mit-license.html MIT License
11 * @link http://pear.php.net/package/Math_BigInteger
12 */
13
14 declare(strict_types=1);
15
16 namespace phpseclib3\Math\BigInteger\Engines;
17
18 /**
19 * Pure-PHP 32-bit Engine.
20 *
21 * Uses 64-bit floats if int size is 4 bits
22 *
23 * @author Jim Wigginton <terrafrost@php.net>
24 */
25 class PHP32 extends PHP
26 {
27 // Constants used by PHP.php
28 public const BASE = 26;
29 public const BASE_FULL = 0x4000000;
30 public const MAX_DIGIT = 0x3FFFFFF;
31 public const MSB = 0x2000000;
32
33 /**
34 * MAX10 in greatest MAX10LEN satisfying
35 * MAX10 = 10**MAX10LEN <= 2**BASE.
36 */
37 public const MAX10 = 10000000;
38
39 /**
40 * MAX10LEN in greatest MAX10LEN satisfying
41 * MAX10 = 10**MAX10LEN <= 2**BASE.
42 */
43 public const MAX10LEN = 7;
44 public const MAX_DIGIT2 = 4503599627370496;
45
46 /**
47 * Initialize a PHP32 BigInteger Engine instance
48 *
49 * @see parent::initialize()
50 */
51 protected function initialize(int $base): void
52 {
53 if ($base != 256 && $base != -256) {
54 parent::initialize($base);
55 return;
56 }
57
58 $val = $this->value;
59 $this->value = [];
60 $vals = &$this->value;
61 $i = strlen($val);
62 if (!$i) {
63 return;
64 }
65
66 while (true) {
67 $i -= 4;
68 if ($i < 0) {
69 if ($i == -4) {
70 break;
71 }
72 $val = substr($val, 0, 4 + $i);
73 $val = str_pad($val, 4, "\0", STR_PAD_LEFT);
74 if ($val == "\0\0\0\0") {
75 break;
76 }
77 $i = 0;
78 }
79 [, $digit] = unpack('N', substr($val, $i, 4));
80 if ($digit < 0) {
81 $digit += 0xFFFFFFFF + 1;
82 }
83 $step = count($vals) & 3;
84 if ($step) {
85 $digit = (int) floor($digit / 2 ** (2 * $step));
86 }
87 if ($step != 3) {
88 $digit = (int) fmod($digit, static::BASE_FULL);
89 $i++;
90 }
91 $vals[] = $digit;
92 }
93 while (end($vals) === 0) {
94 array_pop($vals);
95 }
96 reset($vals);
97 }
98
99 /**
100 * Test for engine validity
101 *
102 * @see parent::__construct()
103 */
104 public static function isValidEngine(): bool
105 {
106 return PHP_INT_SIZE >= 4 && !self::testJITOnWindows();
107 }
108
109 /**
110 * Adds two BigIntegers.
111 */
112 public function add(PHP32 $y): PHP32
113 {
114 $temp = self::addHelper($this->value, $this->is_negative, $y->value, $y->is_negative);
115
116 return $this->convertToObj($temp);
117 }
118
119 /**
120 * Subtracts two BigIntegers.
121 */
122 public function subtract(PHP32 $y): PHP32
123 {
124 $temp = self::subtractHelper($this->value, $this->is_negative, $y->value, $y->is_negative);
125
126 return $this->convertToObj($temp);
127 }
128
129 /**
130 * Multiplies two BigIntegers.
131 */
132 public function multiply(PHP32 $y): PHP32
133 {
134 $temp = self::multiplyHelper($this->value, $this->is_negative, $y->value, $y->is_negative);
135
136 return $this->convertToObj($temp);
137 }
138
139 /**
140 * Divides two BigIntegers.
141 *
142 * Returns an array whose first element contains the quotient and whose second element contains the
143 * "common residue". If the remainder would be positive, the "common residue" and the remainder are the
144 * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder
145 * and the divisor (basically, the "common residue" is the first positive modulo).
146 *
147 * @return array{PHP32, PHP32}
148 */
149 public function divide(PHP32 $y): array
150 {
151 return $this->divideHelper($y);
152 }
153
154 /**
155 * Calculates modular inverses.
156 *
157 * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
158 * @return false|PHP32
159 */
160 public function modInverse(PHP32 $n)
161 {
162 return $this->modInverseHelper($n);
163 }
164
165 /**
166 * Calculates modular inverses.
167 *
168 * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
169 * @return PHP32[]
170 */
171 public function extendedGCD(PHP32 $n): array
172 {
173 return $this->extendedGCDHelper($n);
174 }
175
176 /**
177 * Calculates the greatest common divisor
178 *
179 * Say you have 693 and 609. The GCD is 21.
180 */
181 public function gcd(PHP32 $n): PHP32
182 {
183 return $this->extendedGCD($n)['gcd'];
184 }
185
186 /**
187 * Logical And
188 */
189 public function bitwise_and(PHP32 $x): PHP32
190 {
191 return $this->bitwiseAndHelper($x);
192 }
193
194 /**
195 * Logical Or
196 */
197 public function bitwise_or(PHP32 $x): PHP32
198 {
199 return $this->bitwiseOrHelper($x);
200 }
201
202 /**
203 * Logical Exclusive Or
204 */
205 public function bitwise_xor(PHP32 $x): PHP32
206 {
207 return $this->bitwiseXorHelper($x);
208 }
209
210 /**
211 * Compares two numbers.
212 *
213 * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is
214 * demonstrated thusly:
215 *
216 * $x > $y: $x->compare($y) > 0
217 * $x < $y: $x->compare($y) < 0
218 * $x == $y: $x->compare($y) == 0
219 *
220 * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y).
221 *
222 * {@internal Could return $this->subtract($x), but that's not as fast as what we do do.}
223 *
224 * @return int in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
225 * @see self::equals()
226 */
227 public function compare(PHP32 $y): int
228 {
229 return $this->compareHelper($this->value, $this->is_negative, $y->value, $y->is_negative);
230 }
231
232 /**
233 * Tests the equality of two numbers.
234 *
235 * If you need to see if one number is greater than or less than another number, use BigInteger::compare()
236 */
237 public function equals(PHP32 $x): bool
238 {
239 return $this->value === $x->value && $this->is_negative == $x->is_negative;
240 }
241
242 /**
243 * Performs modular exponentiation.
244 */
245 public function modPow(PHP32 $e, PHP32 $n): PHP32
246 {
247 return $this->powModOuter($e, $n);
248 }
249
250 /**
251 * Performs modular exponentiation.
252 *
253 * Alias for modPow().
254 */
255 public function powMod(PHP32 $e, PHP32 $n): PHP32
256 {
257 return $this->powModOuter($e, $n);
258 }
259
260 /**
261 * Generate a random prime number between a range
262 *
263 * If there's not a prime within the given range, false will be returned.
264 *
265 * @return false|PHP32
266 */
267 public static function randomRangePrime(PHP32 $min, PHP32 $max)
268 {
269 return self::randomRangePrimeOuter($min, $max);
270 }
271
272 /**
273 * Generate a random number between a range
274 *
275 * Returns a random number between $min and $max where $min and $max
276 * can be defined using one of the two methods:
277 *
278 * BigInteger::randomRange($min, $max)
279 * BigInteger::randomRange($max, $min)
280 */
281 public static function randomRange(PHP32 $min, PHP32 $max): PHP32
282 {
283 return self::randomRangeHelper($min, $max);
284 }
285
286 /**
287 * Performs exponentiation.
288 */
289 public function pow(PHP32 $n): PHP32
290 {
291 return $this->powHelper($n);
292 }
293
294 /**
295 * Return the minimum BigInteger between an arbitrary number of BigIntegers.
296 */
297 public static function min(PHP32 ...$nums): PHP32
298 {
299 return self::minHelper($nums);
300 }
301
302 /**
303 * Return the maximum BigInteger between an arbitrary number of BigIntegers.
304 */
305 public static function max(PHP32 ...$nums): PHP32
306 {
307 return self::maxHelper($nums);
308 }
309
310 /**
311 * Tests BigInteger to see if it is between two integers, inclusive
312 */
313 public function between(PHP32 $min, PHP32 $max): bool
314 {
315 return $this->compare($min) >= 0 && $this->compare($max) <= 0;
316 }
317 }
318