├── .gitignore ├── README.md ├── index.php ├── LICENSE ├── v1 └── MusicAPI.php └── v2 ├── MusicAPI.php └── BigInteger.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Folder view configuration files 2 | .idea 3 | .DS_Store 4 | Desktop.ini 5 | 6 | # Thumbnail cache files 7 | ._* 8 | Thumbs.db 9 | 10 | # Files that might appear on external disks 11 | .Spotlight-V100 12 | .Trashes 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NeteaseCloudMusicApi 2 | 3 | 搜集到以下可用API: 4 | 5 | * 搜索API 6 | * 歌曲详情API 7 | * 歌词API 8 | 9 | **更新:(增加网易云新版api)** 10 | 11 | * 获取歌曲API 12 | * 歌单API 13 | * 歌曲MVAPI 14 | 15 | 新版API参考 16 | 17 | * METO [NeteaseCloudMusicApi](https://github.com/metowolf/NeteaseCloudMusicApi/blob/master/v2/MusicAPI.php) 18 | 19 | API返回的是json格式,前端可用js输出。具体使用请看index.php; 20 | 21 | ## License 22 | 23 | [MIT License](https://github.com/axhello/NeteaseCloudMusicApi/blob/master/LICENSE) 24 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | search($keyword, 10); 15 | print_r($result); 16 | 17 | //返回歌曲详情 18 | // $detail = $api->detail($song_id); 19 | // print_r($detail); 20 | 21 | //返回歌曲链接 22 | // $mp3url = $api->mp3url($song_id); 23 | // print_r($mp3url); 24 | 25 | //歌曲专辑(不可用) 26 | // $albums = $api->albums($album_id, 30); 27 | // print_r($albums); 28 | 29 | //歌曲歌词 30 | // $lyric = $api->lyric($song_id); 31 | // print_r($lyric); 32 | 33 | //歌曲MV (如果有) 34 | // $mv = $api->mv($mv_id); 35 | // print_r($mv); 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | License 2 | 3 | NeteaseCloudMusicApi is under the MIT license. 4 | 5 | Copyright (c) 2016 Axhello 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 8 | 9 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /v1/MusicAPI.php: -------------------------------------------------------------------------------- 1 | $s, 'limit' => $limit, 'type' => $type, 'offset' => $offset]; 25 | return $this->restAPI($url, $params); 26 | } 27 | 28 | /** 29 | * 歌曲详情API,于搜索API不同,此API带有MP3链接 30 | * @param $song_id 歌曲id 31 | * @return JSON 32 | */ 33 | public function detail($song_id) 34 | { 35 | $url = 'http://music.163.com/api/song/detail'; 36 | $params = ['id' => $song_id, 'ids' => '[' . $song_id . ']']; 37 | return $this->restAPI($url, $params); 38 | } 39 | 40 | /** 41 | * 歌词API,根据JSON判断是否有歌词,nolyric表示无歌词,uncollected表示暂时无人提交歌词 42 | * @param $song_id 歌曲id 43 | * @return JSON 44 | */ 45 | public function lyric($song_id) 46 | { 47 | $url = 'http://music.163.com/api/song/lyric'; 48 | $params = ['os' => 'pc', 'id' => $song_id, 'lv' => -1, 'kv' => -1, 'tv' => -1]; 49 | return $this->restAPI($url, $params); 50 | } 51 | 52 | /** 53 | * 歌单详情 API 54 | * @param $playlist_id 歌单id,排行榜也归类为歌单 55 | * @return JSON 56 | */ 57 | public function playlist($playlist_id) 58 | { 59 | $url = 'http://music.163.com/api/playlist/detail'; 60 | $params = ['id' => $playlist_id, 'updateTime' => '-1']; 61 | return $this->restAPI($url, $params); 62 | } 63 | 64 | /** 65 | * MV_API 66 | * @param $mv_id MV_id 67 | * @return JSON 68 | */ 69 | public function mv($mv_id) 70 | { 71 | $url = 'http://music.163.com/api/mv/detail'; 72 | $params = ['id' => $mv_id, 'type' => 'mp4']; 73 | return $this->restAPI($url, $params); 74 | } 75 | 76 | /** 77 | * @param $url CURL API地址 78 | * @param $data 79 | * @return json 80 | */ 81 | protected function restAPI($url, $params) 82 | { 83 | $curl = curl_init(); 84 | curl_setopt($curl, CURLOPT_URL, $url); 85 | curl_setopt($curl, CURLOPT_POST, true); 86 | curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); 87 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 88 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); 89 | curl_setopt($curl, CURLOPT_ENCODING, 'application/json'); 90 | curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); 91 | $result = curl_exec($curl); 92 | curl_close($curl); 93 | return $result; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /v2/MusicAPI.php: -------------------------------------------------------------------------------- 1 | secretKey = $this->createSecretKey(16); 24 | } 25 | 26 | protected function createSecretKey($length) 27 | { 28 | $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 29 | $r = ''; 30 | for ($i = 0; $i < $length; $i++) { 31 | $r .= $str[rand(0, strlen($str) - 1)]; 32 | } 33 | return $r; 34 | } 35 | 36 | protected function prepare($data) 37 | { 38 | $data['params'] = $this->aesEncrypt($data['params'], self::NONCE); 39 | $data['params'] = $this->aesEncrypt($data['params'], $this->secretKey); 40 | $data['encSecKey'] = $this->rsaEncrypt($this->secretKey); 41 | return $data; 42 | } 43 | 44 | protected function aesEncrypt($secretData, $secret) 45 | { 46 | return openssl_encrypt($secretData, 'aes-128-cbc', $secret, false, '0102030405060708'); 47 | } 48 | 49 | /** 50 | * @param $text 51 | * @return string 52 | */ 53 | protected function rsaEncrypt($text) 54 | { 55 | $rtext = strrev(utf8_encode($text)); 56 | $keytext = $this->bchexdec($this->strToHex($rtext)); 57 | $biText = new Math_BigInteger($keytext); 58 | $biKey = new Math_BigInteger($this->bchexdec(self::PUBKEY)); 59 | $biMod = new Math_BigInteger($this->bchexdec(self::MODULUS)); 60 | $key = $biText->modPow($biKey, $biMod)->toHex(); 61 | return str_pad($key, 256, '0', STR_PAD_LEFT); 62 | } 63 | 64 | protected function bchexdec($hex) 65 | { 66 | $dec = 0; 67 | $len = strlen($hex); 68 | for ($i = 0; $i < $len; $i++) { 69 | $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i])), bcpow('16', strval($len - $i - 1)))); 70 | } 71 | return $dec; 72 | } 73 | 74 | protected function strToHex($str) 75 | { 76 | $hex = ''; 77 | for ($i = 0; $i < strlen($str); $i++) { 78 | $hex .= dechex(ord($str[$i])); 79 | } 80 | return $hex; 81 | } 82 | 83 | protected function curl($url, $data = null) 84 | { 85 | $curl = curl_init(); 86 | curl_setopt($curl, CURLOPT_URL, $url); 87 | if ($data) { 88 | if (is_array($data)) $data = http_build_query($data); 89 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 90 | curl_setopt($curl, CURLOPT_POST, 1); 91 | } 92 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 93 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); 94 | curl_setopt($curl, CURLOPT_REFERER, 'http://music.163.com/'); 95 | curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); 96 | curl_setopt($curl, CURLOPT_ENCODING, 'application/json'); 97 | $result = curl_exec($curl); 98 | curl_close($curl); 99 | return $result; 100 | } 101 | 102 | /** 103 | * 搜索API 104 | * @param $s 要搜索的内容 105 | * @param $limit 要返回的条数 106 | * @param $offset 设置偏移量 用于分页 107 | * @param $type 类型 [1 单曲] [10 专辑] [100 歌手] [1000 歌单] [1002 用户] 108 | * @return JSON 109 | */ 110 | public function search($s = null, $limit = 30, $offset = 0, $type = 1) 111 | { 112 | $url = 'http://music.163.com/weapi/cloudsearch/get/web?csrf_token='; 113 | $data = ['params' => '{ 114 | "s":"' . $s . '", 115 | "type":"' . $type . '", 116 | "limit":"' . $limit . '", 117 | "total":"true", 118 | "offset":"' . $offset . '", 119 | "csrf_token": "" 120 | }']; 121 | return $this->curl($url, $this->prepare($data)); 122 | } 123 | 124 | /** 125 | * 歌曲详情API,不带MP3链接 126 | * @param $song_id 歌曲id 127 | * @return JSON 128 | */ 129 | public function detail($song_id) 130 | { 131 | $url = 'http://music.163.com/weapi/v1/song/detail'; 132 | if (is_array($song_id)) $s = '["' . implode('","', $song_id) . '"]'; else $s = '["' . $song_id . '"]'; 133 | $data = ['params' => '{ 134 | "ids":' . $s . ', 135 | "csrf_token":"" 136 | }']; 137 | return $this->curl($url, $this->prepare($data)); 138 | } 139 | 140 | /** 141 | * 新版API歌曲链接不包含在歌曲详情API里,通过此API获取 142 | * @param $song_id 143 | * @param int $br 144 | * @return JSON 145 | */ 146 | public function mp3url($song_id, $br = 320000) 147 | { 148 | $url = 'http://music.163.com/weapi/song/enhance/player/url?csrf_token='; 149 | if (is_array($song_id)) $s = '["' . implode('","', $song_id) . '"]'; else $s = '["' . $song_id . '"]'; 150 | $data = ['params' => '{ 151 | "ids":' . $s . ', 152 | "br":"' . $br . '", 153 | "csrf_token":"" 154 | }']; 155 | return $this->curl($url, $this->prepare($data)); 156 | } 157 | 158 | /** 159 | * 歌词API 增加了几个字段 160 | * @param $song_id 161 | * @return JSON 162 | */ 163 | public function lyric($song_id) 164 | { 165 | $url = 'http://music.163.com/weapi/song/lyric?csrf_token='; 166 | $data = ['params' => '{ 167 | "id":"' . $song_id . '", 168 | "os":"pc", 169 | "lv":"-1", 170 | "kv":"-1", 171 | "tv":"-1", 172 | "csrf_token":"" 173 | }']; 174 | return $this->curl($url, $this->prepare($data)); 175 | } 176 | 177 | /** 178 | * 歌单API 179 | * @param $playlist_id 180 | * @return JSON 181 | */ 182 | public function playlist($playlist_id) 183 | { 184 | $url = 'http://music.163.com/weapi/v3/playlist/detail?csrf_token='; 185 | $data = ['params' => '{ 186 | "id":"' . $playlist_id . '", 187 | "n":"1000", 188 | "csrf_token":"" 189 | }']; 190 | return $this->curl($url, $this->prepare($data)); 191 | } 192 | 193 | /** 194 | * 根据MVid(如果有)获取MV链接 195 | * @param $mv_id 196 | * @return JSON 197 | */ 198 | public function mv($mv_id) 199 | { 200 | $url = 'http://music.163.com/weapi/mv/detail/'; 201 | $data = ['params' => '{ 202 | "id":"' . $mv_id . '", 203 | "csrf_token":"" 204 | }']; 205 | return $this->curl($url, $this->prepare($data)); 206 | } 207 | 208 | } -------------------------------------------------------------------------------- /v2/BigInteger.php: -------------------------------------------------------------------------------- 1 | > and << cannot be used, nor can the modulo operator %, 19 | * which only supports integers. Although this fact will slow this library down, the fact that such a high 20 | * base is being used should more than compensate. 21 | * 22 | * When PHP version 6 is officially released, we'll be able to use 64-bit integers. This should, once again, 23 | * allow bitwise operators, and will increase the maximum possible base to 2**31 (or 2**62 for addition / 24 | * subtraction). 25 | * 26 | * Numbers are stored in {@link http://en.wikipedia.org/wiki/Endianness little endian} format. ie. 27 | * (new Math_BigInteger(pow(2, 26)))->value = array(0, 1) 28 | * 29 | * Useful resources are as follows: 30 | * 31 | * - {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf Handbook of Applied Cryptography (HAC)} 32 | * - {@link http://math.libtomcrypt.com/files/tommath.pdf Multi-Precision Math (MPM)} 33 | * - Java's BigInteger classes. See /j2se/src/share/classes/java/math in jdk-1_5_0-src-jrl.zip 34 | * 35 | * Here's an example of how to use this library: 36 | * 37 | * add($b); 44 | * 45 | * echo $c->toString(); // outputs 5 46 | * ?> 47 | * 48 | * 49 | * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy 50 | * of this software and associated documentation files (the "Software"), to deal 51 | * in the Software without restriction, including without limitation the rights 52 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 53 | * copies of the Software, and to permit persons to whom the Software is 54 | * furnished to do so, subject to the following conditions: 55 | * 56 | * The above copyright notice and this permission notice shall be included in 57 | * all copies or substantial portions of the Software. 58 | * 59 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 60 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 61 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 62 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 63 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 64 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 65 | * THE SOFTWARE. 66 | * 67 | * @category Math 68 | * @package Math_BigInteger 69 | * @author Jim Wigginton 70 | * @copyright MMVI Jim Wigginton 71 | * @license http://www.opensource.org/licenses/mit-license.html MIT License 72 | * @link http://pear.php.net/package/Math_BigInteger 73 | */ 74 | 75 | /**#@+ 76 | * Reduction constants 77 | * 78 | * @access private 79 | * @see Math_BigInteger::_reduce() 80 | */ 81 | /** 82 | * @see Math_BigInteger::_montgomery() 83 | * @see Math_BigInteger::_prepMontgomery() 84 | */ 85 | define('MATH_BIGINTEGER_MONTGOMERY', 0); 86 | /** 87 | * @see Math_BigInteger::_barrett() 88 | */ 89 | define('MATH_BIGINTEGER_BARRETT', 1); 90 | /** 91 | * @see Math_BigInteger::_mod2() 92 | */ 93 | define('MATH_BIGINTEGER_POWEROF2', 2); 94 | /** 95 | * @see Math_BigInteger::_remainder() 96 | */ 97 | define('MATH_BIGINTEGER_CLASSIC', 3); 98 | /** 99 | * @see Math_BigInteger::__clone() 100 | */ 101 | define('MATH_BIGINTEGER_NONE', 4); 102 | /**#@-*/ 103 | 104 | /**#@+ 105 | * Array constants 106 | * 107 | * Rather than create a thousands and thousands of new Math_BigInteger objects in repeated function calls to add() and 108 | * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them. 109 | * 110 | * @access private 111 | */ 112 | /** 113 | * $result[MATH_BIGINTEGER_VALUE] contains the value. 114 | */ 115 | define('MATH_BIGINTEGER_VALUE', 0); 116 | /** 117 | * $result[MATH_BIGINTEGER_SIGN] contains the sign. 118 | */ 119 | define('MATH_BIGINTEGER_SIGN', 1); 120 | /**#@-*/ 121 | 122 | /**#@+ 123 | * @access private 124 | * @see Math_BigInteger::_montgomery() 125 | * @see Math_BigInteger::_barrett() 126 | */ 127 | /** 128 | * Cache constants 129 | * 130 | * $cache[MATH_BIGINTEGER_VARIABLE] tells us whether or not the cached data is still valid. 131 | */ 132 | define('MATH_BIGINTEGER_VARIABLE', 0); 133 | /** 134 | * $cache[MATH_BIGINTEGER_DATA] contains the cached data. 135 | */ 136 | define('MATH_BIGINTEGER_DATA', 1); 137 | /**#@-*/ 138 | 139 | /**#@+ 140 | * Mode constants. 141 | * 142 | * @access private 143 | * @see Math_BigInteger::Math_BigInteger() 144 | */ 145 | /** 146 | * To use the pure-PHP implementation 147 | */ 148 | define('MATH_BIGINTEGER_MODE_INTERNAL', 1); 149 | /** 150 | * To use the BCMath library 151 | * 152 | * (if enabled; otherwise, the internal implementation will be used) 153 | */ 154 | define('MATH_BIGINTEGER_MODE_BCMATH', 2); 155 | /** 156 | * To use the GMP library 157 | * 158 | * (if present; otherwise, either the BCMath or the internal implementation will be used) 159 | */ 160 | define('MATH_BIGINTEGER_MODE_GMP', 3); 161 | /**#@-*/ 162 | 163 | /** 164 | * Karatsuba Cutoff 165 | * 166 | * At what point do we switch between Karatsuba multiplication and schoolbook long multiplication? 167 | * 168 | * @access private 169 | */ 170 | define('MATH_BIGINTEGER_KARATSUBA_CUTOFF', 25); 171 | 172 | /** 173 | * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 174 | * numbers. 175 | * 176 | * @package Math_BigInteger 177 | * @author Jim Wigginton 178 | * @version 1.0.0RC4 179 | * @access public 180 | */ 181 | class Math_BigInteger 182 | { 183 | /** 184 | * Holds the BigInteger's value. 185 | * 186 | * @var Array 187 | * @access private 188 | */ 189 | var $value; 190 | 191 | /** 192 | * Holds the BigInteger's magnitude. 193 | * 194 | * @var Boolean 195 | * @access private 196 | */ 197 | var $is_negative = false; 198 | 199 | /** 200 | * Random number generator function 201 | * 202 | * @see setRandomGenerator() 203 | * @access private 204 | */ 205 | var $generator = 'mt_rand'; 206 | 207 | /** 208 | * Precision 209 | * 210 | * @see setPrecision() 211 | * @access private 212 | */ 213 | var $precision = -1; 214 | 215 | /** 216 | * Precision Bitmask 217 | * 218 | * @see setPrecision() 219 | * @access private 220 | */ 221 | var $bitmask = false; 222 | 223 | /** 224 | * Mode independent value used for serialization. 225 | * 226 | * If the bcmath or gmp extensions are installed $this->value will be a non-serializable resource, hence the need for 227 | * a variable that'll be serializable regardless of whether or not extensions are being used. Unlike $this->value, 228 | * however, $this->hex is only calculated when $this->__sleep() is called. 229 | * 230 | * @see __sleep() 231 | * @see __wakeup() 232 | * @var String 233 | * @access private 234 | */ 235 | var $hex; 236 | 237 | /** 238 | * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers. 239 | * 240 | * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using 241 | * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. 242 | * 243 | * Here's an example: 244 | * 245 | * <?php 246 | * include('Math/BigInteger.php'); 247 | * 248 | * $a = new Math_BigInteger('0x32', 16); // 50 in base-16 249 | * 250 | * echo $a->toString(); // outputs 50 251 | * ?> 252 | * 253 | * 254 | * @param optional $x base-10 number or base-$base number if $base set. 255 | * @param optional integer $base 256 | * @return Math_BigInteger 257 | * @access public 258 | */ 259 | function __construct($x = 0, $base = 10) 260 | { 261 | if ( !defined('MATH_BIGINTEGER_MODE') ) { 262 | switch (true) { 263 | case extension_loaded('gmp'): 264 | define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); 265 | break; 266 | case extension_loaded('bcmath'): 267 | define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); 268 | break; 269 | default: 270 | define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL); 271 | } 272 | } 273 | 274 | if (function_exists('openssl_public_encrypt') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { 275 | // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work 276 | ob_start(); 277 | phpinfo(); 278 | $content = ob_get_contents(); 279 | ob_end_clean(); 280 | 281 | preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches); 282 | 283 | $versions = array(); 284 | if (!empty($matches[1])) { 285 | for ($i = 0; $i < count($matches[1]); $i++) { 286 | $versions[$matches[1][$i]] = trim(str_replace('=>', '', strip_tags($matches[2][$i]))); 287 | } 288 | } 289 | 290 | // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+ 291 | switch (true) { 292 | case !isset($versions['Header']): 293 | case !isset($versions['Library']): 294 | case $versions['Header'] == $versions['Library']: 295 | define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); 296 | break; 297 | default: 298 | define('MATH_BIGINTEGER_OPENSSL_DISABLE', true); 299 | } 300 | } 301 | 302 | if (!defined('PHP_INT_SIZE')) { 303 | define('PHP_INT_SIZE', 4); 304 | } 305 | 306 | if (!defined('MATH_BIGINTEGER_BASE') && MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_INTERNAL) { 307 | switch (PHP_INT_SIZE) { 308 | case 8: // use 64-bit integers if int size is 8 bytes 309 | define('MATH_BIGINTEGER_BASE', 31); 310 | define('MATH_BIGINTEGER_BASE_FULL', 0x80000000); 311 | define('MATH_BIGINTEGER_MAX_DIGIT', 0x7FFFFFFF); 312 | define('MATH_BIGINTEGER_MSB', 0x40000000); 313 | // 10**9 is the closest we can get to 2**31 without passing it 314 | define('MATH_BIGINTEGER_MAX10', 1000000000); 315 | define('MATH_BIGINTEGER_MAX10_LEN', 9); 316 | // the largest digit that may be used in addition / subtraction 317 | define('MATH_BIGINTEGER_MAX_DIGIT2', pow(2, 62)); 318 | break; 319 | //case 4: // use 64-bit floats if int size is 4 bytes 320 | default: 321 | define('MATH_BIGINTEGER_BASE', 26); 322 | define('MATH_BIGINTEGER_BASE_FULL', 0x4000000); 323 | define('MATH_BIGINTEGER_MAX_DIGIT', 0x3FFFFFF); 324 | define('MATH_BIGINTEGER_MSB', 0x2000000); 325 | // 10**7 is the closest to 2**26 without passing it 326 | define('MATH_BIGINTEGER_MAX10', 10000000); 327 | define('MATH_BIGINTEGER_MAX10_LEN', 7); 328 | // the largest digit that may be used in addition / subtraction 329 | // we do pow(2, 52) instead of using 4503599627370496 directly because some 330 | // PHP installations will truncate 4503599627370496. 331 | define('MATH_BIGINTEGER_MAX_DIGIT2', pow(2, 52)); 332 | } 333 | } 334 | 335 | switch ( MATH_BIGINTEGER_MODE ) { 336 | case MATH_BIGINTEGER_MODE_GMP: 337 | if (is_resource($x) && get_resource_type($x) == 'GMP integer') { 338 | $this->value = $x; 339 | return; 340 | } 341 | $this->value = gmp_init(0); 342 | break; 343 | case MATH_BIGINTEGER_MODE_BCMATH: 344 | $this->value = '0'; 345 | break; 346 | default: 347 | $this->value = array(); 348 | } 349 | 350 | // '0' counts as empty() but when the base is 256 '0' is equal to ord('0') or 48 351 | // '0' is the only value like this per http://php.net/empty 352 | if (empty($x) && (abs($base) != 256 || $x !== '0')) { 353 | return; 354 | } 355 | 356 | switch ($base) { 357 | case -256: 358 | if (ord($x[0]) & 0x80) { 359 | $x = ~$x; 360 | $this->is_negative = true; 361 | } 362 | case 256: 363 | switch ( MATH_BIGINTEGER_MODE ) { 364 | case MATH_BIGINTEGER_MODE_GMP: 365 | $sign = $this->is_negative ? '-' : ''; 366 | $this->value = gmp_init($sign . '0x' . bin2hex($x)); 367 | break; 368 | case MATH_BIGINTEGER_MODE_BCMATH: 369 | // round $len to the nearest 4 (thanks, DavidMJ!) 370 | $len = (strlen($x) + 3) & 0xFFFFFFFC; 371 | 372 | $x = str_pad($x, $len, chr(0), STR_PAD_LEFT); 373 | 374 | for ($i = 0; $i < $len; $i+= 4) { 375 | $this->value = bcmul($this->value, '4294967296', 0); // 4294967296 == 2**32 376 | $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + ((ord($x[$i + 1]) << 16) | (ord($x[$i + 2]) << 8) | ord($x[$i + 3])), 0); 377 | } 378 | 379 | if ($this->is_negative) { 380 | $this->value = '-' . $this->value; 381 | } 382 | 383 | break; 384 | // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb) 385 | default: 386 | while (strlen($x)) { 387 | $this->value[] = $this->_bytes2int($this->_base256_rshift($x, MATH_BIGINTEGER_BASE)); 388 | } 389 | } 390 | 391 | if ($this->is_negative) { 392 | if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) { 393 | $this->is_negative = false; 394 | } 395 | $temp = $this->add(new Math_BigInteger('-1')); 396 | $this->value = $temp->value; 397 | } 398 | break; 399 | case 16: 400 | case -16: 401 | if ($base > 0 && $x[0] == '-') { 402 | $this->is_negative = true; 403 | $x = substr($x, 1); 404 | } 405 | 406 | $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x); 407 | 408 | $is_negative = false; 409 | if ($base < 0 && hexdec($x[0]) >= 8) { 410 | $this->is_negative = $is_negative = true; 411 | $x = bin2hex(~pack('H*', $x)); 412 | } 413 | 414 | switch ( MATH_BIGINTEGER_MODE ) { 415 | case MATH_BIGINTEGER_MODE_GMP: 416 | $temp = $this->is_negative ? '-0x' . $x : '0x' . $x; 417 | $this->value = gmp_init($temp); 418 | $this->is_negative = false; 419 | break; 420 | case MATH_BIGINTEGER_MODE_BCMATH: 421 | $x = ( strlen($x) & 1 ) ? '0' . $x : $x; 422 | $temp = new Math_BigInteger(pack('H*', $x), 256); 423 | $this->value = $this->is_negative ? '-' . $temp->value : $temp->value; 424 | $this->is_negative = false; 425 | break; 426 | default: 427 | $x = ( strlen($x) & 1 ) ? '0' . $x : $x; 428 | $temp = new Math_BigInteger(pack('H*', $x), 256); 429 | $this->value = $temp->value; 430 | } 431 | 432 | if ($is_negative) { 433 | $temp = $this->add(new Math_BigInteger('-1')); 434 | $this->value = $temp->value; 435 | } 436 | break; 437 | case 10: 438 | case -10: 439 | // (?value = gmp_init($x); 447 | break; 448 | case MATH_BIGINTEGER_MODE_BCMATH: 449 | // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different 450 | // results then doing it on '-1' does (modInverse does $x[0]) 451 | $this->value = $x === '-' ? '0' : (string) $x; 452 | break; 453 | default: 454 | $temp = new Math_BigInteger(); 455 | 456 | $multiplier = new Math_BigInteger(); 457 | $multiplier->value = array(MATH_BIGINTEGER_MAX10); 458 | 459 | if ($x[0] == '-') { 460 | $this->is_negative = true; 461 | $x = substr($x, 1); 462 | } 463 | 464 | $x = str_pad($x, strlen($x) + ((MATH_BIGINTEGER_MAX10_LEN - 1) * strlen($x)) % MATH_BIGINTEGER_MAX10_LEN, 0, STR_PAD_LEFT); 465 | while (strlen($x)) { 466 | $temp = $temp->multiply($multiplier); 467 | $temp = $temp->add(new Math_BigInteger($this->_int2bytes(substr($x, 0, MATH_BIGINTEGER_MAX10_LEN)), 256)); 468 | $x = substr($x, MATH_BIGINTEGER_MAX10_LEN); 469 | } 470 | 471 | $this->value = $temp->value; 472 | } 473 | break; 474 | case 2: // base-2 support originally implemented by Lluis Pamies - thanks! 475 | case -2: 476 | if ($base > 0 && $x[0] == '-') { 477 | $this->is_negative = true; 478 | $x = substr($x, 1); 479 | } 480 | 481 | $x = preg_replace('#^([01]*).*#', '$1', $x); 482 | $x = str_pad($x, strlen($x) + (3 * strlen($x)) % 4, 0, STR_PAD_LEFT); 483 | 484 | $str = '0x'; 485 | while (strlen($x)) { 486 | $part = substr($x, 0, 4); 487 | $str.= dechex(bindec($part)); 488 | $x = substr($x, 4); 489 | } 490 | 491 | if ($this->is_negative) { 492 | $str = '-' . $str; 493 | } 494 | 495 | $temp = new Math_BigInteger($str, 8 * $base); // ie. either -16 or +16 496 | $this->value = $temp->value; 497 | $this->is_negative = $temp->is_negative; 498 | 499 | break; 500 | default: 501 | // base not supported, so we'll let $this == 0 502 | } 503 | } 504 | 505 | /** 506 | * This function exists to maintain backwards compatibility with older code 507 | * 508 | * @param int $x base-10 number or base-$base number if $base set. 509 | * @param int $base Number base 510 | */ 511 | public function Math_BigInteger($x = 0, $base = 10) 512 | { 513 | self::__construct($x, $base); 514 | } 515 | 516 | /** 517 | * Converts a BigInteger to a byte string (eg. base-256). 518 | * 519 | * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're 520 | * saved as two's compliment. 521 | * 522 | * Here's an example: 523 | * 524 | * toBytes(); // outputs chr(65) 530 | * ?> 531 | * 532 | * 533 | * @param Boolean $twos_compliment 534 | * @return String 535 | * @access public 536 | * @internal Converts a base-2**26 number to base-2**8 537 | */ 538 | function toBytes($twos_compliment = false) 539 | { 540 | if ($twos_compliment) { 541 | $comparison = $this->compare(new Math_BigInteger()); 542 | if ($comparison == 0) { 543 | return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; 544 | } 545 | 546 | $temp = $comparison < 0 ? $this->add(new Math_BigInteger(1)) : $this->copy(); 547 | $bytes = $temp->toBytes(); 548 | 549 | if (empty($bytes)) { // eg. if the number we're trying to convert is -1 550 | $bytes = chr(0); 551 | } 552 | 553 | if (ord($bytes[0]) & 0x80) { 554 | $bytes = chr(0) . $bytes; 555 | } 556 | 557 | return $comparison < 0 ? ~$bytes : $bytes; 558 | } 559 | 560 | switch ( MATH_BIGINTEGER_MODE ) { 561 | case MATH_BIGINTEGER_MODE_GMP: 562 | if (gmp_cmp($this->value, gmp_init(0)) == 0) { 563 | return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; 564 | } 565 | 566 | $temp = gmp_strval(gmp_abs($this->value), 16); 567 | $temp = ( strlen($temp) & 1 ) ? '0' . $temp : $temp; 568 | $temp = pack('H*', $temp); 569 | 570 | return $this->precision > 0 ? 571 | substr(str_pad($temp, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : 572 | ltrim($temp, chr(0)); 573 | case MATH_BIGINTEGER_MODE_BCMATH: 574 | if ($this->value === '0') { 575 | return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; 576 | } 577 | 578 | $value = ''; 579 | $current = $this->value; 580 | 581 | if ($current[0] == '-') { 582 | $current = substr($current, 1); 583 | } 584 | 585 | while (bccomp($current, '0', 0) > 0) { 586 | $temp = bcmod($current, '16777216'); 587 | $value = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $value; 588 | $current = bcdiv($current, '16777216', 0); 589 | } 590 | 591 | return $this->precision > 0 ? 592 | substr(str_pad($value, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : 593 | ltrim($value, chr(0)); 594 | } 595 | 596 | if (!count($this->value)) { 597 | return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; 598 | } 599 | $result = $this->_int2bytes($this->value[count($this->value) - 1]); 600 | 601 | $temp = $this->copy(); 602 | 603 | for ($i = count($temp->value) - 2; $i >= 0; --$i) { 604 | $temp->_base256_lshift($result, MATH_BIGINTEGER_BASE); 605 | $result = $result | str_pad($temp->_int2bytes($temp->value[$i]), strlen($result), chr(0), STR_PAD_LEFT); 606 | } 607 | 608 | return $this->precision > 0 ? 609 | str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) : 610 | $result; 611 | } 612 | 613 | /** 614 | * Converts a BigInteger to a hex string (eg. base-16)). 615 | * 616 | * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're 617 | * saved as two's compliment. 618 | * 619 | * Here's an example: 620 | * 621 | * toHex(); // outputs '41' 627 | * ?> 628 | * 629 | * 630 | * @param Boolean $twos_compliment 631 | * @return String 632 | * @access public 633 | * @internal Converts a base-2**26 number to base-2**8 634 | */ 635 | function toHex($twos_compliment = false) 636 | { 637 | return bin2hex($this->toBytes($twos_compliment)); 638 | } 639 | 640 | /** 641 | * Converts a BigInteger to a bit string (eg. base-2). 642 | * 643 | * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're 644 | * saved as two's compliment. 645 | * 646 | * Here's an example: 647 | * 648 | * toBits(); // outputs '1000001' 654 | * ?> 655 | * 656 | * 657 | * @param Boolean $twos_compliment 658 | * @return String 659 | * @access public 660 | * @internal Converts a base-2**26 number to base-2**2 661 | */ 662 | function toBits($twos_compliment = false) 663 | { 664 | $hex = $this->toHex($twos_compliment); 665 | $bits = ''; 666 | for ($i = strlen($hex) - 8, $start = strlen($hex) & 7; $i >= $start; $i-=8) { 667 | $bits = str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT) . $bits; 668 | } 669 | if ($start) { // hexdec('') == 0 670 | $bits = str_pad(decbin(hexdec(substr($hex, 0, $start))), 8, '0', STR_PAD_LEFT) . $bits; 671 | } 672 | $result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0'); 673 | 674 | if ($twos_compliment && $this->compare(new Math_BigInteger()) > 0 && $this->precision <= 0) { 675 | return '0' . $result; 676 | } 677 | 678 | return $result; 679 | } 680 | 681 | /** 682 | * Converts a BigInteger to a base-10 number. 683 | * 684 | * Here's an example: 685 | * 686 | * toString(); // outputs 50 692 | * ?> 693 | * 694 | * 695 | * @return String 696 | * @access public 697 | * @internal Converts a base-2**26 number to base-10**7 (which is pretty much base-10) 698 | */ 699 | function toString() 700 | { 701 | switch ( MATH_BIGINTEGER_MODE ) { 702 | case MATH_BIGINTEGER_MODE_GMP: 703 | return gmp_strval($this->value); 704 | case MATH_BIGINTEGER_MODE_BCMATH: 705 | if ($this->value === '0') { 706 | return '0'; 707 | } 708 | 709 | return ltrim($this->value, '0'); 710 | } 711 | 712 | if (!count($this->value)) { 713 | return '0'; 714 | } 715 | 716 | $temp = $this->copy(); 717 | $temp->is_negative = false; 718 | 719 | $divisor = new Math_BigInteger(); 720 | $divisor->value = array(MATH_BIGINTEGER_MAX10); 721 | $result = ''; 722 | while (count($temp->value)) { 723 | list($temp, $mod) = $temp->divide($divisor); 724 | $result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', MATH_BIGINTEGER_MAX10_LEN, '0', STR_PAD_LEFT) . $result; 725 | } 726 | $result = ltrim($result, '0'); 727 | if (empty($result)) { 728 | $result = '0'; 729 | } 730 | 731 | if ($this->is_negative) { 732 | $result = '-' . $result; 733 | } 734 | 735 | return $result; 736 | } 737 | 738 | /** 739 | * Copy an object 740 | * 741 | * PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee 742 | * that all objects are passed by value, when appropriate. More information can be found here: 743 | * 744 | * {@link http://php.net/language.oop5.basic#51624} 745 | * 746 | * @access public 747 | * @see __clone() 748 | * @return Math_BigInteger 749 | */ 750 | function copy() 751 | { 752 | $temp = new Math_BigInteger(); 753 | $temp->value = $this->value; 754 | $temp->is_negative = $this->is_negative; 755 | $temp->generator = $this->generator; 756 | $temp->precision = $this->precision; 757 | $temp->bitmask = $this->bitmask; 758 | return $temp; 759 | } 760 | 761 | /** 762 | * __toString() magic method 763 | * 764 | * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call 765 | * toString(). 766 | * 767 | * @access public 768 | * @internal Implemented per a suggestion by Techie-Michael - thanks! 769 | */ 770 | function __toString() 771 | { 772 | return $this->toString(); 773 | } 774 | 775 | /** 776 | * __clone() magic method 777 | * 778 | * Although you can call Math_BigInteger::__toString() directly in PHP5, you cannot call Math_BigInteger::__clone() 779 | * directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5 780 | * only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5, 781 | * call Math_BigInteger::copy(), instead. 782 | * 783 | * @access public 784 | * @see copy() 785 | * @return Math_BigInteger 786 | */ 787 | function __clone() 788 | { 789 | return $this->copy(); 790 | } 791 | 792 | /** 793 | * __sleep() magic method 794 | * 795 | * Will be called, automatically, when serialize() is called on a Math_BigInteger object. 796 | * 797 | * @see __wakeup() 798 | * @access public 799 | */ 800 | function __sleep() 801 | { 802 | $this->hex = $this->toHex(true); 803 | $vars = array('hex'); 804 | if ($this->generator != 'mt_rand') { 805 | $vars[] = 'generator'; 806 | } 807 | if ($this->precision > 0) { 808 | $vars[] = 'precision'; 809 | } 810 | return $vars; 811 | 812 | } 813 | 814 | /** 815 | * __wakeup() magic method 816 | * 817 | * Will be called, automatically, when unserialize() is called on a Math_BigInteger object. 818 | * 819 | * @see __sleep() 820 | * @access public 821 | */ 822 | function __wakeup() 823 | { 824 | $temp = new Math_BigInteger($this->hex, -16); 825 | $this->value = $temp->value; 826 | $this->is_negative = $temp->is_negative; 827 | $this->setRandomGenerator($this->generator); 828 | if ($this->precision > 0) { 829 | // recalculate $this->bitmask 830 | $this->setPrecision($this->precision); 831 | } 832 | } 833 | 834 | /** 835 | * Adds two BigIntegers. 836 | * 837 | * Here's an example: 838 | * 839 | * add($b); 846 | * 847 | * echo $c->toString(); // outputs 30 848 | * ?> 849 | * 850 | * 851 | * @param Math_BigInteger $y 852 | * @return Math_BigInteger 853 | * @access public 854 | * @internal Performs base-2**52 addition 855 | */ 856 | function add($y) 857 | { 858 | switch ( MATH_BIGINTEGER_MODE ) { 859 | case MATH_BIGINTEGER_MODE_GMP: 860 | $temp = new Math_BigInteger(); 861 | $temp->value = gmp_add($this->value, $y->value); 862 | 863 | return $this->_normalize($temp); 864 | case MATH_BIGINTEGER_MODE_BCMATH: 865 | $temp = new Math_BigInteger(); 866 | $temp->value = bcadd($this->value, $y->value, 0); 867 | 868 | return $this->_normalize($temp); 869 | } 870 | 871 | $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative); 872 | 873 | $result = new Math_BigInteger(); 874 | $result->value = $temp[MATH_BIGINTEGER_VALUE]; 875 | $result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; 876 | 877 | return $this->_normalize($result); 878 | } 879 | 880 | /** 881 | * Performs addition. 882 | * 883 | * @param Array $x_value 884 | * @param Boolean $x_negative 885 | * @param Array $y_value 886 | * @param Boolean $y_negative 887 | * @return Array 888 | * @access private 889 | */ 890 | function _add($x_value, $x_negative, $y_value, $y_negative) 891 | { 892 | $x_size = count($x_value); 893 | $y_size = count($y_value); 894 | 895 | if ($x_size == 0) { 896 | return array( 897 | MATH_BIGINTEGER_VALUE => $y_value, 898 | MATH_BIGINTEGER_SIGN => $y_negative 899 | ); 900 | } else if ($y_size == 0) { 901 | return array( 902 | MATH_BIGINTEGER_VALUE => $x_value, 903 | MATH_BIGINTEGER_SIGN => $x_negative 904 | ); 905 | } 906 | 907 | // subtract, if appropriate 908 | if ( $x_negative != $y_negative ) { 909 | if ( $x_value == $y_value ) { 910 | return array( 911 | MATH_BIGINTEGER_VALUE => array(), 912 | MATH_BIGINTEGER_SIGN => false 913 | ); 914 | } 915 | 916 | $temp = $this->_subtract($x_value, false, $y_value, false); 917 | $temp[MATH_BIGINTEGER_SIGN] = $this->_compare($x_value, false, $y_value, false) > 0 ? 918 | $x_negative : $y_negative; 919 | 920 | return $temp; 921 | } 922 | 923 | if ($x_size < $y_size) { 924 | $size = $x_size; 925 | $value = $y_value; 926 | } else { 927 | $size = $y_size; 928 | $value = $x_value; 929 | } 930 | 931 | $value[] = 0; // just in case the carry adds an extra digit 932 | 933 | $carry = 0; 934 | for ($i = 0, $j = 1; $j < $size; $i+=2, $j+=2) { 935 | $sum = $x_value[$j] * MATH_BIGINTEGER_BASE_FULL + $x_value[$i] + $y_value[$j] * MATH_BIGINTEGER_BASE_FULL + $y_value[$i] + $carry; 936 | $carry = $sum >= MATH_BIGINTEGER_MAX_DIGIT2; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 937 | $sum = $carry ? $sum - MATH_BIGINTEGER_MAX_DIGIT2 : $sum; 938 | 939 | $temp = (int) ($sum / MATH_BIGINTEGER_BASE_FULL); 940 | 941 | $value[$i] = (int) ($sum - MATH_BIGINTEGER_BASE_FULL * $temp); // eg. a faster alternative to fmod($sum, 0x4000000) 942 | $value[$j] = $temp; 943 | } 944 | 945 | if ($j == $size) { // ie. if $y_size is odd 946 | $sum = $x_value[$i] + $y_value[$i] + $carry; 947 | $carry = $sum >= MATH_BIGINTEGER_BASE_FULL; 948 | $value[$i] = $carry ? $sum - MATH_BIGINTEGER_BASE_FULL : $sum; 949 | ++$i; // ie. let $i = $j since we've just done $value[$i] 950 | } 951 | 952 | if ($carry) { 953 | for (; $value[$i] == MATH_BIGINTEGER_MAX_DIGIT; ++$i) { 954 | $value[$i] = 0; 955 | } 956 | ++$value[$i]; 957 | } 958 | 959 | return array( 960 | MATH_BIGINTEGER_VALUE => $this->_trim($value), 961 | MATH_BIGINTEGER_SIGN => $x_negative 962 | ); 963 | } 964 | 965 | /** 966 | * Subtracts two BigIntegers. 967 | * 968 | * Here's an example: 969 | * 970 | * subtract($b); 977 | * 978 | * echo $c->toString(); // outputs -10 979 | * ?> 980 | * 981 | * 982 | * @param Math_BigInteger $y 983 | * @return Math_BigInteger 984 | * @access public 985 | * @internal Performs base-2**52 subtraction 986 | */ 987 | function subtract($y) 988 | { 989 | switch ( MATH_BIGINTEGER_MODE ) { 990 | case MATH_BIGINTEGER_MODE_GMP: 991 | $temp = new Math_BigInteger(); 992 | $temp->value = gmp_sub($this->value, $y->value); 993 | 994 | return $this->_normalize($temp); 995 | case MATH_BIGINTEGER_MODE_BCMATH: 996 | $temp = new Math_BigInteger(); 997 | $temp->value = bcsub($this->value, $y->value, 0); 998 | 999 | return $this->_normalize($temp); 1000 | } 1001 | 1002 | $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative); 1003 | 1004 | $result = new Math_BigInteger(); 1005 | $result->value = $temp[MATH_BIGINTEGER_VALUE]; 1006 | $result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; 1007 | 1008 | return $this->_normalize($result); 1009 | } 1010 | 1011 | /** 1012 | * Performs subtraction. 1013 | * 1014 | * @param Array $x_value 1015 | * @param Boolean $x_negative 1016 | * @param Array $y_value 1017 | * @param Boolean $y_negative 1018 | * @return Array 1019 | * @access private 1020 | */ 1021 | function _subtract($x_value, $x_negative, $y_value, $y_negative) 1022 | { 1023 | $x_size = count($x_value); 1024 | $y_size = count($y_value); 1025 | 1026 | if ($x_size == 0) { 1027 | return array( 1028 | MATH_BIGINTEGER_VALUE => $y_value, 1029 | MATH_BIGINTEGER_SIGN => !$y_negative 1030 | ); 1031 | } else if ($y_size == 0) { 1032 | return array( 1033 | MATH_BIGINTEGER_VALUE => $x_value, 1034 | MATH_BIGINTEGER_SIGN => $x_negative 1035 | ); 1036 | } 1037 | 1038 | // add, if appropriate (ie. -$x - +$y or +$x - -$y) 1039 | if ( $x_negative != $y_negative ) { 1040 | $temp = $this->_add($x_value, false, $y_value, false); 1041 | $temp[MATH_BIGINTEGER_SIGN] = $x_negative; 1042 | 1043 | return $temp; 1044 | } 1045 | 1046 | $diff = $this->_compare($x_value, $x_negative, $y_value, $y_negative); 1047 | 1048 | if ( !$diff ) { 1049 | return array( 1050 | MATH_BIGINTEGER_VALUE => array(), 1051 | MATH_BIGINTEGER_SIGN => false 1052 | ); 1053 | } 1054 | 1055 | // switch $x and $y around, if appropriate. 1056 | if ( (!$x_negative && $diff < 0) || ($x_negative && $diff > 0) ) { 1057 | $temp = $x_value; 1058 | $x_value = $y_value; 1059 | $y_value = $temp; 1060 | 1061 | $x_negative = !$x_negative; 1062 | 1063 | $x_size = count($x_value); 1064 | $y_size = count($y_value); 1065 | } 1066 | 1067 | // at this point, $x_value should be at least as big as - if not bigger than - $y_value 1068 | 1069 | $carry = 0; 1070 | for ($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2) { 1071 | $sum = $x_value[$j] * MATH_BIGINTEGER_BASE_FULL + $x_value[$i] - $y_value[$j] * MATH_BIGINTEGER_BASE_FULL - $y_value[$i] - $carry; 1072 | $carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 1073 | $sum = $carry ? $sum + MATH_BIGINTEGER_MAX_DIGIT2 : $sum; 1074 | 1075 | $temp = (int) ($sum / MATH_BIGINTEGER_BASE_FULL); 1076 | 1077 | $x_value[$i] = (int) ($sum - MATH_BIGINTEGER_BASE_FULL * $temp); 1078 | $x_value[$j] = $temp; 1079 | } 1080 | 1081 | if ($j == $y_size) { // ie. if $y_size is odd 1082 | $sum = $x_value[$i] - $y_value[$i] - $carry; 1083 | $carry = $sum < 0; 1084 | $x_value[$i] = $carry ? $sum + MATH_BIGINTEGER_BASE_FULL : $sum; 1085 | ++$i; 1086 | } 1087 | 1088 | if ($carry) { 1089 | for (; !$x_value[$i]; ++$i) { 1090 | $x_value[$i] = MATH_BIGINTEGER_MAX_DIGIT; 1091 | } 1092 | --$x_value[$i]; 1093 | } 1094 | 1095 | return array( 1096 | MATH_BIGINTEGER_VALUE => $this->_trim($x_value), 1097 | MATH_BIGINTEGER_SIGN => $x_negative 1098 | ); 1099 | } 1100 | 1101 | /** 1102 | * Multiplies two BigIntegers 1103 | * 1104 | * Here's an example: 1105 | * 1106 | * multiply($b); 1113 | * 1114 | * echo $c->toString(); // outputs 200 1115 | * ?> 1116 | * 1117 | * 1118 | * @param Math_BigInteger $x 1119 | * @return Math_BigInteger 1120 | * @access public 1121 | */ 1122 | function multiply($x) 1123 | { 1124 | switch ( MATH_BIGINTEGER_MODE ) { 1125 | case MATH_BIGINTEGER_MODE_GMP: 1126 | $temp = new Math_BigInteger(); 1127 | $temp->value = gmp_mul($this->value, $x->value); 1128 | 1129 | return $this->_normalize($temp); 1130 | case MATH_BIGINTEGER_MODE_BCMATH: 1131 | $temp = new Math_BigInteger(); 1132 | $temp->value = bcmul($this->value, $x->value, 0); 1133 | 1134 | return $this->_normalize($temp); 1135 | } 1136 | 1137 | $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative); 1138 | 1139 | $product = new Math_BigInteger(); 1140 | $product->value = $temp[MATH_BIGINTEGER_VALUE]; 1141 | $product->is_negative = $temp[MATH_BIGINTEGER_SIGN]; 1142 | 1143 | return $this->_normalize($product); 1144 | } 1145 | 1146 | /** 1147 | * Performs multiplication. 1148 | * 1149 | * @param Array $x_value 1150 | * @param Boolean $x_negative 1151 | * @param Array $y_value 1152 | * @param Boolean $y_negative 1153 | * @return Array 1154 | * @access private 1155 | */ 1156 | function _multiply($x_value, $x_negative, $y_value, $y_negative) 1157 | { 1158 | //if ( $x_value == $y_value ) { 1159 | // return array( 1160 | // MATH_BIGINTEGER_VALUE => $this->_square($x_value), 1161 | // MATH_BIGINTEGER_SIGN => $x_sign != $y_value 1162 | // ); 1163 | //} 1164 | 1165 | $x_length = count($x_value); 1166 | $y_length = count($y_value); 1167 | 1168 | if ( !$x_length || !$y_length ) { // a 0 is being multiplied 1169 | return array( 1170 | MATH_BIGINTEGER_VALUE => array(), 1171 | MATH_BIGINTEGER_SIGN => false 1172 | ); 1173 | } 1174 | 1175 | return array( 1176 | MATH_BIGINTEGER_VALUE => min($x_length, $y_length) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ? 1177 | $this->_trim($this->_regularMultiply($x_value, $y_value)) : 1178 | $this->_trim($this->_karatsuba($x_value, $y_value)), 1179 | MATH_BIGINTEGER_SIGN => $x_negative != $y_negative 1180 | ); 1181 | } 1182 | 1183 | /** 1184 | * Performs long multiplication on two BigIntegers 1185 | * 1186 | * Modeled after 'multiply' in MutableBigInteger.java. 1187 | * 1188 | * @param Array $x_value 1189 | * @param Array $y_value 1190 | * @return Array 1191 | * @access private 1192 | */ 1193 | function _regularMultiply($x_value, $y_value) 1194 | { 1195 | $x_length = count($x_value); 1196 | $y_length = count($y_value); 1197 | 1198 | if ( !$x_length || !$y_length ) { // a 0 is being multiplied 1199 | return array(); 1200 | } 1201 | 1202 | if ( $x_length < $y_length ) { 1203 | $temp = $x_value; 1204 | $x_value = $y_value; 1205 | $y_value = $temp; 1206 | 1207 | $x_length = count($x_value); 1208 | $y_length = count($y_value); 1209 | } 1210 | 1211 | $product_value = $this->_array_repeat(0, $x_length + $y_length); 1212 | 1213 | // the following for loop could be removed if the for loop following it 1214 | // (the one with nested for loops) initially set $i to 0, but 1215 | // doing so would also make the result in one set of unnecessary adds, 1216 | // since on the outermost loops first pass, $product->value[$k] is going 1217 | // to always be 0 1218 | 1219 | $carry = 0; 1220 | 1221 | for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0 1222 | $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 1223 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 1224 | $product_value[$j] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry); 1225 | } 1226 | 1227 | $product_value[$j] = $carry; 1228 | 1229 | // the above for loop is what the previous comment was talking about. the 1230 | // following for loop is the "one with nested for loops" 1231 | for ($i = 1; $i < $y_length; ++$i) { 1232 | $carry = 0; 1233 | 1234 | for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) { 1235 | $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; 1236 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 1237 | $product_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry); 1238 | } 1239 | 1240 | $product_value[$k] = $carry; 1241 | } 1242 | 1243 | return $product_value; 1244 | } 1245 | 1246 | /** 1247 | * Performs Karatsuba multiplication on two BigIntegers 1248 | * 1249 | * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and 1250 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}. 1251 | * 1252 | * @param Array $x_value 1253 | * @param Array $y_value 1254 | * @return Array 1255 | * @access private 1256 | */ 1257 | function _karatsuba($x_value, $y_value) 1258 | { 1259 | $m = min(count($x_value) >> 1, count($y_value) >> 1); 1260 | 1261 | if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) { 1262 | return $this->_regularMultiply($x_value, $y_value); 1263 | } 1264 | 1265 | $x1 = array_slice($x_value, $m); 1266 | $x0 = array_slice($x_value, 0, $m); 1267 | $y1 = array_slice($y_value, $m); 1268 | $y0 = array_slice($y_value, 0, $m); 1269 | 1270 | $z2 = $this->_karatsuba($x1, $y1); 1271 | $z0 = $this->_karatsuba($x0, $y0); 1272 | 1273 | $z1 = $this->_add($x1, false, $x0, false); 1274 | $temp = $this->_add($y1, false, $y0, false); 1275 | $z1 = $this->_karatsuba($z1[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_VALUE]); 1276 | $temp = $this->_add($z2, false, $z0, false); 1277 | $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false); 1278 | 1279 | $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); 1280 | $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]); 1281 | 1282 | $xy = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]); 1283 | $xy = $this->_add($xy[MATH_BIGINTEGER_VALUE], $xy[MATH_BIGINTEGER_SIGN], $z0, false); 1284 | 1285 | return $xy[MATH_BIGINTEGER_VALUE]; 1286 | } 1287 | 1288 | /** 1289 | * Performs squaring 1290 | * 1291 | * @param Array $x 1292 | * @return Array 1293 | * @access private 1294 | */ 1295 | function _square($x = false) 1296 | { 1297 | return count($x) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ? 1298 | $this->_trim($this->_baseSquare($x)) : 1299 | $this->_trim($this->_karatsubaSquare($x)); 1300 | } 1301 | 1302 | /** 1303 | * Performs traditional squaring on two BigIntegers 1304 | * 1305 | * Squaring can be done faster than multiplying a number by itself can be. See 1306 | * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} / 1307 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information. 1308 | * 1309 | * @param Array $value 1310 | * @return Array 1311 | * @access private 1312 | */ 1313 | function _baseSquare($value) 1314 | { 1315 | if ( empty($value) ) { 1316 | return array(); 1317 | } 1318 | $square_value = $this->_array_repeat(0, 2 * count($value)); 1319 | 1320 | for ($i = 0, $max_index = count($value) - 1; $i <= $max_index; ++$i) { 1321 | $i2 = $i << 1; 1322 | 1323 | $temp = $square_value[$i2] + $value[$i] * $value[$i]; 1324 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 1325 | $square_value[$i2] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry); 1326 | 1327 | // note how we start from $i+1 instead of 0 as we do in multiplication. 1328 | for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) { 1329 | $temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry; 1330 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 1331 | $square_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry); 1332 | } 1333 | 1334 | // the following line can yield values larger 2**15. at this point, PHP should switch 1335 | // over to floats. 1336 | $square_value[$i + $max_index + 1] = $carry; 1337 | } 1338 | 1339 | return $square_value; 1340 | } 1341 | 1342 | /** 1343 | * Performs Karatsuba "squaring" on two BigIntegers 1344 | * 1345 | * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and 1346 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}. 1347 | * 1348 | * @param Array $value 1349 | * @return Array 1350 | * @access private 1351 | */ 1352 | function _karatsubaSquare($value) 1353 | { 1354 | $m = count($value) >> 1; 1355 | 1356 | if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) { 1357 | return $this->_baseSquare($value); 1358 | } 1359 | 1360 | $x1 = array_slice($value, $m); 1361 | $x0 = array_slice($value, 0, $m); 1362 | 1363 | $z2 = $this->_karatsubaSquare($x1); 1364 | $z0 = $this->_karatsubaSquare($x0); 1365 | 1366 | $z1 = $this->_add($x1, false, $x0, false); 1367 | $z1 = $this->_karatsubaSquare($z1[MATH_BIGINTEGER_VALUE]); 1368 | $temp = $this->_add($z2, false, $z0, false); 1369 | $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false); 1370 | 1371 | $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); 1372 | $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]); 1373 | 1374 | $xx = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]); 1375 | $xx = $this->_add($xx[MATH_BIGINTEGER_VALUE], $xx[MATH_BIGINTEGER_SIGN], $z0, false); 1376 | 1377 | return $xx[MATH_BIGINTEGER_VALUE]; 1378 | } 1379 | 1380 | /** 1381 | * Divides two BigIntegers. 1382 | * 1383 | * Returns an array whose first element contains the quotient and whose second element contains the 1384 | * "common residue". If the remainder would be positive, the "common residue" and the remainder are the 1385 | * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder 1386 | * and the divisor (basically, the "common residue" is the first positive modulo). 1387 | * 1388 | * Here's an example: 1389 | * 1390 | * divide($b); 1397 | * 1398 | * echo $quotient->toString(); // outputs 0 1399 | * echo "\r\n"; 1400 | * echo $remainder->toString(); // outputs 10 1401 | * ?> 1402 | * 1403 | * 1404 | * @param Math_BigInteger $y 1405 | * @return Array 1406 | * @access public 1407 | * @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}. 1408 | */ 1409 | function divide($y) 1410 | { 1411 | switch ( MATH_BIGINTEGER_MODE ) { 1412 | case MATH_BIGINTEGER_MODE_GMP: 1413 | $quotient = new Math_BigInteger(); 1414 | $remainder = new Math_BigInteger(); 1415 | 1416 | list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value); 1417 | 1418 | if (gmp_sign($remainder->value) < 0) { 1419 | $remainder->value = gmp_add($remainder->value, gmp_abs($y->value)); 1420 | } 1421 | 1422 | return array($this->_normalize($quotient), $this->_normalize($remainder)); 1423 | case MATH_BIGINTEGER_MODE_BCMATH: 1424 | $quotient = new Math_BigInteger(); 1425 | $remainder = new Math_BigInteger(); 1426 | 1427 | $quotient->value = bcdiv($this->value, $y->value, 0); 1428 | $remainder->value = bcmod($this->value, $y->value); 1429 | 1430 | if ($remainder->value[0] == '-') { 1431 | $remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0); 1432 | } 1433 | 1434 | return array($this->_normalize($quotient), $this->_normalize($remainder)); 1435 | } 1436 | 1437 | if (count($y->value) == 1) { 1438 | list($q, $r) = $this->_divide_digit($this->value, $y->value[0]); 1439 | $quotient = new Math_BigInteger(); 1440 | $remainder = new Math_BigInteger(); 1441 | $quotient->value = $q; 1442 | $remainder->value = array($r); 1443 | $quotient->is_negative = $this->is_negative != $y->is_negative; 1444 | return array($this->_normalize($quotient), $this->_normalize($remainder)); 1445 | } 1446 | 1447 | static $zero; 1448 | if ( !isset($zero) ) { 1449 | $zero = new Math_BigInteger(); 1450 | } 1451 | 1452 | $x = $this->copy(); 1453 | $y = $y->copy(); 1454 | 1455 | $x_sign = $x->is_negative; 1456 | $y_sign = $y->is_negative; 1457 | 1458 | $x->is_negative = $y->is_negative = false; 1459 | 1460 | $diff = $x->compare($y); 1461 | 1462 | if ( !$diff ) { 1463 | $temp = new Math_BigInteger(); 1464 | $temp->value = array(1); 1465 | $temp->is_negative = $x_sign != $y_sign; 1466 | return array($this->_normalize($temp), $this->_normalize(new Math_BigInteger())); 1467 | } 1468 | 1469 | if ( $diff < 0 ) { 1470 | // if $x is negative, "add" $y. 1471 | if ( $x_sign ) { 1472 | $x = $y->subtract($x); 1473 | } 1474 | return array($this->_normalize(new Math_BigInteger()), $this->_normalize($x)); 1475 | } 1476 | 1477 | // normalize $x and $y as described in HAC 14.23 / 14.24 1478 | $msb = $y->value[count($y->value) - 1]; 1479 | for ($shift = 0; !($msb & MATH_BIGINTEGER_MSB); ++$shift) { 1480 | $msb <<= 1; 1481 | } 1482 | $x->_lshift($shift); 1483 | $y->_lshift($shift); 1484 | $y_value = &$y->value; 1485 | 1486 | $x_max = count($x->value) - 1; 1487 | $y_max = count($y->value) - 1; 1488 | 1489 | $quotient = new Math_BigInteger(); 1490 | $quotient_value = &$quotient->value; 1491 | $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1); 1492 | 1493 | static $temp, $lhs, $rhs; 1494 | if (!isset($temp)) { 1495 | $temp = new Math_BigInteger(); 1496 | $lhs = new Math_BigInteger(); 1497 | $rhs = new Math_BigInteger(); 1498 | } 1499 | $temp_value = &$temp->value; 1500 | $rhs_value = &$rhs->value; 1501 | 1502 | // $temp = $y << ($x_max - $y_max-1) in base 2**26 1503 | $temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value); 1504 | 1505 | while ( $x->compare($temp) >= 0 ) { 1506 | // calculate the "common residue" 1507 | ++$quotient_value[$x_max - $y_max]; 1508 | $x = $x->subtract($temp); 1509 | $x_max = count($x->value) - 1; 1510 | } 1511 | 1512 | for ($i = $x_max; $i >= $y_max + 1; --$i) { 1513 | $x_value = &$x->value; 1514 | $x_window = array( 1515 | isset($x_value[$i]) ? $x_value[$i] : 0, 1516 | isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0, 1517 | isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0 1518 | ); 1519 | $y_window = array( 1520 | $y_value[$y_max], 1521 | ( $y_max > 0 ) ? $y_value[$y_max - 1] : 0 1522 | ); 1523 | 1524 | $q_index = $i - $y_max - 1; 1525 | if ($x_window[0] == $y_window[0]) { 1526 | $quotient_value[$q_index] = MATH_BIGINTEGER_MAX_DIGIT; 1527 | } else { 1528 | $quotient_value[$q_index] = (int) ( 1529 | ($x_window[0] * MATH_BIGINTEGER_BASE_FULL + $x_window[1]) 1530 | / 1531 | $y_window[0] 1532 | ); 1533 | } 1534 | 1535 | $temp_value = array($y_window[1], $y_window[0]); 1536 | 1537 | $lhs->value = array($quotient_value[$q_index]); 1538 | $lhs = $lhs->multiply($temp); 1539 | 1540 | $rhs_value = array($x_window[2], $x_window[1], $x_window[0]); 1541 | 1542 | while ( $lhs->compare($rhs) > 0 ) { 1543 | --$quotient_value[$q_index]; 1544 | 1545 | $lhs->value = array($quotient_value[$q_index]); 1546 | $lhs = $lhs->multiply($temp); 1547 | } 1548 | 1549 | $adjust = $this->_array_repeat(0, $q_index); 1550 | $temp_value = array($quotient_value[$q_index]); 1551 | $temp = $temp->multiply($y); 1552 | $temp_value = &$temp->value; 1553 | $temp_value = array_merge($adjust, $temp_value); 1554 | 1555 | $x = $x->subtract($temp); 1556 | 1557 | if ($x->compare($zero) < 0) { 1558 | $temp_value = array_merge($adjust, $y_value); 1559 | $x = $x->add($temp); 1560 | 1561 | --$quotient_value[$q_index]; 1562 | } 1563 | 1564 | $x_max = count($x_value) - 1; 1565 | } 1566 | 1567 | // unnormalize the remainder 1568 | $x->_rshift($shift); 1569 | 1570 | $quotient->is_negative = $x_sign != $y_sign; 1571 | 1572 | // calculate the "common residue", if appropriate 1573 | if ( $x_sign ) { 1574 | $y->_rshift($shift); 1575 | $x = $y->subtract($x); 1576 | } 1577 | 1578 | return array($this->_normalize($quotient), $this->_normalize($x)); 1579 | } 1580 | 1581 | /** 1582 | * Divides a BigInteger by a regular integer 1583 | * 1584 | * abc / x = a00 / x + b0 / x + c / x 1585 | * 1586 | * @param Array $dividend 1587 | * @param Array $divisor 1588 | * @return Array 1589 | * @access private 1590 | */ 1591 | function _divide_digit($dividend, $divisor) 1592 | { 1593 | $carry = 0; 1594 | $result = array(); 1595 | 1596 | for ($i = count($dividend) - 1; $i >= 0; --$i) { 1597 | $temp = MATH_BIGINTEGER_BASE_FULL * $carry + $dividend[$i]; 1598 | $result[$i] = (int) ($temp / $divisor); 1599 | $carry = (int) ($temp - $divisor * $result[$i]); 1600 | } 1601 | 1602 | return array($result, $carry); 1603 | } 1604 | 1605 | /** 1606 | * Performs modular exponentiation. 1607 | * 1608 | * Here's an example: 1609 | * 1610 | * modPow($b, $c); 1618 | * 1619 | * echo $c->toString(); // outputs 10 1620 | * ?> 1621 | * 1622 | * 1623 | * @param Math_BigInteger $e 1624 | * @param Math_BigInteger $n 1625 | * @return Math_BigInteger 1626 | * @access public 1627 | * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and 1628 | * and although the approach involving repeated squaring does vastly better, it, too, is impractical 1629 | * for our purposes. The reason being that division - by far the most complicated and time-consuming 1630 | * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. 1631 | * 1632 | * Modular reductions resolve this issue. Although an individual modular reduction takes more time 1633 | * then an individual division, when performed in succession (with the same modulo), they're a lot faster. 1634 | * 1635 | * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, 1636 | * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the 1637 | * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because 1638 | * the product of two odd numbers is odd), but what about when RSA isn't used? 1639 | * 1640 | * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a 1641 | * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the 1642 | * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, 1643 | * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and 1644 | * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. 1645 | * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. 1646 | */ 1647 | function modPow($e, $n) 1648 | { 1649 | $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs(); 1650 | 1651 | if ($e->compare(new Math_BigInteger()) < 0) { 1652 | $e = $e->abs(); 1653 | 1654 | $temp = $this->modInverse($n); 1655 | if ($temp === false) { 1656 | return false; 1657 | } 1658 | 1659 | return $this->_normalize($temp->modPow($e, $n)); 1660 | } 1661 | 1662 | if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP ) { 1663 | $temp = new Math_BigInteger(); 1664 | $temp->value = gmp_powm($this->value, $e->value, $n->value); 1665 | 1666 | return $this->_normalize($temp); 1667 | } 1668 | 1669 | if ($this->compare(new Math_BigInteger()) < 0 || $this->compare($n) > 0) { 1670 | list(, $temp) = $this->divide($n); 1671 | return $temp->modPow($e, $n); 1672 | } 1673 | 1674 | if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { 1675 | $components = array( 1676 | 'modulus' => $n->toBytes(true), 1677 | 'publicExponent' => $e->toBytes(true) 1678 | ); 1679 | 1680 | $components = array( 1681 | 'modulus' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['modulus'])), $components['modulus']), 1682 | 'publicExponent' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['publicExponent'])), $components['publicExponent']) 1683 | ); 1684 | 1685 | $RSAPublicKey = pack('Ca*a*a*', 1686 | 48, $this->_encodeASN1Length(strlen($components['modulus']) + strlen($components['publicExponent'])), 1687 | $components['modulus'], $components['publicExponent'] 1688 | ); 1689 | 1690 | $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA 1691 | $RSAPublicKey = chr(0) . $RSAPublicKey; 1692 | $RSAPublicKey = chr(3) . $this->_encodeASN1Length(strlen($RSAPublicKey)) . $RSAPublicKey; 1693 | 1694 | $encapsulated = pack('Ca*a*', 1695 | 48, $this->_encodeASN1Length(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey 1696 | ); 1697 | 1698 | $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" . 1699 | chunk_split(base64_encode($encapsulated)) . 1700 | '-----END PUBLIC KEY-----'; 1701 | 1702 | $plaintext = str_pad($this->toBytes(), strlen($n->toBytes(true)) - 1, "\0", STR_PAD_LEFT); 1703 | 1704 | if (openssl_public_encrypt($plaintext, $result, $RSAPublicKey, OPENSSL_NO_PADDING)) { 1705 | return new Math_BigInteger($result, 256); 1706 | } 1707 | } 1708 | 1709 | if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { 1710 | $temp = new Math_BigInteger(); 1711 | $temp->value = bcpowmod($this->value, $e->value, $n->value, 0); 1712 | 1713 | return $this->_normalize($temp); 1714 | } 1715 | 1716 | if ( empty($e->value) ) { 1717 | $temp = new Math_BigInteger(); 1718 | $temp->value = array(1); 1719 | return $this->_normalize($temp); 1720 | } 1721 | 1722 | if ( $e->value == array(1) ) { 1723 | list(, $temp) = $this->divide($n); 1724 | return $this->_normalize($temp); 1725 | } 1726 | 1727 | if ( $e->value == array(2) ) { 1728 | $temp = new Math_BigInteger(); 1729 | $temp->value = $this->_square($this->value); 1730 | list(, $temp) = $temp->divide($n); 1731 | return $this->_normalize($temp); 1732 | } 1733 | 1734 | return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_BARRETT)); 1735 | 1736 | // is the modulo odd? 1737 | if ( $n->value[0] & 1 ) { 1738 | return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_MONTGOMERY)); 1739 | } 1740 | // if it's not, it's even 1741 | 1742 | // find the lowest set bit (eg. the max pow of 2 that divides $n) 1743 | for ($i = 0; $i < count($n->value); ++$i) { 1744 | if ( $n->value[$i] ) { 1745 | $temp = decbin($n->value[$i]); 1746 | $j = strlen($temp) - strrpos($temp, '1') - 1; 1747 | $j+= 26 * $i; 1748 | break; 1749 | } 1750 | } 1751 | // at this point, 2^$j * $n/(2^$j) == $n 1752 | 1753 | $mod1 = $n->copy(); 1754 | $mod1->_rshift($j); 1755 | $mod2 = new Math_BigInteger(); 1756 | $mod2->value = array(1); 1757 | $mod2->_lshift($j); 1758 | 1759 | $part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new Math_BigInteger(); 1760 | $part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2); 1761 | 1762 | $y1 = $mod2->modInverse($mod1); 1763 | $y2 = $mod1->modInverse($mod2); 1764 | 1765 | $result = $part1->multiply($mod2); 1766 | $result = $result->multiply($y1); 1767 | 1768 | $temp = $part2->multiply($mod1); 1769 | $temp = $temp->multiply($y2); 1770 | 1771 | $result = $result->add($temp); 1772 | list(, $result) = $result->divide($n); 1773 | 1774 | return $this->_normalize($result); 1775 | } 1776 | 1777 | /** 1778 | * Performs modular exponentiation. 1779 | * 1780 | * Alias for Math_BigInteger::modPow() 1781 | * 1782 | * @param Math_BigInteger $e 1783 | * @param Math_BigInteger $n 1784 | * @return Math_BigInteger 1785 | * @access public 1786 | */ 1787 | function powMod($e, $n) 1788 | { 1789 | return $this->modPow($e, $n); 1790 | } 1791 | 1792 | /** 1793 | * Sliding Window k-ary Modular Exponentiation 1794 | * 1795 | * Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} / 1796 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims, 1797 | * however, this function performs a modular reduction after every multiplication and squaring operation. 1798 | * As such, this function has the same preconditions that the reductions being used do. 1799 | * 1800 | * @param Math_BigInteger $e 1801 | * @param Math_BigInteger $n 1802 | * @param Integer $mode 1803 | * @return Math_BigInteger 1804 | * @access private 1805 | */ 1806 | function _slidingWindow($e, $n, $mode) 1807 | { 1808 | static $window_ranges = array(7, 25, 81, 241, 673, 1793); // from BigInteger.java's oddModPow function 1809 | //static $window_ranges = array(0, 7, 36, 140, 450, 1303, 3529); // from MPM 7.3.1 1810 | 1811 | $e_value = $e->value; 1812 | $e_length = count($e_value) - 1; 1813 | $e_bits = decbin($e_value[$e_length]); 1814 | for ($i = $e_length - 1; $i >= 0; --$i) { 1815 | $e_bits.= str_pad(decbin($e_value[$i]), MATH_BIGINTEGER_BASE, '0', STR_PAD_LEFT); 1816 | } 1817 | 1818 | $e_length = strlen($e_bits); 1819 | 1820 | // calculate the appropriate window size. 1821 | // $window_size == 3 if $window_ranges is between 25 and 81, for example. 1822 | for ($i = 0, $window_size = 1; $e_length > $window_ranges[$i] && $i < count($window_ranges); ++$window_size, ++$i); 1823 | 1824 | $n_value = $n->value; 1825 | 1826 | // precompute $this^0 through $this^$window_size 1827 | $powers = array(); 1828 | $powers[1] = $this->_prepareReduce($this->value, $n_value, $mode); 1829 | $powers[2] = $this->_squareReduce($powers[1], $n_value, $mode); 1830 | 1831 | // we do every other number since substr($e_bits, $i, $j+1) (see below) is supposed to end 1832 | // in a 1. ie. it's supposed to be odd. 1833 | $temp = 1 << ($window_size - 1); 1834 | for ($i = 1; $i < $temp; ++$i) { 1835 | $i2 = $i << 1; 1836 | $powers[$i2 + 1] = $this->_multiplyReduce($powers[$i2 - 1], $powers[2], $n_value, $mode); 1837 | } 1838 | 1839 | $result = array(1); 1840 | $result = $this->_prepareReduce($result, $n_value, $mode); 1841 | 1842 | for ($i = 0; $i < $e_length; ) { 1843 | if ( !$e_bits[$i] ) { 1844 | $result = $this->_squareReduce($result, $n_value, $mode); 1845 | ++$i; 1846 | } else { 1847 | for ($j = $window_size - 1; $j > 0; --$j) { 1848 | if ( !empty($e_bits[$i + $j]) ) { 1849 | break; 1850 | } 1851 | } 1852 | 1853 | for ($k = 0; $k <= $j; ++$k) {// eg. the length of substr($e_bits, $i, $j+1) 1854 | $result = $this->_squareReduce($result, $n_value, $mode); 1855 | } 1856 | 1857 | $result = $this->_multiplyReduce($result, $powers[bindec(substr($e_bits, $i, $j + 1))], $n_value, $mode); 1858 | 1859 | $i+=$j + 1; 1860 | } 1861 | } 1862 | 1863 | $temp = new Math_BigInteger(); 1864 | $temp->value = $this->_reduce($result, $n_value, $mode); 1865 | 1866 | return $temp; 1867 | } 1868 | 1869 | /** 1870 | * Modular reduction 1871 | * 1872 | * For most $modes this will return the remainder. 1873 | * 1874 | * @see _slidingWindow() 1875 | * @access private 1876 | * @param Array $x 1877 | * @param Array $n 1878 | * @param Integer $mode 1879 | * @return Array 1880 | */ 1881 | function _reduce($x, $n, $mode) 1882 | { 1883 | switch ($mode) { 1884 | case MATH_BIGINTEGER_MONTGOMERY: 1885 | return $this->_montgomery($x, $n); 1886 | case MATH_BIGINTEGER_BARRETT: 1887 | return $this->_barrett($x, $n); 1888 | case MATH_BIGINTEGER_POWEROF2: 1889 | $lhs = new Math_BigInteger(); 1890 | $lhs->value = $x; 1891 | $rhs = new Math_BigInteger(); 1892 | $rhs->value = $n; 1893 | return $x->_mod2($n); 1894 | case MATH_BIGINTEGER_CLASSIC: 1895 | $lhs = new Math_BigInteger(); 1896 | $lhs->value = $x; 1897 | $rhs = new Math_BigInteger(); 1898 | $rhs->value = $n; 1899 | list(, $temp) = $lhs->divide($rhs); 1900 | return $temp->value; 1901 | case MATH_BIGINTEGER_NONE: 1902 | return $x; 1903 | default: 1904 | // an invalid $mode was provided 1905 | } 1906 | } 1907 | 1908 | /** 1909 | * Modular reduction preperation 1910 | * 1911 | * @see _slidingWindow() 1912 | * @access private 1913 | * @param Array $x 1914 | * @param Array $n 1915 | * @param Integer $mode 1916 | * @return Array 1917 | */ 1918 | function _prepareReduce($x, $n, $mode) 1919 | { 1920 | if ($mode == MATH_BIGINTEGER_MONTGOMERY) { 1921 | return $this->_prepMontgomery($x, $n); 1922 | } 1923 | return $this->_reduce($x, $n, $mode); 1924 | } 1925 | 1926 | /** 1927 | * Modular multiply 1928 | * 1929 | * @see _slidingWindow() 1930 | * @access private 1931 | * @param Array $x 1932 | * @param Array $y 1933 | * @param Array $n 1934 | * @param Integer $mode 1935 | * @return Array 1936 | */ 1937 | function _multiplyReduce($x, $y, $n, $mode) 1938 | { 1939 | if ($mode == MATH_BIGINTEGER_MONTGOMERY) { 1940 | return $this->_montgomeryMultiply($x, $y, $n); 1941 | } 1942 | $temp = $this->_multiply($x, false, $y, false); 1943 | return $this->_reduce($temp[MATH_BIGINTEGER_VALUE], $n, $mode); 1944 | } 1945 | 1946 | /** 1947 | * Modular square 1948 | * 1949 | * @see _slidingWindow() 1950 | * @access private 1951 | * @param Array $x 1952 | * @param Array $n 1953 | * @param Integer $mode 1954 | * @return Array 1955 | */ 1956 | function _squareReduce($x, $n, $mode) 1957 | { 1958 | if ($mode == MATH_BIGINTEGER_MONTGOMERY) { 1959 | return $this->_montgomeryMultiply($x, $x, $n); 1960 | } 1961 | return $this->_reduce($this->_square($x), $n, $mode); 1962 | } 1963 | 1964 | /** 1965 | * Modulos for Powers of Two 1966 | * 1967 | * Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1), 1968 | * we'll just use this function as a wrapper for doing that. 1969 | * 1970 | * @see _slidingWindow() 1971 | * @access private 1972 | * @param Math_BigInteger 1973 | * @return Math_BigInteger 1974 | */ 1975 | function _mod2($n) 1976 | { 1977 | $temp = new Math_BigInteger(); 1978 | $temp->value = array(1); 1979 | return $this->bitwise_and($n->subtract($temp)); 1980 | } 1981 | 1982 | /** 1983 | * Barrett Modular Reduction 1984 | * 1985 | * See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} / 1986 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly, 1987 | * so as not to require negative numbers (initially, this script didn't support negative numbers). 1988 | * 1989 | * Employs "folding", as described at 1990 | * {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from 1991 | * it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x." 1992 | * 1993 | * Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that 1994 | * usable on account of (1) its not using reasonable radix points as discussed in 1995 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable 1996 | * radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that 1997 | * (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line 1998 | * comments for details. 1999 | * 2000 | * @see _slidingWindow() 2001 | * @access private 2002 | * @param Array $n 2003 | * @param Array $m 2004 | * @return Array 2005 | */ 2006 | function _barrett($n, $m) 2007 | { 2008 | static $cache = array( 2009 | MATH_BIGINTEGER_VARIABLE => array(), 2010 | MATH_BIGINTEGER_DATA => array() 2011 | ); 2012 | 2013 | $m_length = count($m); 2014 | 2015 | // if ($this->_compare($n, $this->_square($m)) >= 0) { 2016 | if (count($n) > 2 * $m_length) { 2017 | $lhs = new Math_BigInteger(); 2018 | $rhs = new Math_BigInteger(); 2019 | $lhs->value = $n; 2020 | $rhs->value = $m; 2021 | list(, $temp) = $lhs->divide($rhs); 2022 | return $temp->value; 2023 | } 2024 | 2025 | // if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced 2026 | if ($m_length < 5) { 2027 | return $this->_regularBarrett($n, $m); 2028 | } 2029 | 2030 | // n = 2 * m.length 2031 | 2032 | if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { 2033 | $key = count($cache[MATH_BIGINTEGER_VARIABLE]); 2034 | $cache[MATH_BIGINTEGER_VARIABLE][] = $m; 2035 | 2036 | $lhs = new Math_BigInteger(); 2037 | $lhs_value = &$lhs->value; 2038 | $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1)); 2039 | $lhs_value[] = 1; 2040 | $rhs = new Math_BigInteger(); 2041 | $rhs->value = $m; 2042 | 2043 | list($u, $m1) = $lhs->divide($rhs); 2044 | $u = $u->value; 2045 | $m1 = $m1->value; 2046 | 2047 | $cache[MATH_BIGINTEGER_DATA][] = array( 2048 | 'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1) 2049 | 'm1'=> $m1 // m.length 2050 | ); 2051 | } else { 2052 | extract($cache[MATH_BIGINTEGER_DATA][$key]); 2053 | } 2054 | 2055 | $cutoff = $m_length + ($m_length >> 1); 2056 | $lsd = array_slice($n, 0, $cutoff); // m.length + (m.length >> 1) 2057 | $msd = array_slice($n, $cutoff); // m.length >> 1 2058 | $lsd = $this->_trim($lsd); 2059 | $temp = $this->_multiply($msd, false, $m1, false); 2060 | $n = $this->_add($lsd, false, $temp[MATH_BIGINTEGER_VALUE], false); // m.length + (m.length >> 1) + 1 2061 | 2062 | if ($m_length & 1) { 2063 | return $this->_regularBarrett($n[MATH_BIGINTEGER_VALUE], $m); 2064 | } 2065 | 2066 | // (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2 2067 | $temp = array_slice($n[MATH_BIGINTEGER_VALUE], $m_length - 1); 2068 | // if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2 2069 | // if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1 2070 | $temp = $this->_multiply($temp, false, $u, false); 2071 | // if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1 2072 | // if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) 2073 | $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], ($m_length >> 1) + 1); 2074 | // if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1 2075 | // if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1) 2076 | $temp = $this->_multiply($temp, false, $m, false); 2077 | 2078 | // at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit 2079 | // number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop 2080 | // following this comment would loop a lot (hence our calling _regularBarrett() in that situation). 2081 | 2082 | $result = $this->_subtract($n[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false); 2083 | 2084 | while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false) >= 0) { 2085 | $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false); 2086 | } 2087 | 2088 | return $result[MATH_BIGINTEGER_VALUE]; 2089 | } 2090 | 2091 | /** 2092 | * (Regular) Barrett Modular Reduction 2093 | * 2094 | * For numbers with more than four digits Math_BigInteger::_barrett() is faster. The difference between that and this 2095 | * is that this function does not fold the denominator into a smaller form. 2096 | * 2097 | * @see _slidingWindow() 2098 | * @access private 2099 | * @param Array $x 2100 | * @param Array $n 2101 | * @return Array 2102 | */ 2103 | function _regularBarrett($x, $n) 2104 | { 2105 | static $cache = array( 2106 | MATH_BIGINTEGER_VARIABLE => array(), 2107 | MATH_BIGINTEGER_DATA => array() 2108 | ); 2109 | 2110 | $n_length = count($n); 2111 | 2112 | if (count($x) > 2 * $n_length) { 2113 | $lhs = new Math_BigInteger(); 2114 | $rhs = new Math_BigInteger(); 2115 | $lhs->value = $x; 2116 | $rhs->value = $n; 2117 | list(, $temp) = $lhs->divide($rhs); 2118 | return $temp->value; 2119 | } 2120 | 2121 | if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { 2122 | $key = count($cache[MATH_BIGINTEGER_VARIABLE]); 2123 | $cache[MATH_BIGINTEGER_VARIABLE][] = $n; 2124 | $lhs = new Math_BigInteger(); 2125 | $lhs_value = &$lhs->value; 2126 | $lhs_value = $this->_array_repeat(0, 2 * $n_length); 2127 | $lhs_value[] = 1; 2128 | $rhs = new Math_BigInteger(); 2129 | $rhs->value = $n; 2130 | list($temp, ) = $lhs->divide($rhs); // m.length 2131 | $cache[MATH_BIGINTEGER_DATA][] = $temp->value; 2132 | } 2133 | 2134 | // 2 * m.length - (m.length - 1) = m.length + 1 2135 | $temp = array_slice($x, $n_length - 1); 2136 | // (m.length + 1) + m.length = 2 * m.length + 1 2137 | $temp = $this->_multiply($temp, false, $cache[MATH_BIGINTEGER_DATA][$key], false); 2138 | // (2 * m.length + 1) - (m.length - 1) = m.length + 2 2139 | $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], $n_length + 1); 2140 | 2141 | // m.length + 1 2142 | $result = array_slice($x, 0, $n_length + 1); 2143 | // m.length + 1 2144 | $temp = $this->_multiplyLower($temp, false, $n, false, $n_length + 1); 2145 | // $temp == array_slice($temp->_multiply($temp, false, $n, false)->value, 0, $n_length + 1) 2146 | 2147 | if ($this->_compare($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]) < 0) { 2148 | $corrector_value = $this->_array_repeat(0, $n_length + 1); 2149 | $corrector_value[] = 1; 2150 | $result = $this->_add($result, false, $corrector_value, false); 2151 | $result = $result[MATH_BIGINTEGER_VALUE]; 2152 | } 2153 | 2154 | // at this point, we're subtracting a number with m.length + 1 digits from another number with m.length + 1 digits 2155 | $result = $this->_subtract($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]); 2156 | while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false) > 0) { 2157 | $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false); 2158 | } 2159 | 2160 | return $result[MATH_BIGINTEGER_VALUE]; 2161 | } 2162 | 2163 | /** 2164 | * Performs long multiplication up to $stop digits 2165 | * 2166 | * If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved. 2167 | * 2168 | * @see _regularBarrett() 2169 | * @param Array $x_value 2170 | * @param Boolean $x_negative 2171 | * @param Array $y_value 2172 | * @param Boolean $y_negative 2173 | * @param Integer $stop 2174 | * @return Array 2175 | * @access private 2176 | */ 2177 | function _multiplyLower($x_value, $x_negative, $y_value, $y_negative, $stop) 2178 | { 2179 | $x_length = count($x_value); 2180 | $y_length = count($y_value); 2181 | 2182 | if ( !$x_length || !$y_length ) { // a 0 is being multiplied 2183 | return array( 2184 | MATH_BIGINTEGER_VALUE => array(), 2185 | MATH_BIGINTEGER_SIGN => false 2186 | ); 2187 | } 2188 | 2189 | if ( $x_length < $y_length ) { 2190 | $temp = $x_value; 2191 | $x_value = $y_value; 2192 | $y_value = $temp; 2193 | 2194 | $x_length = count($x_value); 2195 | $y_length = count($y_value); 2196 | } 2197 | 2198 | $product_value = $this->_array_repeat(0, $x_length + $y_length); 2199 | 2200 | // the following for loop could be removed if the for loop following it 2201 | // (the one with nested for loops) initially set $i to 0, but 2202 | // doing so would also make the result in one set of unnecessary adds, 2203 | // since on the outermost loops first pass, $product->value[$k] is going 2204 | // to always be 0 2205 | 2206 | $carry = 0; 2207 | 2208 | for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0, $k = $i 2209 | $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 2210 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 2211 | $product_value[$j] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry); 2212 | } 2213 | 2214 | if ($j < $stop) { 2215 | $product_value[$j] = $carry; 2216 | } 2217 | 2218 | // the above for loop is what the previous comment was talking about. the 2219 | // following for loop is the "one with nested for loops" 2220 | 2221 | for ($i = 1; $i < $y_length; ++$i) { 2222 | $carry = 0; 2223 | 2224 | for ($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k) { 2225 | $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; 2226 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 2227 | $product_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry); 2228 | } 2229 | 2230 | if ($k < $stop) { 2231 | $product_value[$k] = $carry; 2232 | } 2233 | } 2234 | 2235 | return array( 2236 | MATH_BIGINTEGER_VALUE => $this->_trim($product_value), 2237 | MATH_BIGINTEGER_SIGN => $x_negative != $y_negative 2238 | ); 2239 | } 2240 | 2241 | /** 2242 | * Montgomery Modular Reduction 2243 | * 2244 | * ($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n. 2245 | * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be 2246 | * improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function 2247 | * to work correctly. 2248 | * 2249 | * @see _prepMontgomery() 2250 | * @see _slidingWindow() 2251 | * @access private 2252 | * @param Array $x 2253 | * @param Array $n 2254 | * @return Array 2255 | */ 2256 | function _montgomery($x, $n) 2257 | { 2258 | static $cache = array( 2259 | MATH_BIGINTEGER_VARIABLE => array(), 2260 | MATH_BIGINTEGER_DATA => array() 2261 | ); 2262 | 2263 | if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { 2264 | $key = count($cache[MATH_BIGINTEGER_VARIABLE]); 2265 | $cache[MATH_BIGINTEGER_VARIABLE][] = $x; 2266 | $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($n); 2267 | } 2268 | 2269 | $k = count($n); 2270 | 2271 | $result = array(MATH_BIGINTEGER_VALUE => $x); 2272 | 2273 | for ($i = 0; $i < $k; ++$i) { 2274 | $temp = $result[MATH_BIGINTEGER_VALUE][$i] * $cache[MATH_BIGINTEGER_DATA][$key]; 2275 | $temp = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * ((int) ($temp / MATH_BIGINTEGER_BASE_FULL))); 2276 | $temp = $this->_regularMultiply(array($temp), $n); 2277 | $temp = array_merge($this->_array_repeat(0, $i), $temp); 2278 | $result = $this->_add($result[MATH_BIGINTEGER_VALUE], false, $temp, false); 2279 | } 2280 | 2281 | $result[MATH_BIGINTEGER_VALUE] = array_slice($result[MATH_BIGINTEGER_VALUE], $k); 2282 | 2283 | if ($this->_compare($result, false, $n, false) >= 0) { 2284 | $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], false, $n, false); 2285 | } 2286 | 2287 | return $result[MATH_BIGINTEGER_VALUE]; 2288 | } 2289 | 2290 | /** 2291 | * Montgomery Multiply 2292 | * 2293 | * Interleaves the montgomery reduction and long multiplication algorithms together as described in 2294 | * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36} 2295 | * 2296 | * @see _prepMontgomery() 2297 | * @see _montgomery() 2298 | * @access private 2299 | * @param Array $x 2300 | * @param Array $y 2301 | * @param Array $m 2302 | * @return Array 2303 | */ 2304 | function _montgomeryMultiply($x, $y, $m) 2305 | { 2306 | $temp = $this->_multiply($x, false, $y, false); 2307 | return $this->_montgomery($temp[MATH_BIGINTEGER_VALUE], $m); 2308 | 2309 | static $cache = array( 2310 | MATH_BIGINTEGER_VARIABLE => array(), 2311 | MATH_BIGINTEGER_DATA => array() 2312 | ); 2313 | 2314 | if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { 2315 | $key = count($cache[MATH_BIGINTEGER_VARIABLE]); 2316 | $cache[MATH_BIGINTEGER_VARIABLE][] = $m; 2317 | $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($m); 2318 | } 2319 | 2320 | $n = max(count($x), count($y), count($m)); 2321 | $x = array_pad($x, $n, 0); 2322 | $y = array_pad($y, $n, 0); 2323 | $m = array_pad($m, $n, 0); 2324 | $a = array(MATH_BIGINTEGER_VALUE => $this->_array_repeat(0, $n + 1)); 2325 | for ($i = 0; $i < $n; ++$i) { 2326 | $temp = $a[MATH_BIGINTEGER_VALUE][0] + $x[$i] * $y[0]; 2327 | $temp = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * ((int) ($temp / MATH_BIGINTEGER_BASE_FULL))); 2328 | $temp = $temp * $cache[MATH_BIGINTEGER_DATA][$key]; 2329 | $temp = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * ((int) ($temp / MATH_BIGINTEGER_BASE_FULL))); 2330 | $temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false); 2331 | $a = $this->_add($a[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false); 2332 | $a[MATH_BIGINTEGER_VALUE] = array_slice($a[MATH_BIGINTEGER_VALUE], 1); 2333 | } 2334 | if ($this->_compare($a[MATH_BIGINTEGER_VALUE], false, $m, false) >= 0) { 2335 | $a = $this->_subtract($a[MATH_BIGINTEGER_VALUE], false, $m, false); 2336 | } 2337 | return $a[MATH_BIGINTEGER_VALUE]; 2338 | } 2339 | 2340 | /** 2341 | * Prepare a number for use in Montgomery Modular Reductions 2342 | * 2343 | * @see _montgomery() 2344 | * @see _slidingWindow() 2345 | * @access private 2346 | * @param Array $x 2347 | * @param Array $n 2348 | * @return Array 2349 | */ 2350 | function _prepMontgomery($x, $n) 2351 | { 2352 | $lhs = new Math_BigInteger(); 2353 | $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x); 2354 | $rhs = new Math_BigInteger(); 2355 | $rhs->value = $n; 2356 | 2357 | list(, $temp) = $lhs->divide($rhs); 2358 | return $temp->value; 2359 | } 2360 | 2361 | /** 2362 | * Modular Inverse of a number mod 2**26 (eg. 67108864) 2363 | * 2364 | * Based off of the bnpInvDigit function implemented and justified in the following URL: 2365 | * 2366 | * {@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js} 2367 | * 2368 | * The following URL provides more info: 2369 | * 2370 | * {@link http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85} 2371 | * 2372 | * As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For 2373 | * instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields 2374 | * int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't 2375 | * auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that 2376 | * the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the 2377 | * maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to 2378 | * 40 bits, which only 64-bit floating points will support. 2379 | * 2380 | * Thanks to Pedro Gimeno Fortea for input! 2381 | * 2382 | * @see _montgomery() 2383 | * @access private 2384 | * @param Array $x 2385 | * @return Integer 2386 | */ 2387 | function _modInverse67108864($x) // 2**26 == 67,108,864 2388 | { 2389 | $x = -$x[0]; 2390 | $result = $x & 0x3; // x**-1 mod 2**2 2391 | $result = ($result * (2 - $x * $result)) & 0xF; // x**-1 mod 2**4 2392 | $result = ($result * (2 - ($x & 0xFF) * $result)) & 0xFF; // x**-1 mod 2**8 2393 | $result = ($result * ((2 - ($x & 0xFFFF) * $result) & 0xFFFF)) & 0xFFFF; // x**-1 mod 2**16 2394 | $result = fmod($result * (2 - fmod($x * $result, MATH_BIGINTEGER_BASE_FULL)), MATH_BIGINTEGER_BASE_FULL); // x**-1 mod 2**26 2395 | return $result & MATH_BIGINTEGER_MAX_DIGIT; 2396 | } 2397 | 2398 | /** 2399 | * Calculates modular inverses. 2400 | * 2401 | * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. 2402 | * 2403 | * Here's an example: 2404 | * 2405 | * modInverse($b); 2412 | * echo $c->toString(); // outputs 4 2413 | * 2414 | * echo "\r\n"; 2415 | * 2416 | * $d = $a->multiply($c); 2417 | * list(, $d) = $d->divide($b); 2418 | * echo $d; // outputs 1 (as per the definition of modular inverse) 2419 | * ?> 2420 | * 2421 | * 2422 | * @param Math_BigInteger $n 2423 | * @return mixed false, if no modular inverse exists, Math_BigInteger, otherwise. 2424 | * @access public 2425 | * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information. 2426 | */ 2427 | function modInverse($n) 2428 | { 2429 | switch ( MATH_BIGINTEGER_MODE ) { 2430 | case MATH_BIGINTEGER_MODE_GMP: 2431 | $temp = new Math_BigInteger(); 2432 | $temp->value = gmp_invert($this->value, $n->value); 2433 | 2434 | return ( $temp->value === false ) ? false : $this->_normalize($temp); 2435 | } 2436 | 2437 | static $zero, $one; 2438 | if (!isset($zero)) { 2439 | $zero = new Math_BigInteger(); 2440 | $one = new Math_BigInteger(1); 2441 | } 2442 | 2443 | // $x mod -$n == $x mod $n. 2444 | $n = $n->abs(); 2445 | 2446 | if ($this->compare($zero) < 0) { 2447 | $temp = $this->abs(); 2448 | $temp = $temp->modInverse($n); 2449 | return $this->_normalize($n->subtract($temp)); 2450 | } 2451 | 2452 | extract($this->extendedGCD($n)); 2453 | 2454 | if (!$gcd->equals($one)) { 2455 | return false; 2456 | } 2457 | 2458 | $x = $x->compare($zero) < 0 ? $x->add($n) : $x; 2459 | 2460 | return $this->compare($zero) < 0 ? $this->_normalize($n->subtract($x)) : $this->_normalize($x); 2461 | } 2462 | 2463 | /** 2464 | * Calculates the greatest common divisor and Bezout's identity. 2465 | * 2466 | * Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that 2467 | * 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which 2468 | * combination is returned is dependant upon which mode is in use. See 2469 | * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information. 2470 | * 2471 | * Here's an example: 2472 | * 2473 | * extendedGCD($b)); 2480 | * 2481 | * echo $gcd->toString() . "\r\n"; // outputs 21 2482 | * echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21 2483 | * ?> 2484 | * 2485 | * 2486 | * @param Math_BigInteger $n 2487 | * @return Math_BigInteger 2488 | * @access public 2489 | * @internal Calculates the GCD using the binary xGCD algorithim described in 2490 | * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes, 2491 | * the more traditional algorithim requires "relatively costly multiple-precision divisions". 2492 | */ 2493 | function extendedGCD($n) 2494 | { 2495 | switch ( MATH_BIGINTEGER_MODE ) { 2496 | case MATH_BIGINTEGER_MODE_GMP: 2497 | extract(gmp_gcdext($this->value, $n->value)); 2498 | 2499 | return array( 2500 | 'gcd' => $this->_normalize(new Math_BigInteger($g)), 2501 | 'x' => $this->_normalize(new Math_BigInteger($s)), 2502 | 'y' => $this->_normalize(new Math_BigInteger($t)) 2503 | ); 2504 | case MATH_BIGINTEGER_MODE_BCMATH: 2505 | // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works 2506 | // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is, 2507 | // the basic extended euclidean algorithim is what we're using. 2508 | 2509 | $u = $this->value; 2510 | $v = $n->value; 2511 | 2512 | $a = '1'; 2513 | $b = '0'; 2514 | $c = '0'; 2515 | $d = '1'; 2516 | 2517 | while (bccomp($v, '0', 0) != 0) { 2518 | $q = bcdiv($u, $v, 0); 2519 | 2520 | $temp = $u; 2521 | $u = $v; 2522 | $v = bcsub($temp, bcmul($v, $q, 0), 0); 2523 | 2524 | $temp = $a; 2525 | $a = $c; 2526 | $c = bcsub($temp, bcmul($a, $q, 0), 0); 2527 | 2528 | $temp = $b; 2529 | $b = $d; 2530 | $d = bcsub($temp, bcmul($b, $q, 0), 0); 2531 | } 2532 | 2533 | return array( 2534 | 'gcd' => $this->_normalize(new Math_BigInteger($u)), 2535 | 'x' => $this->_normalize(new Math_BigInteger($a)), 2536 | 'y' => $this->_normalize(new Math_BigInteger($b)) 2537 | ); 2538 | } 2539 | 2540 | $y = $n->copy(); 2541 | $x = $this->copy(); 2542 | $g = new Math_BigInteger(); 2543 | $g->value = array(1); 2544 | 2545 | while ( !(($x->value[0] & 1)|| ($y->value[0] & 1)) ) { 2546 | $x->_rshift(1); 2547 | $y->_rshift(1); 2548 | $g->_lshift(1); 2549 | } 2550 | 2551 | $u = $x->copy(); 2552 | $v = $y->copy(); 2553 | 2554 | $a = new Math_BigInteger(); 2555 | $b = new Math_BigInteger(); 2556 | $c = new Math_BigInteger(); 2557 | $d = new Math_BigInteger(); 2558 | 2559 | $a->value = $d->value = $g->value = array(1); 2560 | $b->value = $c->value = array(); 2561 | 2562 | while ( !empty($u->value) ) { 2563 | while ( !($u->value[0] & 1) ) { 2564 | $u->_rshift(1); 2565 | if ( (!empty($a->value) && ($a->value[0] & 1)) || (!empty($b->value) && ($b->value[0] & 1)) ) { 2566 | $a = $a->add($y); 2567 | $b = $b->subtract($x); 2568 | } 2569 | $a->_rshift(1); 2570 | $b->_rshift(1); 2571 | } 2572 | 2573 | while ( !($v->value[0] & 1) ) { 2574 | $v->_rshift(1); 2575 | if ( (!empty($d->value) && ($d->value[0] & 1)) || (!empty($c->value) && ($c->value[0] & 1)) ) { 2576 | $c = $c->add($y); 2577 | $d = $d->subtract($x); 2578 | } 2579 | $c->_rshift(1); 2580 | $d->_rshift(1); 2581 | } 2582 | 2583 | if ($u->compare($v) >= 0) { 2584 | $u = $u->subtract($v); 2585 | $a = $a->subtract($c); 2586 | $b = $b->subtract($d); 2587 | } else { 2588 | $v = $v->subtract($u); 2589 | $c = $c->subtract($a); 2590 | $d = $d->subtract($b); 2591 | } 2592 | } 2593 | 2594 | return array( 2595 | 'gcd' => $this->_normalize($g->multiply($v)), 2596 | 'x' => $this->_normalize($c), 2597 | 'y' => $this->_normalize($d) 2598 | ); 2599 | } 2600 | 2601 | /** 2602 | * Calculates the greatest common divisor 2603 | * 2604 | * Say you have 693 and 609. The GCD is 21. 2605 | * 2606 | * Here's an example: 2607 | * 2608 | * extendedGCD($b); 2615 | * 2616 | * echo $gcd->toString() . "\r\n"; // outputs 21 2617 | * ?> 2618 | * 2619 | * 2620 | * @param Math_BigInteger $n 2621 | * @return Math_BigInteger 2622 | * @access public 2623 | */ 2624 | function gcd($n) 2625 | { 2626 | extract($this->extendedGCD($n)); 2627 | return $gcd; 2628 | } 2629 | 2630 | /** 2631 | * Absolute value. 2632 | * 2633 | * @return Math_BigInteger 2634 | * @access public 2635 | */ 2636 | function abs() 2637 | { 2638 | $temp = new Math_BigInteger(); 2639 | 2640 | switch ( MATH_BIGINTEGER_MODE ) { 2641 | case MATH_BIGINTEGER_MODE_GMP: 2642 | $temp->value = gmp_abs($this->value); 2643 | break; 2644 | case MATH_BIGINTEGER_MODE_BCMATH: 2645 | $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value; 2646 | break; 2647 | default: 2648 | $temp->value = $this->value; 2649 | } 2650 | 2651 | return $temp; 2652 | } 2653 | 2654 | /** 2655 | * Compares two numbers. 2656 | * 2657 | * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is 2658 | * demonstrated thusly: 2659 | * 2660 | * $x > $y: $x->compare($y) > 0 2661 | * $x < $y: $x->compare($y) < 0 2662 | * $x == $y: $x->compare($y) == 0 2663 | * 2664 | * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). 2665 | * 2666 | * @param Math_BigInteger $y 2667 | * @return Integer < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal. 2668 | * @access public 2669 | * @see equals() 2670 | * @internal Could return $this->subtract($x), but that's not as fast as what we do do. 2671 | */ 2672 | function compare($y) 2673 | { 2674 | switch ( MATH_BIGINTEGER_MODE ) { 2675 | case MATH_BIGINTEGER_MODE_GMP: 2676 | return gmp_cmp($this->value, $y->value); 2677 | case MATH_BIGINTEGER_MODE_BCMATH: 2678 | return bccomp($this->value, $y->value, 0); 2679 | } 2680 | 2681 | return $this->_compare($this->value, $this->is_negative, $y->value, $y->is_negative); 2682 | } 2683 | 2684 | /** 2685 | * Compares two numbers. 2686 | * 2687 | * @param Array $x_value 2688 | * @param Boolean $x_negative 2689 | * @param Array $y_value 2690 | * @param Boolean $y_negative 2691 | * @return Integer 2692 | * @see compare() 2693 | * @access private 2694 | */ 2695 | function _compare($x_value, $x_negative, $y_value, $y_negative) 2696 | { 2697 | if ( $x_negative != $y_negative ) { 2698 | return ( !$x_negative && $y_negative ) ? 1 : -1; 2699 | } 2700 | 2701 | $result = $x_negative ? -1 : 1; 2702 | 2703 | if ( count($x_value) != count($y_value) ) { 2704 | return ( count($x_value) > count($y_value) ) ? $result : -$result; 2705 | } 2706 | $size = max(count($x_value), count($y_value)); 2707 | 2708 | $x_value = array_pad($x_value, $size, 0); 2709 | $y_value = array_pad($y_value, $size, 0); 2710 | 2711 | for ($i = count($x_value) - 1; $i >= 0; --$i) { 2712 | if ($x_value[$i] != $y_value[$i]) { 2713 | return ( $x_value[$i] > $y_value[$i] ) ? $result : -$result; 2714 | } 2715 | } 2716 | 2717 | return 0; 2718 | } 2719 | 2720 | /** 2721 | * Tests the equality of two numbers. 2722 | * 2723 | * If you need to see if one number is greater than or less than another number, use Math_BigInteger::compare() 2724 | * 2725 | * @param Math_BigInteger $x 2726 | * @return Boolean 2727 | * @access public 2728 | * @see compare() 2729 | */ 2730 | function equals($x) 2731 | { 2732 | switch ( MATH_BIGINTEGER_MODE ) { 2733 | case MATH_BIGINTEGER_MODE_GMP: 2734 | return gmp_cmp($this->value, $x->value) == 0; 2735 | default: 2736 | return $this->value === $x->value && $this->is_negative == $x->is_negative; 2737 | } 2738 | } 2739 | 2740 | /** 2741 | * Set Precision 2742 | * 2743 | * Some bitwise operations give different results depending on the precision being used. Examples include left 2744 | * shift, not, and rotates. 2745 | * 2746 | * @param Integer $bits 2747 | * @access public 2748 | */ 2749 | function setPrecision($bits) 2750 | { 2751 | $this->precision = $bits; 2752 | if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ) { 2753 | $this->bitmask = new Math_BigInteger(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256); 2754 | } else { 2755 | $this->bitmask = new Math_BigInteger(bcpow('2', $bits, 0)); 2756 | } 2757 | 2758 | $temp = $this->_normalize($this); 2759 | $this->value = $temp->value; 2760 | } 2761 | 2762 | /** 2763 | * Logical And 2764 | * 2765 | * @param Math_BigInteger $x 2766 | * @access public 2767 | * @internal Implemented per a request by Lluis Pamies i Juarez 2768 | * @return Math_BigInteger 2769 | */ 2770 | function bitwise_and($x) 2771 | { 2772 | switch ( MATH_BIGINTEGER_MODE ) { 2773 | case MATH_BIGINTEGER_MODE_GMP: 2774 | $temp = new Math_BigInteger(); 2775 | $temp->value = gmp_and($this->value, $x->value); 2776 | 2777 | return $this->_normalize($temp); 2778 | case MATH_BIGINTEGER_MODE_BCMATH: 2779 | $left = $this->toBytes(); 2780 | $right = $x->toBytes(); 2781 | 2782 | $length = max(strlen($left), strlen($right)); 2783 | 2784 | $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); 2785 | $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); 2786 | 2787 | return $this->_normalize(new Math_BigInteger($left & $right, 256)); 2788 | } 2789 | 2790 | $result = $this->copy(); 2791 | 2792 | $length = min(count($x->value), count($this->value)); 2793 | 2794 | $result->value = array_slice($result->value, 0, $length); 2795 | 2796 | for ($i = 0; $i < $length; ++$i) { 2797 | $result->value[$i]&= $x->value[$i]; 2798 | } 2799 | 2800 | return $this->_normalize($result); 2801 | } 2802 | 2803 | /** 2804 | * Logical Or 2805 | * 2806 | * @param Math_BigInteger $x 2807 | * @access public 2808 | * @internal Implemented per a request by Lluis Pamies i Juarez 2809 | * @return Math_BigInteger 2810 | */ 2811 | function bitwise_or($x) 2812 | { 2813 | switch ( MATH_BIGINTEGER_MODE ) { 2814 | case MATH_BIGINTEGER_MODE_GMP: 2815 | $temp = new Math_BigInteger(); 2816 | $temp->value = gmp_or($this->value, $x->value); 2817 | 2818 | return $this->_normalize($temp); 2819 | case MATH_BIGINTEGER_MODE_BCMATH: 2820 | $left = $this->toBytes(); 2821 | $right = $x->toBytes(); 2822 | 2823 | $length = max(strlen($left), strlen($right)); 2824 | 2825 | $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); 2826 | $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); 2827 | 2828 | return $this->_normalize(new Math_BigInteger($left | $right, 256)); 2829 | } 2830 | 2831 | $length = max(count($this->value), count($x->value)); 2832 | $result = $this->copy(); 2833 | $result->value = array_pad($result->value, $length, 0); 2834 | $x->value = array_pad($x->value, $length, 0); 2835 | 2836 | for ($i = 0; $i < $length; ++$i) { 2837 | $result->value[$i]|= $x->value[$i]; 2838 | } 2839 | 2840 | return $this->_normalize($result); 2841 | } 2842 | 2843 | /** 2844 | * Logical Exclusive-Or 2845 | * 2846 | * @param Math_BigInteger $x 2847 | * @access public 2848 | * @internal Implemented per a request by Lluis Pamies i Juarez 2849 | * @return Math_BigInteger 2850 | */ 2851 | function bitwise_xor($x) 2852 | { 2853 | switch ( MATH_BIGINTEGER_MODE ) { 2854 | case MATH_BIGINTEGER_MODE_GMP: 2855 | $temp = new Math_BigInteger(); 2856 | $temp->value = gmp_xor($this->value, $x->value); 2857 | 2858 | return $this->_normalize($temp); 2859 | case MATH_BIGINTEGER_MODE_BCMATH: 2860 | $left = $this->toBytes(); 2861 | $right = $x->toBytes(); 2862 | 2863 | $length = max(strlen($left), strlen($right)); 2864 | 2865 | $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); 2866 | $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); 2867 | 2868 | return $this->_normalize(new Math_BigInteger($left ^ $right, 256)); 2869 | } 2870 | 2871 | $length = max(count($this->value), count($x->value)); 2872 | $result = $this->copy(); 2873 | $result->value = array_pad($result->value, $length, 0); 2874 | $x->value = array_pad($x->value, $length, 0); 2875 | 2876 | for ($i = 0; $i < $length; ++$i) { 2877 | $result->value[$i]^= $x->value[$i]; 2878 | } 2879 | 2880 | return $this->_normalize($result); 2881 | } 2882 | 2883 | /** 2884 | * Logical Not 2885 | * 2886 | * @access public 2887 | * @internal Implemented per a request by Lluis Pamies i Juarez 2888 | * @return Math_BigInteger 2889 | */ 2890 | function bitwise_not() 2891 | { 2892 | // calculuate "not" without regard to $this->precision 2893 | // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0) 2894 | $temp = $this->toBytes(); 2895 | $pre_msb = decbin(ord($temp[0])); 2896 | $temp = ~$temp; 2897 | $msb = decbin(ord($temp[0])); 2898 | if (strlen($msb) == 8) { 2899 | $msb = substr($msb, strpos($msb, '0')); 2900 | } 2901 | $temp[0] = chr(bindec($msb)); 2902 | 2903 | // see if we need to add extra leading 1's 2904 | $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8; 2905 | $new_bits = $this->precision - $current_bits; 2906 | if ($new_bits <= 0) { 2907 | return $this->_normalize(new Math_BigInteger($temp, 256)); 2908 | } 2909 | 2910 | // generate as many leading 1's as we need to. 2911 | $leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3); 2912 | $this->_base256_lshift($leading_ones, $current_bits); 2913 | 2914 | $temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT); 2915 | 2916 | return $this->_normalize(new Math_BigInteger($leading_ones | $temp, 256)); 2917 | } 2918 | 2919 | /** 2920 | * Logical Right Shift 2921 | * 2922 | * Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift. 2923 | * 2924 | * @param Integer $shift 2925 | * @return Math_BigInteger 2926 | * @access public 2927 | * @internal The only version that yields any speed increases is the internal version. 2928 | */ 2929 | function bitwise_rightShift($shift) 2930 | { 2931 | $temp = new Math_BigInteger(); 2932 | 2933 | switch ( MATH_BIGINTEGER_MODE ) { 2934 | case MATH_BIGINTEGER_MODE_GMP: 2935 | static $two; 2936 | 2937 | if (!isset($two)) { 2938 | $two = gmp_init('2'); 2939 | } 2940 | 2941 | $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift)); 2942 | 2943 | break; 2944 | case MATH_BIGINTEGER_MODE_BCMATH: 2945 | $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0); 2946 | 2947 | break; 2948 | default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten 2949 | // and I don't want to do that... 2950 | $temp->value = $this->value; 2951 | $temp->_rshift($shift); 2952 | } 2953 | 2954 | return $this->_normalize($temp); 2955 | } 2956 | 2957 | /** 2958 | * Logical Left Shift 2959 | * 2960 | * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. 2961 | * 2962 | * @param Integer $shift 2963 | * @return Math_BigInteger 2964 | * @access public 2965 | * @internal The only version that yields any speed increases is the internal version. 2966 | */ 2967 | function bitwise_leftShift($shift) 2968 | { 2969 | $temp = new Math_BigInteger(); 2970 | 2971 | switch ( MATH_BIGINTEGER_MODE ) { 2972 | case MATH_BIGINTEGER_MODE_GMP: 2973 | static $two; 2974 | 2975 | if (!isset($two)) { 2976 | $two = gmp_init('2'); 2977 | } 2978 | 2979 | $temp->value = gmp_mul($this->value, gmp_pow($two, $shift)); 2980 | 2981 | break; 2982 | case MATH_BIGINTEGER_MODE_BCMATH: 2983 | $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0); 2984 | 2985 | break; 2986 | default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten 2987 | // and I don't want to do that... 2988 | $temp->value = $this->value; 2989 | $temp->_lshift($shift); 2990 | } 2991 | 2992 | return $this->_normalize($temp); 2993 | } 2994 | 2995 | /** 2996 | * Logical Left Rotate 2997 | * 2998 | * Instead of the top x bits being dropped they're appended to the shifted bit string. 2999 | * 3000 | * @param Integer $shift 3001 | * @return Math_BigInteger 3002 | * @access public 3003 | */ 3004 | function bitwise_leftRotate($shift) 3005 | { 3006 | $bits = $this->toBytes(); 3007 | 3008 | if ($this->precision > 0) { 3009 | $precision = $this->precision; 3010 | if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { 3011 | $mask = $this->bitmask->subtract(new Math_BigInteger(1)); 3012 | $mask = $mask->toBytes(); 3013 | } else { 3014 | $mask = $this->bitmask->toBytes(); 3015 | } 3016 | } else { 3017 | $temp = ord($bits[0]); 3018 | for ($i = 0; $temp >> $i; ++$i); 3019 | $precision = 8 * strlen($bits) - 8 + $i; 3020 | $mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3); 3021 | } 3022 | 3023 | if ($shift < 0) { 3024 | $shift+= $precision; 3025 | } 3026 | $shift%= $precision; 3027 | 3028 | if (!$shift) { 3029 | return $this->copy(); 3030 | } 3031 | 3032 | $left = $this->bitwise_leftShift($shift); 3033 | $left = $left->bitwise_and(new Math_BigInteger($mask, 256)); 3034 | $right = $this->bitwise_rightShift($precision - $shift); 3035 | $result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right); 3036 | return $this->_normalize($result); 3037 | } 3038 | 3039 | /** 3040 | * Logical Right Rotate 3041 | * 3042 | * Instead of the bottom x bits being dropped they're prepended to the shifted bit string. 3043 | * 3044 | * @param Integer $shift 3045 | * @return Math_BigInteger 3046 | * @access public 3047 | */ 3048 | function bitwise_rightRotate($shift) 3049 | { 3050 | return $this->bitwise_leftRotate(-$shift); 3051 | } 3052 | 3053 | /** 3054 | * Set random number generator function 3055 | * 3056 | * This function is deprecated. 3057 | * 3058 | * @param String $generator 3059 | * @access public 3060 | */ 3061 | function setRandomGenerator($generator) 3062 | { 3063 | } 3064 | 3065 | /** 3066 | * Generates a random BigInteger 3067 | * 3068 | * Byte length is equal to $length. Uses crypt_random if it's loaded and mt_rand if it's not. 3069 | * 3070 | * @param Integer $length 3071 | * @return Math_BigInteger 3072 | * @access private 3073 | */ 3074 | function _random_number_helper($size) 3075 | { 3076 | $crypt_random = function_exists('crypt_random_string') || (!class_exists('Crypt_Random') && function_exists('crypt_random_string')); 3077 | if ($crypt_random) { 3078 | $random = crypt_random_string($size); 3079 | } else { 3080 | $random = ''; 3081 | 3082 | if ($size & 1) { 3083 | $random.= chr(mt_rand(0, 255)); 3084 | } 3085 | 3086 | $blocks = $size >> 1; 3087 | for ($i = 0; $i < $blocks; ++$i) { 3088 | // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems 3089 | $random.= pack('n', mt_rand(0, 0xFFFF)); 3090 | } 3091 | } 3092 | 3093 | return new Math_BigInteger($random, 256); 3094 | } 3095 | 3096 | /** 3097 | * Generate a random number 3098 | * 3099 | * @param optional Integer $min 3100 | * @param optional Integer $max 3101 | * @return Math_BigInteger 3102 | * @access public 3103 | */ 3104 | function random($min = false, $max = false) 3105 | { 3106 | if ($min === false) { 3107 | $min = new Math_BigInteger(0); 3108 | } 3109 | 3110 | if ($max === false) { 3111 | $max = new Math_BigInteger(0x7FFFFFFF); 3112 | } 3113 | 3114 | $compare = $max->compare($min); 3115 | 3116 | if (!$compare) { 3117 | return $this->_normalize($min); 3118 | } else if ($compare < 0) { 3119 | // if $min is bigger then $max, swap $min and $max 3120 | $temp = $max; 3121 | $max = $min; 3122 | $min = $temp; 3123 | } 3124 | 3125 | static $one; 3126 | if (!isset($one)) { 3127 | $one = new Math_BigInteger(1); 3128 | } 3129 | 3130 | $max = $max->subtract($min->subtract($one)); 3131 | $size = strlen(ltrim($max->toBytes(), chr(0))); 3132 | 3133 | /* 3134 | doing $random % $max doesn't work because some numbers will be more likely to occur than others. 3135 | eg. if $max is 140 and $random's max is 255 then that'd mean both $random = 5 and $random = 145 3136 | would produce 5 whereas the only value of random that could produce 139 would be 139. ie. 3137 | not all numbers would be equally likely. some would be more likely than others. 3138 | 3139 | creating a whole new random number until you find one that is within the range doesn't work 3140 | because, for sufficiently small ranges, the likelihood that you'd get a number within that range 3141 | would be pretty small. eg. with $random's max being 255 and if your $max being 1 the probability 3142 | would be pretty high that $random would be greater than $max. 3143 | 3144 | phpseclib works around this using the technique described here: 3145 | 3146 | http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string 3147 | */ 3148 | $random_max = new Math_BigInteger(chr(1) . str_repeat("\0", $size), 256); 3149 | $random = $this->_random_number_helper($size); 3150 | 3151 | list($max_multiple) = $random_max->divide($max); 3152 | $max_multiple = $max_multiple->multiply($max); 3153 | 3154 | while ($random->compare($max_multiple) >= 0) { 3155 | $random = $random->subtract($max_multiple); 3156 | $random_max = $random_max->subtract($max_multiple); 3157 | $random = $random->bitwise_leftShift(8); 3158 | $random = $random->add($this->_random_number_helper(1)); 3159 | $random_max = $random_max->bitwise_leftShift(8); 3160 | list($max_multiple) = $random_max->divide($max); 3161 | $max_multiple = $max_multiple->multiply($max); 3162 | } 3163 | list(, $random) = $random->divide($max); 3164 | 3165 | return $this->_normalize($random->add($min)); 3166 | } 3167 | 3168 | /** 3169 | * Generate a random prime number. 3170 | * 3171 | * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, 3172 | * give up and return false. 3173 | * 3174 | * @param optional Integer $min 3175 | * @param optional Integer $max 3176 | * @param optional Integer $timeout 3177 | * @return Math_BigInteger 3178 | * @access public 3179 | * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. 3180 | */ 3181 | function randomPrime($min = false, $max = false, $timeout = false) 3182 | { 3183 | if ($min === false) { 3184 | $min = new Math_BigInteger(0); 3185 | } 3186 | 3187 | if ($max === false) { 3188 | $max = new Math_BigInteger(0x7FFFFFFF); 3189 | } 3190 | 3191 | $compare = $max->compare($min); 3192 | 3193 | if (!$compare) { 3194 | return $min->isPrime() ? $min : false; 3195 | } else if ($compare < 0) { 3196 | // if $min is bigger then $max, swap $min and $max 3197 | $temp = $max; 3198 | $max = $min; 3199 | $min = $temp; 3200 | } 3201 | 3202 | static $one, $two; 3203 | if (!isset($one)) { 3204 | $one = new Math_BigInteger(1); 3205 | $two = new Math_BigInteger(2); 3206 | } 3207 | 3208 | $start = time(); 3209 | 3210 | $x = $this->random($min, $max); 3211 | 3212 | // gmp_nextprime() requires PHP 5 >= 5.2.0 per . 3213 | if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime') ) { 3214 | $p = new Math_BigInteger(); 3215 | $p->value = gmp_nextprime($x->value); 3216 | 3217 | if ($p->compare($max) <= 0) { 3218 | return $p; 3219 | } 3220 | 3221 | if (!$min->equals($x)) { 3222 | $x = $x->subtract($one); 3223 | } 3224 | 3225 | return $x->randomPrime($min, $x); 3226 | } 3227 | 3228 | if ($x->equals($two)) { 3229 | return $x; 3230 | } 3231 | 3232 | $x->_make_odd(); 3233 | if ($x->compare($max) > 0) { 3234 | // if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range 3235 | if ($min->equals($max)) { 3236 | return false; 3237 | } 3238 | $x = $min->copy(); 3239 | $x->_make_odd(); 3240 | } 3241 | 3242 | $initial_x = $x->copy(); 3243 | 3244 | while (true) { 3245 | if ($timeout !== false && time() - $start > $timeout) { 3246 | return false; 3247 | } 3248 | 3249 | if ($x->isPrime()) { 3250 | return $x; 3251 | } 3252 | 3253 | $x = $x->add($two); 3254 | 3255 | if ($x->compare($max) > 0) { 3256 | $x = $min->copy(); 3257 | if ($x->equals($two)) { 3258 | return $x; 3259 | } 3260 | $x->_make_odd(); 3261 | } 3262 | 3263 | if ($x->equals($initial_x)) { 3264 | return false; 3265 | } 3266 | } 3267 | } 3268 | 3269 | /** 3270 | * Make the current number odd 3271 | * 3272 | * If the current number is odd it'll be unchanged. If it's even, one will be added to it. 3273 | * 3274 | * @see randomPrime() 3275 | * @access private 3276 | */ 3277 | function _make_odd() 3278 | { 3279 | switch ( MATH_BIGINTEGER_MODE ) { 3280 | case MATH_BIGINTEGER_MODE_GMP: 3281 | gmp_setbit($this->value, 0); 3282 | break; 3283 | case MATH_BIGINTEGER_MODE_BCMATH: 3284 | if ($this->value[strlen($this->value) - 1] % 2 == 0) { 3285 | $this->value = bcadd($this->value, '1'); 3286 | } 3287 | break; 3288 | default: 3289 | $this->value[0] |= 1; 3290 | } 3291 | } 3292 | 3293 | /** 3294 | * Checks a numer to see if it's prime 3295 | * 3296 | * Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the 3297 | * $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed accross multiple pageloads 3298 | * on a website instead of just one. 3299 | * 3300 | * @param optional Integer $t 3301 | * @return Boolean 3302 | * @access public 3303 | * @internal Uses the 3304 | * {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See 3305 | * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}. 3306 | */ 3307 | function isPrime($t = false) 3308 | { 3309 | $length = strlen($this->toBytes()); 3310 | 3311 | if (!$t) { 3312 | // see HAC 4.49 "Note (controlling the error probability)" 3313 | // @codingStandardsIgnoreStart 3314 | if ($length >= 163) { $t = 2; } // floor(1300 / 8) 3315 | else if ($length >= 106) { $t = 3; } // floor( 850 / 8) 3316 | else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8) 3317 | else if ($length >= 68 ) { $t = 5; } // floor( 550 / 8) 3318 | else if ($length >= 56 ) { $t = 6; } // floor( 450 / 8) 3319 | else if ($length >= 50 ) { $t = 7; } // floor( 400 / 8) 3320 | else if ($length >= 43 ) { $t = 8; } // floor( 350 / 8) 3321 | else if ($length >= 37 ) { $t = 9; } // floor( 300 / 8) 3322 | else if ($length >= 31 ) { $t = 12; } // floor( 250 / 8) 3323 | else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8) 3324 | else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8) 3325 | else { $t = 27; } 3326 | // @codingStandardsIgnoreEnd 3327 | } 3328 | 3329 | // ie. gmp_testbit($this, 0) 3330 | // ie. isEven() or !isOdd() 3331 | switch ( MATH_BIGINTEGER_MODE ) { 3332 | case MATH_BIGINTEGER_MODE_GMP: 3333 | return gmp_prob_prime($this->value, $t) != 0; 3334 | case MATH_BIGINTEGER_MODE_BCMATH: 3335 | if ($this->value === '2') { 3336 | return true; 3337 | } 3338 | if ($this->value[strlen($this->value) - 1] % 2 == 0) { 3339 | return false; 3340 | } 3341 | break; 3342 | default: 3343 | if ($this->value == array(2)) { 3344 | return true; 3345 | } 3346 | if (~$this->value[0] & 1) { 3347 | return false; 3348 | } 3349 | } 3350 | 3351 | static $primes, $zero, $one, $two; 3352 | 3353 | if (!isset($primes)) { 3354 | $primes = array( 3355 | 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 3356 | 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 3357 | 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 3358 | 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 3359 | 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 3360 | 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 3361 | 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 3362 | 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 3363 | 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 3364 | 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 3365 | 953, 967, 971, 977, 983, 991, 997 3366 | ); 3367 | 3368 | if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { 3369 | for ($i = 0; $i < count($primes); ++$i) { 3370 | $primes[$i] = new Math_BigInteger($primes[$i]); 3371 | } 3372 | } 3373 | 3374 | $zero = new Math_BigInteger(); 3375 | $one = new Math_BigInteger(1); 3376 | $two = new Math_BigInteger(2); 3377 | } 3378 | 3379 | if ($this->equals($one)) { 3380 | return false; 3381 | } 3382 | 3383 | // see HAC 4.4.1 "Random search for probable primes" 3384 | if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { 3385 | foreach ($primes as $prime) { 3386 | list(, $r) = $this->divide($prime); 3387 | if ($r->equals($zero)) { 3388 | return $this->equals($prime); 3389 | } 3390 | } 3391 | } else { 3392 | $value = $this->value; 3393 | foreach ($primes as $prime) { 3394 | list(, $r) = $this->_divide_digit($value, $prime); 3395 | if (!$r) { 3396 | return count($value) == 1 && $value[0] == $prime; 3397 | } 3398 | } 3399 | } 3400 | 3401 | $n = $this->copy(); 3402 | $n_1 = $n->subtract($one); 3403 | $n_2 = $n->subtract($two); 3404 | 3405 | $r = $n_1->copy(); 3406 | $r_value = $r->value; 3407 | // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s)); 3408 | if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { 3409 | $s = 0; 3410 | // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier 3411 | while ($r->value[strlen($r->value) - 1] % 2 == 0) { 3412 | $r->value = bcdiv($r->value, '2', 0); 3413 | ++$s; 3414 | } 3415 | } else { 3416 | for ($i = 0, $r_length = count($r_value); $i < $r_length; ++$i) { 3417 | $temp = ~$r_value[$i] & 0xFFFFFF; 3418 | for ($j = 1; ($temp >> $j) & 1; ++$j); 3419 | if ($j != 25) { 3420 | break; 3421 | } 3422 | } 3423 | $s = 26 * $i + $j - 1; 3424 | $r->_rshift($s); 3425 | } 3426 | 3427 | for ($i = 0; $i < $t; ++$i) { 3428 | $a = $this->random($two, $n_2); 3429 | $y = $a->modPow($r, $n); 3430 | 3431 | if (!$y->equals($one) && !$y->equals($n_1)) { 3432 | for ($j = 1; $j < $s && !$y->equals($n_1); ++$j) { 3433 | $y = $y->modPow($two, $n); 3434 | if ($y->equals($one)) { 3435 | return false; 3436 | } 3437 | } 3438 | 3439 | if (!$y->equals($n_1)) { 3440 | return false; 3441 | } 3442 | } 3443 | } 3444 | return true; 3445 | } 3446 | 3447 | /** 3448 | * Logical Left Shift 3449 | * 3450 | * Shifts BigInteger's by $shift bits. 3451 | * 3452 | * @param Integer $shift 3453 | * @access private 3454 | */ 3455 | function _lshift($shift) 3456 | { 3457 | if ( $shift == 0 ) { 3458 | return; 3459 | } 3460 | 3461 | $num_digits = (int) ($shift / MATH_BIGINTEGER_BASE); 3462 | $shift %= MATH_BIGINTEGER_BASE; 3463 | $shift = 1 << $shift; 3464 | 3465 | $carry = 0; 3466 | 3467 | for ($i = 0; $i < count($this->value); ++$i) { 3468 | $temp = $this->value[$i] * $shift + $carry; 3469 | $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL); 3470 | $this->value[$i] = (int) ($temp - $carry * MATH_BIGINTEGER_BASE_FULL); 3471 | } 3472 | 3473 | if ( $carry ) { 3474 | $this->value[] = $carry; 3475 | } 3476 | 3477 | while ($num_digits--) { 3478 | array_unshift($this->value, 0); 3479 | } 3480 | } 3481 | 3482 | /** 3483 | * Logical Right Shift 3484 | * 3485 | * Shifts BigInteger's by $shift bits. 3486 | * 3487 | * @param Integer $shift 3488 | * @access private 3489 | */ 3490 | function _rshift($shift) 3491 | { 3492 | if ($shift == 0) { 3493 | return; 3494 | } 3495 | 3496 | $num_digits = (int) ($shift / MATH_BIGINTEGER_BASE); 3497 | $shift %= MATH_BIGINTEGER_BASE; 3498 | $carry_shift = MATH_BIGINTEGER_BASE - $shift; 3499 | $carry_mask = (1 << $shift) - 1; 3500 | 3501 | if ( $num_digits ) { 3502 | $this->value = array_slice($this->value, $num_digits); 3503 | } 3504 | 3505 | $carry = 0; 3506 | 3507 | for ($i = count($this->value) - 1; $i >= 0; --$i) { 3508 | $temp = $this->value[$i] >> $shift | $carry; 3509 | $carry = ($this->value[$i] & $carry_mask) << $carry_shift; 3510 | $this->value[$i] = $temp; 3511 | } 3512 | 3513 | $this->value = $this->_trim($this->value); 3514 | } 3515 | 3516 | /** 3517 | * Normalize 3518 | * 3519 | * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision 3520 | * 3521 | * @param Math_BigInteger 3522 | * @return Math_BigInteger 3523 | * @see _trim() 3524 | * @access private 3525 | */ 3526 | function _normalize($result) 3527 | { 3528 | $result->precision = $this->precision; 3529 | $result->bitmask = $this->bitmask; 3530 | 3531 | switch ( MATH_BIGINTEGER_MODE ) { 3532 | case MATH_BIGINTEGER_MODE_GMP: 3533 | if (!empty($result->bitmask->value)) { 3534 | $result->value = gmp_and($result->value, $result->bitmask->value); 3535 | } 3536 | 3537 | return $result; 3538 | case MATH_BIGINTEGER_MODE_BCMATH: 3539 | if (!empty($result->bitmask->value)) { 3540 | $result->value = bcmod($result->value, $result->bitmask->value); 3541 | } 3542 | 3543 | return $result; 3544 | } 3545 | 3546 | $value = &$result->value; 3547 | 3548 | if ( !count($value) ) { 3549 | return $result; 3550 | } 3551 | 3552 | $value = $this->_trim($value); 3553 | 3554 | if (!empty($result->bitmask->value)) { 3555 | $length = min(count($value), count($this->bitmask->value)); 3556 | $value = array_slice($value, 0, $length); 3557 | 3558 | for ($i = 0; $i < $length; ++$i) { 3559 | $value[$i] = $value[$i] & $this->bitmask->value[$i]; 3560 | } 3561 | } 3562 | 3563 | return $result; 3564 | } 3565 | 3566 | /** 3567 | * Trim 3568 | * 3569 | * Removes leading zeros 3570 | * 3571 | * @param Array $value 3572 | * @return Math_BigInteger 3573 | * @access private 3574 | */ 3575 | function _trim($value) 3576 | { 3577 | for ($i = count($value) - 1; $i >= 0; --$i) { 3578 | if ( $value[$i] ) { 3579 | break; 3580 | } 3581 | unset($value[$i]); 3582 | } 3583 | 3584 | return $value; 3585 | } 3586 | 3587 | /** 3588 | * Array Repeat 3589 | * 3590 | * @param $input Array 3591 | * @param $multiplier mixed 3592 | * @return Array 3593 | * @access private 3594 | */ 3595 | function _array_repeat($input, $multiplier) 3596 | { 3597 | return ($multiplier) ? array_fill(0, $multiplier, $input) : array(); 3598 | } 3599 | 3600 | /** 3601 | * Logical Left Shift 3602 | * 3603 | * Shifts binary strings $shift bits, essentially multiplying by 2**$shift. 3604 | * 3605 | * @param $x String 3606 | * @param $shift Integer 3607 | * @return String 3608 | * @access private 3609 | */ 3610 | function _base256_lshift(&$x, $shift) 3611 | { 3612 | if ($shift == 0) { 3613 | return; 3614 | } 3615 | 3616 | $num_bytes = $shift >> 3; // eg. floor($shift/8) 3617 | $shift &= 7; // eg. $shift % 8 3618 | 3619 | $carry = 0; 3620 | for ($i = strlen($x) - 1; $i >= 0; --$i) { 3621 | $temp = ord($x[$i]) << $shift | $carry; 3622 | $x[$i] = chr($temp); 3623 | $carry = $temp >> 8; 3624 | } 3625 | $carry = ($carry != 0) ? chr($carry) : ''; 3626 | $x = $carry . $x . str_repeat(chr(0), $num_bytes); 3627 | } 3628 | 3629 | /** 3630 | * Logical Right Shift 3631 | * 3632 | * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder. 3633 | * 3634 | * @param $x String 3635 | * @param $shift Integer 3636 | * @return String 3637 | * @access private 3638 | */ 3639 | function _base256_rshift(&$x, $shift) 3640 | { 3641 | if ($shift == 0) { 3642 | $x = ltrim($x, chr(0)); 3643 | return ''; 3644 | } 3645 | 3646 | $num_bytes = $shift >> 3; // eg. floor($shift/8) 3647 | $shift &= 7; // eg. $shift % 8 3648 | 3649 | $remainder = ''; 3650 | if ($num_bytes) { 3651 | $start = $num_bytes > strlen($x) ? -strlen($x) : -$num_bytes; 3652 | $remainder = substr($x, $start); 3653 | $x = substr($x, 0, -$num_bytes); 3654 | } 3655 | 3656 | $carry = 0; 3657 | $carry_shift = 8 - $shift; 3658 | for ($i = 0; $i < strlen($x); ++$i) { 3659 | $temp = (ord($x[$i]) >> $shift) | $carry; 3660 | $carry = (ord($x[$i]) << $carry_shift) & 0xFF; 3661 | $x[$i] = chr($temp); 3662 | } 3663 | $x = ltrim($x, chr(0)); 3664 | 3665 | $remainder = chr($carry >> $carry_shift) . $remainder; 3666 | 3667 | return ltrim($remainder, chr(0)); 3668 | } 3669 | 3670 | // one quirk about how the following functions are implemented is that PHP defines N to be an unsigned long 3671 | // at 32-bits, while java's longs are 64-bits. 3672 | 3673 | /** 3674 | * Converts 32-bit integers to bytes. 3675 | * 3676 | * @param Integer $x 3677 | * @return String 3678 | * @access private 3679 | */ 3680 | function _int2bytes($x) 3681 | { 3682 | return ltrim(pack('N', $x), chr(0)); 3683 | } 3684 | 3685 | /** 3686 | * Converts bytes to 32-bit integers 3687 | * 3688 | * @param String $x 3689 | * @return Integer 3690 | * @access private 3691 | */ 3692 | function _bytes2int($x) 3693 | { 3694 | $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT)); 3695 | return $temp['int']; 3696 | } 3697 | 3698 | /** 3699 | * DER-encode an integer 3700 | * 3701 | * The ability to DER-encode integers is needed to create RSA public keys for use with OpenSSL 3702 | * 3703 | * @see modPow() 3704 | * @access private 3705 | * @param Integer $length 3706 | * @return String 3707 | */ 3708 | function _encodeASN1Length($length) 3709 | { 3710 | if ($length <= 0x7F) { 3711 | return chr($length); 3712 | } 3713 | 3714 | $temp = ltrim(pack('N', $length), chr(0)); 3715 | return pack('Ca*', 0x80 | strlen($temp), $temp); 3716 | } 3717 | } --------------------------------------------------------------------------------