BigIntegerBcmath.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the PHPASN1 library.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace FG\Utility;
  9. /**
  10. * Class BigIntegerBcmath
  11. * Integer representation of big numbers using the bcmath library to perform large operations.
  12. * @package FG\Utility
  13. * @internal
  14. */
  15. class BigIntegerBcmath extends BigInteger
  16. {
  17. protected $_str;
  18. public function __clone()
  19. {
  20. // nothing needed to copy
  21. }
  22. protected function _fromString($str)
  23. {
  24. $this->_str = (string)$str;
  25. }
  26. protected function _fromInteger($integer)
  27. {
  28. $this->_str = (string)$integer;
  29. }
  30. public function __toString()
  31. {
  32. return $this->_str;
  33. }
  34. public function toInteger()
  35. {
  36. if ($this->compare(PHP_INT_MAX) > 0 || $this->compare(PHP_INT_MIN) < 0) {
  37. throw new \OverflowException(sprintf('Can not represent %s as integer.', $this->_str));
  38. }
  39. return (int)$this->_str;
  40. }
  41. public function isNegative()
  42. {
  43. return bccomp($this->_str, '0', 0) < 0;
  44. }
  45. protected function _unwrap($number)
  46. {
  47. if ($number instanceof self) {
  48. return $number->_str;
  49. }
  50. return $number;
  51. }
  52. public function compare($number)
  53. {
  54. return bccomp($this->_str, $this->_unwrap($number), 0);
  55. }
  56. public function add($b)
  57. {
  58. $ret = new self();
  59. $ret->_str = bcadd($this->_str, $this->_unwrap($b), 0);
  60. return $ret;
  61. }
  62. public function subtract($b)
  63. {
  64. $ret = new self();
  65. $ret->_str = bcsub($this->_str, $this->_unwrap($b), 0);
  66. return $ret;
  67. }
  68. public function multiply($b)
  69. {
  70. $ret = new self();
  71. $ret->_str = bcmul($this->_str, $this->_unwrap($b), 0);
  72. return $ret;
  73. }
  74. public function modulus($b)
  75. {
  76. $ret = new self();
  77. if ($this->isNegative()) {
  78. // bcmod handles negative numbers differently
  79. $b = $this->_unwrap($b);
  80. $ret->_str = bcsub($b, bcmod(bcsub('0', $this->_str, 0), $b), 0);
  81. }
  82. else {
  83. $ret->_str = bcmod($this->_str, $this->_unwrap($b));
  84. }
  85. return $ret;
  86. }
  87. public function toPower($b)
  88. {
  89. $ret = new self();
  90. $ret->_str = bcpow($this->_str, $this->_unwrap($b), 0);
  91. return $ret;
  92. }
  93. public function shiftRight($bits = 8)
  94. {
  95. $ret = new self();
  96. $ret->_str = bcdiv($this->_str, bcpow('2', $bits));
  97. return $ret;
  98. }
  99. public function shiftLeft($bits = 8) {
  100. $ret = new self();
  101. $ret->_str = bcmul($this->_str, bcpow('2', $bits));
  102. return $ret;
  103. }
  104. public function absoluteValue()
  105. {
  106. $ret = new self();
  107. if (-1 === bccomp($this->_str, '0', 0)) {
  108. $ret->_str = bcsub('0', $this->_str, 0);
  109. }
  110. else {
  111. $ret->_str = $this->_str;
  112. }
  113. return $ret;
  114. }
  115. }