Compare commits

...

117 Commits

Author SHA1 Message Date
Anton Smirnov 1c36416a2b Build coverage for tags 2023-09-07 16:01:52 +03:00
Anton Smirnov a96b7769db Changelog for 1.0.2 2023-09-07 16:00:12 +03:00
Anton Smirnov 9f177d14d1 Group by major version 2023-09-07 15:58:20 +03:00
Anton Smirnov 6337d693f7 Require arokettu/unsigned at least v1.2.1 2023-09-05 17:10:55 +03:00
Anton Smirnov 4ddedb80bf Test gmp only on versions where it properly builds 2023-09-05 02:13:41 +03:00
Anton Smirnov 8e0c74b85f Fix test error on 7.1 2023-09-05 02:08:27 +03:00
Anton Smirnov c8f2d081fe Build matrix 2023-09-04 23:31:25 +03:00
Anton Smirnov f536dfea76 Update configs from templates 2023-08-18 22:46:33 +03:00
Anton Smirnov 379fe45154 Pin PHPUnit to 9.5 2023-02-23 23:08:22 +02:00
Anton Smirnov b0382f15c4 Stable code standard 2023-02-18 09:45:10 +02:00
Anton Smirnov 87b2372582 License link should lead to the repo 2023-02-18 09:41:11 +02:00
Anton Smirnov eafd1ef4e6 Make license detectable by github 2023-02-18 09:39:18 +02:00
Anton Smirnov 1ffe409988 Run release image 2022-12-09 18:20:12 +02:00
Anton Smirnov 1b1be45ea1 Update docs to reflect PHP 8.2 final release 2022-12-08 21:35:45 +02:00
Anton Smirnov 0e5326edae Changelog for 1.0.1 2022-11-12 01:32:27 +02:00
Anton Smirnov 0e7cf39973 Changelog for compatibility fix 2022-11-12 01:23:14 +02:00
Anton Smirnov 6758b6e517 Fall back to badscaling only directly in getInt 2022-11-12 01:23:14 +02:00
Anton Smirnov 32119d9ef3 Prepare a test case for 9839 compatibility 2022-11-12 01:23:14 +02:00
Anton Smirnov 6f480a73cf Move development to GitHub 2022-11-10 23:21:43 +02:00
Anton Smirnov 273dcaae8c Changelog for #6 2022-11-10 23:17:44 +02:00
Anton Smirnov 08e9aa6dd3 Ignore class name in the exception message 2022-11-10 23:12:40 +02:00
Nicolas Grekas f604b83754 Rely on symfony/polyfill-php82 ^1.27 2022-11-10 22:57:45 +02:00
Anton Smirnov 54307e9fea Missing final 2022-11-03 20:40:47 +02:00
Anton Smirnov 45c39e14d5 Fix more insanity in the docs 2022-10-26 19:00:27 +03:00
Anton Smirnov e339842409 Release as 1.0.0 2022-10-26 15:21:54 +03:00
Anton Smirnov cf3b20b1ea Do not allow failure anymore 2022-10-26 15:20:48 +03:00
Anton Smirnov f3c524be37 Assert for the message first in case the exception itself changes (caught a conflict with xdebug) 2022-10-13 05:47:54 +03:00
Anton Smirnov 400c8d872d RC2 changelog 2022-10-01 19:05:37 +03:00
Anton Smirnov 71cac6c5c9 Optimize multiplication in native engine 2022-10-01 19:02:07 +03:00
Anton Smirnov 441fda27c4 Update docs 2022-09-27 23:42:34 +03:00
Anton Smirnov bcd08d85a6 This should be covered now since 32/64-bitness logic changed 2022-09-23 18:17:46 +03:00
Anton Smirnov 21662ceabe Fix tests under 32 bit 2022-09-15 01:33:22 +03:00
Anton Smirnov ab223a058d Docker is slow again 2022-09-14 23:43:32 +03:00
Anton Smirnov ee3fb8b7a2 Changelog for RC1 2022-09-14 23:39:05 +03:00
Anton Smirnov 92cd9f593b Require fixed arokettu/unsigned 2022-09-14 23:34:25 +03:00
Anton Smirnov 3b083b3fc3 Code style 2022-09-14 23:34:12 +03:00
Anton Smirnov 493ad2e6b3 Improve coverage, fix some tests not running 2022-09-14 22:26:17 +03:00
Anton Smirnov a8871787bd Length check is already implemented, just use it 2022-09-14 22:02:50 +03:00
Anton Smirnov 5fe4ee3500 Import random/randomizer tests 2022-09-14 21:48:03 +03:00
Anton Smirnov 6dedd00d98 Import random/randomizer/methods tests 2022-09-14 20:57:35 +03:00
Anton Smirnov c9be5ccc01 Fix generate result not truncated to 64 bits 2022-09-14 20:15:47 +03:00
Anton Smirnov 3680a07316 Update php/engine tests 2022-09-14 19:47:02 +03:00
Anton Smirnov 93345ce7e8 Check for the number of elements 2022-09-14 19:24:45 +03:00
Anton Smirnov 1ad149d804 Fix list 2022-09-07 03:58:56 +03:00
Anton Smirnov 435f6eafd6 Performance update 2022-09-03 15:41:49 +03:00
Anton Smirnov 70d1e02b0c Speed up MT and Xoshiro a bit 2022-09-03 02:15:22 +03:00
Anton Smirnov ec07169e37 Update index 2022-09-03 01:57:49 +03:00
Anton Smirnov b83ef88aee Thanks 2022-09-03 01:57:19 +03:00
Anton Smirnov 5e9a505483 Performance notes 2022-09-03 01:54:47 +03:00
Anton Smirnov 0179d83a08 Replace md headers with adoc 2022-09-03 01:08:42 +03:00
Anton Smirnov d228654d57 License 2022-09-03 01:07:35 +03:00
Anton Smirnov fde0cf3ea5 Update compatibility notes 2022-09-03 01:05:18 +03:00
Anton Smirnov 442de5e0e2 Obsolete 2022-09-02 21:55:09 +03:00
Anton Smirnov 363e9375af Secure engine should throw RandomException too 2022-09-02 21:54:41 +03:00
Anton Smirnov 0f8c61c149 Show PHP for master, remove version, showing 1.99 is meaningless 2022-09-02 01:29:48 +03:00
Anton Smirnov be4bb40006 Changelog for 1.0.0-beta1 2022-09-02 01:26:31 +03:00
Anton Smirnov 48f78d5a54 Select range like RC1 2022-09-01 16:03:26 +03:00
Anton Smirnov 67e0307059 Tests for RC1 2022-09-01 15:59:30 +03:00
Anton Smirnov 317ab93c7b Changelog for 0.5.0 2022-09-01 04:09:14 +03:00
Anton Smirnov 16c3b96192 Fix the last todo 2022-09-01 02:40:44 +03:00
Anton Smirnov 142ecefc60 Work on exception compatibility, test all corners 2022-09-01 02:29:29 +03:00
Anton Smirnov 7c0c701ccc Test legacy serialization 2022-09-01 01:50:31 +03:00
Anton Smirnov f77a75c796 Use stable version 2022-09-01 01:21:25 +03:00
Anton Smirnov d282df4521 Apply code style to tests too 2022-08-31 04:26:19 +03:00
Anton Smirnov 161bc30b89 Make fully qualified instead of import everything 2022-08-31 04:01:49 +03:00
Anton Smirnov 17211c7228 PHPDoc fixes 2022-08-31 03:56:42 +03:00
Anton Smirnov feda981c1e Test both unsigned and gmp 2022-08-31 03:45:57 +03:00
Anton Smirnov fc7ca2c854 Fix tests for unsigned 2022-08-31 03:42:29 +03:00
Anton Smirnov f23352bed2 Fix compare 2022-08-30 01:47:30 +03:00
Anton Smirnov d0944cac06 GMP and native pass 2022-08-30 01:30:07 +03:00
Anton Smirnov 6f60ad580c Finalize conversion sort of 2022-08-30 00:35:58 +03:00
Anton Smirnov 51dad539e6 GMP can be made optional 2022-08-26 22:07:51 +03:00
Anton Smirnov 3844f3a02d Fix Mt19937 with arokettu/unsigned 2022-08-26 22:07:51 +03:00
Anton Smirnov 34d19f7bbd Convert Mt19937 (broken with MathUnsigned) 2022-08-26 22:07:51 +03:00
Anton Smirnov e63498e381 Use native int for 32 bit unsigned on the 64 bit system 2022-08-26 06:17:33 +03:00
Anton Smirnov 397b1fbb16 Convert PCG 2022-08-26 05:06:04 +03:00
Anton Smirnov c3cc555b2f Convert Xoshiro 2022-08-26 04:13:24 +03:00
Anton Smirnov 608080e1ab Math engine 2022-08-26 04:11:22 +03:00
Anton Smirnov 195a4900cf Declare GMP as optional 2022-08-26 02:39:19 +03:00
Anton Smirnov 43444bdf8b Abandon the slow version 2022-08-20 00:33:11 +03:00
Anton Smirnov a224a9cd29 Prepare to split the nogmp version 2022-08-19 23:34:15 +03:00
Anton Smirnov 59acb2da2e Different method in the old PHPUnit 2022-08-19 23:07:09 +03:00
Anton Smirnov 271520911f Different message for anon class in earlier PHP 2022-08-19 23:02:32 +03:00
Anton Smirnov 38e55887d1 Note compatibility for 0.4 2022-08-19 22:58:19 +03:00
Anton Smirnov a7feb067d7 Internal mark 2022-08-19 22:55:56 +03:00
Anton Smirnov 756d952641 Changelog for 0.4.0 2022-08-19 22:54:53 +03:00
Anton Smirnov 4bc6745219 It won't run under PHP 8.2+ anyway 2022-08-19 22:52:13 +03:00
Anton Smirnov 1d4cd7e496 Disallow failures 2022-08-19 22:51:48 +03:00
Anton Smirnov 438612e090 user_exits.phpt 2022-08-19 22:45:00 +03:00
Anton Smirnov ec50e29642 user_throws.phpt 2022-08-19 22:36:40 +03:00
Anton Smirnov 43458d0a69 user_unsafe.phpt: heavily biased 2022-08-19 22:36:17 +03:00
Anton Smirnov b802917a10 Let them throw 2022-08-19 22:30:14 +03:00
Anton Smirnov 947b9a6807 user_unsafe.phpt: Empty string 2022-08-19 22:23:30 +03:00
Anton Smirnov 57542b11e5 pick_array_keys_error.phpt
readonly.phpt
serialize.phpt
2022-08-19 22:02:23 +03:00
Anton Smirnov c4f6cfaf00 Stubs 2022-08-18 22:46:18 +03:00
Anton Smirnov 6f921650ac Optimize slightly 2022-08-18 22:31:06 +03:00
Anton Smirnov 3bfd2e57f2 get_bytes.phpt
get_int_user.phpt
2022-08-18 21:54:31 +03:00
Anton Smirnov a18a3e41a0 construct_twice.phpt 2022-08-18 21:41:36 +03:00
Anton Smirnov b429eca714 basic.phpt
compatibility_mt.phpt
compatibility_user.phpt
2022-08-18 21:36:36 +03:00
Anton Smirnov 306cc30a15 Errors and exceptions 2022-08-18 21:17:45 +03:00
Anton Smirnov de90b6e4aa Beta 3 is out: handle all zeros in Xoshiro 2022-08-18 20:53:40 +03:00
Anton Smirnov 43a73d95ed xoshiro256starstar_seed.phpt (beta 2)
xoshiro256starstar_serialize.phpt
xoshiro256starstar_value.phpt
2022-08-17 17:11:40 +03:00
Anton Smirnov a7a45ff3c0 Remove todo 2022-08-17 17:10:36 +03:00
Anton Smirnov 68deb644a2 Constants are likely to be initialized either all or none, remove needless ifs 2022-08-17 16:49:22 +03:00
Anton Smirnov c00862192d user_compatibility.phpt
user_xoshiro128plusplus.phpt
2022-08-17 16:49:22 +03:00
Anton Smirnov 95ed61984b Use LogicException 2022-08-17 16:49:22 +03:00
Anton Smirnov f30c324248 pcgoneseq128xslrr64_jump_error.phpt
pcgoneseq128xslrr64_seed.phpt
pcgoneseq128xslrr64_serialize.phpt
pcgoneseq128xslrr64_value.phpt
2022-08-16 23:59:38 +03:00
Anton Smirnov c76af7ce8d Protect from setting dynamic properties 2022-08-16 23:39:17 +03:00
Anton Smirnov 9e5c654a1e Explain weird exception testing 2022-08-15 20:52:38 +03:00
Anton Smirnov f4c677ad56 all_serialize_native.phpt
all_serialize_user.phpt
mt19937_error.phpt
mt19937_serialize.phpt
mt19937_value.phpt
2022-08-15 19:34:10 +03:00
Anton Smirnov a1f89bd9b8 Done actually 2022-08-15 19:24:23 +03:00
Anton Smirnov 8ada4e0b2a all_serialize_error.phpt 2022-08-15 18:59:43 +03:00
Anton Smirnov d923e77b92 Plain string 2022-08-06 03:33:09 +03:00
Anton Smirnov 446baee786 These constant-like variables must always be set 2022-08-05 07:12:36 +03:00
Anton Smirnov 6ab0110ee9 Actually we provide 8.2.0 2022-08-05 06:08:52 +03:00
Anton Smirnov 9225e9a36a Missing license headers 2022-08-05 04:46:23 +03:00
Anton Smirnov 9ed72d3542 Update compatibility 2022-08-05 04:44:55 +03:00
100 changed files with 5449 additions and 1005 deletions

