includes/math.php
author Dan
Fri, 29 May 2009 14:03:05 -0400
changeset 1010 12e76b1acb4e
parent 801 eb8b23f11744
child 1081 745200a9cc2a
permissions -rw-r--r--
Clarified some licensing issues from the Fedora project's preliminary review.
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
801
eb8b23f11744 Two big commits in one day I know, but redid password storage to use HMAC-SHA1. Consolidated much AES processing to three core methods in session that should handle everything automagically. Installation works; upgrades should. Rebranded as 1.1.6.
Dan
parents: 768
diff changeset
     5
 * Version 1.1.6 (Caoineag beta 1)
536
218a627eb53e Rebrand as 1.1.4 (Caoineag alpha 4)
Dan
parents: 507
diff changeset
     6
 * Copyright (C) 2006-2008 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
     7
 * 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
     8
 *
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
 * 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
    10
 * 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
    11
 *
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
 * 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
    13
 * 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
    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
/**
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
 * 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
    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
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
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
    21
{
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
  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
    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
  /**
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
   * 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
    26
   * @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
    27
   * @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
    28
   * @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
    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
  
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
  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
    32
  {
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
    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
    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
  /**
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
   * 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
    38
   * @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
    39
   * @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
    40
   * @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
    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
  
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
  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
    44
  {
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
    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
    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
  /**
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
   * 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
    50
   * @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
    51
   * @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
    52
   * @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
    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
   
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
  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
    56
  {
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
    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
    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
  /**
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
   * 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
    62
   * @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
    63
   * @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
    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
  
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
  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
    67
  {
768
111fd7a4415c Fixed: private keys were way too long in EnanoMath
Dan
parents: 685
diff changeset
    68
    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
    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
  /**
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
   * 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
    73
   * @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
    74
   * @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
    75
   * @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
    76
   * @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
    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
  
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
  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
    80
  {
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
    $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
    82
    $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
    83
    $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
    84
    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
    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
/**
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
 * 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
    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
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
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
    93
{
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
  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
    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
  /**
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
   * 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
    98
   * @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
    99
   * @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
   100
   * @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
   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
  
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
  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
   104
  {
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
    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
   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
  /**
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
   * 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
   110
   * @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
   111
   * @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
   112
   * @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
   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
  
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
  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
   116
  {
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
    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
   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
  /**
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
   * 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
   122
   * @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
   123
   * @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
   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
  
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
  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
   127
  {
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
    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
   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
  /**
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
   * 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
   133
   * @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
   134
   * @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
   135
   * @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
   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
  
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
  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
   139
  {
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
    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
   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
  /**
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
   * 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
   145
   * @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
   146
   * @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
   147
   * @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
   148
   * @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
   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
  
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
  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
   152
  {
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
    $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
   154
    $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
   155
    $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
   156
    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
   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
/**
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
 * 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
   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
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
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
   165
{
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
  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
   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
  /**
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
   * 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
   170
   * @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
   171
   * @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
   172
   * @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
   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
  
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
  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
   176
  {
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
    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
   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
  /**
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
   * 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
   182
   * @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
   183
   * @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
   184
   * @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
   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
   
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
  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
   188
  {
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
    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
   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
  /**
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
   * 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
   194
   * @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
   195
   * @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
   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
  
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
  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
   199
  {
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
    $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
   201
    $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
   202
    $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
   203
    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
   204
    {
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
      $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
   206
      $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
   207
      $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
   208
    }
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
    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
   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
  /**
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
   * 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
   214
   * @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
   215
   * @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
   216
   * @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
   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
  
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
  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
   220
  {
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
    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
   222
      $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
   223
    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
   224
      $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
   225
    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
   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
  /**
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
   * 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
   230
   * @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
   231
   * @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
   232
   * @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
   233
   * @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
   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
  
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
  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
   237
  {
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
    $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
   239
    $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
   240
    $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
   241
    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
   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
  
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
  // 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
   245
  // 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
   246
  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
   247
      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
   248
      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
   249
      $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
   250
      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
   251
      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
   252
          $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
   253
          $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
   254
          $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
   255
      }
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
      $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
   257
      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
   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
  
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
  // 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
   261
  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
   262
      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
   263
      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
   264
      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
   265
      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
   266
      $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
   267
      $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
   268
      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
   269
          $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
   270
          $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
   271
          $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
   272
      }
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
      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
   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
  
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
  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
   277
      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
   278
          $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
   279
          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
   280
              $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
   281
          }
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
      } 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
   283
          $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
   284
          $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
   285
      }
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
      $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
   287
      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
   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
/**
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
 * 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
   293
 * @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
   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
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
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
   297
{
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
  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
   299
    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
   300
  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
   301
    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
   302
  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
   303
    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
   304
  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
   305
    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
   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
e4bbd6fb8df3 Modifed EnanoMath layer by segregating it into its own file; got support for big_int PHP extension backend working
Dan
parents:
diff changeset
   308
?>