Skip to content

random: Remove engine_combinedlcg.c #15216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,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
Expand Down
1 change: 0 additions & 1 deletion ext/random/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/random/config.w32
Original file line number Diff line number Diff line change
@@ -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");
123 changes: 0 additions & 123 deletions ext/random/engine_combinedlcg.c

This file was deleted.

10 changes: 1 addition & 9 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)

Expand Down
36 changes: 33 additions & 3 deletions ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,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;
}
/* }}} */

Expand Down
Loading