跳转到主要内容

PyTorch的密码学安全伪随机数生成器

项目描述

PyTorch/CSPRNG

CircleCI

torchcsprng是一个PyTorch C++/CUDA扩展,它提供

设计

torchcsprng使用其生成器之一在CPU上生成一个128位密钥,并使用AES128CTR模式下运行,可以在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上既安全又并行。

CSPRNG architecture

优点

  • 用户可以选择基于种子(用于测试)或基于随机设备的生成器(完全加密安全)
  • 一个生成器实例用于CPU和CUDA张量(因为加密密钥总是生成在CPU上)
  • CPU随机数生成也是并行的(与默认PyTorch CPU生成器不同)

特性

torchcsprng 0.2.0公开了新的张量加密/解密API。张量加密/解密API对dtype不敏感,因此任何dtype的张量都可以被加密,结果可以存储到任何dtype的张量中。加密密钥也可以是任何dtype的张量。目前torchcsprng支持128位密钥的AES密码,有两种模式:ECBCTR

  • torchcsprng.encrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)
  • input张量可以是任何dtype和任意大小的CPU或CUDA张量(使用零填充使其大小在字节上可被块大小整除)
  • output张量可以是任何dtype,但具有与input张量相同的设备和字节大小(向上取整到块大小字节,AES 128为16字节)
  • key张量可以是任何dtype,但具有与input张量相同的设备和大小为16字节(AES 128)
  • cipher当前只能有一个支持的值"aes128"
  • mode当前可以是"ecb""ctr"
  • torchcsprng.decrypt(input: Tensor, output: Tensor, key: Tensor, cipher: string, mode: string)
  • input张量可以是任何dtype,但大小在字节上可被块大小整除(AES 128为16字节)
  • output张量可以是任何dtype,但具有与input张量相同的设备和相同大小的字节
  • key张量可以是任何dtype,但具有与input张量相同的设备和大小为16字节(AES 128)
  • cipher当前只能有一个支持的值"aes128"
  • mode当前可以是"ecb""ctr"

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 (1.3 MB 查看哈希值)

上传时间 CPython 3.9 Windows x86-64

torchcsprng-0.2.1-cp39-cp39-manylinux1_x86_64.whl (4.9 MB 查看哈希值)

上传时间 CPython 3.9

torchcsprng-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl (472.5 kB 查看哈希值)

上传时间 CPython 3.9 macOS 10.9+ x86-64

torchcsprng-0.2.1-cp38-cp38-win_amd64.whl (1.3 MB 查看哈希值)

上传时间 CPython 3.8 Windows x86-64

torchcsprng-0.2.1-cp38-cp38-manylinux1_x86_64.whl (4.9 MB 查看哈希值)

上传时间 CPython 3.8

torchcsprng-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl (512.1 kB 查看哈希值)

上传时间 CPython 3.8 macOS 10.9+ x86-64

torchcsprng-0.2.1-cp37-cp37m-win_amd64.whl (1.3 MB 查看哈希值)

上传于 CPython 3.7m Windows x86-64

torchcsprng-0.2.1-cp37-cp37m-manylinux1_x86_64.whl (4.9 MB 查看哈希)

上传于 CPython 3.7m

torchcsprng-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (511.9 kB 查看哈希)

上传于 CPython 3.7m macOS 10.9+ x86-64

torchcsprng-0.2.1-cp36-cp36m-win_amd64.whl (1.3 MB 查看哈希)

上传于 CPython 3.6m Windows x86-64

torchcsprng-0.2.1-cp36-cp36m-manylinux1_x86_64.whl (4.9 MB 查看哈希)

上传于 CPython 3.6m

torchcsprng-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl (512.0 kB 查看哈希)

上传于 CPython 3.6m macOS 10.9+ x86-64

由以下支持