Cryptographically secure pseudorandom number generators for PyTorch
Project description
PyTorch/CSPRNG
torchcsprng is a PyTorch C++/CUDA extension that provides:
- AES 128-bit encryption/decryption in two modes: ECB and CTR
- cryptographically secure pseudorandom number generators for PyTorch.
Design
torchcsprng generates a random 128-bit key on CPU using one of its generators and runs AES128 in CTR mode either on CPU or on GPU using CUDA to generate a random 128 bit state and apply a transformation function to map it to target tensor values. This approach is based on Parallel Random Numbers: As Easy as 1, 2, 3(John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, D. E. Shaw Research). It makes torchcsprng both crypto-secure and parallel on CUDA and CPU.
Advantages:
- The user can choose either seed-based(for testing) or random device based(fully crypto-secure) generators
- One generator instance for both CPU and CUDA tensors(because the encryption key is always generated on CPU)
- CPU random number generation is also parallel(unlike the default PyTorch CPU generator)
Features
torchcsprng 0.2.0 exposes new API for tensor encryption/decryption. Tensor encryption/decryption API is dtype agnostic, so a tensor of any dtype can be encrypted and the result can be stored to a tensor of any dtype. An encryption key also can be a tensor of any dtype. Currently torchcsprng supports AES cipher with 128-bit key in two modes: ECB and CTR.
torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)
input
tensor can be any CPU or CUDA tensor of any dtype and size in bytes(zero-padding is used to make its size in bytes divisible by block size in bytes)output
tensor can have any dtype and the same device asinput
tensor and the size in bytes rounded up to the block size in bytes(16 bytes for AES 128)key
tensor can have any dtype and the same device asinput
tensor and size in bytes equal to 16 for AES 128cipher
currently can be only one supported value"aes128"
mode
currently can be either"ecb"
or"ctr"
torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)
input
tensor can be any CPU or CUDA tensor of any dtype with size in bytes divisible by the block size in bytes(16 bytes for AES 128)output
tensor can have any dtype but the same device asinput
tensor and the same size in bytes asinput
tensorkey
tensor can have any dtype and the same device asinput
tensor and size in bytes equal to 16 for AES 128cipher
currently can be only one supported value"aes128"
mode
currently can be either"ecb"
or"ctr"
torchcsprng exposes two methods to create crypto-secure and non-crypto-secure PRNGs:
Method to create PRNG | Is crypto-secure? | Has seed? | Underlying implementation |
---|---|---|---|
create_random_device_generator(token: string=None) | yes | no | See std::random_device and its constructor. The implementation in libstdc++ expects token to name the source of random bytes. Possible token values include "default", "rand_s", "rdseed", "rdrand", "rdrnd", "/dev/urandom", "/dev/random", "mt19937", and integer string specifying the seed of the mt19937 engine. (Token values other than "default" are only valid for certain targets.) If token=None then constructs a new std::random_device object with an implementation-defined token. |
create_mt19937_generator(seed: int=None) | no | yes | See std::mt19937 and its constructor. Constructs a mersenne_twister_engine object, and initializes its internal state sequence to pseudo-random values. If seed=None then seeds the engine with default_seed. |
The following list of methods supports all forementioned PRNGs:
Kernel | CUDA | CPU |
---|---|---|
random_() | yes | yes |
random_(to) | yes | yes |
random_(from, to) | yes | yes |
uniform_(from, to) | yes | yes |
normal_(mean, std) | yes | yes |
cauchy_(median, sigma) | yes | yes |
log_normal_(mean, std) | yes | yes |
geometric_(p) | yes | yes |
exponential_(lambda) | yes | yes |
randperm(n) | yes* | yes |
- the calculations are done on CPU and the result is copied to CUDA
Installation
CSPRNG works with Python 3.6-3.9 on the following operating systems and can be used with PyTorch tensors on the following devices:
Tensor Device Type | Linux | macOS | MS Window |
---|---|---|---|
CPU | Supported | Supported | Supported |
CUDA | Supported | Not Supported | Supported since 0.2.0 |
The following is the corresponding CSPRNG versions and supported Python versions.
PyTorch | CSPRNG | Python | CUDA |
---|---|---|---|
1.8.0 | 0.2.0 | 3.7-3.9 | 10.1, 10.2, 11.1 |
1.7.1 | 0.1.4 | 3.6-3.8 | 9.2, 10.1, 10.2 |
1.7.0 | 0.1.3 | 3.6-3.8 | 9.2, 10.1, 10.2 |
1.6.0 | 0.1.2 | 3.6-3.8 | 9.2, 10.1, 10.2 |
Binary Installation
Anaconda:
OS | CUDA | |
---|---|---|
Linux/Windows | 10.1 10.2 11.1 None |
conda install torchcsprng cudatoolkit=10.1 -c pytorch -c conda-forge conda install torchcsprng cudatoolkit=10.2 -c pytorch -c conda-forge conda install torchcsprng cudatoolkit=11.1 -c pytorch -c conda-forge conda install torchcsprng cpuonly -c pytorch -c conda-forge |
macOS | None | conda install torchcsprng -c pytorch |
pip:
OS | CUDA | |
---|---|---|
Linux/Windows | 10.1 10.2 11.1 None |
pip install torchcsprng==0.2.0+cu101 torch==1.8.0+cu101 -f https://download.pytorch.org/whl/cu101/torch_stable.html pip install torchcsprng==0.2.0 torch==1.8.0 -f https://download.pytorch.org/whl/cu102/torch_stable.html pip install torchcsprng==0.2.0+cu111 torch==1.8.0+cu111 -f https://download.pytorch.org/whl/cu111/torch_stable.html pip install torchcsprng==0.2.0+cpu torch==1.8.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html |
macOS | None | pip install torchcsprng torch |
Nightly builds:
Anaconda:
OS | CUDA | |
---|---|---|
Linux/Windows | 10.1 10.2 11.1 None |
conda install torchcsprng cudatoolkit=10.1 -c pytorch-nightly -c conda-forge conda install torchcsprng cudatoolkit=10.2 -c pytorch-nightly -c conda-forge conda install torchcsprng cudatoolkit=11.1 -c pytorch-nightly -c conda-forge conda install torchcsprng cpuonly -c pytorch-nightly -c conda-forge |
macOS | None | conda install torchcsprng -c pytorch-nightly |
pip:
OS | CUDA | |
---|---|---|
Linux/Windows | 10.1 10.2 11.1 None |
pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu111/torch_nightly.html pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html |
macOS | None | pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html |
From Source
torchcsprng is a Python C++/CUDA extension that depends on PyTorch. In order to build CSPRNG from source it is required to have Python(>=3.7) with PyTorch(>=1.8.0) installed and C++ compiler(gcc/clang for Linux, XCode for macOS, Visual Studio for MS Windows). To build torchcsprng you can run the following:
python setup.py install
By default, GPU support is built if CUDA is found and torch.cuda.is_available() is True. Additionally, it is possible to force building GPU support by setting the FORCE_CUDA=1 environment variable, which is useful when building a docker image.
Getting Started
The torchcsprng API is available in torchcsprng
module:
import torch
import torchcsprng as csprng
Create crypto-secure PRNG from /dev/urandom:
urandom_gen = csprng.create_random_device_generator('/dev/urandom')
Create empty boolean tensor on CUDA and initialize it with random values from urandom_gen:
torch.empty(10, dtype=torch.bool, device='cuda').random_(generator=urandom_gen)
tensor([ True, False, False, True, False, False, False, True, False, False],
device='cuda:0')
Create empty int16 tensor on CUDA and initialize it with random values in range [0, 100) from urandom_gen:
torch.empty(10, dtype=torch.int16, device='cuda').random_(100, generator=urandom_gen)
tensor([59, 20, 68, 51, 18, 37, 7, 54, 74, 85], device='cuda:0',
dtype=torch.int16)
Create non-crypto-secure MT19937 PRNG:
mt19937_gen = csprng.create_mt19937_generator()
torch.empty(10, dtype=torch.int64, device='cuda').random_(torch.iinfo(torch.int64).min, to=None, generator=mt19937_gen)
tensor([-7584783661268263470, 2477984957619728163, -3472586837228887516,
-5174704429717287072, 4125764479102447192, -4763846282056057972,
-182922600982469112, -498242863868415842, 728545841957750221,
7740902737283645074], device='cuda:0')
Create crypto-secure PRNG from default random device:
default_device_gen = csprng.create_random_device_generator()
torch.randn(10, device='cuda', generator=default_device_gen)
tensor([ 1.2885, 0.3240, -1.1813, 0.8629, 0.5714, 2.3720, -0.5627, -0.5551,
-0.6304, 0.1090], device='cuda:0')
Create non-crypto-secure MT19937 PRNG with seed:
mt19937_gen = csprng.create_mt19937_generator(42)
torch.empty(10, device='cuda').geometric_(p=0.2, generator=mt19937_gen)
tensor([ 7., 1., 8., 1., 11., 3., 1., 1., 5., 10.], device='cuda:0')
Recreate MT19937 PRNG with the same seed:
mt19937_gen = csprng.create_mt19937_generator(42)
torch.empty(10, device='cuda').geometric_(p=0.2, generator=mt19937_gen)
tensor([ 7., 1., 8., 1., 11., 3., 1., 1., 5., 10.], device='cuda:0')
Contributing
We appreciate all contributions. If you are planning to contribute back bug-fixes, please do so without any further discussion. If you plan to contribute new features, utility functions or extensions, please first open an issue and discuss the feature with us.
License
torchcsprng is BSD 3-clause licensed. See the license file here
Terms of Use
Privacy Policy
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file torchcsprng-0.2.1-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5531752c23074fd83a7ad21f401e4ef99c554ccb0309ec0871e0869b178f30a |
|
MD5 | 2ca3fc8f105753b4e9a69769dca9c832 |
|
BLAKE2b-256 | 3493646df4da6e2b0304b7af7be759e7baa189db48f4de7f3d61ea718c201127 |
File details
Details for the file torchcsprng-0.2.1-cp39-cp39-manylinux1_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp39-cp39-manylinux1_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.9
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe54ff9893bd4bef2b0dd453dc6cfc3feb6f1565dd1a6782fe722679fbbf6dd0 |
|
MD5 | e2c715eb991922241492fccbc7fee248 |
|
BLAKE2b-256 | aac4a96d0fc3ad9acfe7a9e9810a38f72bcc3088d9f5124093152cc821edecff |
File details
Details for the file torchcsprng-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 472.5 kB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 585240a6177109f6ba66e9c10e599e7568bea8e8277f69a009a0967ed4a465c9 |
|
MD5 | 4961dd9a6cef158ae94f2e499f760304 |
|
BLAKE2b-256 | 73c57f0687d8465faaee0bd90843b5a473c293a85bc1f74e5bc5d5b34cbfbd44 |
File details
Details for the file torchcsprng-0.2.1-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73e5bb01c69fe2ce58b3d103c32152d5916d5ca74a310a6af418d9b68b7cd073 |
|
MD5 | ed1334e6c795c377a65300655648c27d |
|
BLAKE2b-256 | e3c8ed6ae1016006d3695f6471fa7b5cbf94b26178272dc9628e3d5953ed9596 |
File details
Details for the file torchcsprng-0.2.1-cp38-cp38-manylinux1_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp38-cp38-manylinux1_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58deb6fee47451faecbd9fdfb2545f973b254b8447ff8e1906c347ccde1d7629 |
|
MD5 | 287121b7ed7e9c0d132d410aa75d9fd2 |
|
BLAKE2b-256 | ae5fec03ab5f538a57274ac725ac3da188b4e36068eb48f1f93160919ccd9169 |
File details
Details for the file torchcsprng-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
- Upload date:
- Size: 512.1 kB
- Tags: CPython 3.8, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90ade9c0fd217506dd9e9ce8836c94af7b6c1c04c361206823e4b5dbe847c7cc |
|
MD5 | 4fe64d0e2b1dcbeccb3e9e7302dc06ea |
|
BLAKE2b-256 | 1c2e7c325cbf82739c3e9cbafa6cc6a1f5094715bf8ea6a4a01dd3f2b0d30511 |
File details
Details for the file torchcsprng-0.2.1-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b3d41514e2077392f21d6b29ae1e785e3d135094ba7a08b3f0ee359678ecb03 |
|
MD5 | c9997e129af43dc97b2f02eb1427e041 |
|
BLAKE2b-256 | de9e62644d78bf053665c7e5a0125deb01be09a9fd89a5a5a6828bd8b81748ae |
File details
Details for the file torchcsprng-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bb8624c31ee6a9b4c6404168607d34bdd8cbb7863e107ae2640b6637075b9db |
|
MD5 | 2972be1156e015dc07f6e7c3097878b4 |
|
BLAKE2b-256 | 78c8d4bd692fae7f50f1a1f928fad846f423be333ae266b746679c78489a11de |
File details
Details for the file torchcsprng-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 511.9 kB
- Tags: CPython 3.7m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b9e948e0768f0967e7c6dbb832ed9e060327a9fa89286a08fc1593d26ace38b |
|
MD5 | fc4687564ea7192c50c3ae00cb863f0e |
|
BLAKE2b-256 | 916cafceae0666aa7c5020e543ffa0af760561958326ff5cc3d6a0b3c8a4e794 |
File details
Details for the file torchcsprng-0.2.1-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5076124deeee9b87574a9da3881d1254977c70eb66086da1644ea6fafc36350 |
|
MD5 | 66fea182273c4b00e598f1e8056a89f5 |
|
BLAKE2b-256 | 321b28ad4162582fd7c51f20be1664b782dd5a0d169d098e65960079b705e756 |
File details
Details for the file torchcsprng-0.2.1-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2b2c215613790e45ea438e88f6fc0ac796cd3b4b5e7c0c9cd9182135464e36f |
|
MD5 | cc5a89d45f56e1e85958310670532b5a |
|
BLAKE2b-256 | 2f57d929464db8d96e4a8735b5413c484b724b7ab3aa6e360bdf40d76dd84bb3 |
File details
Details for the file torchcsprng-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: torchcsprng-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 512.0 kB
- Tags: CPython 3.6m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27ea3b547d993a38cdadcd1442e4e5612fd1f167524ecce5e328566f85080db9 |
|
MD5 | 2fb63ec8ceaf0921b04cc1aa91b6553b |
|
BLAKE2b-256 | 08463645539906db76fec003f47186305094cd7ff0b9f5da0993d0c575d90b5e |