web/lib/Zend/Crypt/Math/BigInteger/Gmp.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Crypt
    16  * @package    Zend_Crypt
    17  * @subpackage Math
    17  * @subpackage Math
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Gmp.php 24593 2012-01-05 20:35:02Z matthew $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Crypt_Math_BigInteger_Interface
    24  * @see Zend_Crypt_Math_BigInteger_Interface
    25  */
    25  */
    31  * Zend_Crypt_Math_BigInteger_Gmp is a wrapper across the PHP BCMath
    31  * Zend_Crypt_Math_BigInteger_Gmp is a wrapper across the PHP BCMath
    32  * extension.
    32  * extension.
    33  *
    33  *
    34  * @category   Zend
    34  * @category   Zend
    35  * @package    Zend_Crypt
    35  * @package    Zend_Crypt
    36  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  */
    38  */
    39 class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface
    39 class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface
    40 {
    40 {
    41 
    41 
    51     }
    51     }
    52 
    52 
    53     /**
    53     /**
    54      * Adds two arbitrary precision numbers
    54      * Adds two arbitrary precision numbers
    55      *
    55      *
    56      * @param string $left_operand
    56      * @param resource $left_operand
    57      * @param string $right_operand
    57      * @param resource $right_operand
    58      * @return string
    58      * @return string
    59      */
    59      */
    60     public function add($left_operand, $right_operand)
    60     public function add($left_operand, $right_operand)
    61     {
    61     {
    62         $result = gmp_add($left_operand, $right_operand);
    62         $result = gmp_add($left_operand, $right_operand);
    63         return gmp_strval($result);
    63         return gmp_strval($result);
    64     }
    64     }
    65 
    65 
    66     /**
    66     /**
    67      * @param string $left_operand
    67      * Subtract numbers
    68      * @param string $right_operand
    68      *
       
    69      * @param resource $left_operand
       
    70      * @param resource $right_operand
    69      * @return string
    71      * @return string
    70      */
    72      */
    71     public function subtract($left_operand, $right_operand)
    73     public function subtract($left_operand, $right_operand)
    72     {
    74     {
    73         $result = gmp_sub($left_operand, $right_operand);
    75         $result = gmp_sub($left_operand, $right_operand);
    76 
    78 
    77     /**
    79     /**
    78      * Compare two big integers and returns result as an integer where 0 means
    80      * Compare two big integers and returns result as an integer where 0 means
    79      * both are identical, 1 that left_operand is larger, or -1 that
    81      * both are identical, 1 that left_operand is larger, or -1 that
    80      * right_operand is larger.
    82      * right_operand is larger.
    81      * @param string $left_operand
    83      *
    82      * @param string $right_operand
    84      * @param resource $left_operand
       
    85      * @param resource $right_operand
    83      * @return int
    86      * @return int
    84      */
    87      */
    85     public function compare($left_operand, $right_operand)
    88     public function compare($left_operand, $right_operand)
    86     {
    89     {
    87         $result = gmp_cmp($left_operand, $right_operand);
    90         $result = gmp_cmp($left_operand, $right_operand);
    89     }
    92     }
    90 
    93 
    91     /**
    94     /**
    92      * Divide two big integers and return result or NULL if the denominator
    95      * Divide two big integers and return result or NULL if the denominator
    93      * is zero.
    96      * is zero.
    94      * @param string $left_operand
    97      *
    95      * @param string $right_operand
    98      * @param resource $left_operand
       
    99      * @param resource $right_operand
    96      * @return string|null
   100      * @return string|null
    97      */
   101      */
    98     public function divide($left_operand, $right_operand)
   102     public function divide($left_operand, $right_operand)
    99     {
   103     {
   100         $result = gmp_div($left_operand, $right_operand);
   104         $result = gmp_div($left_operand, $right_operand);
   101         return gmp_strval($result);
   105         return gmp_strval($result);
   102     }
   106     }
   103 
   107 
   104     /**
   108     /**
   105      * @param string $left_operand
   109      * Modulo operation
   106      * @param string $right_operand
   110      *
       
   111      * @param resource $left_operand
       
   112      * @param resource $modulus
       
   113      * @internal param string $right_operand
   107      * @return string
   114      * @return string
   108      */
   115      */
   109     public function modulus($left_operand, $modulus)
   116     public function modulus($left_operand, $modulus)
   110     {
   117     {
   111         $result = gmp_mod($left_operand, $modulus);
   118         $result = gmp_mod($left_operand, $modulus);
   112         return gmp_strval($result);
   119         return gmp_strval($result);
   113     }
   120     }
   114 
   121 
   115     /**
   122     /**
   116      * @param string $left_operand
   123      * Multiply numbers
   117      * @param string $right_operand
   124      *
       
   125      * @param resource $left_operand
       
   126      * @param resource $right_operand
   118      * @return string
   127      * @return string
   119      */
   128      */
   120     public function multiply($left_operand, $right_operand)
   129     public function multiply($left_operand, $right_operand)
   121     {
   130     {
   122         $result = gmp_mul($left_operand, $right_operand);
   131         $result = gmp_mul($left_operand, $right_operand);
   123         return gmp_strval($result);
   132         return gmp_strval($result);
   124     }
   133     }
   125 
   134 
   126     /**
   135     /**
   127      * @param string $left_operand
   136      * Raise number into power
   128      * @param string $right_operand
   137      *
       
   138      * @param resource $left_operand
       
   139      * @param int      $right_operand
   129      * @return string
   140      * @return string
   130      */
   141      */
   131     public function pow($left_operand, $right_operand)
   142     public function pow($left_operand, $right_operand)
   132     {
   143     {
   133         $result = gmp_pow($left_operand, $right_operand);
   144         $result = gmp_pow($left_operand, $right_operand);
   134         return gmp_strval($result);
   145         return gmp_strval($result);
   135     }
   146     }
   136 
   147 
   137     /**
   148     /**
   138      * @param string $left_operand
   149      * Raise number into power with modulo
   139      * @param string $right_operand
   150      *
       
   151      * @param resource $left_operand
       
   152      * @param resource $right_operand
       
   153      * @param resource $modulus
   140      * @return string
   154      * @return string
   141      */
   155      */
   142     public function powmod($left_operand, $right_operand, $modulus)
   156     public function powmod($left_operand, $right_operand, $modulus)
   143     {
   157     {
   144         $result = gmp_powm($left_operand, $right_operand, $modulus);
   158         $result = gmp_powm($left_operand, $right_operand, $modulus);
   145         return gmp_strval($result);
   159         return gmp_strval($result);
   146     }
   160     }
   147 
   161 
   148     /**
   162     /**
   149      * @param string $left_operand
   163      * Calculate square root
   150      * @param string $right_operand
   164      *
       
   165      * @param $operand
   151      * @return string
   166      * @return string
   152      */
   167      */
   153     public function sqrt($operand)
   168     public function sqrt($operand)
   154     {
   169     {
   155         $result = gmp_sqrt($operand);
   170         $result = gmp_sqrt($operand);
   156         return gmp_strval($result);
   171         return gmp_strval($result);
   157     }
   172     }
   158 
   173 
   159 
   174     /**
       
   175      * @param string $operand
       
   176      * @return string
       
   177      */
   160     public function binaryToInteger($operand)
   178     public function binaryToInteger($operand)
   161     {
   179     {
   162         $result = '0';
   180         $result = '0';
   163         while (strlen($operand)) {
   181         while (strlen($operand)) {
   164             $ord = ord(substr($operand, 0, 1));
   182             $ord = ord(substr($operand, 0, 1));
   166             $operand = substr($operand, 1);
   184             $operand = substr($operand, 1);
   167         }
   185         }
   168         return gmp_strval($result);
   186         return gmp_strval($result);
   169     }
   187     }
   170 
   188 
   171 
   189     /**
       
   190      * @param resource $operand GMP number resource
       
   191      * @return string
       
   192      */
   172     public function integerToBinary($operand)
   193     public function integerToBinary($operand)
   173     {
   194     {
   174         $bigInt = gmp_strval($operand, 16);
   195         $bigInt = gmp_strval($operand, 16);
   175         if (strlen($bigInt) % 2 != 0) {
   196         if (strlen($bigInt) % 2 != 0) {
   176             $bigInt = '0' . $bigInt;
   197             $bigInt = '0' . $bigInt;
   179         }
   200         }
   180         $return = pack("H*", $bigInt);
   201         $return = pack("H*", $bigInt);
   181         return $return;
   202         return $return;
   182     }
   203     }
   183 
   204 
   184 
   205     /**
       
   206      * @param string $operand
       
   207      * @return string
       
   208      */
   185     public function hexToDecimal($operand)
   209     public function hexToDecimal($operand)
   186     {
   210     {
   187         $return = '0';
   211         $return = '0';
   188         while(strlen($hex)) {
   212         while(strlen($hex)) {
   189             $hex = hexdec(substr($operand, 0, 4));
   213             $hex = hexdec(substr($operand, 0, 4));