xref: /PHP-8.3/ext/random/php_random.h (revision d5484bf1)
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 #define PHP_RANDOM_RANGE_ATTEMPTS (50)
80 
81 PHPAPI void php_mt_srand(uint32_t seed);
82 PHPAPI uint32_t php_mt_rand(void);
83 PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
84 PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
85 
86 # ifndef RAND_MAX
87 #  define RAND_MAX PHP_MT_RAND_MAX
88 # endif
89 
90 # define PHP_RAND_MAX PHP_MT_RAND_MAX
91 
92 PHPAPI void php_srand(zend_long seed);
93 PHPAPI zend_long php_rand(void);
94 
95 # if !defined(__SIZEOF_INT128__) || defined(PHP_RANDOM_FORCE_EMULATE_128)
96 typedef struct _php_random_uint128_t {
97 	uint64_t hi;
98 	uint64_t lo;
99 } php_random_uint128_t;
100 
php_random_uint128_hi(php_random_uint128_t num)101 static inline uint64_t php_random_uint128_hi(php_random_uint128_t num)
102 {
103 	return num.hi;
104 }
105 
php_random_uint128_lo(php_random_uint128_t num)106 static inline uint64_t php_random_uint128_lo(php_random_uint128_t num)
107 {
108 	return num.lo;
109 }
110 
php_random_uint128_constant(uint64_t hi,uint64_t lo)111 static inline php_random_uint128_t php_random_uint128_constant(uint64_t hi, uint64_t lo)
112 {
113 	php_random_uint128_t r;
114 
115 	r.hi = hi;
116 	r.lo = lo;
117 
118 	return r;
119 }
120 
php_random_uint128_add(php_random_uint128_t num1,php_random_uint128_t num2)121 static inline php_random_uint128_t php_random_uint128_add(php_random_uint128_t num1, php_random_uint128_t num2)
122 {
123 	php_random_uint128_t r;
124 
125 	r.lo = (num1.lo + num2.lo);
126 	r.hi = (num1.hi + num2.hi + (r.lo < num1.lo));
127 
128 	return r;
129 }
130 
php_random_uint128_multiply(php_random_uint128_t num1,php_random_uint128_t num2)131 static inline php_random_uint128_t php_random_uint128_multiply(php_random_uint128_t num1, php_random_uint128_t num2)
132 {
133 	php_random_uint128_t r;
134 	const uint64_t
135 		x0 = num1.lo & 0xffffffffULL,
136 		x1 = num1.lo >> 32,
137 		y0 = num2.lo & 0xffffffffULL,
138 		y1 = num2.lo >> 32,
139 		z0 = (((x1 * y0) + (x0 * y0 >> 32)) & 0xffffffffULL) + x0 * y1;
140 
141 	r.hi = num1.hi * num2.lo + num1.lo * num2.hi;
142 	r.lo = num1.lo * num2.lo;
143 	r.hi += x1 * y1 + ((x1 * y0 + (x0 * y0 >> 32)) >> 32) + (z0 >> 32);
144 
145 	return r;
146 }
147 
php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)148 static inline uint64_t php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)
149 {
150 	const uint64_t
151 		v = (num.hi ^ num.lo),
152 		s = num.hi >> 58U;
153 
154 	return (v >> s) | (v << ((-s) & 63));
155 }
156 # else
157 typedef __uint128_t php_random_uint128_t;
158 
php_random_uint128_hi(php_random_uint128_t num)159 static inline uint64_t php_random_uint128_hi(php_random_uint128_t num)
160 {
161 	return (uint64_t) (num >> 64);
162 }
163 
php_random_uint128_lo(php_random_uint128_t num)164 static inline uint64_t php_random_uint128_lo(php_random_uint128_t num)
165 {
166 	return (uint64_t) num;
167 }
168 
php_random_uint128_constant(uint64_t hi,uint64_t lo)169 static inline php_random_uint128_t php_random_uint128_constant(uint64_t hi, uint64_t lo)
170 {
171 	php_random_uint128_t r;
172 
173 	r = ((php_random_uint128_t) hi << 64) + lo;
174 
175 	return r;
176 }
177 
php_random_uint128_add(php_random_uint128_t num1,php_random_uint128_t num2)178 static inline php_random_uint128_t php_random_uint128_add(php_random_uint128_t num1, php_random_uint128_t num2)
179 {
180 	return num1 + num2;
181 }
182 
php_random_uint128_multiply(php_random_uint128_t num1,php_random_uint128_t num2)183 static inline php_random_uint128_t php_random_uint128_multiply(php_random_uint128_t num1, php_random_uint128_t num2)
184 {
185 	return num1 * num2;
186 }
187 
php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)188 static inline uint64_t php_random_pcgoneseq128xslrr64_rotr64(php_random_uint128_t num)
189 {
190 	const uint64_t
191 		v = ((uint64_t) (num >> 64U)) ^ (uint64_t) num,
192 		s = num >> 122U;
193 
194 	return (v >> s) | (v << ((-s) & 63));
195 }
196 # endif
197 
198 PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw);
199 PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);
200 
php_random_bytes_throw(void * bytes,size_t size)201 static inline zend_result php_random_bytes_throw(void *bytes, size_t size)
202 {
203 	return php_random_bytes(bytes, size, true);
204 }
205 
php_random_bytes_silent(void * bytes,size_t size)206 static inline zend_result php_random_bytes_silent(void *bytes, size_t size)
207 {
208 	return php_random_bytes(bytes, size, false);
209 }
210 
php_random_int_throw(zend_long min,zend_long max,zend_long * result)211 static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result)
212 {
213 	return php_random_int(min, max, result, true);
214 }
215 
php_random_int_silent(zend_long min,zend_long max,zend_long * result)216 static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result)
217 {
218 	return php_random_int(min, max, result, false);
219 }
220 
221 typedef struct _php_random_status_ {
222 	size_t last_generated_size;
223 	void *state;
224 } php_random_status;
225 
226 typedef struct _php_random_status_state_combinedlcg {
227 	int32_t state[2];
228 } php_random_status_state_combinedlcg;
229 
230 typedef struct _php_random_status_state_mt19937 {
231 	uint32_t state[MT_N];
232 	uint32_t count;
233 	uint8_t mode;
234 } php_random_status_state_mt19937;
235 
236 typedef struct _php_random_status_state_pcgoneseq128xslrr64 {
237 	php_random_uint128_t state;
238 } php_random_status_state_pcgoneseq128xslrr64;
239 
240 typedef struct _php_random_status_state_xoshiro256starstar {
241 	uint64_t state[4];
242 } php_random_status_state_xoshiro256starstar;
243 
244 typedef struct _php_random_status_state_user {
245 	zend_object *object;
246 	zend_function *generate_method;
247 } php_random_status_state_user;
248 
249 typedef struct _php_random_algo {
250 	const size_t generate_size;
251 	const size_t state_size;
252 	void (*seed)(php_random_status *status, uint64_t seed);
253 	uint64_t (*generate)(php_random_status *status);
254 	zend_long (*range)(php_random_status *status, zend_long min, zend_long max);
255 	bool (*serialize)(php_random_status *status, HashTable *data);
256 	bool (*unserialize)(php_random_status *status, HashTable *data);
257 } php_random_algo;
258 
259 extern PHPAPI const php_random_algo php_random_algo_combinedlcg;
260 extern PHPAPI const php_random_algo php_random_algo_mt19937;
261 extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
262 extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
263 extern PHPAPI const php_random_algo php_random_algo_secure;
264 extern PHPAPI const php_random_algo php_random_algo_user;
265 
266 typedef struct _php_random_engine {
267 	const php_random_algo *algo;
268 	php_random_status *status;
269 	zend_object std;
270 } php_random_engine;
271 
272 typedef struct _php_random_randomizer {
273 	const php_random_algo *algo;
274 	php_random_status *status;
275 	bool is_userland_algo;
276 	zend_object std;
277 } php_random_randomizer;
278 
279 extern PHPAPI zend_class_entry *random_ce_Random_Engine;
280 extern PHPAPI zend_class_entry *random_ce_Random_CryptoSafeEngine;
281 
282 extern PHPAPI zend_class_entry *random_ce_Random_RandomError;
283 extern PHPAPI zend_class_entry *random_ce_Random_BrokenRandomEngineError;
284 extern PHPAPI zend_class_entry *random_ce_Random_RandomException;
285 
286 extern PHPAPI zend_class_entry *random_ce_Random_Engine_PcgOneseq128XslRr64;
287 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
288 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Xoshiro256StarStar;
289 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Secure;
290 
291 extern PHPAPI zend_class_entry *random_ce_Random_Randomizer;
292 
293 extern PHPAPI zend_class_entry *random_ce_Random_IntervalBoundary;
294 
php_random_engine_from_obj(zend_object * object)295 static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
296 	return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
297 }
298 
php_random_randomizer_from_obj(zend_object * object)299 static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
300 	return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
301 }
302 
303 # define Z_RANDOM_ENGINE_P(zval) php_random_engine_from_obj(Z_OBJ_P(zval))
304 
305 # define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
306 
307 PHPAPI php_random_status *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
308 PHPAPI php_random_status *php_random_status_copy(const php_random_algo *algo, php_random_status *old_status, php_random_status *new_status);
309 PHPAPI void php_random_status_free(php_random_status *status, const bool persistent);
310 PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo);
311 PHPAPI void php_random_engine_common_free_object(zend_object *object);
312 PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
313 PHPAPI uint32_t php_random_range32(const php_random_algo *algo, php_random_status *status, uint32_t umax);
314 PHPAPI uint64_t php_random_range64(const php_random_algo *algo, php_random_status *status, uint64_t umax);
315 PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max);
316 PHPAPI const php_random_algo *php_random_default_algo(void);
317 PHPAPI php_random_status *php_random_default_status(void);
318 
319 PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
320 PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
321 
322 PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state);
323 
324 PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
325 
326 PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance);
327 
328 PHPAPI void php_random_xoshiro256starstar_jump(php_random_status_state_xoshiro256starstar *state);
329 PHPAPI void php_random_xoshiro256starstar_jump_long(php_random_status_state_xoshiro256starstar *state);
330 
331 PHPAPI double php_random_gammasection_closed_open(const php_random_algo *algo, php_random_status *status, double min, double max);
332 PHPAPI double php_random_gammasection_closed_closed(const php_random_algo *algo, php_random_status *status, double min, double max);
333 PHPAPI double php_random_gammasection_open_closed(const php_random_algo *algo, php_random_status *status, double min, double max);
334 PHPAPI double php_random_gammasection_open_open(const php_random_algo *algo, php_random_status *status, double min, double max);
335 
336 extern zend_module_entry random_module_entry;
337 # define phpext_random_ptr &random_module_entry
338 
339 PHP_MINIT_FUNCTION(random);
340 PHP_MSHUTDOWN_FUNCTION(random);
341 PHP_RINIT_FUNCTION(random);
342 
343 ZEND_BEGIN_MODULE_GLOBALS(random)
344 	php_random_status *combined_lcg;
345 	bool combined_lcg_seeded;
346 	php_random_status *mt19937;
347 	bool mt19937_seeded;
348 	int random_fd;
349 ZEND_END_MODULE_GLOBALS(random)
350 
351 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
352 
353 # define RANDOM_G(v)	ZEND_MODULE_GLOBALS_ACCESSOR(random, v)
354 
355 #endif	/* PHP_RANDOM_H */
356