extend-mt19937-predictor


Nameextend-mt19937-predictor JSON
Version 19937.0.3 PyPI version JSON
download
home_pagehttps://github.com/NonupleBroken/ExtendMT19937Predictor
SummaryExtend Mt19937 Predictor
upload_time2022-07-01 08:29:28
maintainer
docs_urlNone
authorNonupleBroken
requires_python
licenseGPLv3
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Extend MT19937 Predictor

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/NonupleBroken/ExtendMT19937Predictor/Unit%20Testing) ![GitHub](https://img.shields.io/github/license/NonupleBroken/ExtendMT19937Predictor) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/extend-mt19937-predictor) ![PyPI](https://img.shields.io/pypi/v/extend-mt19937-predictor) ![PyPI - Status](https://img.shields.io/pypi/status/extend-mt19937-predictor)

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

```bash
$ 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.

```python
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.

```python
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.

```python
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)

```

```python
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

* https://github.com/kmyk/mersenne-twister-predictor
* https://en.wikipedia.org/wiki/Mersenne_Twister

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/NonupleBroken/ExtendMT19937Predictor",
    "name": "extend-mt19937-predictor",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "NonupleBroken",
    "author_email": "nonuplebroken@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/3f/65/0aab1a7e0ba75a6bff5ef28fcfc15d3f3002f42da849800cbf61086a50b5/extend_mt19937_predictor-19937.0.3.tar.gz",
    "platform": null,
    "description": "# Extend MT19937 Predictor\n\n![GitHub Workflow Status](https://img.shields.io/github/workflow/status/NonupleBroken/ExtendMT19937Predictor/Unit%20Testing) ![GitHub](https://img.shields.io/github/license/NonupleBroken/ExtendMT19937Predictor) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/extend-mt19937-predictor) ![PyPI](https://img.shields.io/pypi/v/extend-mt19937-predictor) ![PyPI - Status](https://img.shields.io/pypi/status/extend-mt19937-predictor)\n\nPredict and Backtrack MT19937 PRNG by putting 32 * 624 bits generated numbers.\n\nPython \"random\" standard library uses mt19937, so we can easily crack it.\n\n## Usage\n\n### Install\n\n```bash\n$ pip install extend_mt19937_predictor\n```\n\n### Predict\n\nAfter putting 32 * 624 bits numbers,  the internal state is uniquely determined. And the random number can be predicted at will.\n\n```python\nimport random\nfrom extend_mt19937_predictor import ExtendMT19937Predictor\n\npredictor = ExtendMT19937Predictor()\n\nfor _ in range(624):\n    predictor.setrandbits(random.getrandbits(32), 32)\n\nfor _ in range(1024):\n    assert predictor.predict_getrandbits(32) == random.getrandbits(32)\n    assert predictor.predict_getrandbits(64) == random.getrandbits(64)\n    assert predictor.predict_getrandbits(128) == random.getrandbits(128)\n    assert predictor.predict_getrandbits(256) == random.getrandbits(256)\n```\n\n### Backtrack\n\nBesides prediction, it can also backtrack the previous random numbers.\n\n```python\nimport random\nfrom extend_mt19937_predictor import ExtendMT19937Predictor\n\nnumbers = [random.getrandbits(64) for _ in range(1024)]\n\npredictor = ExtendMT19937Predictor()\n\nfor _ in range(78):\n    predictor.setrandbits(random.getrandbits(256), 256)\n\n_ = [predictor.backtrack_getrandbits(256) for _ in range(78)]\n\nfor x in numbers[::-1]:\n    assert x == predictor.backtrack_getrandbits(64)\n```\n\n### Advanced\n\n`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.\n\n When setting `check` param to False, it will directly overwrite the state without checking.\n\n```python\nimport random\nfrom extend_mt19937_predictor import ExtendMT19937Predictor\n\npredictor = ExtendMT19937Predictor(check=True)\n\nfor _ in range(1024):\n    predictor.setrandbits(random.getrandbits(32), 32)\n\nfor _ in range(1024):\n    assert predictor.predict_getrandbits(32) == random.getrandbits(32)\n\n```\n\n```python\nimport random\nfrom extend_mt19937_predictor import ExtendMT19937Predictor\n\npredictor = ExtendMT19937Predictor(check=True)\n\nfor _ in range(624):\n    predictor.setrandbits(random.getrandbits(32), 32)\n\n_ = predictor.setrandbits(0, 32)\n# ValueError: this rand number is not correct: 0. should be: 2370104960\n```\n\nBesides \"random\" standard library function `getrandbits`, these functions can be predicted.\n\n```\nrandom\nrandrange\nrandint\nuniform\n```\n\nBut only these functions can be backtracked, because of cannot determine how many times the base functions are called by the others.\n\n```\nrandom\nuniform\n```\n\n## Reference\n\n* https://github.com/kmyk/mersenne-twister-predictor\n* https://en.wikipedia.org/wiki/Mersenne_Twister\n",
    "bugtrack_url": null,
    "license": "GPLv3",
    "summary": "Extend Mt19937 Predictor",
    "version": "19937.0.3",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "2f9730fad9ca6a1774d2c346bf806b26",
                "sha256": "2ef80db1f5a82547abbb1f3375a5566fe0f1935c12856ff260f02541e971cbdd"
            },
            "downloads": -1,
            "filename": "extend_mt19937_predictor-19937.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2f9730fad9ca6a1774d2c346bf806b26",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 16959,
            "upload_time": "2022-07-01T08:29:26",
            "upload_time_iso_8601": "2022-07-01T08:29:26.246002Z",
            "url": "https://files.pythonhosted.org/packages/15/35/e63036a95392af21b5c8801a2ff164c68133d6075305d4a2a58566e90dc9/extend_mt19937_predictor-19937.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "af0ad36ff47b3ed03c20c99e9b104e5b",
                "sha256": "fb5c4b9bb12c8f2b5cdebaf3547f4e1cf7296ae334bee378d5b57b1706a9d09a"
            },
            "downloads": -1,
            "filename": "extend_mt19937_predictor-19937.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "af0ad36ff47b3ed03c20c99e9b104e5b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16562,
            "upload_time": "2022-07-01T08:29:28",
            "upload_time_iso_8601": "2022-07-01T08:29:28.640891Z",
            "url": "https://files.pythonhosted.org/packages/3f/65/0aab1a7e0ba75a6bff5ef28fcfc15d3f3002f42da849800cbf61086a50b5/extend_mt19937_predictor-19937.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-07-01 08:29:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "NonupleBroken",
    "github_project": "ExtendMT19937Predictor",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "extend-mt19937-predictor"
}
        
Elapsed time: 0.41737s