bcpowmod.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // $Id: bcpowmod.php,v 1.1 2007-07-02 04:19:55 terrafrost Exp $
  3. /**
  4. * Replace bcpowmod()
  5. *
  6. * @category PHP
  7. * @package PHP_Compat
  8. * @license LGPL - http://www.gnu.org/licenses/lgpl.html
  9. * @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
  10. * @link http://php.net/function.bcpowmod
  11. * @author Sara Golemon <pollita@php.net>
  12. * @version $Revision: 1.1 $
  13. * @since PHP 5.0.0
  14. * @require PHP 4.0.0 (user_error)
  15. */
  16. function php_compat_bcpowmod($x, $y, $modulus, $scale = 0)
  17. {
  18. // Sanity check
  19. if (!is_scalar($x)) {
  20. user_error('bcpowmod() expects parameter 1 to be string, ' .
  21. gettype($x) . ' given', E_USER_WARNING);
  22. return false;
  23. }
  24. if (!is_scalar($y)) {
  25. user_error('bcpowmod() expects parameter 2 to be string, ' .
  26. gettype($y) . ' given', E_USER_WARNING);
  27. return false;
  28. }
  29. if (!is_scalar($modulus)) {
  30. user_error('bcpowmod() expects parameter 3 to be string, ' .
  31. gettype($modulus) . ' given', E_USER_WARNING);
  32. return false;
  33. }
  34. if (!is_scalar($scale)) {
  35. user_error('bcpowmod() expects parameter 4 to be integer, ' .
  36. gettype($scale) . ' given', E_USER_WARNING);
  37. return false;
  38. }
  39. $t = '1';
  40. while (bccomp($y, '0')) {
  41. if (bccomp(bcmod($y, '2'), '0')) {
  42. $t = bcmod(bcmul($t, $x), $modulus);
  43. $y = bcsub($y, '1');
  44. }
  45. $x = bcmod(bcmul($x, $x), $modulus);
  46. $y = bcdiv($y, '2');
  47. }
  48. return $t;
  49. }
  50. // Define
  51. if (!function_exists('bcpowmod')) {
  52. function bcpowmod($x, $y, $modulus, $scale = 0)
  53. {
  54. return php_compat_bcpowmod($x, $y, $modulus, $scale);
  55. }
  56. }