xref: /php-src/ext/random/php_random.h (revision 81744d6c)
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 # include "php_random_csprng.h"
36 # include "php_random_uint128.h"
37 
38 PHPAPI double php_combined_lcg(void);
39 
40 PHPAPI uint64_t php_random_generate_fallback_seed(void);
41 
GENERATE_SEED(void)42 static inline zend_long GENERATE_SEED(void)
43 {
44 	return (zend_long)php_random_generate_fallback_seed();
45 }
46 
47 # define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
48 
49 enum php_random_mt19937_mode {
50 	MT_RAND_MT19937 = 0,
51 	MT_RAND_PHP = 1,
52 };
53 
54 #define PHP_RANDOM_RANGE_ATTEMPTS (50)
55 
56 PHPAPI void php_mt_srand(uint32_t seed);
57 PHPAPI uint32_t php_mt_rand(void);
58 PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
59 PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
60 
61 # ifndef RAND_MAX
62 #  define RAND_MAX PHP_MT_RAND_MAX
63 # endif
64 
65 # define PHP_RAND_MAX PHP_MT_RAND_MAX
66 
67 PHPAPI void php_srand(zend_long seed);
68 PHPAPI zend_long php_rand(void);
69 
70 typedef struct _php_random_status_state_combinedlcg {
71 	int32_t state[2];
72 } php_random_status_state_combinedlcg;
73 
74 typedef struct _php_random_status_state_mt19937 {
75 	uint32_t count;
76 	enum php_random_mt19937_mode mode;
77 	uint32_t state[624];
78 } php_random_status_state_mt19937;
79 
80 typedef struct _php_random_status_state_pcgoneseq128xslrr64 {
81 	php_random_uint128_t state;
82 } php_random_status_state_pcgoneseq128xslrr64;
83 
84 typedef struct _php_random_status_state_xoshiro256starstar {
85 	uint64_t state[4];
86 } php_random_status_state_xoshiro256starstar;
87 
88 typedef struct _php_random_status_state_user {
89 	zend_object *object;
90 	zend_function *generate_method;
91 } php_random_status_state_user;
92 
93 typedef struct _php_random_result {
94 	const uint64_t result;
95 	const size_t size;
96 } php_random_result;
97 
98 typedef struct _php_random_algo {
99 	const size_t state_size;
100 	php_random_result (*generate)(void *state);
101 	zend_long (*range)(void *state, zend_long min, zend_long max);
102 	bool (*serialize)(void *state, HashTable *data);
103 	bool (*unserialize)(void *state, HashTable *data);
104 } php_random_algo;
105 
106 typedef struct _php_random_algo_with_state {
107 	const php_random_algo *algo;
108 	void *state;
109 } php_random_algo_with_state;
110 
111 extern PHPAPI const php_random_algo php_random_algo_combinedlcg;
112 extern PHPAPI const php_random_algo php_random_algo_mt19937;
113 extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
114 extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
115 extern PHPAPI const php_random_algo php_random_algo_secure;
116 extern PHPAPI const php_random_algo php_random_algo_user;
117 
118 typedef struct _php_random_engine {
119 	php_random_algo_with_state engine;
120 	zend_object std;
121 } php_random_engine;
122 
123 typedef struct _php_random_randomizer {
124 	php_random_algo_with_state engine;
125 	bool is_userland_algo;
126 	zend_object std;
127 } php_random_randomizer;
128 
129 extern PHPAPI zend_class_entry *random_ce_Random_Engine;
130 extern PHPAPI zend_class_entry *random_ce_Random_CryptoSafeEngine;
131 
132 extern PHPAPI zend_class_entry *random_ce_Random_RandomError;
133 extern PHPAPI zend_class_entry *random_ce_Random_BrokenRandomEngineError;
134 extern PHPAPI zend_class_entry *random_ce_Random_RandomException;
135 
136 extern PHPAPI zend_class_entry *random_ce_Random_Engine_PcgOneseq128XslRr64;
137 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
138 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Xoshiro256StarStar;
139 extern PHPAPI zend_class_entry *random_ce_Random_Engine_Secure;
140 
141 extern PHPAPI zend_class_entry *random_ce_Random_Randomizer;
142 
143 extern PHPAPI zend_class_entry *random_ce_Random_IntervalBoundary;
144 
php_random_engine_from_obj(zend_object * object)145 static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
146 	return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
147 }
148 
php_random_randomizer_from_obj(zend_object * object)149 static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
150 	return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
151 }
152 
153 # define Z_RANDOM_ENGINE_P(zval) php_random_engine_from_obj(Z_OBJ_P(zval))
154 
155 # define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
156 
157 PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
158 PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status);
159 PHPAPI void php_random_status_free(void *status, const bool persistent);
160 PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo);
161 PHPAPI void php_random_engine_common_free_object(zend_object *object);
162 PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
163 PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t umax);
164 PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t umax);
165 PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max);
166 PHPAPI const php_random_algo *php_random_default_algo(void);
167 PHPAPI void *php_random_default_status(void);
168 
php_random_default_engine(void)169 static inline php_random_algo_with_state php_random_default_engine(void)
170 {
171 	return (php_random_algo_with_state){
172 		.algo = php_random_default_algo(),
173 		.state = php_random_default_status(),
174 	};
175 }
176 
177 PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
178 PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
179 
180 PHPAPI void php_random_combinedlcg_seed64(php_random_status_state_combinedlcg *state, uint64_t seed);
181 PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state);
182 
183 PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed);
184 PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
185 
186 PHPAPI void php_random_pcgoneseq128xslrr64_seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed);
187 PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance);
188 
189 PHPAPI void php_random_xoshiro256starstar_seed64(php_random_status_state_xoshiro256starstar *state, uint64_t seed);
190 PHPAPI void php_random_xoshiro256starstar_seed256(php_random_status_state_xoshiro256starstar *state, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3);
191 PHPAPI void php_random_xoshiro256starstar_jump(php_random_status_state_xoshiro256starstar *state);
192 PHPAPI void php_random_xoshiro256starstar_jump_long(php_random_status_state_xoshiro256starstar *state);
193 
194 PHPAPI double php_random_gammasection_closed_open(php_random_algo_with_state engine, double min, double max);
195 PHPAPI double php_random_gammasection_closed_closed(php_random_algo_with_state engine, double min, double max);
196 PHPAPI double php_random_gammasection_open_closed(php_random_algo_with_state engine, double min, double max);
197 PHPAPI double php_random_gammasection_open_open(php_random_algo_with_state engine, double min, double max);
198 
199 extern zend_module_entry random_module_entry;
200 # define phpext_random_ptr &random_module_entry
201 
202 PHP_MINIT_FUNCTION(random);
203 PHP_MSHUTDOWN_FUNCTION(random);
204 PHP_RINIT_FUNCTION(random);
205 
206 ZEND_BEGIN_MODULE_GLOBALS(random)
207 	int random_fd;
208 	bool combined_lcg_seeded;
209 	bool mt19937_seeded;
210 	bool fallback_seed_initialized;
211 	unsigned char fallback_seed[20];
212 	php_random_status_state_combinedlcg combined_lcg;
213 	php_random_status_state_mt19937 mt19937;
214 ZEND_END_MODULE_GLOBALS(random)
215 
216 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
217 
218 # define RANDOM_G(v)	ZEND_MODULE_GLOBALS_ACCESSOR(random, v)
219 
220 #endif	/* PHP_RANDOM_H */
221