Speed
Where math operations are concerned phpseclib will use whatever extensions are availale to speed up operations. The impact of these extensions on various versions of PHP is shown below.
Note that the following benchmarks were performed with phpseclib v1. phpseclib v3's performance should be comparable. The reason phpseclib v1 was employed is to show how PHP has sped up over the years.
The benchmarks were performed on GitHub Actions using Docker containers from phpseclib/docker-php.
Benchmarks
PHP32 | PHP64 | BCMath | PHP32 | PHP64 | BCMath | GMP | |
w/o OpenSSL | w/ OpenSSL | ||||||
PHP 4.4 | 7.084 | 4.538 | 2.301 | 0.685 | 0.584 | 0.062 | 0.002 |
PHP 5.0 | 7.338 | 4.756 | 2.290 | 0.713 | 0.605 | 0.062 | 0.002 |
PHP 5.1 | 3.236 | 2.031 | 2.240 | 0.403 | 0.335 | 0.060 | 0.002 |
PHP 5.2 | 3.674 | 2.040 | 2.235 | 0.366 | 0.321 | 0.062 | 0.002 |
PHP 5.3 | 3.281 | 1.942 | 2.267 | 0.351 | 0.293 | 0.057 | 0.002 |
PHP 5.4 | 2.053 | 1.239 | 2.267 | 0.249 | 0.209 | 0.057 | 0.002 |
PHP 5.5 | 2.026 | 1.134 | 2.282 | 0.262 | 0.202 | 0.060 | 0.002 |
PHP 5.6 | 1.942 | 1.161 | 2.351 | 0.256 | 0.209 | 0.056 | 0.002 |
PHP 7.0 | 0.907 | 0.551 | 2.275 | 0.089 | 0.074 | 0.062 | 0.002 |
PHP 7.1 | 0.783 | 0.501 | 2.303 | 0.085 | 0.069 | 0.055 | 0.002 |
PHP 7.2 | 0.684 | 0.448 | 2.303 | 0.065 | 0.056 | 0.054 | 0.001 |
PHP 7.3 | 0.681 | 0.446 | 2.294 | 0.072 | 0.054 | 0.052 | 0.002 |
PHP 7.4 | 0.609 | 0.393 | 2.343 | 0.059 | 0.050 | 0.061 | 0.001 |
PHP 8.0 | 0.643 | 0.411 | 2.237 | 0.062 | 0.050 | 0.052 | 0.001 |
PHP 8.1 | 0.624 | 0.416 | 2.222 | 0.060 | 0.052 | 0.055 | 0.002 |
PHP 8.2 | 0.616 | 0.401 | 2.218 | 0.059 | 0.051 | 0.053 | 0.002 |
PHP 8.3 | 0.630 | 0.419 | 2.197 | 0.063 | 0.051 | 0.051 | 0.002 |
Benchmarks with JIT
PHP 8 introduced a new Just-In-Time (JIT) compilation engine. The specific JIT settings that were used can be seen in the Docker containers opcache.ini.
PHP32 | PHP64 | BCMath | PHP32 | PHP64 | BCMath | GMP | |
w/o OpenSSL | w/ OpenSSL | ||||||
PHP 8.0 | 0.210 | 0.100 | 2.247 | 0.032 | 0.028 | 0.053 | 0.002 |
PHP 8.1 | 0.188 | 0.087 | 2.228 | 0.033 | 0.029 | 0.053 | 0.002 |
PHP 8.2 | 0.197 | 0.087 | 2.222 | 0.032 | 0.028 | 0.053 | 0.002 |
PHP 8.3 | 0.189 | 0.089 | 2.202 | 0.032 | 0.029 | 0.052 | 0.002 |
GMP Engine
GMP, to quote wikipedia, "aims to be faster than any other bignum library", using "highly optimized assembly language code".
PHP32 / PHP64
These days, most systems are 64-bit, however, a notable exception are Raspberry Pi's.
On 32-bit systems phpseclib uses base-2**26 to reduce the number of digits of each number. When two 26-bit numbers are multiplied together the result is a 64-bit floating point (of which only 48 bits are used), which is then converted back to two 32-bit signed integers (of which only 26 bits are used).
On 64-bit systems phpseclib uses base-2**31 to reduce the number of digits of each number. When two 31-bit numbers are multiplied together the result is a 64-bit signed integer (of which only 62 bits are used), which is then converted back to two 32-bit signed integers (of which only 31 bits are used).
OpenSSL Enhancements
Only used for powMod(). Converts the exponent and the modulo to an appropriately formatted RSA public key and performs unpadded RSA encryption with that.