includes/math.php
author Dan
Fri, 21 Aug 2009 11:47:26 -0400
changeset 1085 3343a05e7e5b
parent 1081 745200a9cc2a
child 1227 bdac73ed481e
permissions -rw-r--r--
SECURITY: Comments: fix poor sanitization of subject on initial submit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
467
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     1
<?php
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     2
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     3
/*
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
1081
745200a9cc2a Fixed some upgrade bugs; added support for choosing one's own date/time formats; rebrand as 1.1.7
Dan
parents: 801
diff changeset
     5
 * Copyright (C) 2006-2009 Dan Fuhry
467
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     6
 * diffiehellman.php - Diffie Hellman key exchange and supporting functions
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     7
 *
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    10
 *
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    13
 */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    14
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    15
/**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    16
 * EnanoMath GMP backend
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    17
 */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    18
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    19
class EnanoMath_GMP
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    20
{
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    21
  var $api = 'GMP';
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    22
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    23
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    24
   * Initializes a number to a GMP integer.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    25
   * @param string String representation of the number
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    26
   * @param int Base the number is currently in, defaults to 10
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    27
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    28
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    29
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    30
  function init($int, $base = 10)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    31
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    32
    return ( is_resource($int) ) ? $int : gmp_init($int, $base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    33
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    34
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    35
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    36
   * Converts a number from a GMP integer to a string
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    37
   * @param resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    38
   * @param int Base, default is 10
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    39
   * @return string
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    40
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    41
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    42
  function str($int, $base = 10)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    43
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    44
    return ( is_string($int) ) ? $int : gmp_strval($int, $base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    45
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    46
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    47
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    48
   * Converts a number between bases.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    49
   * @param resource BigInt to convert
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    50
   * @param int Base to convert from
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    51
   * @param int Base to convert to
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    52
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    53
   
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    54
  function basecon($int, $from, $to)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    55
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    56
    return $this->init(gmp_strval(gmp_init($this->str($int), $from), $to));
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    57
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    58
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    59
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    60
   * Generates a random integer.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    61
   * @param int Length
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    62
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    63
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    64
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    65
  function random($len)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    66
  {
768
111fd7a4415c Fixed: private keys were way too long in EnanoMath
Dan
parents: 685
diff changeset
    67
    return gmp_random($len / 8);
467
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    68
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    69
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    70
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    71
   * Powmod operation (calculates (a ^ b) mod m)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    72
   * @param resource a
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    73
   * @param resource b
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    74
   * @param resource m
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    75
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    76
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    77
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    78
  function powmod($a, $b, $m)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    79
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    80
    $a = $this->init($a);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    81
    $b = $this->init($b);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    82
    $m = $this->init($m);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    83
    return ( function_exists('gmp_powm') ) ? gmp_powm($a, $b, $m) : gmp_mod(gmp_pow($a, $b), $m);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    84
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    85
}
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    86
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    87
/**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    88
 * EnanoMath big_int backend
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    89
 */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    90
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    91
class EnanoMath_BigInt
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    92
{
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    93
  var $api = 'big_int';
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    94
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    95
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    96
   * Initializes a number to a BigInt integer.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    97
   * @param string String representation of the number
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    98
   * @param int Base the number is in, defaults to 10
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
    99
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   100
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   101
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   102
  function init($int, $base = 10)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   103
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   104
    return (is_resource($int)) ? $int : bi_from_str($int, $base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   105
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   106
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   107
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   108
   * Converts a number from a BigInt integer to a string
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   109
   * @param resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   110
   * @param int Base, default is 10
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   111
   * @return string
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   112
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   113
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   114
  function str($int, $base = 10)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   115
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   116
    return ( is_string($int) ) ? $int : bi_to_str($int, $base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   117
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   118
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   119
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   120
   * Generates a random integer
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   121
   * @param int Length (bits)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   122
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   123
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   124
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   125
  function random($len)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   126
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   127
    return bi_rand($len);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   128
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   129
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   130
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   131
   * Converts a number between bases.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   132
   * @param resource BigInt to convert
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   133
   * @param int Base to convert from
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   134
   * @param int Base to convert to
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   135
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   136
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   137
  function basecon($int, $from, $to)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   138
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   139
    return bi_base_convert($this->str($int, $from), $from, $to);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   140
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   141
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   142
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   143
   * Powmod operation (calculates (a ^ b) mod m)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   144
   * @param resource a
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   145
   * @param resource b
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   146
   * @param resource m
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   147
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   148
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   149
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   150
  function powmod($a, $b, $m)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   151
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   152
    $a = $this->init($a);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   153
    $b = $this->init($b);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   154
    $m = $this->init($m);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   155
    return bi_powmod($a, $b, $m);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   156
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   157
}
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   158
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   159
/**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   160
 * EnanoMath BCMath backend
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   161
 */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   162
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   163
class EnanoMath_BCMath
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   164
{
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   165
  var $api = 'BCMath';
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   166
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   167
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   168
   * Initializes a number to a BCMath integer.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   169
   * @param string String representation of the number
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   170
   * @param int Base the number is in, defaults to 10
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   171
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   172
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   173
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   174
  function init($int, $base = 10)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   175
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   176
    return $this->basecon($int, $base, 10);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   177
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   178
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   179
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   180
   * Converts a number from a BCMath integer to a string
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   181
   * @param resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   182
   * @param int Base, default is 10
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   183
   * @return string
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   184
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   185
   
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   186
  function str($res)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   187
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   188
    return ( is_string($res) ) ? $res : strval($this->basecon($res, 10, $base));
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   189
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   190
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   191
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   192
   * Generates a random integer
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   193
   * @param int Length in bits
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   194
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   195
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   196
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   197
  function random($len)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   198
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   199
    $len = 4 * $len;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   200
    $chars = '0123456789abcdef';
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   201
    $ret = '';
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   202
    for ( $i = 0; $i < $len; $i++ )
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   203
    {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   204
      $chid = mt_rand ( 0, strlen($chars) - 1 );
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   205
      $chr = $chars{$chid};
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   206
      $ret .= $chr;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   207
    }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   208
    return $this->basecon($this->init($ret), 16, 10);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   209
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   210
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   211
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   212
   * Converts a number between bases.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   213
   * @param resource BigInt to convert
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   214
   * @param int Base to convert from
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   215
   * @param int Base to convert to
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   216
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   217
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   218
  function basecon($int, $from, $to)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   219
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   220
    if ( $from != 10 )
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   221
      $int = $this->_bcmath_base2dec($int, $from);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   222
    if ( $to != 10 )
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   223
      $int = $this->_bcmath_dec2base($int, $to);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   224
    return $int;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   225
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   226
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   227
  /**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   228
   * Powmod operation (calculates (a ^ b) mod m)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   229
   * @param resource a
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   230
   * @param resource b
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   231
   * @param resource m
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   232
   * @return resource
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   233
   */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   234
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   235
  function powmod($a, $b, $m)
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   236
  {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   237
    $a = $this->init($a);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   238
    $b = $this->init($b);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   239
    $m = $this->init($m);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   240
    return ( function_exists('bcpowmod') ) ? bcpowmod($a, $b, $m) : bcmod( bcpow($a, $b), $m );
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   241
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   242
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   243
  // from us.php.net/bc:
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   244
  // convert a decimal value to any other base value
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   245
  function _bcmath_dec2base($dec,$base,$digits=FALSE) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   246
      if($base<2 or $base>256) die("Invalid Base: ".$base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   247
      bcscale(0);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   248
      $value="";
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   249
      if(!$digits) $digits=$this->_bcmath_digits($base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   250
      while($dec>$base-1) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   251
          $rest=bcmod($dec,$base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   252
          $dec=bcdiv($dec,$base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   253
          $value=$digits[$rest].$value;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   254
      }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   255
      $value=$digits[intval($dec)].$value;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   256
      return (string) $value;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   257
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   258
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   259
  // convert another base value to its decimal value
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   260
  function _bcmath_base2dec($value,$base,$digits=FALSE) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   261
      if($base<2 or $base>256) die("Invalid Base: ".$base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   262
      bcscale(0);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   263
      if($base<37) $value=strtolower($value);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   264
      if(!$digits) $digits=$this->_bcmath_digits($base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   265
      $size=strlen($value);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   266
      $dec="0";
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   267
      for($loop=0;$loop<$size;$loop++) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   268
          $element=strpos($digits,$value[$loop]);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   269
          $power=bcpow($base,$size-$loop-1);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   270
          $dec=bcadd($dec,bcmul($element,$power));
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   271
      }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   272
      return (string) $dec;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   273
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   274
  
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   275
  function _bcmath_digits($base) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   276
      if($base>64) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   277
          $digits="";
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   278
          for($loop=0;$loop<256;$loop++) {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   279
              $digits.=chr($loop);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   280
          }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   281
      } else {
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   282
          $digits ="0123456789abcdefghijklmnopqrstuvwxyz";
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   283
          $digits.="ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   284
      }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   285
      $digits=substr($digits,0,$base);
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   286
      return (string) $digits;
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   287
  }
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   288
}
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   289
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   290
/**
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   291
 * Creates a new math object based on what libraries are available.
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   292
 * @return object
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   293
 */
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   294
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   295
function enanomath_create()
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   296
{
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   297
  if ( function_exists('gmp_init') )
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   298
    return new EnanoMath_GMP();
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   299
  else if ( function_exists('bi_from_str') )
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   300
    return new EnanoMath_BigInt();
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   301
  else if ( function_exists('bcadd') )
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   302
    return new EnanoMath_BCMath();
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   303
  else
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   304
    throw new Exception('dh_err_not_supported');
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   305
}
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   306
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   307
?>