Skip to main content

Extend MT19937 Predictor

GitHub Workflow Status GitHub PyPI - Python Version PyPI PyPI - Status

Predict and Backtrack MT19937 PRNG by putting 32 * 624 bits generated numbers.

Python "random" standard library uses mt19937, so we can easily crack it.

Usage

Install

$ pip install extend_mt19937_predictor

Predict

After putting 32 * 624 bits numbers, the internal state is uniquely determined. And the random number can be predicted at will.

import random
from extend_mt19937_predictor import ExtendMT19937Predictor

predictor = ExtendMT19937Predictor()

for _ in range(624):
    predictor.setrandbits(random.getrandbits(32), 32)

for _ in range(1024):
    assert predictor.predict_getrandbits(32) == random.getrandbits(32)
    assert predictor.predict_getrandbits(64) == random.getrandbits(64)
    assert predictor.predict_getrandbits(128) == random.getrandbits(128)
    assert predictor.predict_getrandbits(256) == random.getrandbits(256)

Backtrack

Besides prediction, it can also backtrack the previous random numbers.

import random
from extend_mt19937_predictor import ExtendMT19937Predictor

numbers = [random.getrandbits(64) for _ in range(1024)]

predictor = ExtendMT19937Predictor()

for _ in range(78):
    predictor.setrandbits(random.getrandbits(256), 256)

_ = [predictor.backtrack_getrandbits(256) for _ in range(78)]

for x in numbers[::-1]:
    assert x == predictor.backtrack_getrandbits(64)

Advanced

check param is True by default. It is ok to put more than 32 * 624 bits numbers when initializing. It will automatically check whether the excess number is the same as the predicted number, and also change the internal state.

When setting check param to False, it will directly overwrite the state without checking.

import random
from extend_mt19937_predictor import ExtendMT19937Predictor

predictor = ExtendMT19937Predictor(check=True)

for _ in range(1024):
    predictor.setrandbits(random.getrandbits(32), 32)

for _ in range(1024):
    assert predictor.predict_getrandbits(32) == random.getrandbits(32)
import random
from extend_mt19937_predictor import ExtendMT19937Predictor

predictor = ExtendMT19937Predictor(check=True)

for _ in range(624):
    predictor.setrandbits(random.getrandbits(32), 32)

_ = predictor.setrandbits(0, 32)
# ValueError: this rand number is not correct: 0. should be: 2370104960

Besides "random" standard library function getrandbits, these functions can be predicted.

random
randrange
randint
uniform

But only these functions can be backtracked, because of cannot determine how many times the base functions are called by the others.

random
uniform

Reference

Release files for extend-mt19937-predictor 19937.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for extend-mt19937-predictor 19937.0.3
File Size Uploaded
extend_mt19937_predictor-19937.0.3.tar.gz 16.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for extend-mt19937-predictor 19937.0.3
File Interpreter ABI Platform
extend_mt19937_predictor-19937.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 33.5 kB

Release files / extend_mt19937_predictor-19937.0.3.tar.gz

Download URL extend_mt19937_predictor-19937.0.3.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fb5c4b9bb12c8f2b5cdebaf3547f4e1cf7296ae334bee378d5b57b1706a9d09a
BLAKE2b-256 checksum
How to use checksums
3f650aab1a7e0ba75a6bff5ef28fcfc15d3f3002f42da849800cbf61086a50b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.1

Release files / extend_mt19937_predictor-19937.0.3-py3-none-any.whl

Download URL extend_mt19937_predictor-19937.0.3-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ef80db1f5a82547abbb1f3375a5566fe0f1935c12856ff260f02541e971cbdd
BLAKE2b-256 checksum
How to use checksums
1535e63036a95392af21b5c8801a2ff164c68133d6075305d4a2a58566e90dc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.1

Release history Release notifications | RSS feed

This release

19937.0.3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page