Rev 874 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 874 | Rev 1042 | ||
---|---|---|---|
Line 3... | Line 3... | ||
3 | /** |
3 | /** |
4 | * BCMath BigInteger Engine |
4 | * BCMath BigInteger Engine |
5 | * |
5 | * |
6 | * PHP version 5 and 7 |
6 | * PHP version 5 and 7 |
7 | * |
7 | * |
8 | * @category Math |
- | |
9 | * @package BigInteger |
- | |
10 | * @author Jim Wigginton <terrafrost@php.net> |
8 | * @author Jim Wigginton <terrafrost@php.net> |
11 | * @copyright 2017 Jim Wigginton |
9 | * @copyright 2017 Jim Wigginton |
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
13 | * @link http://pear.php.net/package/Math_BigInteger |
11 | * @link http://pear.php.net/package/Math_BigInteger |
14 | */ |
12 | */ |
15 | 13 | ||
16 | namespace phpseclib3\Math\BigInteger\Engines; |
14 | namespace phpseclib3\Math\BigInteger\Engines; |
17 | 15 | ||
18 | use ParagonIE\ConstantTime\Hex; |
16 | use phpseclib3\Common\Functions\Strings; |
19 | use phpseclib3\Exception\BadConfigurationException; |
17 | use phpseclib3\Exception\BadConfigurationException; |
20 | 18 | ||
21 | /** |
19 | /** |
22 | * BCMath Engine. |
20 | * BCMath Engine. |
23 | * |
21 | * |
24 | * @package BCMath |
- | |
25 | * @author Jim Wigginton <terrafrost@php.net> |
22 | * @author Jim Wigginton <terrafrost@php.net> |
26 | * @access public |
- | |
27 | */ |
23 | */ |
28 | class BCMath extends Engine |
24 | class BCMath extends Engine |
29 | { |
25 | { |
30 | /** |
26 | /** |
31 | * Can Bitwise operations be done fast? |
27 | * Can Bitwise operations be done fast? |
32 | * |
28 | * |
33 | * @see parent::bitwise_leftRotate() |
29 | * @see parent::bitwise_leftRotate() |
34 | * @see parent::bitwise_rightRotate() |
30 | * @see parent::bitwise_rightRotate() |
35 | * @access protected |
- | |
36 | */ |
31 | */ |
37 | const FAST_BITWISE = false; |
32 | const FAST_BITWISE = false; |
38 | 33 | ||
39 | /** |
34 | /** |
40 | * Engine Directory |
35 | * Engine Directory |
41 | * |
36 | * |
42 | * @see parent::setModExpEngine |
37 | * @see parent::setModExpEngine |
43 | * @access protected |
- | |
44 | */ |
38 | */ |
45 | const ENGINE_DIR = 'BCMath'; |
39 | const ENGINE_DIR = 'BCMath'; |
46 | 40 | ||
47 | /** |
41 | /** |
48 | * Test for engine validity |
42 | * Test for engine validity |
Line 85... | Line 79... | ||
85 | protected function initialize($base) |
79 | protected function initialize($base) |
86 | { |
80 | { |
87 | switch (abs($base)) { |
81 | switch (abs($base)) { |
88 | case 256: |
82 | case 256: |
89 | // round $len to the nearest 4 |
83 | // round $len to the nearest 4 |
90 | $len = (strlen($this->value) + 3) & 0xFFFFFFFC; |
84 | $len = (strlen($this->value) + 3) & ~3; |
91 | 85 | ||
92 | $x = str_pad($this->value, $len, chr(0), STR_PAD_LEFT); |
86 | $x = str_pad($this->value, $len, chr(0), STR_PAD_LEFT); |
93 | 87 | ||
94 | $this->value = '0'; |
88 | $this->value = '0'; |
95 | for ($i = 0; $i < $len; $i += 4) { |
89 | for ($i = 0; $i < $len; $i += 4) { |
Line 107... | Line 101... | ||
107 | $this->value = '-' . $this->value; |
101 | $this->value = '-' . $this->value; |
108 | } |
102 | } |
109 | break; |
103 | break; |
110 | case 16: |
104 | case 16: |
111 | $x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value; |
105 | $x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value; |
112 | $temp = new self(Hex::decode($x), 256); |
106 | $temp = new self(Strings::hex2bin($x), 256); |
113 | $this->value = $this->is_negative ? '-' . $temp->value : $temp->value; |
107 | $this->value = $this->is_negative ? '-' . $temp->value : $temp->value; |
114 | $this->is_negative = false; |
108 | $this->is_negative = false; |
115 | break; |
109 | break; |
116 | case 10: |
110 | case 10: |
117 | // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different |
111 | // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different |