Skip to content

Commit 7ed6a34

Browse files
committed
random: Remove engine_combinedlcg.c
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 16d9bd0 commit 7ed6a34

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
@@ -276,6 +276,10 @@ PHP 8.4 INTERNALS UPGRADE NOTES
276276
implementation has been improved to generate better seeds, however any
277277
users should use the opportunity to verify that seeding is first
278278
attempted using the CSPRNG for better output size flexibility.
279+
- The standalone combined_lcg engine has been removed, as the lcg_value()
280+
userland function is deprecated and as the engine is unable to return
281+
unbiased integer values. The internal php_combined_lcg() function remains
282+
available for now.
279283

280284
c. ext/xsl
281285
- 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
@@ -395,14 +395,44 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
395395
/* {{{ php_combined_lcg */
396396
PHPAPI double php_combined_lcg(void)
397397
{
398-
php_random_status_state_combinedlcg *state = &RANDOM_G(combined_lcg);
398+
int32_t *state = RANDOM_G(combined_lcg);
399399

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

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

0 commit comments

Comments
 (0)