|
2 | 2 | import numpy as np
|
3 | 3 |
|
4 | 4 | from randomgen.common cimport *
|
5 |
| -from randomgen.entropy import random_entropy, seed_by_array |
6 | 5 |
|
7 | 6 | __all__ = ["ChaCha"]
|
8 | 7 |
|
@@ -222,27 +221,14 @@ cdef class ChaCha(BitGenerator):
|
222 | 221 | array[i] = (value // 2**(64*i)) % 2**64.
|
223 | 222 | """
|
224 | 223 | if seed is not None and key is not None:
|
225 |
| - raise ValueError("seed and key cannot be both used") |
| 224 | + raise ValueError("seed and key cannot be simultaneously used") |
226 | 225 | if key is None:
|
227 | 226 | BitGenerator._seed_with_seed_sequence(self, seed, counter=counter)
|
228 |
| - try: |
229 |
| - if self.seed_seq is not None: |
230 |
| - return |
231 |
| - except AttributeError: |
232 |
| - if self._seed_seq is not None: |
233 |
| - return |
234 |
| - |
235 |
| - seed = object_to_int(seed, 256, "seed") |
| 227 | + return |
| 228 | + |
236 | 229 | key = object_to_int(key, 256, "key")
|
237 | 230 | counter = object_to_int(counter, 128, "counter")
|
238 |
| - if seed is not None and key is not None: |
239 |
| - raise ValueError("seed and key cannot be simultaneously used") |
240 |
| - if key is not None: |
241 |
| - seed = int_to_array(key, "key", 256, 32) |
242 |
| - elif seed is not None: |
243 |
| - seed = seed_by_array(int_to_array(seed, "seed", None, 64), 4) |
244 |
| - else: |
245 |
| - seed = random_entropy(8, "auto") |
| 231 | + seed = int_to_array(key, "key", 256, 32) |
246 | 232 | _seed = seed
|
247 | 233 | if _seed.dtype != np.uint64:
|
248 | 234 | _seed = view_little_endian(_seed, np.uint64)
|
@@ -301,112 +287,3 @@ cdef class ChaCha(BitGenerator):
|
301 | 287 | for i in range(2):
|
302 | 288 | self.rng_state.ctr[i] = ctr[i]
|
303 | 289 | self.rng_state.rounds = state["rounds"]
|
304 |
| - |
305 |
| - cdef jump_inplace(self, object iter): |
306 |
| - """ |
307 |
| - Jump state in-place |
308 |
| -
|
309 |
| - Not part of public API |
310 |
| -
|
311 |
| - Parameters |
312 |
| - ---------- |
313 |
| - iter : integer, positive |
314 |
| - Number of times to jump the state of the rng. |
315 |
| - """ |
316 |
| - self.advance(iter * int(2 ** 64)) |
317 |
| - |
318 |
| - def jump(self, iter=1): |
319 |
| - """ |
320 |
| - jump(iter=1) |
321 |
| -
|
322 |
| - Jumps the state as-if 2**64 random numbers have been generated. |
323 |
| -
|
324 |
| - Parameters |
325 |
| - ---------- |
326 |
| - iter : integer, positive |
327 |
| - Number of times to jump the state of the rng. |
328 |
| -
|
329 |
| - Returns |
330 |
| - ------- |
331 |
| - self : ChaCha |
332 |
| - PRNG jumped iter times |
333 |
| -
|
334 |
| - Notes |
335 |
| - ----- |
336 |
| - Jumping the rng state resets any pre-computed random numbers. This is |
337 |
| - required to ensure exact reproducibility. |
338 |
| - """ |
339 |
| - import warnings |
340 |
| - warnings.warn("jump (in-place) has been deprecated in favor of jumped" |
341 |
| - ", which returns a new instance", DeprecationWarning) |
342 |
| - self.jump_inplace(iter) |
343 |
| - return self |
344 |
| - |
345 |
| - def jumped(self, iter=1): |
346 |
| - """ |
347 |
| - jumped(iter=1) |
348 |
| -
|
349 |
| - Returns a new bit generator with the state jumped |
350 |
| -
|
351 |
| - The state of the returned big generator is jumped as-if |
352 |
| - 2**(64 * iter) random numbers have been generated. |
353 |
| -
|
354 |
| - Parameters |
355 |
| - ---------- |
356 |
| - iter : integer, positive |
357 |
| - Number of times to jump the state of the bit generator returned |
358 |
| -
|
359 |
| - Returns |
360 |
| - ------- |
361 |
| - bit_generator : ChaCha |
362 |
| - New instance of generator jumped iter times |
363 |
| - """ |
364 |
| - cdef ChaCha bit_generator |
365 |
| - |
366 |
| - bit_generator = self.__class__(seed=self._copy_seed()) |
367 |
| - bit_generator.state = self.state |
368 |
| - bit_generator.jump_inplace(iter) |
369 |
| - |
370 |
| - return bit_generator |
371 |
| - |
372 |
| - def advance(self, delta): |
373 |
| - """ |
374 |
| - advance(delta) |
375 |
| -
|
376 |
| - Advance the underlying RNG as-if delta draws have occurred. |
377 |
| -
|
378 |
| - Parameters |
379 |
| - ---------- |
380 |
| - delta : integer, positive |
381 |
| - Number of draws to advance the RNG. |
382 |
| -
|
383 |
| - Returns |
384 |
| - ------- |
385 |
| - self : ChaCha |
386 |
| - RNG advanced delta steps |
387 |
| -
|
388 |
| - Notes |
389 |
| - ----- |
390 |
| - Advancing a RNG updates the underlying RNG state as-if a given |
391 |
| - number of calls to the underlying RNG have been made. In general |
392 |
| - there is not a one-to-one relationship between the number output |
393 |
| - random values from a particular distribution and the number of |
394 |
| - draws from the core RNG. This occurs for two reasons: |
395 |
| -
|
396 |
| - * The random values are simulated using a rejection-based method |
397 |
| - and so, on average, more than one value from the underlying |
398 |
| - RNG is required to generate an single draw. |
399 |
| - * The number of bits required to generate a simulated value |
400 |
| - differs from the number of bits generated by the underlying |
401 |
| - RNG. For example, two 16-bit integer values can be simulated |
402 |
| - from a single draw of a 32-bit RNG. |
403 |
| -
|
404 |
| - Advancing the RNG state resets any pre-computed random numbers. |
405 |
| - This is required to ensure exact reproducibility. |
406 |
| - """ |
407 |
| - cdef np.ndarray step |
408 |
| - |
409 |
| - delta = wrap_int(delta, 128) |
410 |
| - step = int_to_array(delta, "delta", 128, 64) |
411 |
| - chacha_advance(self.rng_state, <uint64_t *>np.PyArray_DATA(step)) |
412 |
| - return self |
0 commit comments