Skip to content

Commit 6910167

Browse files
authored
random: Remove engine_combinedlcg.c (#15216)
The standalone engine cannot be usefully used for any other purpose. Remove it and inline the implementation into the `php_combined_lcg()` function.
1 parent e8ff7c7 commit 6910167

File tree

6 files changed

+39
-137
lines changed

6 files changed

+39
-137
lines changed

UPGRADING.INTERNALS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ PHP 8.4 INTERNALS UPGRADE NOTES
277277
implementation has been improved to generate better seeds, however any
278278
users should use the opportunity to verify that seeding is first
279279
attempted using the CSPRNG for better output size flexibility.
280+
- The standalone combined_lcg engine has been removed, as the lcg_value()
281+
userland function is deprecated and as the engine is unable to return
282+
unbiased integer values. The internal php_combined_lcg() function remains
283+
available for now.
280284

281285
c. ext/xsl
282286
- The function php_xsl_create_object() was removed as it was not used

ext/random/config.m4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ dnl Setup extension
1717
dnl
1818
PHP_NEW_EXTENSION([random], m4_normalize([
1919
csprng.c
20-
engine_combinedlcg.c
2120
engine_mt19937.c
2221
engine_pcgoneseq128xslrr64.c
2322
engine_secure.c

ext/random/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
22
PHP_RANDOM="yes";
3-
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");
3+
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");
44
PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_csprng.h php_random_uint128.h");

ext/random/engine_combinedlcg.c

Lines changed: 0 additions & 123 deletions
This file was deleted.

ext/random/php_random.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ PHPAPI uint32_t php_mt_rand(void);
6161
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
6262
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
6363

64-
typedef struct _php_random_status_state_combinedlcg {
65-
int32_t state[2];
66-
} php_random_status_state_combinedlcg;
67-
6864
typedef struct _php_random_status_state_mt19937 {
6965
uint32_t count;
7066
enum php_random_mt19937_mode mode;
@@ -107,7 +103,6 @@ typedef struct _php_random_fallback_seed_state {
107103
unsigned char seed[20];
108104
} php_random_fallback_seed_state;
109105

110-
extern PHPAPI const php_random_algo php_random_algo_combinedlcg;
111106
extern PHPAPI const php_random_algo php_random_algo_mt19937;
112107
extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
113108
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)
176171
PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
177172
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
178173

179-
PHPAPI void php_random_combinedlcg_seed64(php_random_status_state_combinedlcg *state, uint64_t seed);
180-
PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state);
181-
182174
PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed);
183175
PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
184176

@@ -206,7 +198,7 @@ ZEND_BEGIN_MODULE_GLOBALS(random)
206198
bool combined_lcg_seeded;
207199
bool mt19937_seeded;
208200
php_random_fallback_seed_state fallback_seed_state;
209-
php_random_status_state_combinedlcg combined_lcg;
201+
int32_t combined_lcg[2];
210202
php_random_status_state_mt19937 mt19937;
211203
ZEND_END_MODULE_GLOBALS(random)
212204

ext/random/random.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,44 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
396396
/* {{{ php_combined_lcg */
397397
PHPAPI double php_combined_lcg(void)
398398
{
399-
php_random_status_state_combinedlcg *state = &RANDOM_G(combined_lcg);
399+
int32_t *state = RANDOM_G(combined_lcg);
400400

401401
if (!RANDOM_G(combined_lcg_seeded)) {
402-
php_random_combinedlcg_seed_default(state);
402+
uint64_t seed = 0;
403+
404+
if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
405+
seed = php_random_generate_fallback_seed();
406+
}
407+
408+
state[0] = seed & 0xffffffffU;
409+
state[1] = seed >> 32;
403410
RANDOM_G(combined_lcg_seeded) = true;
404411
}
405412

406-
return php_random_algo_combinedlcg.generate(state).result * 4.656613e-10;
413+
/*
414+
* combinedLCG() returns a pseudo random number in the range of (0, 1).
415+
* The function combines two CGs with periods of
416+
* 2^31 - 85 - 1 and 2^31 - 249 - 1. The period of this function
417+
* is equal to the product of the two underlying periods, divided
418+
* by factors shared by the underlying periods, i.e. 2.3 * 10^18.
419+
*
420+
* see: https://library.sciencemadness.org/lanl1_a/lib-www/numerica/f7-1.pdf
421+
*/
422+
#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
423+
424+
int32_t q, z;
425+
426+
/* state[0] = (state[0] * 40014) % 2147483563; */
427+
PHP_COMBINED_LCG_MODMULT(53668, 40014, 12211, 2147483563L, state[0]);
428+
/* state[1] = (state[1] * 40692) % 2147483399; */
429+
PHP_COMBINED_LCG_MODMULT(52774, 40692, 3791, 2147483399L, state[1]);
430+
431+
z = state[0] - state[1];
432+
if (z < 1) {
433+
z += 2147483562;
434+
}
435+
436+
return ((uint64_t)z) * 4.656613e-10;
407437
}
408438
/* }}} */
409439

0 commit comments

Comments
 (0)