diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 621739b01a5..d04d7a5b278 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -277,6 +277,10 @@ PHP 8.4 INTERNALS UPGRADE NOTES implementation has been improved to generate better seeds, however any users should use the opportunity to verify that seeding is first attempted using the CSPRNG for better output size flexibility. + - The standalone combined_lcg engine has been removed, as the lcg_value() + userland function is deprecated and as the engine is unable to return + unbiased integer values. The internal php_combined_lcg() function remains + available for now. c. ext/xsl - The function php_xsl_create_object() was removed as it was not used diff --git a/ext/random/config.m4 b/ext/random/config.m4 index 3d6e85a8e3c..5c4c2b8bda3 100644 --- a/ext/random/config.m4 +++ b/ext/random/config.m4 @@ -17,7 +17,6 @@ dnl Setup extension dnl PHP_NEW_EXTENSION([random], m4_normalize([ csprng.c - engine_combinedlcg.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_secure.c diff --git a/ext/random/config.w32 b/ext/random/config.w32 index 307a4f0d292..717055d6fc4 100644 --- a/ext/random/config.w32 +++ b/ext/random/config.w32 @@ -1,4 +1,4 @@ EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); PHP_RANDOM="yes"; -ADD_SOURCES(configure_module_dirname, "csprng.c engine_combinedlcg.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_xoshiro256starstar.c engine_secure.c engine_user.c gammasection.c randomizer.c zend_utils.c", "random"); +ADD_SOURCES(configure_module_dirname, "csprng.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_xoshiro256starstar.c engine_secure.c engine_user.c gammasection.c randomizer.c zend_utils.c", "random"); PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_csprng.h php_random_uint128.h"); diff --git a/ext/random/engine_combinedlcg.c b/ext/random/engine_combinedlcg.c deleted file mode 100644 index a68f0e3d0cb..00000000000 --- a/ext/random/engine_combinedlcg.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | Copyright (c) The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | https://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Sascha Schumann | - | Go Kudo | - +----------------------------------------------------------------------+ -*/ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "php.h" -#include "php_random.h" - -#include "Zend/zend_exceptions.h" - -/* - * combinedLCG() returns a pseudo random number in the range of (0, 1). - * The function combines two CGs with periods of - * 2^31 - 85 - 1 and 2^31 - 249 - 1. The period of this function - * is equal to the product of the two underlying periods, divided - * by factors shared by the underlying periods, i.e. 2.3 * 10^18. - * - * see: https://library.sciencemadness.org/lanl1_a/lib-www/numerica/f7-1.pdf - */ -#define MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m - -PHPAPI void php_random_combinedlcg_seed64(php_random_status_state_combinedlcg *state, uint64_t seed) -{ - state->state[0] = seed & 0xffffffffU; - state->state[1] = seed >> 32; -} - -static php_random_result generate(void *state) -{ - php_random_status_state_combinedlcg *s = state; - int32_t q, z; - - /* s->state[0] = (s->state[0] * 40014) % 2147483563; */ - MODMULT(53668, 40014, 12211, 2147483563L, s->state[0]); - /* s->state[1] = (s->state[1] * 40692) % 2147483399; */ - MODMULT(52774, 40692, 3791, 2147483399L, s->state[1]); - - z = s->state[0] - s->state[1]; - if (z < 1) { - z += 2147483562; - } - - return (php_random_result){ - .size = sizeof(uint32_t), - .result = (uint64_t) z, - }; -} - -static zend_long range(void *state, zend_long min, zend_long max) -{ - return php_random_range((php_random_algo_with_state){ - .algo = &php_random_algo_combinedlcg, - .state = state, - }, min, max); -} - -static bool serialize(void *state, HashTable *data) -{ - php_random_status_state_combinedlcg *s = state; - zval t; - - for (uint32_t i = 0; i < 2; i++) { - ZVAL_STR(&t, php_random_bin2hex_le(&s->state[i], sizeof(uint32_t))); - zend_hash_next_index_insert(data, &t); - } - - return true; -} - -static bool unserialize(void *state, HashTable *data) -{ - php_random_status_state_combinedlcg *s = state; - zval *t; - - for (uint32_t i = 0; i < 2; i++) { - t = zend_hash_index_find(data, i); - if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint32_t))) { - return false; - } - if (!php_random_hex2bin_le(Z_STR_P(t), &s->state[i])) { - return false; - } - } - - return true; -} - -PHPAPI const php_random_algo php_random_algo_combinedlcg = { - sizeof(php_random_status_state_combinedlcg), - generate, - range, - serialize, - unserialize -}; - -/* {{{ php_random_combinedlcg_seed_default */ -PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state) -{ - uint64_t seed = 0; - - if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) { - seed = php_random_generate_fallback_seed(); - } - - php_random_combinedlcg_seed64(state, seed); -} -/* }}} */ diff --git a/ext/random/php_random.h b/ext/random/php_random.h index 4c6ce344f6e..12172c6a083 100644 --- a/ext/random/php_random.h +++ b/ext/random/php_random.h @@ -61,10 +61,6 @@ PHPAPI uint32_t php_mt_rand(void); PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max); PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max); -typedef struct _php_random_status_state_combinedlcg { - int32_t state[2]; -} php_random_status_state_combinedlcg; - typedef struct _php_random_status_state_mt19937 { uint32_t count; enum php_random_mt19937_mode mode; @@ -107,7 +103,6 @@ typedef struct _php_random_fallback_seed_state { unsigned char seed[20]; } php_random_fallback_seed_state; -extern PHPAPI const php_random_algo php_random_algo_combinedlcg; extern PHPAPI const php_random_algo php_random_algo_mt19937; extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64; extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar; @@ -176,9 +171,6 @@ static inline php_random_algo_with_state php_random_default_engine(void) PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len); PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest); -PHPAPI void php_random_combinedlcg_seed64(php_random_status_state_combinedlcg *state, uint64_t seed); -PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state); - PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed); PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state); @@ -206,7 +198,7 @@ ZEND_BEGIN_MODULE_GLOBALS(random) bool combined_lcg_seeded; bool mt19937_seeded; php_random_fallback_seed_state fallback_seed_state; - php_random_status_state_combinedlcg combined_lcg; + int32_t combined_lcg[2]; php_random_status_state_mt19937 mt19937; ZEND_END_MODULE_GLOBALS(random) diff --git a/ext/random/random.c b/ext/random/random.c index 102ee0ebde8..46a6c4fd44f 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -396,14 +396,44 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest) /* {{{ php_combined_lcg */ PHPAPI double php_combined_lcg(void) { - php_random_status_state_combinedlcg *state = &RANDOM_G(combined_lcg); + int32_t *state = RANDOM_G(combined_lcg); if (!RANDOM_G(combined_lcg_seeded)) { - php_random_combinedlcg_seed_default(state); + uint64_t seed = 0; + + if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) { + seed = php_random_generate_fallback_seed(); + } + + state[0] = seed & 0xffffffffU; + state[1] = seed >> 32; RANDOM_G(combined_lcg_seeded) = true; } - return php_random_algo_combinedlcg.generate(state).result * 4.656613e-10; + /* + * combinedLCG() returns a pseudo random number in the range of (0, 1). + * The function combines two CGs with periods of + * 2^31 - 85 - 1 and 2^31 - 249 - 1. The period of this function + * is equal to the product of the two underlying periods, divided + * by factors shared by the underlying periods, i.e. 2.3 * 10^18. + * + * see: https://library.sciencemadness.org/lanl1_a/lib-www/numerica/f7-1.pdf + */ +#define PHP_COMBINED_LCG_MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m + + int32_t q, z; + + /* state[0] = (state[0] * 40014) % 2147483563; */ + PHP_COMBINED_LCG_MODMULT(53668, 40014, 12211, 2147483563L, state[0]); + /* state[1] = (state[1] * 40692) % 2147483399; */ + PHP_COMBINED_LCG_MODMULT(52774, 40692, 3791, 2147483399L, state[1]); + + z = state[0] - state[1]; + if (z < 1) { + z += 2147483562; + } + + return ((uint64_t)z) * 4.656613e-10; } /* }}} */