Skip to content

Commit 55328bd

Browse files
committed
Add CI to run AVX-512 backend (from aes CI config) and fix build with RNG feature
1 parent 045a79e commit 55328bd

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

.github/workflows/chacha20.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,43 @@ jobs:
7979
- run: cargo check --target ${{ matrix.target }} --all-features
8080
- run: cargo hack test --feature-powerset --target ${{ matrix.target }}
8181

82+
# Tests for the AVX-512 backend
83+
vaes512:
84+
runs-on: ubuntu-latest
85+
strategy:
86+
matrix:
87+
include:
88+
- target: x86_64-unknown-linux-gnu
89+
rust: stable
90+
RUSTFLAGS: "-Dwarnings --cfg chacha20_avx512"
91+
env:
92+
CARGO_INCREMENTAL: 0
93+
RUSTFLAGS: ${{ matrix.RUSTFLAGS }}
94+
steps:
95+
- uses: actions/checkout@v4
96+
- name: Install Intel SDE
97+
run: |
98+
curl -JLO "https://downloadmirror.intel.com/${{ env.SDE_FULL_VERSION_MIRROR }}/sde-external-${{ env.SDE_FULL_VERSION }}-lin.tar.xz"
99+
tar xvf sde-external-${{ env.SDE_FULL_VERSION }}-lin.tar.xz -C /opt
100+
echo "/opt/sde-external-${{ env.SDE_FULL_VERSION }}-lin" >> $GITHUB_PATH
101+
- uses: RustCrypto/actions/cargo-cache@master
102+
- uses: dtolnay/rust-toolchain@master
103+
with:
104+
toolchain: ${{ matrix.rust }}
105+
targets: ${{ matrix.target }}
106+
# NOTE: Write a `.cargo/config.toml` to configure the target for AVX-512
107+
# NOTE: We use intel-sde as the runner since not all GitHub CI hosts support AVX512
108+
- name: write .cargo/config.toml
109+
shell: bash
110+
run: |
111+
cd ../chacha20/..
112+
mkdir -p .cargo
113+
echo '[target.${{ matrix.target }}]' > .cargo/config.toml
114+
echo 'runner = "sde64 -future --"' >> .cargo/config.toml
115+
- run: ${{ matrix.deps }}
116+
- run: cargo test --target ${{ matrix.target }}
117+
- run: cargo test --target ${{ matrix.target }} --all-features
118+
82119
# Tests for the AVX2 backend
83120
avx2:
84121
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chacha20/src/backends.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ cfg_if! {
99
cfg_if! {
1010
if #[cfg(all(chacha20_avx512, chacha20_force_avx512))] {
1111
pub(crate) mod avx512;
12+
// AVX-2 backend needed for RNG if enabled
13+
#[cfg(feature = "rng")]
14+
pub(crate) mod avx2;
1215
} else if #[cfg(chacha20_force_avx2)] {
1316
pub(crate) mod avx2;
1417
} else if #[cfg(chacha20_force_sse2)] {

chacha20/src/backends/avx2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const N: usize = PAR_BLOCKS / 2;
2727
#[inline]
2828
#[target_feature(enable = "avx2")]
2929
#[cfg(feature = "cipher")]
30+
#[cfg_attr(chacha20_force_avx512, expect(unused))]
3031
pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
3132
where
3233
R: Rounds,

chacha20/src/rng.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
189189
backends::soft::Backend(self).gen_ks_blocks(buffer);
190190
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
191191
cfg_if! {
192-
if #[cfg(chacha20_force_avx2)] {
192+
// AVX-512 doesn't support RNG, so use AVX-2 instead
193+
if #[cfg(any(chacha20_force_avx2, chacha20_force_avx512))] {
193194
unsafe {
194195
backends::avx2::rng_inner::<R, V>(self, buffer);
195196
}
@@ -198,7 +199,11 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
198199
backends::sse2::rng_inner::<R, V>(self, buffer);
199200
}
200201
} else {
202+
#[cfg(chacha20_avx512)]
201203
let (_avx512_token, avx2_token, sse2_token) = self.tokens;
204+
#[cfg(not(chacha20_avx512))]
205+
let (avx2_token, sse2_token) = self.tokens;
206+
202207
if avx2_token.get() {
203208
unsafe {
204209
backends::avx2::rng_inner::<R, V>(self, buffer);

0 commit comments

Comments
 (0)