Skip to main content

PyTorch/CSPRNG

torchcsprng is a PyTorch C++/CUDA extension that provides cryptographically secure pseudorandom number generators for PyTorch.

CircleCI

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.

CSPRNG architecture

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

Installation

CSPRNG works with Python 3.6/3.7/3.8 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 Coming

Binary Installation

Anaconda:

conda install torchcsprng -c pytorch

pip:

pip install torchcsprng

Nightly builds:

Anaconda:

OS CUDA
Linux 9.2

10.1

10.2

None
conda install torchcsprng -c pytorch-nightly
macOS

Windows
None

None
conda install torchcsprng -c pytorch-nightly

conda install torchcsprng cpuonly -c pytorch-nightly

pip:

OS CUDA
Linux 9.2

10.1

10.2

None
pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu92/torch_nightly.html

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/cpu/torch_nightly.html
macOS

Windows
None

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.6) with PyTorch(>=1.6.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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

torchcsprng-0.1.1-cp38-cp38-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8

torchcsprng-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl (153.8 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

torchcsprng-0.1.1-cp37-cp37m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m

torchcsprng-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl (153.0 kB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

torchcsprng-0.1.1-cp36-cp36m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m

torchcsprng-0.1.1-cp36-cp36m-macosx_10_9_x86_64.whl (153.0 kB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

Details for the file torchcsprng-0.1.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: torchcsprng-0.1.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.1

File hashes

Hashes for torchcsprng-0.1.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b48c4ae4e2b48cbce65faaca7a815ca17c6373205116285d152ad2a02f26b92c
MD5 866a0401aca643d59b5a408328423018
BLAKE2b-256 ecdae212a5ff16a6725762ac99c8266aa9ce7984c5e74f780fa774241ef5fde9

See more details on using hashes here.

File details

Details for the file torchcsprng-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: torchcsprng-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 153.8 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.1

File hashes

Hashes for torchcsprng-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9a52830f8305e7ee996ff89757c5bfc3e40810aced322dcc2c3d8776e84a9793
MD5 9c52bfa464baeddbbb6e717346e8677f
BLAKE2b-256 b690f288f70b36c999f0d7bd8a7ddc43428ac13503c1226d501eade1a05c601f

See more details on using hashes here.

File details

Details for the file torchcsprng-0.1.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: torchcsprng-0.1.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.1

File hashes

Hashes for torchcsprng-0.1.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 78b1dc9c6a043bb7f28b3f1a356b8498b53d971a65c555a8b10b552924212328
MD5 07a2b6055c80dde78484b24746f56194
BLAKE2b-256 e41d1af2ed8b18b68a04cf6004c5221bd05f949920fdc7fe4b9b5967fdef1a5a

See more details on using hashes here.

File details

Details for the file torchcsprng-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: torchcsprng-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 153.0 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.1

File hashes

Hashes for torchcsprng-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 efca6b7878943de34ef756e05af82c367e179da251043d8839e77a979f9e1045
MD5 208b306fa8446474d00f560f8c80e815
BLAKE2b-256 051437e0449fe7fe0f9d5cbbfd3402bb8c4a48072d411e0585bddf3d86da1fbd

See more details on using hashes here.

File details

Details for the file torchcsprng-0.1.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: torchcsprng-0.1.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.1

File hashes

Hashes for torchcsprng-0.1.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 664d7b546c8d53e4a62790276d38d5a0dbfc29f3e19687cd5d1fae7e02426306
MD5 67ef2822840ad90b1f3dd04f94a4a3af
BLAKE2b-256 e17ef73f77c7ed1f52c87c8aac6c487102392da76ca626db7198280bec369c9d

See more details on using hashes here.

File details

Details for the file torchcsprng-0.1.1-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: torchcsprng-0.1.1-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 153.0 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.1

File hashes

Hashes for torchcsprng-0.1.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c919ad0dca9d2a9d16b87ab925029500e7e4dc58a5ac2d4a9757a071b25c56f7
MD5 9b70ece7f6e4d7c0ac49280872a17551
BLAKE2b-256 64eaab0ed18a49a034610ad0227db3249b8b93939416f1079b53cf18593c97b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page