GMP.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /******************************************************************************
  3. * This file is part of the Phactor PHP project. You can always find the latest
  4. * version of this class and project at: https://github.com/ionux/phactor
  5. *
  6. * Copyright (c) 2015-2018 Rich Morgan, rich@richmorgan.me
  7. *
  8. * The MIT License (MIT)
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  11. * this software and associated documentation files (the "Software"), to deal in
  12. * the Software without restriction, including without limitation the rights to
  13. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  14. * the Software, and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  22. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  23. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  24. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. ******************************************************************************/
  27. namespace Phactor;
  28. /**
  29. * GMP math class used by the main math class.
  30. *
  31. * @author Rich Morgan <rich@richmorgan.me>
  32. */
  33. final class GMP
  34. {
  35. /**
  36. * Adds two arbitrary precision numbers.
  37. *
  38. * @param string $a The first number.
  39. * @param string $b The second number.
  40. * @return string
  41. */
  42. public function add($a, $b)
  43. {
  44. return gmp_strval(gmp_add($a, $b));
  45. }
  46. /**
  47. * Multiplies two arbitrary precision numbers.
  48. *
  49. * @param string $a The first number.
  50. * @param string $b The second number.
  51. * @return string
  52. */
  53. public function mul($a, $b)
  54. {
  55. return gmp_strval(gmp_mul($a, $b));
  56. }
  57. /**
  58. * Divides two arbitrary precision numbers.
  59. *
  60. * @param string $a The first number.
  61. * @param string $b The second number.
  62. * @return string
  63. */
  64. public function div($a, $b)
  65. {
  66. return gmp_strval(gmp_div($a, $b));
  67. }
  68. /**
  69. * Subtracts two arbitrary precision numbers.
  70. *
  71. * @param string $a The first number.
  72. * @param string $b The second number.
  73. * @return string
  74. */
  75. public function sub($a, $b)
  76. {
  77. return gmp_strval(gmp_sub($a, $b));
  78. }
  79. /**
  80. * Calculates the modulo of two numbers.
  81. * There's a slight quirk in GMP's implementation
  82. * so this returns a mathematically correct answer
  83. * if you specify the $correct parameter.
  84. *
  85. * @param string $a The first number.
  86. * @param string $b The second number.
  87. * @param boolean $correct Flag to calculate mathematically correct modulo.
  88. * @return string
  89. */
  90. public function mod($a, $b, $correct = false)
  91. {
  92. if ($correct === true) {
  93. if (gmp_cmp($a, '0') < 0) {
  94. return gmp_strval(gmp_sub(gmp_mod($a, $b), $a));
  95. }
  96. }
  97. return gmp_strval(gmp_mod($a, $b));
  98. }
  99. /**
  100. * Raises an arbitrary precision number to another,
  101. * reduced by a specified modulus.
  102. *
  103. * @param string $a The first number.
  104. * @param string $b The exponent.
  105. * @param string $c The modulus.
  106. * @return string The result of the operation.
  107. */
  108. public function powmod($a, $b, $c)
  109. {
  110. return gmp_strval(gmp_powm($a, $b, $c));
  111. }
  112. /**
  113. * Compares two arbitrary precision numbers.
  114. *
  115. * @param string $a The first number.
  116. * @param string $b The second number.
  117. * @return integer
  118. */
  119. public function comp($a, $b)
  120. {
  121. return gmp_cmp($a, $b);
  122. }
  123. /**
  124. * GMP's inverse modulo function, where ax = 1(mod p).
  125. *
  126. * @param string $a The number to inverse modulo.
  127. * @param string $b The modulus.
  128. * @return string
  129. */
  130. public function inv($a, $b)
  131. {
  132. return gmp_strval(gmp_invert($a, $b));
  133. }
  134. /**
  135. * Returns the string value of a GMP resource.
  136. *
  137. * @param mixed $a Number to normalize.
  138. * @return string
  139. */
  140. public function normalize($a)
  141. {
  142. return gmp_strval($a);
  143. }
  144. /**
  145. * Raises 'a' to the power 'b'.
  146. *
  147. * @param string $a The first number.
  148. * @param string $b The second number.
  149. * @return string
  150. */
  151. public function power($a, $b)
  152. {
  153. return gmp_strval(gmp_pow($a, $b));
  154. }
  155. /**
  156. * Calculates & returns the integer portion of the square root.
  157. *
  158. * @param string $a The first number.
  159. * @return string
  160. */
  161. public function sqrt($a)
  162. {
  163. return gmp_strval(gmp_sqrt($a));
  164. }
  165. }