Skip to content

Commit

Permalink
chacha20: Use criterion for benchmarking
Browse files Browse the repository at this point in the history
...with the `criterion-cycles-per-byte` plugin

This unfortunately means we can no-longer run tests for chacha20 against
Rust 1.27.0, since it adds 2018 edition `dev-dependencies`. However, we
can still confirm release builds against this version work.

As we're starting to reach a microoptimization stage, I think criterion
will be extremely helpful in determining if our microoptimizations are
actually improving performance.

Currently I'm getting between 5.1 - 5.8 cpb across the tests on a
Kaby Lake i7. Curiously `-Ctarget-cpu=native` seems to negatively impact
performance. I'm not seeing much difference between the software and
SSE2 backends: +5% on the `chacha20/apply_keystream/1024` benchmark,
negligable on the others (i.e. +1-2%)
  • Loading branch information
tarcieri committed Oct 19, 2019
1 parent 890823a commit 9562b98
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
34 changes: 21 additions & 13 deletions .travis.yml
Expand Up @@ -25,26 +25,34 @@ matrix:
rust: 1.27.0
env: {} # clear `-D warnings` above; allow warnings
script:
- cargo test --all --release
- cargo test --all --exclude chacha20 --release
- ./test_aesni.sh

# chacha20 crate with SSE2 backend
- name: "Rust: stable (chacha20)"
rust: stable
env: RUSTFLAGS="-Ctarget-feature=+sse2"
script: cargo test --package chacha20 --release

# no_std build
- name: "Rust: 1.27.0 (thumbv7em-none-eabihf)"
rust: 1.27.0
env: {} # clear `-D warnings` above; allow warnings
install: rustup target add thumbv7em-none-eabihf
script: cargo build --all --target thumbv7em-none-eabihf --release
- name: "Rust: stable (thumbv7em-none-eabihf)"
rust: stable
install:
- rustup target add thumbv7em-none-eabihf
script:
- cargo build --all --target thumbv7em-none-eabihf --release
install: rustup target add thumbv7em-none-eabihf
script: cargo build --all --target thumbv7em-none-eabihf --release

- name: rustfmt
rust: stable
install:
- rustup component add rustfmt
script:
- cargo fmt --all -- --check
install: rustup component add rustfmt
script: cargo fmt --all -- --check
- name: clippy
rust: stable
install:
- rustup component add clippy
script:
- cargo clippy --all
install: rustup component add clippy
script: cargo clippy --all

branches:
only:
Expand Down
12 changes: 9 additions & 3 deletions chacha20/Cargo.toml
Expand Up @@ -12,22 +12,28 @@ keywords = ["crypto", "stream-cipher", "xchacha20"]
categories = ["cryptography", "no-std"]
readme = "README.md"

[badges]
travis-ci = { repository = "RustCrypto/stream-ciphers" }

[dependencies]
byteorder = { version = "1", default-features = false }
stream-cipher = "0.3"
salsa20-core = { version = "0.2", path = "../salsa20-core" }

[dev-dependencies]
stream-cipher = { version = "0.3", features = ["dev"] }

[badges]
travis-ci = { repository = "RustCrypto/stream-ciphers" }
criterion = "0.3"
criterion-cycles-per-byte = "0.1"

[features]
default = ["xchacha20"]
legacy = []
xchacha20 = []
zeroize = ["salsa20-core/zeroize"]

[[bench]]
name = "chacha20"
harness = false

[package.metadata.docs.rs]
all-features = true
39 changes: 35 additions & 4 deletions chacha20/benches/chacha20.rs
@@ -1,6 +1,37 @@
#![feature(test)]
#[macro_use]
extern crate stream_cipher;
extern crate chacha20;
extern crate criterion;
extern crate criterion_cycles_per_byte;

bench_sync!(chacha20::ChaCha20);
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use criterion_cycles_per_byte::CyclesPerByte;

use chacha20::{ChaCha20, stream_cipher::{NewStreamCipher, SyncStreamCipher}};

const KB: usize = 1024;

fn bench(c: &mut Criterion<CyclesPerByte>) {
let mut group = c.benchmark_group("chacha20");

for size in &[KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB] {
let mut buf = vec![0u8; *size];

group.throughput(Throughput::Bytes(*size as u64));

group.bench_function(BenchmarkId::new("apply_keystream", size), |b| {
let key = Default::default();
let nonce = Default::default();
let mut cipher = ChaCha20::new(&key, &nonce);

b.iter(|| cipher.apply_keystream(&mut buf));
});
}

group.finish();
}

criterion_group!(
name = benches;
config = Criterion::default().with_measurement(CyclesPerByte);
targets = bench
);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion test_aesni.sh
@@ -1 +1 @@
RUSTFLAGS="-C target-feature=+aes,+sse2,+ssse3" RUSTDOCFLAGS=$RUSTFLAGS cargo test --all
RUSTFLAGS="-C target-feature=+aes,+sse2,+ssse3" RUSTDOCFLAGS=$RUSTFLAGS cargo test --all --exclude chacha20

0 comments on commit 9562b98

Please sign in to comment.