xref: /php-src/ext/random/engine_combinedlcg.c (revision 7c851042)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Sascha Schumann <sascha@schumann.cx>                        |
14    |          Go Kudo <zeriyoshi@php.net>                                 |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 
22 #include "php.h"
23 #include "php_random.h"
24 
25 #include "Zend/zend_exceptions.h"
26 
27 /*
28  * combinedLCG() returns a pseudo random number in the range of (0, 1).
29  * The function combines two CGs with periods of
30  * 2^31 - 85 - 1 and 2^31 - 249 - 1. The period of this function
31  * is equal to the product of the two underlying periods, divided
32  * by factors shared by the underlying periods, i.e. 2.3 * 10^18.
33  *
34  * see: https://library.sciencemadness.org/lanl1_a/lib-www/numerica/f7-1.pdf
35  */
36 #define MODMULT(a, b, c, m, s) q = s / a; s = b * (s - a * q) - c * q; if (s < 0) s += m
37 
php_random_combinedlcg_seed64(php_random_status_state_combinedlcg * state,uint64_t seed)38 PHPAPI void php_random_combinedlcg_seed64(php_random_status_state_combinedlcg *state, uint64_t seed)
39 {
40 	state->state[0] = seed & 0xffffffffU;
41 	state->state[1] = seed >> 32;
42 }
43 
generate(void * state)44 static php_random_result generate(void *state)
45 {
46 	php_random_status_state_combinedlcg *s = state;
47 	int32_t q, z;
48 
49 	/* s->state[0] = (s->state[0] * 40014) % 2147483563; */
50 	MODMULT(53668, 40014, 12211, 2147483563L, s->state[0]);
51 	/* s->state[1] = (s->state[1] * 40692) % 2147483399; */
52 	MODMULT(52774, 40692, 3791, 2147483399L, s->state[1]);
53 
54 	z = s->state[0] - s->state[1];
55 	if (z < 1) {
56 		z += 2147483562;
57 	}
58 
59 	return (php_random_result){
60 		.size = sizeof(uint32_t),
61 		.result = (uint64_t) z,
62 	};
63 }
64 
range(void * state,zend_long min,zend_long max)65 static zend_long range(void *state, zend_long min, zend_long max)
66 {
67 	return php_random_range((php_random_algo_with_state){
68 		.algo = &php_random_algo_combinedlcg,
69 		.state = state,
70 	}, min, max);
71 }
72 
serialize(void * state,HashTable * data)73 static bool serialize(void *state, HashTable *data)
74 {
75 	php_random_status_state_combinedlcg *s = state;
76 	zval t;
77 
78 	for (uint32_t i = 0; i < 2; i++) {
79 		ZVAL_STR(&t, php_random_bin2hex_le(&s->state[i], sizeof(uint32_t)));
80 		zend_hash_next_index_insert(data, &t);
81 	}
82 
83 	return true;
84 }
85 
unserialize(void * state,HashTable * data)86 static bool unserialize(void *state, HashTable *data)
87 {
88 	php_random_status_state_combinedlcg *s = state;
89 	zval *t;
90 
91 	for (uint32_t i = 0; i < 2; i++) {
92 		t = zend_hash_index_find(data, i);
93 		if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint32_t))) {
94 			return false;
95 		}
96 		if (!php_random_hex2bin_le(Z_STR_P(t), &s->state[i])) {
97 			return false;
98 		}
99 	}
100 
101 	return true;
102 }
103 
104 const php_random_algo php_random_algo_combinedlcg = {
105 	sizeof(php_random_status_state_combinedlcg),
106 	generate,
107 	range,
108 	serialize,
109 	unserialize
110 };
111 
112 /* {{{ php_random_combinedlcg_seed_default */
php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg * state)113 PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state)
114 {
115 	uint64_t seed = 0;
116 
117 	if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
118 		seed = php_random_generate_fallback_seed();
119 	}
120 
121 	php_random_combinedlcg_seed64(state, seed);
122 }
123 /* }}} */
124