Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/random/engine_pcgoneseq128xslrr64.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Go Kudo <zeriyoshi@php.net>                                  |
12
   |                                                                      |
13
   | Based on code from: Melissa O'Neill <oneill@pcg-random.org>          |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#ifdef HAVE_CONFIG_H
18
# include "config.h"
19
#endif
20
21
#include "php.h"
22
#include "php_random.h"
23
#include "php_random_csprng.h"
24
#include "php_random_uint128.h"
25
26
#include "Zend/zend_exceptions.h"
27
28
static inline void step(php_random_status_state_pcgoneseq128xslrr64 *s)
29
4
{
30
4
  s->state = php_random_uint128_add(
31
4
    php_random_uint128_multiply(s->state, php_random_uint128_constant(2549297995355413924ULL,4865540595714422341ULL)),
32
4
    php_random_uint128_constant(6364136223846793005ULL,1442695040888963407ULL)
33
4
  );
34
4
}
35
36
PHPAPI inline void php_random_pcgoneseq128xslrr64_seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed)
37
2
{
38
2
  s->state = php_random_uint128_constant(0ULL, 0ULL);
39
2
  step(s);
40
2
  s->state = php_random_uint128_add(s->state, seed);
41
2
  step(s);
42
2
}
43
44
static php_random_result generate(void *state)
45
0
{
46
0
  php_random_status_state_pcgoneseq128xslrr64 *s = state;
47
48
0
  step(s);
49
50
0
  return (php_random_result){
51
0
    .size = sizeof(uint64_t),
52
0
    .result = php_random_pcgoneseq128xslrr64_rotr64(s->state),
53
0
  };
54
0
}
55
56
static zend_long range(void *state, zend_long min, zend_long max)
57
0
{
58
0
  return php_random_range((php_random_algo_with_state){
59
0
    .algo = &php_random_algo_pcgoneseq128xslrr64,
60
0
    .state = state,
61
0
  }, min, max);
62
0
}
63
64
static bool serialize(void *state, HashTable *data)
65
0
{
66
0
  php_random_status_state_pcgoneseq128xslrr64 *s = state;
67
0
  uint64_t u;
68
0
  zval z;
69
70
0
  u = php_random_uint128_hi(s->state);
71
0
  ZVAL_STR(&z, php_random_bin2hex_le(&u, sizeof(uint64_t)));
72
0
  zend_hash_next_index_insert(data, &z);
73
74
0
  u = php_random_uint128_lo(s->state);
75
0
  ZVAL_STR(&z, php_random_bin2hex_le(&u, sizeof(uint64_t)));
76
0
  zend_hash_next_index_insert(data, &z);
77
78
0
  return true;
79
0
}
80
81
static bool unserialize(void *state, HashTable *data)
82
0
{
83
0
  php_random_status_state_pcgoneseq128xslrr64 *s = state;
84
0
  uint64_t u[2];
85
0
  zval *t;
86
87
  /* Verify the expected number of elements, this implicitly ensures that no additional elements are present. */
88
0
  if (zend_hash_num_elements(data) != 2) {
89
0
    return false;
90
0
  }
91
92
0
  for (uint32_t i = 0; i < 2; i++) {
93
0
    t = zend_hash_index_find(data, i);
94
0
    if (!t || Z_TYPE_P(t) != IS_STRING || Z_STRLEN_P(t) != (2 * sizeof(uint64_t))) {
95
0
      return false;
96
0
    }
97
0
    if (!php_random_hex2bin_le(Z_STR_P(t), &u[i])) {
98
0
      return false;
99
0
    }
100
0
  }
101
0
  s->state = php_random_uint128_constant(u[0], u[1]);
102
103
0
  return true;
104
0
}
105
106
PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64 = {
107
  sizeof(php_random_status_state_pcgoneseq128xslrr64),
108
  generate,
109
  range,
110
  serialize,
111
  unserialize
112
};
113
114
/* {{{ php_random_pcgoneseq128xslrr64_advance */
115
PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance)
116
0
{
117
0
  php_random_uint128_t
118
0
    cur_mult = php_random_uint128_constant(2549297995355413924ULL,4865540595714422341ULL),
119
0
    cur_plus = php_random_uint128_constant(6364136223846793005ULL,1442695040888963407ULL),
120
0
    acc_mult = php_random_uint128_constant(0ULL, 1ULL),
121
0
    acc_plus = php_random_uint128_constant(0ULL, 0ULL);
122
123
0
  while (advance > 0) {
124
0
    if (advance & 1) {
125
0
      acc_mult = php_random_uint128_multiply(acc_mult, cur_mult);
126
0
      acc_plus = php_random_uint128_add(php_random_uint128_multiply(acc_plus, cur_mult), cur_plus);
127
0
    }
128
0
    cur_plus = php_random_uint128_multiply(php_random_uint128_add(cur_mult, php_random_uint128_constant(0ULL, 1ULL)), cur_plus);
129
0
    cur_mult = php_random_uint128_multiply(cur_mult, cur_mult);
130
0
    advance /= 2;
131
0
  }
132
133
0
  state->state = php_random_uint128_add(php_random_uint128_multiply(acc_mult, state->state), acc_plus);
134
0
}
135
/* }}} */
136
137
/* {{{ Random\Engine\PcgOneseq128XslRr64::__construct */
138
PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, __construct)
139
2
{
140
2
  php_random_algo_with_state engine = Z_RANDOM_ENGINE_P(ZEND_THIS)->engine;
141
2
  php_random_status_state_pcgoneseq128xslrr64 *state = engine.state;
142
2
  zend_string *str_seed = NULL;
143
2
  zend_long int_seed = 0;
144
2
  bool seed_is_null = true;
145
146
6
  ZEND_PARSE_PARAMETERS_START(0, 1)
147
6
    Z_PARAM_OPTIONAL;
148
6
    Z_PARAM_STR_OR_LONG_OR_NULL(str_seed, int_seed, seed_is_null);
149
4
  ZEND_PARSE_PARAMETERS_END();
150
151
2
  if (seed_is_null) {
152
2
    php_random_uint128_t s;
153
154
2
    if (php_random_bytes_throw(&s, sizeof(s)) == FAILURE) {
155
0
      zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
156
0
      RETURN_THROWS();
157
0
    }
158
159
2
    php_random_pcgoneseq128xslrr64_seed128(state, s);
160
2
  } else {
161
0
    if (str_seed) {
162
      /* char (byte: 8 bit) * 16 = 128 bits */
163
0
      if (ZSTR_LEN(str_seed) == 16) {
164
0
        uint64_t t[2];
165
166
        /* Endianness safe copy */
167
0
        for (uint32_t i = 0; i < 2; i++) {
168
0
          t[i] = 0;
169
0
          for (uint32_t j = 0; j < 8; j++) {
170
0
            t[i] += ((uint64_t) (unsigned char) ZSTR_VAL(str_seed)[(i * 8) + j]) << (j * 8);
171
0
          }
172
0
        }
173
174
0
        php_random_pcgoneseq128xslrr64_seed128(state, php_random_uint128_constant(t[0], t[1]));
175
0
      } else {
176
0
        zend_argument_value_error(1, "must be a 16 byte (128 bit) string");
177
0
        RETURN_THROWS();
178
0
      }
179
0
    } else {
180
0
      php_random_pcgoneseq128xslrr64_seed128(state, php_random_uint128_constant(0ULL, (uint64_t) int_seed));
181
0
    }
182
0
  }
183
2
}
184
/* }}} */
185
186
/* {{{ Random\Engine\PcgOneseq128XslRr64::jump() */
187
PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, jump)
188
0
{
189
0
  php_random_algo_with_state engine = Z_RANDOM_ENGINE_P(ZEND_THIS)->engine;
190
0
  php_random_status_state_pcgoneseq128xslrr64 *state = engine.state;
191
0
  zend_long advance = 0;
192
193
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
194
0
    Z_PARAM_LONG(advance);
195
0
  ZEND_PARSE_PARAMETERS_END();
196
197
0
  if (UNEXPECTED(advance < 0)) {
198
0
    zend_argument_value_error(1, "must be greater than or equal to 0");
199
0
    RETURN_THROWS();
200
0
  }
201
202
0
  php_random_pcgoneseq128xslrr64_advance(state, advance);
203
0
}
204
/* }}} */