7
.gitattributes vendored
View File

@ -1,8 +1,13 @@
# template
/docs/*.py export-ignore
/docs/*.txt export-ignore
/docs/_* export-ignore
/tests export-ignore
/debug export-ignore
/.git* export-ignore
/*.yml export-ignore
/*.yaml export-ignore
/*.xml export-ignore
/*.xml.dist export-ignore
# project
/debug export-ignore

View File

@ -14,7 +14,6 @@ cache:
# install system packages
- apt-get update && apt-get install -y git unzip libgmp-dev
# install extensions
- docker-php-ext-install gmp
- if [ "$INSTALL_XDEBUG" -eq 1 ]; then pecl install xdebug; docker-php-ext-enable xdebug; fi
# install composer
- php -r "copy('https://composer.github.io/installer.sig', '/tmp/composer.sig');"
@ -37,53 +36,55 @@ coverage:
stage: report
only:
- master
- tags
image: php:8.1
script:
- composer update
- docker-php-ext-install gmp
- XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover coverage.xml
- bash <(curl -s https://codecov.io/bash)
# test in every version
test-7.1:
test:
extends: .test
stage: test
image: php:7.1
image: ${ARCH}/php:${PHP_VERSION}
parallel:
matrix:
- PHP_VERSION:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
ARCH:
- amd64
- i386
test-7.2:
test-gmp:
extends: .test
stage: test
image: php:7.2
test-7.3:
extends: .test
stage: test
image: php:7.3
test-7.4:
extends: .test
stage: test
image: php:7.4
test-8.0:
extends: .test
stage: test
image: php:8.0
test-8.1:
extends: .test
stage: test
image: php:8.1
image: ${ARCH}/php:${PHP_VERSION}
parallel:
matrix:
- PHP_VERSION:
- '7.4'
- '8.0'
- '8.1'
ARCH:
- amd64
- i386
script:
- docker-php-ext-install gmp
- composer update
- vendor/bin/phpunit
# control that our tests are valid
test-8.2-control:
extends: .test
stage: test
image: php:8.2-rc
# current version is compatible with current master, not any existing PHP 8.2 release
# todo: disallow failure after 8.2.0 is released
allow_failure: true
image: php:8.2
script:
# fake the version for some dependencies
- composer config platform.php '8.1.0'
- composer update
- vendor/bin/phpunit

13
.readthedocs.yaml Normal file
View File

@ -0,0 +1,13 @@
version: 2
build:
os: 'ubuntu-22.04'
tools:
python: '3.11'
sphinx:
configuration: 'docs/conf.py'
python:
install:
- requirements: 'docs/requirements.txt'

View File

@ -1,6 +1,80 @@
# Changelog
## 0.3.0
[gh@TimWolla]: https://github.com/TimWolla
[gh@nicolas-grekas]: https://github.com/nicolas-grekas
## 1.x
### 1.0.2
*Sep 7, 2023*
* Bump arokettu/unsigned to 1.2.1, previous versions may cause invalid results in Mersenne Twister on 32-bit systems.
### 1.0.1
*Nov 12, 2022*
* Rely on symfony/polyfill-php82 for interface implementations and Random\Engine\Secure ([#6][gh#6]),
thanks to [Nicolas Grekas][gh@nicolas-grekas]
* Added compatibility with PHP regression fix: [PHP#9839] ([#5][gh#5])
[gh#6]: https://github.com/arokettu/php-random-polyfill/pull/6
[PHP#9839]: https://github.com/php/php-src/pull/9839
[gh#5]: https://github.com/arokettu/php-random-polyfill/pull/5
### 1.0.0
*Oct 26, 2022*
Since RC1/RC2 are compatible with last 4 release candidates of PHP 8.2 (RC2-RC5),
RC2 is re-released as stable.
* No user facing changes since RC2
### 1.0.0-rc2
*Oct 1, 2022*
* Speed up 32 bit calculations in 64 bit envs
### 1.0.0-rc1
*Sep 14, 2022*
* `Secure` engine throws `RandomException` instead of `Exception`
* Added verification of the number of elements in serialized data
* Generated data is truncated to 64 bits in `Randomizer::getBytes()`
* Fixed integer overflow in the upstream math lib
### 1.0.0-beta1
*Sep 2, 2022*
* Fix incompatibility with PHP 8.2.0 RC1
## 0.x
### 0.5.0
*Sep 1, 2022*
Hopefully the last alpha
* Exceptions are now compatible in most known cases
* Full coverage
* GMP is now an optional dependency (but highly recommended)
### 0.4.0
*Aug 19, 2022*
* Verified compatibility with PHP 8.2.0 beta 3
* Tests ported from the PHP engine
* Various compatibility fixes
* Exceptions compatibility (mostly)
### 0.3.0
*Aug 5, 2022*
@ -9,7 +83,7 @@
* `Xoshiro256StarStar`
* Split `nextInt()` and `getInt($min, $max)` like in beta 2
## 0.2.1
### 0.2.1
*Jul 31, 2022*
@ -18,13 +92,12 @@
* ``arrayPickKeys()`` throws a warning because full compatibility is not achievable.
Thanks to [Tim Düsterhus][gh@TimWolla] for [the explanation][gh#1]
* No warning if the engine is ``CryptoSafeEngine``
* Fixed incorrect range function selection [[#1][gh#1]], thanks to [Tim Düsterhus][gh@TimWolla]
* Fixed incorrect range function selection ([#1][gh#1]), thanks to [Tim Düsterhus][gh@TimWolla]
* Fixed byte selection in range64
[gh#1]: https://github.com/arokettu/php-random-polyfill/issues/1
[gh@TimWolla]: https://github.com/TimWolla
## 0.2.0
### 0.2.0
*Jul 27, 2022*
@ -43,13 +116,13 @@ This will be fixed in 0.3.0 after PHP 8.2.0 beta 2 is released with some critica
* Serialization and unserialization are now compatible with PHP 8.2
if performed in PHP 7.4+
## 0.1.1
### 0.1.1
*Jul 23, 2022*
* Fixed Mt not generating enough data sometimes
## 0.1.0
### 0.1.0
*Jul 23, 2022*

View File

@ -34,7 +34,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
....
## PHP
== PHP
The library includes code taken from the PHP 8.1.0 beta 1 and rewritten from C to PHP.
@ -111,9 +111,9 @@ PHP includes the Zend Engine, freely available at
<http://www.zend.com>.
....
## Mersenne Twister
== Mersenne Twister
Mersenne Twister code is adapted from C++ code distributed under the 3-Clause BSD License.
Mersenne Twister code in PHP is adapted from C++ code distributed under the 3-Clause BSD License.
....
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,

View File

@ -1,7 +1,5 @@
# BSD 3-Clause "New" or "Revised" License
_This is a license for the product as a whole, see `COPYING.adoc` for additional licenses._
Copyright © 2022 Anton Smirnov
Redistribution and use in source and binary forms, with or without

View File

@ -1,22 +1,16 @@
# Random Extension Polyfill for PHP
[![Packagist](https://img.shields.io/packagist/v/arokettu/random-polyfill.svg?style=flat-square)](https://packagist.org/packages/arokettu/random-polyfill)
[![PHP](https://img.shields.io/packagist/php-v/arokettu/random-polyfill.svg?style=flat-square)](https://packagist.org/packages/arokettu/random-polyfill)
[![Packagist](https://img.shields.io/packagist/l/arokettu/random-polyfill.svg?style=flat-square)](https://opensource.org/licenses/BSD-3-Clause)
[![PHP](https://img.shields.io/packagist/php-v/arokettu/random-polyfill/dev-master.svg?style=flat-square)](https://packagist.org/packages/arokettu/random-polyfill)
[![Packagist](https://img.shields.io/packagist/l/arokettu/random-polyfill.svg?style=flat-square)][COPYING]
[![Gitlab pipeline status](https://img.shields.io/gitlab/pipeline/sandfox/php-random-polyfill/master.svg?style=flat-square)](https://gitlab.com/sandfox/php-random-polyfill/-/pipelines)
[![Codecov](https://img.shields.io/codecov/c/gl/sandfox/php-random-polyfill?style=flat-square)](https://codecov.io/gl/sandfox/php-random-polyfill/)
This is a polyfill for the new `ext-random` extension that will be released with PHP 8.2.
RFC:
* https://wiki.php.net/rfc/rng_extension
* https://wiki.php.net/rfc/random_extension_improvement
This is a polyfill for the new `ext-random` extension that was released with PHP 8.2.
## Requirements
* PHP 7.1
* GMP extension
* GMP extension is strongly recommended on PHP 7
## Installation
@ -26,39 +20,28 @@ composer require 'arokettu/random-polyfill'
## Compatibility
The library is compatible with `ext-random` as released in PHP 8.2.0 beta 1.
## What works
* `Random\Randomizer`
* Engines
* `Random\Engine` interface
* `Random\CryptoSafeEngine` interface
* Secure Engine: `Random\Engine\Secure`
* Mersenne Twister: `Random\Engine\Mt19937`
* PCG64: `Random\Engine\PcgOneseq128XslRr64`
* xoshiro256**: `Random\Engine\Xoshiro256StarStar`
## TODO
* Keep updating with fixes from the upcoming betas and release 1.0.0 around PHP 8.2.0 rc 1
* Empty `arokettu/random-polyfill` v1.99 for PHP 8.2.0 users
* Spin-off without extension dependencies?
The library aims to be compatible with `ext-random` as released in PHP 8.2.0 and subsequent patch releases.
## Documentation
### Random Extension
Read the official PHP doc: https://www.php.net/manual/en/book.random.php
### The Polyfill
Read full documentation here: <https://sandfox.dev/php/random-polyfill.html>
Also on Read the Docs: <https://php-random-polyfill.readthedocs.io/>
## Support
Please file issues on our main repo at GitLab: <https://gitlab.com/sandfox/php-random-polyfill/-/issues>
Please file issues on our main repo at GitHub: <https://github.com/arokettu/php-random-polyfill/issues>
## License
The library is available as open source under the terms of the [3-Clause BSD License].
See [COPYING.adoc][COPYING] for additional licenses.
[3-Clause BSD License]: https://opensource.org/licenses/BSD-3-Clause
[3-Clause BSD License]: LICENSE.md
[COPYING]: COPYING.adoc

View File

@ -6,8 +6,8 @@
"license": "BSD-3-Clause",
"homepage": "https://sandfox.dev/php/random-polyfill.html",
"support": {
"issues": "https://gitlab.com/sandfox/php-random-polyfill/-/issues",
"source": "https://gitlab.com/sandfox/php-random-polyfill",
"issues": "https://github.com/arokettu/php-random-polyfill/issues",
"source": "https://github.com/arokettu/php-random-polyfill",
"docs": "https://php-random-polyfill.readthedocs.io/",
"chat": "https://gitter.im/arokettu/community"
},
@ -37,18 +37,22 @@
},
"require": {
"php": "^7.1 | ^8.0",
"ext-gmp": "*",
"arokettu/unsigned": "^1.2.1",
"symfony/polyfill-php80": "^1.22",
"symfony/polyfill-php81": "^1.22"
"symfony/polyfill-php81": "^1.22",
"symfony/polyfill-php82": "^1.27"
},
"require-dev": {
"phpunit/phpunit": "^7.5 | ^8.5 | ^9.5",
"phpunit/phpunit": "^7.5 | ^8.5 | 9.5.*",
"psy/psysh": "*",
"sandfox.dev/code-standard": "^10@dev",
"sandfox.dev/code-standard": "^1",
"squizlabs/php_codesniffer": "*",
"vimeo/psalm": "^4.24"
},
"provide": {
"ext-random": "8.1.0"
"ext-random": "8.2.0"
},
"suggest": {
"ext-gmp": "For significantly faster calculation"
}
}

11
debug/gh-9186.php Normal file
View File

@ -0,0 +1,11 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
var_dump(
unserialize(
'O:17:"Random\Randomizer":1:{i:0;a:2:{s:3:"foo";N;s:6:"engine";O:32:"Random\Engine\Xoshiro256StarStar":2:' .
'{i:0;a:0:{}i:1;a:4:{i:0;s:16:"7520fbc2d6f8de46";i:1;s:16:"84d2d2b9d7ba0a34";i:2;s:16:"d975f36db6490b32";i:3;' .
's:16:"c19991ee16785b94";}}}}'
)
);

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,7 @@
require __DIR__ . '/../vendor/autoload.php';
//$rnd = new \Random\Randomizer(new \Random\Engine\Secure());
//$rnd = new \Random\Randomizer(new \Arokettu\Random\Tests\DevEngines\Xorshift32(123)); // TODO: different results
//$rnd = new \Random\Randomizer(new \Arokettu\Random\Tests\DevEngines\Xorshift32(123));
//$rnd = new \Random\Randomizer(new \Random\Engine\Mt19937(123, MT_RAND_MT19937));
$rnd = new \Random\Randomizer(new \Random\Engine\Mt19937(123, MT_RAND_PHP));
//$rnd = new \Random\Randomizer(new \Arokettu\Random\Tests\DevEngines\Zeros());

3
docs/_templates/rtd-version.html vendored Normal file
View File

@ -0,0 +1,3 @@
{%- if current_version -%}
<div class="sidebar-brand">{{ current_version }}</div>
{%- endif -%}

View File

@ -1,22 +0,0 @@
<a class="sidebar-brand{% if logo %} centered{% endif %}" href="{{ pathto(master_doc) }}">
{% block brand_content %}
{%- if logo_url %}
<div class="sidebar-logo-container">
<img class="sidebar-logo" src="{{ logo_url }}" alt="Logo"/>
</div>
{%- endif %}
{%- if theme_light_logo and theme_dark_logo %}
<div class="sidebar-logo-container">
<img class="sidebar-logo only-light" src="{{ pathto('_static/' + theme_light_logo, 1) }}" alt="Light Logo"/>
<img class="sidebar-logo only-dark" src="{{ pathto('_static/' + theme_dark_logo, 1) }}" alt="Dark Logo"/>
</div>
{%- endif %}
{% if not theme_sidebar_hide_name %}
<span class="sidebar-brand-text">{{ docstitle if docstitle else project }}</span>
{%- endif %}
{% endblock brand_content %}
</a>
{%- if current_version -%}
<div class="sidebar-brand">{{ current_version }}</div>
{%- endif -%}

View File

@ -4,38 +4,33 @@ Compatibility Notes
PHP
===
The library is aimed to be compatible with ``ext-random`` from PHP 8.1.
* Version 0.1 is compatible with PHP 8.2.0 beta 1
* Version 1.0 will be compatible with PHP 8.2.0 rc 1
Performance
===========
Well, it uses GMP, it's guaranteed to be slow.
According to my tests, Randomizer + Mersenne Twister is 20 times slower than native.
Good enough if you need a dozen numbers or so.
What works
==========
.. note::
Random Extension doc on the PHP website:
https://www.php.net/manual/en/book.random.php
The library aims to be compatible with ``ext-random`` as released in PHP 8.2.0 and subsequent patch releases.
The library will not be a full replacement for ``ext-random`` and total compatibility does not seem to be achievable.
* ``Random\Randomizer``
Available classes:
* Engines
* This polyfill:
* ``Random\Engine`` interface
* ``Random\CryptoSafeEngine`` interface
* Secure Engine: ``Random\Engine\Secure``
* Mersenne Twister: ``Random\Engine\Mt19937``
* PCG64: ``Random\Engine\PcgOneseq128XslRr64``
* xoshiro256**: ``Random\Engine\Xoshiro256StarStar``
* `Random\\Randomizer <https://www.php.net/manual/en/class.random-randomizer.php>`__
* `Random\\Engine\\Mt19937 <https://www.php.net/manual/en/class.random-engine-mt19937.php>`__
* `Random\\Engine\\PcgOneseq128XslRr64 <https://www.php.net/manual/en/class.random-engine-pcgoneseq128xslrr64.php>`__
* `Random\\Engine\\Xoshiro256StarStar <https://www.php.net/manual/en/class.random-engine-xoshiro256starstar.php>`__
* From `symfony/polyfill-php82 <https://packagist.org/packages/symfony/polyfill-php82>`__:
* `Random\\Engine <https://www.php.net/manual/en/class.random-engine.php>`__
* `Random\\Engine\\CryptoSafeEngine <https://www.php.net/manual/en/class.random-cryptosafeengine.php>`__
* `Random\\Engine\\Secure <https://www.php.net/manual/en/class.random-engine-secure.php>`__
Version 1.99.0
==============
Version 1.99.0 will be released as an empty package for PHP >= 8.2.
Version 1.99.0 is an empty package for PHP >= 8.2.
It ensures that no overhead or extra code will be used for PHP 8.2+ apps.
Known differences
=================
@ -52,6 +47,10 @@ Serialization
unserializable by the native extension.
* Serialization in PHP 7.1 - 7.3 will trigger a warning.
Silence it with ``@`` if you don't care.
* Does not throw in GH-9186_ case but also does not create a dynamic property (PHP 7.4+)
Undefined behavior for PHP <7.4 (returns false with a warning for the given code)
.. _GH-9186: https://github.com/php/php-src/issues/9186
Randomizer
----------
@ -92,3 +91,8 @@ Mt19937
* Generating integers with ``$max - $min >= mt_getrandmax()`` with ``MT_RAND_PHP`` is considered undefined behavior.
This library is consistent with PHP 8.2 behavior, not the version it runs under.
Warnings
--------
For obvious reasons, native extension produces ``E_WARNING`` and the library produces ``E_USER_WARNING``.

View File

@ -1,6 +1,10 @@
from datetime import datetime
project = 'Random Extension Polyfill'
# import specific project config
import os, sys
sys.path.append(os.curdir)
from conf_project import *
author = 'Anton Smirnov'
copyright = '{} {}'.format(datetime.now().year, author)
language = 'en'
@ -8,3 +12,17 @@ language = 'en'
html_title = project
html_theme = 'furo'
templates_path = ["_templates"]
html_sidebars = {
"**": [
"sidebar/brand.html",
"rtd-version.html",
"sidebar/search.html",
"sidebar/scroll-start.html",
"sidebar/navigation.html",
"sidebar/ethical-ads.html",
"sidebar/scroll-end.html",
"sidebar/variant-selector.html",
]
}
exclude_patterns = ['venv/*']

1
docs/conf_project.py Normal file
View File

@ -0,0 +1 @@
project = 'Random Extension Polyfill'

View File

@ -3,18 +3,13 @@ Random Extension Polyfill
|Packagist| |GitLab| |GitHub| |Bitbucket| |Gitea|
This is a polyfill for the new ``ext-random`` extension that will be released with PHP 8.2.
RFC:
* https://wiki.php.net/rfc/rng_extension
* https://wiki.php.net/rfc/random_extension_improvement
This is a polyfill for the new ``ext-random`` extension that was released with PHP 8.2.
Requirements
============
* PHP 7.1
* GMP extension
* GMP extension is strongly recommended on PHP 7 and somewhat recommended on PHP 8
Installation
============
@ -23,6 +18,11 @@ Installation
composer require 'arokettu/random-polyfill'
Random Extension Documentation
==============================
On the PHP website: https://www.php.net/manual/en/book.random.php
Documentation
=============
@ -30,27 +30,17 @@ Documentation
:maxdepth: 2
compatibility
TODO
====
* Keep updating with fixes from the upcoming betas and release 1.0.0 around PHP 8.2.0 rc 1
* Empty ``arokettu/random-polyfill`` v1.99 for PHP 8.2.0 users
* Other engines
* Maybe
* Some day
* If I have time
* Don't count on it
performance
license
thanks
License
=======
The library is available as open source under the terms of the `3-Clause BSD License`__.
See `COPYING.adoc`__ for additional licenses.
See :ref:`license` for additional licenses.
.. __: https://opensource.org/licenses/BSD-3-Clause
.. __: https://gitlab.com/sandfox/php-random-polyfill/-/blob/master/COPYING.adoc
.. |Packagist| image:: https://img.shields.io/packagist/v/arokettu/random-polyfill.svg?style=flat-square
:target: https://packagist.org/packages/arokettu/random-polyfill

156
docs/license.rst Normal file
View File

@ -0,0 +1,156 @@
.. _license:
License
#######
The library itself is distributed under the 3-Clause BSD License but it also uses some code under other licenses.
Here are all the terms on one page.
Random Extension Polyfill for PHP
=================================
The library itself.
Copyright © 2022 Anton Smirnov
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
PHP
===
The library includes code taken from the PHP 8.1.0 beta 1 and rewritten from C to PHP.
PHP is distributed under the PHP License.
.. code-block:: text
--------------------------------------------------------------------
The PHP License, version 3.01
Copyright (c) 1999 - 2019 The PHP Group. All rights reserved.
--------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
modification, is permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name "PHP" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact group@php.net.
4. Products derived from this software may not be called "PHP", nor
may "PHP" appear in their name, without prior written permission
from group@php.net. You may indicate that your software works in
conjunction with PHP by saying "Foo for PHP" instead of calling
it "PHP Foo" or "phpfoo"
5. The PHP Group may publish revised and/or new versions of the
license from time to time. Each version will be given a
distinguishing version number.
Once covered code has been published under a particular version
of the license, you may always continue to use it under the terms
of that version. You may also choose to use such covered code
under the terms of any subsequent version of the license
published by the PHP Group. No one other than the PHP Group has
the right to modify the terms applicable to covered code created
under this License.
6. Redistributions of any form whatsoever must retain the following
acknowledgment:
"This product includes PHP software, freely available from
<http://www.php.net/software/>".
THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND
ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP
DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------
This software consists of voluntary contributions made by many
individuals on behalf of the PHP Group.
The PHP Group can be contacted via Email at group@php.net.
For more information on the PHP Group and the PHP project,
please see <http://www.php.net>.
PHP includes the Zend Engine, freely available at
<http://www.zend.com>.
Mersenne Twister
================
Mersenne Twister code in PHP is adapted from C++ code distributed under the 3-Clause BSD License.
.. code-block:: text
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
Copyright (C) 2000 - 2003, Richard J. Wagner
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

22
docs/performance.rst Normal file
View File

@ -0,0 +1,22 @@
Performance Notes
#################
Well, even with GMP, it's guaranteed to be slow.
Good enough if you need a dozen numbers or so.
Performance test
================
https://github.com/arokettu/random-polyfill-perf
* Secure engine is mostly unaffected by the choice of backend (except for nextInt() case)
* PHP 7:
* With GMP installed, all engines are approximately 400 times slower than native
* Mersenne Twister is consistently 400 times slower than native, whether you use GMP or not
* PCG is 100 times slower without GMP and xoshiro256** is 50 times slower without GMP
* PHP 8:
* 100-150 times slower than native
* GMP presence does not affect performance that much, but PCG will run twice as fast
* JIT helps almost as much as GMP (use both for max performance)

View File

@ -1 +1,2 @@
sphinx>=7
furo

7
docs/thanks.rst Normal file
View File

@ -0,0 +1,7 @@
Thanks
######
* Random extension developers:
Go Kudo, Tim Düsterhus, Guilliam Xavier, Christoph M. Becker, Jakub Zelenka, Bob Weinand, Máté Kocsis,
and Original RNG implementators
* Tim Düsterhus for reviews, issue reports and useful advices

View File

@ -4,11 +4,18 @@
-->
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">
<rule ref="SandFox_PHP71">
<!-- all functions are imported -->
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/>
<!-- crashes -->
<exclude name="Security.BadFunctions.CallbackFunctions"/>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions">
<properties>
<!-- Reset to default (include all functions) -->
<property name="includeSpecialFunctions" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
<!-- no need to cover 8.2 and later -->
<config name="testVersion" value="7.1-8.1"/>
<file>src</file>
<!-- <file>tests</file>-->
<file>tests</file>
</ruleset>

View File

@ -1,102 +0,0 @@
<?php
declare(strict_types=1);
namespace Arokettu\Random;
use GMP;
use function gmp_export;
use function gmp_import;
use function gmp_init;
use function str_pad;
use function substr;
use const GMP_LITTLE_ENDIAN;
use const GMP_LSW_FIRST;
/**
* @internal
*/
trait BigIntExportImport
{
/**
* @var int
* @psalm-var positive-int
*/
private static $SIZEOF_UINT32_T = 4;
/**
* @var int
* @psalm-var positive-int
*/
private static $SIZEOF_UINT64_T = 8;
/**
* @var int
* @psalm-var positive-int
*/
private static $SIZEOF_UINT128_T = 16;
/** @var GMP|null 32-bit bitmask aka max 32-bit uint */
private static $UINT32_MASK = null;
/** @var GMP|null 64-bit bitmask aka max 64-bit uint */
private static $UINT64_MASK = null;
/** @var GMP|null 128-bit bitmask aka max 128-bit uint */
private static $UINT128_MASK = null;
private function initGmpConst(): void
{
if (self::$UINT32_MASK === null) {
self::$UINT32_MASK = gmp_init('ffffffff', 16);
}
if (self::$UINT64_MASK === null) {
self::$UINT64_MASK = gmp_init('ffffffffffffffff', 16);
}
if (self::$UINT128_MASK === null) {
self::$UINT128_MASK = gmp_init('ffffffffffffffffffffffffffffffff', 16);
}
}
private function importGmp32(string $value): GMP
{
$value = substr($value, 0, self::$SIZEOF_UINT32_T);
$value = str_pad($value, self::$SIZEOF_UINT32_T, "\0");
return gmp_import($value, self::$SIZEOF_UINT32_T, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
}
private function importGmp64(string $value): GMP
{
$value = substr($value, 0, self::$SIZEOF_UINT64_T);
$value = str_pad($value, self::$SIZEOF_UINT64_T, "\0");
return gmp_import($value, self::$SIZEOF_UINT64_T, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
}
private function importGmp128hilo(string $hi, string $lo): GMP
{
$value = $lo . $hi;
$value = substr($value, 0, self::$SIZEOF_UINT128_T);
$value = str_pad($value, self::$SIZEOF_UINT128_T, "\0");
return gmp_import($value, self::$SIZEOF_UINT128_T, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
}
private function exportGmp32(GMP $value): string
{
$value = $value & self::$UINT32_MASK;
$value = gmp_export($value, self::$SIZEOF_UINT32_T, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
return str_pad($value, self::$SIZEOF_UINT32_T, "\0");
}
private function exportGmp64(GMP $value): string
{
$value = $value & self::$UINT64_MASK;
$value = gmp_export($value, self::$SIZEOF_UINT64_T, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
return str_pad($value, self::$SIZEOF_UINT64_T, "\0");
}
private function exportGmp128hilo(GMP $value): array
{
$value = $value & self::$UINT128_MASK;
$value = gmp_export($value, self::$SIZEOF_UINT128_T, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
$value = str_pad($value, self::$SIZEOF_UINT128_T, "\0");
return array_reverse(str_split($value, self::$SIZEOF_UINT64_T));
}
}

View File

@ -0,0 +1,160 @@
<?php
/**
* @copyright Copyright © 2022 Anton Smirnov
* @license BSD-3-Clause https://spdx.org/licenses/BSD-3-Clause.html
*
* @noinspection PhpComposerExtensionStubsInspection
*/
declare(strict_types=1);
namespace Arokettu\Random;
use GMP;
/**
* @codeCoverageIgnore We don't care about math that was not used
*/
abstract class Math
{
public const SIZEOF_UINT32_T = 4;
public const SIZEOF_UINT64_T = 8;
public const SIZEOF_UINT128_T = 16;
/** @var Math[] */
private static $maths = [];
public static function create(int $sizeof): self
{
return self::$maths[$sizeof] ?? self::$maths[$sizeof] = self::build($sizeof);
}
protected static function build(int $sizeof): self
{
// only less because PHP int is always signed
if ($sizeof < \PHP_INT_SIZE) {
return new MathNative($sizeof);
}
if (\extension_loaded('gmp')) {
return new MathGMP($sizeof);
}
return new MathUnsigned($sizeof);
}
/**
* @param int $sizeof
*/
abstract protected function __construct(int $sizeof);
/**
* @param int|string|GMP $value
* @param int $shift
* @return int|string|GMP
*/
abstract public function shiftLeft($value, int $shift);
/**
* @param int|string|GMP $value
* @param int $shift
* @return int|string|GMP
*/
abstract public function shiftRight($value, int $shift);
/**
* @param int|string|GMP $value1
* @param int|string|GMP $value2
* @return int|string|GMP
*/
abstract public function add($value1, $value2);
/**
* @param int|string|GMP $value1
* @param int $value2
* @return int|string|GMP
*/
abstract public function addInt($value1, int $value2);
/**
* @param int|string|GMP $value1
* @param int|string|GMP $value2
* @return int|string|GMP
*/
abstract public function sub($value1, $value2);
/**
* @param int|string|GMP $value1
* @param int $value2
* @return int|string|GMP
*/
abstract public function subInt($value1, int $value2);
/**
* @param int|string|GMP $value1
* @param int|string|GMP $value2
* @return int|string|GMP
*/
abstract public function mul($value1, $value2);
/**
* @param int|string|GMP $value1
* @param int $value2
* @return int|string|GMP
*/
abstract public function mulInt($value1, int $value2);
/**
* @param int|string|GMP $value1
* @param int|string|GMP $value2
* @return int|string|GMP
*/
abstract public function mod($value1, $value2);
/**
* @param int|string|GMP $value1
* @param int|string|GMP $value2
* @return int
*/
abstract public function compare($value1, $value2): int;
/**
* @param string $value
* @return int|string|GMP
*/
abstract public function fromHex(string $value);
/**
* @param int $value
* @return int|string|GMP
*/
abstract public function fromInt(int $value);
/**
* @param string $value
* @return int|string|GMP
*/
abstract public function fromBinary(string $value);
/**
* @param int|string|GMP $value
*/
abstract public function toInt($value): int;
/**
* @param int|string|GMP $value
*/
abstract public function toSignedInt($value): int;
/**
* @param int|string|GMP $value
*/
abstract public function toBinary($value): string;
/**
* @param int|string|GMP $value
* @return int[]|string[]|GMP[]
*/
abstract public function splitHiLo($value): array;
}

View File

@ -0,0 +1,222 @@
<?php
/**
* @copyright Copyright © 2022 Anton Smirnov
* @license BSD-3-Clause https://spdx.org/licenses/BSD-3-Clause.html
*
* @noinspection PhpComposerExtensionStubsInspection
* @noinspection PhpReturnDocTypeMismatchInspection
* @noinspection PhpIncompatibleReturnTypeInspection
*/
declare(strict_types=1);
namespace Arokettu\Random;
use GMP;
/**
* @internal
* @psalm-suppress MoreSpecificImplementedParamType
* @codeCoverageIgnore We don't care about math that was not used
*/
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
// phpcs:disable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
final class MathGMP extends Math
{
/** @var GMP */
private $mask;
/** @var int */
private $sizeof;
/**
* @param int $sizeof
*/
public function __construct(int $sizeof)
{
$this->mask = \gmp_pow(2, $sizeof * 8) - 1;
$this->sizeof = $sizeof;
}
/**
* @param GMP $value