Skip to main content

Cryptographically secure pseudorandom number generators for PyTorch

Project description

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

torchcsprng-0.1.0-cp38-cp38-manylinux1_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.8

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

Uploaded CPython 3.8 macOS 10.9+ x86-64

torchcsprng-0.1.0-cp37-cp37m-manylinux1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.7m

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

Uploaded CPython 3.7m macOS 10.9+ x86-64

torchcsprng-0.1.0-cp36-cp36m-manylinux1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.6m

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

Uploaded CPython 3.6m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: torchcsprng-0.1.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.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.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1d00ffc13ddb79c0539bca3afea8916d5dc9f1c31485dadad8cb0aff5042fd63
MD5 622082db2a39463fae3d9f88177ac268
BLAKE2b-256 049d2eabf26f426336e461fbe0f97daa9e2b9d0bb6d760a2def2b3e7dd7ad84f

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: torchcsprng-0.1.0-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.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a3ff0cc43644aa1833a7bd4cbb34361e6c67875ae76a4ada3f3ae07bbec4cd0e
MD5 4ac004931516fc56afc2a21801866c29
BLAKE2b-256 ebc67808039a805bbfccddff3b3a346bae7290aea20199863f74ec2426f35e05

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: torchcsprng-0.1.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.5 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.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9d6794ada93ea68a1be493fc8d00ee5daa9bb0698d1c1f5f7c652e07a543f4e9
MD5 d1be3a98039ed316e835306367a9513a
BLAKE2b-256 33464ffa3b90f19db1fd74fced254b075dbdfdc0ebe81688b444729c7781c5f8

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: torchcsprng-0.1.0-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.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 26f43b57e47f8923e3d32b6d0793a44efb9930f2d4bab58f2a61465bf6481de7
MD5 307814bffc584b100b6c03c942830d92
BLAKE2b-256 f009f6b0760282688f61d0b316e882f681fd78a288f58bcb99faa179c70e95b2

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: torchcsprng-0.1.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.5 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.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 80f843957c7c0aeefb3cb0f1e890c730caf57e2cefb576c0f71e3f1861c24294
MD5 87d24dc20053d8e58be9b9ec5d1bb0b4
BLAKE2b-256 5170b525703793aff340a94af4c30a77b456f4e3751fe19352105d0c0e3d79ab

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: torchcsprng-0.1.0-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.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bd4332c5df07ed2470c30db8ed8f9b4fe5539eed34a0030a65e0256109eeec2e
MD5 514ad737fe53ae737ab028639f103313
BLAKE2b-256 8091c427dd12f3ddc107576ca561e50abc2987463fe619e04f36424f179d5cf3

See more details on using hashes here.

Provenance

Supported by

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