PyTorch的密码学安全伪随机数生成器
项目描述
PyTorch/CSPRNG
torchcsprng是一个PyTorch C++/CUDA扩展,它提供
- AES 128位加密/解密,两种模式:ECB 和 CTR
- 密码学安全伪随机数生成器,适用于PyTorch。
设计
torchcsprng使用其生成器之一在CPU上生成一个128位密钥,并使用AES128在CTR模式下运行,可以在CPU或GPU上使用CUDA生成一个随机的128位状态,并将其应用于变换函数以映射到目标张量值。此方法基于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)。这使得torchcsprng在CUDA和CPU上既安全又并行。
优点
- 用户可以选择基于种子(用于测试)或基于随机设备的生成器(完全加密安全)
- 一个生成器实例用于CPU和CUDA张量(因为加密密钥总是生成在CPU上)
- CPU随机数生成也是并行的(与默认PyTorch CPU生成器不同)
特性
torchcsprng 0.2.0公开了新的张量加密/解密API。张量加密/解密API对dtype不敏感,因此任何dtype的张量都可以被加密,结果可以存储到任何dtype的张量中。加密密钥也可以是任何dtype的张量。目前torchcsprng支持128位密钥的AES密码,有两种模式:ECB和CTR。
torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)
torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)
torchcsprng公开了两种创建加密安全和非加密安全的PRNG的方法
创建PRNG的方法 | 是否加密安全? | 是否有种子? | 底层实现 |
---|---|---|---|
create_random_device_generator(token: string=None) | 是 | 否 | 见std::random_device及其构造函数。libstdc++中的实现期望token命名随机字节的来源。可能的token值包括"default"、"rand_s"、"rdseed"、"rdrand"、"rdrnd"、"/dev/urandom"、"/dev/random"、"mt19937"以及指定mt19937引擎种子的整数字符串。(除了"default"之外的其他token值仅对某些目标有效。)如果token=None,则使用实现定义的token构造一个新的std::random_device对象。 |
create_mt19937_generator(seed: int=None) | 否 | 是 | 参见 std::mt19937 和 其构造函数。构建一个 mersenne_twister_engine 对象,并将其内部状态序列初始化为伪随机值。如果 seed=None,则使用 default_seed 初始化引擎。 |
以下列表支持所有之前提到的 PRNG
内核 | CUDA | CPU |
---|---|---|
random_() | 是 | 是 |
random_(to) | 是 | 是 |
random_(from, to) | 是 | 是 |
uniform_(from, to) | 是 | 是 |
normal_(mean, std) | 是 | 是 |
cauchy_(median, sigma) | 是 | 是 |
log_normal_(mean, std) | 是 | 是 |
geometric_(p) | 是 | 是 |
exponential_(lambda) | 是 | 是 |
randperm(n) | yes* | 是 |
- 计算在 CPU 上完成,结果复制到 CUDA
安装
CSPRNG 与 Python 3.6-3.9 兼容,支持以下操作系统,并可以在以下设备上与 PyTorch 索引一起使用
索引设备类型 | Linux | macOS | MS Window |
---|---|---|---|
CPU | 支持 | 支持 | 支持 |
CUDA | 支持 | 不支持 | 自 0.2.0 版本起支持 |
以下是对应的 CSPRNG 版本和支持的 Python 版本。
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 |
二进制安装
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 |
夜间构建
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 |
从源码构建
torchcsprng 是一个依赖 PyTorch 的 Python C++/CUDA 扩展。为了从源码构建 CSPRNG,需要安装 Python(>=3.7) 和 PyTorch(>=1.8.0),以及 C++ 编译器(Linux 上的 gcc/clang,macOS 上的 XCode,MS Windows 上的 Visual Studio)。要构建 torchcsprng,可以运行以下命令:
python setup.py install
默认情况下,如果找到 CUDA 并且 torch.cuda.is_available() 为 True,则会构建 GPU 支持。此外,还可以通过设置 FORCE_CUDA=1 环境变量强制构建 GPU 支持,这在构建 docker 图像时很有用。
入门
torchcsprng API 可在 torchcsprng
模块中使用
import torch
import torchcsprng as csprng
从 /dev/urandom 创建加密安全的 PRNG
urandom_gen = csprng.create_random_device_generator('/dev/urandom')
在 CUDA 上创建空布尔索引,并用 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')
在 CUDA 上创建空 int16 索引,并用 urandom_gen 中的范围 [0, 100) 内的随机值初始化
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)
创建非加密安全的 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')
从默认随机设备创建加密安全的伪随机数生成器(PRNG)
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')
使用种子创建非加密安全的MT19937 PRNG
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')
使用相同的种子重新创建MT19937 PRNG
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')
贡献
我们感谢所有贡献。如果您计划贡献回错误修复,请直接进行,无需进一步讨论。如果您计划贡献新功能、实用函数或扩展,请首先提交一个issue并与我们讨论该功能。
许可协议
torchcsprng遵循BSD 3条款许可。请参阅许可文件此处
使用条款
隐私政策
项目详情
torchcsprng-0.2.1-cp39-cp39-win_amd64.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c5531752c23074fd83a7ad21f401e4ef99c554ccb0309ec0871e0869b178f30a |
|
MD5 | 2ca3fc8f105753b4e9a69769dca9c832 |
|
BLAKE2b-256 | 3493646df4da6e2b0304b7af7be759e7baa189db48f4de7f3d61ea718c201127 |
torchcsprng-0.2.1-cp39-cp39-manylinux1_x86_64.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fe54ff9893bd4bef2b0dd453dc6cfc3feb6f1565dd1a6782fe722679fbbf6dd0 |
|
MD5 | e2c715eb991922241492fccbc7fee248 |
|
BLAKE2b-256 | aac4a96d0fc3ad9acfe7a9e9810a38f72bcc3088d9f5124093152cc821edecff |
torchcsprng-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 585240a6177109f6ba66e9c10e599e7568bea8e8277f69a009a0967ed4a465c9 |
|
MD5 | 4961dd9a6cef158ae94f2e499f760304 |
|
BLAKE2b-256 | 73c57f0687d8465faaee0bd90843b5a473c293a85bc1f74e5bc5d5b34cbfbd44 |
torchcsprng-0.2.1-cp38-cp38-win_amd64.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 73e5bb01c69fe2ce58b3d103c32152d5916d5ca74a310a6af418d9b68b7cd073 |
|
MD5 | ed1334e6c795c377a65300655648c27d |
|
BLAKE2b-256 | e3c8ed6ae1016006d3695f6471fa7b5cbf94b26178272dc9628e3d5953ed9596 |
torchcsprng-0.2.1-cp38-cp38-manylinux1_x86_64.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 58deb6fee47451faecbd9fdfb2545f973b254b8447ff8e1906c347ccde1d7629 |
|
MD5 | 287121b7ed7e9c0d132d410aa75d9fd2 |
|
BLAKE2b-256 | ae5fec03ab5f538a57274ac725ac3da188b4e36068eb48f1f93160919ccd9169 |
torchcsprng-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 90ade9c0fd217506dd9e9ce8836c94af7b6c1c04c361206823e4b5dbe847c7cc |
|
MD5 | 4fe64d0e2b1dcbeccb3e9e7302dc06ea |
|
BLAKE2b-256 | 1c2e7c325cbf82739c3e9cbafa6cc6a1f5094715bf8ea6a4a01dd3f2b0d30511 |
哈希值 for torchcsprng-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8bb8624c31ee6a9b4c6404168607d34bdd8cbb7863e107ae2640b6637075b9db |
|
MD5 | 2972be1156e015dc07f6e7c3097878b4 |
|
BLAKE2b-256 | 78c8d4bd692fae7f50f1a1f928fad846f423be333ae266b746679c78489a11de |
哈希值 for torchcsprng-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7b9e948e0768f0967e7c6dbb832ed9e060327a9fa89286a08fc1593d26ace38b |
|
MD5 | fc4687564ea7192c50c3ae00cb863f0e |
|
BLAKE2b-256 | 916cafceae0666aa7c5020e543ffa0af760561958326ff5cc3d6a0b3c8a4e794 |
哈希值 for torchcsprng-0.2.1-cp36-cp36m-manylinux1_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a2b2c215613790e45ea438e88f6fc0ac796cd3b4b5e7c0c9cd9182135464e36f |
|
MD5 | cc5a89d45f56e1e85958310670532b5a |
|
BLAKE2b-256 | 2f57d929464db8d96e4a8735b5413c484b724b7ab3aa6e360bdf40d76dd84bb3 |
哈希值 for torchcsprng-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 27ea3b547d993a38cdadcd1442e4e5612fd1f167524ecce5e328566f85080db9 |
|
MD5 | 2fb63ec8ceaf0921b04cc1aa91b6553b |
|
BLAKE2b-256 | 08463645539906db76fec003f47186305094cd7ff0b9f5da0993d0c575d90b5e |