Skip to content

Commit 785df1d

Browse files
Merge pull request pytorch#459 from pytorch/torchcsprng-Release-blog
Create 2020-08-21-torchcsprng-release-blog.md
2 parents 304bc47 + ba45d52 commit 785df1d

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
layout: blog_detail
3+
title: 'PyTorch framework for cryptographically secure random number generation, torchcsprng, now available'
4+
author: Team PyTorch
5+
---
6+
7+
[torchcsprng](https://github.com/pytorch/csprng) is a PyTorch [C++/CUDA extension](https://pytorch.org/tutorials/advanced/cpp_extension.html) that provides [cryptographically secure pseudorandom number generators](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) for PyTorch.
8+
9+
One of the key components of modern cryptography is the pseudorandom number generator. Katz and Lindell stated, "The use of badly designed or inappropriate random number generators can often leave a good cryptosystem vulnerable to attack. Particular care must be taken to use a random number generator that is designed for cryptographic use, rather than a 'general-purpose' random number generator which may be fine for some applications but not ones that are required to be cryptographically secure."[1] Additionally, most pseudorandom number generators scale poorly to massively parallel high-performance computation because of their sequential nature. Others don’t satisfy cryptographically secure properties.
10+
11+
## torchcsprng overview
12+
13+
Historically, PyTorch had only two pseudorandom number generator implementations: Mersenne Twister for CPU and Nvidia’s cuRAND Philox for CUDA. Despite good performance properties, neither of them are suitable for cryptographic applications. Over the course of the past several months, the PyTorch team developed the torchcsprng extension API. Based on PyTorch dispatch mechanism and operator registration, it allows the users to extend c10::GeneratorImpl and implement their own custom pseudorandom number generator.
14+
15+
torchcsprng generates a random 128-bit key on the CPU using one of its generators and then runs AES128 in [CTR mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)) either on CPU or GPU using CUDA. This then generates a random 128-bit state and applies 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)](http://www.thesalmons.org/john/random123/papers/random123sc11.pdf). It makes torchcsprng both crypto-secure and parallel on both CPU and CUDA.
16+
17+
<div class="text-center">
18+
<img src="{{ site.url }}/assets/images/torchcsprng.png" width="100%">
19+
</div>
20+
21+
Since torchcsprng is a PyTorch extension, it is available on the platforms where PyTorch is available (support for Windows-CUDA will be available in the coming months).
22+
23+
## Using torchcsprng
24+
25+
The torchcsprng API is very simple to use and is fully compatible with the PyTorch random infrastructure:
26+
27+
**Step 1: Install via binary distribution**
28+
29+
Anaconda:
30+
31+
```python
32+
conda install torchcsprng -c pytorch
33+
```
34+
35+
pip:
36+
37+
```python
38+
pip install torchcsprng
39+
```
40+
41+
**Step 2: import packages as usual but add csprng**
42+
43+
```python
44+
import torch
45+
import torchcsprng as csprng
46+
```
47+
48+
**Step 3: Create a cryptographically secure pseudorandom number generator from /dev/urandom:**
49+
50+
```python
51+
urandom_gen = csprng.create_random_device_generator('/dev/urandom')
52+
```
53+
54+
and simply use it with the existing PyTorch methods:
55+
56+
```python
57+
torch.randn(10, device='cpu', generator=urandom_gen)
58+
```
59+
60+
**Step 4: Test with Cuda**
61+
62+
One of the advantages of torchcsprng generators is that they can be used with both CPU and CUDA tensors:
63+
64+
```python
65+
torch.randn(10, device='cuda', generator=urandom_gen)
66+
```
67+
68+
Another advantage of torchcsprng generators is that they are parallel on CPU unlike the default PyTorch CPU generator.
69+
70+
## Getting Started
71+
72+
The easiest way to get started with torchcsprng is by visiting the [GitHub page](https://github.com/pytorch/csprng) where you can find installation and build instructions, and more how-to examples.
73+
74+
Cheers,
75+
76+
The PyTorch Team
77+
78+
[1] [Introduction to Modern Cryptography: Principles and Protocols (Chapman & Hall/CRC Cryptography and Network Security Series)](https://www.amazon.com/Introduction-Modern-Cryptography-Principles-Protocols/dp/1584885513) by Jonathan Katz and Yehuda Lindell
79+
80+
81+
82+

0 commit comments

Comments
 (0)