3 * A class for arbitrary precision math functions implemented in PHP
7 * @category PHPCryptLib
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
15 namespace CryptLib\Core\BigMath
;
17 use CryptLib\Core\BaseConverter
;
20 * A class for arbitrary precision math functions implemented in PHP
22 * @category PHPCryptLib
26 class PHPMath
extends \CryptLib\Core\BigMath
{
29 * Add two numbers together
31 * @param string $left The left argument
32 * @param string $right The right argument
34 * @return A base-10 string of the sum of the two arguments
36 public function add($left, $right) {
39 } elseif (empty($right)) {
43 if ($left[0] == '-' && $right[0] == '-') {
45 $left = substr($left, 1);
46 $right = substr($right, 1);
47 } elseif ($left[0] == '-') {
48 return $this->subtract($right, substr($left, 1));
49 } elseif ($right[0] == '-') {
50 return $this->subtract($left, substr($right, 1));
52 $left = $this->normalize($left);
53 $right = $this->normalize($right);
54 $result = BaseConverter
::convertFromBinary(
55 $this->addBinary($left, $right),
58 return $negative . $result;
62 * Subtract two numbers
64 * @param string $left The left argument
65 * @param string $right The right argument
67 * @return A base-10 string of the difference of the two arguments
69 public function subtract($left, $right) {
72 } elseif (empty($right)) {
74 } elseif ($right[0] == '-') {
75 return $this->add($left, substr($right, 1));
76 } elseif ($left[0] == '-') {
77 return '-' . $this->add(ltrim($left, '-'), $right);
79 $left = $this->normalize($left);
80 $right = $this->normalize($right);
81 $results = $this->subtractBinary($left, $right);
82 $result = BaseConverter
::convertFromBinary($results[1], '0123456789');
83 return $results[0] . $result;
87 * Add two binary strings together
89 * @param string $left The left argument
90 * @param string $right The right argument
92 * @return string The binary result
94 protected function addBinary($left, $right) {
95 $len = max(strlen($left), strlen($right));
96 $left = str_pad($left, $len, chr(0), STR_PAD_LEFT
);
97 $right = str_pad($right, $len, chr(0), STR_PAD_LEFT
);
100 for ($i = 0; $i < $len; $i++
) {
101 $sum = ord($left[$len - $i - 1])
102 +
ord($right[$len - $i - 1])
104 $result .= chr($sum %
256);
108 $result .= chr($carry %
256);
111 return strrev($result);
115 * Subtract two binary strings using 256's compliment
117 * @param string $left The left argument
118 * @param string $right The right argument
120 * @return string The binary result
122 protected function subtractBinary($left, $right) {
123 $len = max(strlen($left), strlen($right));
124 $left = str_pad($left, $len, chr(0), STR_PAD_LEFT
);
125 $right = str_pad($right, $len, chr(0), STR_PAD_LEFT
);
126 $right = $this->compliment($right);
127 $result = $this->addBinary($left, $right);
128 if (strlen($result) > $len) {
130 $carry = substr($result, 0, -1 * $len);
131 $result = substr($result, strlen($carry));
134 $this->addBinary($result, $carry)
137 return array('-', $this->compliment($result));
141 * Take the 256 base compliment
143 * @param string $string The binary string to compliment
145 * @return string The complimented string
147 protected function compliment($string) {
149 $len = strlen($string);
150 for ($i = 0; $i < $len; $i++
) {
151 $result .= chr(255 - ord($string[$i]));
157 * Transform a string number into a binary string using base autodetection
159 * @param string $string The string to transform
161 * @return string The binary transformed number
163 protected function normalize($string) {
164 return BaseConverter
::convertToBinary(