Skip to content

random: Clean Up the Mt19937 state struct #13577

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 3 commits into from
Mar 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
22 changes: 13 additions & 9 deletions ext/random/engine_mt19937.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define N MT_N /* length of state vector */
#define N 624 /* length of state vector */
ZEND_STATIC_ASSERT(
N == sizeof(((php_random_status_state_mt19937*)0)->state) / sizeof(((php_random_status_state_mt19937*)0)->state[0]),
"Assumed length of Mt19937 state vector does not match actual size."
);
#define M (397) /* a period parameter */
#define hiBit(u) ((u) & 0x80000000U) /* mask all but highest bit of u */
#define loBit(u) ((u) & 0x00000001U) /* mask all but lowest bit of u */
Expand Down Expand Up @@ -130,7 +134,7 @@ PHPAPI inline void php_random_mt19937_seed32(php_random_status_state_mt19937 *st
In previous versions, most significant bits (MSBs) of the seed affect
only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto. */
state->state[0] = seed;
for (i = 1; i < MT_N; i++) {
for (i = 1; i < N; i++) {
prev_state = state->state[i - 1];
state->state[i] = (1812433253U * (prev_state ^ (prev_state >> 30)) + i) & 0xffffffffU;
}
Expand All @@ -144,7 +148,7 @@ static php_random_result generate(void *state)
php_random_status_state_mt19937 *s = state;
uint32_t s1;

if (s->count >= MT_N) {
if (s->count >= N) {
mt19937_reload(s);
}

Expand Down Expand Up @@ -172,7 +176,7 @@ static bool serialize(void *state, HashTable *data)
php_random_status_state_mt19937 *s = state;
zval t;

for (uint32_t i = 0; i < MT_N; i++) {
for (uint32_t i = 0; i < N; i++) {
ZVAL_STR(&t, php_random_bin2hex_le(&s->state[i], sizeof(uint32_t)));
zend_hash_next_index_insert(data, &t);
}
Expand All @@ -190,11 +194,11 @@ static bool unserialize(void *state, HashTable *data)
zval *t;

/* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
if (zend_hash_num_elements(data) != (MT_N + 2)) {
if (zend_hash_num_elements(data) != (N + 2)) {
return false;
}

for (uint32_t i = 0; i < MT_N; i++) {
for (uint32_t i = 0; i < N; 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;
Expand All @@ -203,16 +207,16 @@ static bool unserialize(void *state, HashTable *data)
return false;
}
}
t = zend_hash_index_find(data, MT_N);
t = zend_hash_index_find(data, N);
if (!t || Z_TYPE_P(t) != IS_LONG) {
return false;
}
s->count = Z_LVAL_P(t);
if (s->count > MT_N) {
if (s->count > N) {
return false;
}

t = zend_hash_index_find(data, MT_N + 1);
t = zend_hash_index_find(data, N + 1);
if (!t || Z_TYPE_P(t) != IS_LONG) {
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ PHPAPI double php_combined_lcg(void);

# define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */

# define MT_RAND_MT19937 0
# define MT_RAND_PHP 1

# define MT_N (624)
enum php_random_mt19937_mode {
MT_RAND_MT19937 = 0,
MT_RAND_PHP = 1,
};

#define PHP_RANDOM_RANGE_ATTEMPTS (50)

Expand All @@ -71,9 +71,9 @@ typedef struct _php_random_status_state_combinedlcg {
} php_random_status_state_combinedlcg;

typedef struct _php_random_status_state_mt19937 {
uint32_t state[MT_N];
uint32_t count;
uint8_t mode;
enum php_random_mt19937_mode mode;
uint32_t state[624];
} php_random_status_state_mt19937;

typedef struct _php_random_status_state_pcgoneseq128xslrr64 {
Expand Down