Skip to content

sha2: document available backends #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sha2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ assert_eq!(hash512, hex!(

Also, see the [examples section] in the RustCrypto/hashes readme.

## Backends

This crate supports the following backends:
- `soft`: portable implementation with fully unrolled rounds
- `soft-compact`: portable implementation which produces smaller binaries
- `aarch64-sha2`: uses the AArch64 `sha2` extension, fallbacks to the `soft` backend
if the extension is not available
- `loongarch64-asm`: `asm!`-based implementation for LoongArch64 targets
- `riscv-zknh`: uses the RISC-V `Zknh` scalar crypto extension (experimental)
- `riscv-zknh-compact`: same as `riscv_zknh` but does not unroll rounds (experimental)
- `wasm32-simd`: uses the WASM `simd128` extension
- `x86-shani`: uses the x86 SHA-NI extension, fallbacks to the `soft` backend
if the extension is not available (SHA-256 only)
- `x86-avx2`: uses the x86 AVX2 extension, fallbacks to the `soft` backend
if the extension is not available (SHA-512 only)

You can force backend selection using the `sha2_backend` configuration flag. It can be enabled
using either environment variable (e.g. `RUSTFLAGS='--cfg sha2_backend="soft"' cargo build`), or
by modifying your `.cargo/config.toml` file. Currently the flag supports the following values:
`soft`, `soft-compact`, `riscv-zknh`, and `riscv-zknh-compact`.

Note that the RISC-V backends are experimental and require Nightly compiler.

## License

The crate is licensed under either of:
Expand Down
8 changes: 4 additions & 4 deletions sha2/src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ cfg_if::cfg_if! {
use riscv_zknh_compact::compress;
} else if #[cfg(target_arch = "aarch64")] {
mod soft;
mod aarch64;
use aarch64::compress;
mod aarch64_sha2;
use aarch64_sha2::compress;
} else if #[cfg(target_arch = "loongarch64")] {
mod loongarch64_asm;
use loongarch64_asm::compress;
} else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
mod wasm32;
use wasm32::compress;
mod wasm32_simd128;
use wasm32_simd128::compress;
} else {
mod soft;
use soft::compress;
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions sha2/src/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ cfg_if::cfg_if! {
use riscv_zknh_compact::compress;
} else if #[cfg(target_arch = "aarch64")] {
mod soft;
mod aarch64;
use aarch64::compress;
mod aarch64_sha2;
use aarch64_sha2::compress;
} else if #[cfg(target_arch = "loongarch64")] {
mod loongarch64_asm;
use loongarch64_asm::compress;
} else if #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))] {
mod wasm32;
use wasm32::compress;
mod wasm32_simd128;
use wasm32_simd128::compress;
} else {
mod soft;
use soft::compress;
Expand Down
File renamed without changes.
File renamed without changes.
Loading