CyclicPRNG
A cyclical PRNG implementation that can provably generate every number in the cycle exactly once before restarting. For more information on why this was built and how it works, see the corresponding natlas documentation.
Mathematical Background
In short, CyclicPRNG uses a multiplicative cyclic group of prime order, and computes the factorization of the order of the group to obtain possible subgroup orders. CyclicPRNG uses these subgroup orders for generator testing, and finds random generators. CyclicPRNG uses these generators to determine the order to traverse the cycle.
Installation
pipenv install cyclicprng
Usage
Initializing a Reordering PRNG
from cyclicprng import CyclicPRNG
cycle_size = 10
c = CyclicPRNG(cycle_size)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert first_cycle != second_cycle
Initializing a Consistent PRNG
from cyclicprng import CyclicPRNG
cycle_size = 10
c = CyclicPRNG(cycle_size, consistent=True)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert first_cycle == second_cycle
Registering Event Handlers
CyclicPRNG allows you to register event handlers to receive messages when the PRNG restarts. Optionally, an event handler could be provided at initialization to also receive an init event.
The two events emitted by CyclicPRNG are:
initrestart
>>> from cyclicprng import CyclicPRNG
>>> cycle_size = 5
>>> c = CyclicPRNG(cycle_size, event_handler=print)
init
>>> for i in range(cycle_size):
... print(c.get_random())
...
2
3
1
5
restart
4
>>> c.unregister_event_handler(print)
>>> c.notify
[]
>>> c.register_event_handler(print)
>>> c.notify
[<built-in function print>]
>>> c.clear_event_handlers()
>>> c.notify
[]
You may notice that the restart event triggers before the last value is returned. This happens because the cycle automatically restarts when it reaches the final value in the cycle, right before that value is returned to the caller.
Edge Cases
The following edge cases exist for CyclicPRNG:
size < 1-> Raise ValueErrorsize = 1-> Always return 1size = 2-> The generator is always 2 (equivalent to consistent=true)
Performance
I've included these performance numbers based on tests on my development desktop. As such, they may not be indicative of performance on your machine.
Initialization
On my desktop, CyclicPRNG repeatedly initializes with a size of 2^128 (the size of the IPv6 address space) in 0.03-0.04 seconds. I've included results from 10 sample inits below.
>>> import timeit
>>> ip6_timer = timeit.Timer(
... "CyclicPRNG(340282366920938463463374607431768211456)",
... setup="from cyclicprng import CyclicPRNG",
... )
>>>
>>> ip6_timer.repeat(10, 1)
[0.03722329996526241, 0.0364310999866575, 0.03413929999805987, 0.034686600090935826, 0.03407520009204745, 0.033979699946939945, 0.034076200099661946, 0.03446660004556179, 0.03457159991376102, 0.03437739983201027]
Getting The Next Number
In a similar test, 10 subsequent calls to get_random() all measured in the range of 3e-6 to 7e-6 (3 to 7 microseconds). This would iterate the entire IPv4 address space (2^32) in a little over 8 hours.
>>> random_timer = timeit.Timer(
... "c.get_random()",
... setup="from cyclicprng import CyclicPRNG; c=CyclicPRNG(340282366920938463463374607431768211456)"
... )
>>> random_timer.repeat(10, 1)
[5.499925464391708e-06, 7.0999376475811005e-06, 5.600042641162872e-06, 6.4999330788850784e-06, 5.499925464391708e-06, 4.200031980872154e-06, 6.200047209858894e-06, 6.7998189479112625e-06, 5.899928510189056e-06, 3.00002284348011e-06]
Testing
CyclicPRNG uses pytest for testing. You can run tests yourself by cloning this repo and setting up like so:
git clone https://github.com/natlas/cyclicprng.git
cd cyclicprng/
pipenv install --dev
pipenv run pytest tests.py
License
Copyright 2020 The Natlas Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Metadata
Release files for cyclicprng 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cyclicprng-0.2.0.tar.gz | 9.9 kB | With provenance | Details for cyclicprng-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cyclicprng-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for cyclicprng-0.2.0-py3-none-any.whl |
Total release size: 22.8 kB
Release files / cyclicprng-0.2.0.tar.gz
| Download URL | cyclicprng-0.2.0.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c64fdadd664f569b2acc84361ea6d33e3117aa85e07f3a1ea561e1d33f473bd4
|
|
BLAKE2b-256 checksum How to use checksums |
2d3d68a963c7249f4b851423eef94d7edfb292fe9864607aca5fcfee521cfb89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 5, 2025.
Transparency logRelease files / cyclicprng-0.2.0-py3-none-any.whl
| Download URL | cyclicprng-0.2.0-py3-none-any.whl |
|---|---|
| Size | 12.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9af4304ff0064787abf313d52f89daa514ed30f4ba3142f983a7ee6fa1cabbf4
|
|
BLAKE2b-256 checksum How to use checksums |
c85cc5b301f6039825ad8290d071c031dd21939a6ef46518a8001e9e590cddbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 5, 2025.
Transparency logSecurity for cyclicprng 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for cyclicprng-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 5, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.