Complex classes like BigInteger often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BigInteger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class BigInteger |
||
15 | { |
||
16 | const MONTGOMERY = 0; |
||
17 | |||
18 | const BARRETT = 1; |
||
19 | |||
20 | const POWEROF2 = 2; |
||
21 | |||
22 | const CLASSIC = 3; |
||
23 | |||
24 | const NONE = 4; |
||
25 | |||
26 | /**#@+ |
||
27 | * Array constants |
||
28 | * |
||
29 | * Rather than create a thousands and thousands of new BigInteger objects in repeated function calls to add() and |
||
30 | * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them. |
||
31 | * |
||
32 | */ |
||
33 | /** |
||
34 | * $result[self::VALUE] contains the value. |
||
35 | */ |
||
36 | const VALUE = 0; |
||
37 | /** |
||
38 | * $result[self::SIGN] contains the sign. |
||
39 | */ |
||
40 | const SIGN = 1; |
||
41 | /**#@-*/ |
||
42 | |||
43 | /**#@+ |
||
44 | */ |
||
45 | /** |
||
46 | * Cache constants. |
||
47 | * |
||
48 | * $cache[self::VARIABLE] tells us whether or not the cached data is still valid. |
||
49 | */ |
||
50 | const VARIABLE = 0; |
||
51 | /** |
||
52 | * $cache[self::DATA] contains the cached data. |
||
53 | */ |
||
54 | const DATA = 1; |
||
55 | /**#@-*/ |
||
56 | |||
57 | /** |
||
58 | * Karatsuba Cutoff. |
||
59 | * |
||
60 | * At what point do we switch between Karatsuba multiplication and schoolbook long multiplication? |
||
61 | */ |
||
62 | const KARATSUBA_CUTOFF = 25; |
||
63 | |||
64 | /**#@+ |
||
65 | * Static properties used by the pure-PHP implementation. |
||
66 | * |
||
67 | * @see __construct() |
||
68 | */ |
||
69 | protected static $base; |
||
70 | protected static $baseFull; |
||
71 | protected static $maxDigit; |
||
72 | protected static $msb; |
||
73 | |||
74 | /** |
||
75 | * $max10 in greatest $max10Len satisfying |
||
76 | * $max10 = 10**$max10Len <= 2**$base. |
||
77 | */ |
||
78 | protected static $max10; |
||
79 | |||
80 | /** |
||
81 | * $max10Len in greatest $max10Len satisfying |
||
82 | * $max10 = 10**$max10Len <= 2**$base. |
||
83 | */ |
||
84 | protected static $max10Len; |
||
85 | protected static $maxDigit2; |
||
86 | /**#@-*/ |
||
87 | |||
88 | /** |
||
89 | * Holds the BigInteger's value. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | private $value; |
||
94 | |||
95 | /** |
||
96 | * Holds the BigInteger's magnitude. |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | private $is_negative = false; |
||
101 | |||
102 | /** |
||
103 | * Precision. |
||
104 | */ |
||
105 | private $precision = -1; |
||
106 | |||
107 | /** |
||
108 | * Precision Bitmask. |
||
109 | */ |
||
110 | private $bitmask = false; |
||
111 | |||
112 | /** |
||
113 | * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers. |
||
114 | * |
||
115 | * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using |
||
116 | * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. |
||
117 | * |
||
118 | * Here's an example: |
||
119 | * <code> |
||
120 | * <?php |
||
121 | * $a = new \Jose\Util\in base-16 |
||
122 | * |
||
123 | * echo $a->toString(); // outputs 50 |
||
124 | * ?> |
||
125 | * </code> |
||
126 | * |
||
127 | * @param $x base-10 number or base-$base number if $base set. |
||
128 | * @param int $base |
||
129 | * |
||
130 | * @return \Jose\Util\BigInteger |
||
|
|||
131 | */ |
||
132 | public function __construct($x = 0, $base = 10) |
||
233 | |||
234 | /** |
||
235 | * Converts a BigInteger to a byte string (eg. base-256). |
||
236 | * |
||
237 | * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're |
||
238 | * saved as two's compliment. |
||
239 | * |
||
240 | * Here's an example: |
||
241 | * <code> |
||
242 | * <?php |
||
243 | * $a = new \Jose\Util\ger('65'); |
||
244 | * |
||
245 | * echo $a->toBytes(); // outputs chr(65) |
||
246 | * ?> |
||
247 | * </code> |
||
248 | * |
||
249 | * @param bool $twos_compliment |
||
250 | * |
||
251 | * @return string |
||
252 | * |
||
253 | */ |
||
254 | public function toBytes($twos_compliment = false) |
||
288 | |||
289 | /** |
||
290 | * Adds two BigIntegers. |
||
291 | * |
||
292 | * Here's an example: |
||
293 | * <code> |
||
294 | * <?php |
||
295 | * $a = new \Jose\Util\ger('10'); |
||
296 | * $b = new \Jose\Util\ger('20'); |
||
297 | * |
||
298 | * $c = $a->add($b); |
||
299 | * |
||
300 | * echo $c->toString(); // outputs 30 |
||
301 | * ?> |
||
302 | * </code> |
||
303 | * |
||
304 | * @param \Jose\Util\Integer $y |
||
305 | * |
||
306 | * @return \Jose\Util\BigInteger |
||
307 | * |
||
308 | */ |
||
309 | public function add(BigInteger $y) |
||
316 | |||
317 | /** |
||
318 | * Subtracts two BigIntegers. |
||
319 | * |
||
320 | * Here's an example: |
||
321 | * <code> |
||
322 | * <?php |
||
323 | * $a = new \Jose\Util\ger('10'); |
||
324 | * $b = new \Jose\Util\ger('20'); |
||
325 | * |
||
326 | * $c = $a->subtract($b); |
||
327 | * |
||
328 | * echo $c->toString(); // outputs -10 |
||
329 | * ?> |
||
330 | * </code> |
||
331 | * |
||
332 | * @param \Jose\Util\Integer $y |
||
333 | * |
||
334 | * @return \Jose\Util\BigInteger |
||
335 | * |
||
336 | */ |
||
337 | public function subtract(BigInteger $y) |
||
344 | |||
345 | /** |
||
346 | * Multiplies two BigIntegers. |
||
347 | * |
||
348 | * Here's an example: |
||
349 | * <code> |
||
350 | * <?php |
||
351 | * $a = new \Jose\Util\ger('10'); |
||
352 | * $b = new \Jose\Util\ger('20'); |
||
353 | * |
||
354 | * $c = $a->multiply($b); |
||
355 | * |
||
356 | * echo $c->toString(); // outputs 200 |
||
357 | * ?> |
||
358 | * </code> |
||
359 | * |
||
360 | * @param \Jose\Util\Integer $x |
||
361 | * |
||
362 | * @return \Jose\Util\BigInteger |
||
363 | */ |
||
364 | public function multiply(BigInteger $x) |
||
371 | |||
372 | /** |
||
373 | * Divides two BigIntegers. |
||
374 | * |
||
375 | * Returns an array whose first element contains the quotient and whose second element contains the |
||
376 | * "common residue". If the remainder would be positive, the "common residue" and the remainder are the |
||
377 | * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder |
||
378 | * and the divisor (basically, the "common residue" is the first positive modulo). |
||
379 | * |
||
380 | * Here's an example: |
||
381 | * <code> |
||
382 | * <?php |
||
383 | * $a = new \Jose\Util\ger('10'); |
||
384 | * $b = new \Jose\Util\ger('20'); |
||
385 | * |
||
386 | * list($quotient, $remainder) = $a->divide($b); |
||
387 | * |
||
388 | * echo $quotient->toString(); // outputs 0 |
||
389 | * echo "\r\n"; |
||
390 | * echo $remainder->toString(); // outputs 10 |
||
391 | * ?> |
||
392 | * </code> |
||
393 | * |
||
394 | * @param \Jose\Util\Integer $y |
||
395 | * |
||
396 | * @return array |
||
397 | * |
||
398 | */ |
||
399 | public function divide(BigInteger $y) |
||
412 | |||
413 | /** |
||
414 | * Performs modular exponentiation. |
||
415 | * |
||
416 | * Here's an example: |
||
417 | * <code> |
||
418 | * <?php |
||
419 | * $a = new \Jose\Util\ger('10'); |
||
420 | * $b = new \Jose\Util\ger('20'); |
||
421 | * $c = new \Jose\Util\ger('30'); |
||
422 | * |
||
423 | * $c = $a->modPow($b, $c); |
||
424 | * |
||
425 | * echo $c->toString(); // outputs 10 |
||
426 | * ?> |
||
427 | * </code> |
||
428 | * |
||
429 | * @param \Jose\Util\Integer $e |
||
430 | * @param \Jose\Util\Integer $n |
||
431 | * |
||
432 | * @return \Jose\Util\BigInteger |
||
433 | * |
||
434 | * and although the approach involving repeated squaring does vastly better, it, too, is impractical |
||
435 | * for our purposes. The reason being that division - by far the most complicated and time-consuming |
||
436 | * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. |
||
437 | * |
||
438 | * Modular reductions resolve this issue. Although an individual modular reduction takes more time |
||
439 | * then an individual division, when performed in succession (with the same modulo), they're a lot faster. |
||
440 | * |
||
441 | * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, |
||
442 | * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the |
||
443 | * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because |
||
444 | * the product of two odd numbers is odd), but what about when RSA isn't used? |
||
445 | * |
||
446 | * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a |
||
447 | * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the |
||
448 | * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, |
||
449 | * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and |
||
450 | * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. |
||
451 | * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. |
||
452 | */ |
||
453 | public function modPow(BigInteger $e, BigInteger $n) |
||
473 | |||
474 | /** |
||
475 | * Calculates modular inverses. |
||
476 | * |
||
477 | * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. |
||
478 | * |
||
479 | * Here's an example: |
||
480 | * <code> |
||
481 | * <?php |
||
482 | * $a = new \Jose\Util\teger(30); |
||
483 | * $b = new \Jose\Util\teger(17); |
||
484 | * |
||
485 | * $c = $a->modInverse($b); |
||
486 | * echo $c->toString(); // outputs 4 |
||
487 | * |
||
488 | * echo "\r\n"; |
||
489 | * |
||
490 | * $d = $a->multiply($c); |
||
491 | * list(, $d) = $d->divide($b); |
||
492 | * echo $d; // outputs 1 (as per the definition of modular inverse) |
||
493 | * ?> |
||
494 | * </code> |
||
495 | * |
||
496 | * @param \Jose\Util\Integer $n |
||
497 | * |
||
498 | * @return \Jose\Util\eger|false |
||
499 | * |
||
500 | */ |
||
501 | public function modInverse(BigInteger $n) |
||
508 | |||
509 | /** |
||
510 | * Absolute value. |
||
511 | * |
||
512 | * @return \Jose\Util\BigInteger |
||
513 | */ |
||
514 | public function abs() |
||
522 | |||
523 | /** |
||
524 | * Compares two numbers. |
||
525 | * |
||
526 | * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is |
||
527 | * demonstrated thusly: |
||
528 | * |
||
529 | * $x > $y: $x->compare($y) > 0 |
||
530 | * $x < $y: $x->compare($y) < 0 |
||
531 | * $x == $y: $x->compare($y) == 0 |
||
532 | * |
||
533 | * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). |
||
534 | * |
||
535 | * @param \Jose\Util\Integer $y |
||
536 | * |
||
537 | * @return int < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal. |
||
538 | * |
||
539 | */ |
||
540 | public function compare(BigInteger $y) |
||
544 | |||
545 | /** |
||
546 | * Logical Left Shift. |
||
547 | * |
||
548 | * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. |
||
549 | * |
||
550 | * @param int $shift |
||
551 | * |
||
552 | * @return \Jose\Util\BigInteger |
||
553 | * |
||
554 | */ |
||
555 | public function bitwise_leftShift($shift) |
||
569 | |||
570 | /** |
||
571 | * Generates a random BigInteger. |
||
572 | * |
||
573 | * Byte length is equal to $length. Uses \phpseclib\Crypt\Random if it's loaded and mt_rand if it's not. |
||
574 | * |
||
575 | * @param int $length |
||
576 | * |
||
577 | * @return \Jose\Util\BigInteger |
||
578 | */ |
||
579 | private static function _random_number_helper($size) |
||
583 | |||
584 | /** |
||
585 | * Generate a random number. |
||
586 | * |
||
587 | * Returns a random number between $min and $max where $min and $max |
||
588 | * can be defined using one of the two methods: |
||
589 | * |
||
590 | * BigInteger::random($min, $max) |
||
591 | * BigInteger::random($max, $min) |
||
592 | * |
||
593 | * @param \Jose\Util\eger $arg1 |
||
594 | * @param \Jose\Util\eger $arg2 |
||
595 | * |
||
596 | * @return \Jose\Util\BigInteger |
||
597 | */ |
||
598 | public static function random(BigInteger $min, BigInteger $max) |
||
653 | |||
654 | /** |
||
655 | * Normalize. |
||
656 | * |
||
657 | * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision |
||
658 | * |
||
659 | * @param \Jose\Util\BigInteger |
||
660 | * |
||
661 | * @return \Jose\Util\BigInteger |
||
662 | */ |
||
663 | private function _normalize($result) |
||
674 | } |
||
675 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.