Package: torchcsprng
PyTorch/CSPRNGtorchcsprng is aPyTorch C++/CUDA extensionthat provides:AES128-bit encryption/decryption in two modes:ECBandCTRcryptographically secure pseudorandom number generatorsfor PyTorch.Designtorchcsprng generates a random 128-bit key on CPU using one of its generators and runsAES128inCTR modeeither 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 onParallel 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) generatorsOne 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)Featurestorchcsprng 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 supportsAEScipher with 128-bit key in two modes:ECBandCTR.torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)inputtensor 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)outputtensor can have any dtype and the same device asinputtensor and the size in bytes rounded up to the block size in bytes(16 bytes for AES 128)keytensor can have any dtype and the same device asinputtensor and size in bytes equal to 16 for AES 128ciphercurrently can be only one supported value"aes128"modecurrently can be either"ecb"or"ctr"torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)inputtensor 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)outputtensor can have any dtype but the same device asinputtensor and the same size in bytes asinputtensorkeytensor can have any dtype and the same device asinputtensor and size in bytes equal to 16 for AES 128ciphercurrently can be only one supported value"aes128"modecurrently can be either"ecb"or"ctr"torchcsprng exposes two methods to create crypto-secure and non-crypto-secure PRNGs:Method to create PRNGIs crypto-secure?Has seed?Underlying implementationcreate_random_device_generator(token: string=None)yesnoSeestd::random_deviceandits 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)noyesSeestd::mt19937andits 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:KernelCUDACPUrandom_()yesyesrandom_(to)yesyesrandom_(from, to)yesyesuniform_(from, to)yesyesnormal_(mean, std)yesyescauchy_(median, sigma)yesyeslog_normal_(mean, std)yesyesgeometric_(p)yesyesexponential_(lambda)yesyesrandperm(n)yes*yesthe calculations are done on CPU and the result is copied to CUDAInstallationCSPRNG 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 TypeLinuxmacOSMS WindowCPUSupportedSupportedSupportedCUDASupportedNot SupportedSupported since 0.2.0The following is the corresponding CSPRNG versions and supported Python versions.PyTorchCSPRNGPythonCUDA1.8.00.2.03.7-3.910.1, 10.2, 11.11.7.10.1.43.6-3.89.2, 10.1, 10.21.7.00.1.33.6-3.89.2, 10.1, 10.21.6.00.1.23.6-3.89.2, 10.1, 10.2Binary InstallationAnaconda:OSCUDALinux/Windows10.110.211.1Noneconda install torchcsprng cudatoolkit=10.1 -c pytorch -c conda-forgeconda install torchcsprng cudatoolkit=10.2 -c pytorch -c conda-forgeconda install torchcsprng cudatoolkit=11.1 -c pytorch -c conda-forgeconda install torchcsprng cpuonly -c pytorch -c conda-forgemacOSNoneconda install torchcsprng -c pytorchpip:OSCUDALinux/Windows10.110.211.1Nonepip install torchcsprng==0.2.0+cu101 torch==1.8.0+cu101 -fhttps://download.pytorch.org/whl/cu101/torch_stable.htmlpip install torchcsprng==0.2.0 torch==1.8.0 -fhttps://download.pytorch.org/whl/cu102/torch_stable.htmlpip install torchcsprng==0.2.0+cu111 torch==1.8.0+cu111 -fhttps://download.pytorch.org/whl/cu111/torch_stable.htmlpip install torchcsprng==0.2.0+cpu torch==1.8.0+cpu -fhttps://download.pytorch.org/whl/cpu/torch_stable.htmlmacOSNonepip install torchcsprng torchNightly builds:Anaconda:OSCUDALinux/Windows10.110.211.1Noneconda install torchcsprng cudatoolkit=10.1 -c pytorch-nightly -c conda-forgeconda install torchcsprng cudatoolkit=10.2 -c pytorch-nightly -c conda-forgeconda install torchcsprng cudatoolkit=11.1 -c pytorch-nightly -c conda-forgeconda install torchcsprng cpuonly -c pytorch-nightly -c conda-forgemacOSNoneconda install torchcsprng -c pytorch-nightlypip:OSCUDALinux/Windows10.110.211.1Nonepip install --pre torchcsprng -fhttps://download.pytorch.org/whl/nightly/cu101/torch_nightly.htmlpip install --pre torchcsprng -fhttps://download.pytorch.org/whl/nightly/cu102/torch_nightly.htmlpip install --pre torchcsprng -fhttps://download.pytorch.org/whl/nightly/cu111/torch_nightly.htmlpip install --pre torchcsprng -fhttps://download.pytorch.org/whl/nightly/cpu/torch_nightly.htmlmacOSNonepip install --pre torchcsprng -fhttps://download.pytorch.org/whl/nightly/cpu/torch_nightly.htmlFrom Sourcetorchcsprng 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 installBy 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 StartedThe torchcsprng API is available intorchcsprngmodule:importtorchimporttorchcsprngascsprngCreate 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')ContributingWe 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.Licensetorchcsprng is BSD 3-clause licensed. See the license filehereTerms of UsePrivacy Policy