xref: /PHP-8.2/ext/random/php_random.h (revision 22040f5a)
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: Rasmus Lerdorf <rasmus@php.net>                             |
14    |          Zeev Suraski <zeev@php.net>                                 |
15    |          Sascha Schumann <sascha@schumann.cx>                        |
16    |          Pedro Melo <melo@ip.pt>                                     |
17    |          Sterling Hughes <sterling@php.net>                          |
18    |          Sammy Kaye Powers <me@sammyk.me>                            |
19    |          Go Kudo <zeriyoshi@php.net>                                 |
20    |                                                                      |
21    | Based on code from: Richard J. Wagner <rjwagner@writeme.com>         |
22    |                     Makoto Matsumoto <matumoto@math.keio.ac.jp>      |
23    |                     Takuji Nishimura                                 |
24    |                     Shawn Cokus <Cokus@math.washington.edu>          |
25    |                     David Blackman                                   |
26    |                     Sebastiano Vigna <vigna@acm.org>                 |
27    |                     Melissa O'Neill <oneill@pcg-random.org>          |
28    +----------------------------------------------------------------------+
29 */
30 
31 #ifndef PHP_RANDOM_H
32 # define PHP_RANDOM_H
33 
34 # include "php.h"
35 
36 PHPAPI double php_combined_lcg(void);
37 
38 /*
39  * A bit of tricky math here.  We want to avoid using a modulus because
40  * that simply tosses the high-order bits and might skew the distribution
41  * of random values over the range.  Instead we map the range directly.
42  *
43  * We need to map the range from 0...M evenly to the range a...b
44  * Let n = the random number and n' = the mapped random number
45  *
46  * Then we have: n' = a + n(b-a)/M
47  *
48  * We have a problem here in that only n==M will get mapped to b which
49  * means the chances of getting b is much much less than getting any of
50  * the other values in the range.  We can fix this by increasing our range
51  * artificially and using:
52  *
53  *               n' = a + n(b-a+1)/M
54  *
55  * Now we only have a problem if n==M which would cause us to produce a
56  * number of b+1 which would be bad.  So we bump M up by one to make sure
57  * this will never happen, and the final algorithm looks like this:
58  *
59  *               n' = a + n(b-a+1)/(M+1)
60  *
61  * -RL
62  */
63 # define RAND_RANGE_BADSCALING(__n, __min, __max, __tmax) \
64 	(__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
65 
66 # ifdef PHP_WIN32
67 #  define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
68 # else
69 #  define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
70 # endif
71 
72 # define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
73 
74 # define MT_RAND_MT19937 0
75 # define MT_RAND_PHP 1
76 
77 # define MT_N (624)
78 
79 PHPAPI void php_mt_srand(uint32_t seed);
80 PHPAPI uint32_t php_mt_rand(void);
81 PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
82 PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
83 
84 # ifndef RAND_MAX
85 #  define RAND_MAX PHP_MT_RAND_MAX
86 # endif
87 
88 # define PHP_RAND_MAX PHP_MT_RAND_MAX
89 
90 PHPAPI void php_srand(zend_long seed);
91 PHPAPI zend_long php_rand(void);
92 
93 # if !defined(__SIZEOF_INT128__) || defined(PHP_RANDOM_FORCE_EMULATE_128)
94 typedef struct _php_random_uint128_t {
95 	uint64_t hi;
96 	uint64_t lo;
97 } php_random_uint128_t;
98 
php_random_uint128_hi(php_random_uint128_t num)99 static inline uint64_t php_random_uint128_hi(php_random_uint128_t num)
100 {
101 	return num.hi;
102 }
103 
php_random_uint128_lo(php_random_uint128_t num)104 static inline uint64_t php_random_uint128_lo(php_random_uint128_t num)
105 {
106 	return num.lo;
107 }
108 
php_random_uint128_constant(uint64_t hi,uint64_t lo)109 static inline php_random_uint128_t php_random_uint128_constant(uint64_t hi, uint64_t lo)
110 {
111 	php_random_uint128_t r;
112 
113 	r.hi = hi;
114 	r.lo = lo;
115 
116 	return r;
117 }
118 
php_random_uint128_add(php_random_uint128_t num1,php_random_uint128_t num2)119 static inline php_random_uint128_t php_random_uint128_add(php_random_uint128_t num1, php_random_uint128_t num2)
120 {
121 	php_random_uint128_t r;
122 
123 	r.lo = (num1.lo + num2.lo);
124 	r.hi = (num1.hi + num2.hi + (r.lo < num1.lo));
125 
126 	return r;
127 }
128 
php_random_uint128_multiply(php_random_uint128_t num1,php_random_uint128_t num2)129 static inline php_random_uint128_t php_random_uint128_multiply(php_random_uint128_t num1, php_random_uint128_t num2)
130 {
131 	php_random_uint128_t r;
132 	const uint64_t
133 		x0 = num1.lo & 0xffffffffULL,
134 		x1 = num1.lo >> 32,
135 		y0 = num2.lo & 0xffffffffULL,
136 		y1 = num2.lo >> 32,
137 		z0 = (((x1 * y0) + (x0 * y0 >> 32)) & 0xffffffffULL) + x0 * y1;
138 
139 	r.hi = num1.hi * num2.lo + num1.lo * num2.hi;
140 	r.lo = num1.lo * num2.lo;
141 	r.hi += x1 * y1 + ((x1 * y0 + (x0 * y0 >> 32)) >> 32) + (z0 >> 32);
142 
143 	return r;
144 }
145 
php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)146 static inline uint64_t php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)
147 {
148 	const uint64_t
149 		v = (num.hi ^ num.lo),
150 		s = num.hi >> 58U;
151 
152 	return (v >> s) | (v << ((-s) & 63));
153 }
154 # else
155 typedef __uint128_t php_random_uint128_t;
156 
php_random_uint128_hi(php_random_uint128_t num)157 static inline uint64_t php_random_uint128_hi(php_random_uint128_t num)
158 {
159 	return (uint64_t) (num >> 64);
160 }
161 
php_random_uint128_lo(php_random_uint128_t num)162 static inline uint64_t php_random_uint128_lo(php_random_uint128_t num)
163 {
164 	return (uint64_t) num;
165 }
166 
php_random_uint128_constant(uint64_t hi,uint64_t lo)167 static inline php_random_uint128_t php_random_uint128_constant(uint64_t hi, uint64_t lo)
168 {
169 	php_random_uint128_t r;
170 
171 	r = ((php_random_uint128_t) hi << 64) + lo;
172 
173 	return r;
174 }
175 
php_random_uint128_add(php_random_uint128_t num1,php_random_uint128_t num2)176 static inline php_random_uint128_t php_random_uint128_add(php_random_uint128_t num1, php_random_uint128_t num2)
177 {
178 	return num1 + num2;
179 }
180 
php_random_uint128_multiply(php_random_uint128_t num1,php_random_uint128_t num2)181 static inline php_random_uint128_t php_random_uint128_multiply(php_random_uint128_t num1, php_random_uint128_t num2)
182 {
183 	return num1 * num2;
184 }
185 
php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)186 static inline uint64_t php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)
187 {
188 	const uint64_t
189 		v = ((uint64_t) (num >> 64U)) ^ (uint64_t) num,
190 		s = num >> 122U;
191 
192 	return (v >> s) | (v << ((-s) & 63));
193 }
194 # endif
195 
196 # define php_random_bytes_throw(b, s) php_random_bytes((b), (s), 1)
197 # define php_random_bytes_silent(b, s) php_random_bytes((b), (s), 0)
198 # define php_random_int_throw(min, max, result) php_random_int((min), (max), (result), 1)
199 # define php_random_int_silent(min, max, result) php_random_int((min), (max), (result), 0)
200 
201 PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw);
202 PHPAPI int php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);
203 
204 typedef struct _php_random_status_ {
205 	size_t last_generated_size;
206 	void *state;
207 } php_random_status;
208 
209 typedef struct _php_random_status_state_combinedlcg {
210 	int32_t state[2];
211 } php_random_status_state_combinedlcg;
212 
213 typedef struct _php_random_status_state_mt19937 {
214 	uint32_t state[MT_N];
215 	uint32_t count;
216 	uint8_t mode;
217 } php_random_status_state_mt19937;
218 
219 typedef struct _php_random_status_state_pcgoneseq128xslrr64 {
220 	php_random_uint128_t state;
221 } php_random_status_state_pcgoneseq128xslrr64;
222 
223 typedef struct _php_random_status_state_xoshiro256starstar {
224 	uint64_t state[4];
225 } php_random_status_state_xoshiro256starstar;
226 
227 typedef struct _php_random_status_state_user {
228 	zend_object *object;
229 	zend_function *generate_method;
230 } php_random_status_state_user;
231 
232 typedef struct _php_random_algo {
233 	const size_t generate_size;
234 	const size_t state_size;
235 	void (*seed)(php_random_status *status, uint64_t seed);
236 	uint64_t (*generate)(php_random_status *status);
237 	zend_long (*range)(php_random_status *status, zend_long min, zend_long max);
238 	bool (*serialize)(php_random_status *status, HashTable *data);
239 	bool (*unserialize)(php_random_status *status, HashTable *data);
240 } php_random_algo;
241 
242 extern PHPAPI const php_random_algo php_random_algo_combinedlcg;
243 extern PHPAPI const php_random_algo php_random_algo_mt19937;
244 extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
245 extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
246 extern PHPAPI const php_random_algo php_random_algo_secure;
247 extern PHPAPI const php_random_algo php_random_algo_user;
248 
249 typedef struct _php_random_engine {
250 	const php_random_algo *algo;
251 	php_random_status *status;
252 	zend_object std;
253 } php_random_engine;
254 
255 typedef struct _php_random_randomizer {
256 	const php_random_algo *algo;
257 	php_random_status *status;
258 	bool is_userland_algo;
259 	zend_object std;
260 } php_random_randomizer;
261 
262 extern PHPAPI zend_class_entry *random_ce_Random_Engine;
263 extern PHPAPI zend_class_entry *random_ce_Random_CryptoSafeEngine;
264 
265 extern PHPAPI zend_class_entry *random_ce_Random_RandomError;
266 extern PHPAPI zend_class_entry *random_ce_Random_BrokenRandomEngineError;
267 extern PHPAPI zend_class_entry *random_ce_Random_RandomException;
268 
269 extern PHPAPI zend_class_entry *random_ce_Random_Engine_PcgOneseq128XslRr64;
270 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
271 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Xoshiro256StarStar;
272 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Secure;
273 extern PHPAPI zend_class_entry *random_ce_Random_Randomizer;
274 
php_random_engine_from_obj(zend_object * object)275 static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
276 	return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
277 }
278 
php_random_randomizer_from_obj(zend_object * object)279 static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
280 	return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
281 }
282 
283 # define Z_RANDOM_ENGINE_P(zval) php_random_engine_from_obj(Z_OBJ_P(zval))
284 
285 # define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
286 
287 PHPAPI php_random_status *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
288 PHPAPI php_random_status *php_random_status_copy(const php_random_algo *algo, php_random_status *old_status, php_random_status *new_status);
289 PHPAPI void php_random_status_free(php_random_status *status, const bool persistent);
290 PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo);
291 PHPAPI void php_random_engine_common_free_object(zend_object *object);
292 PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
293 PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max);
294 PHPAPI const php_random_algo *php_random_default_algo(void);
295 PHPAPI php_random_status *php_random_default_status(void);
296 
297 PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
298 PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
299 
300 PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state);
301 
302 PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
303 
304 PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance);
305 
306 PHPAPI void php_random_xoshiro256starstar_jump(php_random_status_state_xoshiro256starstar *state);
307 PHPAPI void php_random_xoshiro256starstar_jump_long(php_random_status_state_xoshiro256starstar *state);
308 
309 extern zend_module_entry random_module_entry;
310 # define phpext_random_ptr &random_module_entry
311 
312 PHP_MINIT_FUNCTION(random);
313 PHP_MSHUTDOWN_FUNCTION(random);
314 PHP_RINIT_FUNCTION(random);
315 
316 ZEND_BEGIN_MODULE_GLOBALS(random)
317 	php_random_status *combined_lcg;
318 	bool combined_lcg_seeded;
319 	php_random_status *mt19937;
320 	bool mt19937_seeded;
321 	int random_fd;
322 ZEND_END_MODULE_GLOBALS(random)
323 
324 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
325 
326 # define RANDOM_G(v)	ZEND_MODULE_GLOBALS_ACCESSOR(random, v)
327 
328 #endif	/* PHP_RANDOM_H */
329