Apache Zeta Components Manual :: File Source for bcmath_library.php

Source for file bcmath_library.php

Documentation is available at bcmath_library.php

  1. <?php
  2. /**
  3.  * File containing the ezcAuthenticationBcmathLibrary class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @filesource
  24.  * @package Authentication
  25.  * @version //autogen//
  26.  */
  27.  
  28. /**
  29.  * Wrapper class for the PHP bcmath extension.
  30.  *
  31.  * @package Authentication
  32.  * @version //autogen//
  33.  * @access private
  34.  */
  35. class ezcAuthenticationBcmathLibrary extends ezcAuthenticationBignumLibrary
  36. {
  37.     /**
  38.      * Creates a new big number from $number in the base $base.
  39.      *
  40.      * In the PHP extension bcmath the numbers are used as strings, not as
  41.      * strings, so this function returns the provided $number as it is, and
  42.      * without transforming it in the base $base.
  43.      *
  44.      * @param string $number The number from which to create the result
  45.      * @param int $base The base in which the result will be
  46.      * @return string 
  47.      */
  48.     public function init$number$base 10 )
  49.     {
  50.         return $number;
  51.     }
  52.  
  53.     /**
  54.      * Adds two numbers.
  55.      *
  56.      * @param string $a The first number
  57.      * @param string $b The second number
  58.      * @return string 
  59.      */
  60.     public function add$a$b )
  61.     {
  62.         return bcadd$a$b );
  63.     }
  64.  
  65.     /**
  66.      * Substracts two numbers.
  67.      *
  68.      * @param string $a The first number
  69.      * @param string $b The second number
  70.      * @return string 
  71.      */
  72.     public function sub$a$b )
  73.     {
  74.         return bcsub$a$b );
  75.     }
  76.  
  77.     /**
  78.      * Multiplies two numbers.
  79.      *
  80.      * @param string $a The first number
  81.      * @param string $b The second number
  82.      * @return string 
  83.      */
  84.     public function mul$a$b )
  85.     {
  86.         return bcmul$a$b );
  87.     }
  88.  
  89.     /**
  90.      * Divides two numbers.
  91.      *
  92.      * @param string $a The first number
  93.      * @param string $b The second number
  94.      * @return string 
  95.      */
  96.     public function div$a$b )
  97.     {
  98.         return bcdiv$a$b);
  99.     }
  100.  
  101.     /**
  102.      * Computes $base modulo $modulus.
  103.      *
  104.      * @param string $base The number to apply modulo to
  105.      * @param string $modulus The modulo value to be applied to $base
  106.      * @return string 
  107.      */
  108.     public function mod$base$modulus )
  109.     {
  110.         return bcmod$base$modulus );
  111.     }
  112.  
  113.     /**
  114.      * Computes $base to the power of $exponent.
  115.      *
  116.      * @param string $base The number to be exponentiated
  117.      * @param string $exponent The exponent to apply to $base
  118.      * @return string 
  119.      */
  120.     public function pow$base$exponent )
  121.     {
  122.         return bcpow$base$exponent );
  123.     }
  124.  
  125.     /**
  126.      * Computes $base to the power of $exponent and then applies modulo $modulus.
  127.      *
  128.      * @param string $base The number to be exponentiated
  129.      * @param string $exponent The exponent to apply to $base
  130.      * @param string $modulus The modulo value to be applied to the result
  131.      * @return string 
  132.      */
  133.     public function powmod$base$exponent$modulus )
  134.     {
  135.         return bcpowmod$base$exponent$modulus );
  136.     }
  137.  
  138.     /**
  139.      * Computes the inverse of $number in modulo $modulus.
  140.      *
  141.      * @param string $number The number for which to calculate the inverse
  142.      * @param string $modulus The modulo value in which the inverse is calculated
  143.      * @return string 
  144.      */
  145.     public function invert$number$modulus )
  146.     {
  147.         while bccomp$number)
  148.         
  149.             $number bcadd$number$modulus );
  150.         }
  151.         $r $this->gcd$number$modulus );
  152.         if (int)$r[2=== )
  153.         {
  154.             $a $r[0];
  155.             while bccomp$a)
  156.             {
  157.                 $a bcadd$a$modulus );
  158.             }
  159.             return $a;
  160.         }
  161.         else
  162.         {
  163.             return false;
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Finds the greatest common denominator of two numbers using the extended
  169.      * Euclidean algorithm.
  170.      *
  171.      * The returned array is ( x, y, gcd( a, b ) ), where
  172.      *     x * a + y * b = gcd( a, b )
  173.      *
  174.      * @param string $a The first number
  175.      * @param string $b The second number
  176.      * @return array(string) 
  177.      */
  178.     public function gcd$a$b )
  179.     {
  180.         $x 0;
  181.         $xLast 1;
  182.  
  183.         $y 1;
  184.         $yLast 0;
  185.  
  186.         while bccomp$b!== )
  187.         {
  188.             $temp $b;
  189.             $q bcdiv$a$b);
  190.             $b bcmod$a$b );
  191.             $a $temp;
  192.  
  193.             $temp $x;
  194.             $x bcsub$xLastbcmul$q$x ) );
  195.             $xLast $temp;
  196.  
  197.             $temp $y;
  198.             $y bcsub$yLastbcmul$q$y ) );
  199.             $yLast $temp;
  200.  
  201.         }
  202.         return array$xLast$yLast$a );
  203.     }
  204.  
  205.     /**
  206.      * Compares two numbers.
  207.      *
  208.      * Returns an integer:
  209.      *  - a positive value if $a > $b
  210.      *  - zero if $a == $b
  211.      *  - a negative value if $a < $b
  212.      *
  213.      * @param string $a The first number
  214.      * @param string $b The second number
  215.      * @return int 
  216.      */
  217.     public function cmp$a$b )
  218.     {
  219.         return bccomp$a$b );
  220.     }
  221.  
  222.     /**
  223.      * Returns the string representation of number $a.
  224.      *
  225.      * @param string $number The number to be represented as a string
  226.      * @return string 
  227.      */
  228.     public function toString$number )
  229.     {
  230.         return $number;
  231.     }
  232. }
  233. ?>
Documentation generated by phpDocumentor 1.4.3