Skip to content

Commit

Permalink
chacha20: Process 4 blocks at a time in AVX2 backend (#267)
Browse files Browse the repository at this point in the history
- Refactor usage of `blocks` variant of `avx2::StateWords`

It is now agnostic of the number of blocks processed, which is now a
constant.

- Pass around `&mut avx2::StateWord` instead of `&mut __m256i`

-  Add `avx2::StateWord` methods for required ops; MSRV 1.51+

-  Change `buffer_pos` to a `u16`

For a 4-block buffer, we need to be able to represent the past-the-end
buffer position of 256, which is too large for a `u8`.

- Switch to 4-block buffer for SSE2 / AVX2 backend

- Add a `BlockRngResults` wrapper type

When the non-soft backend is being used, its 4-block buffer size results
in a `BlockRngCore::Results` type of `[u32; 64]` which doesn't implement
`Default`. We replace it with a wrapper type on which we implement the
necessary traits.
  • Loading branch information
str4d authored Aug 29, 2021
1 parent 8cfea58 commit 818c4ac
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 109 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/chacha20.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.49.0 # MSRV
- 1.51.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand Down Expand Up @@ -53,15 +53,15 @@ jobs:
include:
# 32-bit Linux
- target: i686-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
deps: sudo apt update && sudo apt install gcc-multilib
- target: i686-unknown-linux-gnu
rust: stable
deps: sudo apt update && sudo apt install gcc-multilib

# 64-bit Linux
- target: x86_64-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
- target: x86_64-unknown-linux-gnu
rust: stable
steps:
Expand Down Expand Up @@ -90,15 +90,15 @@ jobs:
include:
# 32-bit Linux
- target: i686-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
deps: sudo apt update && sudo apt install gcc-multilib
- target: i686-unknown-linux-gnu
rust: stable
deps: sudo apt update && sudo apt install gcc-multilib

# 64-bit Linux
- target: x86_64-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
- target: x86_64-unknown-linux-gnu
rust: stable
steps:
Expand Down Expand Up @@ -126,15 +126,15 @@ jobs:
include:
# 32-bit Linux
- target: i686-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
deps: sudo apt update && sudo apt install gcc-multilib
- target: i686-unknown-linux-gnu
rust: stable
deps: sudo apt update && sudo apt install gcc-multilib

# 64-bit Linux
- target: x86_64-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
- target: x86_64-unknown-linux-gnu
rust: stable
steps:
Expand All @@ -160,13 +160,13 @@ jobs:
include:
# ARM64
- target: aarch64-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
- target: aarch64-unknown-linux-gnu
rust: stable

# PPC32
- target: powerpc-unknown-linux-gnu
rust: 1.49.0 # MSRV
rust: 1.51.0 # MSRV
- target: powerpc-unknown-linux-gnu
rust: stable

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.49.0 # MSRV (highest in repo)
toolchain: 1.51.0 # MSRV (highest in repo)
components: clippy
override: true
profile: minimal
Expand Down
4 changes: 2 additions & 2 deletions chacha20/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ stream cipher itself) are designed to execute in constant time.

## Minimum Supported Rust Version

Rust **1.49** or higher.
Rust **1.51** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.
Expand Down Expand Up @@ -94,7 +94,7 @@ dual licensed as above, without any additional terms or conditions.
[docs-image]: https://docs.rs/chacha20/badge.svg
[docs-link]: https://docs.rs/chacha20/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.49+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.51+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260049-stream-ciphers
[build-image]: https://github.com/RustCrypto/stream-ciphers/workflows/chacha20/badge.svg?branch=master&event=push
Expand Down
4 changes: 3 additions & 1 deletion chacha20/src/backend/autodetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use core::mem::ManuallyDrop;

/// Size of buffers passed to `generate` and `apply_keystream` for this
/// backend, which operates on two blocks in parallel for optimal performance.
pub(crate) const BUFFER_SIZE: usize = BLOCK_SIZE * 2;
/// The backend consumes four blocks at a time, so that the AVX2 implementation
/// can additionally pipeline the pairs of blocks for better ILP.
pub(crate) const BUFFER_SIZE: usize = BLOCK_SIZE * 4;

cpufeatures::new!(avx2_cpuid, "avx2");

Expand Down
Loading

0 comments on commit 818c4ac

Please sign in to comment.