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

Source for file bignum_library.php

Documentation is available at bignum_library.php

  1. <?php
  2. /**
  3.  * File containing the ezcAuthenticationBignumLibrary 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.  * Abstract class for large number support.
  30.  *
  31.  * Classes which extend this class must implement all the abstract methods.
  32.  *
  33.  * @package Authentication
  34.  * @version //autogen//
  35.  * @access private
  36.  */
  37. abstract class ezcAuthenticationBignumLibrary
  38. {
  39.     /**
  40.      * Creates a new big number from $number in the base $base.
  41.      *
  42.      * The number $number can be integer or string.
  43.      *
  44.      * @param mixed $number The number from which to create the result
  45.      * @param int $base The base in which the result will be
  46.      * @return mixed 
  47.      */
  48.     abstract public function init$number$base 10 );
  49.  
  50.     /**
  51.      * Adds two numbers.
  52.      *
  53.      * @param mixed $a The first number
  54.      * @param mixed $b The second number
  55.      * @return mixed 
  56.      */
  57.     abstract public function add$a$b );
  58.  
  59.     /**
  60.      * Substracts two numbers.
  61.      *
  62.      * @param mixed $a The first  number
  63.      * @param mixed $b The second  number
  64.      * @return mixed 
  65.      */
  66.     abstract public function sub$a$b );
  67.  
  68.     /**
  69.      * Multiplies two numbers.
  70.      *
  71.      * @param mixed $a The first number
  72.      * @param mixed $b The second number
  73.      * @return mixed 
  74.      */
  75.     abstract public function mul$a$b );
  76.  
  77.     /**
  78.      * Divides two numbers.
  79.      *
  80.      * @param mixed $a The first number
  81.      * @param mixed $b The second number
  82.      * @return mixed 
  83.      */
  84.     abstract public function div$a$b );
  85.  
  86.     /**
  87.      * Computes $base modulo $modulus.
  88.      *
  89.      * @param mixed $base The number to apply modulo to
  90.      * @param mixed $modulus The modulo value to be applied to $base
  91.      * @return mixed 
  92.      */
  93.     abstract public function mod$base$modulus );
  94.  
  95.     /**
  96.      * Computes $base to the power of $exponent.
  97.      *
  98.      * @param mixed $base The number to be exponentiated
  99.      * @param mixed $exponent The exponent to apply to $base
  100.      * @return mixed 
  101.      */
  102.     abstract public function pow$base$exponent );
  103.  
  104.     /**
  105.      * Computes $base to the power of $exponent and then applies modulo $modulus.
  106.      *
  107.      * @param mixed $base The number to be exponentiated
  108.      * @param mixed $exponent The exponent to apply to $base
  109.      * @param mixed $modulus The modulo value to be applied to the result
  110.      * @return mixed 
  111.      */
  112.     abstract public function powmod$base$exponent$modulus );
  113.  
  114.     /**
  115.      * Computes the inverse of $number in modulo $modulus.
  116.      *
  117.      * @param mixed $number The number for which to calculate the inverse
  118.      * @param mixed $modulus The modulo value in which the inverse is calculated
  119.      * @return mixed 
  120.      */
  121.     abstract public function invert$number$modulus );
  122.  
  123.     /**
  124.      * Finds the greatest common denominator of two numbers using the extended
  125.      * Euclidean algorithm.
  126.      *
  127.      * The returned array is ( a0, b0, gcd( a, b ) ), where
  128.      *     a0 * a + b0 * b = gcd( a, b )
  129.      *
  130.      * @param mixed $a The first number
  131.      * @param mixed $b The second number
  132.      * @return array(mixed) 
  133.      */
  134.     abstract public function gcd$a$b );
  135.  
  136.     /**
  137.      * Compares two  numbers.
  138.      *
  139.      * Returns an integer:
  140.      *  - a positive value if $a > $b
  141.      *  - zero if $a == $b
  142.      *  - a negative value if $a < $b
  143.      *
  144.      * @param mixed $a The first number
  145.      * @param mixed $b The second number
  146.      * @return int 
  147.      */
  148.     abstract public function cmp$a$b );
  149.  
  150.     /**
  151.      * Returns the string representation of number $a.
  152.      *
  153.      * @param mixed $a The number to be represented as a string
  154.      * @return string 
  155.      */
  156.     abstract public function toString$a );
  157.  
  158.     /**
  159.      * Converts a binary value to a decimal value.
  160.      *
  161.      * @param mixed $bin Binary value
  162.      * @return string 
  163.      */
  164.     public function binToDec$bin )
  165.     {
  166.         $dec $this->init);
  167.         while strlen$bin ) )
  168.         {
  169.             $i ordsubstr$bin0) );
  170.             $dec $this->add$this->mul$dec256 )$i );
  171.             $bin substr$bin);
  172.         }
  173.         return $this->toString$dec );
  174.     }
  175.  
  176.     /**
  177.      * Converts an hexadecimal value to a decimal value.
  178.      *
  179.      * @param mixed $hex Hexadecimal value
  180.      * @return string 
  181.      */
  182.     public function hexToDec$hex )
  183.     {
  184.         $dec $this->init);
  185.         while strlen$hex ) )
  186.         {
  187.             $i hexdecsubstr$hex0) );
  188.             $dec $this->add$this->mul$dec65536 )$i );
  189.             $hex substr$hex);
  190.         }
  191.         return $this->toString$dec );
  192.     }
  193.  
  194.     /**
  195.      * Returns bignum $number in big-endian signed two's complement.
  196.      *
  197.      * @param mixed $number The number to convert
  198.      * @return mixed 
  199.      */
  200.     public function btwoc$number )
  201.     {
  202.         $cmp $this->cmp$number);
  203.         if $cmp )
  204.         {
  205.             return null;
  206.         }
  207.         elseif $cmp === )
  208.         {
  209.             return "\x00";
  210.         }
  211.  
  212.         $bytes array();
  213.         while $this->cmp$number)
  214.         {
  215.             array_unshift$bytes$this->mod$number256 ) );
  216.             $number $this->div$number256 );
  217.         }
  218.  
  219.         if $bytes && $bytes[0127 ) )
  220.         {
  221.             array_unshift$bytes);
  222.         }
  223.  
  224.         $string '';
  225.         foreach $bytes as $byte )
  226.         {
  227.             $string .= pack'C'$byte );
  228.         }
  229.  
  230.         return $string;
  231.     }
  232.  
  233.     /**
  234.      * Generates a random bignum.
  235.      *
  236.      * @param mixed $stop The top limit of the random number
  237.      * @return mixed 
  238.      */
  239.     public function rand$stop )
  240.     {
  241.         $num_bytes strlen$this->btwoc$stop ) );
  242.         $bytes '';
  243.         for $i 0$i $num_bytes$i += )
  244.         {
  245.             $bytes .= pack'L'mt_rand() );
  246.         }
  247.         $bytes "\x00" substr$bytes0$num_bytes );
  248.         $n $this->binToDec$bytes );
  249.         return $this->mod$n$stop );
  250.     }
  251. }
  252. ?>
Documentation generated by phpDocumentor 1.4.3