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 | final class BigInteger |
||
| 15 | { |
||
| 16 | /**#@+ |
||
| 17 | * Array constants |
||
| 18 | * |
||
| 19 | * Rather than create a thousands and thousands of new BigInteger objects in repeated function calls to add() and |
||
| 20 | * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them. |
||
| 21 | * |
||
| 22 | */ |
||
| 23 | /** |
||
| 24 | * $result[self::VALUE] contains the value. |
||
| 25 | */ |
||
| 26 | const VALUE = 0; |
||
| 27 | /** |
||
| 28 | * $result[self::SIGN] contains the sign. |
||
| 29 | */ |
||
| 30 | const SIGN = 1; |
||
| 31 | /**#@-*/ |
||
| 32 | |||
| 33 | /**#@+ |
||
| 34 | * Static properties used by the pure-PHP implementation. |
||
| 35 | * |
||
| 36 | * @see __construct() |
||
| 37 | */ |
||
| 38 | private static $base; |
||
|
|
|||
| 39 | private static $baseFull; |
||
| 40 | private static $maxDigit; |
||
| 41 | private static $msb; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * $max10 in greatest $max10Len satisfying |
||
| 45 | * $max10 = 10**$max10Len <= 2**$base. |
||
| 46 | */ |
||
| 47 | private static $max10; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * $max10Len in greatest $max10Len satisfying |
||
| 51 | * $max10 = 10**$max10Len <= 2**$base. |
||
| 52 | */ |
||
| 53 | private static $max10Len; |
||
| 54 | private static $maxDigit2; |
||
| 55 | /**#@-*/ |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds the BigInteger's value. |
||
| 59 | * |
||
| 60 | * @var resource |
||
| 61 | */ |
||
| 62 | private $value; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Holds the BigInteger's magnitude. |
||
| 66 | * |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | private $is_negative = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Precision. |
||
| 73 | */ |
||
| 74 | private $precision = -1; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Precision Bitmask. |
||
| 78 | */ |
||
| 79 | private $bitmask = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers. |
||
| 83 | * |
||
| 84 | * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using |
||
| 85 | * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. |
||
| 86 | * |
||
| 87 | * Here's an example: |
||
| 88 | * <code> |
||
| 89 | * <?php |
||
| 90 | * $a = new \Jose\Util\in base-16 |
||
| 91 | * |
||
| 92 | * echo $a->toString(); // outputs 50 |
||
| 93 | * ?> |
||
| 94 | * </code> |
||
| 95 | * |
||
| 96 | * @param $x base-10 number or base-$base number if $base set. |
||
| 97 | * @param int $base |
||
| 98 | */ |
||
| 99 | public function __construct($x = 0, $base = 10) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Converts a BigInteger to a byte string (eg. base-256). |
||
| 199 | * |
||
| 200 | * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're |
||
| 201 | * saved as two's compliment. |
||
| 202 | * |
||
| 203 | * Here's an example: |
||
| 204 | * <code> |
||
| 205 | * <?php |
||
| 206 | * $a = new \Jose\Util\ger('65'); |
||
| 207 | * |
||
| 208 | * echo $a->toBytes(); // outputs chr(65) |
||
| 209 | * ?> |
||
| 210 | * </code> |
||
| 211 | * |
||
| 212 | * @param bool $twos_compliment |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | * |
||
| 216 | */ |
||
| 217 | public function toBytes($twos_compliment = false) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Adds two BigIntegers. |
||
| 254 | * |
||
| 255 | * Here's an example: |
||
| 256 | * <code> |
||
| 257 | * <?php |
||
| 258 | * $a = new \Jose\Util\ger('10'); |
||
| 259 | * $b = new \Jose\Util\ger('20'); |
||
| 260 | * |
||
| 261 | * $c = $a->add($b); |
||
| 262 | * |
||
| 263 | * echo $c->toString(); // outputs 30 |
||
| 264 | * ?> |
||
| 265 | * </code> |
||
| 266 | * |
||
| 267 | * @param \Jose\Util\BigInteger $y |
||
| 268 | * |
||
| 269 | * @return \Jose\Util\BigInteger |
||
| 270 | * |
||
| 271 | */ |
||
| 272 | public function add(BigInteger $y) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Subtracts two BigIntegers. |
||
| 282 | * |
||
| 283 | * Here's an example: |
||
| 284 | * <code> |
||
| 285 | * <?php |
||
| 286 | * $a = new \Jose\Util\ger('10'); |
||
| 287 | * $b = new \Jose\Util\ger('20'); |
||
| 288 | * |
||
| 289 | * $c = $a->subtract($b); |
||
| 290 | * |
||
| 291 | * echo $c->toString(); // outputs -10 |
||
| 292 | * ?> |
||
| 293 | * </code> |
||
| 294 | * |
||
| 295 | * @param \Jose\Util\BigInteger $y |
||
| 296 | * |
||
| 297 | * @return \Jose\Util\BigInteger |
||
| 298 | * |
||
| 299 | */ |
||
| 300 | public function subtract(BigInteger $y) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Multiplies two BigIntegers. |
||
| 310 | * |
||
| 311 | * Here's an example: |
||
| 312 | * <code> |
||
| 313 | * <?php |
||
| 314 | * $a = new \Jose\Util\ger('10'); |
||
| 315 | * $b = new \Jose\Util\ger('20'); |
||
| 316 | * |
||
| 317 | * $c = $a->multiply($b); |
||
| 318 | * |
||
| 319 | * echo $c->toString(); // outputs 200 |
||
| 320 | * ?> |
||
| 321 | * </code> |
||
| 322 | * |
||
| 323 | * @param \Jose\Util\BigInteger $x |
||
| 324 | * |
||
| 325 | * @return \Jose\Util\BigInteger |
||
| 326 | */ |
||
| 327 | public function multiply(BigInteger $x) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Divides two BigIntegers. |
||
| 337 | * |
||
| 338 | * Returns an array whose first element contains the quotient and whose second element contains the |
||
| 339 | * "common residue". If the remainder would be positive, the "common residue" and the remainder are the |
||
| 340 | * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder |
||
| 341 | * and the divisor (basically, the "common residue" is the first positive modulo). |
||
| 342 | * |
||
| 343 | * Here's an example: |
||
| 344 | * <code> |
||
| 345 | * <?php |
||
| 346 | * $a = new \Jose\Util\ger('10'); |
||
| 347 | * $b = new \Jose\Util\ger('20'); |
||
| 348 | * |
||
| 349 | * list($quotient, $remainder) = $a->divide($b); |
||
| 350 | * |
||
| 351 | * echo $quotient->toString(); // outputs 0 |
||
| 352 | * echo "\r\n"; |
||
| 353 | * echo $remainder->toString(); // outputs 10 |
||
| 354 | * ?> |
||
| 355 | * </code> |
||
| 356 | * |
||
| 357 | * @param \Jose\Util\BigInteger $y |
||
| 358 | * |
||
| 359 | * @return @return \Jose\Util\BigInteger[] |
||
| 360 | * |
||
| 361 | */ |
||
| 362 | public function divide(BigInteger $y) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Performs modular exponentiation. |
||
| 378 | * |
||
| 379 | * Here's an example: |
||
| 380 | * <code> |
||
| 381 | * <?php |
||
| 382 | * $a = new \Jose\Util\ger('10'); |
||
| 383 | * $b = new \Jose\Util\ger('20'); |
||
| 384 | * $c = new \Jose\Util\ger('30'); |
||
| 385 | * |
||
| 386 | * $c = $a->modPow($b, $c); |
||
| 387 | * |
||
| 388 | * echo $c->toString(); // outputs 10 |
||
| 389 | * ?> |
||
| 390 | * </code> |
||
| 391 | * |
||
| 392 | * @param \Jose\Util\BigInteger $e |
||
| 393 | * @param \Jose\Util\BigInteger $n |
||
| 394 | * |
||
| 395 | * @return \Jose\Util\BigInteger |
||
| 396 | * |
||
| 397 | * and although the approach involving repeated squaring does vastly better, it, too, is impractical |
||
| 398 | * for our purposes. The reason being that division - by far the most complicated and time-consuming |
||
| 399 | * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. |
||
| 400 | * |
||
| 401 | * Modular reductions resolve this issue. Although an individual modular reduction takes more time |
||
| 402 | * then an individual division, when performed in succession (with the same modulo), they're a lot faster. |
||
| 403 | * |
||
| 404 | * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, |
||
| 405 | * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the |
||
| 406 | * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because |
||
| 407 | * the product of two odd numbers is odd), but what about when RSA isn't used? |
||
| 408 | * |
||
| 409 | * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a |
||
| 410 | * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the |
||
| 411 | * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, |
||
| 412 | * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and |
||
| 413 | * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. |
||
| 414 | * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. |
||
| 415 | */ |
||
| 416 | public function modPow(BigInteger $e, BigInteger $n) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Calculates modular inverses. |
||
| 439 | * |
||
| 440 | * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. |
||
| 441 | * |
||
| 442 | * Here's an example: |
||
| 443 | * <code> |
||
| 444 | * <?php |
||
| 445 | * $a = new \Jose\Util\teger(30); |
||
| 446 | * $b = new \Jose\Util\teger(17); |
||
| 447 | * |
||
| 448 | * $c = $a->modInverse($b); |
||
| 449 | * echo $c->toString(); // outputs 4 |
||
| 450 | * |
||
| 451 | * echo "\r\n"; |
||
| 452 | * |
||
| 453 | * $d = $a->multiply($c); |
||
| 454 | * list(, $d) = $d->divide($b); |
||
| 455 | * echo $d; // outputs 1 (as per the definition of modular inverse) |
||
| 456 | * ?> |
||
| 457 | * </code> |
||
| 458 | * |
||
| 459 | * @param \Jose\Util\BigInteger $n |
||
| 460 | * |
||
| 461 | * @return \Jose\Util\BigInteger|bool |
||
| 462 | * |
||
| 463 | */ |
||
| 464 | public function modInverse(BigInteger $n) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Absolute value. |
||
| 474 | * |
||
| 475 | * @return \Jose\Util\BigInteger |
||
| 476 | */ |
||
| 477 | public function abs() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Compares two numbers. |
||
| 488 | * |
||
| 489 | * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is |
||
| 490 | * demonstrated thusly: |
||
| 491 | * |
||
| 492 | * $x > $y: $x->compare($y) > 0 |
||
| 493 | * $x < $y: $x->compare($y) < 0 |
||
| 494 | * $x == $y: $x->compare($y) == 0 |
||
| 495 | * |
||
| 496 | * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). |
||
| 497 | * |
||
| 498 | * @param \Jose\Util\BigInteger $y |
||
| 499 | * |
||
| 500 | * @return int < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal. |
||
| 501 | * |
||
| 502 | */ |
||
| 503 | public function compare(BigInteger $y) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Logical Left Shift. |
||
| 510 | * |
||
| 511 | * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. |
||
| 512 | * |
||
| 513 | * @param int $shift |
||
| 514 | * |
||
| 515 | * @return \Jose\Util\BigInteger |
||
| 516 | * |
||
| 517 | */ |
||
| 518 | public function bitwise_leftShift($shift) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Generates a random BigInteger. |
||
| 535 | * |
||
| 536 | * Byte length is equal to $length. Uses \phpseclib\Crypt\Random if it's loaded and mt_rand if it's not. |
||
| 537 | * |
||
| 538 | * @param int $size |
||
| 539 | * |
||
| 540 | * @return \Jose\Util\BigInteger |
||
| 541 | */ |
||
| 542 | private static function _random_number_helper($size) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Generate a random number. |
||
| 549 | * |
||
| 550 | * Returns a random number between $min and $max where $min and $max |
||
| 551 | * can be defined using one of the two methods: |
||
| 552 | * |
||
| 553 | * BigInteger::random($min, $max) |
||
| 554 | * BigInteger::random($max, $min) |
||
| 555 | * |
||
| 556 | * @param \Jose\Util\BigInteger $min |
||
| 557 | * @param \Jose\Util\BigInteger $max |
||
| 558 | * |
||
| 559 | * @return \Jose\Util\BigInteger |
||
| 560 | */ |
||
| 561 | public static function random(BigInteger $min, BigInteger $max) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Normalize. |
||
| 619 | * |
||
| 620 | * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision |
||
| 621 | * |
||
| 622 | * @param \Jose\Util\BigInteger $result |
||
| 623 | * |
||
| 624 | * @return \Jose\Util\BigInteger |
||
| 625 | */ |
||
| 626 | private function _normalize($result) |
||
| 637 | } |
||
| 638 |
This check marks private properties in classes that are never used. Those properties can be removed.