GithubHelp home page GithubHelp logo

aes-prng's Introduction

AES-PRNG

crate Docs Build Status Apache2 License 2.0 Minimum rustc version Downloads

A Rust library for random number generation using AES as the underlying block-cipher.

Usage

Add this to your Cargo.toml:

[dependencies]
aes-prng = "0.2.1"

Example

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 can also be created from an existing seed:

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

Rust version requirements

AES-PRNG requires Rustc version 1.56 or greater due to the RustCrypto dependency.

Crate Features

AES-PRNG is built with the -Ctarget-feature=+aes feature enabled by default to get the benefit of AES-NI instructions for speeding up the PRNG calls.

Benchmarks

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]

Releasing

We release manually using carge-release:

$ cargo release minor

License

AES-PRNG is distributed under the terms of Apache License (Version 2.0). Copyright as specified in NOTICE.

aes-prng's People

Contributors

dd23 avatar kc1212 avatar mortendahl avatar rdragos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dd23 kc1212

aes-prng's Issues

Investigate simple performance improvement for Apple M1

AES seems to be slower than ChaCha on Apple M1:

rng_fill/chacha8/2000000
                        time:   [1.6929 ms 1.6940 ms 1.6951 ms]
Found 9 outliers among 100 measurements (9.00%)
  2 (2.00%) low severe
  1 (1.00%) low mild
  2 (2.00%) high mild
  4 (4.00%) high severe
rng_fill/chacha12/2000000
                        time:   [2.4556 ms 2.4581 ms 2.4606 ms]
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild
rng_fill/chacha20/2000000
                        time:   [3.9821 ms 3.9857 ms 3.9895 ms]
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high mild
rng_fill/aes/2000000    time:   [8.4624 ms 8.4707 ms 8.4792 ms]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

rng_next_u64/chacha8    time:   [8.0137 us 8.0212 us 8.0285 us]
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) low mild
  4 (4.00%) high mild
  4 (4.00%) high severe
rng_next_u64/chacha12   time:   [11.055 us 11.065 us 11.076 us]
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low mild
  4 (4.00%) high mild
  2 (2.00%) high severe
rng_next_u64/chacha20   time:   [17.142 us 17.161 us 17.179 us]
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low mild
  5 (5.00%) high mild
  1 (1.00%) high severe
rng_next_u64/aes        time:   [36.918 us 36.950 us 36.983 us]
Found 10 outliers among 100 measurements (10.00%)
  5 (5.00%) low mild
  4 (4.00%) high mild
  1 (1.00%) high severe

Maybe this can be fixed by simply enabling a flag.

Implement trait `CryptoRng`

Currently the code does not implement the CryptoRng trait which indicates that the rng is secure for cryptographic purposes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.