fast_chacha

Crates.iofast_chacha
lib.rsfast_chacha
version0.3.0
created_at2025-05-28 02:34:13.193974+00
updated_at2025-06-04 13:59:09.76541+00
descriptionCPU-optimized ChaCha20 implementation with rust fallbacks
homepagehttps://github.com/sh0rch/fast_chacha
repositoryhttps://github.com/sh0rch/fast_chacha
max_upload_size
id1692251
size622,970
(sh0rch)

documentation

https://docs.rs/fast_chacha

README

fast_chacha

Crates.io docs.rs License: Apache-2.0 TESTS

High-performance ChaCha20 stream cipher implementation with optional assembly acceleration (leveraging OpenSSL's assembler modules) and pure Rust fallback.


Features

  • OpenSSL Assembly Modules: Integrates optimized assembly routines sourced from OpenSSL for top-tier performance.
  • Pure Rust Fallback: Portable implementation when assembly is not supported on the target.
  • Runtime CPU Detection: Automatically selects the fastest backend at runtime.
  • no_std Support: Works in embedded and bare-metal environments (disable default std).

Installation

Add fast_chacha to your Cargo.toml:

[dependencies]
fast_chacha = "0.1.0"

By default, the std feature is enabled. To use in no_std environments:

[dependencies]
fast_chacha = { version = "0.1.0", default-features = false }

Usage

Basic example:

use fast_chacha::FastChaCha20;

let key = [0u8; 32];
let nonce = [0u8; 12];
let mut data = b"Secret message!".to_vec();

// Encrypt or decrypt in-place
let mut cipher = FastChaCha20::new(&key, &nonce);
cipher.apply_keystream(&mut data);

If you need to force the pure Rust fallback implementation:

let mut data = b"Secret message!".to_vec();
let mut cipher = FastChaCha20::new(&key, &nonce);
cipher.apply_keystream_pure(&mut data);

API Documentation

Full documentation is available on docs.rs:


Build & Assembly Acceleration

The build script (build.rs) will compile and link optimized assembly implementations when available. This crate integrates assembly modules directly from OpenSSL, ensuring battle-tested, platform-specific routines. If no assembly is detected for your architecture, the crate falls back to the pure Rust version and emits a warning:

cargo:warning=fast_chacha: no ASM for <target>

Conditional compilation flag fast_chacha_asm is enabled when assembly (from OpenSSL) is used.


Benchmark

Here is a sample result of the comparison test for encrypting a 1 MiB block:

chacha20 (RustCrypto)   : 111.8584ms
fast_chacha ("ASM")     : 547.7µs
fast_chacha (Fallback)  : 14.6214ms
chacha6 (Fallback)      : 9.4224ms

Actual results of tests you can see on Github Actions page.

  • fast_chacha ("ASM") is significantly faster than both the pure Rust fallback and the reference RustCrypto implementation.
  • The pure Rust fallback is also noticeably faster than RustCrypto's chacha20.

Note: Actual performance may vary depending on your CPU and platform.


License

Licensed under the Apache License, Version 2.0 © 2025 sh0rch sh0rch@iwl.dev


Contributing

Contributions, issues, and feature requests are welcome! Please open an issue or pull request on GitHub:

https://github.com/sh0rch/fast_chacha


Acknowledgements

Based on the original ChaCha20 specification (RFC 8439) and assembly modules from OpenSSL.

Commit count: 10

cargo fmt