#random #aes

aes-prng

基于AES的伪随机数生成器

3个不稳定版本

0.2.1 2024年2月9日
0.2.0 2022年5月30日
0.1.0 2022年5月26日

#151 in 密码学

Download history 168/week @ 2024-03-13 215/week @ 2024-03-20 167/week @ 2024-03-27 164/week @ 2024-04-03 222/week @ 2024-04-10 226/week @ 2024-04-17 161/week @ 2024-04-24 204/week @ 2024-05-01 265/week @ 2024-05-08 328/week @ 2024-05-15 436/week @ 2024-05-22 367/week @ 2024-05-29 375/week @ 2024-06-05 508/week @ 2024-06-12 577/week @ 2024-06-19 349/week @ 2024-06-26

1,873 每月下载量
用于 3 crates

Apache-2.0

20KB
246 代码行

AES-PRNG

crate Docs Build Status Apache2 License 2.0 Minimum rustc version Downloads

使用AES作为底层块加密的Rust库进行随机数生成。

使用方法

将此添加到您的 Cargo.toml

[dependencies]
aes-prng = "0.2.1"

示例

use rand::{RngCore, SeedableRng};
use aes_prng::AesRng;

let mut rng = AesRng::from_random_seed();

// sample random bytes
let mut bytes = [0; 1024];
rng.fill_bytes(&mut bytes);

// sample random u32
let r_u32 = rng.next_u32();

// sample random u64
let r_u64 = rng.next_u64();

AesRng 也可以从现有的种子创建

let seed = AesRng::generate_random_seed();
let mut rng = AesRng::from_seed(seed);

Rust版本要求

AES-PRNG由于依赖于RustCrypto,需要 Rustc版本1.56或更高版本

包功能

AES-PRNG默认启用了-Ctarget-feature=+aes功能,以利用AES-NI指令加快PRNG调用的速度。

基准测试

AMD Ryzen 9 3900X

$ cargo bench -- rng_fill

rng_fill/chacha8/100    time:   [18.266 us 18.269 us 18.271 us]
rng_fill/chacha12/100   time:   [24.603 us 24.607 us 24.610 us]
rng_fill/chacha20/100   time:   [38.965 us 38.970 us 38.974 us]
rng_fill/aes/100        time:   [24.080 us 24.113 us 24.144 us]

rng_fill/chacha8/1000   time:   [176.70 us 176.71 us 176.73 us]
rng_fill/chacha12/1000  time:   [248.39 us 248.41 us 248.44 us]
rng_fill/chacha20/1000  time:   [391.49 us 391.68 us 391.90 us]
rng_fill/aes/1000       time:   [225.52 us 225.53 us 225.54 us]

Apple M1 Max

$ cargo bench -- rng_fill

rng_fill/chacha8/100    time:   [82.938 us 83.033 us 83.144 us]
rng_fill/chacha12/100   time:   [120.63 us 120.84 us 121.05 us]
rng_fill/chacha20/100   time:   [195.85 us 196.17 us 196.51 us]
rng_fill/aes/100        time:   [414.90 us 415.26 us 415.71 us]

rng_fill/chacha8/1000   time:   [833.53 us 834.31 us 835.25 us]
rng_fill/chacha12/1000  time:   [1.2083 ms 1.2093 ms 1.2106 ms]
rng_fill/chacha20/1000  time:   [1.9600 ms 1.9638 ms 1.9685 ms]
rng_fill/aes/1000       time:   [4.1675 ms 4.1731 ms 4.1792 ms]
$ RUSTUP_TOOLCHAIN=nightly \
  RUSTFLAGS="--cfg aes_armv8" \
  cargo bench -- rng_fill

rng_fill/chacha8/100    time:   [74.994 us 75.104 us 75.223 us]
rng_fill/chacha12/100   time:   [109.58 us 109.75 us 109.95 us]
rng_fill/chacha20/100   time:   [179.29 us 179.52 us 179.79 us]
rng_fill/aes/100        time:   [11.019 us 11.064 us 11.113 us]

rng_fill/chacha8/1000   time:   [751.56 us 752.02 us 752.55 us]
rng_fill/chacha12/1000  time:   [1.1022 ms 1.1036 ms 1.1054 ms]
rng_fill/chacha20/1000  time:   [1.8051 ms 1.8100 ms 1.8157 ms]
rng_fill/aes/1000       time:   [112.58 us 113.03 us 113.49 us]

发布

我们使用carge-release手动发布

$ cargo release minor

许可证

AES-PRNG根据Apache许可证(版本2.0)的条款分发。版权信息请参阅NOTICE

依赖项

~1MB
~21K SLoC